public void GivenListOfIndividuals_WhenTheDifference_ItShouldReturnAnIndividualWithEachDimensionBeingTheDifferenceBetweenTheSameDimensionInAllTheIndividualsInTheList()
        {
            Individual individual = new Individual(null, new Configuration())
            {
                Position = new double[] { 1.0, 2.0, 4.0 }
            };

            Individual individual2 = new Individual(null, new Configuration())
            {
                Position = new double[] { 3.0, 3.0, 3.0 }
            };

            Individual individual3 = new Individual(null, new Configuration())
            {
                Position = new double[] { 4.0, 5.0, 6.0 }
            };

            TrialIndividualMutationStrategy generator = new TrialIndividualMutationStrategy(new Configuration());
            Individual result = generator.GetDifference(new List <Individual>()
            {
                individual, individual2, individual3
            });

            Assert.AreEqual(result.Position[0], -6);
            Assert.AreEqual(result.Position[1], -6);
            Assert.AreEqual(result.Position[2], -5);
        }
        public void GivenThreeIndividuals_WhenTheTrialIndividualIsGenerated_ItShouldReturnTheTargetIndividualAddedToTheScaledDifferenceOfTheOtherIndividuals()
        {
            Individual target = new Individual(null, new Configuration())
            {
                Position = new double[] { 1.0, 2.0, 3.0 }
            };

            Individual individual1 = new Individual(null, new Configuration())
            {
                Position = new double[] { 9.0, 11.0, 11.0 }
            };

            Individual individual2 = new Individual(null, new Configuration())
            {
                Position = new double[] { 6.0, 7.0, 8.0 }
            };

            TrialIndividualMutationStrategy generator = new TrialIndividualMutationStrategy(new Configuration());
            Individual result = generator.GetTrialVector(target, new List <Individual>()
            {
                individual1, individual2
            });

            Assert.AreEqual(result.Position[0], 2.5);
            Assert.AreEqual(result.Position[1], 4.0);
            Assert.AreEqual(result.Position[2], 4.5);
        }
Example #3
0
        static void Main(string[] args)
        {
            IConfiguration             configuration = new Configuration();
            IMutationStrategy          trialVectorMutationStrategy       = new TrialIndividualMutationStrategy(configuration);
            ICrossoverStrategy         crossoverStrategy                 = new BinomialCrossoverStrategy(configuration);
            ISelectionStrategy         generationSelectionStrategy       = new MinimisationElitistSelectionStrategy();
            ISelectionStrategy         differenceVectorSelectionStrategy = new RandomSelectionStrategy();
            IFitnessEvaluationStrategy fitnessEvaluationStrategy         = new RastriginFitnessEvaluationStrategy();

            DifferentialEvolution differentialEvolution = new DifferentialEvolution(
                trialVectorMutationStrategy,
                crossoverStrategy,
                generationSelectionStrategy,
                differenceVectorSelectionStrategy,
                fitnessEvaluationStrategy,
                configuration
                );

            differentialEvolution.Run();

            var population = differentialEvolution.population;

            foreach (Individual individual in population)
            {
                foreach (double dimension in individual.Position)
                {
                    Console.Write(Math.Round(dimension) + ", ");
                }

                Console.WriteLine();
            }

            Console.Read();
        }
        public void GivenAnIndividual_WhenTheIndividualIsScaled_ItShouldReturnAnIndividualWithEachDimensionScaled()
        {
            Individual individual = new Individual(null, new Configuration())
            {
                Position = new double[] { 1.0, 2.0, 4.0 }
            };

            TrialIndividualMutationStrategy generator = new TrialIndividualMutationStrategy(new Configuration());
            Individual result = generator.Scale(individual, 0.5);

            Assert.AreEqual(result.Position[0], 0.5);
            Assert.AreEqual(result.Position[1], 1.0);
            Assert.AreEqual(result.Position[2], 2.0);
        }