src/GenFx.Components.Tests/FitnessProportionateSelectionOperatorTest.cs
/// <summary>
/// Tests that the Select method works correctly when trying to minimize the fitness.
/// </summary>
Tests that the Select method works correctly when trying to minimize the fitness.
Tests that the Select method works correctly when trying to minimize the fitness .
[Fact]
        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; 
            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).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]);
        }
[ Fact ] 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 ; 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 ) . 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 ] ) ; }
Tests that the Select method works correctly when trying to minimize the fitness.
Tests that the Select method works correctly when trying to minimize the fitness .
// Slice size: 5 // Slice size: 1 // Slice size: 4