/// <summary> /// Construct a starting HyperNEAT population. does not generate the /// initial random population of genomes. /// </summary> /// <param name="theSubstrate">The substrate ID.</param> /// <param name="populationSize">The population size.</param> public NEATPopulation(Substrate theSubstrate, int populationSize) : base(populationSize, new FactorHyperNEATGenome()) { SurvivalRate = DefaultSurvivalRate; WeightRange = 5; InitialConnectionDensity = 0.1; RandomNumberFactory = EncogFramework.Instance .RandomFactory.FactorFactory(); CurrentSubstrate = theSubstrate; InputCount = 6; OutputCount = 2; HyperNEATGenome.BuildCPPNActivationFunctions(_activationFunctions); }
/// <inheritdoc/> public Object Read(Stream istream) { long nextInnovationId = 0; long nextGeneId = 0; var result = new NEATPopulation(); var innovationList = new NEATInnovationList { Population = result }; result.Innovations = innovationList; var reader = new EncogReadHelper(istream); EncogFileSection section; while ((section = reader.ReadNextSection()) != null) { if (section.SectionName.Equals("NEAT-POPULATION") && section.SubSectionName.Equals("INNOVATIONS")) { foreach (String line in section.Lines) { IList <String> cols = EncogFileSection .SplitColumns(line); var innovation = new NEATInnovation(); var innovationId = int.Parse(cols[1]); innovation.InnovationId = innovationId; innovation.NeuronId = int.Parse(cols[2]); result.Innovations.Innovations[cols[0]] = innovation; nextInnovationId = Math.Max(nextInnovationId, innovationId + 1); } } else if (section.SectionName.Equals("NEAT-POPULATION") && section.SubSectionName.Equals("SPECIES")) { NEATGenome lastGenome = null; BasicSpecies lastSpecies = null; foreach (String line in section.Lines) { IList <String> cols = EncogFileSection.SplitColumns(line); if (String.Compare(cols[0], "s", StringComparison.OrdinalIgnoreCase) == 0) { lastSpecies = new BasicSpecies { Population = result, Age = int.Parse(cols[1]), BestScore = CSVFormat.EgFormat.Parse(cols[2]), GensNoImprovement = int.Parse(cols[3]) }; result.Species.Add(lastSpecies); } else if (String.Compare(cols[0], "g", StringComparison.OrdinalIgnoreCase) == 0) { bool isLeader = lastGenome == null; lastGenome = new NEATGenome { InputCount = result.InputCount, OutputCount = result.OutputCount, Species = lastSpecies, AdjustedScore = CSVFormat.EgFormat.Parse(cols[1]), Score = CSVFormat.EgFormat.Parse(cols[2]), BirthGeneration = int.Parse(cols[3]) }; lastSpecies.Add(lastGenome); if (isLeader) { lastSpecies.Leader = lastGenome; } } else if (String.Compare(cols[0], "n", StringComparison.OrdinalIgnoreCase) == 0) { var neuronGene = new NEATNeuronGene(); int geneId = int.Parse(cols[1]); neuronGene.Id = geneId; IActivationFunction af = EncogFileSection.ParseActivationFunction(cols[2]); neuronGene.ActivationFunction = af; neuronGene.NeuronType = PersistNEATPopulation.StringToNeuronType(cols[3]); neuronGene.InnovationId = int.Parse(cols[4]); lastGenome.NeuronsChromosome.Add(neuronGene); nextGeneId = Math.Max(geneId + 1, nextGeneId); } else if (String.Compare(cols[0], "l", StringComparison.OrdinalIgnoreCase) == 0) { var linkGene = new NEATLinkGene { Id = int.Parse(cols[1]), Enabled = (int.Parse(cols[2]) > 0), FromNeuronId = int.Parse(cols[3]), ToNeuronId = int.Parse(cols[4]), Weight = CSVFormat.EgFormat.Parse(cols[5]), InnovationId = int.Parse(cols[6]) }; lastGenome.LinksChromosome.Add(linkGene); } } } else if (section.SectionName.Equals("NEAT-POPULATION") && section.SubSectionName.Equals("CONFIG")) { IDictionary <string, string> prm = section.ParseParams(); string afStr = prm[NEATPopulation.PropertyNEATActivation]; if (String.Compare(afStr, TypeCppn, StringComparison.OrdinalIgnoreCase) == 0) { HyperNEATGenome.BuildCPPNActivationFunctions(result.ActivationFunctions); } else { result.NEATActivationFunction = EncogFileSection.ParseActivationFunction(prm, NEATPopulation.PropertyNEATActivation); } result.ActivationCycles = EncogFileSection.ParseInt(prm, PersistConst.ActivationCycles); result.InputCount = EncogFileSection.ParseInt(prm, PersistConst.InputCount); result.OutputCount = EncogFileSection.ParseInt(prm, PersistConst.OutputCount); result.PopulationSize = EncogFileSection.ParseInt(prm, NEATPopulation.PropertyPopulationSize); result.SurvivalRate = EncogFileSection.ParseDouble(prm, NEATPopulation.PropertySurvivalRate); result.ActivationCycles = EncogFileSection.ParseInt(prm, NEATPopulation.PropertyCycles); } } // set factories if (result.IsHyperNEAT) { result.GenomeFactory = new FactorHyperNEATGenome(); result.CODEC = new HyperNEATCODEC(); } else { result.GenomeFactory = new FactorNEATGenome(); result.CODEC = new NEATCODEC(); } // set the next ID's result.InnovationIDGenerate.CurrentID = nextInnovationId; result.GeneIdGenerate.CurrentID = nextGeneId; // find first genome, which should be the best genome if (result.Species.Count > 0) { ISpecies species = result.Species[0]; if (species.Members.Count > 0) { result.BestGenome = species.Members[0]; } } return(result); }