/// <summary> /// Construct a starting NEAT population. /// </summary> /// /// <param name="inputCount">The input neuron count.</param> /// <param name="outputCount">The output neuron count.</param> /// <param name="populationSize">The population size.</param> public NEATPopulation(int inputCount, int outputCount, int populationSize) : base(populationSize) { _neatActivationFunction = new ActivationSigmoid(); _outputActivationFunction = new ActivationLinear(); InputCount = inputCount; OutputCount = outputCount; if (populationSize == 0) { throw new NeuralNetworkError( "Population must have more than zero genomes."); } // create the initial population for (int i = 0; i < populationSize; i++) { var genome = new NEATGenome(AssignGenomeID(), inputCount, outputCount); Add(genome); } // create initial innovations var genome2 = (NEATGenome)Genomes[0]; Innovations = new NEATInnovationList(this, genome2.Links, genome2.Neurons); }
/// <summary> /// Create an initial random population. /// </summary> public void Reset() { // create the genome factory if (IsHyperNEAT) { CODEC = new HyperNEATCODEC(); GenomeFactory = new FactorHyperNEATGenome(); } else { CODEC = new NEATCODEC(); GenomeFactory = new FactorNEATGenome(); } // create the new genomes Species.Clear(); // reset counters GeneIdGenerate.CurrentID = 1; InnovationIDGenerate.CurrentID = 1; EncogRandom rnd = RandomNumberFactory.Factor(); // create one default species BasicSpecies defaultSpecies = new BasicSpecies(); defaultSpecies.Population = this; // create the initial population for (int i = 0; i < PopulationSize; i++) { NEATGenome genome = GenomeFactory.Factor(rnd, this, InputCount, OutputCount, InitialConnectionDensity); defaultSpecies.Add(genome); } defaultSpecies.Leader = defaultSpecies.Members[0]; Species.Add(defaultSpecies); // create initial innovations Innovations = new NEATInnovationList(this); }
/// <summary> /// Read the object. /// </summary> /// <param name="mask0">The stream to read the object from.</param> /// <returns>The object that was loaded.</returns> public virtual Object Read(Stream mask0) { var result = new NEATPopulation(); var innovationList = new NEATInnovationList { Population = result }; result.Innovations = innovationList; var ins0 = new EncogReadHelper(mask0); IDictionary <Int32, ISpecies> speciesMap = new Dictionary <Int32, ISpecies>(); IDictionary <ISpecies, Int32> leaderMap = new Dictionary <ISpecies, Int32>(); IDictionary <Int32, IGenome> genomeMap = new Dictionary <Int32, IGenome>(); EncogFileSection section; while ((section = ins0.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 { InnovationID = Int32.Parse(cols[0]), InnovationType = StringToInnovationType(cols[1]), NeuronType = StringToNeuronType(cols[2]), SplitX = CSVFormat.EgFormat.Parse(cols[3]), SplitY = CSVFormat.EgFormat.Parse(cols[4]), NeuronID = Int32.Parse(cols[5]), FromNeuronID = Int32.Parse(cols[6]), ToNeuronID = Int32.Parse(cols[7]) }; result.Innovations.Add(innovation); } } else if (section.SectionName.Equals("NEAT-POPULATION") && section.SubSectionName.Equals("SPECIES")) { foreach (String line in section.Lines) { String[] cols = line.Split(','); var species = new BasicSpecies { SpeciesID = Int32.Parse(cols[0]), Age = Int32.Parse(cols[1]), BestScore = CSVFormat.EgFormat.Parse(cols[2]), GensNoImprovement = Int32.Parse(cols[3]), SpawnsRequired = CSVFormat.EgFormat .Parse(cols[4]) }; species.SpawnsRequired = CSVFormat.EgFormat .Parse(cols[5]); leaderMap[(species)] = (Int32.Parse(cols[6])); result.Species.Add(species); speciesMap[((int)species.SpeciesID)] = (species); } } else if (section.SectionName.Equals("NEAT-POPULATION") && section.SubSectionName.Equals("GENOMES")) { NEATGenome lastGenome = null; foreach (String line in section.Lines) { IList <String> cols = EncogFileSection.SplitColumns(line); if (cols[0].Equals("g", StringComparison.InvariantCultureIgnoreCase)) { lastGenome = new NEATGenome { NeuronsChromosome = new Chromosome(), LinksChromosome = new Chromosome() }; lastGenome.Chromosomes.Add(lastGenome.NeuronsChromosome); lastGenome.Chromosomes.Add(lastGenome.LinksChromosome); lastGenome.GenomeID = Int32.Parse(cols[1]); lastGenome.SpeciesID = Int32.Parse(cols[2]); lastGenome.AdjustedScore = CSVFormat.EgFormat .Parse(cols[3]); lastGenome.AmountToSpawn = CSVFormat.EgFormat .Parse(cols[4]); lastGenome.NetworkDepth = Int32.Parse(cols[5]); lastGenome.Score = CSVFormat.EgFormat.Parse(cols[6]); result.Add(lastGenome); genomeMap[(int)lastGenome.GenomeID] = lastGenome; } else if (cols[0].Equals("n", StringComparison.InvariantCultureIgnoreCase)) { var neuronGene = new NEATNeuronGene { Id = Int32.Parse(cols[1]), NeuronType = StringToNeuronType(cols[2]), Enabled = Int32.Parse(cols[3]) > 0, InnovationId = Int32.Parse(cols[4]), ActivationResponse = CSVFormat.EgFormat .Parse(cols[5]), SplitX = CSVFormat.EgFormat.Parse(cols[6]), SplitY = CSVFormat.EgFormat.Parse(cols[7]) }; lastGenome.Neurons.Add(neuronGene); } else if (cols[0].Equals("l", StringComparison.InvariantCultureIgnoreCase)) { var linkGene = new NEATLinkGene(); linkGene.Id = Int32.Parse(cols[1]); linkGene.Enabled = Int32.Parse(cols[2]) > 0; linkGene.Recurrent = Int32.Parse(cols[3]) > 0; linkGene.FromNeuronID = Int32.Parse(cols[4]); linkGene.ToNeuronID = Int32.Parse(cols[5]); linkGene.Weight = CSVFormat.EgFormat.Parse(cols[6]); linkGene.InnovationId = Int32.Parse(cols[7]); lastGenome.Links.Add(linkGene); } } } else if (section.SectionName.Equals("NEAT-POPULATION") && section.SubSectionName.Equals("CONFIG")) { IDictionary <String, String> paras = section.ParseParams(); result.NeatActivationFunction = EncogFileSection .ParseActivationFunction(paras, NEATPopulation.PropertyNEATActivation); result.OutputActivationFunction = EncogFileSection .ParseActivationFunction(paras, NEATPopulation.PropertyOutputActivation); result.Snapshot = EncogFileSection.ParseBoolean(paras, PersistConst.Snapshot); result.InputCount = EncogFileSection.ParseInt(paras, PersistConst.InputCount); result.OutputCount = EncogFileSection.ParseInt(paras, PersistConst.OutputCount); result.OldAgePenalty = EncogFileSection.ParseDouble(paras, PopulationConst.PropertyOldAgePenalty); result.OldAgeThreshold = EncogFileSection.ParseInt(paras, PopulationConst.PropertyOldAgeThreshold); result.PopulationSize = EncogFileSection.ParseInt(paras, PopulationConst.PropertyPopulationSize); result.SurvivalRate = EncogFileSection.ParseDouble(paras, PopulationConst.PropertySurvivalRate); result.YoungBonusAgeThreshhold = EncogFileSection.ParseInt( paras, PopulationConst.PropertyYoungAgeThreshold); result.YoungScoreBonus = EncogFileSection.ParseDouble(paras, PopulationConst.PropertyYoungAgeBonus); result.GenomeIDGenerate.CurrentID = EncogFileSection.ParseInt(paras, PopulationConst. PropertyNextGenomeID); result.InnovationIDGenerate.CurrentID = EncogFileSection.ParseInt(paras, PopulationConst. PropertyNextInnovationID); result.GeneIDGenerate.CurrentID = EncogFileSection.ParseInt(paras, PopulationConst. PropertyNextGeneID); result.SpeciesIDGenerate.CurrentID = EncogFileSection.ParseInt(paras, PopulationConst. PropertyNextSpeciesID); } } // now link everything up // first put all the genomes into correct species foreach (IGenome genome in result.Genomes) { var neatGenome = (NEATGenome)genome; var speciesId = (int)neatGenome.SpeciesID; if (speciesMap.ContainsKey(speciesId)) { ISpecies s = speciesMap[speciesId]; s.Members.Add(neatGenome); } neatGenome.InputCount = result.InputCount; neatGenome.OutputCount = result.OutputCount; } // set the species leader links foreach (ISpecies species in leaderMap.Keys) { int leaderID = leaderMap[species]; IGenome leader = genomeMap[leaderID]; species.Leader = leader; ((BasicSpecies)species).Population = result; } return(result); }
/// <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); }