public void ExecutionPanelViewModel_StepExecutionAsync_FromPaused() { GeneticAlgorithm algorithm = CreateTestAlgorithm(true); ExecutionContext context = new ExecutionContext(algorithm) { ExecutionState = ExecutionState.Idle }; using (ExecutionPanelViewModel viewModel = new ExecutionPanelViewModel(context)) { Task task = Task.Run(async() => await viewModel.StepExecutionAsync()); // Wait for the algorithm to finish execution TestHelper.WaitForResult(ExecutionState.Paused, () => context.ExecutionState); GeneticEnvironment environment = algorithm.Environment; TestHelper.WaitForPropertyChanged(context, new[] { new Tuple <string, object>(nameof(context.ExecutionState), ExecutionState.Running), new Tuple <string, object>(nameof(context.ExecutionState), ExecutionState.Paused) }, () => task = Task.Run(async() => await viewModel.StepExecutionAsync())); // Verify InitializeAsync was not called Assert.Same(environment, algorithm.Environment); Assert.True(task.IsCompleted); } }
public void ExecutionPanelViewModel_StepExecutionAsync_FromPaused_ToCompletion() { GeneticAlgorithm algorithm = CreateTestAlgorithm(true); ExecutionContext context = new ExecutionContext(algorithm) { ExecutionState = ExecutionState.Idle }; using (ExecutionPanelViewModel viewModel = new ExecutionPanelViewModel(context)) { Task task = Task.Run(async() => await viewModel.StepExecutionAsync()); // Wait for the algorithm to finish execution TestHelper.WaitForResult(ExecutionState.Paused, () => context.ExecutionState); GeneticEnvironment environment = algorithm.Environment; // Trigger the algorithm to complete ((TestTerminator)algorithm.Terminator).IsCompleteValue = true; // Resume execution task = Task.Run(async() => await viewModel.StepExecutionAsync()); // Wait for the algorithm to finish TestHelper.WaitForResult(ExecutionState.Idle, () => context.ExecutionState); // Verify InitializeAsync was not called Assert.Same(environment, algorithm.Environment); Assert.True(task.IsCompleted); } }
public async Task GeneticEnvironment_EvaluateFitness_Async() { GeneticAlgorithm algorithm = new MockGeneticAlgorithm { PopulationSeed = new MockPopulation(), GeneticEntitySeed = new MockEntity(), FitnessEvaluator = new MockFitnessEvaluator(), SelectionOperator = new MockSelectionOperator { SelectionBasedOnFitnessType = FitnessType.Scaled } }; algorithm.FitnessEvaluator = new MockFitnessEvaluator(); algorithm.FitnessEvaluator.Initialize(algorithm); algorithm.SelectionOperator = new MockSelectionOperator(); algorithm.SelectionOperator.Initialize(algorithm); GeneticEnvironment environment = new GeneticEnvironment(algorithm); MockPopulation population1 = GetPopulation(algorithm); MockPopulation population2 = GetPopulation(algorithm); environment.Populations.Add(population1); environment.Populations.Add(population2); await environment.EvaluateFitnessAsync(); VerifyFitnessEvaluation(population1); VerifyFitnessEvaluation(population2); }
public async Task GeneticEnvironment_Initialize_Async() { int environmentSize = 2; int populationSize = 5; GeneticAlgorithm algorithm = new MockGeneticAlgorithm { FitnessEvaluator = new MockFitnessEvaluator(), SelectionOperator = new MockSelectionOperator(), GeneticEntitySeed = new MockEntity(), MinimumEnvironmentSize = environmentSize, PopulationSeed = new MockPopulation { MinimumPopulationSize = populationSize } }; algorithm.GeneticEntitySeed.Initialize(algorithm); algorithm.PopulationSeed.Initialize(algorithm); GeneticEnvironment environment = new GeneticEnvironment(algorithm); await environment.InitializeAsync(); Assert.Equal(environmentSize, environment.Populations.Count); for (int i = 0; i < environmentSize; i++) { Assert.Equal(i, environment.Populations[i].Index); Assert.IsType <MockPopulation>(environment.Populations[i]); Assert.Equal(populationSize, environment.Populations[i].Entities.Count); } }
public void EnvironmentFitnessEvaluatedEventArgs_InvalidGenerationIndex() { GeneticEnvironment environment = new GeneticEnvironment(new MockGeneticAlgorithm()); int generationIndex = -1; Assert.Throws <ArgumentOutOfRangeException>(() => new EnvironmentFitnessEvaluatedEventArgs( environment, generationIndex)); }
public void EnvironmentViewer_EnvironmentProperty() { EnvironmentViewer viewer = new EnvironmentViewer(); GeneticEnvironment environment = new GeneticEnvironment(Mock.Of <GeneticAlgorithm>()); viewer.Environment = environment; Assert.Same(environment, viewer.Environment); }
public void EnvironmentFitnessEvaluatedEventArgs_Constructor() { GeneticEnvironment environment = new GeneticEnvironment(new MockGeneticAlgorithm()); int generationIndex = 2; EnvironmentFitnessEvaluatedEventArgs args = new EnvironmentFitnessEvaluatedEventArgs( environment, generationIndex); Assert.Same(environment, args.Environment); Assert.Equal(generationIndex, args.GenerationIndex); }
public void PopulationSelector_OnEnvironmentChanged_WithPopulation() { PopulationSelector selector = new PopulationSelector(); GeneticEnvironment environment = new GeneticEnvironment(Mock.Of <GeneticAlgorithm>()); Population population = Mock.Of <Population>(); environment.Populations.Add(population); selector.Environment = environment; Assert.Same(population, selector.SelectedPopulation); }
public void PopulationSelector_InitialPopulationAdded() { PopulationSelector selector = new PopulationSelector(); GeneticEnvironment environment = new GeneticEnvironment(Mock.Of <GeneticAlgorithm>()); selector.Environment = environment; Population population = Mock.Of <Population>(); environment.Populations.Add(population); DispatcherHelper.DoEvents(); Assert.Same(population, selector.SelectedPopulation); }
public void PopulationSelector_SelectedPopulationRemoved() { PopulationSelector selector = new PopulationSelector(); GeneticEnvironment environment = new GeneticEnvironment(Mock.Of <GeneticAlgorithm>()); Population population = Mock.Of <Population>(); environment.Populations.Add(population); selector.Environment = environment; environment.Populations.RemoveAt(0); DispatcherHelper.DoEvents(); Assert.Null(selector.SelectedPopulation); }
public void PopulationSelector_SelectedPopulationIndexChanged_GreaterThanAllowed() { PopulationSelector selector = new PopulationSelector(); GeneticEnvironment environment = new GeneticEnvironment(Mock.Of <GeneticAlgorithm>()); Population population = Mock.Of <Population>(); environment.Populations.Add(population); selector.Environment = environment; Assert.Same(population, selector.SelectedPopulation); selector.SelectedPopulationIndex = 1; Assert.Null(selector.SelectedPopulation); }
public void GeneticEnvironment_Serialization() { GeneticEnvironment environment = new GeneticEnvironment(new MockGeneticAlgorithm()); environment.Populations.Add(new MockPopulation()); GeneticEnvironment result = (GeneticEnvironment)SerializationHelper.TestSerialization(environment, new Type[] { typeof(MockGeneticAlgorithm), typeof(MockPopulation), typeof(DefaultTerminator) }); Assert.IsType <MockPopulation>(result.Populations[0]); PrivateObject privObj = new PrivateObject(environment); Assert.IsType <MockGeneticAlgorithm>(privObj.GetField("algorithm")); }
public void PopulationSelector_SelectedPopulationUpdatedOnRemoval_NonDefaultIndex() { PopulationSelector selector = new PopulationSelector(); GeneticEnvironment environment = new GeneticEnvironment(Mock.Of <GeneticAlgorithm>()); Population population = Mock.Of <Population>(); environment.Populations.Add(population); Population population2 = Mock.Of <Population>(); environment.Populations.Add(population2); selector.Environment = environment; selector.SelectedPopulationIndex = 1; environment.Populations.RemoveAt(1); DispatcherHelper.DoEvents(); Assert.Same(population, selector.SelectedPopulation); }
public ClientContext() { DefaultAccount = new Account(); PortfolioManager = new PortfolioManager(); GeneticEnvironment = new GeneticEnvironment(); MarketWatch = new MarketWatch(); MarketMeter = new MarketMeter(); PerformanceLogic = new PerformanceLogic(); PositionLogic = new PositionLogic(); StrategyRepository = new StrategyRepository(); IndicatorRepository = new IndicatorRepository(); PersistenceLogic = new PersistenceLogic(); PersistenceLogic.Register(this); //we need marketWatch.Instruments in strategyRep before connecting to server when loading report StrategyRepository.Register(MarketWatch, null); //we need this when loading performance PerformanceLogic.Register(this); }
/// <summary> /// Handles the event when a generation has been created. /// </summary> /// <param name="environment">The environment representing the generation that was created.</param> /// <param name="generationIndex">Index value of the generation that was created.</param> protected override void OnFitnessEvaluated(GeneticEnvironment environment, int generationIndex) { if (environment == null) { throw new ArgumentNullException(nameof(environment)); } if (this.Algorithm == null) { return; } foreach (Population population in environment.Populations) { foreach (Metric metric in this.Algorithm.Metrics) { string?metricVal = metric.GetResultValue(population)?.ToString(); string metricName; // TypeDescriptor will always provide a DisplayNameAttribute, even if one isn't declared on the type. DisplayNameAttribute attrib = (DisplayNameAttribute)TypeDescriptor.GetAttributes(metric)[typeof(DisplayNameAttribute)]; if (!String.IsNullOrEmpty(attrib.DisplayName)) { metricName = attrib.DisplayName; } else { metricName = metric.ToString(); } if (metricVal == null) { metricVal = "<null>"; } this.WriteTrace(String.Format(CultureInfo.CurrentCulture, Resources.MetricLogger_StatTrace, metricName, metricVal, population.Index, generationIndex)); } } }
/// <summary> /// Handles the event when the <see cref="Environment"/> property changes. /// </summary> /// <param name="obj">The <see cref="DependencyObject"/> that owns the property.</param> /// <param name="e">The <see cref="DependencyPropertyChangedEventArgs"/> associated with the event.</param> private static void OnEnvironmentChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e) { PopulationSelector selector = (PopulationSelector)obj; if (e.OldValue != null) { GeneticEnvironment environment = (GeneticEnvironment)e.OldValue; environment.Populations.CollectionChanged -= selector.Populations_CollectionChanged; selector.isSelectedPopulationInitialized = false; selector.SelectedPopulationIndex = -1; } if (e.NewValue != null) { GeneticEnvironment environment = (GeneticEnvironment)e.NewValue; environment.Populations.CollectionChanged += selector.Populations_CollectionChanged; if (environment.Populations.Any()) { selector.TryInitializeSelectedPopulation(); } } }
public void PopulationSelector_OnEnvironmentChanged_RemoveEnvironment() { PopulationSelector selector = new PopulationSelector(); GeneticEnvironment environment = new GeneticEnvironment(Mock.Of <GeneticAlgorithm>()); Population population = Mock.Of <Population>(); environment.Populations.Add(population); selector.Environment = environment; selector.Environment = null; Assert.Null(selector.SelectedPopulation); Assert.Equal(-1, selector.SelectedPopulationIndex); // Verify the control doesn't respond to a population being added to removed environment environment.Populations.Add(Mock.Of <Population>()); Assert.Null(selector.SelectedPopulation); Assert.Equal(-1, selector.SelectedPopulationIndex); }
public void MetricLogger_FitnessEvaluated() { MockGeneticAlgorithm algorithm = new MockGeneticAlgorithm(); algorithm.Metrics.Add(new TestMetric1()); algorithm.Metrics.Add(new TestMetric2()); algorithm.Metrics.Add(new TestMetric3()); MetricLogger logger = new MetricLogger { TraceCategory = "test" }; logger.Initialize(algorithm); Assert.Equal(String.Empty, this.traceListener.Output.ToString()); GeneticEnvironment environment = new GeneticEnvironment(algorithm); environment.Populations.Add(new MockPopulation { Index = 0 }); environment.Populations.Add(new MockPopulation { Index = 1 }); PrivateObject accessor = new PrivateObject(logger); accessor.Invoke("OnFitnessEvaluated", environment, 0); string[] lines = this.traceListener.Output.ToString().Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries); Assert.Equal(24, lines.Length); this.VerifyMetricOutput(lines.Take(4).ToArray(), logger.TraceCategory, "Metric 1", "1:0", 0, 0); this.VerifyMetricOutput(lines.Skip(4).Take(4).ToArray(), logger.TraceCategory, "Metric 2", "2:0", 0, 0); this.VerifyMetricOutput(lines.Skip(8).Take(4).ToArray(), logger.TraceCategory, typeof(TestMetric3).FullName, "3:0", 0, 0); this.VerifyMetricOutput(lines.Skip(12).Take(4).ToArray(), logger.TraceCategory, "Metric 1", "1:1", 1, 0); this.VerifyMetricOutput(lines.Skip(16).Take(4).ToArray(), logger.TraceCategory, "Metric 2", "2:1", 1, 0); this.VerifyMetricOutput(lines.Skip(20).Take(4).ToArray(), logger.TraceCategory, typeof(TestMetric3).FullName, "3:1", 1, 0); }
protected override void OnFitnessEvaluated(GeneticEnvironment environment, int generationIndex) { this.OnFitnessEvaluatedCalled = true; base.OnFitnessEvaluated(environment, generationIndex); }