Beispiel #1
0
        private NeatEvolutionAlgorithm <NeatGenome> CreateEvolutionAlgorithm(IGenomeListEvaluator <NeatGenome> evaluator, int populationSize)
        {
            var genomeFactory = new NeatGenomeFactory(_inputCount.Value, _outputCount.Value, _neatGenomeParams);
            var genomeList    = genomeFactory.CreateGenomeList(populationSize, 0);

            return(CreateEvolutionAlgorithm(evaluator, genomeList));
        }
Beispiel #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 = GetGenomeListEvaluator(genomeDecoder);

            // 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);
        }
 /// <summary>
 /// Initializes the evolution algorithm with the provided IGenomeListEvaluator, IGenomeFactory
 /// and an initial population of genomes.
 /// </summary>
 /// <param name="genomeListEvaluator">The genome evaluation scheme for the evolution algorithm.</param>
 /// <param name="genomeFactory">The factory that was used to create the genomeList and which is therefore referenced by the genomes.</param>
 /// <param name="genomeList">An initial genome population.</param>
 public override void Initialize(IGenomeListEvaluator <TGenome> genomeListEvaluator,
                                 IGenomeFactory <TGenome> genomeFactory,
                                 List <TGenome> genomeList)
 {
     base.Initialize(genomeListEvaluator, genomeFactory, genomeList);
     Initialize();
 }
Beispiel #4
0
        public IEnumerator Start()
        {
            Debug.Assert(_populationSize > 5);
            Debug.Log($"Main thread is {Thread.CurrentThread.ManagedThreadId}");
            Application.runInBackground = true;

            _activationScheme = NetworkActivationScheme.CreateCyclicFixedTimestepsScheme(2, true);

            _eaParams = new NeatEvolutionAlgorithmParameters();
            _eaParams.ElitismProportion = _elitismProportion;
            _eaParams.SpecieCount       = _specieCount;

            _neatGenomeParams = new NeatGenomeParameters();
            _neatGenomeParams.AddConnectionMutationProbability    = _AddConnectionMutationProbability;
            _neatGenomeParams.DeleteConnectionMutationProbability = _DeleteConnectionMutationProbability;
            _neatGenomeParams.AddNodeMutationProbability          = _AddNodeMutationProbability;
            _neatGenomeParams.ConnectionWeightMutationProbability = _ConnectionWeightMutationProbability;
            _neatGenomeParams.InitialInterconnectionsProportion   = _InitialInterconnectionsProportion;
            _neatGenomeParams.FeedforwardOnly = _activationScheme.AcyclicNetwork;

            Debug.Log("Creating evaluator");
            yield return(new WaitForSeconds(0.1f));

            _evaluator = CreateEvaluator();

            Debug.Log("Creating algorithm");
            yield return(new WaitForSeconds(0.1f));

            _ea              = CreateEvolutionAlgorithm(_evaluator, _populationSize);
            _ea.UpdateEvent += _ea_UpdateEvent;
        }
 /// <summary>
 /// Initializes the evolution algorithm with the provided IGenomeListEvaluator
 /// and an IGenomeFactory that can be used to create an initial population of genomes.
 /// </summary>
 /// <param name="genomeListEvaluator">The genome evaluation scheme for the evolution algorithm.</param>
 /// <param name="genomeFactory">The factory that was used to create the genomeList and which is therefore referenced by the genomes.</param>
 /// <param name="populationSize">The number of genomes to create for the initial population.</param>
 public override void Initialize(IGenomeListEvaluator <TGenome> genomeListEvaluator,
                                 IGenomeFactory <TGenome> genomeFactory,
                                 int populationSize)
 {
     base.Initialize(genomeListEvaluator, genomeFactory, populationSize);
     Initialize();
 }
 /// <summary>
 /// Initializes the evolution algorithm with the provided IGenomeListEvaluator, IGenomeFactory
 /// an initial population of genomes and a boolean to update the currentGeneration value.
 /// </summary>
 /// <param name="genomeListEvaluator">The genome evaluation scheme for the evolution algorithm.</param>
 /// <param name="genomeFactory">The factory that was used to create the genomeList and which is therefore referenced by the genomes.</param>
 /// <param name="genomeList">An initial genome population.</param>
 /// <param name="addGenerationValue">boolean to trigger currentGeneration value from last genome in list</param>
 public override void Initialize(IGenomeListEvaluator <TGenome> genomeListEvaluator,
                                 IGenomeFactory <TGenome> genomeFactory,
                                 List <TGenome> genomeList,
                                 bool addGenerationValue)
 {
     base.Initialize(genomeListEvaluator, genomeFactory, genomeList, addGenerationValue);
     Initialize();
 }
Beispiel #7
0
 /// <summary>
 /// Construct with the provided number of generations per champion, weight to the hall
 /// of fame fitness, and other parameters.
 /// The number of parallel threads defaults to Environment.ProcessorCount.
 /// </summary>
 public ParallelHallOfFameListEvaluator(uint generationsPerChampion,
                                        double hallOfFameWeight,
                                        AbstractGenerationalAlgorithm <TGenome> ea,
                                        IGenomeListEvaluator <TGenome> innerEvaluator,
                                        IGenomeDecoder <TGenome, TPhenome> genomeDecoder,
                                        ICoevolutionPhenomeEvaluator <TPhenome> phenomeEvaluator)
     : this(generationsPerChampion, hallOfFameWeight, ea, innerEvaluator, genomeDecoder, phenomeEvaluator, new ParallelOptions())
 {
 }
Beispiel #8
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 DefaultEvolutionAlgorithm(
     EAParameters eaParams,
     IGenomeListEvaluator <TGenome> evaluator,
     ISelectionReproductionStrategy <TGenome> selectionReproStrategy,
     Population <TGenome> population)
 {
     _eaParams  = eaParams;
     _evaluator = evaluator;
     _selectionReproStrategy = selectionReproStrategy;
     _pop = population;
 }
Beispiel #10
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);
        }
Beispiel #11
0
 /// <summary>
 /// Initializes the evolution algorithm with the provided IGenomeListEvaluator, IGenomeFactory
 /// and an initial population of genomes.
 /// </summary>
 /// <param name="genomeListEvaluator">The genome evaluation scheme for the evolution algorithm.</param>
 /// <param name="genomeFactory">The factory that was used to create the genomeList and which is therefore referenced by the genomes.</param>
 /// <param name="genomeList">An initial genome population.</param>
 public virtual void Initialize(IGenomeListEvaluator <TGenome> genomeListEvaluator,
                                IGenomeFactory <TGenome> genomeFactory,
                                List <TGenome> genomeList)
 {
     _currentGeneration   = 0;
     _genomeListEvaluator = genomeListEvaluator;
     _genomeFactory       = genomeFactory;
     _genomeList          = genomeList;
     _populationSize      = _genomeList.Count;
     _runState            = RunState.Ready;
     _updateScheme        = new UpdateScheme(new TimeSpan(0, 0, 1));
 }
Beispiel #12
0
 /// <summary>
 /// Initializes the evolution algorithm with the provided IGenomeListEvaluator
 /// and an IGenomeFactory that can be used to create an initial population of genomes.
 /// </summary>
 /// <param name="genomeListEvaluator">The genome evaluation scheme for the evolution algorithm.</param>
 /// <param name="genomeFactory">The factory that was used to create the genomeList and which is therefore referenced by the genomes.</param>
 /// <param name="populationSize">The number of genomes to create for the initial population.</param>
 public virtual void Initialize(IGenomeListEvaluator <TGenome> genomeListEvaluator,
                                IGenomeFactory <TGenome> genomeFactory,
                                int populationSize)
 {
     _currentGeneration   = 0;
     _genomeListEvaluator = genomeListEvaluator;
     _genomeFactory       = genomeFactory;
     _genomeList          = genomeFactory.CreateGenomeList(populationSize, _currentGeneration);
     _populationSize      = populationSize;
     _runState            = RunState.Ready;
     _updateScheme        = new UpdateScheme(new TimeSpan(0, 0, 1));
 }
Beispiel #13
0
        private void Initialize_private(IGenomeListEvaluator <TGenome> genomeListEvaluator, IGenomeFactory <TGenome> genomeFactory, List <TGenome> genomeList)
        {
            _prevUpdateGeneration = 0;
            _prevUpdateTimeTick   = DateTime.UtcNow.Ticks;

            _currentGeneration   = 0;
            _genomeListEvaluator = genomeListEvaluator;
            _genomeFactory       = genomeFactory;
            _genomeList          = genomeList;
            _populationSize      = _genomeList.Count;
            _runState            = RunState.Ready;
            _updateScheme        = new UpdateScheme(new TimeSpan(0, 0, 1));
        }
 /// <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="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,
     NeatReproductionAsexualSettings reproductionAsexualSettings,
     NeatReproductionSexualSettings reproductionSexualSettings,
     WeightMutationScheme <T> weightMutationScheme)
     : this(eaSettings, evaluator, speciationStrategy, population,
            reproductionAsexualSettings, reproductionSexualSettings,
            weightMutationScheme,
            RandomDefaults.CreateRandomSource())
 {
 }
Beispiel #15
0
 /// <summary>
 /// Initializes the evolution algorithm with the provided IGenomeListEvaluator
 /// and an IGenomeFactory that can be used to create an initial population of genomes.
 /// </summary>
 /// <param name="genomeListEvaluator">The genome evaluation scheme for
 /// the evolution algorithm.</param>
 /// <param name="genomeFactory">The factory that was used to create the
 /// genomeList and which is therefore referenced by the genomes.</param>
 /// <param name="populationSize">The number of genomes to create for the
 /// initial population.</param>
 public virtual void Initialize(IGenomeListEvaluator <TGenome> genomeListEvaluator,
                                IGenomeFactory <TGenome> genomeFactory,
                                int populationSize)
 {
     _currentGeneration   = 0;
     _genomeListEvaluator = genomeListEvaluator;
     _genomeFactory       = genomeFactory;
     _genomeList          = genomeFactory.CreateGenomeList(populationSize, _currentGeneration);
     // We set an arbitrary genome as champion to avoid possible null exceptions.
     _currentBestGenome = _genomeList[0];
     _populationSize    = populationSize;
     _runState          = RunState.Ready;
     _updateScheme      = new UpdateScheme(new TimeSpan(0, 0, 1));
 }
Beispiel #16
0
        private NeatEvolutionAlgorithm <NeatGenome> CreateEvolutionAlgorithm(IGenomeListEvaluator <NeatGenome> evaluator, List <NeatGenome> list)
        {
            IDistanceMetric distanceMetric = new ManhattanDistanceMetric(1.0, 0.0, 10.0);
            ISpeciationStrategy <NeatGenome> speciationStrategy           = new KMeansClusteringStrategy <NeatGenome>(distanceMetric);
            IComplexityRegulationStrategy    complexityRegulationStrategy = new DefaultComplexityRegulationStrategy(ComplexityCeilingType.Absolute, _complexityThreshold);

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

            var genomeFactory = new NeatGenomeFactory(_inputCount.Value, _outputCount.Value, _neatGenomeParams);

            neatEvolutionAlgorithm.Initialize(evaluator, genomeFactory, list);

            return(neatEvolutionAlgorithm);
        }
Beispiel #17
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;
        }
Beispiel #18
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 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.
            DeepBeliefNetworkBiasEvaluator evaluator = new DeepBeliefNetworkBiasEvaluator();

            // 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(_lengthCppnInput);

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

            if (Constants.IS_MULTI_THREADING)
            {
                innerEvaluator = new ParallelGenomeListEvaluator <NeatGenome, IBlackBox>(genomeDecoder, evaluator, _parallelOptions);
            }
            else
            {
                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);
        }
Beispiel #19
0
        /// <summary>
        /// Initializes the evolution algorithm with the provided IGenomeListEvaluator, IGenomeFactory
        /// an initial population of genomes and a boolean to update the currentGeneration value.
        /// </summary>
        /// <param name="genomeListEvaluator">The genome evaluation scheme for the evolution algorithm.</param>
        /// <param name="genomeFactory">The factory that was used to create the genomeList and which is therefore referenced by the genomes.</param>
        /// <param name="genomeList">An initial genome population.</param>
        /// <param name="addGenerationValue">boolean to trigger currentGeneration value from last genome in list</param>
        public virtual void Initialize(IGenomeListEvaluator <TGenome> genomeListEvaluator,
                                       IGenomeFactory <TGenome> genomeFactory,
                                       List <TGenome> genomeList,
                                       bool addGenerationValue)
        {
            _currentGeneration = 0;

            if (genomeList.Count > 0)
            {
                int LastIndex = genomeList.Count - 1;

                _currentGeneration = genomeList.Min <TGenome, uint>(genome => genome.BirthGeneration);
            }
            _genomeListEvaluator = genomeListEvaluator;
            _genomeFactory       = genomeFactory;
            _genomeList          = genomeList;
            _populationSize      = _genomeList.Count;
            _runState            = RunState.Ready;
            _updateScheme        = new UpdateScheme(new TimeSpan(0, 0, 1));
        }
Beispiel #20
0
        /// <summary>
        /// Construct with the provided number of generations per champion, weight to the hall
        /// of fame fitness, parallel options, and other parameters.
        /// </summary>
        public ParallelHallOfFameListEvaluator(uint generationsPerChampion,
                                               double hallOfFameWeight,
                                               AbstractGenerationalAlgorithm <TGenome> ea,
                                               IGenomeListEvaluator <TGenome> innerEvaluator,
                                               IGenomeDecoder <TGenome, TPhenome> genomeDecoder,
                                               ICoevolutionPhenomeEvaluator <TPhenome> phenomeEvaluator,
                                               ParallelOptions options)
        {
            Debug.Assert(hallOfFameWeight >= 0d);
            Debug.Assert(hallOfFameWeight <= 1d);

            _generationsPerChampion = generationsPerChampion;
            _hallOfFameWeight       = hallOfFameWeight;
            _innerEvaluator         = innerEvaluator;
            _genomeDecoder          = genomeDecoder;
            _phenomeEvaluator       = phenomeEvaluator;
            _parallelOptions        = options;

            _hallOfFame     = new List <TGenome>();
            ea.UpdateEvent += new EventHandler(ea_UpdateEvent);
        }
 /// <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.
 /// Uses the experiments default population size defined in the experiment's config XML.
 /// </summary>
 public NeatEvolutionAlgorithm<NeatGenome> CreateEvolutionAlgorithm(IGenomeListEvaluator<NeatGenome> eval = null)
 {
     return CreateEvolutionAlgorithm(_populationSize, eval);
 }
Beispiel #22
0
 /// <summary>
 /// Initializes the evolution algorithm with the provided IGenomeListEvaluator
 /// and an IGenomeFactory that can be used to create an initial population of genomes.
 /// </summary>
 /// <param name="genomeListEvaluator">The genome evaluation scheme for the evolution algorithm.</param>
 /// <param name="genomeFactory">The factory that was used to create the genomeList and which is therefore referenced by the genomes.</param>
 /// <param name="populationSize">The number of genomes to create for the initial population.</param>
 public virtual void Initialize(IGenomeListEvaluator <TGenome> genomeListEvaluator, IGenomeFactory <TGenome> genomeFactory, int populationSize)
 {
     //NOTE: Not calling the other public overload because it's virtual and that could mess with derived classes
     Initialize_private(genomeListEvaluator, genomeFactory, genomeFactory.CreateGenomeList(populationSize, _currentGeneration));
 }
 /// <summary>
 /// Construct with the provided IGenomeDecoder and IPhenomeEvaluator.
 /// </summary>
 public SelectiveGenomeListEvaluator(IGenomeListEvaluator <TGenome> innerEvaluator,
                                     Predicate <TGenome> selectionPredicate)
 {
     _innerEvaluator     = innerEvaluator;
     _selectionPredicate = selectionPredicate;
 }
Beispiel #24
0
        /// <summary>
        /// Log
        /// </summary>
        /// <param name="ea"></param>
        /// <param name="igle"></param>
        public void Log(NeatEvolutionAlgorithm <NeatGenome> ea)
        {
            Func <NeatGenome, double> costOfNeatGenome = neatGenome => neatGenome.EvaluationInfo.AuxFitnessArr[0]._value;
            // I made the evaluator public myself...
            IGenomeListEvaluator <NeatGenome> igle = ea._genomeListEvaluator;

            var champGenome = ea.GenomeList.Aggregate(
                (currentBest, candidate) =>
                costOfNeatGenome(currentBest) <= costOfNeatGenome(candidate)
                    ? currentBest
                    : candidate);

            var currentTime         = DateTime.Now;
            var currentGeneration   = ea.CurrentGeneration;
            var minimumCost         = costOfNeatGenome(champGenome);
            var meanCost            = ea.GenomeList.Average(costOfNeatGenome);
            var highestCost         = ea.GenomeList.Max(costOfNeatGenome);
            var meanSpecieChampCost = ea.SpecieList.Average(
                specieList => specieList.GenomeList.Min(costOfNeatGenome));
            var champComplexity = champGenome.Complexity;
            var meanComplexity  = ea.GenomeList.Average(neatGenome => neatGenome.Complexity);
            var maxComplexity   = ea.GenomeList.Max(neatGenome => neatGenome.Complexity);
            //double totalEvaluationCount = ea.GenomeList.Sum(neatGenome => neatGenome.EvaluationInfo.EvaluationCount);
            var totalEvaluationCount = igle.EvaluationCount;

            var timeSinceLastCallToLog = currentTime - _oldCurrentTime;

            // To smooth out the evaluations per second statistic, we update only if at least one second has elapsed since it was last updated.
            // This was taken from NeatEvolutionAlgorithm.cs
            // Note that there, the documentation does not actually correspond to the implementation.
            //
            // I would actually write down the exact number I'm getting, calculating
            // sensible rolling averages during processing of results because I save
            // the result as a double instead of an integer.
            // For me, this does not really matter though: a generation takes far longer
            // than a second, and the Log function is called only at the end of a generation...
            //if (timeSinceLastCallToLog.Milliseconds > 999)
            //{
            var evaluationsSinceLastUpdate = totalEvaluationCount - _oldTotalEvaluationCount;
            var evaluationsPerSecond       = ((double)TimeSpan.TicksPerSecond * evaluationsSinceLastUpdate) / (timeSinceLastCallToLog.Ticks);

            // Reset working variables
            _oldCurrentTime              = currentTime;
            _oldTotalEvaluationCount     = totalEvaluationCount;
            _oldEvaluationCountPerSecond = evaluationsPerSecond;
            //}

            IEnumerable <int> specieSizes      = ea.SpecieList.Select(specie => specie.GenomeList.Count).ToList();
            String            specieSizeString = String.Join(";", specieSizes);

            //ea.SpecieList.Aggregate("",(accumulatedString,specie) => specie.)

            _neatsim_logger.WriteLine(
                string.Format(
                    CultureInfo.InvariantCulture,
                    "{0:yyyy-MM-dd HH:mm:ss.fff},{1},{2},{3},{4},{5},{6},{7},{8},{9},{10},{11},{12}",
                    currentTime,                  //1
                    currentGeneration,            //2
                    minimumCost,                  //3
                    meanCost,                     //4
                    highestCost,                  //5
                    meanSpecieChampCost,          //6
                    champComplexity,              //7
                    meanComplexity,               //8
                    maxComplexity,                //9
                    totalEvaluationCount,         //10
                    _oldEvaluationCountPerSecond, //11
                    ea.ComplexityRegulationMode,  //12
                    specieSizeString              //13
                    )
                );
            _neatsim_logger.Flush();

            var    fitnessValues      = ea.GenomeList.Select(individual => individual.EvaluationInfo.AuxFitnessArr[0]._value);
            String fitnessValueString = String.Join(";", fitnessValues);

            _fitness_logger.WriteLine(fitnessValueString);
            _fitness_logger.Flush();
        }
Beispiel #25
0
 /// <summary>
 /// Initializes the evolution algorithm with the provided IGenomeListEvaluator, IGenomeFactory
 /// and an initial population of genomes.
 /// </summary>
 /// <param name="genomeListEvaluator">The genome evaluation scheme for the evolution algorithm.</param>
 /// <param name="genomeFactory">The factory that was used to create the genomeList and which is therefore referenced by the genomes.</param>
 /// <param name="genomeList">An initial genome population.</param>
 public virtual void Initialize(IGenomeListEvaluator <TGenome> genomeListEvaluator, IGenomeFactory <TGenome> genomeFactory, List <TGenome> genomeList)
 {
     Initialize_private(genomeListEvaluator, genomeFactory, genomeList);
 }
 /// <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.
 /// Uses the experiments default population size defined in the experiment's config XML.
 /// </summary>
 public NeatEvolutionAlgorithm <NeatGenome> CreateEvolutionAlgorithm(IGenomeListEvaluator <NeatGenome> eval = null)
 {
     return(CreateEvolutionAlgorithm(_populationSize, eval));
 }
        /// <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 population size parameter that specifies how many genomes to create in an initial randomly
        /// generated population.
        /// </summary>
        public NeatEvolutionAlgorithm <NeatGenome> CreateEvolutionAlgorithm(int populationSize, IGenomeListEvaluator <NeatGenome> eval = null)
        {
            // Create a genome factory with our neat genome parameters object and the appropriate number of input and output neuron genes.
            IGenomeFactory <NeatGenome> genomeFactory = CreateGenomeFactory();

            // Create an initial population of randomly generated genomes.
            List <NeatGenome> genomeList = genomeFactory.CreateGenomeList(populationSize, 0);

            // Create evolution algorithm.
            return(CreateEvolutionAlgorithm(genomeFactory, genomeList, eval));
        }
        public NeatEvolutionAlgorithm <NeatGenome> CreateEvolutionAlgorithm(IGenomeFactory <NeatGenome> genomeFactory, List <NeatGenome> genomeList, IGenomeListEvaluator <NeatGenome> eval = 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, 0.0, 10.0);
            ISpeciationStrategy <NeatGenome> speciationStrategy = new ParallelKMeansClusteringStrategy <NeatGenome>(distanceMetric, _parallelOptions);

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

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

            // Create the MC evaluator
            PasswordCrackingEvaluator.Passwords = _passwords;

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

            // If we're running specially on Condor, skip this
            if (eval == null)
            {
                _evaluator = new PasswordCrackingEvaluator(_guesses, Hashed);

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

                /*
                 * // 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(innerEvaluator, genomeFactory, genomeList);
            }
            else
            {
                // Initialize the evolution algorithm.
                ea.Initialize(eval, genomeFactory, genomeList);
            }



            // Finished. Return the evolution algorithm
            return(ea);
        }
        public NeatEvolutionAlgorithm<NeatGenome> CreateEvolutionAlgorithm(IGenomeFactory<NeatGenome> genomeFactory, List<NeatGenome> genomeList, IGenomeListEvaluator<NeatGenome> eval = 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, 0.0, 10.0);
            ISpeciationStrategy<NeatGenome> speciationStrategy = new ParallelKMeansClusteringStrategy<NeatGenome>(distanceMetric, _parallelOptions);

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

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

            // Create the MC evaluator
            PasswordCrackingEvaluator.Passwords = _passwords;

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

            // If we're running specially on Condor, skip this
            if (eval == null)
            {
                _evaluator = new PasswordCrackingEvaluator(_guesses, Hashed);

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

                /*
                // 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(innerEvaluator, genomeFactory, genomeList);
            }
            else
                // Initialize the evolution algorithm.
                ea.Initialize(eval, 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 population size parameter that specifies how many genomes to create in an initial randomly
        /// generated population.
        /// </summary>
        public NeatEvolutionAlgorithm<NeatGenome> CreateEvolutionAlgorithm(int populationSize, IGenomeListEvaluator<NeatGenome> eval = null)
        {
            // Create a genome factory with our neat genome parameters object and the appropriate number of input and output neuron genes.
            IGenomeFactory<NeatGenome> genomeFactory = CreateGenomeFactory();

            // Create an initial population of randomly generated genomes.
            List<NeatGenome> genomeList = genomeFactory.CreateGenomeList(populationSize, 0);

            // Create evolution algorithm.
            return CreateEvolutionAlgorithm(genomeFactory, genomeList, eval);
        }