private static bool GenericPrimitiveGenotypeTest <T>(ArithmeticCrossover crossover, TestCase <T> testCase) where T : struct
        {
            var longestGenotype  = testCase.Parents.Select(x => x.Length).Max();
            var shortestGenotype = testCase.Parents.Select(x => x.Length).Min();

            Assert.AreEqual(longestGenotype, shortestGenotype);

            var expectedGenotypeLength = testCase.Parents[0].Length;

            var children = crossover.Cross(testCase.Parents);

            // Arithmetic Crossover must return only one child
            Assert.AreEqual(1, children.Length);

            var childGenotype = children[0];

            Assert.NotNull(childGenotype);

            var typedGenotype = childGenotype as GenericPrimitiveGenotype <T>;

            Assert.NotNull(typedGenotype);

            Assert.AreEqual(expectedGenotypeLength, typedGenotype.Length);

            Assert.AreEqual(testCase.ExpectedValue, typedGenotype.Value);

            return(true);
        }
Example #2
0
        public static void Main(string[] args)
        {
            if (args.Length < 1)
            {
                Console.Error.WriteLine("You have to at least specify the data file path");
                return;
            }

            var mutation  = new UniformMutation(MutationProbability);
            var selection = new KTournamentSelection(TournamentSize);
            var crossover = new ArithmeticCrossover();

            var fitnessFunction = new FitnessFunction(args[0]);

            IGeneticAlgorithm <DecimalArrayChromosome> geneticAlgorithm;

            if (args.Length == 2 && args[1].ToLower() == "gen")
            {
                geneticAlgorithm = new GenerationGeneticAlgorithm(mutation, selection, crossover, fitnessFunction,
                                                                  IterationLimit, FitnessTerminator, PopulationSize);
            }
            else
            {
                geneticAlgorithm = new EliminationGeneticAlgorithm(mutation, selection, crossover, fitnessFunction,
                                                                   IterationLimit, FitnessTerminator, PopulationSize);
            }

            var optimum = geneticAlgorithm.FindOptimum();

            Console.WriteLine();
            Console.WriteLine(optimum);
        }
 public void Setup()
 {
     _singleArithmeticCrossover = new ArithmeticCrossover(new [] { ArithmeticCrossover.EMode.Single });
 }