src/GenFx.Tests/GeneticAlgorithmTest.cs
/// <summary>
/// Tests that the Step method works correctly.
/// </summary>
Tests that the Step method works correctly.
Tests that the Step method works correctly .
[Fact]
        public async Task GeneticAlgorithm_Step_Async()
        {
            TestGeneticAlgorithm algorithm = GetAlgorithm();
            int eventHandlerCallCount = 0;
            await algorithm.InitializeAsync();
            algorithm.FitnessEvaluated += new EventHandler<EnvironmentFitnessEvaluatedEventArgs>(delegate (object sender, EnvironmentFitnessEvaluatedEventArgs args)
            {
                eventHandlerCallCount++;
            });
            bool stepResult = await algorithm.StepAsync();
            Assert.False(stepResult, "Algorithm should not be complete yet.");
            Assert.Equal(1, eventHandlerCallCount);
            Assert.Equal(1, algorithm.CurrentGeneration);
            Assert.Equal(0, ((MockCrossoverOperator)algorithm.CrossoverOperator).DoCrossoverCallCount);
            Assert.Equal(0, ((MockElitismStrategy)algorithm.ElitismStrategy).GetElitistGeneticEntitiesCallCount);
            Assert.Equal(400, ((MockFitnessEvaluator)algorithm.FitnessEvaluator).DoEvaluateFitnessCallCount);
            Assert.Equal(4, ((MockFitnessScalingStrategy)algorithm.FitnessScalingStrategy).OnScaleCallCount);
            Assert.Equal(0, ((MockMutationOperator)algorithm.MutationOperator).DoMutateCallCount);
            Assert.Equal(0, ((MockSelectionOperator)algorithm.SelectionOperator).DoSelectCallCount);
            stepResult = await algorithm.StepAsync();
            Assert.False(stepResult, "Algorithm should not be complete yet.");
            stepResult = await algorithm.StepAsync();
            Assert.True(stepResult, "Algorithm should be complete.");
            Assert.Equal(3, eventHandlerCallCount);
            Assert.Equal(3, algorithm.CurrentGeneration);
            Assert.Equal(0, ((MockCrossoverOperator)algorithm.CrossoverOperator).DoCrossoverCallCount);
            Assert.Equal(0, ((MockElitismStrategy)algorithm.ElitismStrategy).GetElitistGeneticEntitiesCallCount);
            Assert.Equal(800, ((MockFitnessEvaluator)algorithm.FitnessEvaluator).DoEvaluateFitnessCallCount);
            Assert.Equal(8, ((MockFitnessScalingStrategy)algorithm.FitnessScalingStrategy).OnScaleCallCount);
            Assert.Equal(0, ((MockMutationOperator)algorithm.MutationOperator).DoMutateCallCount);
            Assert.Equal(0, ((MockSelectionOperator)algorithm.SelectionOperator).DoSelectCallCount);
        }
[ Fact ] public async Task GeneticAlgorithm_Step_Async ( ) { TestGeneticAlgorithm algorithm = GetAlgorithm ( ) ; int eventHandlerCallCount = 0 ; await algorithm . InitializeAsync ( ) ; algorithm . FitnessEvaluated += new EventHandler < EnvironmentFitnessEvaluatedEventArgs > ( delegate ( object sender , EnvironmentFitnessEvaluatedEventArgs args ) { eventHandlerCallCount ++ ; } ) ; bool stepResult = await algorithm . StepAsync ( ) ; Assert . False ( stepResult , " " ) ; Assert . Equal ( 1 , eventHandlerCallCount ) ; Assert . Equal ( 1 , algorithm . CurrentGeneration ) ; Assert . Equal ( 0 , ( ( MockCrossoverOperator ) algorithm . CrossoverOperator ) . DoCrossoverCallCount ) ; Assert . Equal ( 0 , ( ( MockElitismStrategy ) algorithm . ElitismStrategy ) . GetElitistGeneticEntitiesCallCount ) ; Assert . Equal ( 400 , ( ( MockFitnessEvaluator ) algorithm . FitnessEvaluator ) . DoEvaluateFitnessCallCount ) ; Assert . Equal ( 4 , ( ( MockFitnessScalingStrategy ) algorithm . FitnessScalingStrategy ) . OnScaleCallCount ) ; Assert . Equal ( 0 , ( ( MockMutationOperator ) algorithm . MutationOperator ) . DoMutateCallCount ) ; Assert . Equal ( 0 , ( ( MockSelectionOperator ) algorithm . SelectionOperator ) . DoSelectCallCount ) ; stepResult = await algorithm . StepAsync ( ) ; Assert . False ( stepResult , " " ) ; stepResult = await algorithm . StepAsync ( ) ; Assert . True ( stepResult , " " ) ; Assert . Equal ( 3 , eventHandlerCallCount ) ; Assert . Equal ( 3 , algorithm . CurrentGeneration ) ; Assert . Equal ( 0 , ( ( MockCrossoverOperator ) algorithm . CrossoverOperator ) . DoCrossoverCallCount ) ; Assert . Equal ( 0 , ( ( MockElitismStrategy ) algorithm . ElitismStrategy ) . GetElitistGeneticEntitiesCallCount ) ; Assert . Equal ( 800 , ( ( MockFitnessEvaluator ) algorithm . FitnessEvaluator ) . DoEvaluateFitnessCallCount ) ; Assert . Equal ( 8 , ( ( MockFitnessScalingStrategy ) algorithm . FitnessScalingStrategy ) . OnScaleCallCount ) ; Assert . Equal ( 0 , ( ( MockMutationOperator ) algorithm . MutationOperator ) . DoMutateCallCount ) ; Assert . Equal ( 0 , ( ( MockSelectionOperator ) algorithm . SelectionOperator ) . DoSelectCallCount ) ; }
Tests that the Step method works correctly.
Tests that the Step method works correctly .