public void FitnessProportionateSelectionOperator_SelectEntitiesFromPopulation_NullPopulation() { FitnessProportionateSelectionOperator op = new FitnessProportionateSelectionOperator(); PrivateObject accessor = new PrivateObject(op); Assert.Throws <ArgumentNullException>(() => accessor.Invoke("SelectEntitiesFromPopulation", (int)0, (Population)null)); }
public void FitnessProportionateSelectionOperator_Select_MinimizeFitness() { GeneticAlgorithm algorithm = GetAlgorithm(); ((MockFitnessEvaluator)algorithm.FitnessEvaluator).EvaluationMode = FitnessEvaluationMode.Minimize; FitnessProportionateSelectionOperator op = new FitnessProportionateSelectionOperator(); op.Initialize(algorithm); SimplePopulation population = new SimplePopulation(); population.Initialize(algorithm); MockEntity entity1 = new MockEntity(); entity1.Initialize(algorithm); entity1.ScaledFitnessValue = 1; // Slice size: 5 MockEntity entity2 = new MockEntity(); entity2.Initialize(algorithm); entity2.ScaledFitnessValue = 5; // Slice size: 1 MockEntity entity3 = new MockEntity(); entity3.Initialize(algorithm); entity3.ScaledFitnessValue = 4; // Slice size: 4 population.Entities.Add(entity1); population.Entities.Add(entity2); population.Entities.Add(entity3); FakeRandomUtil randomUtil = new FakeRandomUtil(); RandomNumberService.Instance = randomUtil; randomUtil.RandomRatio = 0; IList <GeneticEntity> selectedEntities = op.SelectEntities(1, population).ToList(); Assert.Same(entity2, selectedEntities[0]); randomUtil.RandomRatio = .099999; selectedEntities = op.SelectEntities(1, population); Assert.Same(entity2, selectedEntities[0]); randomUtil.RandomRatio = .1; selectedEntities = op.SelectEntities(1, population); Assert.Same(entity3, selectedEntities[0]); randomUtil.RandomRatio = .499999; selectedEntities = op.SelectEntities(1, population); Assert.Same(entity3, selectedEntities[0]); randomUtil.RandomRatio = .5; selectedEntities = op.SelectEntities(1, population); Assert.Same(entity1, selectedEntities[0]); randomUtil.RandomRatio = 1; selectedEntities = op.SelectEntities(1, population); Assert.Same(entity1, selectedEntities[0]); }
public void FitnessProportionateSelectionOperator_Select() { GeneticAlgorithm algorithm = GetAlgorithm(); FitnessProportionateSelectionOperator op = new FitnessProportionateSelectionOperator(); op.Initialize(algorithm); SimplePopulation population = new SimplePopulation(); population.Initialize(algorithm); MockEntity entity1 = new MockEntity(); entity1.Initialize(algorithm); entity1.ScaledFitnessValue = 1; MockEntity entity2 = new MockEntity(); entity2.Initialize(algorithm); entity2.ScaledFitnessValue = 5; MockEntity entity3 = new MockEntity(); entity3.Initialize(algorithm); entity3.ScaledFitnessValue = 4; population.Entities.Add(entity1); population.Entities.Add(entity2); population.Entities.Add(entity3); FakeRandomUtil randomUtil = new FakeRandomUtil(); RandomNumberService.Instance = randomUtil; randomUtil.RandomRatio = 0; IList <GeneticEntity> selectedEntities = op.SelectEntities(1, population); Assert.Same(entity1, selectedEntities[0]); randomUtil.RandomRatio = .099999; selectedEntities = op.SelectEntities(1, population); Assert.Same(entity1, selectedEntities[0]); randomUtil.RandomRatio = .1; selectedEntities = op.SelectEntities(1, population); Assert.Same(entity2, selectedEntities[0]); randomUtil.RandomRatio = .599999; selectedEntities = op.SelectEntities(1, population); Assert.Same(entity2, selectedEntities[0]); randomUtil.RandomRatio = .6; selectedEntities = op.SelectEntities(1, population); Assert.Same(entity3, selectedEntities[0]); randomUtil.RandomRatio = 1; selectedEntities = op.SelectEntities(1, population); Assert.Same(entity3, selectedEntities[0]); }