public void BinaryStringEntity_Serialization() { BinaryStringEntity entity = new BinaryStringEntity(); entity.MinimumStartingLength = entity.MaximumStartingLength = 3; entity.IsFixedSize = true; entity.Initialize(new MockGeneticAlgorithm()); BinaryStringEntity result = (BinaryStringEntity)SerializationHelper.TestSerialization( entity, new Type[] { typeof(MockGeneticAlgorithm), typeof(DefaultTerminator) }); for (int i = 0; i < 3; i++) { Assert.Equal(entity[i], result[i]); } Assert.True(result.IsFixedSize); }
public void BinaryStringEntity_SetLengthToExpand() { BinaryStringEntity entity = new BinaryStringEntity { MinimumStartingLength = 2, MaximumStartingLength = 2, }; entity.Initialize(new MockGeneticAlgorithm()); Assert.Equal(2, entity.Length); entity.Length = 4; Assert.Equal(4, entity.Length); Assert.False(entity[2]); Assert.False(entity[3]); }
public void BinaryStringEntity_SetLengthToContract() { BinaryStringEntity entity = new BinaryStringEntity { MinimumStartingLength = 4, MaximumStartingLength = 4, }; entity.Initialize(new MockGeneticAlgorithm()); Assert.Equal(4, entity.Length); entity[0] = true; Assert.True(entity[0]); entity.Length = 1; Assert.Equal(1, entity.Length); Assert.True(entity[0]); }
public void InversionOperator_Mutate() { GeneticAlgorithm algorithm = new MockGeneticAlgorithm { SelectionOperator = new MockSelectionOperator(), PopulationSeed = new MockPopulation(), FitnessEvaluator = new MockFitnessEvaluator(), GeneticEntitySeed = new BinaryStringEntity { MinimumStartingLength = 4, MaximumStartingLength = 4 }, MutationOperator = new InversionOperator { MutationRate = 1 } }; algorithm.GeneticEntitySeed.Initialize(algorithm); InversionOperator op = (InversionOperator)algorithm.MutationOperator; op.Initialize(algorithm); BinaryStringEntity entity = (BinaryStringEntity)algorithm.GeneticEntitySeed.CreateNewAndInitialize(); entity.Age = 10; entity.Initialize(algorithm); entity[0] = true; entity[1] = true; entity[2] = false; entity[3] = true; FakeRandomUtil randomUtil = new FakeRandomUtil(); RandomNumberService.Instance = randomUtil; randomUtil.RandomValue = 1; GeneticEntity mutant = op.Mutate(entity); Assert.Equal("1011", mutant.Representation); Assert.Equal(0, mutant.Age); }
public async Task UniformBitMutationOperator_Mutate() { MockGeneticAlgorithm algorithm = new MockGeneticAlgorithm { PopulationSeed = new MockPopulation(), SelectionOperator = new MockSelectionOperator(), FitnessEvaluator = new MockFitnessEvaluator2(), GeneticEntitySeed = new BinaryStringEntity { MinimumStartingLength = 4, MaximumStartingLength = 4 }, MutationOperator = new UniformBitMutationOperator { MutationRate = 1 } }; await algorithm.InitializeAsync(); UniformBitMutationOperator op = new UniformBitMutationOperator { MutationRate = 1 }; op.Initialize(algorithm); BinaryStringEntity entity = new BinaryStringEntity { MinimumStartingLength = 4, MaximumStartingLength = 4 }; entity.Age = 10; entity.Initialize(algorithm); entity[0] = true; entity[1] = true; entity[2] = false; entity[3] = true; GeneticEntity mutant = op.Mutate(entity); Assert.Equal("0010", mutant.Representation); Assert.Equal(0, mutant.Age); }
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); }