Пример #1
0
        public static void Run()
        {
            int seed = 1;
            var rng  = new Random(seed);

            OptimizationProblem optimizationProblem = new Rosenbrock();

            var builder = new DifferentialEvolutionAlgorithm.Builder(optimizationProblem);

            builder.PopulationSize        = 100;
            builder.MutationFactor        = 0.6;
            builder.CrossoverProbability  = 0.9;
            builder.ConvergenceCriterion  = new MaxFunctionEvaluations(100000);
            builder.RandomNumberGenerator = rng;
            IOptimizationAlgorithm de = builder.Build();

            IOptimizationAnalyzer analyzer = new OptimizationAnalyzer(de);

            analyzer.Optimize();

            double expectedFitness = 0.0;
            var    expectedDesign  = Vector.CreateWithValue(optimizationProblem.Dimension, 1.0);

            Assert.Equal(expectedFitness, de.BestFitness, 10);
            Assert.True(Vector.CreateFromArray(de.BestPosition).Equals(expectedDesign, 1E-6));
        }
Пример #2
0
        public void TwoDim_EvaluatesCorrectValues(double[] values)
        {
            var evaluator = new Rosenbrock(2);
            var ds        = evaluator.GetGlobalOptimum().GetDecisionSpace();
            var result    = evaluator.Evaluate(DecisionVector.CreateFromArray(ds, new[] { values[0], values[1] }));

            Assert.Equal(values[2], result.ElementAt(0));
        }
Пример #3
0
        //[Fact] // This algorithm is too slow for Rosenbrock test. It does converge though.
        public static void TestBinaryGA()
        {
            int seed = 1;
            var rng  = new StandardGenerator(seed);
            //var rng = RandomNumberGenerationUtilities.troschuetzRandom;

            // Define optim problem
            OptimizationProblem problem = new Rosenbrock();

            // Define optim algorithm
            var optimBuilder = new BinaryGeneticAlgorithmBuilder(problem);

            optimBuilder.Logger = new BestOfIterationLogger();

            // Setup logging
            //optimBuilder.Logger = new EmptyLogger();

            // Define convergence criteria
            optimBuilder.ConvergenceCriterion = new MaxFunctionEvaluations(1000000);

            // Define encoding
            optimBuilder.Encoding = new GrayCoding(problem, 16, 8);

            // Initialization
            optimBuilder.Initializer = new RealUniformRandomInitializer(problem, rng);

            // Define population size
            optimBuilder.PopulationSize = 100;

            // Define selection strategy
            //optimBuilder.Selection = new RouletteWheelSelection<bool>(new InverseRankScaling<bool>(0.5));
            //optimBuilder.Selection = new TournamentSelection<bool>(2, false);
            //optimBuilder.Selection = new StochasticUniversalSampling<bool>(new InverseRankScaling<bool>(2.0
            //optimBuilder.Selection = new RemainderStochasticSamplingWithReplacement<bool>(new InverseRankScaling<bool>(1.0));
            optimBuilder.Selection = new TruncationSelection <bool>(rng, 0.5);

            // Define recombination strategy
            optimBuilder.Recombination = new SinglePointCrossover <bool>(rng);

            // Define mutation strategy
            optimBuilder.Mutation = new BitFlipMutation(0.05, rng);

            // Start optimization
            GeneticAlgorithm <bool> optimAlgorithm = optimBuilder.BuildAlgorithm();

            optimAlgorithm.Solve();

            // Check results
            double expectedFitness = 0.0;
            var    expectedDesign  = Vector.CreateWithValue(problem.Dimension, 1.0);

            Assert.Equal(expectedFitness, optimAlgorithm.BestFitness, 10);
            Assert.True(Vector.CreateFromArray(optimAlgorithm.BestPosition).Equals(expectedDesign, 1E-6));
        }
Пример #4
0
        public void Evaluate()
        {
            Rosenbrock function = new Rosenbrock();

            Vector v1 = new Vector(new double[] { 1, 2, 3 });
            Vector v2 = new Vector(new double[] { 3, 2, 1 });
            Vector v3 = new Vector(new double[] { 1, 2, 3, 4 });

            Assert.AreEqual(201, function.Evaluate(v1));
            Assert.AreEqual(5805, function.Evaluate(v2));
            Assert.AreEqual(2705, function.Evaluate(v3));
        }
Пример #5
0
        public void CalculateParabolaMinimum()
        {
            int _dim=5;
            double []_data = {5,34,-12,55,6};
            double []_reference = {0,0,0,0,0};
            double _epsilon=1e-7;
            double _realEpsilon = System.Math.Sqrt(_epsilon);
            double _step=1000;
            int _itmax=1000;
            double diff;

            Parabola func = new Parabola(_dim);
            Rosenbrock objRosenbrock = new Rosenbrock(_dim, _data, func, _step, _epsilon, _itmax);
            objRosenbrock.FindMinimum();
            diff = NumericNet.CalcDiff(_dim,_reference,_data);
            Assert.IsTrue(diff<_realEpsilon);
        }
Пример #6
0
        public void CalculateBananaMinimum()
        {
            int _dim=2;
            double []_data = {5,34};
            double []_reference = {1,1};
            double _epsilon=1e-7;
            double _realEpsilon = System.Math.Sqrt(_epsilon);
            double _step=1000;
            int _itmax=1000;
            double diff;

            Banana func = new Banana();
            Rosenbrock objRosenbrock = new Rosenbrock(_dim, _data, func, _step, _epsilon, _itmax);
            objRosenbrock.FindMinimum();
            diff = NumericNet.CalcDiff(_dim,_reference,_data);
            Assert.IsTrue(diff<_realEpsilon);
        }
Пример #7
0
 private void SolveClick(object sender, RoutedEventArgs args)
 {
     if (worker.IsBusy)
     {
         MessageBox.Show("Обрабатывается предыдущий запрос. Пожалуйста, подождите");
         return;
     }
     if (function == null)
     {
         MessageBox.Show("Неверно задана функция");
         return;
     }
     rosenbrock = new Rosenbrock(function, new Vector(X, Y), Epsilon)
     {
         Direction = ToMin ? OptimizationDirection.ToMin : OptimizationDirection.ToMax
     };
     worker.RunWorkerAsync();
 }
Пример #8
0
        public Vector Solve()
        {
            details = new List <PenaltySolverDetails>();
            Vector x = startPoint;

            while (true)
            {
                var    r    = new Rosenbrock(Function, x, epsilon);
                Vector oldx = x;
                x = r.Solve();
                details.Add(new PenaltySolverDetails(mu, new SmartPoint(oldx), Math.Pow(Penalty(x), 2)));
                if (details.Last().MuAlpha < epsilon)
                {
                    break;
                }
                mu *= beta;
            }
            return(x);
        }
Пример #9
0
        public void CalculateBananaMinimum()
        {
            int _dim = 2;

            double [] _data        = { 5, 34 };
            double [] _reference   = { 1, 1 };
            double    _epsilon     = 1e-7;
            double    _realEpsilon = System.Math.Sqrt(_epsilon);
            double    _step        = 1000;
            int       _itmax       = 1000;
            double    diff;

            Banana     func          = new Banana();
            Rosenbrock objRosenbrock = new Rosenbrock(_dim, _data, func, _step, _epsilon, _itmax);

            objRosenbrock.FindMinimum();
            diff = NumericNet.CalcDiff(_dim, _reference, _data);
            Assert.IsTrue(diff < _realEpsilon);
        }
Пример #10
0
        public void CalculateParabolaMinimum()
        {
            int _dim = 5;

            double [] _data        = { 5, 34, -12, 55, 6 };
            double [] _reference   = { 0, 0, 0, 0, 0 };
            double    _epsilon     = 1e-7;
            double    _realEpsilon = System.Math.Sqrt(_epsilon);
            double    _step        = 1000;
            int       _itmax       = 1000;
            double    diff;

            Parabola   func          = new Parabola(_dim);
            Rosenbrock objRosenbrock = new Rosenbrock(_dim, _data, func, _step, _epsilon, _itmax);

            objRosenbrock.FindMinimum();
            diff = NumericNet.CalcDiff(_dim, _reference, _data);
            Assert.IsTrue(diff < _realEpsilon);
        }
Пример #11
0
        public static void ObliczIndiwFitness(Particle ind)
        {
            switch (FunctionName.type)
            {
            case FunctionName.Type.Schwefel:
                Schwefel.setFitness(ind);
                break;

            case FunctionName.Type.DeJong1:
                DeJong1.setFitness(ind);
                break;

            case FunctionName.Type.Rastrigin:
                Rastrigin.setFitness(ind);
                break;

            case FunctionName.Type.Rosenbrock:
                Rosenbrock.setFitness(ind);
                break;
            }
        }
Пример #12
0
        private static void Assignment2()
        {
            Console.WriteLine("--------------ASSIGNMENT [2]--------------------");
            Console.WriteLine("Starting vector 1:");
            var x1 = new DenseVector(new[] { -1.9, 2.0 });

            Console.WriteLine(x1);
            Console.WriteLine("Starting vector 2:");
            Console.WriteLine();
            var x2 = new DenseVector(new[] { 0.1, 0.3 });

            Console.WriteLine(x2);

            Console.WriteLine();
            var function1 = new Rosenbrock();

            Console.WriteLine(function1);
            Console.WriteLine();
            var function2 = new Function2();

            Console.WriteLine(function2);

            Console.WriteLine("ROSENBROCK ALGORITHMS: optimal gradient descent & Newton Raphson");
            var optimalDescentRosenbrock = Algorithms.GradientDescent(function1, x1);
            var newtonRaphsonRosenbrock  = Algorithms.NewtonRaphson(function1, x1);

            Console.WriteLine(optimalDescentRosenbrock);
            Console.WriteLine(newtonRaphsonRosenbrock);
            Console.WriteLine();

            Console.WriteLine("F2 ALGORITHMS: optimal gradient descent & Newton Raphson");
            var optimalDescentF2 = Algorithms.GradientDescent(function2, x2);
            var newtonRaphsonF2  = Algorithms.NewtonRaphson(function2, x2);

            Console.WriteLine(optimalDescentF2);
            Console.WriteLine(newtonRaphsonF2);
            Console.WriteLine("--------------------END----------------------");
        }
Пример #13
0
        private static void Assignment3()
        {
            Console.WriteLine("--------------ASSIGNMENT [3]--------------------");
            Console.WriteLine("Starting vector 1:");
            var x1 = new DenseVector(new[] { -1.9, 2.0 });

            Console.WriteLine(x1);
            Console.WriteLine("Starting vector 2:");
            Console.WriteLine();
            var x2 = new DenseVector(new[] { 0.1, 0.3 });

            Console.WriteLine(x2);

            Console.WriteLine();
            var function1 = new Rosenbrock();

            Console.WriteLine(function1);

            Console.WriteLine();

            var function2 = new Function2();

            Console.WriteLine(function2);
Пример #14
0
        internal void ObliczPopulFitness(FunctionName.Type type)
        {
            for (int j = 0; j < population.Count; j++)
            {
                switch (type)
                {
                case FunctionName.Type.DeJong1:
                    DeJong1.setFitness(population[j]);
                    break;

                case FunctionName.Type.Schwefel:
                    Schwefel.setFitness(population[j]);
                    break;

                case FunctionName.Type.Rastrigin:
                    Rastrigin.setFitness(population[j]);
                    break;

                case FunctionName.Type.Rosenbrock:
                    Rosenbrock.setFitness(population[j]);
                    break;
                }
            }
        }
Пример #15
0
        public static void Main(string[] args)
        {
            var param = new Parametrii();

            param.W  = 0.729;
            param.C1 = 1.49445;
            param.C2 = 1.49445;

            param.DimensiuneProblema = 3;

            // Rastrigin
            // param.Max = 5.12;
            // param.Min = -5.12;

            // Ackley
            // param.Min = -32.768;
            // param.Max = 32.768;

            // nGriewank
            param.Min = -600;
            param.Max = 600;

            param.NumarIteratii  = 1000;
            param.NumarParticule = 50;
            param.VitezaMaxima   = 1;

            ProblemaDeBaza p   = new Rastrigin();
            var            rez = p.Rezolva(param);

            Console.WriteLine("Cost: " + rez.Cost);
            foreach (var weight in rez.Pozitie)
            {
                Console.Write(weight + " ");
            }

            Console.WriteLine("\nRosenbrock");
            p   = new Rosenbrock();
            rez = p.Rezolva(param);

            Console.WriteLine("Cost: " + rez.Cost);
            foreach (var weight in rez.Pozitie)
            {
                Console.Write(weight + " ");
            }

            Console.WriteLine("\nAckley");
            p   = new Ackley();
            rez = p.Rezolva(param);

            Console.WriteLine("Cost: " + rez.Cost);
            foreach (var weight in rez.Pozitie)
            {
                Console.Write(weight + " ");
            }

            Console.WriteLine("\nGriewank");
            p   = new Griewank();
            rez = p.Rezolva(param);

            Console.WriteLine("Cost: " + rez.Cost);
            foreach (var weight in rez.Pozitie)
            {
                Console.Write(weight + " ");
            }
        }