src/GenFx.Components.Tests/MultiDemeGeneticAlgorithmTest.cs
/// <summary>
/// Tests that the Migrate method works correctly.
/// </summary>
Tests that the Migrate method works correctly.
Tests that the Migrate method works correctly .
[Fact]
        public async Task MultiDemeGeneticAlgorithm_Migrate()
        {
            MultiDemeGeneticAlgorithm algorithm = new MultiDemeGeneticAlgorithm
            {
                MinimumEnvironmentSize = 3,
                GeneticEntitySeed = new MockEntity(),
                PopulationSeed = new SimplePopulation
                {
                    MinimumPopulationSize = 4
                },
                FitnessEvaluator = new MockFitnessEvaluator(),
                MigrantCount = 2,
                SelectionOperator = new MockSelectionOperator
                {
                    SelectionBasedOnFitnessType = FitnessType.Scaled
                },
            };
            algorithm.SelectionOperator = new MockSelectionOperator();
            algorithm.FitnessEvaluator = new MockFitnessEvaluator();
            await algorithm.InitializeAsync();
            SimplePopulation population1 = (SimplePopulation)algorithm.Environment.Populations[0];
            population1.Entities[0].ScaledFitnessValue = 1;
            population1.Entities[1].ScaledFitnessValue = 5;
            population1.Entities[2].ScaledFitnessValue = 2;
            population1.Entities[3].ScaledFitnessValue = 4;
            SimplePopulation population2 = (SimplePopulation)algorithm.Environment.Populations[1];
            population2.Entities[0].ScaledFitnessValue = 6;
            population2.Entities[1].ScaledFitnessValue = 3;
            population2.Entities[2].ScaledFitnessValue = 8;
            population2.Entities[3].ScaledFitnessValue = 7;
            SimplePopulation population3 = (SimplePopulation)algorithm.Environment.Populations[2];
            population3.Entities[0].ScaledFitnessValue = 9;
            population3.Entities[1].ScaledFitnessValue = 13;
            population3.Entities[2].ScaledFitnessValue = 10;
            population3.Entities[3].ScaledFitnessValue = 12;
            algorithm.Migrate();
            Assert.Equal((double)1, population1.Entities[0].ScaledFitnessValue);
            Assert.Equal((double)2, population1.Entities[1].ScaledFitnessValue);
            Assert.Equal((double)13, population1.Entities[2].ScaledFitnessValue);
            Assert.Equal((double)12, population1.Entities[3].ScaledFitnessValue);
            Assert.Equal((double)6, population2.Entities[0].ScaledFitnessValue);
            Assert.Equal((double)3, population2.Entities[1].ScaledFitnessValue);
            Assert.Equal((double)5, population2.Entities[2].ScaledFitnessValue);
            Assert.Equal((double)4, population2.Entities[3].ScaledFitnessValue);
            Assert.Equal((double)9, population3.Entities[0].ScaledFitnessValue);
            Assert.Equal((double)10, population3.Entities[1].ScaledFitnessValue);
            Assert.Equal((double)8, population3.Entities[2].ScaledFitnessValue);
            Assert.Equal((double)7, population3.Entities[3].ScaledFitnessValue);
        }
[ Fact ] public async Task MultiDemeGeneticAlgorithm_Migrate ( ) { MultiDemeGeneticAlgorithm algorithm = new MultiDemeGeneticAlgorithm { MinimumEnvironmentSize = 3 , GeneticEntitySeed = new MockEntity ( ) , PopulationSeed = new SimplePopulation { MinimumPopulationSize = 4 } , FitnessEvaluator = new MockFitnessEvaluator ( ) , MigrantCount = 2 , SelectionOperator = new MockSelectionOperator { SelectionBasedOnFitnessType = FitnessType . Scaled } , } ; algorithm . SelectionOperator = new MockSelectionOperator ( ) ; algorithm . FitnessEvaluator = new MockFitnessEvaluator ( ) ; await algorithm . InitializeAsync ( ) ; SimplePopulation population1 = ( SimplePopulation ) algorithm . Environment . Populations [ 0 ] ; population1 . Entities [ 0 ] . ScaledFitnessValue = 1 ; population1 . Entities [ 1 ] . ScaledFitnessValue = 5 ; population1 . Entities [ 2 ] . ScaledFitnessValue = 2 ; population1 . Entities [ 3 ] . ScaledFitnessValue = 4 ; SimplePopulation population2 = ( SimplePopulation ) algorithm . Environment . Populations [ 1 ] ; population2 . Entities [ 0 ] . ScaledFitnessValue = 6 ; population2 . Entities [ 1 ] . ScaledFitnessValue = 3 ; population2 . Entities [ 2 ] . ScaledFitnessValue = 8 ; population2 . Entities [ 3 ] . ScaledFitnessValue = 7 ; SimplePopulation population3 = ( SimplePopulation ) algorithm . Environment . Populations [ 2 ] ; population3 . Entities [ 0 ] . ScaledFitnessValue = 9 ; population3 . Entities [ 1 ] . ScaledFitnessValue = 13 ; population3 . Entities [ 2 ] . ScaledFitnessValue = 10 ; population3 . Entities [ 3 ] . ScaledFitnessValue = 12 ; algorithm . Migrate ( ) ; Assert . Equal ( ( double ) 1 , population1 . Entities [ 0 ] . ScaledFitnessValue ) ; Assert . Equal ( ( double ) 2 , population1 . Entities [ 1 ] . ScaledFitnessValue ) ; Assert . Equal ( ( double ) 13 , population1 . Entities [ 2 ] . ScaledFitnessValue ) ; Assert . Equal ( ( double ) 12 , population1 . Entities [ 3 ] . ScaledFitnessValue ) ; Assert . Equal ( ( double ) 6 , population2 . Entities [ 0 ] . ScaledFitnessValue ) ; Assert . Equal ( ( double ) 3 , population2 . Entities [ 1 ] . ScaledFitnessValue ) ; Assert . Equal ( ( double ) 5 , population2 . Entities [ 2 ] . ScaledFitnessValue ) ; Assert . Equal ( ( double ) 4 , population2 . Entities [ 3 ] . ScaledFitnessValue ) ; Assert . Equal ( ( double ) 9 , population3 . Entities [ 0 ] . ScaledFitnessValue ) ; Assert . Equal ( ( double ) 10 , population3 . Entities [ 1 ] . ScaledFitnessValue ) ; Assert . Equal ( ( double ) 8 , population3 . Entities [ 2 ] . ScaledFitnessValue ) ; Assert . Equal ( ( double ) 7 , population3 . Entities [ 3 ] . ScaledFitnessValue ) ; }
Tests that the Migrate method works correctly.
Tests that the Migrate method works correctly .