コード例 #1
0
        public static NeatEvolutionAlgorithm <double> CreateNeatEvolutionAlgorithm(
            INeatExperimentFactory <double> experimentFactory,
            string jsonConfigFilename)
        {
            // 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 = NeatExperimentUtils.CreateNeatEvolutionAlgorithm(neatExperiment);

            return(ea);
        }
コード例 #2
0
ファイル: Utils.cs プロジェクト: eschurma/sharpneat-refactor
        public static NeatEvolutionAlgorithm <double> CreateNeatEvolutionAlgorithm(
            INeatExperimentFactory <double> experimentFactory,
            string jsonConfigFilename)
        {
            // Read experiment json config from file.
            // Note. We read the entire contents into a string; we don't ever expect to see large json files here, so this fine.
            string jsonStr = File.ReadAllText(jsonConfigFilename);

            // Create an instance of INeatExperiment, configured using the supplied json config.
            INeatExperiment <double> neatExperiment = experimentFactory.CreateExperiment(jsonStr);

            // Create a NeatEvolutionAlgorithm instance ready to run the experiment.
            var ea = NeatExperimentUtils.CreateNeatEvolutionAlgorithm(neatExperiment);

            return(ea);
        }
コード例 #3
0
        /// <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 = NeatExperimentUtils.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 Sample();

            sample.ElapsedTimeSecs = _stopwatch.ElapsedMilliseconds * 0.001;
            sample.GenerationCount = (int)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.
            ea = null;
            GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced, true);

            // Return the sample.
            return(sample);
        }
コード例 #4
0
        /// <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 = NeatExperimentUtils.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);
        }