Beispiel #1
0
        public static void Main(String[] args)
        {
            GeneticSelectionProcess <Population <TestIndividual <Boolean>, Boolean>, TestIndividual <Boolean>,
                                     Chromosome <Boolean>, Boolean, ProcessInformation> .Builder builder =
                new GeneticSelectionProcess <Population <TestIndividual <Boolean>, Boolean>, TestIndividual <Boolean>,
                                             Chromosome <Boolean>, Boolean, ProcessInformation> .Builder();

            Random rng = new Random();

            Int32 genomeSize            = 100;
            Int32 targetFitness         = 90;
            Int32 fitnessDeltaTolerance = 10;

            Int32  populationSize = 1000;
            Double mutationRate   = 0.01;

            GeneticSelectionProcess <Population <TestIndividual <Boolean>, Boolean>, TestIndividual <Boolean>,
                                     Chromosome <Boolean>, Boolean, ProcessInformation> process = builder
                                                                                                  .SetInitialPopulationSize(populationSize).UseDefaultConsoleLogger()
                                                                                                  .SetProcessInformationComposer(
                new ProcessInformationComposer <Population <TestIndividual <Boolean>, Boolean>, TestIndividual <Boolean>
                                                , Boolean>())
                                                                                                  .SetMatingFunction(
                new CrossOverMatingFunction <TestIndividual <Boolean>, Chromosome <Boolean>, Boolean>(3))
//                    new RandomCombinationMatingFunction<
//                        TestIndividual<Boolean>
//                        , Chromosome<Boolean>
//                        , Boolean>())
                                                                                                  .SetParentSelector(
                new SingleGroupFittestParentsSelector <Population <TestIndividual <Boolean>, Boolean>,
                                                       TestIndividual <Boolean>, Boolean>()).SetFitnessFunction(new TestFitnessFunction())
                                                                                                  .SetSurvivorSelector(
                new ReplaceLeastFitSelector <Population <TestIndividual <Boolean>, Boolean>, TestIndividual <Boolean>,
                                             Boolean>())
                                                                                                  .SetPopulationInitializer(
                new BooleanRandomPopulationInitializer <Population <TestIndividual <Boolean>, Boolean>,
                                                        TestIndividual <Boolean>, Chromosome <Boolean> >(genomeSize))
                                                                                                  .AddTerminationCondition(
                new FitnessTerminator <Population <TestIndividual <Boolean>, Boolean>, TestIndividual <Boolean>, Boolean
                                       >(targetFitness))
                                                                                                  .AddPopulationModifier(
                new RandomMutationModifier <Population <TestIndividual <Boolean>, Boolean>, TestIndividual <Boolean>,
                                            Boolean>(mutationRate, () => rng.Next(2) == 1)).Build();

            process.Initialize();
            process.RunUntilConvergence();

            IndividualWithFitness <TestIndividual <Boolean>, Boolean> fittest = process.GetPopulation().GetFittest();
            Double fittestFitness = fittest.GetFitness();

            Boolean[] fittestGenome = fittest.GetIndividual().GetChromosome().GetGenome();

            Console.WriteLine($"Max fitness {fittestFitness}");
            Console.WriteLine($"Genome {string.Join("", fittestGenome.Select(b => b ? 1 : 0))}");
            Console.WriteLine("Done");
        }
Beispiel #2
0
        public void TrivialMaximizeTruesProcessShouldRun()
        {
            Random rng = new Random();

            Int32  genomeSize     = 100;
            Int32  targetFitness  = 90;
            Int32  populationSize = 100;
            Double mutationRate   = 0.01;

            var matingFunctions = new List <IMatingFunction <TestIndividual <Boolean>, Chromosome <Boolean>, Boolean> >
            {
                new CrossOverMatingFunction <TestIndividual <Boolean>, Chromosome <Boolean>, Boolean>(1)
                , new CrossOverMatingFunction <TestIndividual <Boolean>, Chromosome <Boolean>, Boolean>(3)
                , new RandomCombinationMatingFunction <TestIndividual <Boolean>, Chromosome <Boolean>, Boolean>()
            };

            foreach (IMatingFunction <TestIndividual <Boolean>, Chromosome <Boolean>, Boolean> matingFunction in
                     matingFunctions)
            {
                var builder =
                    new GeneticSelectionProcess <Population <TestIndividual <Boolean>, Boolean>, TestIndividual <Boolean>,
                                                 Chromosome <Boolean>, Boolean, ProcessInformation> .Builder();

                var process = builder.SetInitialPopulationSize(populationSize).UseDefaultConsoleLogger()
                              .SetProcessInformationComposer(
                    new ProcessInformationComposer <Population <TestIndividual <Boolean>, Boolean>,
                                                    TestIndividual <Boolean>, Boolean>()).SetMatingFunction(matingFunction)
                              .SetParentSelector(
                    new SingleGroupFittestParentsSelector <Population <TestIndividual <Boolean>, Boolean>,
                                                           TestIndividual <Boolean>, Boolean>()).SetFitnessFunction(new TestFitnessFunction())
                              .SetSurvivorSelector(
                    new ReplaceLeastFitSelector <Population <TestIndividual <Boolean>, Boolean>,
                                                 TestIndividual <Boolean>, Boolean>())
                              .SetPopulationInitializer(
                    new BooleanRandomPopulationInitializer <Population <TestIndividual <Boolean>, Boolean>,
                                                            TestIndividual <Boolean>, Chromosome <Boolean> >(genomeSize))
                              .AddTerminationCondition(
                    new FitnessTerminator <Population <TestIndividual <Boolean>, Boolean>, TestIndividual <Boolean>,
                                           Boolean>(targetFitness))
                              .AddPopulationModifier(
                    new RandomMutationModifier <Population <TestIndividual <Boolean>, Boolean>, TestIndividual <Boolean>
                                                , Boolean>(mutationRate, () => rng.Next(2) == 1)).Build();

                process.Initialize();
                process.RunUntilConvergence();

                IndividualWithFitness <TestIndividual <Boolean>, Boolean> fittest = process.GetPopulation().GetFittest();
                Double    fittestFitness = fittest.GetFitness();
                Boolean[] fittestGenome  = fittest.GetIndividual().GetChromosome().GetGenome();

                Double expectedFitness = fittestGenome.Select(b => b ? 1 : 0).Sum();

                Assert.True(fittestFitness >= 90);
                Assert.True(expectedFitness >= 90);
                Assert.True(expectedFitness.AreDoublesEqual(fittestFitness));

                IList <ProcessInformation> history = process.GetHistory();
                Assert.True(history.Count > 1);
                Assert.True(fittestFitness.AreDoublesEqual(history[history.Count - 1].FittestFitness));
            }
        }