src/GenFx.Components.Tests/SinglePointCrossoverOperatorTest.cs
/// <summary>
/// Tests that the Crossover method works correctly.
/// </summary>
Tests that the Crossover method works correctly.
Tests that the Crossover method works correctly .
[Fact]
        public void SinglePointCrossoverOperator_GenerateCrossover_SameLength()
        {
            MockGeneticAlgorithm algorithm = new MockGeneticAlgorithm
            {
                PopulationSeed = new MockPopulation(),
                SelectionOperator = new MockSelectionOperator(),
                FitnessEvaluator = new MockFitnessEvaluator(),
                CrossoverOperator = new SinglePointCrossoverOperator
                {
                    CrossoverRate = 1
                },
                GeneticEntitySeed = new BinaryStringEntity
                {
                    MinimumStartingLength = 4,
                    MaximumStartingLength = 4
                }
            };
            algorithm.GeneticEntitySeed.Initialize(algorithm);
            SinglePointCrossoverOperator op = new SinglePointCrossoverOperator { CrossoverRate = 1 };
            op.Initialize(algorithm);
            BinaryStringEntity entity1 = (BinaryStringEntity)algorithm.GeneticEntitySeed.CreateNewAndInitialize();
            entity1[0] = true;
            entity1[1] = false;
            entity1[2] = false;
            entity1[3] = true;
            BinaryStringEntity entity2 = (BinaryStringEntity)algorithm.GeneticEntitySeed.CreateNewAndInitialize();
            entity2.Initialize(algorithm);
            entity2[0] = true;
            entity2[1] = true;
            entity2[2] = false;
            entity2[3] = false;
            TestRandomUtil randomUtil = new TestRandomUtil();
            RandomNumberService.Instance = randomUtil;
            randomUtil.RandomVal = 1;
            IList<GeneticEntity> result = op.Crossover(new GeneticEntity[] { entity1, entity2 }).ToList();
            BinaryStringEntity resultEntity1 = (BinaryStringEntity)result[0];
            BinaryStringEntity resultEntity2 = (BinaryStringEntity)result[1];
            Assert.Equal("1100", resultEntity1.Representation);
            Assert.Equal("1001", resultEntity2.Representation);
            randomUtil.RandomVal = 3;
            result = op.Crossover(new GeneticEntity[] { entity1, entity2 }).ToList();
            resultEntity1 = (BinaryStringEntity)result[0];
            resultEntity2 = (BinaryStringEntity)result[1];
            Assert.Equal("1000", resultEntity1.Representation);
            Assert.Equal("1101", resultEntity2.Representation);
        }
[ Fact ] public void SinglePointCrossoverOperator_GenerateCrossover_SameLength ( ) { MockGeneticAlgorithm algorithm = new MockGeneticAlgorithm { PopulationSeed = new MockPopulation ( ) , SelectionOperator = new MockSelectionOperator ( ) , FitnessEvaluator = new MockFitnessEvaluator ( ) , CrossoverOperator = new SinglePointCrossoverOperator { CrossoverRate = 1 } , GeneticEntitySeed = new BinaryStringEntity { MinimumStartingLength = 4 , MaximumStartingLength = 4 } } ; algorithm . GeneticEntitySeed . Initialize ( algorithm ) ; SinglePointCrossoverOperator op = new SinglePointCrossoverOperator { CrossoverRate = 1 } ; op . Initialize ( algorithm ) ; BinaryStringEntity entity1 = ( BinaryStringEntity ) algorithm . GeneticEntitySeed . CreateNewAndInitialize ( ) ; entity1 [ 0 ] = true ; entity1 [ 1 ] = false ; entity1 [ 2 ] = false ; entity1 [ 3 ] = true ; BinaryStringEntity entity2 = ( BinaryStringEntity ) algorithm . GeneticEntitySeed . CreateNewAndInitialize ( ) ; entity2 . Initialize ( algorithm ) ; entity2 [ 0 ] = true ; entity2 [ 1 ] = true ; entity2 [ 2 ] = false ; entity2 [ 3 ] = false ; TestRandomUtil randomUtil = new TestRandomUtil ( ) ; RandomNumberService . Instance = randomUtil ; randomUtil . RandomVal = 1 ; IList < GeneticEntity > result = op . Crossover ( new GeneticEntity [ ] { entity1 , entity2 } ) . ToList ( ) ; BinaryStringEntity resultEntity1 = ( BinaryStringEntity ) result [ 0 ] ; BinaryStringEntity resultEntity2 = ( BinaryStringEntity ) result [ 1 ] ; Assert . Equal ( " " , resultEntity1 . Representation ) ; Assert . Equal ( " " , resultEntity2 . Representation ) ; randomUtil . RandomVal = 3 ; result = op . Crossover ( new GeneticEntity [ ] { entity1 , entity2 } ) . ToList ( ) ; resultEntity1 = ( BinaryStringEntity ) result [ 0 ] ; resultEntity2 = ( BinaryStringEntity ) result [ 1 ] ; Assert . Equal ( " " , resultEntity1 . Representation ) ; Assert . Equal ( " " , resultEntity2 . Representation ) ; }
Tests that the Crossover method works correctly.
Tests that the Crossover method works correctly .