public void ExecutionPanelViewModel_Dispose()
        {
            GeneticAlgorithm algorithm = CreateTestAlgorithm();

            ExecutionContext context = new ExecutionContext(algorithm);

            ExecutionPanelViewModel viewModel = new ExecutionPanelViewModel(context);

            context.ExecutionState = ExecutionState.Running;

            viewModel.Dispose();

            // Verify the ExecutionState is not reset when the genetic algorithm completes.
            PrivateObject algorithmAccessor = new PrivateObject(algorithm, new PrivateType(typeof(GeneticAlgorithm)));

            algorithmAccessor.Invoke("OnAlgorithmCompleted");
            Assert.Equal(ExecutionState.Running, context.ExecutionState);

            // Verify the view model has unsubscribed from the FitnessEvaluated event.
            context.ExecutionState = ExecutionState.PausePending;
            EnvironmentFitnessEvaluatedEventArgs eventArgs = new EnvironmentFitnessEvaluatedEventArgs(new GeneticEnvironment(algorithm), 0);

            algorithmAccessor.Invoke("OnFitnessEvaluated", eventArgs);
            Assert.False(eventArgs.Cancel);
        }
        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);
        }
예제 #3
0
 /// <summary>
 /// Handles the event when the algorithm has evaluated the fitness of a population.
 /// </summary>
 /// <param name="sender">Sender of the event.</param>
 /// <param name="e">The<see cref="EnvironmentFitnessEvaluatedEventArgs"/> associated with the event.</param>
 private void Algorithm_FitnessEvaluated(object?sender, EnvironmentFitnessEvaluatedEventArgs e)
 {
     if (e.GenerationIndex == 0)
     {
         this.RefreshAllSeries();
     }
     else
     {
         this.RefreshPlot();
     }
 }
예제 #4
0
        /// <summary>
        /// Handles the event when the algorithm has evaluated the fitness of a population.
        /// </summary>
        /// <param name="sender">Sender of the event.</param>
        /// <param name="e">The<see cref="EnvironmentFitnessEvaluatedEventArgs"/> associated with the event.</param>
        private void Algorithm_FitnessEvaluated(object?sender, EnvironmentFitnessEvaluatedEventArgs e)
        {
            this.Dispatcher.Invoke(() =>
            {
                bool forceRefresh = false;
                if (this.TryInitializeStopwatch())
                {
                    forceRefresh = true;
                }

                this.RefreshChart(forceRefresh);
            });
        }
예제 #5
0
        /// <summary>
        /// Handles the event when the associated <see cref="GeneticAlgorithm"/> has evaluted the fitness of a generation.
        /// </summary>
        /// <param name="sender">Sender of the event.</param>
        /// <param name="e">The <see cref="EnvironmentFitnessEvaluatedEventArgs"/> associated with the event.</param>
        private void GeneticAlgorithm_FitnessEvaluated(object?sender, EnvironmentFitnessEvaluatedEventArgs e)
        {
            if (this.context.ExecutionState == ExecutionState.PausePending ||
                this.context.ExecutionState == ExecutionState.IdlePending)
            {
                e.Cancel = true;

                if (this.context.ExecutionState == ExecutionState.PausePending)
                {
                    this.context.ExecutionState = ExecutionState.Paused;
                }
                else
                {
                    this.context.ExecutionState = ExecutionState.Idle;
                    this.UnsubscribeFromAlgorithmEvents();
                }
            }
        }