public void SelectChromosomes_offspringSizeLowerThanMinSize_Selectoffspring()
        {
            var target = new ElitistReinsertion ();

            var population = new Population (6, 8, MockRepository.GenerateStub<ChromosomeBase> (2));
            var offspring = new List<IChromosome> () {
                MockRepository.GenerateStub<ChromosomeBase> (2),
                MockRepository.GenerateStub<ChromosomeBase> (2),
                MockRepository.GenerateStub<ChromosomeBase> (3),
                MockRepository.GenerateStub<ChromosomeBase> (4)
            };

            var parents = new List<IChromosome> () {
                MockRepository.GenerateStub<ChromosomeBase> (5),
                MockRepository.GenerateStub<ChromosomeBase> (6),
                MockRepository.GenerateStub<ChromosomeBase> (7),
                MockRepository.GenerateStub<ChromosomeBase> (8)
            };

            parents [0].Fitness = 0.2;
            parents [1].Fitness = 0.3;
            parents [2].Fitness = 0.5;
            parents [3].Fitness = 0.7;

            var selected = target.SelectChromosomes (population, offspring, parents);
            Assert.AreEqual (6, selected.Count);
            Assert.AreEqual (2, selected [0].Length);
            Assert.AreEqual (2, selected [1].Length);
            Assert.AreEqual (3, selected [2].Length);
            Assert.AreEqual (4, selected [3].Length);
            Assert.AreEqual (8, selected [4].Length);
            Assert.AreEqual (7, selected [5].Length);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="GeneticSharp.Domain.GeneticAlgorithm"/> class.
        /// </summary>
        /// <param name="population">The chromosomes population.</param>
        /// <param name="fitness">The fitness evaluation function.</param>
        /// <param name="selection">The selection operator.</param>
        /// <param name="crossover">The crossover operator.</param>
        /// <param name="mutation">The mutation operator.</param>
        public GeneticAlgorithm(
            Population population,
            IFitness fitness,
            ISelection selection,
            ICrossover crossover,
            IMutation mutation)
        {
            ExceptionHelper.ThrowIfNull("Population", population);
            ExceptionHelper.ThrowIfNull("fitness", fitness);
            ExceptionHelper.ThrowIfNull("selection", selection);
            ExceptionHelper.ThrowIfNull("crossover", crossover);
            ExceptionHelper.ThrowIfNull("mutation", mutation);

            Population = population;
            Fitness = fitness;
            Selection = selection;
            Crossover = crossover;
            Mutation = mutation;
            Reinsertion = new ElitistReinsertion();
            Termination = new GenerationNumberTermination(1);

            CrossoverProbability = DefaultCrossoverProbability;
            MutationProbability = DefaultMutationProbability;
            TimeEvolving = TimeSpan.Zero;
            State = GeneticAlgorithmState.NotStarted;
            TaskExecutor = new LinearTaskExecutor();
        }