示例#1
0
        private async Task TestRefreshChartScenario(FitnessType fitnessType, FitnessSortOption fitnessSortOption,
                                                    bool switchPopulations, bool createNewGeneration, bool completeAlgorithm, bool elapseTime)
        {
            TimeSpan          elapsedTime   = TimeSpan.FromSeconds(0);
            Mock <IStopwatch> stopwatchMock = new Mock <IStopwatch>();

            stopwatchMock
            .SetupGet(o => o.Elapsed)
            .Returns(() => elapsedTime);
            Mock <IStopwatchFactory> stopwatchFactoryMock = new Mock <IStopwatchFactory>();

            stopwatchFactoryMock
            .Setup(o => o.Create())
            .Returns(stopwatchMock.Object);

            FitnessChart chart = new FitnessChart(stopwatchFactoryMock.Object)
            {
                FitnessSortOption = fitnessSortOption,
                FitnessType       = fitnessType
            };

            PlotModel    model = chart.PlotModel;
            CategoryAxis axis  = (CategoryAxis)model.Axes[0];

            axis.Labels.Add("test"); // Add test label to ensure it gets cleared

            TestAlgorithm algorithm = new TestAlgorithm
            {
                MinimumEnvironmentSize = 2,
                PopulationSeed         = new TestPopulation()
                {
                    MinimumPopulationSize = 5
                },
                FitnessEvaluator  = new TestFitnessEvaluator(),
                GeneticEntitySeed = new TestEntity(),
                SelectionOperator = new TestSelectionOperator(),
                Terminator        = new TestTerminator()
            };

            await algorithm.InitializeAsync();

            if (switchPopulations)
            {
                chart.Population = algorithm.Environment.Populations[1];
            }

            Population population = algorithm.Environment.Populations[0];

            TestEntity[] entities = population.Entities.Cast <TestEntity>().ToArray();
            entities[0].RawFitnessValue = 2;
            entities[1].RawFitnessValue = 0;
            entities[2].RawFitnessValue = 1;
            entities[3].RawFitnessValue = 4;
            entities[4].RawFitnessValue = 3;

            entities[0].ScaledFitnessValue = 0;
            entities[1].ScaledFitnessValue = 3;
            entities[2].ScaledFitnessValue = 2;
            entities[3].ScaledFitnessValue = 4;
            entities[4].ScaledFitnessValue = 1;

            entities[0].CompareFactor = 4;
            entities[1].CompareFactor = 0;
            entities[2].CompareFactor = 3;
            entities[3].CompareFactor = 2;
            entities[4].CompareFactor = 1;

            // Set the Population which will trigger the logic to test
            chart.Population = population;

            stopwatchFactoryMock.Verify(o => o.Create(), Times.Once());
            stopwatchMock.Verify(o => o.Start(), Times.Once());
            stopwatchMock.Verify(o => o.Restart(), switchPopulations ? Times.Exactly(2) : Times.Once());

            List <GeneticEntity> sortedEntities;

            if (fitnessSortOption == FitnessSortOption.Entity)
            {
                sortedEntities = new List <GeneticEntity>
                {
                    entities[1],
                    entities[4],
                    entities[3],
                    entities[2],
                    entities[0],
                };
            }
            else
            {
                if (fitnessType == FitnessType.Raw)
                {
                    sortedEntities = new List <GeneticEntity>
                    {
                        entities[1],
                        entities[2],
                        entities[0],
                        entities[4],
                        entities[3],
                    };
                }
                else
                {
                    sortedEntities = new List <GeneticEntity>
                    {
                        entities[0],
                        entities[4],
                        entities[2],
                        entities[1],
                        entities[3],
                    };
                }
            }

            ColumnSeries columnSeries = (ColumnSeries)model.Series[0];

            if (fitnessType == FitnessType.Scaled)
            {
                Assert.Equal(nameof(GeneticEntity.ScaledFitnessValue), columnSeries.ValueField);
            }
            else
            {
                Assert.Equal(nameof(GeneticEntity.RawFitnessValue), columnSeries.ValueField);
            }

            Assert.Equal(sortedEntities, columnSeries.ItemsSource.Cast <object>().ToList());

            Assert.Equal(algorithm.PopulationSeed.MinimumPopulationSize, axis.Labels.Count);
            for (int i = 0; i < algorithm.PopulationSeed.MinimumPopulationSize; i++)
            {
                Assert.Equal("", axis.Labels[i]);
            }

            if (createNewGeneration)
            {
                if (elapseTime)
                {
                    // Ensure that enough time has passed for refresh to occur
                    elapsedTime = TimeSpan.FromSeconds(5);
                }

                // Create next generation to cause the chart to be refresh again
                await algorithm.StepAsync();

                if (elapseTime)
                {
                    Assert.NotEqual(sortedEntities,
                                    columnSeries.ItemsSource.Cast <object>().ToList());
                }
            }

            if (completeAlgorithm)
            {
                if (elapseTime)
                {
                    // Ensure that enough time has passed for refresh to occur
                    elapsedTime = TimeSpan.FromSeconds(5);
                }

                ((TestTerminator)algorithm.Terminator).IsCompleteValue = true;
                // Create next generation to cause the chart to be refresh again
                await algorithm.StepAsync();

                if (createNewGeneration && elapseTime)
                {
                    Assert.NotEqual(sortedEntities,
                                    columnSeries.ItemsSource.Cast <object>().ToList());
                }
                else
                {
                    Assert.Equal(sortedEntities,
                                 columnSeries.ItemsSource.Cast <object>().ToList());
                }
            }

            // Set the population to null to verify the series gets cleared
            chart.Population = null;
            Assert.Null(columnSeries.ItemsSource);
        }