static public INetwork DecodeToConcurrentNetwork(NeatGenome.NeatGenome g, IActivationFunction activationFn) { //----- Loop the neuronGenes. Create Neuron for each one. // Store a table of neurons keyed by their id. Hashtable neuronTable = new Hashtable(g.NeuronGeneList.Count); NeuronList neuronList = new NeuronList(); foreach (NeuronGene neuronGene in g.NeuronGeneList) { Neuron newNeuron = new Neuron(activationFn, neuronGene.NeuronType, neuronGene.InnovationId); neuronTable.Add(newNeuron.Id, newNeuron); neuronList.Add(newNeuron); } //----- Loop the connection genes. Create a Connection for each one and bind them to the relevant Neurons. foreach (ConnectionGene connectionGene in g.ConnectionGeneList) { Connection newConnection = new Connection(connectionGene.SourceNeuronId, connectionGene.TargetNeuronId, connectionGene.Weight); // Bind the connection to it's source neuron. newConnection.SetSourceNeuron((Neuron)neuronTable[connectionGene.SourceNeuronId]); // Store the new connection against it's target neuron. ((Neuron)(neuronTable[connectionGene.TargetNeuronId])).ConnectionList.Add(newConnection); } return(new ConcurrentNetwork(neuronList)); }
static public INetwork DecodeToConcurrentNetwork(NeatGenome.NeatGenome g, IActivationFunction activationFn) { //----- Loop the neuronGenes. Create Neuron for each one. // Store a table of neurons keyed by their id. Hashtable neuronTable = new Hashtable(g.NeuronGeneList.Count); NeuronList neuronList = new NeuronList(); foreach(NeuronGene neuronGene in g.NeuronGeneList) { Neuron newNeuron = new Neuron(activationFn, neuronGene.NeuronType, neuronGene.InnovationId); neuronTable.Add(newNeuron.Id, newNeuron); neuronList.Add(newNeuron); } //----- Loop the connection genes. Create a Connection for each one and bind them to the relevant Neurons. foreach(ConnectionGene connectionGene in g.ConnectionGeneList) { Connection newConnection = new Connection(connectionGene.SourceNeuronId, connectionGene.TargetNeuronId, connectionGene.Weight); // Bind the connection to it's source neuron. newConnection.SetSourceNeuron((Neuron)neuronTable[connectionGene.SourceNeuronId]); // Store the new connection against it's target neuron. ((Neuron)(neuronTable[connectionGene.TargetNeuronId])).ConnectionList.Add(newConnection); } return new ConcurrentNetwork(neuronList); }
public void CreatedNeuronList() { NeuronList list = new NeuronList(42); Assert.IsNotNull(list.Neurons); Assert.AreEqual(42, list.Neurons.Count); for (int k = 0; k < 42; k++) { Assert.IsNotNull(list.Neurons[k]); } }
public void CreateWeightedValue() { NeuronList list = new NeuronList(42); var result = list.CreateWeightedValue(); Assert.IsNotNull(result); Assert.IsNotNull(result.Inputs); Assert.AreEqual(42, result.Inputs.Count); for (int k = 0; k < 42; k++) { Assert.AreSame(list.Neurons[k], result.Inputs[k]); Assert.IsTrue(result.Weights[k] >= -1.0); Assert.IsTrue(result.Weights[k] <= 1.0); Assert.IsTrue(result.Weights.Any(x => x != 0)); } }
/// <summary> /// Accepts a list of interconnected neurons that describe the network and loads them into this class instance /// so that the network can be run. This primarily means placing input and output nodes into their own Lists /// for use during the run. /// </summary> /// <param name="neuronList"></param> private void LoadNeuronList(NeuronList neuronList) { masterNeuronList = neuronList; int loopBound = masterNeuronList.Count; for (int j = 0; j < loopBound; j++) { Neuron neuron = masterNeuronList[j]; switch (neuron.NeuronType) { case NeuronType.Input: inputNeuronList.Add(neuron); break; case NeuronType.Output: outputNeuronList.Add(neuron); break; } } }
public AbstractNetwork(NeuronList neuronList) { inputNeuronList = new NeuronList(); outputNeuronList = new NeuronList(); LoadNeuronList(neuronList); }
public ConcurrentNetwork(NeuronList neuronList) : base(neuronList) { }
private static ConcurrentNetwork ReadNetwork(XmlElement xmlNetwork) { //--- Read the activation function id. string activationFnId = XmlUtilities.GetAttributeValue(xmlNetwork, "activation-fn-id"); IActivationFunction activationFn = ActivationFunctionFactory.GetActivationFunction(activationFnId); // Read the neurons into a list and also into a table keyed on id. Hashtable neuronTable = new Hashtable(); NeuronList biasNeuronList = new NeuronList(); NeuronList inputNeuronList = new NeuronList(); NeuronList hiddenNeuronList = new NeuronList(); NeuronList outputNeuronList = new NeuronList(); NeuronList masterNeuronList = new NeuronList(); XmlNodeList listNeurons = xmlNetwork.SelectNodes("neurons/neuron"); foreach (XmlElement xmlNeuron in listNeurons) { Neuron neuron = ReadNeuron(xmlNeuron); neuronTable.Add(neuron.Id, neuron); switch (neuron.NeuronType) { case NeuronType.Bias: biasNeuronList.Add(neuron); break; case NeuronType.Input: inputNeuronList.Add(neuron); break; case NeuronType.Hidden: hiddenNeuronList.Add(neuron); break; case NeuronType.Output: outputNeuronList.Add(neuron); break; } } //----- Build a master list of neurons. Neurons must be ordered by type - bias,input,hidden,output. if (biasNeuronList.Count != 1) { throw new SharpNeatLib.Xml.XmlException("Neural Network XML must contain exactly 1 bias node."); } foreach (Neuron neuron in biasNeuronList) { masterNeuronList.Add(neuron); } foreach (Neuron neuron in inputNeuronList) { masterNeuronList.Add(neuron); } foreach (Neuron neuron in hiddenNeuronList) { masterNeuronList.Add(neuron); } foreach (Neuron neuron in outputNeuronList) { masterNeuronList.Add(neuron); } //----- Read Connections and store against target neurons. XmlNodeList listConnections = xmlNetwork.SelectNodes("connections/connection"); foreach (XmlElement xmlConnection in listConnections) { Connection connection = ReadConnection(xmlConnection); // Store the connection with it's target neuron. ((Neuron)neuronTable[connection.TargetNeuronId]).ConnectionList.Add(connection); // Bind the connection to it's source neuron. connection.SetSourceNeuron((Neuron)neuronTable[connection.SourceNeuronId]); } return(new ConcurrentNetwork(masterNeuronList)); }
/// <summary> /// Accepts a list of interconnected neurons that describe the network and loads them into this class instance /// so that the network can be run. This primarily means placing input and output nodes into their own Lists /// for use during the run. /// </summary> /// <param name="neuronList"></param> private void LoadNeuronList(NeuronList neuronList) { masterNeuronList = neuronList; int loopBound = masterNeuronList.Count; for(int j=0; j<loopBound; j++) { Neuron neuron = masterNeuronList[j]; switch(neuron.NeuronType) { case NeuronType.Input: inputNeuronList.Add(neuron); break; case NeuronType.Output: outputNeuronList.Add(neuron); break; } } }
private static ConcurrentNetwork ReadNetwork(XmlElement xmlNetwork) { //--- Read the activation function id. string activationFnId = XmlUtilities.GetAttributeValue(xmlNetwork, "activation-fn-id"); IActivationFunction activationFn = ActivationFunctionFactory.GetActivationFunction(activationFnId); // Read the neurons into a list and also into a table keyed on id. Hashtable neuronTable = new Hashtable(); NeuronList biasNeuronList = new NeuronList(); NeuronList inputNeuronList = new NeuronList(); NeuronList hiddenNeuronList = new NeuronList(); NeuronList outputNeuronList = new NeuronList(); NeuronList masterNeuronList = new NeuronList(); XmlNodeList listNeurons = xmlNetwork.SelectNodes("neurons/neuron"); foreach(XmlElement xmlNeuron in listNeurons) { Neuron neuron = ReadNeuron(xmlNeuron); neuronTable.Add(neuron.Id, neuron); switch(neuron.NeuronType) { case NeuronType.Bias: biasNeuronList.Add(neuron); break; case NeuronType.Input: inputNeuronList.Add(neuron); break; case NeuronType.Hidden: hiddenNeuronList.Add(neuron); break; case NeuronType.Output: outputNeuronList.Add(neuron); break; } } //----- Build a master list of neurons. Neurons must be ordered by type - bias,input,hidden,output. if(biasNeuronList.Count != 1) throw new SharpNeatLib.Xml.XmlException("Neural Network XML must contain exactly 1 bias node."); foreach(Neuron neuron in biasNeuronList) masterNeuronList.Add(neuron); foreach(Neuron neuron in inputNeuronList) masterNeuronList.Add(neuron); foreach(Neuron neuron in hiddenNeuronList) masterNeuronList.Add(neuron); foreach(Neuron neuron in outputNeuronList) masterNeuronList.Add(neuron); //----- Read Connections and store against target neurons. XmlNodeList listConnections = xmlNetwork.SelectNodes("connections/connection"); foreach(XmlElement xmlConnection in listConnections) { Connection connection = ReadConnection(xmlConnection); // Store the connection with it's target neuron. ((Neuron)neuronTable[connection.TargetNeuronId]).ConnectionList.Add(connection); // Bind the connection to it's source neuron. connection.SetSourceNeuron((Neuron)neuronTable[connection.SourceNeuronId]); } return new ConcurrentNetwork(masterNeuronList); }