Esempio n. 1
0
        private static void RunGeneticOptimizer()
        {
            //optimizer variables
            var variables = new List <GeneticOptimizerVariable>()
            {
                new GeneticOptimizerVariable("x", 4, -10, 10)
            };

            //optimizer configuration
            var configuration = new GeneticOptimizerConfiguration("y", variables, 1);

            //objective function
            var objectiveFunction = new Func <double[], double>(inputs =>
            {
                Console.WriteLine("objectiveFunction");
                Thread.Sleep(1000);

                var objectiveFunctionResult = Math.Pow(inputs[0], 3) / Math.Exp(Math.Pow(inputs[0], 0.8));
                return(objectiveFunctionResult);
            });

            //optimizer
            var optimizer = new GeneticOptimizer(configuration, objectiveFunction, GenerationRanCallback);

            var watch = new Stopwatch();

            watch.Start();

            optimizer.Start();

            watch.Stop();

            Console.WriteLine($"Number milliseconds: {watch.ElapsedMilliseconds}");


            Console.WriteLine($"Press Key to quit");
            Console.ReadLine();
        }
Esempio n. 2
0
        public GeneticOptimizer(GeneticOptimizerConfiguration configuration, Func <double[], double> objectiveFunction, Action <string> generationRanCallback = null)
        {
            //store configuration
            _configuration         = configuration;
            _generationRanCallback = generationRanCallback;

            //set min/max/precision of input variables
            var minValues      = new double[_configuration.Variables.Count];
            var maxValues      = new double[_configuration.Variables.Count];
            var fractionDigits = new int[_configuration.Variables.Count];

            for (int index = 0; index < _configuration.Variables.Count; index++)
            {
                minValues[index]      = _configuration.Variables[index].MinimumValue;
                maxValues[index]      = _configuration.Variables[index].MaximumValue;
                fractionDigits[index] = _configuration.Variables[index].NumberDigitsPrecision;
            }

            //total bits
            var totalBits = new int[] { 64 };

            //chromosome
            var chromosome = new FloatingPointChromosome(minValues, maxValues, totalBits, fractionDigits);

            //population
            var population = new Population(MinimumNumberPopulation, MaximumNumberPopulation, chromosome);

            //set fitness function
            var fitnessFunction = new FuncFitness(c =>
            {
                var fc     = c as FloatingPointChromosome;
                var inputs = fc.ToFloatingPoints();
                var result = objectiveFunction(inputs);

                //add to results
                if (!Double.IsNaN(result))
                {
                    var list = inputs.ToList();
                    list.Add(result);

                    _result.IterationArray.Add(string.Join(",", list));
                }

                return(result);
            });

            //other variables
            var selection   = new EliteSelection();
            var crossover   = new UniformCrossover(0.5f);
            var mutation    = new FlipBitMutation();
            var termination = new FitnessThresholdTermination();

            _algorithm = new GeneticAlgorithm(population, fitnessFunction, selection, crossover, mutation)
            {
                Termination = termination,
            };

            //task parallelism
            var taskExecutor = new ParallelTaskExecutor();

            taskExecutor.MinThreads = 1;
            taskExecutor.MaxThreads = _configuration.NumberThreadsToUse;
            _algorithm.TaskExecutor = taskExecutor;

            //if (_configuration.NumberThreadsToUse > 1)
            //{
            //    var taskExecutor = new ParallelTaskExecutor();
            //    taskExecutor.MinThreads = 1;
            //    taskExecutor.MaxThreads = _configuration.NumberThreadsToUse;
            //    _algorithm.TaskExecutor = taskExecutor;
            //}

            //register generation ran callback
            _algorithm.GenerationRan += AlgorithmOnGenerationRan;
        }