public CTRNN(int biasNeuronCount, int inputNeuronCount, int outputNeuronCount, int totalNeuronCount, FloatFastConnection[] connections, float[] biasList, IActivationFunction[] activationFunctions, ModulePacket[] modules, float[] timeConstArray) : base(biasNeuronCount,inputNeuronCount,outputNeuronCount,totalNeuronCount,connections, biasList,activationFunctions, modules) { timeConstantArray = timeConstArray; activeSumsArray = new float[timeConstantArray.Length]; oldActivation = new float[timeConstantArray.Length]; }
static public ModularNetwork DecodeToCTRNN(NeatGenome.NeatGenome g) { int inputCount = g.InputNeuronCount; int outputCount = g.OutputNeuronCount; int neuronCount = g.NeuronGeneList.Count; IActivationFunction[] activationFunctions = new IActivationFunction[neuronCount]; float[] biasList = new float[neuronCount]; float[] timeConst = new float[neuronCount]; Dictionary<uint, int> neuronLookup = new Dictionary<uint, int>(neuronCount); // Create an array of the activation functions for each non-module node node in the genome. // Start with a bias node if there is one in the genome. // The genome's neuron list is assumed to be ordered by type, with the bias node appearing first. int neuronGeneIndex = 0; for (; neuronGeneIndex < neuronCount; neuronGeneIndex++) { if (g.NeuronGeneList[neuronGeneIndex].NeuronType != NeuronType.Bias) break; activationFunctions[neuronGeneIndex] = g.NeuronGeneList[neuronGeneIndex].ActivationFunction; neuronLookup.Add(g.NeuronGeneList[neuronGeneIndex].InnovationId, neuronGeneIndex); } int biasCount = neuronGeneIndex; for (; neuronGeneIndex < neuronCount; neuronGeneIndex++) { activationFunctions[neuronGeneIndex] = g.NeuronGeneList[neuronGeneIndex].ActivationFunction; neuronLookup.Add(g.NeuronGeneList[neuronGeneIndex].InnovationId, neuronGeneIndex); biasList[neuronGeneIndex] = g.NeuronGeneList[neuronGeneIndex].Bias; timeConst[neuronGeneIndex] = g.NeuronGeneList[neuronGeneIndex].TimeConstant; } // Create an array of the activation functions, inputs, and outputs for each module in the genome. ModulePacket[] modules = new ModulePacket[g.ModuleGeneList.Count]; for (int i = g.ModuleGeneList.Count - 1; i >= 0; i--) { modules[i].function = g.ModuleGeneList[i].Function; // Must translate input and output IDs to array locations. modules[i].inputLocations = new int[g.ModuleGeneList[i].InputIds.Count]; for (int j = g.ModuleGeneList[i].InputIds.Count - 1; j >= 0; j--) { modules[i].inputLocations[j] = neuronLookup[g.ModuleGeneList[i].InputIds[j]]; } modules[i].outputLocations = new int[g.ModuleGeneList[i].OutputIds.Count]; for (int j = g.ModuleGeneList[i].OutputIds.Count - 1; j >= 0; j--) { modules[i].outputLocations[j] = neuronLookup[g.ModuleGeneList[i].OutputIds[j]]; } } // ConnectionGenes point to a neuron's innovation ID. Translate this ID to the neuron's index in the neuron array. FloatFastConnection[] connections = new FloatFastConnection[g.ConnectionGeneList.Count]; for (int connectionGeneIndex = g.ConnectionGeneList.Count - 1; connectionGeneIndex >= 0; connectionGeneIndex--) { ConnectionGene connectionGene = g.ConnectionGeneList[connectionGeneIndex]; connections[connectionGeneIndex].sourceNeuronIdx = neuronLookup[connectionGene.SourceNeuronId]; connections[connectionGeneIndex].targetNeuronIdx = neuronLookup[connectionGene.TargetNeuronId]; connections[connectionGeneIndex].weight = (float)connectionGene.Weight; } CTRNN mn = new CTRNN(biasCount, inputCount, outputCount, neuronCount, connections, biasList, activationFunctions, modules,timeConst); mn.genome = g; return mn; }
static public ModularNetwork DecodeToModularNetwork(NeatGenome.NeatGenome g) { int inputCount = g.InputNeuronCount; int outputCount = g.OutputNeuronCount; int neuronCount = g.NeuronGeneList.Count; IActivationFunction[] activationFunctions = new IActivationFunction[neuronCount]; float[] biasList = new float[neuronCount]; Dictionary<uint, int> neuronLookup = new Dictionary<uint, int>(neuronCount); // Schrum: In case there are output neurons out of order g.NeuronGeneList.NeuronSortCheck(); // Create an array of the activation functions for each non-module node node in the genome. // Start with a bias node if there is one in the genome. // The genome's neuron list is assumed to be ordered by type, with the bias node appearing first. int neuronGeneIndex = 0; for (; neuronGeneIndex < neuronCount; neuronGeneIndex++) { if (g.NeuronGeneList[neuronGeneIndex].NeuronType != NeuronType.Bias) break; activationFunctions[neuronGeneIndex] = g.NeuronGeneList[neuronGeneIndex].ActivationFunction; neuronLookup.Add(g.NeuronGeneList[neuronGeneIndex].InnovationId, neuronGeneIndex); } int biasCount = neuronGeneIndex; // Schrum: debug //Console.WriteLine("start (after bias): " + g.GenomeId); // Schrum: Debugging //NeuronType expectedType = NeuronType.Input; for (; neuronGeneIndex < neuronCount; neuronGeneIndex++) { activationFunctions[neuronGeneIndex] = g.NeuronGeneList[neuronGeneIndex].ActivationFunction; // Schrum: Debug /* if (expectedType != g.NeuronGeneList[neuronGeneIndex].NeuronType) { if (expectedType == NeuronType.Input && g.NeuronGeneList[neuronGeneIndex].NeuronType == NeuronType.Output) { expectedType = NeuronType.Output; } else if (expectedType == NeuronType.Output && g.NeuronGeneList[neuronGeneIndex].NeuronType == NeuronType.Hidden) { expectedType = NeuronType.Hidden; } else { // Error condition: Console.WriteLine("Error with genome: " + g.GenomeId); XmlDocument doc = new XmlDocument(); XmlGenomeWriterStatic.Write(doc, (SharpNeatLib.NeatGenome.NeatGenome)g); FileInfo oFileInfo = new FileInfo("ProblemGenome.xml"); doc.Save(oFileInfo.FullName); Environment.Exit(1); } } */ neuronLookup.Add(g.NeuronGeneList[neuronGeneIndex].InnovationId, neuronGeneIndex); biasList[neuronGeneIndex] = g.NeuronGeneList[neuronGeneIndex].Bias; } // Create an array of the activation functions, inputs, and outputs for each module in the genome. ModulePacket[] modules = new ModulePacket[g.ModuleGeneList.Count]; for (int i = g.ModuleGeneList.Count - 1; i >= 0; i--) { modules[i].function = g.ModuleGeneList[i].Function; // Must translate input and output IDs to array locations. modules[i].inputLocations = new int[g.ModuleGeneList[i].InputIds.Count]; for (int j = g.ModuleGeneList[i].InputIds.Count - 1; j >= 0; j--) { modules[i].inputLocations[j] = neuronLookup[g.ModuleGeneList[i].InputIds[j]]; } modules[i].outputLocations = new int[g.ModuleGeneList[i].OutputIds.Count]; for (int j = g.ModuleGeneList[i].OutputIds.Count - 1; j >= 0; j--) { modules[i].outputLocations[j] = neuronLookup[g.ModuleGeneList[i].OutputIds[j]]; } } // ConnectionGenes point to a neuron's innovation ID. Translate this ID to the neuron's index in the neuron array. FloatFastConnection[] connections = new FloatFastConnection[g.ConnectionGeneList.Count]; for (int connectionGeneIndex = g.ConnectionGeneList.Count - 1; connectionGeneIndex >= 0; connectionGeneIndex--) { ConnectionGene connectionGene = g.ConnectionGeneList[connectionGeneIndex]; connections[connectionGeneIndex].sourceNeuronIdx = neuronLookup[connectionGene.SourceNeuronId]; connections[connectionGeneIndex].targetNeuronIdx = neuronLookup[connectionGene.TargetNeuronId]; connections[connectionGeneIndex].weight = (float)connectionGene.Weight; connections[connectionGeneIndex].learningRate = connectionGene.learningRate; connections[connectionGeneIndex].A = connectionGene.A; connections[connectionGeneIndex].B = connectionGene.B; connections[connectionGeneIndex].C = connectionGene.C; connections[connectionGeneIndex].D = connectionGene.D; connections[connectionGeneIndex].modConnection = connectionGene.modConnection; } ModularNetwork mn = new ModularNetwork(biasCount, inputCount, outputCount, g.OutputsPerPolicy, neuronCount, connections, biasList, activationFunctions, modules); if (g.networkAdaptable) mn.adaptable = true; if (g.networkModulatory) mn.modulatory = true; mn.genome = g; return mn; }
public ModularNetwork(int biasNeuronCount, int inputNeuronCount, int outputNeuronCount, int totalNeuronCount, FloatFastConnection[] connections, float[] biasList, IActivationFunction[] activationFunctions, ModulePacket[] modules) { this.biasNeuronCount = biasNeuronCount; this.inputNeuronCount = inputNeuronCount; this.totalInputNeuronCount = biasNeuronCount + inputNeuronCount; this.outputNeuronCount = outputNeuronCount; this.connections = connections; this.activationFunctions = activationFunctions; this.modules = modules; this.biasList = biasList; // Justin: Setup fields for recursive network activation calculated = new bool[totalNeuronCount]; dependencies = new List<int>[totalNeuronCount]; endNeurons = new List<int>(); for (int i = 0; i < totalNeuronCount; i++) { dependencies[i] = new List<int>(); calculated[i] = false; } // Calculate dependencies. We will temporarily use calculated[] to store whether or not neurons have dependent "children" (will re-initialize the array to false afterwards) for (int i = 0; i < connections.Length; i++) { calculated[connections[i].sourceNeuronIdx] = true; // Mark the source as having dependents dependencies[connections[i].targetNeuronIdx].Add(i); // Add the source as a dependency of the target } // Mark "ending neurons" by seeing which neurons have no dependents for (int i = 0; i < totalNeuronCount; i++) { if (calculated[i] == false) endNeurons.Add(i); calculated[i] = false; // Also, re-initialize calculated[] to false } //foreach (int i in endNeurons) Console.Write(i + " "); Console.WriteLine(); //Console.WriteLine("# Outputs: " + endNeurons.Count + " guessed: " + outputNeuronCount + " total: " + totalNeuronCount + " bias: " + biasNeuronCount); /*int counter = 0; for (int i = 0; i < dependencies.Length; i++) { if (dependencies[i].Count == 0) counter++; //foreach (int arr in dependencies[i]) Console.Write(arr + " "); //Console.WriteLine(); } Console.WriteLine("# Inputs: " + counter + " guessed: " + totalInputNeuronCount + " total: " + totalNeuronCount + " bias: " + biasNeuronCount); //*/ // Allocate the arrays that store the states at different points in the neural network. // The neuron signals are initialised to 0 by default. Only bias nodes need setting to 1. neuronSignals = new float[totalNeuronCount]; modSignals = new float[totalNeuronCount]; neuronSignalsBeingProcessed = new float[totalNeuronCount]; for (int i = 0; i < biasNeuronCount; i++) { neuronSignals[i] = 1.0F; } }
public ModularNetwork(int biasNeuronCount, int inputNeuronCount, int outputNeuronCount, int totalNeuronCount, FloatFastConnection[] connections, float[] biasList, IActivationFunction[] activationFunctions, ModulePacket[] modules) { this.biasNeuronCount = biasNeuronCount; this.inputNeuronCount = inputNeuronCount; this.totalInputNeuronCount = biasNeuronCount + inputNeuronCount; this.outputNeuronCount = outputNeuronCount; this.connections = connections; this.activationFunctions = activationFunctions; this.modules = modules; this.biasList = biasList; adaptable = false; modulatory = false; // Allocate the arrays that store the states at different points in the neural network. // The neuron signals are initialised to 0 by default. Only bias nodes need setting to 1. neuronSignals = new float[totalNeuronCount]; modSignals = new float[totalNeuronCount]; neuronSignalsBeingProcessed = new float[totalNeuronCount]; for (int i = 0; i < biasNeuronCount; i++) { neuronSignals[i] = 1.0F; } this.activated = new bool[TotalNeuronCount]; this.inActivation = new bool[TotalNeuronCount]; this.lastActivation = new float[TotalNeuronCount]; adjacentList = new List<int>[TotalNeuronCount]; reverseAdjacentList = new List<int>[TotalNeuronCount]; adjacentMatrix = new float[TotalNeuronCount, TotalNeuronCount]; for (int i = 0; i < TotalNeuronCount; i++) { this.activated[i] = this.inActivation[i] = false; this.lastActivation[i] = 0; this.adjacentList[i] = new List<int>(TotalNeuronCount); this.reverseAdjacentList[i] = new List<int>(TotalNeuronCount); // this.adjacentMatrix[i] = new List<float>(TotalNeuronCount); } // Set up adjacency list and matrix for (int i = 0; i < this.connections.Length; i++) { int crs = connections[i].sourceNeuronIdx; int crt = connections[i].targetNeuronIdx; // Holds outgoing nodes this.adjacentList[crs].Add(crt); // Holds incoming nodes this.reverseAdjacentList[crt].Add(crs); this.adjacentMatrix[crs, crt] = connections[i].weight; } }