public void UniformSelectionOperator_Select() { MockGeneticAlgorithm algorithm = new MockGeneticAlgorithm { FitnessEvaluator = new MockFitnessEvaluator(), GeneticEntitySeed = new MockEntity(), PopulationSeed = new SimplePopulation(), SelectionOperator = new UniformSelectionOperator { SelectionBasedOnFitnessType = FitnessType.Scaled } }; UniformSelectionOperator op = new UniformSelectionOperator(); op.Initialize(algorithm); SimplePopulation population = new SimplePopulation(); population.Initialize(algorithm); for (int i = 0; i < 4; i++) { MockEntity entity = new MockEntity(); entity.Initialize(algorithm); population.Entities.Add(entity); } TestRandomUtil randomUtil = new TestRandomUtil(); RandomNumberService.Instance = randomUtil; randomUtil.Value = 3; IList <GeneticEntity> selectedEntities = op.SelectEntities(1, population); Assert.Same(population.Entities[randomUtil.Value], selectedEntities[0]); randomUtil.Value = 2; selectedEntities = op.SelectEntities(1, population); Assert.Same(population.Entities[randomUtil.Value], selectedEntities[0]); randomUtil.Value = 1; selectedEntities = op.SelectEntities(1, population); Assert.Same(population.Entities[randomUtil.Value], selectedEntities[0]); randomUtil.Value = 0; selectedEntities = op.SelectEntities(1, population); Assert.Same(population.Entities[randomUtil.Value], selectedEntities[0]); }
public void RouletteWheelSampler_GetEntity_NoSizes() { MockGeneticAlgorithm algorithm = new MockGeneticAlgorithm { GeneticEntitySeed = new MockEntity(), SelectionOperator = new MockSelectionOperator(), FitnessEvaluator = new MockFitnessEvaluator(), PopulationSeed = new MockPopulation() }; List <WheelSlice> slices = new List <WheelSlice>(); MockEntity entity1 = new MockEntity(); entity1.Initialize(algorithm); MockEntity entity2 = new MockEntity(); entity2.Initialize(algorithm); MockEntity entity3 = new MockEntity(); entity3.Initialize(algorithm); MockEntity entity4 = new MockEntity(); entity4.Initialize(algorithm); slices.Add(new WheelSlice(entity1, 0)); slices.Add(new WheelSlice(entity2, 0)); slices.Add(new WheelSlice(entity3, 0)); slices.Add(new WheelSlice(entity4, 0)); TestRandomUtil randomUtil = new TestRandomUtil(); RandomNumberService.Instance = randomUtil; randomUtil.RandomValue = 2; GeneticEntity sampledEntity = RouletteWheelSampler.GetEntity(slices); Assert.Equal(4, randomUtil.MaxValuePassed); Assert.Same(entity3, sampledEntity); }
public void RouletteWheelSampler_GetEntity() { MockGeneticAlgorithm algorithm = new MockGeneticAlgorithm { GeneticEntitySeed = new MockEntity(), SelectionOperator = new MockSelectionOperator(), FitnessEvaluator = new MockFitnessEvaluator(), PopulationSeed = new MockPopulation() }; List <WheelSlice> slices = new List <WheelSlice>(); MockEntity entity1 = new MockEntity(); entity1.Initialize(algorithm); MockEntity entity2 = new MockEntity(); entity2.Initialize(algorithm); MockEntity entity3 = new MockEntity(); entity3.Initialize(algorithm); MockEntity entity4 = new MockEntity(); entity4.Initialize(algorithm); slices.Add(new WheelSlice(entity1, 4)); slices.Add(new WheelSlice(entity2, 2)); slices.Add(new WheelSlice(entity3, 1)); slices.Add(new WheelSlice(entity4, 3)); TestRandomUtil randomUtil = new TestRandomUtil(); RandomNumberService.Instance = randomUtil; randomUtil.Ratio = 0; GeneticEntity sampledEntity = RouletteWheelSampler.GetEntity(slices); Assert.Same(entity1, sampledEntity); randomUtil.Ratio = .39999; sampledEntity = RouletteWheelSampler.GetEntity(slices); Assert.Same(entity1, sampledEntity); randomUtil.Ratio = .4; sampledEntity = RouletteWheelSampler.GetEntity(slices); Assert.Same(entity2, sampledEntity); randomUtil.Ratio = .59999; sampledEntity = RouletteWheelSampler.GetEntity(slices); Assert.Same(entity2, sampledEntity); randomUtil.Ratio = .6; sampledEntity = RouletteWheelSampler.GetEntity(slices); Assert.Same(entity3, sampledEntity); randomUtil.Ratio = .69999; sampledEntity = RouletteWheelSampler.GetEntity(slices); Assert.Same(entity3, sampledEntity); randomUtil.Ratio = .7; sampledEntity = RouletteWheelSampler.GetEntity(slices); Assert.Same(entity4, sampledEntity); randomUtil.Ratio = 1; sampledEntity = RouletteWheelSampler.GetEntity(slices); Assert.Same(entity4, sampledEntity); }
public void RankSelectionOperator_Select() { GeneticAlgorithm algorithm = GetAlgorithm(); RankSelectionOperator op = new RankSelectionOperator(); op.Initialize(algorithm); SimplePopulation population = new SimplePopulation(); population.Initialize(algorithm); MockEntity entity1 = new MockEntity(); entity1.Initialize(algorithm); MockEntity entity2 = new MockEntity(); entity2.Initialize(algorithm); MockEntity entity3 = new MockEntity(); entity3.Initialize(algorithm); MockEntity entity4 = new MockEntity(); entity4.Initialize(algorithm); entity1.ScaledFitnessValue = 0; entity2.ScaledFitnessValue = 50; entity3.ScaledFitnessValue = 23; entity4.ScaledFitnessValue = 25; population.Entities.Add(entity1); population.Entities.Add(entity2); population.Entities.Add(entity3); population.Entities.Add(entity4); TestRandomUtil randomUtil = new TestRandomUtil(); RandomNumberService.Instance = randomUtil; randomUtil.Ratio = 0; IList <GeneticEntity> selectedEntities = op.SelectEntities(1, population).ToList(); Assert.Same(entity1, selectedEntities[0]); randomUtil.Ratio = .099999; selectedEntities = op.SelectEntities(1, population); Assert.Same(entity1, selectedEntities[0]); randomUtil.Ratio = .1; selectedEntities = op.SelectEntities(1, population); Assert.Same(entity3, selectedEntities[0]); randomUtil.Ratio = .299999; selectedEntities = op.SelectEntities(1, population); Assert.Same(entity3, selectedEntities[0]); randomUtil.Ratio = .3; selectedEntities = op.SelectEntities(1, population); Assert.Same(entity4, selectedEntities[0]); randomUtil.Ratio = .599999; selectedEntities = op.SelectEntities(1, population); Assert.Same(entity4, selectedEntities[0]); randomUtil.Ratio = .6; selectedEntities = op.SelectEntities(1, population); Assert.Same(entity2, selectedEntities[0]); randomUtil.Ratio = 1; selectedEntities = op.SelectEntities(1, population); Assert.Same(entity2, selectedEntities[0]); }
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); }