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)));
        }
 public CrossoverStrategyType ProvideMappingType(ICrossoverStrategy strategy)
 {
     if (strategy is OnePointCrossoverStrategy)
     {
         return(CrossoverStrategyType.OnePointCrossoverStrategy);
     }
     return(CrossoverStrategyType.None);
 }
Exemplo n.º 6
0
 /// <summary>
 /// Creates setup of genetic operators for <see cref="T:Localwire.Graphinder.Algorithms.Core.Algorithms.GeneticAlgorithm.GenetichAlgorithm"/>
 /// </summary>
 /// <param name="selectionStrategy">Selection strategy</param>
 /// <param name="crossoverStrategy">Crossover strategy</param>
 /// <param name="mutationStrategy">Mutation strategy</param>
 public GeneticOperators(ISelectionStrategy selectionStrategy,
     ICrossoverStrategy crossoverStrategy, IMutationStrategy mutationStrategy)
 {
     if (selectionStrategy == null)
         throw new ArgumentNullException(nameof(selectionStrategy));
     if (crossoverStrategy == null)
         throw new ArgumentNullException(nameof(crossoverStrategy));
     if (mutationStrategy == null)
         throw new ArgumentNullException(nameof(mutationStrategy));
     SelectionStrategy = selectionStrategy;
     MutationStrategy = mutationStrategy;
     CrossoverStrategy = crossoverStrategy;
 }
        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));
        }
Exemplo n.º 8
0
 public Evolution(IRandomNumberGenerator random, 
     IChromosomeFactory chromosomeFactory, 
     IPopulationStrategy populationStrategy, 
     IMutationStrategy mutationStrategy,
     ICrossoverStrategy crossoverStrategy, 
     ISelectionStrategy selectionStrategy)
 {
     // TODO: Complete member initialization
     this.random = random;
     this.chromosomeFactory = chromosomeFactory;
     this.populationStrategy = populationStrategy;
     this.mutationStrategy = mutationStrategy;
     this.crossoverStrategy = crossoverStrategy;
     this.selectionStrategy = selectionStrategy;
 }
 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());
        }
        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));
        }
Exemplo n.º 12
0
 /// <summary>
 /// Creates setup of genetic operators for <see cref="T:Localwire.Graphinder.Algorithms.Core.Algorithms.GeneticAlgorithm.GenetichAlgorithm"/>
 /// </summary>
 /// <param name="selectionStrategy">Selection strategy</param>
 /// <param name="crossoverStrategy">Crossover strategy</param>
 /// <param name="mutationStrategy">Mutation strategy</param>
 public GeneticOperators(ISelectionStrategy selectionStrategy,
                         ICrossoverStrategy crossoverStrategy, IMutationStrategy mutationStrategy)
 {
     if (selectionStrategy == null)
     {
         throw new ArgumentNullException(nameof(selectionStrategy));
     }
     if (crossoverStrategy == null)
     {
         throw new ArgumentNullException(nameof(crossoverStrategy));
     }
     if (mutationStrategy == null)
     {
         throw new ArgumentNullException(nameof(mutationStrategy));
     }
     SelectionStrategy = selectionStrategy;
     MutationStrategy  = mutationStrategy;
     CrossoverStrategy = crossoverStrategy;
 }
        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());
        }
Exemplo n.º 14
0
 public ReproductionFacade(ICrossoverStrategy cs, IRepairStrategy rs, IMutationStrategy ms)
 {
     this._crossoverMechanism = cs;
     this._repairMechanism    = rs;
     this._mutationMechanism  = ms;
 }
Exemplo n.º 15
0
 public ReproductionFacade(ICrossoverStrategy cs, IRepairStrategy rs, IMutationStrategy ms)
 {
     this._crossoverMechanism = cs;
     this._repairMechanism = rs;
     this._mutationMechanism = ms;
 }
 public CrossoverStrategyType ProvideMappingType(ICrossoverStrategy strategy)
 {
     if (strategy is OnePointCrossoverStrategy)
         return CrossoverStrategyType.OnePointCrossoverStrategy;
     return CrossoverStrategyType.None;
 }