/// <summary>
 /// Copy constructor.
 /// </summary>
 /// <param name="copyFrom">The settings object to copy.</param>
 public NeatReproductionAsexualSettings(NeatReproductionAsexualSettings copyFrom)
 {
     this.ConnectionWeightMutationProbability = copyFrom.ConnectionWeightMutationProbability;
     this.AddNodeMutationProbability          = copyFrom.AddNodeMutationProbability;
     this.AddConnectionMutationProbability    = copyFrom.AddConnectionMutationProbability;
     this.DeleteConnectionMutationProbability = copyFrom.DeleteConnectionMutationProbability;
 }
 private void SendSettingsToUI(NeatReproductionAsexualSettings settings)
 {
     SetValue(txtConnectionWeightMutationProbability, settings.ConnectionWeightMutationProbability);
     SetValue(txtAddNodeMutationProbability, settings.AddNodeMutationProbability);
     SetValue(txtAddConnectionMutationProbability, settings.AddConnectionMutationProbability);
     SetValue(txtDeleteConnectionMutationProbability, settings.DeleteConnectionMutationProbability);
 }
Пример #3
0
 /// <summary>
 /// Read json into a target instance of <see cref="NeatReproductionAsexualSettings"/>.
 /// Settings that are present are read and set on the target settings object; all other settings
 /// remain unchanged on the target object.
 /// </summary>
 /// <param name="target">The target settings object to store the read values on.</param>
 /// <param name="jelem">The json element to read from.</param>
 public static void Read(
     NeatReproductionAsexualSettings target,
     JsonElement jelem)
 {
     ReadDoubleOptional(jelem, "connectionWeightMutationProbability", x => target.ConnectionWeightMutationProbability = x);
     ReadDoubleOptional(jelem, "addNodeMutationProbability", x => target.AddNodeMutationProbability                   = x);
     ReadDoubleOptional(jelem, "addConnectionMutationProbability", x => target.AddConnectionMutationProbability       = x);
     ReadDoubleOptional(jelem, "deleteConnectionMutationProbability", x => target.DeleteConnectionMutationProbability = x);
     target.Validate();
 }
Пример #4
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);
        }
 /// <summary>
 /// Create a new instance of <see cref="DiscreteDistribution"/> that represents a subset of the possible
 /// genome mutation types, and their relative probabilities. The subset consists of mutation types that
 /// are non-destructive (i.e. weight mutation, add node mutation, add connection mutation).
 /// </summary>
 /// <param name="settings">Asexual reproduction settings.</param>
 /// <returns>A new instance of <see cref="DiscreteDistribution"/>.</returns>
 private static DiscreteDistribution CreateMutationTypeDiscreteDistribution_NonDestructive(
     NeatReproductionAsexualSettings settings)
 {
     double[] probabilities = new double[]
     {
         settings.ConnectionWeightMutationProbability,
         settings.AddNodeMutationProbability,
         settings.AddConnectionMutationProbability
     };
     return(new DiscreteDistribution(probabilities));
 }
 /// <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())
 {
 }
        private void GetSettingsFromUI(NeatReproductionAsexualSettings settings)
        {
            double connectionWeightMutationProbability = GetValue(txtConnectionWeightMutationProbability, settings.ConnectionWeightMutationProbability);
            double addNodeMutationProbability          = GetValue(txtAddNodeMutationProbability, settings.AddNodeMutationProbability);
            double addConnectionMutationProbability    = GetValue(txtAddConnectionMutationProbability, settings.AddConnectionMutationProbability);
            double deleteConnectionMutationProbability = GetValue(txtDeleteConnectionMutationProbability, settings.DeleteConnectionMutationProbability);

            Normalize(
                ref connectionWeightMutationProbability, ref addNodeMutationProbability,
                ref addConnectionMutationProbability, ref deleteConnectionMutationProbability);

            settings.ConnectionWeightMutationProbability = connectionWeightMutationProbability;
            settings.AddNodeMutationProbability          = addNodeMutationProbability;
            settings.AddConnectionMutationProbability    = addConnectionMutationProbability;
            settings.DeleteConnectionMutationProbability = deleteConnectionMutationProbability;
        }
    /// <summary>
    /// Creates a new settings object based on the current settings object, but modified to be suitable for use when
    /// the evolution algorithm is in simplifying mode.
    /// </summary>
    /// <returns>A new instance of <see cref="NeatReproductionAsexualSettings"/>.</returns>
    public NeatReproductionAsexualSettings CreateSimplifyingSettings()
    {
        // Invoke the copy constructor with the current object.
        //
        // Note. Currently all of the settings are modified, therefore it's not necessary to use the copy constructor
        // however, if additional settings are added to the settings class then they will be handled automatically here
        // without having to update this code, so this is a slightly safer approach.
        var settings = new NeatReproductionAsexualSettings(this)
        {
            ConnectionWeightMutationProbability = 0.6,
            AddNodeMutationProbability          = 0.0,
            AddConnectionMutationProbability    = 0.0,
            DeleteConnectionMutationProbability = 0.4
        };

        return(settings);
    }
    public void Read()
    {
        JsonDocument jdoc = JsonDocument.Parse(
            @"{
    ""connectionWeightMutationProbability"":0.11,
    ""addNodeMutationProbability"":0.22,
    ""addConnectionMutationProbability"":0.33,
    ""deleteConnectionMutationProbability"":0.34
}");
        // Init a default settings object.
        var settings = new NeatReproductionAsexualSettings();

        // Read json properties into the settings object.
        NeatReproductionAsexualSettingsJsonReader.Read(settings, jdoc.RootElement);

        // Assert the expected values.
        Assert.Equal(0.11, settings.ConnectionWeightMutationProbability);
        Assert.Equal(0.22, settings.AddNodeMutationProbability);
        Assert.Equal(0.33, settings.AddConnectionMutationProbability);
        Assert.Equal(0.34, settings.DeleteConnectionMutationProbability);
    }
        public void Read()
        {
            JObject jobj = JObject.Parse(
                @"{
    'connectionWeightMutationProbability':0.11,
    'addNodeMutationProbability':0.22,
    'addConnectionMutationProbability':0.33,
    'deleteConnectionMutationProbability':0.44,
}");
            // Init a default settings object.
            var settings = new NeatReproductionAsexualSettings();

            // Read json properties into the settings object.
            NeatReproductionAsexualSettingsJsonReader.Read(settings, jobj);

            // Assert the expected values.
            Assert.AreEqual(0.11, settings.ConnectionWeightMutationProbability);
            Assert.AreEqual(0.22, settings.AddNodeMutationProbability);
            Assert.AreEqual(0.33, settings.AddConnectionMutationProbability);
            Assert.AreEqual(0.44, settings.DeleteConnectionMutationProbability);
        }
    /// <summary>
    /// Construct a new instance.
    /// </summary>
    /// <param name="metaNeatGenome">NeatGenome metadata.</param>
    /// <param name="genomeBuilder">NeatGenome builder.</param>
    /// <param name="genomeIdSeq">Genome ID sequence; for obtaining new genome IDs.</param>
    /// <param name="innovationIdSeq">Innovation ID sequence; for obtaining new innovation IDs.</param>
    /// <param name="generationSeq">Generation sequence; for obtaining the current generation number.</param>
    /// <param name="addedNodeBuffer">A history buffer of added nodes.</param>
    /// <param name="settings">Asexual reproduction settings.</param>
    /// <param name="weightMutationScheme">Connection weight mutation scheme.</param>
    public NeatReproductionAsexual(
        MetaNeatGenome <T> metaNeatGenome,
        INeatGenomeBuilder <T> genomeBuilder,
        Int32Sequence genomeIdSeq,
        Int32Sequence innovationIdSeq,
        Int32Sequence generationSeq,
        AddedNodeBuffer addedNodeBuffer,
        NeatReproductionAsexualSettings settings,
        WeightMutationScheme <T> weightMutationScheme)
    {
        var settingsComplexifying = settings;
        var settingsSimplifying   = settings.CreateSimplifyingSettings();

        _mutationTypeDistributionsComplexifying = new MutationTypeDistributions(settingsComplexifying);
        _mutationTypeDistributionsSimplifying   = new MutationTypeDistributions(settingsSimplifying);
        _mutationTypeDistributionsCurrent       = _mutationTypeDistributionsComplexifying;

        // Instantiate reproduction strategies.
        _mutateWeightsStrategy    = new MutateWeightsStrategy <T>(genomeBuilder, genomeIdSeq, generationSeq, weightMutationScheme);
        _deleteConnectionStrategy = new DeleteConnectionStrategy <T>(genomeBuilder, genomeIdSeq, generationSeq);

        // Add connection mutation; select acyclic/cyclic strategy as appropriate.
        if (metaNeatGenome.IsAcyclic)
        {
            _addConnectionStrategy = new AddAcyclicConnectionStrategy <T>(
                metaNeatGenome, genomeBuilder,
                genomeIdSeq, generationSeq);
        }
        else
        {
            _addConnectionStrategy = new AddCyclicConnectionStrategy <T>(
                metaNeatGenome, genomeBuilder,
                genomeIdSeq, generationSeq);
        }

        _addNodeStrategy = new AddNodeStrategy <T>(metaNeatGenome, genomeBuilder, genomeIdSeq, innovationIdSeq, generationSeq, addedNodeBuffer);
    }
Пример #12
0
        public NeatEvolutionAlgorithm <double> CreateNeatEvolutionAlgorithm()
        {
            // Create a genome evaluator.
            var genomeListEvaluator = CreateGenomeListEvaluator(out int inputCount, out int outputCount);

            // Create an initial population.
            _metaNeatGenome          = CreateMetaNeatGenome(inputCount, outputCount);
            _eaSettings              = new NeatEvolutionAlgorithmSettings();
            _eaSettings.SpeciesCount = 40;
            _neatPop = CreatePopulation(_metaNeatGenome, 600);

            // Create a speciation strategy instance.
            var distanceMetric     = new ManhattanDistanceMetric(1.0, 0.0, 10.0);
            var speciationStrategy = new SharpNeat.Neat.Speciation.GeneticKMeans.Parallelized.GeneticKMeansSpeciationStrategy <double>(distanceMetric, 5);

            // Create an asexual reproduction settings object (default settings).
            var reproductionAsexualSettings = new NeatReproductionAsexualSettings();

            // Create a sexual reproduction settings object (default settings).
            var reproductionSexualSettings = new NeatReproductionSexualSettings();

            // Create a connection weight mutation scheme.
            var weightMutationScheme = WeightMutationSchemeFactory.CreateDefaultScheme(_metaNeatGenome.ConnectionWeightScale);

            // Pull all of the parts together into an evolution algorithm instance.
            var ea = new NeatEvolutionAlgorithm <double>(
                _eaSettings,
                genomeListEvaluator,
                speciationStrategy,
                _neatPop,
                reproductionAsexualSettings,
                reproductionSexualSettings,
                weightMutationScheme);

            return(ea);
        }
 /// <summary>
 /// Construct a new instance.
 /// </summary>
 /// <param name="settings">Asexual reproduction settings.</param>
 public MutationTypeDistributions(NeatReproductionAsexualSettings settings)
 {
     this.MutationTypeDistribution = CreateMutationTypeDiscreteDistribution(settings);
     this.MutationTypeDistributionNonDestructive = CreateMutationTypeDiscreteDistribution_NonDestructive(settings);
 }