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 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 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 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 MutationStrategyType ProvideMappingType(IMutationStrategy strategy)
 {
     if (strategy is BinaryTransformationStrategy)
     {
         return(MutationStrategyType.BinaryTransformationStrategy);
     }
     return(MutationStrategyType.None);
 }
 /// <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));
        }
Exemple #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));
        }
 /// <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());
        }
        private GeneticAlgorithm(int continuousVariablesCount, int integerVariablesCount, IDesignFactory designFactory,
                                 IOptimizationLogger logger, IConvergenceCriterion convergenceCriterion, IInitializer <double> initializer,
                                 IEncoding <T> encoding, int populationSize, IPopulationStrategy <T> populationStrategy,
                                 ISelectionStrategy <T> selection, IRecombinationStrategy <T> recombination, IMutationStrategy <T> mutation)
        {
            this.continuousVariablesCount = continuousVariablesCount;
            this.integerVariablesCount    = integerVariablesCount;
            this.designFactory            = designFactory;

            this.logger = logger;
            this.convergenceCriterion = convergenceCriterion;
            this.initializer          = initializer;

            this.encoding           = encoding;
            this.populationSize     = populationSize;
            this.populationStrategy = populationStrategy;
            this.selection          = selection;
            this.recombination      = recombination;
            this.mutation           = mutation;

            this.CurrentIteration = -1; // Initialization phase is not counted towards iterations
            this.BestPosition     = null;
            this.BestFitness      = double.MaxValue;
        }
Exemple #15
0
 public ReproductionFacade(ICrossoverStrategy cs, IRepairStrategy rs, IMutationStrategy ms)
 {
     this._crossoverMechanism = cs;
     this._repairMechanism    = rs;
     this._mutationMechanism  = ms;
 }
Exemple #16
0
 public ReproductionFacade(ICrossoverStrategy cs, IRepairStrategy rs, IMutationStrategy ms)
 {
     this._crossoverMechanism = cs;
     this._repairMechanism = rs;
     this._mutationMechanism = ms;
 }
        public Individual<T>[] CreateNextGeneration(Individual<T>[] originalPopulation, ISelectionStrategy<T> selection, 
                                             IRecombinationStrategy<T> recombination, IMutationStrategy<T> mutation)
        {
            Array.Sort(originalPopulation); // sorting may not always be mandatory (e.g. 1-3 elites, selection does not sort)
            int offspringsCount = populationSize - elitesCount;
            // TODO: Redundant copying. A linked list would be better.
            Individual<T>[] offsprings = recombination.Apply(selection, originalPopulation, offspringsCount);
            mutation.Apply(offsprings);

            Individual<T>[] nextPopulation = new Individual<T>[populationSize];
            Array.Copy(originalPopulation, nextPopulation, elitesCount);
            Array.Copy(offsprings, 0, nextPopulation, elitesCount, offsprings.Length);
            return nextPopulation;
        }
 public Individual <T>[] CreateNextGeneration(Individual <T>[] originalPopulation, ISelectionStrategy <T> selection,
                                              IRecombinationStrategy <T> recombination, IMutationStrategy <T> mutation)
 {
     throw new NotImplementedException();
 }
 public MutationStrategyType ProvideMappingType(IMutationStrategy strategy)
 {
     if(strategy is BinaryTransformationStrategy)
         return MutationStrategyType.BinaryTransformationStrategy;
     return MutationStrategyType.None;
 }
        public Individual<T>[] CreateNextGeneration(Individual<T>[] originalPopulation, ISelectionStrategy<T> selection,
                                                    IRecombinationStrategy<T> recombination, IMutationStrategy<T> mutation)
        {
            Array.Sort(originalPopulation);

            // Selection and rcombination: Only the elites and the rest of survivors will have a chance to reproduce
            Individual<T>[] selectionPool = new Individual<T>[elitesCount + survivorsCount];
            Array.Copy(originalPopulation, selectionPool, selectionPool.Length);
            // TODO: Redundant copying. A linked list would be better.
            Individual<T>[] offsprings = recombination.Apply(selection, selectionPool, offspringsCount);

            // Mutation will be applied to the survivors and their offsprings but not on the elites
            Individual<T>[] mutants = new Individual<T>[survivorsCount + offsprings.Length];
            Array.Copy(originalPopulation, elitesCount, mutants, 0, survivorsCount);
            Array.Copy(offsprings, 0, mutants, survivorsCount, offspringsCount);
            mutation.Apply(mutants);

            // The next population will contain the elites, the mutated survivors and the mutated offsprings
            Individual<T>[] nextPopulation = new Individual<T>[originalPopulation.Length];
            Array.Copy(originalPopulation, nextPopulation, elitesCount);
            Array.Copy(mutants, 0, nextPopulation, elitesCount, survivorsCount + offspringsCount);
            return nextPopulation;
        }