src/GenFx.Tests/CrossoverOperatorTest.cs
/// <summary>
/// Tests that the crossover works correctly when the crossover rate is hit.
/// </summary>
Tests that the crossover works correctly when the crossover rate is hit.
Tests that the crossover works correctly when the crossover rate is hit .
[Fact]
        public void CrossoverOperator_Crossover()
        {
            double crossoverRate = 1; 
            MockGeneticAlgorithm algorithm = GetGeneticAlgorithm(crossoverRate);
            FakeCrossoverOperator op = new FakeCrossoverOperator { CrossoverRate = crossoverRate };
            op.Initialize(algorithm);
            MockEntity entity1 = new MockEntity();
            entity1.Initialize(algorithm);
            entity1.Age = 2;
            entity1.Identifier = "1";
            MockEntity entity2 = new MockEntity();
            entity2.Initialize(algorithm);
            entity2.Age = 5;
            entity2.Identifier = "3";
            IList<GeneticEntity> geneticEntities = op.Crossover(new GeneticEntity[] { entity1, entity2 }).ToList();
            Assert.NotSame(entity1, geneticEntities[1]);
            Assert.NotSame(entity2, geneticEntities[0]);
            Assert.Equal(entity1.Identifier, ((MockEntity)geneticEntities[1]).Identifier);
            Assert.Equal(entity2.Identifier, ((MockEntity)geneticEntities[0]).Identifier);
            Assert.Equal(0, geneticEntities[0].Age);
            Assert.Equal(0, geneticEntities[1].Age);
        }
[ Fact ] public void CrossoverOperator_Crossover ( ) { double crossoverRate = 1 ; MockGeneticAlgorithm algorithm = GetGeneticAlgorithm ( crossoverRate ) ; FakeCrossoverOperator op = new FakeCrossoverOperator { CrossoverRate = crossoverRate } ; op . Initialize ( algorithm ) ; MockEntity entity1 = new MockEntity ( ) ; entity1 . Initialize ( algorithm ) ; entity1 . Age = 2 ; entity1 . Identifier = " " ; MockEntity entity2 = new MockEntity ( ) ; entity2 . Initialize ( algorithm ) ; entity2 . Age = 5 ; entity2 . Identifier = " " ; IList < GeneticEntity > geneticEntities = op . Crossover ( new GeneticEntity [ ] { entity1 , entity2 } ) . ToList ( ) ; Assert . NotSame ( entity1 , geneticEntities [ 1 ] ) ; Assert . NotSame ( entity2 , geneticEntities [ 0 ] ) ; Assert . Equal ( entity1 . Identifier , ( ( MockEntity ) geneticEntities [ 1 ] ) . Identifier ) ; Assert . Equal ( entity2 . Identifier , ( ( MockEntity ) geneticEntities [ 0 ] ) . Identifier ) ; Assert . Equal ( 0 , geneticEntities [ 0 ] . Age ) ; Assert . Equal ( 0 , geneticEntities [ 1 ] . Age ) ; }
Tests that the crossover works correctly when the crossover rate is hit.
Tests that the crossover works correctly when the crossover rate is hit .
// force crossover to occur