public NeatEvolutionAlgorithm <NeatGenome> CreateEvolutionAlgorithm(IGenomeFactory <NeatGenome> genomeFactory, List <NeatGenome> genomeList)
    {
        IDistanceMetric distanceMetric = new ManhattanDistanceMetric(1.0, 0.0, 10.0);
        ISpeciationStrategy <NeatGenome> speciationStrategy = new KMeansClusteringStrategy <NeatGenome>(distanceMetric);

        IComplexityRegulationStrategy complexityRegulationStrategy = ExperimentUtils.CreateComplexityRegulationStrategy(_complexityRegulationStr, _complexityThreshold);

        NeatEvolutionAlgorithm <NeatGenome> ea = new NeatEvolutionAlgorithm <NeatGenome>(_eaParams, speciationStrategy, complexityRegulationStrategy);

        // Create black box evaluator
        AgarEvaluator evaluator = new AgarEvaluator(_optimizer);

        IGenomeDecoder <NeatGenome, IBlackBox> genomeDecoder = CreateGenomeDecoder();


        IGenomeListEvaluator <NeatGenome> innerEvaluator = new UnitySingleGenomeEvaluator <NeatGenome, IBlackBox>(genomeDecoder, evaluator, _optimizer);

        IGenomeListEvaluator <NeatGenome> selectiveEvaluator = new SelectiveGenomeListEvaluator <NeatGenome>(innerEvaluator,
                                                                                                             SelectiveGenomeListEvaluator <NeatGenome> .CreatePredicate_OnceOnly());

        //ea.Initialize(selectiveEvaluator, genomeFactory, genomeList);
        ea.Initialize(selectiveEvaluator, genomeFactory, genomeList);

        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 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);
        }
        /// <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.
            _evaluator = CreateEvaluator();

            // 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 SerialGenomeListEvaluator <NeatGenome, IBlackBox>(genomeDecoder, _evaluator);
            /*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 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);
        }
Example #4
0
        public virtual void Initialize(string name, XmlElement xmlConfig)
        {
            _name                    = name;
            _populationSize          = XmlUtils.GetValueAsInt(xmlConfig, "PopulationSize");
            _specieCount             = XmlUtils.GetValueAsInt(xmlConfig, "SpecieCount");
            _activationScheme        = ExperimentUtils.CreateActivationScheme(xmlConfig, "Activation");
            _complexityRegulationStr = XmlUtils.TryGetValueAsString(xmlConfig,
                                                                    "DefaultComplexityRegulationStrategy");
            _complexityThreshold = XmlUtils.TryGetValueAsInt(xmlConfig, "ComplexityThreshold");
            _description         = XmlUtils.TryGetValueAsString(xmlConfig, "Description");
            _parallelOptions     = ExperimentUtils.ReadParallelOptions(xmlConfig);

            _eaParams                         = new NeatEvolutionAlgorithmParameters();
            _eaParams.SpecieCount             = _specieCount;
            _neatGenomeParams                 = new NeatGenomeParameters();
            _neatGenomeParams.FeedforwardOnly = _activationScheme.AcyclicNetwork;

            DefaultComplexityRegulationStrategy = ExperimentUtils.CreateComplexityRegulationStrategy(
                _complexityRegulationStr,
                _complexityThreshold);
            DefaultSpeciationStrategy     = new KMeansClusteringStrategy <NeatGenome>(new ManhattanDistanceMetric(1.0, 0.0, 10.0));
            DefaultNeatEvolutionAlgorithm = new NeatEvolutionAlgorithm <NeatGenome>(
                NeatEvolutionAlgorithmParameters,
                DefaultSpeciationStrategy,
                DefaultComplexityRegulationStrategy);
        }
    public NeatEvolutionAlgorithm <NeatGenome> CreateEvolutionAlgorithm(IGenomeFactory <NeatGenome> genomeFactory, List <NeatGenome> genomeList)
    {
        IDistanceMetric distanceMetric = new ManhattanDistanceMetric(1.0, 0.0, 10.0);
        ISpeciationStrategy <NeatGenome> speciationStrategy = new KMeansClusteringStrategy <NeatGenome>(distanceMetric);

        IComplexityRegulationStrategy complexityRegulationStrategy = ExperimentUtils.CreateComplexityRegulationStrategy(_complexityRegulationStr, _complexityThreshold);

        NeatEvolutionAlgorithm <NeatGenome> ea = new NeatEvolutionAlgorithm <NeatGenome>(_eaParams, speciationStrategy, complexityRegulationStrategy);

        // Create black box evaluator
        SimpleEvaluator evaluator = new SimpleEvaluator(_optimizer);

        IGenomeDecoder <NeatGenome, IBlackBox> genomeDecoder = CreateGenomeDecoder();


        IGenomeListEvaluator <NeatGenome> innerEvaluator = new UnityParallelListEvaluator <NeatGenome, IBlackBox>(genomeDecoder, evaluator, _optimizer);

        IGenomeListEvaluator <NeatGenome> selectiveEvaluator = new SelectiveGenomeListEvaluator <NeatGenome>(innerEvaluator,
                                                                                                             SelectiveGenomeListEvaluator <NeatGenome> .CreatePredicate_OnceOnly());

        //ea.Initialize(selectiveEvaluator, genomeFactory, genomeList);
        //ea.Initialize(innerEvaluator, genomeFactory, genomeList);//this can be used if the currentGeneration does not need to be updated at the start of the training
        ea.Initialize(innerEvaluator, genomeFactory, genomeList, true);

        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 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 genome decoder.
            IGenomeDecoder <NeatGenome, IBlackBox> genomeDecoder = CreateGenomeDecoder();

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

            // Wrap a hall of fame evaluator around the baseline evaluator.
            genomeListEvaluator = new ParallelHallOfFameListEvaluator <NeatGenome, IBlackBox>(50, 0.5, ea, genomeListEvaluator, genomeDecoder, PhenomeEvaluator);

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

            // Finished. Return the evolution algorithm
            return(ea);
        }
        public virtual  void Initialize(string name, XmlElement xmlConfig)
        {
            _name = name;
            _populationSize = XmlUtils.GetValueAsInt(xmlConfig, "PopulationSize");
            _specieCount = XmlUtils.GetValueAsInt(xmlConfig, "SpecieCount");
            _activationScheme = ExperimentUtils.CreateActivationScheme(xmlConfig, "Activation");
            _complexityRegulationStr = XmlUtils.TryGetValueAsString(xmlConfig,
                "DefaultComplexityRegulationStrategy");
            _complexityThreshold = XmlUtils.TryGetValueAsInt(xmlConfig, "ComplexityThreshold");
            _description = XmlUtils.TryGetValueAsString(xmlConfig, "Description");
            _parallelOptions = ExperimentUtils.ReadParallelOptions(xmlConfig);

            _eaParams = new NeatEvolutionAlgorithmParameters();
            _eaParams.SpecieCount = _specieCount;
            _neatGenomeParams = new NeatGenomeParameters();
            _neatGenomeParams.FeedforwardOnly = _activationScheme.AcyclicNetwork;

            DefaultComplexityRegulationStrategy = ExperimentUtils.CreateComplexityRegulationStrategy(
                _complexityRegulationStr,
                _complexityThreshold);
            DefaultSpeciationStrategy = new KMeansClusteringStrategy<NeatGenome>(new ManhattanDistanceMetric(1.0, 0.0, 10.0));
            DefaultNeatEvolutionAlgorithm = new NeatEvolutionAlgorithm<NeatGenome>(
                    NeatEvolutionAlgorithmParameters,
                    DefaultSpeciationStrategy,
                    DefaultComplexityRegulationStrategy);
        }
Example #8
0
 public BraidNeatEvolutionAlgorithm(NeatEvolutionAlgorithmParameters eaParams,
                                    ISpeciationStrategy <TGenome> speciationStrategy,
                                    IComplexityRegulationStrategy complexityRegulationStrategy) : base(eaParams,
                                                                                                       speciationStrategy,
                                                                                                       complexityRegulationStrategy)
 {
     myLogger = new Logger(new MyLogger());
     myLogger.Log(logTag, "Initialized: ");
 }
Example #9
0
        /// <summary>
        /// Create and return a NeatEvolutionAlgorithm list ready for running the NEAT algorithm/search. Various sub-parts
        /// of the algorithm are also constructed and connected up.
        /// </summary>
        public ModuleNeatEvolutionAlgorithm <NeatGenome>[] CreateEvolutionAlgorithms(int populationSize)
        {
            // Create the modules.
            List <Module> modules = new List <Module>();

            for (int i = 0; i < _moduleCount; i++)
            {
                // Create distance metric. Mismatched genes have a fixed distance of 10; for matched genes the distance is their weigth difference.
                IDistanceMetric distanceMetricPitch = new ManhattanDistanceMetric(1.0, 0.0, 10.0);
                ISpeciationStrategy <NeatGenome> speciationStrategyPitch = new ParallelKMeansClusteringStrategy <NeatGenome>(distanceMetricPitch, _parallelOptions);
                IDistanceMetric distanceMetricRhythm = new ManhattanDistanceMetric(1.0, 0.0, 10.0);
                ISpeciationStrategy <NeatGenome> speciationStrategyRhythm = new ParallelKMeansClusteringStrategy <NeatGenome>(distanceMetricRhythm, _parallelOptions);


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


                // Create and add a new module with strategies and the evolution parameters.
                Module module = new Module((i + 1), _eaParams,
                                           speciationStrategyPitch, speciationStrategyRhythm,
                                           complexityRegulationPitch, complexityRegulationRhythm);
                modules.Add(module);
            }

            // Hook-up the modules with each other in circular order.
            // TODO Right now, they are hooked-up in circular order. Check whether it should be done otherwise.
            //for (int i = 0; i < modules.Count; i++)
            //{
            //    modules[i].SetParasiteModule(i != modules.Count - 1 ? modules[i + 1] : modules[0], _parasiteCount,
            //        _championCount, CreateGenomeDecoder());
            //}
            foreach (var module in modules)
            {
                module.SetParasiteModules(modules.Except(new List <Module>()
                {
                    module
                }).ToList(), _parasiteCount, _championCount, CreateGenomeDecoder(), CreateGenomeDecoder());
            }

            // Initialize each module.
            foreach (var module in modules)
            {
                var rhythmFactory = CreateGenomeFactory();
                var pitchFactory  = CreateGenomeFactory();
                module.Initialize(rhythmFactory, pitchFactory, populationSize);
            }

            // Finished. Return the evolution algorithms
            return
                (modules.Select(m => m.RhythmEvolutionAlgorithm).Concat(modules.Select(m => m.PitchEvolutionAlgorithm)).ToArray());
        }
Example #10
0
        /// <summary>
        /// Construct a new instance.
        /// </summary>
        /// <param name="eaSettings">NEAT evolution algorithm settings.</param>
        /// <param name="evaluator">An evaluator of lists of genomes.</param>
        /// <param name="speciationStrategy">Speciation strategy.</param>
        /// <param name="population">An initial population of genomes.</param>
        /// <param name="complexityRegulationStrategy">Complexity regulation strategy.</param>
        /// <param name="reproductionAsexualSettings">Asexual reproduction settings.</param>
        /// <param name="reproductionSexualSettings">Sexual reproduction settings.</param>
        /// <param name="weightMutationScheme">Connection weight mutation scheme.</param>
        /// <param name="rng">Random source.</param>
        public NeatEvolutionAlgorithm(
            NeatEvolutionAlgorithmSettings eaSettings,
            IGenomeListEvaluator <NeatGenome <T> > evaluator,
            ISpeciationStrategy <NeatGenome <T>, T> speciationStrategy,
            NeatPopulation <T> population,
            IComplexityRegulationStrategy complexityRegulationStrategy,
            NeatReproductionAsexualSettings reproductionAsexualSettings,
            NeatReproductionSexualSettings reproductionSexualSettings,
            WeightMutationScheme <T> weightMutationScheme,
            IRandomSource rng)
        {
            _eaSettingsCurrent       = eaSettings ?? throw new ArgumentNullException(nameof(eaSettings));
            _eaSettingsComplexifying = eaSettings;
            _eaSettingsSimplifying   = eaSettings.CreateSimplifyingSettings();

            _evaluator          = evaluator ?? throw new ArgumentNullException(nameof(evaluator));
            _speciationStrategy = speciationStrategy ?? throw new ArgumentNullException(nameof(speciationStrategy));
            _pop = population ?? throw new ArgumentNullException(nameof(population));
            _complexityRegulationStrategy = complexityRegulationStrategy ?? throw new ArgumentNullException(nameof(complexityRegulationStrategy));

            if (reproductionAsexualSettings == null)
            {
                throw new ArgumentNullException(nameof(reproductionAsexualSettings));
            }
            if (reproductionSexualSettings == null)
            {
                throw new ArgumentNullException(nameof(reproductionSexualSettings));
            }

            _rng = rng;
            _genomeComparerDescending = new GenomeComparerDescending(evaluator.FitnessComparer);

            if (eaSettings.SpeciesCount > population.PopulationSize)
            {
                throw new ArgumentException("Species count is higher then the population size.");
            }

            _generationSeq = new Int32Sequence();

            _reproductionAsexual = new NeatReproductionAsexual <T>(
                _pop.MetaNeatGenome, _pop.GenomeBuilder,
                _pop.GenomeIdSeq, population.InnovationIdSeq, _generationSeq,
                _pop.AddedNodeBuffer, reproductionAsexualSettings, weightMutationScheme);

            _reproductionSexual = new NeatReproductionSexual <T>(
                _pop.MetaNeatGenome, _pop.GenomeBuilder,
                _pop.GenomeIdSeq, _generationSeq,
                reproductionSexualSettings);

            _offspringBuilder = new OffspringBuilder <T>(
                _reproductionAsexual,
                _reproductionSexual,
                eaSettings.InterspeciesMatingProportion,
                evaluator.FitnessComparer);
        }
        public void Read_NullStrategy()
        {
            JObject jobj = JObject.Parse(
                @"{
    'strategyName': 'null'
}");

            IComplexityRegulationStrategy strategy = ComplexityRegulationStrategyJsonReader.Read(jobj);

            Assert.AreEqual("NullComplexityRegulationStrategy", strategy.GetType().Name);
        }
        public void Read_NullStrategy()
        {
            JsonDocument jdoc = JsonDocument.Parse(
                @"{
    ""strategyName"": ""null""
}");

            IComplexityRegulationStrategy strategy = ComplexityRegulationStrategyJsonReader.Read(jdoc.RootElement);

            Assert.Equal("NullComplexityRegulationStrategy", strategy.GetType().Name);
        }
        /// <summary>
        /// Constructs with the default NeatEvolutionAlgorithmParameters and speciation strategy
        /// (KMeansClusteringStrategy with ManhattanDistanceMetric).
        /// </summary>
        public NeatEvolutionAlgorithm()
        {
            _eaParams = new NeatEvolutionAlgorithmParameters();
            _eaParamsComplexifying = _eaParams;
            _eaParamsSimplifying   = _eaParams.CreateSimplifyingParameters();
            _stats = new NeatAlgorithmStats(_eaParams);
            _speciationStrategy = new KMeansClusteringStrategy <TGenome>(new ManhattanDistanceMetric());

            _complexityRegulationMode     = ComplexityRegulationMode.Complexifying;
            _complexityRegulationStrategy = new NullComplexityRegulationStrategy();
        }
Example #14
0
 public Module(int id, NeatEvolutionAlgorithmParameters evolutionAlgorithmParameters,
               ISpeciationStrategy <NeatGenome> speciationStrategyPitch, ISpeciationStrategy <NeatGenome> speciationStrategyRhythm,
               IComplexityRegulationStrategy complexityRegulationStrategyPitch, IComplexityRegulationStrategy complexityRegulationStrategyRhythm)
 {
     Name = "M" + id;
     Id   = id;
     //RhythmEvolutionAlgorithm = new NeatEvolutionAlgorithm<NeatGenome>(evolutionAlgorithmParameters, speciationStrategyRhythm, complexityRegulationStrategyRhythm);
     //PitchEvolutionAlgorithm = new NeatEvolutionAlgorithm<NeatGenome>(evolutionAlgorithmParameters, speciationStrategyPitch, complexityRegulationStrategyPitch);
     RhythmEvolutionAlgorithm = new ModuleNeatEvolutionAlgorithm <NeatGenome>(Name + "_R", Id, evolutionAlgorithmParameters, speciationStrategyRhythm, complexityRegulationStrategyRhythm);
     PitchEvolutionAlgorithm  = new ModuleNeatEvolutionAlgorithm <NeatGenome>(Name + "_P", Id, evolutionAlgorithmParameters, speciationStrategyPitch, complexityRegulationStrategyPitch);
 }
Example #15
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>
        /// Constructs with the provided NeatEvolutionAlgorithmParameters and ISpeciationStrategy.
        /// </summary>
        public NeatEvolutionAlgorithm(NeatEvolutionAlgorithmParameters eaParams,
                                      ISpeciationStrategy <TGenome> speciationStrategy,
                                      IComplexityRegulationStrategy complexityRegulationStrategy)
        {
            _eaParams = eaParams;
            _eaParamsComplexifying = _eaParams;
            _eaParamsSimplifying   = _eaParams.CreateSimplifyingParameters();
            _stats = new NeatAlgorithmStats(_eaParams);
            _speciationStrategy = speciationStrategy;

            _complexityRegulationMode     = ComplexityRegulationMode.Complexifying;
            _complexityRegulationStrategy = complexityRegulationStrategy;
        }
        /// <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);
        }
Example #18
0
 public ModuleNeatEvolutionAlgorithm(String name, int id,
                                     NeatEvolutionAlgorithmParameters evolutionAlgorithmParameters,
                                     ISpeciationStrategy <TGenome> speciationStrategy,
                                     IComplexityRegulationStrategy complexityRegulationStrategy)
     : base(evolutionAlgorithmParameters, speciationStrategy, complexityRegulationStrategy)
 {
     Name            = name;
     Id              = id;
     NextParasite    = 0;
     ParasiteGenomes = new List <TGenome> [MusicEnvironment.MODULE_COUNT - 1];
     for (int i = 0; i < ParasiteGenomes.Length; i++)
     {
         ParasiteGenomes[i] = new List <TGenome>();
     }
 }
Example #19
0
    public NeatEvolutionAlgorithm <NeatGenome> CreateEvolutionAlgorithm(IGenomeFactory <NeatGenome> genomeFactory, List <NeatGenome> genomeList)
    {
        IDistanceMetric distanceMetric = new ManhattanDistanceMetric(1.0, 0.0, 10.0);
        ISpeciationStrategy <NeatGenome> speciationStrategy           = new KMeansClusteringStrategy <NeatGenome>(distanceMetric);
        IComplexityRegulationStrategy    complexityRegulationStrategy = ExperimentUtils.CreateComplexityRegulationStrategy(_complexityRegulationStr, _complexityThreshold);

        BraidNeatEvolutionAlgorithm <NeatGenome> ea = new BraidNeatEvolutionAlgorithm <NeatGenome>(_eaParams, speciationStrategy, complexityRegulationStrategy);

        // Create black box evaluator
        BraidEvaluator evaluator = new BraidEvaluator(m_optimizer);
        IGenomeDecoder <NeatGenome, IBlackBox> genomeDecoder  = CreateGenomeDecoder();
        IGenomeListEvaluator <NeatGenome>      innerEvaluator = new BraidListEvaluator <NeatGenome, IBlackBox>(genomeDecoder, evaluator, m_optimizer);

        ea.Initialize(innerEvaluator, genomeFactory, genomeList);
        return(ea);
    }
Example #20
0
        /// <summary>
        /// Constructs with the default NeatEvolutionAlgorithmParameters and speciation strategy
        /// (KMeansClusteringStrategy with ManhattanDistanceMetric).
        /// </summary>

        /*
         * public NeatEvolutionAlgorithm()
         * {
         *  _eaParams = new NeatEvolutionAlgorithmParameters();
         *  _eaParamsComplexifying = _eaParams;
         *  _eaParamsSimplifying = _eaParams.CreateSimplifyingParameters();
         *  _stats = new NeatAlgorithmStats(_eaParams);
         *  _speciationStrategy = new KMeansClusteringStrategy<TGenome>(new ManhattanDistanceMetric());
         *
         *  _complexityRegulationMode = ComplexityRegulationMode.Complexifying;
         *  _complexityRegulationStrategy = new NullComplexityRegulationStrategy();
         * }
         */

        /// <summary>
        /// Constructs with the provided NeatEvolutionAlgorithmParameters and ISpeciationStrategy.
        /// </summary>

/*
 *      public NeatEvolutionAlgorithm(NeatEvolutionAlgorithmParameters eaParams,
 *                                    ISpeciationStrategy<TGenome> speciationStrategy,
 *                                    IComplexityRegulationStrategy complexityRegulationStrategy)
 *      {
 *          _eaParams = eaParams;
 *          _eaParamsComplexifying = _eaParams;
 *          _eaParamsSimplifying = _eaParams.CreateSimplifyingParameters();
 *          _stats = new NeatAlgorithmStats(_eaParams);
 *          _speciationStrategy = speciationStrategy;
 *
 *          _complexityRegulationMode = ComplexityRegulationMode.Complexifying;
 *          _complexityRegulationStrategy = complexityRegulationStrategy;
 *      }
 */
        public void Construct(NeatEvolutionAlgorithmParameters eaParams,
                              ISpeciationStrategy <TGenome> speciationStrategy,
                              IComplexityRegulationStrategy complexityRegulationStrategy,
                              IGenomeListEvaluator <TGenome> initialGenomeListEvaluator)
        {
            _eaParams = eaParams;
            _eaParamsComplexifying = _eaParams;
            _eaParamsSimplifying   = _eaParams.CreateSimplifyingParameters();
            _stats = new NeatAlgorithmStats(_eaParams);
            _speciationStrategy = speciationStrategy;

            _complexityRegulationMode     = ComplexityRegulationMode.Complexifying;
            _complexityRegulationStrategy = complexityRegulationStrategy;

            _initialGenomeListEvaluator = initialGenomeListEvaluator;
        }
        /// <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>[] CreateEvolutionAlgorithms(IGenomeFactory <NeatGenome> genomeFactory1, List <NeatGenome> genomeList1,
                                                                               IGenomeFactory <NeatGenome> genomeFactory2, List <NeatGenome> genomeList2)
        {
            // Create distance metric. Mismatched genes have a fixed distance of 10; for matched genes the distance is their weigth difference.
            IDistanceMetric distanceMetric1 = new ManhattanDistanceMetric(1.0, 0.0, 10.0);
            ISpeciationStrategy <NeatGenome> speciationStrategy1 = new ParallelKMeansClusteringStrategy <NeatGenome>(distanceMetric1, _parallelOptions);

            // Create distance metric. Mismatched genes have a fixed distance of 10; for matched genes the distance is their weigth difference.
            IDistanceMetric distanceMetric2 = new ManhattanDistanceMetric(1.0, 0.0, 10.0);
            ISpeciationStrategy <NeatGenome> speciationStrategy2 = new ParallelKMeansClusteringStrategy <NeatGenome>(distanceMetric2, _parallelOptions);

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

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

            // Create the evolution algorithm.
            NeatEvolutionAlgorithm <NeatGenome> ea1 = new NeatEvolutionAlgorithm <NeatGenome>(_eaParams, speciationStrategy1, complexityRegulationStrategy1);
            NeatEvolutionAlgorithm <NeatGenome> ea2 = new NeatEvolutionAlgorithm <NeatGenome>(_eaParams, speciationStrategy2, complexityRegulationStrategy2);

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

            // Create phenome evaluators. Note we are evolving one population of X players and one of O players.
            ICoevolutionPhenomeEvaluator <IBlackBox> phenomeEvaluator1 = new TicTacToeHostParasiteEvaluator(SquareTypes.X);
            ICoevolutionPhenomeEvaluator <IBlackBox> phenomeEvaluator2 = new TicTacToeHostParasiteEvaluator(SquareTypes.O);

            // Create a genome list evaluator. This packages up the genome decoder with the phenome evaluator.
            HostParasiteCoevolutionListEvaluator <NeatGenome, IBlackBox> genomeListEvaluator1 = new HostParasiteCoevolutionListEvaluator <NeatGenome, IBlackBox>(_parasiteCount, _championCount, ea2, genomeDecoder1, phenomeEvaluator1);
            HostParasiteCoevolutionListEvaluator <NeatGenome, IBlackBox> genomeListEvaluator2 = new HostParasiteCoevolutionListEvaluator <NeatGenome, IBlackBox>(_parasiteCount, _championCount, ea1, genomeDecoder2, phenomeEvaluator2);

            // Initialize the evolution algorithms.
            ea1.Initialize(genomeListEvaluator1, genomeFactory1, genomeList1);
            ea2.Initialize(genomeListEvaluator2, genomeFactory2, genomeList2);

            // Set the evolution algorithms to update every generation.
            ea1.UpdateScheme = new UpdateScheme(1);
            ea2.UpdateScheme = new UpdateScheme(1);

            // Finished. Return the evolution algorithms
            return(new [] { ea1, ea2 });
        }
        /// <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, new 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 genome decoder.
            IGenomeDecoder <NeatGenome, IBlackBox> genomeDecoder = CreateGenomeDecoder();


            // Finished. Return the evolution algorithm
            return(ea);
        }
Example #23
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);
        }
    /// <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)
    {
        Debug.Log("........CreateEvolutionAlgorithm: Setting parameters");
        // 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 KMeansClusteringStrategy <NeatGenome>(distanceMetric);

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

        Debug.Log("........CreateEvolutionAlgorithm: Creating ea");
        // Create the evolution algorithm.
        NeatEvolutionAlgorithm <NeatGenome> ea = new NeatEvolutionAlgorithm <NeatGenome>(_eaParams, speciationStrategy, complexityRegulationStrategy);

        Debug.Log("........CreateEvolutionAlgorithm: Creating evaluator");
        // Create IBlackBox evaluator.
        CPPNRepairEvaluator2 evaluator = new CPPNRepairEvaluator2(this);

        evaluator.SetOriginalFeatures(_originalFeatures);

        Debug.Log("........CreateEvolutionAlgorithm: Creating genome decoder and serial evaluator");
        // 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 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.
        Debug.Log("........CreateEvolutionAlgorithm: Initializing ea");
        ea.Initialize(innerEvaluator, genomeFactory, genomeList);

        Debug.Log("........CreateEvolutionAlgorithm: Returning");
        // Finished. Return the evolution algorithm
        return(ea);
    }
        public void Read_AbsoluteStrategy()
        {
            JsonDocument jdoc = JsonDocument.Parse(
                @"{
    ""strategyName"": ""absolute"",
    ""complexityCeiling"": 11,
    ""minSimplifcationGenerations"": 12
}");

            IComplexityRegulationStrategy strategy = ComplexityRegulationStrategyJsonReader.Read(jdoc.RootElement);

            Assert.Equal("AbsoluteComplexityRegulationStrategy", strategy.GetType().Name);

            // Read private variables with reflection.
            var    absoluteStrategy            = (AbsoluteComplexityRegulationStrategy)strategy;
            object complexityCeiling           = GetInstanceField(absoluteStrategy, "_complexityCeiling");
            object minSimplifcationGenerations = GetInstanceField(absoluteStrategy, "_minSimplifcationGenerations");

            Assert.Equal(11.0, complexityCeiling);
            Assert.Equal(12, minSimplifcationGenerations);
        }
Example #26
0
 /// <summary>
 /// Construct a new instance.
 /// </summary>
 /// <param name="eaSettings">NEAT evolution algorithm settings.</param>
 /// <param name="evaluator">An evaluator of lists of genomes.</param>
 /// <param name="speciationStrategy">Speciation strategy.</param>
 /// <param name="population">An initial population of genomes.</param>
 /// <param name="complexityRegulationStrategy" > Complexity regulation strategy.</param>
 /// <param name="reproductionAsexualSettings">Asexual reproduction settings.</param>
 /// <param name="reproductionSexualSettings">Sexual reproduction settings.</param>
 /// <param name="weightMutationScheme">Connection weight mutation scheme.</param>
 public NeatEvolutionAlgorithm(
     NeatEvolutionAlgorithmSettings eaSettings,
     IGenomeListEvaluator <NeatGenome <T> > evaluator,
     ISpeciationStrategy <NeatGenome <T>, T> speciationStrategy,
     NeatPopulation <T> population,
     IComplexityRegulationStrategy complexityRegulationStrategy,
     NeatReproductionAsexualSettings reproductionAsexualSettings,
     NeatReproductionSexualSettings reproductionSexualSettings,
     WeightMutationScheme <T> weightMutationScheme)
     : this(
         eaSettings,
         evaluator,
         speciationStrategy,
         population,
         complexityRegulationStrategy,
         reproductionAsexualSettings,
         reproductionSexualSettings,
         weightMutationScheme,
         RandomDefaults.CreateRandomSource())
 {
 }
        public void Read_AbsoluteStrategy()
        {
            JObject jobj = JObject.Parse(
                @"{
    'strategyName': 'absolute',
    'complexityCeiling': 11,
    'minSimplifcationGenerations': 12
}");

            IComplexityRegulationStrategy strategy = ComplexityRegulationStrategyJsonReader.Read(jobj);

            Assert.AreEqual("AbsoluteComplexityRegulationStrategy", strategy.GetType().Name);

            // Read private variables with reflection.
            var    absoluteStrategy            = (AbsoluteComplexityRegulationStrategy)strategy;
            object complexityCeiling           = GetInstanceField(absoluteStrategy, "_complexityCeiling");
            object minSimplifcationGenerations = GetInstanceField(absoluteStrategy, "_minSimplifcationGenerations");

            Assert.AreEqual(11.0, complexityCeiling);
            Assert.AreEqual(12, minSimplifcationGenerations);
        }
Example #28
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 weigth difference.
            IDistanceMetric distanceMetric = new ManhattanDistanceMetric(1.0, 0.0, 10.0);
            ISpeciationStrategy <NeatGenome> speciationStrategy =
                new KMeansClusteringStrategy <NeatGenome>(distanceMetric);

            // Create complexity regulation strategy.
            IComplexityRegulationStrategy complexityRegulationStrategy =
                ExperimentUtils.CreateComplexityRegulationStrategy("absolute", 10);

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

            // Create IBlackBox evaluator.
            // var evaluator = new RemoteXorEvaluator();
            var evaluator = new RemoteBatchXorEvaluator();
            //LocalXorEvaluator evaluator = new LocalXorEvaluator();

            // Create genome decoder.
            IGenomeDecoder <NeatGenome, FastCyclicNetwork> genomeDecoder = _decoder;

            // Create a genome list evaluator. This packages up the genome decoder with the genome evaluator.
            IGenomeListEvaluator <NeatGenome> innerEvaluator = new BatchGenomeListEvaluator <NeatGenome, FastCyclicNetwork>(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 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);
        }
        /// <summary>
        ///     Configures and instantiates the initialization evolutionary algorithm.
        /// </summary>
        /// <param name="parallelOptions">Synchronous/Asynchronous execution settings.</param>
        /// <param name="genomeList">The initial population of genomes.</param>
        /// <param name="genomeDecoder">The decoder to translate genomes into phenotypes.</param>
        /// <param name="startingEvaluations">
        ///     The number of evaluations that preceeded this from which this process will pick up
        ///     (this is used in the case where we're restarting a run because it failed to find a solution in the allotted time).
        /// </param>
        public virtual void InitializeAlgorithm(ParallelOptions parallelOptions, List<NeatGenome> genomeList,
            IGenomeDecoder<NeatGenome, IBlackBox> genomeDecoder, ulong startingEvaluations)
        {
            ParallelOptions = parallelOptions;
            InitialPopulation = genomeList;
            StartingEvaluations = startingEvaluations;
            GenomeDecoder = genomeDecoder;

            // 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);
            SpeciationStrategy = new ParallelKMeansClusteringStrategy<NeatGenome>(distanceMetric, parallelOptions);

            // Create complexity regulation strategy.
            ComplexityRegulationStrategy =
                ExperimentUtils.CreateComplexityRegulationStrategy(ComplexityRegulationStrategyDefinition,
                    ComplexityThreshold);
        }
        /// <summary>
        /// Initialize the experiment with some optional XML configutation data.
        /// </summary>
        public virtual void Initialize(string name, XmlElement xmlConfig)
        {
            _name             = name;
            _description      = XmlUtils.TryGetValueAsString(xmlConfig, "Description") ?? "";
            _comment          = XmlUtils.TryGetValueAsString(xmlConfig, "Comment") ?? "";
            _populationSize   = XmlUtils.TryGetValueAsInt(xmlConfig, "PopulationSize") ?? 100;
            _maxGenerations   = XmlUtils.TryGetValueAsInt(xmlConfig, "MaxGenerations") ?? 1000;
            _activationScheme = ExperimentUtils.CreateActivationScheme(xmlConfig, "Activation");
            SeedGenome        = XmlUtils.TryGetValueAsString(xmlConfig, "SeedGenome");
            try
            {
                _complexityRegulationStrategy = ExperimentUtils.CreateComplexityRegulationStrategy(xmlConfig, "ComplexityRegulation");
            }
            catch (ArgumentException e)
            {
                _logger.Warn(e.Message);
            }

            _parallelOptions = ExperimentUtils.ReadParallelOptions(xmlConfig);
            _multiThreading  = XmlUtils.TryGetValueAsBool(xmlConfig, "MultiThreading") ?? true;
            _logInterval     = XmlUtils.TryGetValueAsInt(xmlConfig, "LogInterval") ?? 10;

            // Evolutionary algorithm parameters
            _eaParams = new NeatEvolutionAlgorithmParameters();

            XmlElement xmlEAParams = xmlConfig.SelectSingleNode("EAParams") as XmlElement;

            if (xmlEAParams != null)
            {
                _eaParams.SpecieCount                  = XmlUtils.GetValueAsInt(xmlEAParams, "SpecieCount");
                _eaParams.ElitismProportion            = XmlUtils.GetValueAsDouble(xmlEAParams, "ElitismProportion");
                _eaParams.SelectionProportion          = XmlUtils.GetValueAsDouble(xmlEAParams, "SelectionProportion");
                _eaParams.OffspringAsexualProportion   = XmlUtils.GetValueAsDouble(xmlEAParams, "OffspringAsexualProportion");
                _eaParams.OffspringSexualProportion    = XmlUtils.GetValueAsDouble(xmlEAParams, "OffspringSexualProportion");
                _eaParams.InterspeciesMatingProportion = XmlUtils.GetValueAsDouble(xmlEAParams, "InterspeciesMatingProportion");
            }
            else
            {
                _logger.Info("EA parameters not found. Using default.");
            }

            // NEAT Genome parameters
            _neatGenomeParams = new NeatGenomeParameters();

            // Prevent recurrent connections if the activation scheme is acyclic
            _neatGenomeParams.FeedforwardOnly = _activationScheme.AcyclicNetwork;

            XmlElement xmlGenomeParams = xmlConfig.SelectSingleNode("GenomeParams") as XmlElement;

            if (xmlGenomeParams != null)
            {
                _neatGenomeParams.ConnectionWeightRange                    = XmlUtils.GetValueAsDouble(xmlGenomeParams, "ConnectionWeightRange");
                _neatGenomeParams.InitialInterconnectionsProportion        = XmlUtils.GetValueAsDouble(xmlGenomeParams, "InitialInterconnectionsProportion");
                _neatGenomeParams.DisjointExcessGenesRecombinedProbability = XmlUtils.GetValueAsDouble(xmlGenomeParams, "DisjointExcessGenesRecombinedProbability");
                _neatGenomeParams.ConnectionWeightMutationProbability      = XmlUtils.GetValueAsDouble(xmlGenomeParams, "ConnectionWeightMutationProbability");
                _neatGenomeParams.AddNodeMutationProbability               = XmlUtils.GetValueAsDouble(xmlGenomeParams, "AddNodeMutationProbability");
                _neatGenomeParams.AddConnectionMutationProbability         = XmlUtils.GetValueAsDouble(xmlGenomeParams, "AddConnectionMutationProbability");
                _neatGenomeParams.DeleteConnectionMutationProbability      = XmlUtils.GetValueAsDouble(xmlGenomeParams, "DeleteConnectionMutationProbability");
            }
            else
            {
                _logger.Info("Genome parameters not found. Using default.");
            }

            XmlElement xmlNoveltySearchParams = xmlConfig.SelectSingleNode("NoveltySearch") as XmlElement;

            if (xmlNoveltySearchParams != null)
            {
                _noveltySearchParams = NoveltySearchParameters.ReadXmlProperties(xmlNoveltySearchParams);
            }
            else
            {
                _logger.Info("Novelty search parameters not found");
            }

            XmlElement xmlMultiObjectiveParams = xmlConfig.SelectSingleNode("MultiObjective") as XmlElement;

            if (xmlMultiObjectiveParams != null)
            {
                _multiObjectiveParams = MultiObjectiveParameters.ReadXmlProperties(xmlMultiObjectiveParams);
            }
            else
            {
                _logger.Info("Multi objective parameters not found");
            }

            // Create IBlackBox evaluator.
            _evaluator = new TEvaluator();
            _evaluator.Initialize(xmlConfig);
            _evaluator.NoveltySearchParameters = _noveltySearchParams;
        }
Example #31
0
 private void GetComplexityRegulationStrategy()
 {
     _ComplexityRegulationStrategy = ExperimentUtils.CreateComplexityRegulationStrategy(_ComplexityRegulationStr, _ComplexityThreshold);
 }