/// <summary>
        /// Create and return a NeatEvolutionAlgorithm object ready for running the NEAT algorithm/search. Various sub-parts
        /// of the algorithm are also constructed and connected up.
        /// This overload accepts a pre-built genome population and their associated/parent genome factory.
        /// </summary>
        public NeatEvolutionAlgorithm <NeatGenome> CreateEvolutionAlgorithm(IGenomeFactory <NeatGenome> genomeFactory, List <NeatGenome> genomeList)
        {
            // Create distance metric. Mismatched genes have a fixed distance of 10; for matched genes the distance is their weight difference.
            IDistanceMetric distanceMetric = new ManhattanDistanceMetric(1.0, 0.0, 10.0);
            ISpeciationStrategy <NeatGenome> speciationStrategy = new ParallelKMeansClusteringStrategy <NeatGenome>(distanceMetric, _parallelOptions);

            // Create complexity regulation strategy.
            IComplexityRegulationStrategy complexityRegulationStrategy = ExperimentUtils.CreateComplexityRegulationStrategy(_complexityRegulationStr, _complexityThreshold);

            // Create the evolution algorithm.
            NeatEvolutionAlgorithm <NeatGenome> ea = new NeatEvolutionAlgorithm <NeatGenome>(_eaParams, speciationStrategy, complexityRegulationStrategy);

            // Create IBlackBox evaluator.
            BoxesVisualDiscriminationEvaluator evaluator = new BoxesVisualDiscriminationEvaluator(_visualFieldResolution);

            // Create genome decoder. Decodes to a neural network packaged with an activation scheme that defines a fixed number of activations per evaluation.
            IGenomeDecoder <NeatGenome, IBlackBox> genomeDecoder = CreateGenomeDecoder(_visualFieldResolution, _lengthCppnInput);

            // Create a genome list evaluator. This packages up the genome decoder with the genome evaluator.
            IGenomeListEvaluator <NeatGenome> innerEvaluator = new ParallelGenomeListEvaluator <NeatGenome, IBlackBox>(genomeDecoder, evaluator, _parallelOptions);

            // Wrap the list evaluator in a 'selective' evaluator that will only evaluate new genomes. That is, we skip re-evaluating any genomes
            // that were in the population in previous generations (elite genomes). This is determined by examining each genome's evaluation info object.
            IGenomeListEvaluator <NeatGenome> selectiveEvaluator = new SelectiveGenomeListEvaluator <NeatGenome>(
                innerEvaluator,
                SelectiveGenomeListEvaluator <NeatGenome> .CreatePredicate_OnceOnly());

            // Initialize the evolution algorithm.
            ea.Initialize(selectiveEvaluator, genomeFactory, genomeList);

            // Finished. Return the evolution algorithm
            return(ea);
        }
Esempio n. 2
0
        /// <summary>
        /// Create and return a NeatEvolutionAlgorithm object ready for running the NEAT algorithm/search. Various sub-parts
        /// of the algorithm are also constructed and connected up.
        /// This overload accepts a pre-built genome2 population and their associated/parent genome2 factory.
        /// </summary>
        public NeatEvolutionAlgorithm <NeatGenome> CreateEvolutionAlgorithm(IGenomeFactory <NeatGenome> genomeFactory, List <NeatGenome> genomeList)
        {
            // Create distance metric. Mismatched genes have a fixed distance of 10; for matched genes the distance is their weigth difference.
            IDistanceMetric distanceMetric = new ManhattanDistanceMetric(1.0, 0.0, 10.0);
            ISpeciationStrategy <NeatGenome> speciationStrategy = new ParallelKMeansClusteringStrategy <NeatGenome>(distanceMetric, _parallelOptions);

            // Create complexity regulation strategy.
            IComplexityRegulationStrategy complexityRegulationStrategy = ExperimentUtils.CreateComplexityRegulationStrategy(_complexityRegulationStr, _complexityThreshold);

            // Create the evolution algorithm.
            NeatEvolutionAlgorithm <NeatGenome> ea = new NeatEvolutionAlgorithm <NeatGenome>(_eaParams, speciationStrategy, complexityRegulationStrategy);

            // Create genome2 decoder.
            IGenomeDecoder <NeatGenome, IBlackBox> genomeDecoder = new NeatGenomeDecoder(_activationScheme);

            // Create a genome2 list evaluator. This packages up the genome2 decoder with the genome2 evaluator.
            IGenomeListEvaluator <NeatGenome> genomeListEvaluator = new ParallelGenomeListEvaluator <NeatGenome, IBlackBox>(genomeDecoder, PhenomeEvaluator, _parallelOptions);

            // Wrap the list evaluator in a 'selective' evaulator that will only evaluate new genomes. That is, we skip re-evaluating any genomes
            // that were in the population in previous generations (elite genomes). This is determiend by examining each genome2's evaluation info object.
            if (!EvaluateParents)
            {
                genomeListEvaluator = new SelectiveGenomeListEvaluator <NeatGenome>(genomeListEvaluator,
                                                                                    SelectiveGenomeListEvaluator <NeatGenome> .CreatePredicate_OnceOnly());
            }

            // Initialize the evolution algorithm.
            ea.Initialize(genomeListEvaluator, genomeFactory, genomeList);

            // Finished. Return the evolution algorithm
            return(ea);
        }
Esempio n. 3
0
        /// <summary>
        /// Create and return a NeatEvolutionAlgorithm object ready for running the NEAT algorithm/search. Various sub-parts
        /// of the algorithm are also constructed and connected up.
        /// This overload accepts a pre-built genome population and their associated/parent genome factory.
        /// </summary>
        public NeatEvolutionAlgorithm <NeatGenome> CreateEvolutionAlgorithm(IGenomeFactory <NeatGenome> genomeFactory, List <NeatGenome> genomeList)
        {
            // Create distance metric. Mismatched genes have a fixed distance of 10; for matched genes the distance is their weight difference.
            IDistanceMetric distanceMetric = new ManhattanDistanceMetric(1.0, 0.0, 10.0);
            ISpeciationStrategy <NeatGenome> speciationStrategy = new ParallelKMeansClusteringStrategy <NeatGenome>(distanceMetric, _parallelOptions);

            // Create complexity regulation strategy.
            IComplexityRegulationStrategy complexityRegulationStrategy = ExperimentUtils.CreateComplexityRegulationStrategy(_complexityRegulationStr, _complexityThreshold);

            // Create the evolution algorithm.
            NeatEvolutionAlgorithm <NeatGenome> ea = new NeatEvolutionAlgorithm <NeatGenome>(_eaParams, speciationStrategy, complexityRegulationStrategy);

            // Create IBlackBox evaluator.
            PreyCaptureEvaluator evaluator = new PreyCaptureEvaluator(_trialsPerEvaluation, _gridSize, _preyInitMoves, _preySpeed, _sensorRange, _maxTimesteps);

            // Create genome decoder.
            IGenomeDecoder <NeatGenome, IBlackBox> genomeDecoder = CreateGenomeDecoder();

            // TODO: evaluation scheme that re-evaluates existing genomes and takes average over time.
            // Create a genome list evaluator. This packages up the genome decoder with the genome evaluator.
            IGenomeListEvaluator <NeatGenome> genomeListEvaluator = new ParallelGenomeListEvaluator <NeatGenome, IBlackBox>(genomeDecoder, evaluator, _parallelOptions);

            // Initialize the evolution algorithm.
            ea.Initialize(genomeListEvaluator, genomeFactory, genomeList);

            // Finished. Return the evolution algorithm
            return(ea);
        }
Esempio n. 4
0
        private void GetGenomeListEvaluator()
        {
            var innerEvaluator = new ParallelGenomeListEvaluator <NeatGenome, IBlackBox>(_GenomeDecoder, _PhenomeEvaluator, _ParallelOptions);

            _GenomeListEvaluator = new SelectiveGenomeListEvaluator <NeatGenome>(
                innerEvaluator,
                SelectiveGenomeListEvaluator <NeatGenome> .CreatePredicate_OnceOnly());
        }
Esempio n. 5
0
        NeatEvolutionAlgorithm <NeatGenome> CreateEvolutionAlgorithm(bool load)
        {
            // Create a genome2 factory with our neat genome2 parameters object and the appropriate number of input and output neuron genes.
            var genomeFactory = new NeatGenomeFactory(TetrisEvaluator.NumInputs, TetrisEvaluator.NumOutputs);

            // Create an initial population of randomly generated genomes.
            List <NeatGenome> genomeList = null;

            if (load)
            {
                try
                {
                    using (var reader = XmlReader.Create("SavedProgress.xml"))
                        genomeList = NeatGenomeXmlIO.ReadCompleteGenomeList(reader, true, genomeFactory);
                    Console.WriteLine("Loaded network!");
                }
                catch
                {
                    load = false;
                }
            }
            if (!load)
            {
                genomeList = genomeFactory.CreateGenomeList(150, 0);
            }

            var parallelOpts = new ParallelOptions()
            {
                MaxDegreeOfParallelism = -1
            };
            // Create distance metric. Mismatched genes have a fixed distance of 10; for matched genes the distance is their weigth difference.
            var distanceMetric     = new ManhattanDistanceMetric(1.0, 0.0, 10.0);
            var speciationStrategy = new ParallelKMeansClusteringStrategy <NeatGenome>(distanceMetric, parallelOpts);

            // Create the evolution algorithm.
            var ea = new NeatEvolutionAlgorithm <NeatGenome>(new NeatEvolutionAlgorithmParameters {
                SpecieCount = 10
            }, speciationStrategy, new DefaultComplexityRegulationStrategy(ComplexityCeilingType.Absolute, 50));

            // Create genome2 decoder.
            var genomeDecoder = new NeatGenomeDecoder(NetworkActivationScheme.CreateCyclicFixedTimestepsScheme(2));

            // Create a genome2 list evaluator. This packages up the genome2 decoder with the genome2 evaluator.
            IGenomeListEvaluator <NeatGenome> genomeListEvaluator = new ParallelGenomeListEvaluator <NeatGenome, IBlackBox>(genomeDecoder, tetrisEvaluator, parallelOpts);

            // Wrap the list evaluator in a 'selective' evaulator that will only evaluate new genomes. That is, we skip re-evaluating any genomes
            // that were in the population in previous generations (elite genomes). This is determiend by examining each genome2's evaluation info object.
            //if (!EvaluateParents)
            //genomeListEvaluator = new SelectiveGenomeListEvaluator<NeatGenome>(genomeListEvaluator, SelectiveGenomeListEvaluator<NeatGenome>.CreatePredicate_OnceOnly());

            ea.UpdateEvent += Ea_UpdateEvent;

            // Initialize the evolution algorithm.
            ea.Initialize(genomeListEvaluator, genomeFactory, genomeList);

            // Finished. Return the evolution algorithm
            return(ea);
        }
Esempio n. 6
0
        private NeatEvolutionAlgorithm <NeatGenome> CreateEvolutionAlgorithm_private(IGenomeFactory <NeatGenome> genomeFactory, List <NeatGenome> genomeList, HyperNEAT_Args args = null)
        {
            // Create distance metric. Mismatched genes have a fixed distance of 10; for matched genes the distance is their weigth difference.
            IDistanceMetric distanceMetric = new ManhattanDistanceMetric(1, 0, 10);
            ISpeciationStrategy <NeatGenome> speciationStrategy = new ParallelKMeansClusteringStrategy <NeatGenome>(distanceMetric, _parallelOptions);

            // Create complexity regulation strategy.
            IComplexityRegulationStrategy complexityRegulationStrategy = ExperimentUtils.CreateComplexityRegulationStrategy(_complexityRegulationStr, _complexityThreshold);

            // Create the evolution algorithm.
            NeatEvolutionAlgorithm <NeatGenome> retVal = new NeatEvolutionAlgorithm <NeatGenome>(NeatEvolutionAlgorithmParameters, speciationStrategy, complexityRegulationStrategy);

            // Genome Decoder
            IGenomeDecoder <NeatGenome, IBlackBox> genomeDecoder = null;

            if (args == null)
            {
                genomeDecoder = CreateGenomeDecoder();
            }
            else
            {
                genomeDecoder = CreateGenomeDecoder(args);
            }

            // Create a genome list evaluator. This packages up the genome decoder with the genome evaluator.
            IGenomeListEvaluator <NeatGenome> genomeEvaluator = null;

            if (_phenomeEvaluator != null)
            {
                IGenomeListEvaluator <NeatGenome> innerEvaluator = new ParallelGenomeListEvaluator <NeatGenome, IBlackBox>(genomeDecoder, _phenomeEvaluator, _parallelOptions);

                // Wrap the list evaluator in a 'selective' evaluator that will only evaluate new genomes. That is, we skip re-evaluating any genomes
                // that were in the population in previous generations (elite genomes). This is determined by examining each genome's evaluation info object.
                genomeEvaluator = new SelectiveGenomeListEvaluator <NeatGenome>(
                    innerEvaluator,
                    SelectiveGenomeListEvaluator <NeatGenome> .CreatePredicate_OnceOnly());
            }
            else if (_phenomeEvaluators != null)
            {
                // Use the multi tick evaluator
                genomeEvaluator = new TickGenomeListEvaluator <NeatGenome, IBlackBox>(genomeDecoder, _phenomeEvaluators, _phenometickeval_roundRobinManager, _phenometickeval_worldTick);
            }
            else
            {
                throw new ApplicationException("One of the phenome evaluators needs to be populated");
            }

            // Initialize the evolution algorithm.
            retVal.Initialize(genomeEvaluator, genomeFactory, genomeList);

            // Finished. Return the evolution algorithm
            return(retVal);
        }
        /// <summary>
        /// Create and return a NeatEvolutionAlgorithm object ready for running the NEAT algorithm/search. Various sub-parts
        /// of the algorithm are also constructed and connected up.
        /// This overload accepts a pre-built genome population and their associated/parent genome factory.
        /// </summary>
        public NeatEvolutionAlgorithm <NeatGenome> CreateEvolutionAlgorithm(IGenomeFactory <NeatGenome> genomeFactory, List <NeatGenome> genomeList)
        {
            // Create distance metric. Mismatched genes have a fixed distance of 10; for matched genes the distance is their weigth difference.
            IDistanceMetric distanceMetric = new ManhattanDistanceMetric(1.0, 0.0, 10.0);
            ISpeciationStrategy <NeatGenome> speciationStrategy = new ParallelKMeansClusteringStrategy <NeatGenome>(distanceMetric, _parallelOptions);

            // Create complexity regulation strategy.
            IComplexityRegulationStrategy complexityRegulationStrategy = ExperimentUtils.CreateComplexityRegulationStrategy(_complexityRegulationStr, _complexityThreshold);

            // Create the evolution algorithm.
            NeatEvolutionAlgorithm <NeatGenome> ea = new NeatEvolutionAlgorithm <NeatGenome>(_eaParams, speciationStrategy, complexityRegulationStrategy);

            // Create IBlackBox evaluator.
            DoublePoleBalancingEvaluator evaluator;

            switch (_variantStr)
            {
            case "DoublePole":
                evaluator = new DoublePoleBalancingEvaluator();
                break;

            case "DoublePoleNv":
                evaluator = new DoublePoleBalancingEvaluatorNv();
                break;

            case "DoublePoleNvAntiWiggle":
                evaluator = new DoublePoleBalancingEvaluatorNvAntiWiggle();
                break;

            default:
                throw new SharpNeatException(string.Format("DoublePoleBalancing experiment config XML specifies unknown variant [{0}]", _variantStr));
            }

            // Create genome decoder.
            IGenomeDecoder <NeatGenome, IBlackBox> genomeDecoder = CreateGenomeDecoder();

            // Create a genome list evaluator. This packages up the genome decoder with the genome evaluator.
            IGenomeListEvaluator <NeatGenome> innerEvaluator = new ParallelGenomeListEvaluator <NeatGenome, IBlackBox>(genomeDecoder, evaluator, _parallelOptions);

            // Wrap the list evaluator in a 'selective' evaulator that will only evaluate new genomes. That is, we skip re-evaluating any genomes
            // that were in the population in previous generations (elite genomes). This is determiend by examining each genome's evaluation info object.
            IGenomeListEvaluator <NeatGenome> selectiveEvaluator = new SelectiveGenomeListEvaluator <NeatGenome>(
                innerEvaluator,
                SelectiveGenomeListEvaluator <NeatGenome> .CreatePredicate_OnceOnly());

            // Initialize the evolution algorithm.
            ea.Initialize(selectiveEvaluator, genomeFactory, genomeList);

            // Finished. Return the evolution algorithm
            return(ea);
        }
Esempio n. 8
0
        /*
         *      List<NeatGenome> CreateNewGenome(IGenomeFactory<NeatGenome> genomeFactory)
         *      {
         *              Console.WriteLine("Saved genome not found, creating new files.");
         *              return genomeFactory.CreateGenomeList(_populationSize, 0);
         *      }
         */

        /// <summary>
        /// Creates and returns a NeatEvolutionAlgorithm object ready for running
        /// the NEAT algorithm/search. Various sub-parts of the algorithm are also
        /// constructed and connected up. This overload accepts a pre-built genome2
        /// population and their associated/parent genome2 factory.
        /// </summary>
        public NeatEvolutionAlgorithm <NeatGenome> CreateEvolutionAlgorithm(
            IGenomeFactory <NeatGenome> genomeFactory, List <NeatGenome> genomeList)
        {
            // Creates distance metric. Mismatched genes have a fixed distance of 10;
            // for matched genes the distance is their weigth difference.
            IDistanceMetric distanceMetric = new ManhattanDistanceMetric(1.0, 0.0, 10.0);

            //ISpeciationStrategy<NeatGenome> speciationStrategy = new KMeansClusteringStrategy<NeatGenome>(distanceMetric);
            ISpeciationStrategy <NeatGenome> speciationStrategy = new ParallelKMeansClusteringStrategy <NeatGenome>(distanceMetric, _parallelOptions);

            IComplexityRegulationStrategy complexityRegulationStrategy =
                ExperimentUtils.CreateComplexityRegulationStrategy(_complexityRegulationStr, _complexityThreshold);
            // Creates the evolution algorithm.
            NeatEvolutionAlgorithm <NeatGenome> evolAlgorithm = new NeatEvolutionAlgorithm <NeatGenome>(
                _eaParams, speciationStrategy, complexityRegulationStrategy, userName);
            IGenomeDecoder <NeatGenome, IBlackBox> genomeDecoder = new NeatGenomeDecoder(_activationScheme);
            // Creates a genome2 list evaluator. This packages up the genome2 decoder with the genome2 evaluator.
            IGenomeListEvaluator <NeatGenome> genomeListEvaluator =
                new ParallelGenomeListEvaluator <NeatGenome, IBlackBox>(genomeDecoder, PhenomeEvaluator, _parallelOptions);

            //To use single-thread evaluator:
            //IGenomeListEvaluator<NeatGenome> genomeListEvaluator =
            //        new SerialGenomeListEvaluator<NeatGenome, IBlackBox>(genomeDecoder, PhenomeEvaluator, false);
            // Wraps the list evaluator in a 'selective' evaulator that will only
            // evaluate new genomes. That is, we skip re-evaluating any genomes
            // that were in the population in previous generations (elite genomes).
            // This is determiend by examining each genome's evaluation info object.

            /*
             * int reevaluationPeriod = 1;
             * genomeListEvaluator = new SelectiveGenomeListEvaluator<NeatGenome>(
             *      genomeListEvaluator,
             *      SelectiveGenomeListEvaluator<NeatGenome>.CreatePredicate_PeriodicReevaluation(reevaluationPeriod));
             */
            genomeListEvaluator = new SelectiveGenomeListEvaluator <NeatGenome>(
                genomeListEvaluator,
                SelectiveGenomeListEvaluator <NeatGenome> .CreatePredicate_OnceOnly());
            // Initializes the evolution algorithm.
            evolAlgorithm.Initialize(genomeListEvaluator, genomeFactory, genomeList);
            // Finished. Return the evolution algorithm
            return(evolAlgorithm);
        }
Esempio n. 9
0
        public NeatEvolutionAlgorithm <NeatGenome> CreateEvolutionAlgorithm(
            IGenomeFactory <NeatGenome> genomeFactory, List <NeatGenome> genomeList)
        {
            // Create distance metric. Mismatched genes have a fixed distance of 10;
            // for matched genes the distance is their weight difference.
            IDistanceMetric distanceMetric = new ManhattanDistanceMetric(1.0, 0.0, 10.0);
            ISpeciationStrategy <NeatGenome> speciationStrategy =
                new ParallelKMeansClusteringStrategy <NeatGenome>(distanceMetric);

            // Create complexity regulation strategy.
            IComplexityRegulationStrategy complexityRegulationStrategy = new NullComplexityRegulationStrategy();

            // Create the evolution algorithm.
            NeatEvolutionAlgorithm <NeatGenome> ea =
                new NeatEvolutionAlgorithm <NeatGenome>(this.eaParams, speciationStrategy, complexityRegulationStrategy);

            // Create genome decoder.
            IGenomeDecoder <NeatGenome, IBlackBox> genomeDecoder = this.CreateGenomeDecoder();

            var evaluator = new Evaluator();

            // Create a genome list evaluator. This packages up the genome decoder with the genome evaluator.
            //IGenomeListEvaluator<NeatGenome> innerEvaluator =
            //    new SerialGenomeListEvaluator<NeatGenome, IBlackBox>(genomeDecoder, evaluator);
            IGenomeListEvaluator <NeatGenome> innerEvaluator =
                new ParallelGenomeListEvaluator <NeatGenome, IBlackBox>(genomeDecoder, evaluator);

            // Wrap the list evaluator in a 'selective' evaluator that will only evaluate new genomes.
            // That is, we skip re-evaluating any genomes that were in the population in previous
            // generations (elite genomes). This is determined by examining each genome's evaluation info object.
            IGenomeListEvaluator <NeatGenome> selectiveEvaluator =
                new SelectiveGenomeListEvaluator <NeatGenome>(
                    innerEvaluator,
                    SelectiveGenomeListEvaluator <NeatGenome> .CreatePredicate_OnceOnly());

            // Initialize the evolution algorithm.
            ea.Initialize(selectiveEvaluator, genomeFactory, genomeList);
            ea.UpdateScheme = new UpdateScheme(1);

            // Finished. Return the evolution algorithm
            return(ea);
        }
Esempio n. 10
0
        private static void Train()
        {
            File.WriteAllText($"{NeatConsts.experimentName}/fitness.csv", "generation,firness\n");

            var neatGenomeFactory = new NeatGenomeFactory(NeatConsts.ViewX * NeatConsts.ViewY * NeatConsts.typeIds.Count, 1);
            var genomeList        = neatGenomeFactory.CreateGenomeList(NeatConsts.SpecCount, 0);
            var eaParams          = new NeatEvolutionAlgorithmParameters
            {
                SpecieCount = NeatConsts.SpecCount
            };

            //var distanceMetric = new ManhattanDistanceMetric(1.0, 0.0, 10.0);
            var distanceMetric = new ManhattanDistanceMetric();

            var parallelOptions    = new ParallelOptions();
            var speciationStrategy = new ParallelKMeansClusteringStrategy <NeatGenome>(distanceMetric, parallelOptions);
            //var speciationStrategy = new KMeansClusteringStrategy<NeatGenome>(distanceMetric);
            //var speciationStrategy = new RandomClusteringStrategy<NeatGenome>();

            var complexityRegulationStrategy = new NullComplexityRegulationStrategy();
            //var complexityRegulationStrategy = new DefaultComplexityRegulationStrategy(ComplexityCeilingType.Relative, 0.50);

            var ea = new NeatEvolutionAlgorithm <NeatGenome>(eaParams, speciationStrategy, complexityRegulationStrategy);
            var activationScheme    = NetworkActivationScheme.CreateCyclicFixedTimestepsScheme(1);
            var genomeDecoder       = new NeatGenomeDecoder(activationScheme);
            var phenomeEvaluator    = new GameEvaluator();
            var genomeListEvaluator = new ParallelGenomeListEvaluator <NeatGenome, IBlackBox>(genomeDecoder, phenomeEvaluator, parallelOptions);

            ea.Initialize(genomeListEvaluator, neatGenomeFactory, genomeList);
            ea.UpdateScheme = new UpdateScheme(NeatConsts.LogRate);
            ea.StartContinue();
            ea.UpdateEvent += Ea_UpdateEvent;
            while (ea.RunState != RunState.Paused)
            {
            }
            ea.Stop();
        }
        /// <summary>
        /// Create and return a NeatEvolutionAlgorithm object ready for running the NEAT algorithm/search. Various sub-parts
        /// of the algorithm are also constructed and connected up.
        /// This overload accepts a pre-built genome population and their associated/parent genome factory.
        /// </summary>
        public NeatEvolutionAlgorithm<NeatGenome> CreateEvolutionAlgorithm(IGenomeFactory<NeatGenome> genomeFactory, List<NeatGenome> genomeList)
        {
            // Create distance metric. Mismatched genes have a fixed distance of 10; for matched genes the distance is their weigth difference.
            IDistanceMetric distanceMetric = new ManhattanDistanceMetric(1.0, 0.0, 10.0);
            ISpeciationStrategy<NeatGenome> speciationStrategy = new ParallelKMeansClusteringStrategy<NeatGenome>(distanceMetric, _parallelOptions);

            // Create complexity regulation strategy.
            IComplexityRegulationStrategy complexityRegulationStrategy = ExperimentUtils.CreateComplexityRegulationStrategy(_complexityRegulationStr, _complexityThreshold);

            // Create the evolution algorithm.
            NeatEvolutionAlgorithm<NeatGenome> ea = new NeatEvolutionAlgorithm<NeatGenome>(_eaParams, speciationStrategy, complexityRegulationStrategy);

            // Create IBlackBox evaluator.
            InvertedDoublePendulumEvaluator evaluator = new InvertedDoublePendulumEvaluator();

            // Create genome decoder.
            IGenomeDecoder<NeatGenome, IBlackBox> genomeDecoder = CreateGenomeDecoder();

            // Create a genome list evaluator. This packages up the genome decoder with the genome evaluator.
            IGenomeListEvaluator<NeatGenome> innerEvaluator = new ParallelGenomeListEvaluator<NeatGenome, IBlackBox>(genomeDecoder, evaluator, _parallelOptions);
            //IGenomeListEvaluator<NeatGenome> innerEvaluator = new SerialGenomeListEvaluator<NeatGenome, IBlackBox>(genomeDecoder, evaluator);

            // Wrap the list evaluator in a 'selective' evaulator that will only evaluate new genomes. That is, we skip re-evaluating any genomes
            // that were in the population in previous generations (elite genomes). This is determiend by examining each genome's evaluation info object.
            IGenomeListEvaluator<NeatGenome> selectiveEvaluator = new SelectiveGenomeListEvaluator<NeatGenome>(
                                                                                    innerEvaluator,
                                                                                    SelectiveGenomeListEvaluator<NeatGenome>.CreatePredicate_OnceOnly());
            // Initialize the evolution algorithm.
            ea.Initialize(selectiveEvaluator, genomeFactory, genomeList);

            // Finished. Return the evolution algorithm
            return ea;
        }
        /// <summary>
        /// Create and return a NeatEvolutionAlgorithm object ready for running the NEAT algorithm/search. Various sub-parts
        /// of the algorithm are also constructed and connected up.
        /// This overload accepts a pre-built genome population and their associated/parent genome factory.
        /// </summary>
        public NeatEvolutionAlgorithm<NeatGenome> CreateEvolutionAlgorithm(IGenomeFactory<NeatGenome> genomeFactory, List<NeatGenome> genomeList)
        {
            // Create distance metric. Mismatched genes have a fixed distance of 10; for matched genes the distance is their weigth difference.
            IDistanceMetric distanceMetric = new ManhattanDistanceMetric(1.0, 0.0, 10.0);
            ISpeciationStrategy<NeatGenome> speciationStrategy = new ParallelKMeansClusteringStrategy<NeatGenome>(distanceMetric, _parallelOptions);

            // Create complexity regulation strategy.
            IComplexityRegulationStrategy complexityRegulationStrategy = ExperimentUtils.CreateComplexityRegulationStrategy(_complexityRegulationStr, _complexityThreshold);

            // Create the evolution algorithm.
            NeatEvolutionAlgorithm<NeatGenome> ea = new NeatEvolutionAlgorithm<NeatGenome>(_eaParams, speciationStrategy, complexityRegulationStrategy);

            // Create IBlackBox evaluator.
            DoublePoleBalancingEvaluator evaluator;
            switch(_variantStr)
            {
                case "DoublePole":
                    evaluator = new DoublePoleBalancingEvaluator();
                    break;
                case "DoublePoleNv":
                    evaluator = new DoublePoleBalancingEvaluatorNv();
                    break;
                case "DoublePoleNvAntiWiggle":
                    evaluator = new DoublePoleBalancingEvaluatorNvAntiWiggle();
                    break;
                default:
                    throw new SharpNeatException(string.Format("DoublePoleBalancing experiment config XML specifies unknown variant [{0}]", _variantStr));
            }

            // Create genome decoder.
            IGenomeDecoder<NeatGenome, IBlackBox> genomeDecoder = CreateGenomeDecoder();

            // Create a genome list evaluator. This packages up the genome decoder with the genome evaluator.
            IGenomeListEvaluator<NeatGenome> innerEvaluator = new ParallelGenomeListEvaluator<NeatGenome, IBlackBox>(genomeDecoder, evaluator, _parallelOptions);

            // Wrap the list evaluator in a 'selective' evaulator that will only evaluate new genomes. That is, we skip re-evaluating any genomes
            // that were in the population in previous generations (elite genomes). This is determiend by examining each genome's evaluation info object.
            IGenomeListEvaluator<NeatGenome> selectiveEvaluator = new SelectiveGenomeListEvaluator<NeatGenome>(
                                                                                    innerEvaluator,
                                                                                    SelectiveGenomeListEvaluator<NeatGenome>.CreatePredicate_OnceOnly());
            // Initialize the evolution algorithm.
            ea.Initialize(selectiveEvaluator, genomeFactory, genomeList);

            // Finished. Return the evolution algorithm
            return ea;
        }
        /// <summary>
        /// Create and return a NeatEvolutionAlgorithm object ready for running the NEAT algorithm/search. Various sub-parts
        /// of the algorithm are also constructed and connected up.
        /// This overload accepts a pre-built genome population and their associated/parent genome factory.
        /// </summary>
        public NeatEvolutionAlgorithm<NeatGenome> CreateEvolutionAlgorithm(IGenomeFactory<NeatGenome> genomeFactory, List<NeatGenome> genomeList)
        {
            // Create distance metric. Mismatched genes have a fixed distance of 10; for matched genes the distance is their weigth difference.
            IDistanceMetric distanceMetric = new ManhattanDistanceMetric(1.0, 0.0, 10.0);
            ISpeciationStrategy<NeatGenome> speciationStrategy = new ParallelKMeansClusteringStrategy<NeatGenome>(distanceMetric, _parallelOptions);

            // Create complexity regulation strategy.
            IComplexityRegulationStrategy complexityRegulationStrategy = ExperimentUtils.CreateComplexityRegulationStrategy(_complexityRegulationStr, _complexityThreshold);

            // Create the evolution algorithm.
            NeatEvolutionAlgorithm<NeatGenome> ea = new NeatEvolutionAlgorithm<NeatGenome>(_eaParams, speciationStrategy, complexityRegulationStrategy);

            // Create IBlackBox evaluator.
            PreyCaptureEvaluator evaluator = new PreyCaptureEvaluator(_trialsPerEvaluation, _gridSize, _preyInitMoves, _preySpeed, _sensorRange, _maxTimesteps);

            // Create genome decoder.
            IGenomeDecoder<NeatGenome, IBlackBox> genomeDecoder = CreateGenomeDecoder();

            // TODO: evaulation scheme that re-evaulates existing genomes and takes average over time.
            // Create a genome list evaluator. This packages up the genome decoder with the genome evaluator.
            IGenomeListEvaluator<NeatGenome> genomeListEvaluator = new ParallelGenomeListEvaluator<NeatGenome, IBlackBox>(genomeDecoder, evaluator, _parallelOptions);

            // Initialize the evolution algorithm.
            ea.Initialize(genomeListEvaluator, genomeFactory, genomeList);

            // Finished. Return the evolution algorithm
            return ea;
        }
Esempio n. 14
0
        public NeatEvolutionAlgorithm<NeatGenome> CreateEvolutionAlgorithm(IGenomeFactory<NeatGenome> genomeFactory, List<NeatGenome> genomeList)
        {
            var parallelOptions = new ParallelOptions();

            // Create distance metric. Mismatched genes have a fixed distance of 10; for matched genes the distance is their weigth difference.
            var distanceMetric = new ManhattanDistanceMetric(1.0, 0.0, 10.0);
            var speciationStrategy = new ParallelKMeansClusteringStrategy<NeatGenome>(distanceMetric, parallelOptions);

            // Create complexity regulation strategy.
            var complexityRegulationStrategy = ExperimentUtils.CreateComplexityRegulationStrategy(_complexityRegulationStrategy, _complexityThreshold);

            // Create the evolution algorithm.
            var evolutionAlgorithm = new NeatEvolutionAlgorithm<NeatGenome>(NeatEvolutionAlgorithmParameters, speciationStrategy, complexityRegulationStrategy);

            // Create genome decoder.
            var genomeDecoder = CreateGenomeDecoder();

            // Create a genome list evaluator. This packages up the genome decoder with the genome evaluator.
            var genomeListEvaluator = new ParallelGenomeListEvaluator<NeatGenome, IBlackBox>(genomeDecoder, _neuromonPhenomeEvaluator, parallelOptions);

            // Initialize the evolution algorithm.
            evolutionAlgorithm.Initialize(genomeListEvaluator, genomeFactory, genomeList);

            return evolutionAlgorithm;
        }
Esempio n. 15
0
        public static void Main(string[] args)
        {
            var random   = new Random();
            var circuits = circuitsFilePaths().ToArray();

            var perceptionStep        = TimeSpan.FromSeconds(0.1);
            var simulationStep        = TimeSpan.FromSeconds(0.05); // 20Hz
            var maximumSimulationTime = TimeSpan.FromSeconds(60);

            var tracks = circuits.Select(circuitPath => Track.Load($"{circuitPath}/circuit_definition.json"));
            var worlds = tracks.Select(track => new StandardWorld(track, simulationStep)).ToArray();

            var inputSamplesCount       = 3;
            var maximumScanningDistance = 200;

            ILidar createLidarFor(ITrack track)
            => new Lidar(track, inputSamplesCount, Angle.FromDegrees(135), maximumScanningDistance);

            var settings = new EvolutionSettings
            {
                PopulationSize      = 1000,
                SpeciesCount        = 30,
                ElitismProportion   = 0,
                ComplexityThreshold = 50
            };

            // prepare simulation
            var parameters = new NeatEvolutionAlgorithmParameters
            {
                ElitismProportion = settings.ElitismProportion,
                SpecieCount       = settings.SpeciesCount
            };

            var distanceMetric  = new ManhattanDistanceMetric(1.0, 0.0, 10.0);
            var parallelOptions = new ParallelOptions {
                MaxDegreeOfParallelism = 4
            };
            var speciationStrategy           = new ParallelKMeansClusteringStrategy <NeatGenome>(distanceMetric, parallelOptions);
            var complexityRegulationStrategy = new DefaultComplexityRegulationStrategy(ComplexityCeilingType.Absolute, settings.ComplexityThreshold);

            var evolutionaryAlgorithm = new NeatEvolutionAlgorithm <NeatGenome>(
                parameters,
                speciationStrategy,
                complexityRegulationStrategy);

            var phenomeEvaluator = new RaceSimulationEvaluator(
                random,
                simulationStep,
                perceptionStep,
                maximumSimulationTime,
                worlds,
                createLidarFor);

            var genomeDecoder       = new NeatGenomeDecoder(NetworkActivationScheme.CreateAcyclicScheme());
            var genomeListEvaluator = new ParallelGenomeListEvaluator <NeatGenome, IBlackBox>(
                genomeDecoder,
                phenomeEvaluator);

            evolutionaryAlgorithm.Initialize(
                genomeListEvaluator,
                genomeFactory: new NeatGenomeFactory(
                    inputNeuronCount: inputSamplesCount,
                    outputNeuronCount: 2,
                    DefaultActivationFunctionLibrary.CreateLibraryNeat(new BipolarSigmoid()),
                    new NeatGenomeParameters
            {
                FeedforwardOnly                     = true,
                AddNodeMutationProbability          = 0.03,
                DeleteConnectionMutationProbability = 0.05,
                ConnectionWeightMutationProbability = 0.08,
                FitnessHistoryLength                = 10,
            }),
                settings.PopulationSize);

            var lastVisualization = DateTimeOffset.Now;

            evolutionaryAlgorithm.UpdateEvent += onUpdate;
            evolutionaryAlgorithm.StartContinue();

            Console.WriteLine("Press enter to stop the evolution.");
            Console.ReadLine();
            Console.WriteLine("Finishing the evolution.");

            evolutionaryAlgorithm.Stop();
            Console.WriteLine("Evolution is stopped.");

            // simulate best individual
            Console.WriteLine("Simulating best individual...");
            evaluate(evolutionaryAlgorithm.CurrentChampGenome);
            Console.WriteLine("Done.");

            void onUpdate(object sender, EventArgs e)
            {
                Console.WriteLine($"Generation #{evolutionaryAlgorithm.CurrentGeneration}");
                Console.WriteLine($"- max fitness:  {evolutionaryAlgorithm.Statistics._maxFitness}");
                Console.WriteLine($"- mean fitness: {evolutionaryAlgorithm.Statistics._meanFitness}");
                Console.WriteLine();

                if (DateTimeOffset.Now - lastVisualization > TimeSpan.FromSeconds(35))
                {
                    lastVisualization = DateTimeOffset.Now;
                    Console.WriteLine("Simulating currently best individual...");
                    evaluate(evolutionaryAlgorithm.CurrentChampGenome);
                }
            }

            void evaluate(NeatGenome genome)
            {
                var worldId = random.Next(0, worlds.Length - 1);
                var world   = worlds[worldId];

                var bestIndividual = genomeDecoder.Decode(genome);
                var agent          = new NeuralNetworkAgent(createLidarFor(world.Track), bestIndividual);
                var simulation     = new Simulation.Simulation(agent, world);
                var summary        = simulation.Simulate(simulationStep, perceptionStep, maximumSimulationTime);

                File.Copy($"{circuits[worldId]}/visualization.svg", "C:/Users/simon/Projects/racer-experiment/simulator/src/visualization.svg", overwrite: true);
                IO.Simulation.StoreResult(world.Track, world.VehicleModel, summary, "", "C:/Users/simon/Projects/racer-experiment/simulator/src/report.json");
            }
        }
        /// <summary>
        ///     Create and return a NeatEvolutionAlgorithm object ready for running the NEAT algorithm/search based on the given
        ///     genome factory and genome list.  Various sub-parts of the algorithm are also constructed and connected up.
        /// </summary>
        /// <param name="genomeFactory">The genome factory from which to generate new genomes</param>
        /// <param name="genomeList">The current genome population</param>
        /// <returns>Constructed evolutionary algorithm</returns>
        public NeatEvolutionAlgorithm<NeatGenome> CreateEvolutionAlgorithm(IGenomeFactory<NeatGenome> genomeFactory,
            List<NeatGenome> genomeList)
        {
            // Create distance metric. Mismatched genes have a fixed distance of 10; for matched genes the distance is their weigth difference.
            IDistanceMetric distanceMetric = new ManhattanDistanceMetric(1.0, 0.0, 10.0);
            ISpeciationStrategy<NeatGenome> speciationStrategy =
                new ParallelKMeansClusteringStrategy<NeatGenome>(distanceMetric, _parallOptions);

            // Create complexity regulation strategy.
            var complexityRegulationStrategy =
                ExperimentUtils.CreateComplexityRegulationStrategy(_complexityRegulationStrategy, _complexitythreshold);

            // Create the evolution algorithm.
            var ea = new NeatEvolutionAlgorithm<NeatGenome>(NeatEvolutionAlgorithmParameters, speciationStrategy,
                complexityRegulationStrategy);

            // Create IBlackBox evaluator.
            var mazeNavigationEvaluator = new MazeNavigationEvaluator();

            // Create genome decoder.
            var genomeDecoder = CreateGenomeDecoder();

            // Create a genome list evaluator. This packages up the genome decoder with the genome evaluator.
            IGenomeListEvaluator<NeatGenome> listEvaluator =
                new ParallelGenomeListEvaluator<NeatGenome, IBlackBox>(genomeDecoder, mazeNavigationEvaluator,
                    _parallOptions);

            // Initialize the evolution algorithm.
            ea.Initialize(listEvaluator, genomeFactory, genomeList);

            // Finished. Return the evolution algorithm
            return ea;
        }