public virtual IMLData Compute(IMLData input) { int num; int num2; int num3; int num4; double num5; double weight; double output; IMLData data = new BasicMLData(this._outputCount); goto Label_0271; Label_001B: num4++; Label_0021: if (num4 < this._neurons.Count) { NEATNeuron neuron = this._neurons[num4]; num5 = 0.0; foreach (NEATLink link in neuron.InboundLinks) { weight = link.Weight; do { output = link.FromNeuron.Output; } while ((((uint) weight) - ((uint) num2)) < 0); num5 += weight * output; } double[] d = new double[] { num5 / neuron.ActivationResponse }; this._activationFunction.ActivationFunction(d, 0, d.Length); this._neurons[num4].Output = d[0]; if (neuron.NeuronType == NEATNeuronType.Output) { data[num3++] = neuron.Output; if ((((uint) num2) - ((uint) num4)) < 0) { goto Label_0206; } if (-1 == 0) { goto Label_025D; } } goto Label_001B; } num2++; Label_0037: if (num2 < num) { num3 = 0; if ((((uint) weight) - ((uint) weight)) >= 0) { if (1 != 0) { num4 = 0; data.Clear(); while (this._neurons[num4].NeuronType == NEATNeuronType.Input) { this._neurons[num4].Output = input[num4]; num4++; } goto Label_01BB; } goto Label_001B; } if (((uint) output) <= uint.MaxValue) { goto Label_0271; } goto Label_0239; } Label_003E: this._outputActivationFunction.ActivationFunction(data.Data, 0, data.Count); return data; Label_01BB: this._neurons[num4++].Output = 1.0; if (((uint) output) <= uint.MaxValue) { goto Label_0021; } Label_0206: num2 = 0; goto Label_0037; Label_0239: num = this._networkDepth; if ((((uint) num5) | 0x7fffffff) == 0) { goto Label_003E; } goto Label_0206; Label_025D: if (this._neurons.Count == 0) { throw new NeuralNetworkError("This network has not been evolved yet, it has no neurons in the NEAT synapse."); } num = 1; if (this._snapshot) { goto Label_0239; } goto Label_0206; Label_0271: if (1 == 0) { goto Label_001B; } if (0 != 0) { goto Label_01BB; } goto Label_025D; }
/// <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; }
/// <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; }