Esempio n. 1
0
        public double FindMin(IFunction fun)
        {
            List <double[]> nests = new List <double[]>();

            for (int i = 0; i < nestCount; i++)
            {
                nests.Add(RandGenerator.RandomArray(dimension, Space[0], Space[1]));
            }

            for (int i = 0; i < iteration; i++)
            {
                int      NestIndex    = rand.Next(0, nests.Count);
                double[] flightResult = Move(nests[NestIndex], iterationWalk);
                int      cNestIndex   = ChooseNest(NestIndex, nests);

                if (fun.Calculate(flightResult) <= fun.Calculate(nests[cNestIndex]))
                {
                    nests[cNestIndex] = flightResult;
                }

                nests = nests.OrderBy(x => fun.Calculate(x)).ToList();
                Kill(nests, survivaleRate);

                for (int j = nests.Count; j < nestCount; j++)
                {
                    nests.Add(RandGenerator.RandomArray(dimension, Space[0], Space[1]));
                }
            }

            nests = nests.OrderBy(x => fun.Calculate(x)).ToList();
            return(fun.Calculate(nests[0]));
        }
Esempio n. 2
0
        public double FindMin(IFunction function)
        {
            double[] currentState = RandGenerator.RandomArray
                                        (dimensions, searchSpace[0], searchSpace[1]);

            double T     = maxTemp;
            double alpha = 0.999;

            for (int i = 0; i < iterTreshold; i++)
            {
                double[] newState = new double[dimensions];
                currentState.CopyTo(newState, 0);
                for (int j = 0; j < dimensions; j++)
                {
                    newState[j] += RandGenerator.RandomNormal(-1.0, 1.0, 1) * T;
                }
                if (AcceptanceProb(function.Calculate(currentState), function.Calculate(newState), T) >= rand.NextDouble())
                {
                    currentState = newState;
                }

                T *= alpha;
            }
            return(function.Calculate(currentState));
        }
Esempio n. 3
0
        public double FindMin(IFunction fun)
        {
            List <Lift> population = new List <Lift>();

            for (int i = 0; i < populations; i++)
            {
                Lift l = new Lift(RandGenerator.RandomArray(dimension, Space[0], Space[1]));
                population.Add(l);
            }

            double[] bestPos = RandGenerator.RandomArray(dimension, Space[0], Space[1]);
            double   best    = fun.Calculate(bestPos);

            for (int l = 0; l < generations; l++)
            {
                for (int i = 0; i < populations; i++)
                {
                    for (int j = 0; j < populations; j++)
                    {
                        if (i == j)
                        {
                            continue;
                        }

                        if (fun.Calculate(population[j].Pos) < fun.Calculate(population[i].Pos))
                        {
                            double dist = Calculate(population[i], population[j]);

                            double[] Position = new double[dimension];

                            population[i].Pos.CopyTo(Position, 0);

                            for (int k = 0; k < dimension; k++)
                            {
                                Position[k] += (population[j].Pos[k] - population[i].Pos[k]) * dist;
                                Position[k] += ((random.NextDouble() - 0.5) * rndFactor);
                            }
                            if (fun.Calculate(Position) < fun.Calculate(population[i].Pos))
                            {
                                population[i].Pos = Position;
                            }
                        }
                    }
                    double[] pos = population.Aggregate((x, y) => fun.Calculate(x.Pos) < fun.Calculate(y.Pos) ? x : y).Pos;
                    if (fun.Calculate(pos) < best)
                    {
                        bestPos = pos;
                        best    = fun.Calculate(bestPos);
                    }
                }
            }
            return(fun.Calculate(bestPos));
        }
Esempio n. 4
0
        //protected void testResult(int nFunctionDimension, int mFunctionDimension, MeasuredPoint[] measuredPoints, Func<double[], double[]> func)
        public void testResult(int nFunctionDimension, int mFunctionDimension, Func <double[], double[]> func)
        {
            IFunction model = (IFunction)approx;

            Console.WriteLine("\n\nTest result approximation");
            for (int i = 1; i < 10; i += 2)
            {
                double[] point  = new double[nFunctionDimension + mFunctionDimension];
                double[] pointX = new double[nFunctionDimension];
                double[] pointY = new double[mFunctionDimension];

                for (int j = 0; j < nFunctionDimension; j++)
                {
                    point[j]  = model.Min[j] + ((model.Max[j] - model.Min[j]) * i / 10);
                    pointX[j] = point[j];
                }
                model.Calculate(point);

                for (int k = 0; k < mFunctionDimension; ++k)
                {
                    pointY[k] = point[nFunctionDimension + k];
                }

                double[] realPointY = func(point);

                double absErr = realPointY.Zip(pointY, (d1, d2) => Math.Abs(d1 - d2)).ToArray().Sum() / realPointY.Length;

                Console.WriteLine("Vector " + String.Join(", ", pointX) + " Approximation result: " + String.Join(", ", pointY) +
                                  ", real: " + String.Join(", ", realPointY) + ", err: " + absErr);
            }
        }
Esempio n. 5
0
 public override void Execute(IFunction function, ResultRecord result) {
     double value;
     var votes = GetVotes().ToList();
     function.Calculate(votes, result.ContentItemRecord.Id, out value);
     result.Value = value;
     result.Count = votes.Count;
 }
Esempio n. 6
0
        /// <summary>
        /// Peform the function execution
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="function"></param>
        /// <returns></returns>
        private static object Execute <T>(IFunction <T> function)
        {
            function.ResolveInputs();

            var rawOutput = function.Calculate();

            return(function.Render(rawOutput));
        }
Esempio n. 7
0
        /// <summary>
        /// GetColor
        /// </summary>
        public Color GetColor(List <double> values, double alpha)
        {
            Color color;

            var results = function.Calculate(values);

            if (results != null)
            {
                color = alternateColorSpace.GetColor(results, alpha);
            }
            else
            {
                byte gray = 0xB0;
                color = Color.FromArgb((byte)(alpha * 255), gray, gray, gray);
            }

            return(color);
        }
Esempio n. 8
0
        public override void Execute(IFunction function, ResultRecord result)
        {
            double value;
            var    votes = GetVotes().ToList();

            function.Calculate(votes, result.ContentItemRecord.Id, out value);
            result.Value = value;
            result.Count = votes.Count;
        }
Esempio n. 9
0
 void PrintFunction(double start, double end, double step, IFunction function)
 {
     for (double x = start; x <= end; x += step)
     {
         try
         {
             PrintResult(x, function.Calculate(x), function);
         }
         catch (Exception err)
         {
             PrintErrorMessage(x, err.Message, function);
         }
     }
 }
        public double Calculate(double x, double t)
        {
            double omega = _parameterProvider.GetValue("omega");
            double beta  = _parameterProvider.GetValue("beta");
            double res   = 0;

            for (int i = 0; i <= N; ++i)
            {
                res += _eFund.Calculate(i, x) * Math.Sqrt(omega)
                       * _laguerreFunc.Calculate(i, omega * t) * Math.Exp(-(beta / 2.0) * t);
            }

            return(res);
        }
Esempio n. 11
0
        public double FindMin(IFunction fun)
        {
            List<Molecule> molecules = new List<Molecule>();
            double[] bestSwarmPosition = RandGenerator.RandomArray(dimension, Space[0], Space[1]);


            for (int i = 0; i < moleculesCount; i++)
            {
                molecules.Add(
                    new Molecule(
                        RandGenerator.RandomArray(dimension, Space[0], Space[1]), 
                        RandGenerator.RandomArray(dimension, -1.0, 1.0))
                    );

                if (fun.Calculate(molecules.Last().bestPosition) < fun.Calculate(bestSwarmPosition))
                    bestSwarmPosition = molecules.Last().bestPosition;
                
            }

            for (int i = 0; i < iterarion; i++)
            {
                foreach (Molecule p in molecules)
                {
                    for (int j = 0; j < dimension; j++)
                    {
                        double randomPositionFactor = random.NextDouble();
                        double randomSwarmFactor = random.NextDouble();

                        p.velocity[j] = (p.velocity[j] * velocity) + bestPositionFactor * randomPositionFactor * 
                            (p.bestPosition[j] - p.position[j]) +bestSwarmPos * randomSwarmFactor * (bestSwarmPosition[j] - p.position[j]);
                    }

                    for (int j = 0; j < dimension; j++) p.position[j] += p.velocity[j];
                    
                    if (fun.Calculate(p.position) < fun.Calculate(p.bestPosition))  p.bestPosition = p.position;
                    if (fun.Calculate(p.position) < fun.Calculate(bestSwarmPosition)) bestSwarmPosition = p.position;
                      
                }
            }
            return fun.Calculate(bestSwarmPosition);
        }
Esempio n. 12
0
        public void DrawCommand2(Graphics g)
        {
            CreateFromConfigs2();

            var x1 = double.Parse(X1);
            var x2 = double.Parse(X2);
            var dx = double.Parse(Dx);
            var t  = double.Parse(T);

            for (double x = x1; x <= x2; x += dx)
            {
                try
                {
                    var point = GetPointAt(x, _eSumSeries.Calculate(x, t));
                    g.FillEllipse(new SolidBrush(Color.Black), new Rectangle(point.X - 2, point.Y - 2, 4, 4));
                    point = GetPointAt(x, _eAnalytical.Calculate(x, t));
                    g.FillEllipse(new SolidBrush(Color.Red), new Rectangle(point.X - 2, point.Y - 2, 4, 4));
                }
                catch (Exception ex)
                {
                    Trace.WriteLine($"Drawing point failed: {ex.Message}");
                }
            }
        }
Esempio n. 13
0
        public void testResult(Func <double[], double[]> func)
        {
            Console.WriteLine("\n\nTest result approximation");
            //Shepard model = new Shepard(functionDimension, points);
            IFunction model = (IFunction)approx;

            for (int i = 1; i < 10; i += 2)
            {
                double[] a = new double[config.FunctionDimension + config.DependentVariablesNum];
                double[] b = new double[config.FunctionDimension + config.DependentVariablesNum];
                for (int j = 0; j < config.FunctionDimension; j++)
                {
                    a[j] = model.Min[j] + ((model.Max[j] - model.Min[j]) * i / 10);
                    b[j] = a[j];
                }
                double[] results = new double[config.DependentVariablesNum];
                for (int j = 0; j < config.DependentVariablesNum; j++)
                {
                    results[j] = a[j + config.FunctionDimension];
                }
                model.Calculate(a);
                Console.WriteLine("Vector " + String.Join(", ", b) + " Approximation result " + String.Join(", ", results) + " real " + String.Join(", ", func(b)) + " error " + approx.GetError(results, func(b)));
            }
        }
Esempio n. 14
0
 public void CalculateOutput()
 {
     Output = Type == NeuronType.Input
         ? Inputs.First()
         : Function.Calculate(Inputs.Select((t, i) => t * Weights[i]).Sum() + Bias);
 }
Esempio n. 15
0
 public void Calculate(double[] xy)
 {
     func.Calculate(xy);
 }
Esempio n. 16
0
 public IOutBlackBoxParam Function(Double x1, Double x2)
 {
     calls++;
     return(function.Calculate(x1, x2));
 }
Esempio n. 17
0
        private double[] build_features(double[] point, IFunction model, Grid grid, double[] distToKnownPoints, double[][] knownPoints = null, int index = -1)
        {
            // на сколько образующая домен точка близка
            // сколько до и после монотонно
            // расстояние до известной точки
            Analyzer analyzer = new Analyzer(model, knownPoints);

            analyzer.do_some_analyse();
            // min, max in locality
            double maxNeighbours = double.MinValue;
            double minNeighbours = double.MaxValue;

            foreach (var neighbour in grid.Neighbours(index))
            {
                double[] calcNeighbour = (double[])grid.Node[neighbour].Clone();
                model.Calculate(calcNeighbour);
                double calcNeighbourVal = calcVal(model.M, model.N, calcNeighbour);

                if (calcNeighbour[calcNeighbour.Length - 1] < minNeighbours)
                {
                    minNeighbours = calcNeighbourVal;
                }
                if (calcNeighbour[calcNeighbour.Length - 1] > maxNeighbours)
                {
                    maxNeighbours = calcNeighbourVal;
                }
            }
            // current val
            double[] curentNode = (double[])grid.Node[index].Clone();
            model.Calculate(curentNode);
            double curentNodeVal = calcVal(model.M, model.N, curentNode);

            if (curentNodeVal < minNeighbours)
            {
                minNeighbours = curentNodeVal;
            }
            if (curentNodeVal > maxNeighbours)
            {
                maxNeighbours = curentNodeVal;
            }

            List <double[]> temp_points = new List <double[]>();

            temp_points = knownPoints.ToList();
            temp_points.RemoveAt(analyzer.Domain(grid.Node[index]));
            ShepardApprox new_model = new ShepardApprox(model.N, temp_points.ToArray());

            double[] old_model_point = (double[])grid.Node[index].Clone();
            model.Calculate(old_model_point);
            double[] new_model_point = (double[])grid.Node[index].Clone();
            new_model.Calculate(new_model_point);

            double err = 0;

            for (int k = 0; k < model.M; ++k)
            {
                double newErr = Math.Abs(old_model_point[model.N + k] - new_model_point[model.N + k]);
                if (newErr > err)
                {
                    err = newErr;
                }
            }
            this.featureCount = 4;
            double[] features = new double[featureCount];
            features[0] = maxNeighbours - curentNodeVal;
            features[1] = curentNodeVal - minNeighbours;
            features[2] = distToKnownPoints[index];
            features[3] = err;

            return(features);
        }