/// <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); }