src/GenFx.Components.Tests/RankSelectionOperatorTest.cs
/// <summary>
/// Tests that the Select method works correctly.
/// </summary>
Tests that the Select method works correctly.
Tests that the Select method works correctly .
[Fact]
        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]);
        }
[ Fact ] 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 ] ) ; }
Tests that the Select method works correctly.
Tests that the Select method works correctly .