/// <inheritdoc/> public NEATGenome Factor(EncogRandom rnd, NEATPopulation pop, int inputCount, int outputCount, double connectionDensity) { return new NEATGenome(rnd, pop, inputCount, outputCount, connectionDensity); }
public DisplayBoxes(NEATPopulation thePopulation) { InitializeComponent(); testCase.InitTestCase(0); this.pop = thePopulation; Render(); }
/// <inheritdoc/> public NEATGenome Factor(EncogRandom rnd, NEATPopulation pop, int inputCount, int outputCount, double connectionDensity) { return(new NEATGenome(rnd, pop, inputCount, outputCount, connectionDensity)); }
/// <summary> /// Create a NEAT population. /// </summary> /// <param name="architecture">The architecture string to use.</param> /// <param name="input">The input count.</param> /// <param name="output">The output count.</param> /// <returns>The population.</returns> public IMLMethod Create(String architecture, int input, int output) { if (input <= 0) { throw new EncogError("Must have at least one input for NEAT."); } if (output <= 0) { throw new EncogError("Must have at least one output for NEAT."); } IDictionary<String, String> args = ArchitectureParse.ParseParams(architecture); ParamsHolder holder = new ParamsHolder(args); int populationSize = holder.GetInt( MLMethodFactory.PropertyPopulationSize, false, 1000); int cycles = holder.GetInt( MLMethodFactory.PropertyCycles, false, NEATPopulation.DefaultCycles); IActivationFunction af = this.factory.Create( holder.GetString(MLMethodFactory.PropertyAF, false, MLActivationFactory.AF_SSIGMOID)); NEATPopulation pop = new NEATPopulation(input, output, populationSize); pop.Reset(); pop.ActivationCycles = cycles; pop.NEATActivationFunction = af; return pop; }
/// <summary> /// Construct a NEAT or HyperNEAT trainer. /// </summary> /// <param name="calculateScore">The score function.</param> /// <param name="inputCount">The input count.</param> /// <param name="outputCount">The output count.</param> /// <param name="populationSize">The population size.</param> /// <returns></returns> public static TrainEA ConstructNEATTrainer( ICalculateScore calculateScore, int inputCount, int outputCount, int populationSize) { var pop = new NEATPopulation(inputCount, outputCount, populationSize); pop.Reset(); return ConstructNEATTrainer(pop, calculateScore); }
/// <summary> /// Construct a NEAT or HyperNEAT trainer. /// </summary> /// <param name="calculateScore">The score function.</param> /// <param name="inputCount">The input count.</param> /// <param name="outputCount">The output count.</param> /// <param name="populationSize">The population size.</param> /// <returns></returns> public static TrainEA ConstructNEATTrainer( ICalculateScore calculateScore, int inputCount, int outputCount, int populationSize) { var pop = new NEATPopulation(inputCount, outputCount, populationSize); pop.Reset(); return(ConstructNEATTrainer(pop, calculateScore)); }
public void ResetTraining() { Substrate substrate = SubstrateFactory.factorSandwichSubstrate(11, 11); BoxesScore score = new BoxesScore(11); pop = new NEATPopulation(substrate, 500); pop.ActivationCycles = 4; pop.Reset(); train = NEATUtil.ConstructNEATTrainer(pop, score); OriginalNEATSpeciation speciation = new OriginalNEATSpeciation(); train.Speciation = new OriginalNEATSpeciation(); }
private void Validate(NEATPopulation pop) { Assert.AreEqual(0.3, pop.OldAgePenalty); Assert.AreEqual(50, pop.OldAgeThreshold); Assert.AreEqual(10, pop.PopulationSize); Assert.AreEqual(0.2, pop.SurvivalRate); Assert.AreEqual(10, pop.YoungBonusAgeThreshold); Assert.AreEqual(0.3, pop.YoungScoreBonus); // see if the population can actually be used to train IMLDataSet trainingSet = new BasicMLDataSet(XOR.XORInput, XOR.XORIdeal); ICalculateScore score = new TrainingSetScore(trainingSet); NEATTraining train = new NEATTraining(score, pop); train.Iteration(); }
/// <summary> /// Program entry point. /// </summary> /// <param name="app">Holds arguments and other info.</param> public void Execute(IExampleInterface app) { IMLDataSet trainingSet = new BasicMLDataSet(XORInput, XORIdeal); NEATPopulation pop = new NEATPopulation(2, 1, 1000); pop.Reset(); pop.InitialConnectionDensity = 1.0; // not required, but speeds processing. ICalculateScore score = new TrainingSetScore(trainingSet); // train the neural network TrainEA train = NEATUtil.ConstructNEATTrainer(pop,score); EncogUtility.TrainToError(train, 0.01); NEATNetwork network = (NEATNetwork)train.CODEC.Decode(train.BestGenome); // test the neural network Console.WriteLine(@"Neural Network Results:"); EncogUtility.Evaluate(network, trainingSet); }
/// <summary> /// Program entry point. /// </summary> /// <param name="app">Holds arguments and other info.</param> public void Execute(IExampleInterface app) { IMLDataSet trainingSet = new BasicMLDataSet(XORInput, XORIdeal); var pop = new NEATPopulation(2, 1, 1000); ICalculateScore score = new TrainingSetScore(trainingSet); // train the neural network var step = new ActivationStep(); step.Center = 0.5; pop.OutputActivationFunction = step; var train = new NEATTraining(score, pop); EncogUtility.TrainToError(train, 0.01); var network = (NEATNetwork) train.Method; network.ClearContext(); // test the neural network Console.WriteLine(@"Neural Network Results:"); EncogUtility.Evaluate(network, trainingSet); }
/// <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; }
/// <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; }
/// <summary> /// Construct a NEAT (or HyperNEAT trainer. /// </summary> /// <param name="population">The population.</param> /// <param name="calculateScore">The score function.</param> /// <returns>The NEAT EA trainer.</returns> public static TrainEA ConstructNEATTrainer(NEATPopulation population, ICalculateScore calculateScore) { var result = new TrainEA(population, calculateScore) { Speciation = new OriginalNEATSpeciation() }; result.Selection = new TruncationSelection(result, 0.3); var weightMutation = new CompoundOperator(); weightMutation.Components.Add( 0.1125, new NEATMutateWeights(new SelectFixed(1), new MutatePerturbLinkWeight(0.02))); weightMutation.Components.Add( 0.1125, new NEATMutateWeights(new SelectFixed(2), new MutatePerturbLinkWeight(0.02))); weightMutation.Components.Add( 0.1125, new NEATMutateWeights(new SelectFixed(3), new MutatePerturbLinkWeight(0.02))); weightMutation.Components.Add( 0.1125, new NEATMutateWeights(new SelectProportion(0.02), new MutatePerturbLinkWeight(0.02))); weightMutation.Components.Add( 0.1125, new NEATMutateWeights(new SelectFixed(1), new MutatePerturbLinkWeight(1))); weightMutation.Components.Add( 0.1125, new NEATMutateWeights(new SelectFixed(2), new MutatePerturbLinkWeight(1))); weightMutation.Components.Add( 0.1125, new NEATMutateWeights(new SelectFixed(3), new MutatePerturbLinkWeight(1))); weightMutation.Components.Add( 0.1125, new NEATMutateWeights(new SelectProportion(0.02), new MutatePerturbLinkWeight(1))); weightMutation.Components.Add( 0.03, new NEATMutateWeights(new SelectFixed(1), new MutateResetLinkWeight())); weightMutation.Components.Add( 0.03, new NEATMutateWeights(new SelectFixed(2), new MutateResetLinkWeight())); weightMutation.Components.Add( 0.03, new NEATMutateWeights(new SelectFixed(3), new MutateResetLinkWeight())); weightMutation.Components.Add( 0.01, new NEATMutateWeights(new SelectProportion(0.02), new MutateResetLinkWeight())); weightMutation.Components.FinalizeStructure(); result.ChampMutation = weightMutation; result.AddOperation(0.5, new NEATCrossover()); result.AddOperation(0.494, weightMutation); result.AddOperation(0.0005, new NEATMutateAddNode()); result.AddOperation(0.005, new NEATMutateAddLink()); result.AddOperation(0.0005, new NEATMutateRemoveLink()); result.Operators.FinalizeStructure(); if (population.IsHyperNEAT) { result.CODEC = new HyperNEATCODEC(); } else { result.CODEC = new NEATCODEC(); } return(result); }
/// <summary> /// Construct a NEAT (or HyperNEAT trainer. /// </summary> /// <param name="population">The population.</param> /// <param name="calculateScore">The score function.</param> /// <returns>The NEAT EA trainer.</returns> public static TrainEA ConstructNEATTrainer(NEATPopulation population, ICalculateScore calculateScore) { var result = new TrainEA(population, calculateScore) {Speciation = new OriginalNEATSpeciation()}; result.Selection = new TruncationSelection(result, 0.3); var weightMutation = new CompoundOperator(); weightMutation.Components.Add( 0.1125, new NEATMutateWeights(new SelectFixed(1), new MutatePerturbLinkWeight(0.02))); weightMutation.Components.Add( 0.1125, new NEATMutateWeights(new SelectFixed(2), new MutatePerturbLinkWeight(0.02))); weightMutation.Components.Add( 0.1125, new NEATMutateWeights(new SelectFixed(3), new MutatePerturbLinkWeight(0.02))); weightMutation.Components.Add( 0.1125, new NEATMutateWeights(new SelectProportion(0.02), new MutatePerturbLinkWeight(0.02))); weightMutation.Components.Add( 0.1125, new NEATMutateWeights(new SelectFixed(1), new MutatePerturbLinkWeight(1))); weightMutation.Components.Add( 0.1125, new NEATMutateWeights(new SelectFixed(2), new MutatePerturbLinkWeight(1))); weightMutation.Components.Add( 0.1125, new NEATMutateWeights(new SelectFixed(3), new MutatePerturbLinkWeight(1))); weightMutation.Components.Add( 0.1125, new NEATMutateWeights(new SelectProportion(0.02), new MutatePerturbLinkWeight(1))); weightMutation.Components.Add( 0.03, new NEATMutateWeights(new SelectFixed(1), new MutateResetLinkWeight())); weightMutation.Components.Add( 0.03, new NEATMutateWeights(new SelectFixed(2), new MutateResetLinkWeight())); weightMutation.Components.Add( 0.03, new NEATMutateWeights(new SelectFixed(3), new MutateResetLinkWeight())); weightMutation.Components.Add( 0.01, new NEATMutateWeights(new SelectProportion(0.02), new MutateResetLinkWeight())); weightMutation.Components.FinalizeStructure(); result.ChampMutation = weightMutation; result.AddOperation(0.5, new NEATCrossover()); result.AddOperation(0.494, weightMutation); result.AddOperation(0.0005, new NEATMutateAddNode()); result.AddOperation(0.005, new NEATMutateAddLink()); result.AddOperation(0.0005, new NEATMutateRemoveLink()); result.Operators.FinalizeStructure(); if (population.IsHyperNEAT) { result.CODEC = new HyperNEATCODEC(); } else { result.CODEC = new NEATCODEC(); } 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); }
/// <summary> /// Builds and trains a neat network. /// </summary> /// <param name="aset">The IMLDataset.</param> /// <param name="inputcounts">The inputcounts.</param> /// <param name="outputcounts">The outputcounts.</param> /// <param name="populationsize">The populationsize.</param> /// <param name="ToErrorTraining">To error rate you want to train too.</param> /// <returns>a trained netnetwork.</returns> public static NEATNetwork BuildTrainNeatNetwork(IMLDataSet aset, int inputcounts, int outputcounts, int populationsize, double ToErrorTraining) { NEATPopulation pop = new NEATPopulation(inputcounts, outputcounts, populationsize); ICalculateScore score = new TrainingSetScore(aset); // train the neural network ActivationStep step = new ActivationStep(); step.Center = 0.5; pop.OutputActivationFunction = step; NEATTraining train = new NEATTraining(score, pop); EncogUtility.TrainToError(train, ToErrorTraining); NEATNetwork network = (NEATNetwork)train.Method; return network; }
public virtual object Read(Stream mask0) { IDictionary<ISpecies, int> dictionary2; IDictionary<int, IGenome> dictionary3; EncogFileSection section; IDictionary<string, string> dictionary4; int num; int num2; NEATPopulation population = new NEATPopulation(); NEATInnovationList list = new NEATInnovationList { Population = population }; population.Innovations = list; EncogReadHelper helper = new EncogReadHelper(mask0); IDictionary<int, ISpecies> dictionary = new Dictionary<int, ISpecies>(); goto Label_0BD6; Label_0023: if ((section = helper.ReadNextSection()) != null) { if (!section.SectionName.Equals("NEAT-POPULATION")) { goto Label_085C; } if (section.SubSectionName.Equals("INNOVATIONS")) { using (IEnumerator<string> enumerator = section.Lines.GetEnumerator()) { string str; IList<string> list2; NEATInnovation innovation; NEATInnovation innovation2; goto Label_0A6C; Label_0A43: innovation = innovation2; if ((((uint) num2) - ((uint) num)) <= uint.MaxValue) { } population.Innovations.Add(innovation); Label_0A6C: if (enumerator.MoveNext()) { goto Label_0B54; } goto Label_0AD7; Label_0A7A: innovation2.SplitY = CSVFormat.EgFormat.Parse(list2[4]); innovation2.NeuronID = int.Parse(list2[5]); innovation2.FromNeuronID = int.Parse(list2[6]); innovation2.ToNeuronID = int.Parse(list2[7]); goto Label_0A43; Label_0AD7: if ((((uint) num2) - ((uint) num)) >= 0) { goto Label_0023; } Label_0AEF: innovation2 = new NEATInnovation(); innovation2.InnovationID = int.Parse(list2[0]); innovation2.InnovationType = StringToInnovationType(list2[1]); innovation2.NeuronType = StringToNeuronType(list2[2]); innovation2.SplitX = CSVFormat.EgFormat.Parse(list2[3]); if (0 == 0) { goto Label_0A7A; } goto Label_0023; Label_0B54: str = enumerator.Current; do { list2 = EncogFileSection.SplitColumns(str); } while (0 != 0); goto Label_0AEF; } } if (((uint) num) <= uint.MaxValue) { goto Label_085C; } goto Label_030B; } using (IEnumerator<IGenome> enumerator4 = population.Genomes.GetEnumerator()) { IGenome genome3; NEATGenome genome4; ISpecies species3; Label_0040: if (enumerator4.MoveNext()) { goto Label_00D6; } goto Label_0102; Label_0051: genome4.OutputCount = population.OutputCount; if ((((uint) num) - ((uint) num2)) >= 0) { goto Label_0040; } Label_007D: if (dictionary.ContainsKey(num)) { goto Label_00BA; } Label_0087: genome4.InputCount = population.InputCount; goto Label_0051; Label_0096: num = (int) genome4.SpeciesID; if ((((uint) num2) + ((uint) num2)) <= uint.MaxValue) { } goto Label_007D; Label_00BA: species3 = dictionary[num]; species3.Members.Add(genome4); goto Label_0087; Label_00D6: genome3 = enumerator4.Current; genome4 = (NEATGenome) genome3; goto Label_0096; } Label_0102: using (IEnumerator<ISpecies> enumerator5 = dictionary2.Keys.GetEnumerator()) { ISpecies current; goto Label_011F; Label_0112: ((BasicSpecies) current).Population = population; Label_011F: if (enumerator5.MoveNext()) { current = enumerator5.Current; num2 = dictionary2[current]; do { IGenome genome5 = dictionary3[num2]; current.Leader = genome5; } while (-1 == 0); goto Label_0112; } return population; } Label_016E: population.YoungScoreBonus = EncogFileSection.ParseDouble(dictionary4, "youngAgeBonus"); population.GenomeIDGenerate.CurrentID = EncogFileSection.ParseInt(dictionary4, "nextGenomeID"); population.InnovationIDGenerate.CurrentID = EncogFileSection.ParseInt(dictionary4, "nextInnovationID"); population.GeneIDGenerate.CurrentID = EncogFileSection.ParseInt(dictionary4, "nextGeneID"); population.SpeciesIDGenerate.CurrentID = EncogFileSection.ParseInt(dictionary4, "nextSpeciesID"); goto Label_0023; Label_0201: population.SurvivalRate = EncogFileSection.ParseDouble(dictionary4, "survivalRate"); if (0 != 0) { goto Label_03AA; } goto Label_02E9; Label_0242: population.OldAgePenalty = EncogFileSection.ParseDouble(dictionary4, "oldAgePenalty"); population.OldAgeThreshold = EncogFileSection.ParseInt(dictionary4, "oldAgeThreshold"); Label_0266: population.PopulationSize = EncogFileSection.ParseInt(dictionary4, "populationSize"); if (((uint) num2) <= uint.MaxValue) { if ((((uint) num2) - ((uint) num2)) >= 0) { if ((((uint) num) + ((uint) num2)) > uint.MaxValue) { goto Label_0242; } goto Label_0201; } goto Label_02E9; } Label_028A: population.OutputActivationFunction = EncogFileSection.ParseActivationFunction(dictionary4, "outAct"); if ((((uint) num2) + ((uint) num2)) < 0) { goto Label_0BD6; } population.Snapshot = EncogFileSection.ParseBoolean(dictionary4, "snapshot"); population.InputCount = EncogFileSection.ParseInt(dictionary4, "inputCount"); population.OutputCount = EncogFileSection.ParseInt(dictionary4, "outputCount"); goto Label_0242; Label_02E9: if ((((uint) num2) - ((uint) num)) >= 0) { population.YoungBonusAgeThreshhold = EncogFileSection.ParseInt(dictionary4, "youngAgeThreshold"); } if (0xff != 0) { goto Label_016E; } Label_030B: population.NeatActivationFunction = EncogFileSection.ParseActivationFunction(dictionary4, "neatAct"); goto Label_028A; Label_03AA: if (!section.SectionName.Equals("NEAT-POPULATION") || !section.SubSectionName.Equals("CONFIG")) { goto Label_0023; } if ((((uint) num2) + ((uint) num2)) <= uint.MaxValue) { if ((((uint) num2) + ((uint) num2)) <= uint.MaxValue) { dictionary4 = section.ParseParams(); } goto Label_030B; } Label_0821: if (section.SectionName.Equals("NEAT-POPULATION")) { if (section.SubSectionName.Equals("GENOMES")) { NEATGenome genome = null; using (IEnumerator<string> enumerator3 = section.Lines.GetEnumerator()) { string str3; IList<string> list3; NEATGenome genome2; NEATNeuronGene gene; NEATNeuronGene gene2; NEATLinkGene gene3; goto Label_0402; Label_03DA: genome.Links.Add(gene3); goto Label_0402; Label_03EA: if (list3[0].Equals("l", StringComparison.InvariantCultureIgnoreCase)) { goto Label_04EA; } Label_0402: if (enumerator3.MoveNext()) { goto Label_0770; } if ((((uint) num2) | 0x80000000) != 0) { goto Label_0023; } Label_0429: gene3.Enabled = int.Parse(list3[2]) > 0; Label_0440: gene3.Recurrent = int.Parse(list3[3]) > 0; gene3.FromNeuronID = int.Parse(list3[4]); gene3.ToNeuronID = int.Parse(list3[5]); gene3.Weight = CSVFormat.EgFormat.Parse(list3[6]); gene3.InnovationId = int.Parse(list3[7]); goto Label_03DA; Label_04B4: if ((((uint) num) & 0) != 0) { goto Label_03DA; } gene3.Id = int.Parse(list3[1]); goto Label_0429; Label_04EA: gene3 = new NEATLinkGene(); goto Label_04B4; Label_04F6: gene2.SplitY = CSVFormat.EgFormat.Parse(list3[7]); Label_050F: gene = gene2; genome.Neurons.Add(gene); if ((((uint) num2) & 0) != 0) { goto Label_0782; } goto Label_0402; Label_053D: gene2.Enabled = int.Parse(list3[3]) > 0; gene2.InnovationId = int.Parse(list3[4]); if (0xff == 0) { goto Label_050F; } gene2.ActivationResponse = CSVFormat.EgFormat.Parse(list3[5]); gene2.SplitX = CSVFormat.EgFormat.Parse(list3[6]); goto Label_04F6; Label_05A7: gene2.Id = int.Parse(list3[1]); gene2.NeuronType = StringToNeuronType(list3[2]); goto Label_053D; Label_05DA: if (!list3[0].Equals("n", StringComparison.InvariantCultureIgnoreCase)) { goto Label_03EA; } if (3 == 0) { goto Label_0440; } gene2 = new NEATNeuronGene(); goto Label_07C8; Label_0608: population.Add(genome); dictionary3[(int) genome.GenomeID] = genome; if (((uint) num) >= 0) { goto Label_0402; } goto Label_06B0; Label_0638: genome.AmountToSpawn = CSVFormat.EgFormat.Parse(list3[4]); genome.NetworkDepth = int.Parse(list3[5]); if ((((uint) num2) | 0x80000000) == 0) { goto Label_07AD; } if (((uint) num2) < 0) { goto Label_0023; } genome.Score = CSVFormat.EgFormat.Parse(list3[6]); goto Label_06F8; Label_06B0: genome.GenomeID = int.Parse(list3[1]); genome.SpeciesID = int.Parse(list3[2]); genome.AdjustedScore = CSVFormat.EgFormat.Parse(list3[3]); goto Label_0638; Label_06F8: if (3 != 0) { goto Label_0608; } goto Label_0402; Label_0704: genome.Chromosomes.Add(genome.LinksChromosome); if ((((uint) num2) + ((uint) num2)) >= 0) { goto Label_06B0; } goto Label_0402; Label_0737: if (8 == 0) { goto Label_050F; } genome = genome2; genome.Chromosomes.Add(genome.NeuronsChromosome); if ((((uint) num) - ((uint) num2)) <= uint.MaxValue) { goto Label_0704; } Label_0770: str3 = enumerator3.Current; list3 = EncogFileSection.SplitColumns(str3); Label_0782: if (!list3[0].Equals("g", StringComparison.InvariantCultureIgnoreCase)) { goto Label_05DA; } genome2 = new NEATGenome { NeuronsChromosome = new Chromosome() }; Label_07AD: genome2.LinksChromosome = new Chromosome(); goto Label_0737; Label_07C8: if ((((uint) num2) - ((uint) num)) <= uint.MaxValue) { goto Label_05A7; } if (0 == 0) { goto Label_04EA; } goto Label_04B4; } } if ((((uint) num2) < 0) || ((((uint) num2) - ((uint) num2)) > uint.MaxValue)) { goto Label_030B; } } goto Label_03AA; Label_085C: if (section.SectionName.Equals("NEAT-POPULATION")) { if ((((uint) num) | 1) == 0) { goto Label_0266; } if (section.SubSectionName.Equals("SPECIES")) { if ((((uint) num) | 8) == 0) { goto Label_0201; } using (IEnumerator<string> enumerator2 = section.Lines.GetEnumerator()) { string str2; string[] strArray; BasicSpecies species; BasicSpecies species2; goto Label_0913; Label_08CB: species = species2; species.SpawnsRequired = CSVFormat.EgFormat.Parse(strArray[5]); dictionary2[species] = int.Parse(strArray[6]); population.Species.Add(species); dictionary[(int) species.SpeciesID] = species; Label_0913: if (enumerator2.MoveNext()) { goto Label_09D6; } goto Label_0023; Label_0924: if ((((uint) num2) & 0) != 0) { goto Label_0969; } if (2 == 0) { goto Label_0924; } goto Label_09BE; Label_0941: species2 = new BasicSpecies(); species2.SpeciesID = int.Parse(strArray[0]); species2.Age = int.Parse(strArray[1]); Label_0969: species2.BestScore = CSVFormat.EgFormat.Parse(strArray[2]); species2.GensNoImprovement = int.Parse(strArray[3]); species2.SpawnsRequired = CSVFormat.EgFormat.Parse(strArray[4]); if ((((uint) num) + ((uint) num2)) >= 0) { goto Label_0924; } Label_09BE: if ((((uint) num2) + ((uint) num)) <= uint.MaxValue) { goto Label_09FD; } Label_09D6: str2 = enumerator2.Current; strArray = str2.Split(new char[] { ',' }); goto Label_0941; Label_09FD: if ((((uint) num) - ((uint) num)) <= uint.MaxValue) { goto Label_08CB; } goto Label_0023; } } } goto Label_0821; Label_0BD6: dictionary2 = new Dictionary<ISpecies, int>(); dictionary3 = new Dictionary<int, IGenome>(); goto Label_0023; }
private void trainNetworkBackprop() { // IMLTrain train = new Backpropagation(this.network, this.input,this.ideal, 0.000001, 0.1); IMLDataSet aset = new BasicMLDataSet(input, ideal); int epoch = 1; // train the neural network ICalculateScore score = new TrainingSetScore(aset); IMLTrain trainAlt = new NeuralSimulatedAnnealing(network, score, 10, 2, 100); IMLTrain trainMain = new Backpropagation(network, aset, 0.001, 0.0); StopTrainingStrategy stop = new StopTrainingStrategy(); var pop = new NEATPopulation(INPUT_SIZE, OUTPUT_SIZE, 1000); // train the neural network var step = new ActivationStep(); step.Center = 0.5; pop.OutputActivationFunction = step; var train = new NEATTraining(score, pop); trainMain.AddStrategy(new Greedy()); trainMain.AddStrategy(new HybridStrategy(trainAlt)); trainMain.AddStrategy(stop); trainMain.AddStrategy(new HybridStrategy(train)); network.ClearContext(); while (!stop.ShouldStop()) { trainMain.Iteration(); train.Iteration(); Console.WriteLine(@"Training " + @"Epoch #" + epoch + @" Error:" + trainMain.Error+ @" Genetic iteration:"+trainAlt.IterationNumber+ @"neat iteration:"+train.IterationNumber ); epoch++; } }
private void Validate(NEATPopulation pop) { Assert.AreEqual(10, pop.PopulationSize); Assert.AreEqual(0.2, pop.SurvivalRate); // see if the population can actually be used to train IMLDataSet trainingSet = new BasicMLDataSet(XOR.XORInput, XOR.XORIdeal); ICalculateScore score = new TrainingSetScore(trainingSet); IEvolutionaryAlgorithm train = NEATUtil.ConstructNEATTrainer(pop, score); train.Iteration(); }
/// <summary> /// Construct a random HyperNEAT genome. /// </summary> /// <param name="rnd">Random number generator.</param> /// <param name="pop">The target population.</param> /// <param name="inputCount">The input count.</param> /// <param name="outputCount">The output count.</param> /// <param name="connectionDensity">The connection densitoy, 1.0 for fully connected.</param> public HyperNEATGenome(EncogRandom rnd, NEATPopulation pop, int inputCount, int outputCount, double connectionDensity) : base(rnd, pop, inputCount, outputCount, connectionDensity) { }
/// <inheritdoc/> public IMLMethod Decode(NEATPopulation pop, Substrate.Substrate substrate, IGenome genome) { // obtain the CPPN NEATCODEC neatCodec = new NEATCODEC(); NEATNetwork cppn = (NEATNetwork)neatCodec.Decode(genome); List<NEATLink> linkList = new List<NEATLink>(); IActivationFunction[] afs = new IActivationFunction[substrate.NodeCount]; IActivationFunction af = new ActivationSteepenedSigmoid(); // all activation functions are the same for (int i = 0; i < afs.Length; i++) { afs[i] = af; } double c = this.MaxWeight / (1.0 - this.MinWeight); BasicMLData input = new BasicMLData(cppn.InputCount); // First create all of the non-bias links. foreach (SubstrateLink link in substrate.Links) { SubstrateNode source = link.Source; SubstrateNode target = link.Target; int index = 0; foreach (double d in source.Location) { input.Data[index++] = d; } foreach (double d in target.Location) { input.Data[index++] = d; } IMLData output = cppn.Compute(input); double weight = output[0]; if (Math.Abs(weight) > this.MinWeight) { weight = (Math.Abs(weight) - this.MinWeight) * c * Math.Sign(weight); linkList.Add(new NEATLink(source.ID, target.ID, weight)); } } // now create biased links input.Clear(); int d2 = substrate.Dimensions; IList<SubstrateNode> biasedNodes = substrate.GetBiasedNodes(); foreach (SubstrateNode target in biasedNodes) { for (int i = 0; i < d2; i++) { input.Data[d2 + i] = target.Location[i]; } IMLData output = cppn.Compute(input); double biasWeight = output[1]; if (Math.Abs(biasWeight) > this.MinWeight) { biasWeight = (Math.Abs(biasWeight) - this.MinWeight) * c * Math.Sign(biasWeight); linkList.Add(new NEATLink(0, target.ID, biasWeight)); } } // check for invalid neural network if (linkList.Count == 0) { return null; } linkList.Sort(); NEATNetwork network = new NEATNetwork(substrate.InputCount, substrate.OutputCount, linkList, afs); network.ActivationCycles = substrate.ActivationCycles; return network; }