public void SinglePointCrossoverOperator_Validation_WithoutListEntityBase()
        {
            SinglePointCrossoverOperator op        = new SinglePointCrossoverOperator();
            MockGeneticAlgorithm         algorithm = new MockGeneticAlgorithm
            {
                GeneticEntitySeed = new MockEntity()
            };

            op.Initialize(algorithm);

            Assert.Throws <ValidationException>(() => op.Validate());
        }
        public void SinglePointCrossoverOperator_Validation_WithListEntityBase()
        {
            SinglePointCrossoverOperator op        = new SinglePointCrossoverOperator();
            MockGeneticAlgorithm         algorithm = new MockGeneticAlgorithm
            {
                GeneticEntitySeed = new IntegerListEntity()
            };

            op.Initialize(algorithm);

            op.Validate();
        }
        public void SinglePointCrossoverOperator_GenerateCrossover_DifferentLength()
        {
            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.Length = 5;
            entity1[0]     = true;
            entity1[1]     = false;
            entity1[2]     = false;
            entity1[3]     = true;
            entity1[4]     = 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("10011", 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("11011", resultEntity2.Representation);
        }