/// <summary> /// See if the best genome has changed, and decode a new best network, if /// needed. /// </summary> private void UpdateBestNetwork() { if (BestGenome != _cachedBestGenome) { _cachedBestGenome = BestGenome; _bestNetwork = (NEATNetwork)CODEC.Decode(BestGenome); } }
/// <inheritdoc/> public IMLMethod Decode(IGenome genome) { var neatGenome = (NEATGenome)genome; var pop = (NEATPopulation)neatGenome.Population; IList <NEATNeuronGene> neuronsChromosome = neatGenome.NeuronsChromosome; IList <NEATLinkGene> linksChromosome = neatGenome.LinksChromosome; if (neuronsChromosome[0].NeuronType != NEATNeuronType.Bias) { throw new NeuralNetworkError( "The first neuron must be the bias neuron, this genome is invalid."); } var links = new List <NEATLink>(); var afs = new IActivationFunction[neuronsChromosome.Count]; for (int i = 0; i < afs.Length; i++) { afs[i] = neuronsChromosome[i].ActivationFunction; } IDictionary <long, int> lookup = new Dictionary <long, int>(); for (int i = 0; i < neuronsChromosome.Count; i++) { NEATNeuronGene neuronGene = neuronsChromosome[i]; lookup[neuronGene.Id] = i; } // loop over connections for (int i = 0; i < linksChromosome.Count; i++) { NEATLinkGene linkGene = linksChromosome[i]; if (linkGene.Enabled) { links.Add(new NEATLink(lookup[linkGene.FromNeuronId], lookup[linkGene.ToNeuronId], linkGene.Weight)); } } links.Sort(); NEATNetwork network = new NEATNetwork(neatGenome.InputCount, neatGenome.OutputCount, links, afs); network.ActivationCycles = pop.ActivationCycles; return(network); }
public virtual object Read(Stream mask0) { EncogReadHelper helper; EncogFileSection section; IDictionary<int, NEATNeuron> dictionary; IDictionary<string, string> dictionary2; double num2; double num3; int num5; int num6; bool flag; double num7; NEATNetwork network = new NEATNetwork(); if ((((uint) flag) + ((uint) num3)) <= uint.MaxValue) { if ((((uint) num6) + ((uint) num6)) < 0) { goto Label_03FB; } if ((((uint) flag) + ((uint) num3)) <= uint.MaxValue) { helper = new EncogReadHelper(mask0); if ((((uint) flag) & 0) == 0) { dictionary = new Dictionary<int, NEATNeuron>(); goto Label_0023; } } goto Label_03AA; } goto Label_0035; Label_0023: if ((section = helper.ReadNextSection()) != null) { goto Label_045C; } return network; Label_0035: if (section.SectionName.Equals("NEAT")) { while (section.SubSectionName.Equals("LINKS")) { using (IEnumerator<string> enumerator3 = section.Lines.GetEnumerator()) { string str3; IList<string> list2; NEATNeuron neuron3; NEATLink link; goto Label_007D; Label_005B: if ((((uint) num6) & 0) != 0) { goto Label_00D5; } neuron3.InboundLinks.Add(link); Label_007D: if (enumerator3.MoveNext()) { goto Label_00FA; } if ((((uint) num2) + ((uint) num3)) >= 0) { goto Label_0023; } Label_009E: num7 = CSVFormat.EgFormat.Parse(list2[3]); NEATNeuron fromNeuron = dictionary[num5]; neuron3 = dictionary[num6]; link = new NEATLink(num7, fromNeuron, neuron3, flag); Label_00D5: fromNeuron.OutputboundLinks.Add(link); if (((uint) num3) >= 0) { goto Label_005B; } goto Label_0023; Label_00FA: str3 = enumerator3.Current; list2 = EncogFileSection.SplitColumns(str3); num5 = int.Parse(list2[0]); if (-2 != 0) { } num6 = int.Parse(list2[1]); flag = int.Parse(list2[2]) > 0; goto Label_009E; } } if (((uint) num7) >= 0) { } } goto Label_0023; Label_03AA: if (!section.SectionName.Equals("NEAT") || !section.SubSectionName.Equals("NETWORK")) { if (section.SectionName.Equals("NEAT") && section.SubSectionName.Equals("NEURONS")) { using (IEnumerator<string> enumerator2 = section.Lines.GetEnumerator()) { string str2; IList<string> list; long num; NEATNeuronType type; NEATNeuron neuron; Label_01BD: if (enumerator2.MoveNext()) { goto Label_0257; } goto Label_0023; Label_01CE: if ((((uint) num) + ((uint) num5)) > uint.MaxValue) { goto Label_02A0; } network.Neurons.Add(neuron); dictionary[(int) num] = neuron; goto Label_02D0; Label_0206: num3 = CSVFormat.EgFormat.Parse(list[3]); double num4 = CSVFormat.EgFormat.Parse(list[4]); neuron = new NEATNeuron(type, num, num3, num4, num2); if ((((uint) num7) - ((uint) num)) <= uint.MaxValue) { goto Label_02B9; } Label_0257: str2 = enumerator2.Current; do { list = EncogFileSection.SplitColumns(str2); num = int.Parse(list[0]); type = PersistNEATPopulation.StringToNeuronType(list[1]); } while ((((uint) num) + ((uint) num5)) < 0); Label_02A0: num2 = CSVFormat.EgFormat.Parse(list[2]); goto Label_0206; Label_02B9: if ((((uint) num3) & 0) == 0) { goto Label_01CE; } Label_02D0: if ((((uint) num6) - ((uint) num4)) >= 0) { goto Label_01BD; } goto Label_0023; } } goto Label_0035; } IDictionary<string, string> paras = section.ParseParams(); network.InputCount = EncogFileSection.ParseInt(paras, "inputCount"); network.OutputCount = EncogFileSection.ParseInt(paras, "outputCount"); network.ActivationFunction = EncogFileSection.ParseActivationFunction(paras, "activationFunction"); if (((uint) num7) <= uint.MaxValue) { network.OutputActivationFunction = EncogFileSection.ParseActivationFunction(paras, "outAct"); if (((uint) num3) >= 0) { network.NetworkDepth = EncogFileSection.ParseInt(paras, "depth"); network.Snapshot = EncogFileSection.ParseBoolean(paras, "snapshot"); goto Label_0023; } goto Label_0035; } Label_03F3: dictionary2 = section.ParseParams(); Label_03FB: foreach (string str in dictionary2.Keys) { network.Properties.Add(str, dictionary2[str]); } goto Label_03AA; if (((((uint) flag) + ((uint) num2)) <= uint.MaxValue) && (((uint) flag) >= 0)) { goto Label_03AA; } Label_045C: if (section.SectionName.Equals("NEAT") && section.SubSectionName.Equals("PARAMS")) { goto Label_03F3; } goto Label_03AA; }
/// <summary> /// See if the best genome has changed, and decode a new best network, if /// needed. /// </summary> private void UpdateBestNetwork() { if (BestGenome != _cachedBestGenome) { _cachedBestGenome = BestGenome; _bestNetwork = (NEATNetwork)CODEC.Decode(BestGenome); } }
/// <summary> /// Read the object. /// </summary> /// <param name="mask0">The stream to read from.</param> /// <returns>The loaded object.</returns> public virtual Object Read(Stream mask0) { var result = new NEATNetwork(); var ins0 = new EncogReadHelper(mask0); EncogFileSection section; IDictionary<Int32, NEATNeuron> neuronMap = new Dictionary<Int32, NEATNeuron>(); while ((section = ins0.ReadNextSection()) != null) { if (section.SectionName.Equals("NEAT") && section.SubSectionName.Equals("PARAMS")) { IDictionary<String, String> paras = section.ParseParams(); foreach (String key in paras.Keys) { result.Properties.Add(key, paras[key]); } } if (section.SectionName.Equals("NEAT") && section.SubSectionName.Equals("NETWORK")) { IDictionary<String, String> p = section.ParseParams(); result.InputCount = EncogFileSection.ParseInt(p, PersistConst.InputCount); result.OutputCount = EncogFileSection.ParseInt(p, PersistConst.OutputCount); result.ActivationFunction = EncogFileSection .ParseActivationFunction(p, PersistConst.ActivationFunction); result.OutputActivationFunction = EncogFileSection .ParseActivationFunction(p, NEATPopulation.PropertyOutputActivation); result.NetworkDepth = EncogFileSection.ParseInt(p, PersistConst.Depth); result.Snapshot = EncogFileSection.ParseBoolean(p, PersistConst.Snapshot); } else if (section.SectionName.Equals("NEAT") && section.SubSectionName.Equals("NEURONS")) { foreach (String line in section.Lines) { IList<String> cols = EncogFileSection.SplitColumns(line); long neuronID = Int32.Parse(cols[0]); NEATNeuronType neuronType = PersistNEATPopulation .StringToNeuronType(cols[1]); double activationResponse = CSVFormat.EgFormat .Parse(cols[2]); double splitY = CSVFormat.EgFormat .Parse(cols[3]); double splitX = CSVFormat.EgFormat .Parse(cols[4]); var neatNeuron = new NEATNeuron(neuronType, neuronID, splitY, splitX, activationResponse); result.Neurons.Add(neatNeuron); neuronMap[((int) neuronID)] = (neatNeuron); } } else if (section.SectionName.Equals("NEAT") && section.SubSectionName.Equals("LINKS")) { foreach (String line in section.Lines) { IList<String> cols = EncogFileSection.SplitColumns(line); int fromID = Int32.Parse(cols[0]); int toID = Int32.Parse(cols[1]); bool recurrent = Int32.Parse(cols[2]) > 0; double weight = CSVFormat.EgFormat.Parse(cols[3]); NEATNeuron fromNeuron = (neuronMap[fromID]); NEATNeuron toNeuron = (neuronMap[toID]); var neatLink = new NEATLink(weight, fromNeuron, toNeuron, recurrent); fromNeuron.OutputboundLinks.Add(neatLink); toNeuron.InboundLinks.Add(neatLink); } } } return result; }
private NEATNetwork Create() { IList<NEATNeuron> neurons = new List<NEATNeuron>(); IActivationFunction afSigmoid = new ActivationSigmoid(); IActivationFunction afStep = new ActivationStep(); // create the neurons NEATNeuron input1 = new NEATNeuron( NEATNeuronType.Input, 1, 0.1, 0.2, 0.3); NEATNeuron input2 = new NEATNeuron( NEATNeuronType.Input, 2, 0.1, 0.2, 0.3); NEATNeuron bias = new NEATNeuron( NEATNeuronType.Bias, 3, 0.1, 0.2, 0.3); NEATNeuron hidden1 = new NEATNeuron( NEATNeuronType.Hidden, 4, 0.1, 0.2, 0.3); NEATNeuron output = new NEATNeuron( NEATNeuronType.Output, 5, 0.1, 0.2, 0.3); // add the neurons neurons.Add(input1); neurons.Add(input2); neurons.Add(hidden1); neurons.Add(bias); neurons.Add(output); // connect everything Link(0.01, input1, hidden1, false); Link(0.01, input2, hidden1, false); Link(0.01, bias, hidden1, false); Link(0.01, hidden1, output, false); // create the network NEATNetwork result = new NEATNetwork(2, 1, neurons, afSigmoid, afStep, 3); return result; }
public void SortAndRecord() { foreach (IGenome genome in base.Population.Genomes) { genome.Decode(); base.PerformCalculateScore(genome); } while (true) { base.Population.Sort(); do { IGenome best = base.Population.Best; double score = best.Score; if (base.Comparator.IsBetterThan(score, this.x411ff254761a2325)) { this.x411ff254761a2325 = score; this.xc98330799092ad50 = (NEATNetwork) best.Organism; } this.x411ff254761a2325 = base.Comparator.BestScore(this.Error, this.x411ff254761a2325); } while (0x7fffffff == 0); if (0 == 0) { return; } } }
private void Validate(NEATNetwork network) { Assert.AreEqual(1, network.OutputCount); Assert.AreEqual(2, network.InputCount); Assert.AreEqual(3, network.NetworkDepth); Assert.IsTrue(network.ActivationFunction is ActivationSigmoid); Assert.IsTrue(network.OutputActivationFunction is ActivationStep); Assert.AreEqual(0.0, ((ActivationStep)network.OutputActivationFunction).Center); Assert.AreEqual(1.0, ((ActivationStep)network.OutputActivationFunction).High); Assert.AreEqual(0.0, ((ActivationStep)network.OutputActivationFunction).Low); Assert.AreEqual(5, network.Neurons.Count); IDictionary<NEATNeuronType, NEATNeuron> neurons = new Dictionary<NEATNeuronType, NEATNeuron>(); foreach (NEATNeuron neuron in network.Neurons) { neurons[neuron.NeuronType] = neuron; } Assert.AreEqual(4, neurons.Count); NEATNeuron output = neurons[NEATNeuronType.Output]; NEATNeuron input = neurons[NEATNeuronType.Input]; Assert.AreEqual(1, input.OutputboundLinks.Count); Assert.AreEqual(0, input.InboundLinks.Count); Assert.AreEqual(0, output.OutputboundLinks.Count); Assert.AreEqual(1, output.InboundLinks.Count); }
/// <inheritdoc/> public IMLMethod Decode(IGenome genome) { var neatGenome = (NEATGenome)genome; var pop = (NEATPopulation)neatGenome.Population; IList<NEATNeuronGene> neuronsChromosome = neatGenome.NeuronsChromosome; IList<NEATLinkGene> linksChromosome = neatGenome.LinksChromosome; if (neuronsChromosome[0].NeuronType != NEATNeuronType.Bias) { throw new NeuralNetworkError( "The first neuron must be the bias neuron, this genome is invalid."); } var links = new List<NEATLink>(); var afs = new IActivationFunction[neuronsChromosome.Count]; for (int i = 0; i < afs.Length; i++) { afs[i] = neuronsChromosome[i].ActivationFunction; } IDictionary<long, int> lookup = new Dictionary<long, int>(); for (int i = 0; i < neuronsChromosome.Count; i++) { NEATNeuronGene neuronGene = neuronsChromosome[i]; lookup[neuronGene.Id] = i; } // loop over connections for (int i = 0; i < linksChromosome.Count; i++) { NEATLinkGene linkGene = linksChromosome[i]; if (linkGene.Enabled) { links.Add(new NEATLink(lookup[linkGene.FromNeuronId], lookup[linkGene.ToNeuronId], linkGene.Weight)); } } links.Sort(); NEATNetwork network = new NEATNetwork(neatGenome.InputCount, neatGenome.OutputCount, links, afs); network.ActivationCycles = pop.ActivationCycles; return network; }
/// <summary> /// Read the object. /// </summary> /// <param name="mask0">The stream to read from.</param> /// <returns>The loaded object.</returns> public virtual Object Read(Stream mask0) { var result = new NEATNetwork(); var ins0 = new EncogReadHelper(mask0); EncogFileSection section; IDictionary <Int32, NEATNeuron> neuronMap = new Dictionary <Int32, NEATNeuron>(); while ((section = ins0.ReadNextSection()) != null) { if (section.SectionName.Equals("NEAT") && section.SubSectionName.Equals("PARAMS")) { IDictionary <String, String> paras = section.ParseParams(); foreach (String key in paras.Keys) { result.Properties.Add(key, paras[key]); } } if (section.SectionName.Equals("NEAT") && section.SubSectionName.Equals("NETWORK")) { IDictionary <String, String> p = section.ParseParams(); result.InputCount = EncogFileSection.ParseInt(p, PersistConst.InputCount); result.OutputCount = EncogFileSection.ParseInt(p, PersistConst.OutputCount); result.ActivationFunction = EncogFileSection .ParseActivationFunction(p, PersistConst.ActivationFunction); result.OutputActivationFunction = EncogFileSection .ParseActivationFunction(p, NEATPopulation.PropertyOutputActivation); result.NetworkDepth = EncogFileSection.ParseInt(p, PersistConst.Depth); result.Snapshot = EncogFileSection.ParseBoolean(p, PersistConst.Snapshot); } else if (section.SectionName.Equals("NEAT") && section.SubSectionName.Equals("NEURONS")) { foreach (String line in section.Lines) { IList <String> cols = EncogFileSection.SplitColumns(line); long neuronID = Int32.Parse(cols[0]); NEATNeuronType neuronType = PersistNEATPopulation .StringToNeuronType(cols[1]); double activationResponse = CSVFormat.EgFormat .Parse(cols[2]); double splitY = CSVFormat.EgFormat .Parse(cols[3]); double splitX = CSVFormat.EgFormat .Parse(cols[4]); var neatNeuron = new NEATNeuron(neuronType, neuronID, splitY, splitX, activationResponse); result.Neurons.Add(neatNeuron); neuronMap[((int)neuronID)] = (neatNeuron); } } else if (section.SectionName.Equals("NEAT") && section.SubSectionName.Equals("LINKS")) { foreach (String line in section.Lines) { IList <String> cols = EncogFileSection.SplitColumns(line); int fromID = Int32.Parse(cols[0]); int toID = Int32.Parse(cols[1]); bool recurrent = Int32.Parse(cols[2]) > 0; double weight = CSVFormat.EgFormat.Parse(cols[3]); NEATNeuron fromNeuron = (neuronMap[fromID]); NEATNeuron toNeuron = (neuronMap[toID]); var neatLink = new NEATLink(weight, fromNeuron, toNeuron, recurrent); fromNeuron.OutputboundLinks.Add(neatLink); toNeuron.InboundLinks.Add(neatLink); } } } 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; }
public override void Decode() { int elementPos; NEATNetwork network; NEATPopulation population = (NEATPopulation) base.Population; IList<NEATNeuron> neurons = new List<NEATNeuron>(); if ((((uint) elementPos) & 0) == 0) { foreach (IGene gene in this.Neurons.Genes) { NEATNeuronGene gene2 = (NEATNeuronGene) gene; NEATNeuron item = new NEATNeuron(gene2.NeuronType, gene2.Id, gene2.SplitY, gene2.SplitX, gene2.ActivationResponse); do { neurons.Add(item); } while (((uint) elementPos) < 0); } } using (List<IGene>.Enumerator enumerator2 = this.Links.Genes.GetEnumerator()) { IGene gene3; NEATLinkGene gene4; NEATNeuron neuron2; NEATNeuron neuron3; NEATLink link; goto Label_00D5; Label_00A0: link = new NEATLink(gene4.Weight, neuron2, neuron3, gene4.Recurrent); neuron2.OutputboundLinks.Add(link); neuron3.InboundLinks.Add(link); Label_00D5: if (enumerator2.MoveNext()) { goto Label_0159; } goto Label_01CA; Label_00E3: Console.Out.WriteLine("test"); goto Label_0111; Label_00F4: if (elementPos == -1) { goto Label_00E3; } if ((((uint) elementPos) | 15) == 0) { goto Label_0147; } Label_0111: neuron3 = neurons[elementPos]; if ((((uint) elementPos) - ((uint) elementPos)) > uint.MaxValue) { goto Label_00F4; } goto Label_00A0; Label_013D: neuron2 = neurons[elementPos]; Label_0147: elementPos = this.GetElementPos((long) gene4.ToNeuronID); goto Label_00F4; Label_0159: gene3 = enumerator2.Current; gene4 = (NEATLinkGene) gene3; if (!gene4.Enabled) { goto Label_00D5; } elementPos = this.GetElementPos((long) gene4.FromNeuronID); if ((((uint) elementPos) - ((uint) elementPos)) <= uint.MaxValue) { goto Label_013D; } if ((((uint) elementPos) - ((uint) elementPos)) >= 0) { goto Label_00E3; } } Label_01CA: network = new NEATNetwork(this.inputCount, this.outputCount, neurons, population.NeatActivationFunction, population.OutputActivationFunction, 0); network.Snapshot = population.Snapshot; base.Organism = network; }