Esempio n. 1
0
        public void Run()
        {
            // var populations = new int[]{30,50,100,200,500,1000,3000};

            // Population and tournament size box plot data made with following alogorithm

            var populationSizes = new int[] { 30, 50, 100, 200, 500, 1000, 3000 };

            for (int j = 0; j < 10; j++)
            {
                string result = "";
                for (int i = 0; i < populationSizes.Length; i++)
                {
                    var populationSize = populationSizes[i];
                    var problem        = new FunctionMinimizationFloatingPointProblem(new F6(), 2, -50, 150);
                    // var populationSize = 500;
                    var tournamentSize    = 3;
                    var selectionOperator = new TournamentSelectionOperator <FloatingPointChromosome>(tournamentSize, problem);
                    var crossoverOperator = new HeuristicCrossoverOperator(0.8);
                    var mutationOperator  = new GaussianMutationOperator(0.05);
                    var elitismRate       = 1.0 / populationSize;

                    var ga = new GeneticAlgorithmBuilder <FloatingPointChromosome>(
                        problem,
                        crossoverOperator,
                        mutationOperator
                        )
                             .SetGeneticAlgorithmVariant(GeneticAlgorithmVariant.Generational)
                             .SetPopulationSize(populationSize)
                             .SetElitismRate(1.0 / populationSize)
                             .Build();

                    // new GeneticAlgorithmRunner<FloatingPointChromosome>(ga).Run();
                    ga.Run();

                    result += ga.BestIndividual.Fitness;
                    if (i != populationSizes.Length - 1)
                    {
                        result += ",";
                    }
                }

                Console.WriteLine(result);
            }
        }
Esempio n. 2
0
        static void Main(string[] args)
        {
            // trying to make the word "abracadabra"

            var evaluator = new FitnessEvaluator("abracadabra");
            var random    = new Random();
            var generator = new Generator(random);

            const int Total = 100;

            var population = new Population <string>(Total, generator, evaluator);

            var fitnessDict = new Dictionary <Chromosome <string>, double>();

            var mutator = new Mutator();

            var crossOver = new SinglePointCrossOver(random, evaluator);

            var selector = new Selector(evaluator, 10, Total);

            var killer = new ChromosomeKiller(5);

            var solution = new Solution();

            var geneticAlgorithm = new GeneticAlgorithmBuilder <string>()
                                   .SetPopulation(population)
                                   .SetFitnessEvaluator(evaluator)
                                   .SetGenerations(150000)
                                   .SetMutationRate(0.05)
                                   .SetMutator(mutator)
                                   .SetCrossOver(crossOver)
                                   .SetSelection(selector)
                                   .SetKiller(killer)
                                   .SetSolution(solution)
                                   .Build();

            geneticAlgorithm.BeforeRun += GeneticAlgorithm_BeforeRun;

            geneticAlgorithm.AfterRun += GeneticAlgorithm_AfterRun;

            geneticAlgorithm.OnSolution += GeneticAlgorithm_OnSolution;

            geneticAlgorithm.Run();
        }