public NEATLink(double weight, NEATNeuron fromNeuron, NEATNeuron toNeuron, bool recurrent) { this._weight = weight; this._fromNeuron = fromNeuron; this._toNeuron = toNeuron; this._recurrent = recurrent; }
/// <summary> /// Construct a NEAT link. /// </summary> /// /// <param name="weight">The weight between the two neurons.</param> /// <param name="fromNeuron">The source neuron.</param> /// <param name="toNeuron">The target neuron.</param> /// <param name="recurrent">Is this a recurrent link.</param> public NEATLink(double weight, NEATNeuron fromNeuron, NEATNeuron toNeuron, bool recurrent) { _weight = weight; _fromNeuron = fromNeuron; _toNeuron = toNeuron; _recurrent = recurrent; }
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> /// Compute the output from this synapse. /// </summary> /// /// <param name="input">The input to this synapse.</param> /// <returns>The output from this synapse.</returns> public virtual IMLData Compute(IMLData input) { IMLData result = new BasicMLData(_outputCount); if (_neurons.Count == 0) { throw new NeuralNetworkError( "This network has not been evolved yet, it has no neurons in the NEAT synapse."); } int flushCount = 1; if (_snapshot) { flushCount = _networkDepth; } // iterate through the network FlushCount times for (int i = 0; i < flushCount; ++i) { int outputIndex = 0; int index = 0; result.Clear(); // populate the input neurons while (_neurons[index].NeuronType == NEATNeuronType.Input) { _neurons[index].Output = input[index]; index++; } // set the bias neuron _neurons[index++].Output = 1; while (index < _neurons.Count) { NEATNeuron currentNeuron = _neurons[index]; double sum = 0; foreach (NEATLink link in currentNeuron.InboundLinks) { double weight = link.Weight; double neuronOutput = link.FromNeuron.Output; sum += weight * neuronOutput; } var d = new double[1]; d[0] = sum / currentNeuron.ActivationResponse; _activationFunction.ActivationFunction(d, 0, d.Length); _neurons[index].Output = d[0]; if (currentNeuron.NeuronType == NEATNeuronType.Output) { result[outputIndex++] = currentNeuron.Output; } index++; } } _outputActivationFunction.ActivationFunction(result.Data, 0, result.Count); return(result); }
/// <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; }
private void Link( double weight, NEATNeuron from, NEATNeuron to, bool recurrent) { NEATLink l = new NEATLink(weight, from, to, recurrent); from.OutputboundLinks.Add(l); to.InboundLinks.Add(l); }
/// <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); }
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; }