/// <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(); }
/// <summary> /// Construct an innovation list, that includes the initial innovations. /// </summary> /// <param name="population">The population to base this innovation list on.</param> public NEATInnovationList(NEATPopulation population) { Population = population; FindInnovation(Population.AssignGeneId()); // bias // input neurons for (int i = 0; i < Population.InputCount; i++) { FindInnovation(Population.AssignGeneId()); } // output neurons for (int i = 0; i < Population.OutputCount; i++) { FindInnovation(Population.AssignGeneId()); } // connections for (var fromId = 0; fromId < Population.InputCount + 1; fromId++) { for (var toId = 0; toId < Population.OutputCount; toId++) { FindInnovation(fromId, toId); } } }
/// <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); }
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++; } }
/// <inheritdoc/> public IMLMethod Decode(IGenome genome) { NEATPopulation pop = (NEATPopulation)genome.Population; Substrate.Substrate substrate = pop.CurrentSubstrate; return(Decode(pop, substrate, genome)); }
static void Main(string[] args) { // this form of ANN uses genetic algorithm to produce // hidden layer of neurons // A NEAT network starts with only an // input layer and output layer. The rest is evolved as the training progresses. // Connections inside of a NEAT neural network can be feedforward, recurrent, // or self - connected.All of these connection types will be tried by NEAT as it // attempts to evolve a neural network capable of the given task. 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); // TODO no persistance? no means to peek structure? // test the neural network Console.WriteLine(@"Neural Network Results:"); EncogUtility.Evaluate(network, trainingSet); }
public void TestPersistEG() { IPopulation pop = Generate(); EncogDirectoryPersistence.SaveObject((EG_FILENAME), pop); NEATPopulation pop2 = (NEATPopulation)EncogDirectoryPersistence.LoadObject((EG_FILENAME)); Validate(pop2); }
/// <inheritdoc/> public void MutateWeight(EncogRandom rnd, NEATLinkGene linkGene, double weightRange) { double delta = rnd.NextGaussian() * _sigma; double w = linkGene.Weight + delta; w = NEATPopulation.ClampWeight(w, weightRange); linkGene.Weight = w; }
public void TestPersistSerial() { NEATPopulation pop = Generate(); SerializeObject.Save(SERIAL_FILENAME.ToString(), pop); NEATPopulation pop2 = (NEATPopulation)SerializeObject.Load(SERIAL_FILENAME.ToString()); Validate(pop2); }
/// <summary> /// Create a new genome with the specified connection density. This /// constructor is typically used to create the initial population. /// </summary> /// <param name="rnd">Random number generator.</param> /// <param name="pop">The population.</param> /// <param name="inputCount">The input count.</param> /// <param name="outputCount">The output count.</param> /// <param name="connectionDensity">The connection density.</param> public NEATGenome(EncogRandom rnd, NEATPopulation pop, int inputCount, int outputCount, double connectionDensity) { AdjustedScore = 0; InputCount = inputCount; OutputCount = outputCount; // get the activation function IActivationFunction af = pop.ActivationFunctions.PickFirst(); // first bias int innovationId = 0; var biasGene = new NEATNeuronGene(NEATNeuronType.Bias, af, inputCount, innovationId++); _neuronsList.Add(biasGene); // then inputs for (var i = 0; i < inputCount; i++) { var gene = new NEATNeuronGene(NEATNeuronType.Input, af, i, innovationId++); _neuronsList.Add(gene); } // then outputs for (int i = 0; i < outputCount; i++) { var gene = new NEATNeuronGene(NEATNeuronType.Output, af, i + inputCount + 1, innovationId++); _neuronsList.Add(gene); } // and now links for (var i = 0; i < inputCount + 1; i++) { for (var j = 0; j < outputCount; j++) { // make sure we have at least one connection if (_linksList.Count < 1 || rnd.NextDouble() < connectionDensity) { long fromId = this._neuronsList[i].Id; long toId = this._neuronsList[inputCount + j + 1].Id; double w = RangeRandomizer.Randomize(rnd, -pop.WeightRange, pop.WeightRange); var gene = new NEATLinkGene(fromId, toId, true, innovationId++, w); _linksList.Add(gene); } } } }
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(); }
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(); }
/// <summary> /// Construct a neat trainer with a new population. The new population is /// created from the specified parameters. /// </summary> /// <param name="calculateScore">The score calculation object.</param> /// <param name="inputCount">The input neuron count.</param> /// <param name="outputCount">The output neuron count.</param> /// <param name="populationSize">The population size.</param> public NEATTraining(ICalculateScore calculateScore, int inputCount, int outputCount, int populationSize) { this.inputCount = inputCount; this.outputCount = outputCount; CalculateScore = new GeneticScoreAdapter(calculateScore); Comparator = new GenomeComparator(CalculateScore); Population = new NEATPopulation(inputCount, outputCount, populationSize); Init(); }
/// <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); }
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> /// The entry point for this example. If you would like to make this example /// stand alone, then add to its own project and rename to Main. /// </summary> /// <param name="args">Not used.</param> public static void ExampleMain(string[] args) { IMLDataSet trainingSet = new BasicMLDataSet(XORInput, XORIdeal); var 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 var train = NEATUtil.ConstructNEATTrainer(pop, score); EncogUtility.TrainToError(train, 0.01); var 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); }
/// <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 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); }
/// <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) { }