/// <summary> /// Read json into a target instance of <see cref="NeatEvolutionAlgorithmSettings"/>. /// 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( NeatEvolutionAlgorithmSettings target, JsonElement jelem) { ReadIntOptional(jelem, "speciesCount", x => target.SpeciesCount = x); ReadDoubleOptional(jelem, "elitismProportion", x => target.ElitismProportion = x); ReadDoubleOptional(jelem, "selectionProportion", x => target.SelectionProportion = x); ReadDoubleOptional(jelem, "offspringAsexualProportion", x => target.OffspringAsexualProportion = x); ReadDoubleOptional(jelem, "offspringSexualProportion", x => target.OffspringSexualProportion = x); ReadDoubleOptional(jelem, "interspeciesMatingProportion", x => target.InterspeciesMatingProportion = x); ReadIntOptional(jelem, "statisticsMovingAverageHistoryLength", x => target.StatisticsMovingAverageHistoryLength = x); target.Validate(); }
/// <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) { // Perform some basic validation of the provided settings objects. eaSettings.Validate(); reproductionAsexualSettings.Validate(); reproductionSexualSettings.Validate(); // Store instance fields, and null check as we go. _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 is null) { throw new ArgumentNullException(nameof(reproductionAsexualSettings)); } if (reproductionSexualSettings is 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); }