public void GivenAPopulationOfThree_WhenANewPopulationIsRetrieved_ItShouldCallTheEvaluateMethodThreeTimes()
        {
            Mock <IFitnessEvaluationStrategy> fitnessEvaluationMock     = new Mock <IFitnessEvaluationStrategy>();
            IFitnessEvaluationStrategy        fitnessEvaluationStrategy = fitnessEvaluationMock.Object;

            Mock <ICrossoverStrategy> crossoverMock = new Mock <ICrossoverStrategy>();

            crossoverMock.Setup(x => x.Cross(It.IsAny <Individual>(), It.IsAny <Individual>()))
            .Returns(new Individual(fitnessEvaluationStrategy, new Configuration()));
            ICrossoverStrategy crossoverStrategy = crossoverMock.Object;

            Mock <IMutationStrategy> mutationMock     = new Mock <IMutationStrategy>();
            IMutationStrategy        mutationStrategy = mutationMock.Object;

            Mock <ISelectionStrategy> selectionMock     = new Mock <ISelectionStrategy>();
            ISelectionStrategy        selectionStrategy = selectionMock.Object;

            DifferentialEvolution differentialEvolution = new DifferentialEvolution(mutationStrategy,
                                                                                    crossoverStrategy,
                                                                                    selectionStrategy,
                                                                                    selectionStrategy,
                                                                                    fitnessEvaluationStrategy,
                                                                                    new Configuration());

            differentialEvolution.population = new List <Individual>
            {
                new Individual(fitnessEvaluationStrategy, new Configuration()),
                new Individual(fitnessEvaluationStrategy, new Configuration()),
                new Individual(fitnessEvaluationStrategy, new Configuration())
            };

            differentialEvolution.GetNewPopulation();

            fitnessEvaluationMock.Verify(c => c.GetFitnessForIndividual(It.IsAny <Individual>()), Times.Exactly(3 * 2));
        }
        public void GivenADifferentialEvolutionAlgorithm_WhenTheAlgorithmIsRun_ItShouldResultInAPopulationWithAtLeastOneIndividual()
        {
            Mock <IFitnessEvaluationStrategy> fitnessEvaluationMock     = new Mock <IFitnessEvaluationStrategy>();
            IFitnessEvaluationStrategy        fitnessEvaluationStrategy = fitnessEvaluationMock.Object;

            Mock <ICrossoverStrategy> crossoverMock = new Mock <ICrossoverStrategy>();

            crossoverMock.Setup(x => x.Cross(It.IsAny <Individual>(), It.IsAny <Individual>()))
            .Returns(new Individual(fitnessEvaluationStrategy, new Configuration()));
            ICrossoverStrategy crossoverStrategy = crossoverMock.Object;

            Mock <IMutationStrategy> mutationMock     = new Mock <IMutationStrategy>();
            IMutationStrategy        mutationStrategy = mutationMock.Object;

            Mock <ISelectionStrategy> selectionMock = new Mock <ISelectionStrategy>();

            selectionMock.Setup(x => x.Select(It.IsAny <List <Individual> >()))
            .Returns(new Individual(fitnessEvaluationStrategy, new Configuration()));
            ISelectionStrategy selectionStrategy = selectionMock.Object;

            DifferentialEvolution differentialEvolution = new DifferentialEvolution(mutationStrategy,
                                                                                    crossoverStrategy,
                                                                                    selectionStrategy,
                                                                                    selectionStrategy,
                                                                                    fitnessEvaluationStrategy,
                                                                                    new Configuration());

            differentialEvolution.Run();

            Assert.IsTrue(differentialEvolution.population.Count > 0);
        }
        public void GivenFiveIterationsAndThreeIndividuals_WhenTheAlgorithmIsRun_ItShouldCallTheCrossoverStrategyFifteenTimes()
        {
            Mock <IFitnessEvaluationStrategy> fitnessEvaluationMock     = new Mock <IFitnessEvaluationStrategy>();
            IFitnessEvaluationStrategy        fitnessEvaluationStrategy = fitnessEvaluationMock.Object;

            Mock <ICrossoverStrategy> crossoverMock = new Mock <ICrossoverStrategy>();

            crossoverMock.Setup(x => x.Cross(It.IsAny <Individual>(), It.IsAny <Individual>()))
            .Returns(new Individual(fitnessEvaluationStrategy, new Configuration()));
            ICrossoverStrategy crossoverStrategy = crossoverMock.Object;

            Mock <IMutationStrategy> mutationMock     = new Mock <IMutationStrategy>();
            IMutationStrategy        mutationStrategy = mutationMock.Object;

            Mock <ISelectionStrategy> selectionMock = new Mock <ISelectionStrategy>();

            selectionMock.Setup(x => x.Select(It.IsAny <List <Individual> >()))
            .Returns(new Individual(fitnessEvaluationStrategy, new Configuration()));
            ISelectionStrategy selectionStrategy = selectionMock.Object;


            DifferentialEvolution differentialEvolution = new DifferentialEvolution(mutationStrategy,
                                                                                    crossoverStrategy,
                                                                                    selectionStrategy,
                                                                                    selectionStrategy,
                                                                                    fitnessEvaluationStrategy,
                                                                                    new Configuration());

            differentialEvolution.Run();

            crossoverMock.Verify(c => c.Cross(It.IsAny <Individual>(), It.IsAny <Individual>()), Times.Exactly(15));
        }
        public void GivenAPopulationOfOne_WhenANewPopulationIsRetrieved_ItShouldCallTheEvaluateMethodWithTheIndividualAsAParameter()
        {
            Mock <IFitnessEvaluationStrategy> fitnessEvaluationMock     = new Mock <IFitnessEvaluationStrategy>();
            IFitnessEvaluationStrategy        fitnessEvaluationStrategy = fitnessEvaluationMock.Object;

            Mock <ICrossoverStrategy> crossoverMock = new Mock <ICrossoverStrategy>();

            crossoverMock.Setup(x => x.Cross(It.IsAny <Individual>(), It.IsAny <Individual>()))
            .Returns(new Individual(fitnessEvaluationStrategy, new Configuration()));
            ICrossoverStrategy crossoverStrategy = crossoverMock.Object;

            Mock <IMutationStrategy> mutationMock     = new Mock <IMutationStrategy>();
            IMutationStrategy        mutationStrategy = mutationMock.Object;

            Mock <ISelectionStrategy> selectionMock     = new Mock <ISelectionStrategy>();
            ISelectionStrategy        selectionStrategy = selectionMock.Object;

            DifferentialEvolution differentialEvolution = new DifferentialEvolution(mutationStrategy,
                                                                                    crossoverStrategy,
                                                                                    selectionStrategy,
                                                                                    selectionStrategy,
                                                                                    fitnessEvaluationStrategy,
                                                                                    new Configuration());

            Individual individual = new Individual(fitnessEvaluationStrategy, new Configuration());

            differentialEvolution.population = new List <Individual>
            {
                individual
            };

            differentialEvolution.GetNewPopulation();

            fitnessEvaluationMock.Verify(c => c.GetFitnessForIndividual(It.Is <Individual>(p => p == individual)));
        }
Ejemplo n.º 5
0
 public ImageIndividual(ITwoDimensionalMapper mapper,
                        IFitnessEvaluationStrategy fitnessEvaluationStrategy,
                        IConfiguration configuration,
                        int dimensions = 0)
     : base(fitnessEvaluationStrategy, configuration, dimensions)
 {
     this.mapper = mapper;
 }
Ejemplo n.º 6
0
 public Individual(IFitnessEvaluationStrategy fitnessEvaluationStrategy,
                   IConfiguration configuration,
                   int dimensions = 0)
 {
     Fitness            = 0;
     this.configuration = configuration;
     this.InitialisePosition(dimensions);
     this.FitnessEvaluationStrategy = fitnessEvaluationStrategy;
     Individual.staticConfiguration = configuration;
 }
        public void GivenAPopulationOfTwo_WhenANewPopulationIsRetrieved_ItShouldReturnTheExpectedPopulationOfTwo()
        {
            Mock <IFitnessEvaluationStrategy> fitnessEvaluationMock     = new Mock <IFitnessEvaluationStrategy>();
            IFitnessEvaluationStrategy        fitnessEvaluationStrategy = fitnessEvaluationMock.Object;

            Individual individual1 = new Individual(fitnessEvaluationStrategy, new Configuration());
            Individual child1      = new Individual(fitnessEvaluationStrategy, new Configuration());
            Individual individual2 = new Individual(fitnessEvaluationStrategy, new Configuration());
            Individual child2      = new Individual(fitnessEvaluationStrategy, new Configuration());

            Mock <ICrossoverStrategy> crossoverMock = new Mock <ICrossoverStrategy>();

            crossoverMock.Setup(x => x.Cross(It.IsAny <Individual>(), It.IsAny <Individual>()))
            .Returns(new Individual(fitnessEvaluationStrategy, new Configuration()));
            ICrossoverStrategy crossoverStrategy = crossoverMock.Object;

            Mock <IMutationStrategy> mutationMock     = new Mock <IMutationStrategy>();
            IMutationStrategy        mutationStrategy = mutationMock.Object;

            Mock <ISelectionStrategy> selectionMock = new Mock <ISelectionStrategy>();

            selectionMock.SetupSequence(x => x.Select(It.IsAny <List <Individual> >())).Returns(child1)
            .Returns(individual2);
            ISelectionStrategy selectionStrategy = selectionMock.Object;

            Mock <ISelectionStrategy> otherSelectionMock     = new Mock <ISelectionStrategy>();
            ISelectionStrategy        otherSelectionStrategy = otherSelectionMock.Object;

            DifferentialEvolution differentialEvolution = new DifferentialEvolution(mutationStrategy,
                                                                                    crossoverStrategy,
                                                                                    selectionStrategy,
                                                                                    otherSelectionStrategy,
                                                                                    fitnessEvaluationStrategy,
                                                                                    new Configuration());



            differentialEvolution.population = new List <Individual>
            {
                individual1, individual2
            };

            List <Individual> newPopulation = differentialEvolution.GetNewPopulation();

            Assert.AreEqual(2, newPopulation.Count);
            Assert.IsTrue(newPopulation.Contains(child1));
            Assert.IsTrue(newPopulation.Contains(individual2));
            Assert.IsFalse(newPopulation.Contains(child2));
            Assert.IsFalse(newPopulation.Contains(individual1));
        }
 public DifferentialEvolution(IMutationStrategy mutationStrategy,
                              ICrossoverStrategy crossoverStrategy,
                              ISelectionStrategy generationSelectionStrategy,
                              ISelectionStrategy differenceIndividualSelectionStrategy,
                              IFitnessEvaluationStrategy fitnessEvaluationStrategy,
                              IConfiguration configuration
                              )
 {
     this.mutationStrategy                      = mutationStrategy;
     this.crossoverStrategy                     = crossoverStrategy;
     this.generationSelectionStrategy           = generationSelectionStrategy;
     this.differenceIndividualSelectionStrategy = differenceIndividualSelectionStrategy;
     this.fitnessEvaluationStrategy             = fitnessEvaluationStrategy;
     this.configuration = configuration;
 }
        public void GivenAPopulationOfFour_WhenANewPopulationIsRetrieved_ItShouldCallTheMutationStrategyWithThreeDifferentIndividuals()
        {
            Mock <IFitnessEvaluationStrategy> fitnessEvaluationMock     = new Mock <IFitnessEvaluationStrategy>();
            IFitnessEvaluationStrategy        fitnessEvaluationStrategy = fitnessEvaluationMock.Object;

            Mock <ICrossoverStrategy> crossoverMock = new Mock <ICrossoverStrategy>();

            crossoverMock.Setup(x => x.Cross(It.IsAny <Individual>(), It.IsAny <Individual>()))
            .Returns(new Individual(fitnessEvaluationStrategy, new Configuration()));
            ICrossoverStrategy crossoverStrategy = crossoverMock.Object;

            Mock <IMutationStrategy> mutationMock     = new Mock <IMutationStrategy>();
            IMutationStrategy        mutationStrategy = mutationMock.Object;

            Individual individual  = new Individual(fitnessEvaluationStrategy, new Configuration());
            Individual target      = new Individual(fitnessEvaluationStrategy, new Configuration());
            Individual difference1 = new Individual(fitnessEvaluationStrategy, new Configuration());
            Individual difference2 = new Individual(fitnessEvaluationStrategy, new Configuration());

            Mock <ISelectionStrategy> selectionMock = new Mock <ISelectionStrategy>();

            selectionMock.SetupSequence(x => x.Select(It.IsAny <List <Individual> >())).Returns(target)
            .Returns(difference1)
            .Returns(difference2);
            ISelectionStrategy selectionStrategy = selectionMock.Object;

            DifferentialEvolution differentialEvolution = new DifferentialEvolution(mutationStrategy,
                                                                                    crossoverStrategy,
                                                                                    selectionStrategy,
                                                                                    selectionStrategy,
                                                                                    fitnessEvaluationStrategy,
                                                                                    new Configuration());


            differentialEvolution.population = new List <Individual>
            {
                individual, target, difference1, difference2
            };

            differentialEvolution.GetNewPopulation();

            mutationMock.Verify(c => c.GetTrialVector(target, new List <Individual>()
            {
                difference1, difference2
            }), Times.Once());
        }
Ejemplo n.º 10
0
        public void GivenAnIndividualIsInitialised_WhenTheFitnessIsEvaluated_ItShouldReturnTheValueCalculatedByTheFitnessEvaluationStrategy()
        {
            Mock <IFitnessEvaluationStrategy> mock = new Mock <IFitnessEvaluationStrategy>();

            mock.Setup(x => x.GetFitnessForIndividual(It.IsAny <Individual>())).Returns(27);

            IFitnessEvaluationStrategy fitnessEvaluation = mock.Object;

            Individual individual = new Individual(fitnessEvaluation, new Configuration())
            {
                Position = new double[] { 1.0, 2.0, 3.0 }
            };

            individual.EvaluateFitness();

            Assert.AreEqual(27, individual.Fitness);
        }
        public void GivenAPopulationOfOne_WhenANewPopulationIsRetrieved_ItShouldCallTheSelectionStrategyWithTheIndividualAndTheChild()
        {
            Mock <IFitnessEvaluationStrategy> fitnessEvaluationMock     = new Mock <IFitnessEvaluationStrategy>();
            IFitnessEvaluationStrategy        fitnessEvaluationStrategy = fitnessEvaluationMock.Object;

            Individual individual = new Individual(fitnessEvaluationStrategy, new Configuration());
            Individual child      = new Individual(fitnessEvaluationStrategy, new Configuration());

            Mock <ICrossoverStrategy> crossoverMock = new Mock <ICrossoverStrategy>();

            crossoverMock.Setup(x => x.Cross(It.IsAny <Individual>(), It.IsAny <Individual>())).Returns(child);
            ICrossoverStrategy crossoverStrategy = crossoverMock.Object;

            Mock <IMutationStrategy> mutationMock     = new Mock <IMutationStrategy>();
            IMutationStrategy        mutationStrategy = mutationMock.Object;

            List <Individual>         receivedIndividuals = new List <Individual>();
            Mock <ISelectionStrategy> selectionMock       = new Mock <ISelectionStrategy>();

            selectionMock.Setup(x => x.Select(It.IsAny <List <Individual> >())).Callback((List <Individual> individualList) =>
                                                                                         receivedIndividuals = individualList
                                                                                         );
            ISelectionStrategy selectionStrategy = selectionMock.Object;

            DifferentialEvolution differentialEvolution = new DifferentialEvolution(mutationStrategy,
                                                                                    crossoverStrategy,
                                                                                    selectionStrategy,
                                                                                    selectionStrategy,
                                                                                    fitnessEvaluationStrategy,
                                                                                    new Configuration());



            differentialEvolution.population = new List <Individual>
            {
                individual
            };

            differentialEvolution.GetNewPopulation();

            Assert.AreEqual(2, receivedIndividuals.Count);
            Assert.IsTrue(receivedIndividuals.Contains(individual));
            Assert.IsTrue(receivedIndividuals.Contains(child));
        }
        public void GivenAPopulationOfOne_WhenANewPopulationIsRetrieved_ItShouldCallTheCrossoverStrategyWithTheIndividualAndTheTrialIndividual()
        {
            Mock <IFitnessEvaluationStrategy> fitnessEvaluationMock     = new Mock <IFitnessEvaluationStrategy>();
            IFitnessEvaluationStrategy        fitnessEvaluationStrategy = fitnessEvaluationMock.Object;

            Mock <ICrossoverStrategy> crossoverMock = new Mock <ICrossoverStrategy>();

            crossoverMock.Setup(x => x.Cross(It.IsAny <Individual>(), It.IsAny <Individual>()))
            .Returns(new Individual(fitnessEvaluationStrategy, new Configuration()));
            ICrossoverStrategy crossoverStrategy = crossoverMock.Object;

            Individual individual = new Individual(fitnessEvaluationStrategy, new Configuration());
            Individual mutant     = new Individual(fitnessEvaluationStrategy, new Configuration());

            Mock <IMutationStrategy> mutationMock = new Mock <IMutationStrategy>();

            mutationMock.Setup(x => x.GetTrialVector(It.IsAny <Individual>(), It.IsAny <List <Individual> >()))
            .Returns(mutant);
            IMutationStrategy mutationStrategy = mutationMock.Object;

            Mock <ISelectionStrategy> selectionMock     = new Mock <ISelectionStrategy>();
            ISelectionStrategy        selectionStrategy = selectionMock.Object;

            DifferentialEvolution differentialEvolution = new DifferentialEvolution(mutationStrategy,
                                                                                    crossoverStrategy,
                                                                                    selectionStrategy,
                                                                                    selectionStrategy,
                                                                                    fitnessEvaluationStrategy,
                                                                                    new Configuration());



            differentialEvolution.population = new List <Individual>
            {
                individual
            };

            differentialEvolution.GetNewPopulation();

            crossoverMock.Verify(c => c.Cross(individual, mutant), Times.Once());
        }