Beispiel #1
0
        /// <summary>
        /// Converts a value.
        /// </summary>
        /// <param name="value">The value produced by the binding source.</param>
        /// <param name="targetType">The type of the binding target property.</param>
        /// <param name="parameter">The converter parameter to use.</param>
        /// <param name="culture">The culture to use in the converter.</param>
        /// <returns>A converted value. If the method returns null, the valid null value is used.</returns>
        public object?Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value is null)
            {
                return(null);
            }

            object?returnValue = null;

            if (value.GetType() == typeof(FitnessType))
            {
                FitnessType fitnessType = (FitnessType)value;
                switch (fitnessType)
                {
                case FitnessType.Scaled:
                    returnValue = EnumsViewModel.FitnessTypeScaled;
                    break;

                case FitnessType.Raw:
                    returnValue = EnumsViewModel.FitnessTypeRaw;
                    break;

                default:
                    returnValue = null;
                    break;
                }
            }
            else if (value.GetType() == typeof(FitnessSortOption))
            {
                FitnessSortOption fitnessSortOption = (FitnessSortOption)value;
                switch (fitnessSortOption)
                {
                case FitnessSortOption.Entity:
                    returnValue = EnumsViewModel.FitnessSortByEntity;
                    break;

                case FitnessSortOption.Fitness:
                    returnValue = EnumsViewModel.FitnessSortByFitness;
                    break;

                default:
                    returnValue = null;
                    break;
                }
            }

            return(returnValue);
        }
Beispiel #2
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);
        }