public void HasReached_GenerationNumberLowerThanCondition_False()
        {
            var repository = new MockRepository();
            var ga = repository.StrictMock<IGeneticAlgorithm>();

            using (repository.Ordered())
            {
                ga.Expect(g => g.GenerationsNumber).Return(1);
                ga.Expect(g => g.GenerationsNumber).Return(2);
                ga.Expect(g => g.GenerationsNumber).Return(3);
                ga.Expect(g => g.GenerationsNumber).Return(4);
                ga.Expect(g => g.GenerationsNumber).Return(5);
                ga.Expect(g => g.GenerationsNumber).Return(6);
                ga.Expect(g => g.GenerationsNumber).Return(7);
                ga.Expect(g => g.GenerationsNumber).Return(8);
                ga.Expect(g => g.GenerationsNumber).Return(0);
            }
            repository.ReplayAll();

            var target = new GenerationNumberTermination(10);
            Assert.IsFalse(target.HasReached(ga));

            for (int i = 0; i < 8; i++)
            {
                Assert.IsFalse(target.HasReached(ga));
            }

        }
        public void HasReached_GenerationNumberGreaterOrEqualThanCondition_True()
        {
            var repository = new MockRepository ();
            var ga = repository.StrictMock<IGeneticAlgorithm> ();

            using (repository.Ordered()) {
                ga.Expect (g => g.GenerationsNumber).Return (10);
                ga.Expect (g => g.GenerationsNumber).Return (11);
            }
            repository.ReplayAll ();

            var target = new GenerationNumberTermination (10);
            Assert.IsTrue (target.HasReached (ga));
            Assert.IsTrue (target.HasReached (ga));
        }
예제 #3
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();
        }