private void btnSearchStart_Click(object sender, EventArgs e) { if (_eaRunner is not null) { // Resume existing EA & update GUI state. _eaRunner.StartOrResume(); UpdateUIState(); return; } // Get the current neat experiment, with parameters set from the UI. INeatExperiment <double> neatExperiment = GetNeatExperiment(); // Create evolution algorithm and runner. NeatEvolutionAlgorithm <double> ea = NeatUtils.CreateNeatEvolutionAlgorithm(neatExperiment, _neatPop); ea.Initialise(); _eaRunner = new EvolutionAlgorithmRunner(ea, UpdateScheme.CreateTimeSpanUpdateScheme(TimeSpan.FromSeconds(1))); // Attach event listeners. _eaRunner.UpdateEvent += _eaRunner_UpdateEvent; // Start the algorithm & update GUI state. _eaRunner.StartOrResume(); UpdateUIState(); }
private void btnCreateRandomPop_Click(object sender, EventArgs e) { INeatExperiment <double> neatExperiment = GetNeatExperiment(); MetaNeatGenome <double> metaNeatGenome = NeatUtils.CreateMetaNeatGenome(neatExperiment); // Create an initial population of genomes. _neatPop = NeatPopulationFactory <double> .CreatePopulation( metaNeatGenome, connectionsProportion : neatExperiment.InitialInterconnectionsProportion, popSize : neatExperiment.PopulationSize); // Update UI. UpdateUIState(); }
public static NeatEvolutionAlgorithm <double> CreateNeatEvolutionAlgorithm( INeatExperimentFactory experimentFactory) { string jsonConfigFilename = $"experiments-config/{experimentFactory.Id}.config.json"; // Load experiment json config from file. JsonDocument configDoc = JsonUtils.LoadUtf8(jsonConfigFilename); // Create an instance of INeatExperiment, configured using the supplied json config. INeatExperiment <double> neatExperiment = experimentFactory.CreateExperiment(configDoc.RootElement); // Create a NeatEvolutionAlgorithm instance ready to run the experiment. var ea = NeatUtils.CreateNeatEvolutionAlgorithm(neatExperiment); return(ea); }
/// <summary> /// Initialise and run the evolutionary algorithm until the stop condition occurs (either elapsed clock time, or some number of generations). /// Once the stop condition is reached this method returns with the current best fitness in the population. /// </summary> /// <returns></returns> public Sample Sample() { // Create a new instance of an evolution algorithm. NeatEvolutionAlgorithm <double> ea = NeatUtils.CreateNeatEvolutionAlgorithm(_experiment); // Start the stopwatch. _stopwatch.Restart(); // We include clock time spent doing initialisation in the recorded stats for each sample; // this is the scientifically robust approach as initialisation might perform a lot of work. ea.Initialise(); // Run the main EA loop for the required number of generations. for (int i = 0; i < _stopGenerationCount; i++) { ea.PerformOneGeneration(); } // Stop the stopwatch. _stopwatch.Stop(); // Copy the required stats into a new Sample instance. Sample sample = new() { ElapsedTimeSecs = _stopwatch.ElapsedMilliseconds * 0.001, GenerationCount = ea.Stats.Generation }; var pop = ea.Population; sample.BestFitness = pop.Stats.BestFitness.PrimaryFitness; sample.MeanFitness = pop.Stats.MeanFitness; // TODO: Store max complexity? Or should we use BestComplexity? Or both? //sample.MaxComplexity = ; sample.MeanComplexity = pop.Stats.MeanComplexity; sample.EvaluationCount = ea.Stats.TotalEvaluationCount; // Make some attempts at forcing release of resources (especially RAM) before we hand control back. GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced, true); // Return the sample. return(sample); } #endregion }
/// <summary> /// Initialise and run the evolutionary algorithm until the stop condition occurs (either elapsed clock time, or some number of generations). /// Once the stop condition is reached this method returns with the current best fitness in the population. /// </summary> /// <returns></returns> public Sample Sample() { // Create a new instance of an evolution algorithm. _ea = NeatUtils.CreateNeatEvolutionAlgorithm(_experiment); // Start the stopwatch. _stopwatch.Restart(); // Signal the EA thread to start. Ensure the stop flag is reset before we do so. _stopFlag = false; Thread.MemoryBarrier(); _awaitStartEvent.Set(); // Block this thread (the main thread) for the required amount of clock time. Block(_stopTimeSpan); // Signal the EA thread to stop. _stopFlag = true; Thread.MemoryBarrier(); // Record the sample without waiting for the EA to stop. // The EA may be within a PerformOneGeneration() call for some time, so we prefer to record the sample // at the exact required point in time (or very close to it) rather than some arbitrary point in time // after the last generation has completed. Sample sample = RecordSample(); // Now wait for the EA to signal that it has stopped. _awaitStopEvent.WaitOne(); // Make some attempts at forcing release of resources (especially RAM) before we hand control back. _ea = null; GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced, true); // Return the sample. return(sample); }