private static void WriteConnection(XmlElement xmlConnections, ConnectionGene connectionGene) { XmlElement xmlConnection = XmlUtilities.AddElement(xmlConnections, "connection"); XmlUtilities.AddAttribute(xmlConnection, "src-id", connectionGene.SourceNeuronId.ToString() ); XmlUtilities.AddAttribute(xmlConnection, "tgt-id", connectionGene.TargetNeuronId.ToString()); XmlUtilities.AddAttribute(xmlConnection, "weight", connectionGene.Weight.ToString("R")); }
public NewNeuronGeneStruct( NeuronGene newNeuronGene, ConnectionGene newConnectionGene_Input, ConnectionGene newConnectionGene_Output) { this.NewNeuronGene = newNeuronGene; this.NewConnectionGene_Input = newConnectionGene_Input; this.NewConnectionGene_Output = newConnectionGene_Output; }
/// <summary> /// Copy constructor. /// </summary> /// <param name="copyFrom"></param> public ConnectionGene(ConnectionGene copyFrom) { this.innovationId = copyFrom.innovationId; this.sourceNeuronId = copyFrom.sourceNeuronId; this.targetNeuronId = copyFrom.targetNeuronId; // this.enabled = copyFrom.enabled; this.weight = copyFrom.weight; this.fixedWeight = copyFrom.fixedWeight; }
private void MutateConnectionWeight(ConnectionGene connectionGene, NeatParameters np, ConnectionMutationParameterGroup paramGroup) { switch(paramGroup.PerturbationType) { case ConnectionPerturbationType.JiggleEven: { connectionGene.Weight += (Utilities.NextDouble()*2-1.0) * paramGroup.PerturbationFactor; // Cap the connection weight. Large connections weights reduce the effectiveness of the search. connectionGene.Weight = Math.Max(connectionGene.Weight, -np.connectionWeightRange/2.0); connectionGene.Weight = Math.Min(connectionGene.Weight, np.connectionWeightRange/2.0); break; } case ConnectionPerturbationType.JiggleND: { connectionGene.Weight += RandLib.gennor(0, paramGroup.Sigma); // Cap the connection weight. Large connections weights reduce the effectiveness of the search. connectionGene.Weight = Math.Max(connectionGene.Weight, -np.connectionWeightRange/2.0); connectionGene.Weight = Math.Min(connectionGene.Weight, np.connectionWeightRange/2.0); break; } case ConnectionPerturbationType.Reset: { // TODO: Precalculate connectionWeightRange / 2. connectionGene.Weight = (Utilities.NextDouble()*np.connectionWeightRange) - np.connectionWeightRange/2.0; break; } default: { throw new Exception("Unexpected ConnectionPerturbationType"); } } }
private void RemoveSimpleNeuron(uint neuronId, EvolutionAlgorithm ea) { // Create new connections that connect all of the incoming and outgoing neurons // that currently exist for the simple neuron. NeuronConnectionLookup lookup = (NeuronConnectionLookup)neuronConnectionLookupTable[neuronId]; foreach(ConnectionGene incomingConnection in lookup.incomingList) { foreach(ConnectionGene outgoingConnection in lookup.outgoingList) { if(TestForExistingConnection(incomingConnection.SourceNeuronId, outgoingConnection.TargetNeuronId)) { // Connection already exists. continue; } // Test for matching connection within NewConnectionGeneTable. ConnectionEndpointsStruct connectionKey = new ConnectionEndpointsStruct(incomingConnection.SourceNeuronId, outgoingConnection.TargetNeuronId); ConnectionGene existingConnection = (ConnectionGene)ea.NewConnectionGeneTable[connectionKey]; ConnectionGene newConnectionGene; if(existingConnection==null) { // No matching connection found. Create a connection with a new ID. newConnectionGene = new ConnectionGene(ea.NextInnovationId, incomingConnection.SourceNeuronId, outgoingConnection.TargetNeuronId, (Utilities.NextDouble() * ea.NeatParameters.connectionWeightRange) - ea.NeatParameters.connectionWeightRange/2.0); // Register the new ID with NewConnectionGeneTable. ea.NewConnectionGeneTable.Add(connectionKey, newConnectionGene); // Add the new gene to the genome. connectionGeneList.Add(newConnectionGene); } else { // Matching connection found. Re-use its ID. newConnectionGene = new ConnectionGene(existingConnection.InnovationId, incomingConnection.SourceNeuronId, outgoingConnection.TargetNeuronId, (Utilities.NextDouble() * ea.NeatParameters.connectionWeightRange) - ea.NeatParameters.connectionWeightRange/2.0); // Add the new gene to the genome. Use InsertIntoPosition() to ensure we don't break the sort // order of the connection genes. connectionGeneList.InsertIntoPosition(newConnectionGene); } } } // Delete the old connections. foreach(ConnectionGene incomingConnection in lookup.incomingList) connectionGeneList.Remove(incomingConnection); foreach(ConnectionGene outgoingConnection in lookup.outgoingList) { // Filter out recurrent connections - they will have already been // deleted in the loop through 'lookup.incomingList'. if(outgoingConnection.TargetNeuronId != neuronId) connectionGeneList.Remove(outgoingConnection); } // Delete the simple neuron - it no longer has any connections to or from it. neuronGeneList.Remove(neuronId); }
private void Mutate_AddConnection(EvolutionAlgorithm ea) { // We are always guaranteed to have enough neurons to form connections - because the input/output neurons are // fixed. Any domain that doesn't require input/outputs is a bit nonsensical! // Make a fixed number of attempts at finding a suitable connection to add. if(neuronGeneList.Count>1) { // At least 2 neurons, so we have a chance at creating a connection. for(int attempts=0; attempts<5; attempts++) { // Select candidate source and target neurons. Any neuron can be used as the source. Input neurons // should not be used as a target int srcNeuronIdx; int tgtNeuronIdx; /* Here's some code for adding connections that attempts to avoid any recursive conenctions * within a network by only linking to neurons with innovation id's greater than the source neuron. * Unfortunately this doesn't work because new neurons with large innovations ID's are inserted * randomly through a network's topology! Hence this code remains here in readyness to be resurrected * as part of some future work to support feedforward nets. // if(ea.NeatParameters.feedForwardOnly) // { // /* We can ensure that all networks are feedforward only by only adding feedforward connections here. // * Feed forward connections fall into one of the following categories. All references to indexes // * are indexes within this genome's neuronGeneList: // * 1) Source neuron is an input or hidden node, target is an output node. // * 2) Source is an input or hidden node, target is a hidden node with an index greater than the source node's index. // * 3) Source is an output node, target is an output node with an index greater than the source node's index. // * // * These rules are easier to understand if you understand how the different types if neuron are arranged within // * the neuronGeneList array. Neurons are arranged in the following order: // * // * 1) A single bias neuron is always first. // * 2) Experiment specific input neurons. // * 3) Output neurons. // * 4) Hidden neurons. // * // * The quantity and innovationID of all neurons within the first 3 categories remains fixed throughout the life // * of an experiment, hence we always know where to find the bias, input and output nodes. The number of hidden nodes // * can vary as ne nodes are created, pruned away or perhaps dropped during crossover, however they are always arranged // * newest to oldest, or in other words sorted by innovation idea, lowest ID first. // * // * If output neurons were at the end of the list with hidden nodes in the middle then generating feedforward // * connections would be as easy as selecting a target neuron with a higher index than the source neuron. However, that // * type of arrangement is not conducive to the operation of other routines, hence this routine is a little bit more // * complicated as a result. // */ // // // Ok, for a source neuron we can pick any neuron except the last output neuron. // int neuronIdxCount = neuronGeneList.Count; // int neuronIdxBound = neuronIdxCount-1; // // // Generate count-1 possibilities and avoid the last output neuron's idx. // srcNeuronIdx = (int)Math.Floor(Utilities.NextDouble() * neuronIdxBound); // if(srcNeuronIdx>inputBiasOutputNeuronCountMinus2) srcNeuronIdx++; // // // // Now generate a target idx depending on what type of neuron srcNeuronIdx is pointing to. // if(srcNeuronIdx<inputAndBiasNeuronCount) // { // Source is a bias or input neuron. Target can be any output or hidden neuron. // tgtNeuronIdx = inputAndBiasNeuronCount + (int)Math.Floor(Utilities.NextDouble() * (neuronIdxCount-inputAndBiasNeuronCount)); // } // else if(srcNeuronIdx<inputBiasOutputNeuronCount) // { // Source is an output neuron, but not the last output neuron. Target can be any output neuron with an index // // greater than srcNeuronIdx. // tgtNeuronIdx = (inputAndBiasNeuronCount+1) + (int)Math.Floor(Utilities.NextDouble() * ((inputBiasOutputNeuronCount-1)-srcNeuronIdx)); // } // else // { // Source is a hidden neuron. Target can be any hidden neuron after srcNeuronIdx or any output neuron. // tgtNeuronIdx = (int)Math.Floor(Utilities.NextDouble() * ((neuronIdxBound-srcNeuronIdx)+outputNeuronCount)); // // if(tgtNeuronIdx<outputNeuronCount) // { // Map to an output neuron idx. // tgtNeuronIdx += inputAndBiasNeuronCount; // } // else // { // // Map to one of the hidden neurons after srcNeuronIdx. // tgtNeuronIdx = (tgtNeuronIdx-outputNeuronCount)+srcNeuronIdx+1; // } // } // } // // Source neuron can by any neuron. Target neuron is any neuron except input neurons. // srcNeuronIdx = (int)Math.Floor(Utilities.NextDouble() * neuronGeneList.Count); // tgtNeuronIdx = inputAndBiasNeuronCount + (int)Math.Floor(Utilities.NextDouble() * (neuronGeneList.Count-inputAndBiasNeuronCount)); // // NeuronGene sourceNeuron = neuronGeneList[srcNeuronIdx]; // NeuronGene targetNeuron = neuronGeneList[tgtNeuronIdx]; // Find all potential inputs, or quit if there are not enough. // Neurons cannot be inputs if they are dummy input nodes of a module. NeuronGeneList potentialInputs = new NeuronGeneList(); foreach (NeuronGene n in neuronGeneList) { if (!(n.ActivationFunction is ModuleInputNeuron)) { potentialInputs.Add(n); } } if (potentialInputs.Count < 1) return; // Find all potential outputs, or quit if there are not enough. // Neurons cannot be outputs if they are dummy input or output nodes of a module, or network input or bias nodes. NeuronGeneList potentialOutputs = new NeuronGeneList(); foreach (NeuronGene n in neuronGeneList) { if (n.NeuronType != NeuronType.Bias && n.NeuronType != NeuronType.Input && !(n.ActivationFunction is ModuleInputNeuron) && !(n.ActivationFunction is ModuleOutputNeuron)) { potentialOutputs.Add(n); } } if (potentialOutputs.Count < 1) return; NeuronGene sourceNeuron = potentialInputs[Utilities.Next(potentialInputs.Count)]; NeuronGene targetNeuron = potentialOutputs[Utilities.Next(potentialOutputs.Count)]; // Check if a connection already exists between these two neurons. uint sourceId = sourceNeuron.InnovationId; uint targetId = targetNeuron.InnovationId; if(!TestForExistingConnection(sourceId, targetId)) { // Check if a matching mutation has already occured on another genome. // If so then re-use the connection ID. ConnectionEndpointsStruct connectionKey = new ConnectionEndpointsStruct(sourceId, targetId); ConnectionGene existingConnection = (ConnectionGene)ea.NewConnectionGeneTable[connectionKey]; ConnectionGene newConnectionGene; if(existingConnection==null) { // Create a new connection with a new ID and add it to the Genome. newConnectionGene = new ConnectionGene(ea.NextInnovationId, sourceId, targetId, (Utilities.NextDouble() * ea.NeatParameters.connectionWeightRange/4.0) - ea.NeatParameters.connectionWeightRange/8.0); // Register the new connection with NewConnectionGeneTable. ea.NewConnectionGeneTable.Add(connectionKey, newConnectionGene); // Add the new gene to this genome. We have a new ID so we can safely append the gene to the end // of the list without risk of breaking the innovation ID order. connectionGeneList.Add(newConnectionGene); } else { // Create a new connection, re-using the ID from existingConnection, and add it to the Genome. newConnectionGene = new ConnectionGene(existingConnection.InnovationId, sourceId, targetId, (Utilities.NextDouble() * ea.NeatParameters.connectionWeightRange/4.0) - ea.NeatParameters.connectionWeightRange/8.0); // Add the new gene to this genome. We are re-using an ID so we must ensure the connection gene is // inserted into the correct position (sorted by innovation ID). connectionGeneList.InsertIntoPosition(newConnectionGene); } return; } } } // We couldn't find a valid connection to create. Instead of doing nothing lets perform connection // weight mutation. Mutate_ConnectionWeights(ea); }
private void Mutate_AddModule(EvolutionAlgorithm ea) { // Find all potential inputs, or quit if there are not enough. // Neurons cannot be inputs if they are dummy input nodes created for another module. NeuronGeneList potentialInputs = new NeuronGeneList(); foreach (NeuronGene n in neuronGeneList) { if (!(n.ActivationFunction is ModuleInputNeuron)) { potentialInputs.Add(n); } } if (potentialInputs.Count < 1) return; // Find all potential outputs, or quit if there are not enough. // Neurons cannot be outputs if they are dummy input or output nodes created for another module, or network input or bias nodes. NeuronGeneList potentialOutputs = new NeuronGeneList(); foreach (NeuronGene n in neuronGeneList) { if (n.NeuronType != NeuronType.Bias && n.NeuronType != NeuronType.Input && !(n.ActivationFunction is ModuleInputNeuron) && !(n.ActivationFunction is ModuleOutputNeuron)) { potentialOutputs.Add(n); } } if (potentialOutputs.Count < 1) return; // Pick a new function for the new module. IModule func = ModuleFactory.GetRandom(); // Choose inputs uniformly at random, with replacement. // Create dummy neurons to represent the module's inputs. // Create connections between the input nodes and the dummy neurons. IActivationFunction inputFunction = ActivationFunctionFactory.GetActivationFunction("ModuleInputNeuron"); List<uint> inputDummies = new List<uint>(func.InputCount); for (int i = 0; i < func.InputCount; i++) { NeuronGene newNeuronGene = new NeuronGene(ea.NextInnovationId, NeuronType.Hidden, inputFunction); neuronGeneList.Add(newNeuronGene); uint sourceId = potentialInputs[Utilities.Next(potentialInputs.Count)].InnovationId; uint targetId = newNeuronGene.InnovationId; inputDummies.Add(targetId); // Create a new connection with a new ID and add it to the Genome. ConnectionGene newConnectionGene = new ConnectionGene(ea.NextInnovationId, sourceId, targetId, (Utilities.NextDouble() * ea.NeatParameters.connectionWeightRange) - ea.NeatParameters.connectionWeightRange / 2.0); // Register the new connection with NewConnectionGeneTable. ConnectionEndpointsStruct connectionKey = new ConnectionEndpointsStruct(sourceId, targetId); ea.NewConnectionGeneTable.Add(connectionKey, newConnectionGene); // Add the new gene to this genome. We have a new ID so we can safely append the gene to the end // of the list without risk of breaking the innovation ID order. connectionGeneList.Add(newConnectionGene); } // Choose outputs uniformly at random, with replacement. // Create dummy neurons to represent the module's outputs. // Create connections between the output nodes and the dummy neurons. IActivationFunction outputFunction = ActivationFunctionFactory.GetActivationFunction("ModuleOutputNeuron"); List<uint> outputDummies = new List<uint>(func.OutputCount); for (int i = 0; i < func.OutputCount; i++) { NeuronGene newNeuronGene = new NeuronGene(ea.NextInnovationId, NeuronType.Hidden, outputFunction); neuronGeneList.Add(newNeuronGene); uint sourceId = newNeuronGene.InnovationId; uint targetId = potentialOutputs[Utilities.Next(potentialOutputs.Count)].InnovationId; outputDummies.Add(sourceId); // Create a new connection with a new ID and add it to the Genome. ConnectionGene newConnectionGene = new ConnectionGene(ea.NextInnovationId, sourceId, targetId, (Utilities.NextDouble() * ea.NeatParameters.connectionWeightRange) - ea.NeatParameters.connectionWeightRange / 2.0); // Register the new connection with NewConnectionGeneTable. ConnectionEndpointsStruct connectionKey = new ConnectionEndpointsStruct(sourceId, targetId); ea.NewConnectionGeneTable.Add(connectionKey, newConnectionGene); // Add the new gene to this genome. We have a new ID so we can safely append the gene to the end // of the list without risk of breaking the innovation ID order. connectionGeneList.Add(newConnectionGene); } // Pick a new ID for the new module and create it. // Modules do not participate in history comparisons, so we will always create a new innovation ID. // We can change this here if it becomes a problem. ModuleGene newModule = new ModuleGene(ea.NextInnovationId, func, inputDummies, outputDummies); moduleGeneList.Add(newModule); }
/// <summary> /// Add a new node to the Genome. We do this by removing a connection at random and inserting /// a new node and two new connections that make the same circuit as the original connection. /// /// This way the new node is properly integrated into the network from the outset. /// </summary> /// <param name="ea"></param> private void Mutate_AddNode(EvolutionAlgorithm ea) { if(connectionGeneList.Count==0) return; // Select a connection at random. int connectionToReplaceIdx = (int)Math.Floor(Utilities.NextDouble() * connectionGeneList.Count); ConnectionGene connectionToReplace = connectionGeneList[connectionToReplaceIdx]; // Delete the existing connection. JOEL: Why delete old connection? //connectionGeneList.RemoveAt(connectionToReplaceIdx); // Check if this connection has already been split on another genome. If so then we should re-use the // neuron ID and two connection ID's so that matching structures within the population maintain the same ID. object existingNeuronGeneStruct = ea.NewNeuronGeneStructTable[connectionToReplace.InnovationId]; NeuronGene newNeuronGene; ConnectionGene newConnectionGene1; ConnectionGene newConnectionGene2; IActivationFunction actFunct; if(existingNeuronGeneStruct==null) { // No existing matching structure, so generate some new ID's. //TODO: DAVID proper random activation function // Replace connectionToReplace with two new connections and a neuron. actFunct=ActivationFunctionFactory.GetRandomActivationFunction(ea.NeatParameters); //newNeuronGene = new NeuronGene(ea.NextInnovationId, NeuronType.Hidden, actFunct); newNeuronGene = new NeuronGene(null, ea.NextInnovationId, (neuronGeneList.GetNeuronById(connectionToReplace.SourceNeuronId).Layer + neuronGeneList.GetNeuronById(connectionToReplace.TargetNeuronId).Layer) / 2.0, NeuronType.Hidden, actFunct); newConnectionGene1 = new ConnectionGene(ea.NextInnovationId, connectionToReplace.SourceNeuronId, newNeuronGene.InnovationId, 1.0); newConnectionGene2 = new ConnectionGene(ea.NextInnovationId, newNeuronGene.InnovationId, connectionToReplace.TargetNeuronId, connectionToReplace.Weight); // Register the new ID's with NewNeuronGeneStructTable. ea.NewNeuronGeneStructTable.Add(connectionToReplace.InnovationId, new NewNeuronGeneStruct(newNeuronGene, newConnectionGene1, newConnectionGene2)); } else { // An existing matching structure has been found. Re-use its ID's //TODO: DAVID proper random activation function // Replace connectionToReplace with two new connections and a neuron. actFunct = ActivationFunctionFactory.GetRandomActivationFunction(ea.NeatParameters); NewNeuronGeneStruct tmpStruct = (NewNeuronGeneStruct)existingNeuronGeneStruct; //newNeuronGene = new NeuronGene(tmpStruct.NewNeuronGene.InnovationId, NeuronType.Hidden, actFunct); newNeuronGene = new NeuronGene(null, tmpStruct.NewNeuronGene.InnovationId, tmpStruct.NewNeuronGene.Layer, NeuronType.Hidden, actFunct); newConnectionGene1 = new ConnectionGene(tmpStruct.NewConnectionGene_Input.InnovationId, connectionToReplace.SourceNeuronId, newNeuronGene.InnovationId, 1.0); newConnectionGene2 = new ConnectionGene(tmpStruct.NewConnectionGene_Output.InnovationId, newNeuronGene.InnovationId, connectionToReplace.TargetNeuronId, connectionToReplace.Weight); } // Add the new genes to the genome. neuronGeneList.Add(newNeuronGene); connectionGeneList.InsertIntoPosition(newConnectionGene1); connectionGeneList.InsertIntoPosition(newConnectionGene2); }
public CorrelationItem(CorrelationItemType correlationItemType, ConnectionGene connectionGene1, ConnectionGene connectionGene2) { this.correlationItemType = correlationItemType; this.connectionGene1 = connectionGene1; this.connectionGene2 = connectionGene2; }
// Schrum: Simple form of Module Mutation, MM(P) private void Module_Mutation_Previous(EvolutionAlgorithm ea) { // Push all output neurons together this.neuronGeneList.SortByNeuronOrder(); int numModules = this.outputNeuronCount / this.outputsPerPolicy; // Should evenly divide int randomModule = Utilities.Next(numModules); // Because outputs come after inputs. // Although list is 0-indexed, the +1 is needed because the bias does not count as an input double outputLayer = neuronGeneList[1 + inputNeuronCount].Layer; // Create the new module for (int i = 0; i < outputsPerPolicy; i++) { IActivationFunction outputActFunction = ActivationFunctionFactory.GetActivationFunction("BipolarSigmoid"); NeuronGene newNeuronGene = new NeuronGene(null, ea.NextInnovationId, outputLayer, NeuronType.Output, outputActFunction); neuronGeneList.Add(newNeuronGene); // Link to the new neuron: bias, then inputs, then appropriate module, then neuron within that module uint sourceNeuron = neuronGeneList[1 + inputNeuronCount + (randomModule * outputsPerPolicy) + i].InnovationId; ConnectionGene connection = new ConnectionGene(ea.NextInnovationId, sourceNeuron, newNeuronGene.InnovationId, 1.0); connectionGeneList.InsertIntoPosition(connection); this.outputNeuronCount++; // Increase number of outputs } // Schrum: Debugging //Console.WriteLine("MM(P): outputNeuronCount=" + outputNeuronCount); // Schrum: More debugging /* this.neuronGeneList.SortByInnovationId(); XmlDocument doc = new XmlDocument(); XmlGenomeWriterStatic.Write(doc, (NeatGenome)this); FileInfo oFileInfo = new FileInfo("MMPNet.xml"); doc.Save(oFileInfo.FullName); */ }
public NewConnectionGeneStruct(NeatGenome.NeatGenome owningGenome, ConnectionGene newConnectionGene) { this.OwningGenome = owningGenome; this.NewConnectionGene = newConnectionGene; }
private void RemoveSimpleNeuron(long neuronId, NeatParameters neatParameters, IdGenerator idGen, Hashtable NewConnectionGeneTable) { WINManager win = WINManager.SharedWIN; //WINNode node; WINConnection connection; // Create new connections that connect all of the incoming and outgoing neurons // that currently exist for the simple neuron. NeuronConnectionLookup lookup = (NeuronConnectionLookup)neuronConnectionLookupTable[neuronId]; foreach(ConnectionGene incomingConnection in lookup.incomingList) { foreach(ConnectionGene outgoingConnection in lookup.outgoingList) { if(TestForExistingConnection(incomingConnection.SourceNeuronId, outgoingConnection.TargetNeuronId)) { // Connection already exists. continue; } // Test for matching connection within NewConnectionGeneTable. ConnectionEndpointsStruct connectionKey = new ConnectionEndpointsStruct(incomingConnection.SourceNeuronId, outgoingConnection.TargetNeuronId); ConnectionGene existingConnection = (ConnectionGene)NewConnectionGeneTable[connectionKey]; ConnectionGene newConnectionGene; if(existingConnection==null) { // No matching connection found. Create a connection with a new ID. //create our newest conncetion, noting the source,target and weight connection = win.createWINConnection( WINConnection.ConnectionWithProperties(incomingConnection.SourceNeuronId, outgoingConnection.TargetNeuronId), idGen); // Create a new connection with a new ID and add it to the Genome. newConnectionGene = new ConnectionGene(connection.UniqueID, connection.SourceID, connection.TargetID, (Utilities.NextDouble() * neatParameters.connectionWeightRange) - neatParameters.connectionWeightRange / 2.0 ); //newConnectionGene = new ConnectionGene(idGen.NextInnovationId, // incomingConnection.SourceNeuronId, // outgoingConnection.TargetNeuronId, // (Utilities.NextDouble() * neatParameters.connectionWeightRange) - neatParameters.connectionWeightRange/2.0); // Register the new ID with NewConnectionGeneTable. NewConnectionGeneTable.Add(connectionKey, newConnectionGene); // Add the new gene to the genome. connectionGeneList.Add(newConnectionGene); } else { //WIN should acknowledge change in individual here // Matching connection found. Re-use its ID. newConnectionGene = new ConnectionGene(existingConnection.InnovationId, incomingConnection.SourceNeuronId, outgoingConnection.TargetNeuronId, (Utilities.NextDouble() * neatParameters.connectionWeightRange) - neatParameters.connectionWeightRange/2.0); // Add the new gene to the genome. Use InsertIntoPosition() to ensure we don't break the sort // order of the connection genes. connectionGeneList.InsertIntoPosition(newConnectionGene); } } } // Delete the old connections. foreach(ConnectionGene incomingConnection in lookup.incomingList) connectionGeneList.Remove(incomingConnection); foreach(ConnectionGene outgoingConnection in lookup.outgoingList) { // Filter out recurrent connections - they will have already been // deleted in the loop through 'lookup.incomingList'. if(outgoingConnection.TargetNeuronId != neuronId) connectionGeneList.Remove(outgoingConnection); } // Delete the simple neuron - it no longer has any connections to or from it. neuronGeneList.Remove(neuronId); }
/// <summary> /// Add a new node to the Genome. We do this by removing a connection at random and inserting /// a new node and two new connections that make the same circuit as the original connection. /// /// This way the new node is properly integrated into the network from the outset. /// </summary> /// <param name="ea"></param> private void Mutate_AddNode(NeatParameters neatParameters, IdGenerator idGen, Hashtable NewNeuronGeneStructTable) { if(connectionGeneList.Count==0) return; // Select a connection at random. int connectionToReplaceIdx = (int)Math.Floor(Utilities.NextDouble() * connectionGeneList.Count); ConnectionGene connectionToReplace = connectionGeneList[connectionToReplaceIdx]; // Delete the existing connection. JOEL: Why delete old connection? //connectionGeneList.RemoveAt(connectionToReplaceIdx); // Check if this connection has already been split on another genome. If so then we should re-use the // neuron ID and two connection ID's so that matching structures within the population maintain the same ID. object existingNeuronGeneStruct = NewNeuronGeneStructTable[connectionToReplace.InnovationId]; NeuronGene newNeuronGene; ConnectionGene newConnectionGene1; ConnectionGene newConnectionGene2; IActivationFunction actFunct; WINManager win = WINManager.SharedWIN; WINNode node; WINConnection connection; if(existingNeuronGeneStruct==null) { // No existing matching structure, so generate some new ID's. //TODO: DAVID proper random activation function // Replace connectionToReplace with two new connections and a neuron. actFunct=ActivationFunctionFactory.GetRandomActivationFunction(neatParameters); //newNeuronGene = new NeuronGene(ea.NextInnovationId, NeuronType.Hidden, actFunct); //we should be making a call to WIN anytime we call for a new innovationID //here we create a new node, and two new connections //we don't send in a genomeID here, so we get it set for us! node = win.createWINNode(new PropertyObject() { { WINNode.SNodeString, NeuronType.Hidden.ToString()}}); newNeuronGene = new NeuronGene(null, node.UniqueID, (neuronGeneList.GetNeuronById(connectionToReplace.SourceNeuronId).Layer + neuronGeneList.GetNeuronById(connectionToReplace.TargetNeuronId).Layer) / 2.0, NeuronType.Hidden, actFunct); //we don't send in any uniqueIDs so they are generated for us, and we update our idGen object connection = win.createWINConnection(WINConnection.ConnectionWithProperties(connectionToReplace.SourceNeuronId, newNeuronGene.InnovationId), idGen); newConnectionGene1 = new ConnectionGene(connection.UniqueID, connection.SourceID, connection.TargetID , 1.0); //we don't send in any uniqueIDs so they are generated for us, and we update our idGen object connection = win.createWINConnection(WINConnection.ConnectionWithProperties(newNeuronGene.InnovationId, connectionToReplace.TargetNeuronId), idGen); newConnectionGene2 = new ConnectionGene(connection.UniqueID, connection.SourceID, connection.TargetID, connectionToReplace.Weight); // Register the new ID's with NewNeuronGeneStructTable. NewNeuronGeneStructTable.Add(connectionToReplace.InnovationId, new NewNeuronGeneStruct(newNeuronGene, newConnectionGene1, newConnectionGene2)); } else { // An existing matching structure has been found. Re-use its ID's //Since we don't call idGen.nextinnovationID, we don't have to create anything new //however, we need to note the change in weights override previous weight values //this should be documented in WIN - either explicitly (calling a function to note changes) or implicitly (by scanning the changes in a saved genome) //Probably explicitly, calling a mutate function for a SingleStep (since we are in the SingleStep process of creating new individuals) //TODO: DAVID proper random activation function // Replace connectionToReplace with two new connections and a neuron. actFunct = ActivationFunctionFactory.GetRandomActivationFunction(neatParameters); NewNeuronGeneStruct tmpStruct = (NewNeuronGeneStruct)existingNeuronGeneStruct; //newNeuronGene = new NeuronGene(tmpStruct.NewNeuronGene.InnovationId, NeuronType.Hidden, actFunct); newNeuronGene = new NeuronGene(null, tmpStruct.NewNeuronGene.InnovationId, tmpStruct.NewNeuronGene.Layer, NeuronType.Hidden, actFunct); newConnectionGene1 = new ConnectionGene(tmpStruct.NewConnectionGene_Input.InnovationId, connectionToReplace.SourceNeuronId, newNeuronGene.InnovationId, 1.0); newConnectionGene2 = new ConnectionGene(tmpStruct.NewConnectionGene_Output.InnovationId, newNeuronGene.InnovationId, connectionToReplace.TargetNeuronId, connectionToReplace.Weight); } // Add the new genes to the genome. neuronGeneList.Add(newNeuronGene); connectionGeneList.InsertIntoPosition(newConnectionGene1); connectionGeneList.InsertIntoPosition(newConnectionGene2); }
private void Mutate_AddModule(NeatParameters neatParameters, IdGenerator idGen, Hashtable NewConnectionGeneTable) { // Find all potential inputs, or quit if there are not enough. // Neurons cannot be inputs if they are dummy input nodes created for another module. NeuronGeneList potentialInputs = new NeuronGeneList(); foreach (NeuronGene n in neuronGeneList) { if (!(n.ActivationFunction is ModuleInputNeuron)) { potentialInputs.Add(n); } } if (potentialInputs.Count < 1) return; // Find all potential outputs, or quit if there are not enough. // Neurons cannot be outputs if they are dummy input or output nodes created for another module, or network input or bias nodes. NeuronGeneList potentialOutputs = new NeuronGeneList(); foreach (NeuronGene n in neuronGeneList) { if (n.NeuronType != NeuronType.Bias && n.NeuronType != NeuronType.Input && !(n.ActivationFunction is ModuleInputNeuron) && !(n.ActivationFunction is ModuleOutputNeuron)) { potentialOutputs.Add(n); } } if (potentialOutputs.Count < 1) return; // Pick a new function for the new module. IModule func = ModuleFactory.GetRandom(); WINManager win = WINManager.SharedWIN; WINNode node; WINConnection connection; // Choose inputs uniformly at random, with replacement. // Create dummy neurons to represent the module's inputs. // Create connections between the input nodes and the dummy neurons. IActivationFunction inputFunction = ActivationFunctionFactory.GetActivationFunction("ModuleInputNeuron"); List<long> inputDummies = new List<long>(func.InputCount); for (int i = 0; i < func.InputCount; i++) { //we are calling nextinnovationID, this is the place for WIN! //in reality, win should know the activation function as well, but that's not currently implemented //here we create a new node, and two new connections //we don't send in a genomeID here, so we get it set for us! //do we need to inform it of the activation function? I think so? node = win.createWINNode(new PropertyObject() { { WINNode.SNodeString, NeuronType.Hidden.ToString()}}); NeuronGene newNeuronGene = new NeuronGene(node.UniqueID, NeuronType.Hidden, inputFunction); neuronGeneList.Add(newNeuronGene); long sourceId = potentialInputs[Utilities.Next(potentialInputs.Count)].InnovationId; long targetId = newNeuronGene.InnovationId; inputDummies.Add(targetId); //aha! we must call the innovationID again, we check against win //we don't send in any uniqueIDs so they are generated for us, and we update our idGen object connection = win.createWINConnection( WINConnection.ConnectionWithProperties(sourceId, targetId), idGen); // Create a new connection with a new ID and add it to the Genome. ConnectionGene newConnectionGene = new ConnectionGene(connection.UniqueID, connection.SourceID, connection.TargetID, (Utilities.NextDouble() * neatParameters.connectionWeightRange) - neatParameters.connectionWeightRange / 2.0 ); // Register the new connection with NewConnectionGeneTable. ConnectionEndpointsStruct connectionKey = new ConnectionEndpointsStruct(sourceId, targetId); NewConnectionGeneTable.Add(connectionKey, newConnectionGene); // Add the new gene to this genome. We have a new ID so we can safely append the gene to the end // of the list without risk of breaking the innovation ID order. connectionGeneList.Add(newConnectionGene); } // Choose outputs uniformly at random, with replacement. // Create dummy neurons to represent the module's outputs. // Create connections between the output nodes and the dummy neurons. IActivationFunction outputFunction = ActivationFunctionFactory.GetActivationFunction("ModuleOutputNeuron"); List<long> outputDummies = new List<long>(func.OutputCount); for (int i = 0; i < func.OutputCount; i++) { node = win.createWINNode(new PropertyObject() { { WINNode.SNodeString, NeuronType.Hidden.ToString() } }); NeuronGene newNeuronGene = new NeuronGene(node.UniqueID, NeuronType.Hidden, outputFunction); neuronGeneList.Add(newNeuronGene); long sourceId = newNeuronGene.InnovationId; long targetId = potentialOutputs[Utilities.Next(potentialOutputs.Count)].InnovationId; outputDummies.Add(sourceId); connection = win.createWINConnection( WINConnection.ConnectionWithProperties(sourceId, targetId), idGen); // Create a new connection with a new ID and add it to the Genome. ConnectionGene newConnectionGene = new ConnectionGene(connection.UniqueID, connection.SourceID, connection.TargetID, (Utilities.NextDouble() * neatParameters.connectionWeightRange) - neatParameters.connectionWeightRange / 2.0 ); //new ConnectionGene(idGen.NextInnovationId, sourceId, targetId, //(Utilities.NextDouble() * neatParameters.connectionWeightRange) - neatParameters.connectionWeightRange / 2.0); // Register the new connection with NewConnectionGeneTable. ConnectionEndpointsStruct connectionKey = new ConnectionEndpointsStruct(sourceId, targetId); NewConnectionGeneTable.Add(connectionKey, newConnectionGene); // Add the new gene to this genome. We have a new ID so we can safely append the gene to the end // of the list without risk of breaking the innovation ID order. connectionGeneList.Add(newConnectionGene); } // Pick a new ID for the new module and create it. // Modules do not participate in history comparisons, so we will always create a new innovation ID. // We can change this here if it becomes a problem. //TODO: Paul check win conditions here //this is confusing from a WIN perspective, I don't know if I'm going to support modules //I can create a generic NextInnovationID object, but don't know if it's worth it ModuleGene newModule = new ModuleGene(idGen.NextInnovationId, func, inputDummies, outputDummies); moduleGeneList.Add(newModule); }
/// <summary> /// Add a new node to the Genome. We do this by removing a connection at random and inserting /// a new node and two new connections that make the same circuit as the original connection. /// /// This way the new node is properly integrated into the network from the outset. /// </summary> /// <param name="ea"></param> private void Mutate_AddNode(EvolutionAlgorithm ea) { if(connectionGeneList.Count==0) return; for (int attempts = 0; attempts < 5; attempts++) { // Select a connection at random. int connectionToReplaceIdx = (int)Math.Floor(Utilities.NextDouble() * connectionGeneList.Count); ConnectionGene connectionToReplace = connectionGeneList[connectionToReplaceIdx]; // Delete the existing connection. JOEL: Why delete old connection? connectionGeneList.RemoveAt(connectionToReplaceIdx); // Check if this connection has already been split on another genome. If so then we should re-use the // neuron ID and two connection ID's so that matching structures within the population maintain the same ID. object existingNeuronGeneStruct = ea.NewNeuronGeneStructTable[connectionToReplace.InnovationId]; NeuronGene newNeuronGene; ConnectionGene newConnectionGene1; ConnectionGene newConnectionGene2; IActivationFunction actFunct; // JUSTIN: Calculate the layer for this new neuron as halfway between the source and the target neuron layers float sourceLayer = neuronGeneList.GetNeuronById(connectionToReplace.SourceNeuronId).Layer; float targetLayer = neuronGeneList.GetNeuronById(connectionToReplace.TargetNeuronId).Layer; float newLayer = ((sourceLayer + targetLayer) / 2); if (existingNeuronGeneStruct == null) { // No existing matching structure, so generate some new ID's. //TODO: DAVID proper random activation function // Replace connectionToReplace with two new connections and a neuron. // JUSTIN: If using neatBrain, then do NOT get a random actv. function!! actFunct = ea.neatBrain ? ActivationFunctionFactory.GetActivationFunction(HyperNEATParameters.substrateActivationFunction.FunctionId) : ActivationFunctionFactory.GetRandomActivationFunction(ea.NeatParameters); newNeuronGene = new NeuronGene(ea.NextInnovationId, NeuronType.Hidden, actFunct, newLayer); newConnectionGene1 = new ConnectionGene(ea.NextInnovationId, connectionToReplace.SourceNeuronId, newNeuronGene.InnovationId, 1.0); newConnectionGene2 = new ConnectionGene(ea.NextInnovationId, newNeuronGene.InnovationId, connectionToReplace.TargetNeuronId, connectionToReplace.Weight); // Register the new ID's with NewNeuronGeneStructTable. ea.NewNeuronGeneStructTable.Add(connectionToReplace.InnovationId, new NewNeuronGeneStruct(newNeuronGene, newConnectionGene1, newConnectionGene2)); } else { // An existing matching structure has been found. Re-use its ID's //DAVID: Trying to add a node where one already exists, reject, is this good? if (neuronGeneList.GetNeuronById(((NewNeuronGeneStruct)existingNeuronGeneStruct).NewNeuronGene.InnovationId) != null) { continue; } //TODO: DAVID proper random activation function // Replace connectionToReplace with two new connections and a neuron. // JUSTIN: If using neatBrain, then do NOT get a random actv. function!! actFunct = ea.neatBrain ? ActivationFunctionFactory.GetActivationFunction(HyperNEATParameters.substrateActivationFunction.FunctionId) : ActivationFunctionFactory.GetRandomActivationFunction(ea.NeatParameters); NewNeuronGeneStruct tmpStruct = (NewNeuronGeneStruct)existingNeuronGeneStruct; newNeuronGene = new NeuronGene(tmpStruct.NewNeuronGene.InnovationId, NeuronType.Hidden, actFunct, newLayer); newConnectionGene1 = new ConnectionGene(tmpStruct.NewConnectionGene_Input.InnovationId, connectionToReplace.SourceNeuronId, newNeuronGene.InnovationId, (Utilities.NextDouble() * ea.NeatParameters.connectionWeightRange) - ea.NeatParameters.connectionWeightRange / 2.0); newConnectionGene2 = new ConnectionGene(tmpStruct.NewConnectionGene_Output.InnovationId, newNeuronGene.InnovationId, connectionToReplace.TargetNeuronId, (Utilities.NextDouble() * ea.NeatParameters.connectionWeightRange / 8.0) - ea.NeatParameters.connectionWeightRange / 16.0); } // Add the new genes to the genome. //Debug.Assert(); neuronGeneList.Add(newNeuronGene); connectionGeneList.InsertIntoPosition(newConnectionGene1); connectionGeneList.InsertIntoPosition(newConnectionGene2); break; } }
private void BuildNeuronConnectionLookupTable_NewOutgoingConnection(uint neuronId, ConnectionGene connectionGene) { // Is this neuron already known to the lookup table? NeuronConnectionLookup lookup = (NeuronConnectionLookup)neuronConnectionLookupTable[neuronId]; if(lookup==null) { // Creae a new lookup entry for this neuron Id. lookup = new NeuronConnectionLookup(); lookup.neuronGene = (NeuronGene)neuronGeneTable[neuronId]; neuronConnectionLookupTable.Add(neuronId, lookup); } // Register the connection with the NeuronConnectionLookup object. lookup.outgoingList.Add(connectionGene); }
/// <summary> /// Adds a connection to the list that will eventually be copied into a child of this genome during sexual reproduction. /// A helper function that is only called by CreateOffspring_Sexual_ProcessCorrelationItem(). /// </summary> /// <param name="connectionGene">Specifies the connection to add to this genome.</param> /// <param name="overwriteExisting">If there is already a connection from the same source to the same target, /// that connection is replaced when overwriteExisting is true and remains (no change is made) when overwriteExisting is false.</param> private void CreateOffspring_Sexual_AddGene(ConnectionGene connectionGene, bool overwriteExisting) { ConnectionEndpointsStruct connectionKey = new ConnectionEndpointsStruct( connectionGene.SourceNeuronId, connectionGene.TargetNeuronId); // Check if a matching gene has already been added. object oIdx = newConnectionGeneTable[connectionKey]; if(oIdx==null) { // No matching gene has been added. // Register this new gene with the newConnectionGeneTable - store its index within newConnectionGeneList. newConnectionGeneTable[connectionKey] = newConnectionGeneList.Count; // Add the gene to the list. newConnectionGeneList.Add(connectionGene); } else if(overwriteExisting) { // Overwrite the existing matching gene with this one. In fact only the weight value differs between two // matching connection genes, so just overwrite the existing genes weight value. // Remember that we stored the gene's index in newConnectionGeneTable. So use it here. newConnectionGeneList[(int)oIdx].Weight = connectionGene.Weight; } }
// Schrum: Module Mutation Random creates a new module with // completely random incoming links. private void Module_Mutation_Random(EvolutionAlgorithm ea) { // Push all output neurons together this.neuronGeneList.SortByNeuronOrder(); int numModules = this.outputNeuronCount / this.outputsPerPolicy; // Should evenly divide int randomModule = Utilities.Next(numModules); // Because outputs come after inputs. // Although list is 0-indexed, the +1 is needed because the bias does not count as an input double outputLayer = neuronGeneList[1 + inputNeuronCount].Layer; // Create the new module one neuron per loop iteration for (int i = 0; i < outputsPerPolicy; i++) { // The activation function for the output layer IActivationFunction outputActFunction = ActivationFunctionFactory.GetActivationFunction("BipolarSigmoid"); NeuronGene newNeuronGene = new NeuronGene(null, ea.NextInnovationId, outputLayer, NeuronType.Output, outputActFunction); neuronGeneList.Add(newNeuronGene); // Count links to random output neuron: bias, then inputs, then random module, then neuron within that module uint randomModuleInnovation = neuronGeneList[1 + inputNeuronCount + (randomModule * outputsPerPolicy) + i].InnovationId; int numIncoming = 0; foreach (ConnectionGene cg in this.ConnectionGeneList) { // Count the link if (cg.TargetNeuronId == randomModuleInnovation) numIncoming++; } // Give the new module (up to) the same number of links as some other module for (int j = 0; j < numIncoming; j++) // Will always create ay least one link { uint randomSource = NeuronGeneList[Utilities.Next(NeuronGeneList.Count)].InnovationId; // Magic equation stolen from Mutate_AddConnection below double randomWeight = (Utilities.NextDouble() * ea.NeatParameters.connectionWeightRange/4.0) - ea.NeatParameters.connectionWeightRange/8.0; if (!TestForExistingConnection(randomSource, newNeuronGene.InnovationId)) // Only create each connection once { ConnectionGene connection = new ConnectionGene(ea.NextInnovationId, randomSource, newNeuronGene.InnovationId, randomWeight); connectionGeneList.InsertIntoPosition(connection); } } this.outputNeuronCount++; // Increase number of outputs } }
// Schrum: Module Mutation Duplicate creates a new module with // links copying those of another module. private void Module_Mutation_Duplicate(EvolutionAlgorithm ea) { // Push all output neurons together this.neuronGeneList.SortByNeuronOrder(); int numModules = this.outputNeuronCount / this.outputsPerPolicy; // Should evenly divide int randomModule = Utilities.Next(numModules); // Duplicate this module // Because outputs come after inputs. // Although list is 0-indexed, the +1 is needed because the bias does not count as an input double outputLayer = neuronGeneList[1 + inputNeuronCount].Layer; // Create the new module one neuron per loop iteration for (int i = 0; i < outputsPerPolicy; i++) { // The activation function for the output layer IActivationFunction outputActFunction = ActivationFunctionFactory.GetActivationFunction("BipolarSigmoid"); NeuronGene newNeuronGene = new NeuronGene(null, ea.NextInnovationId, outputLayer, NeuronType.Output, outputActFunction); neuronGeneList.Add(newNeuronGene); uint randomModuleInnovation = neuronGeneList[1 + inputNeuronCount + (randomModule * outputsPerPolicy) + i].InnovationId; // Copy each connection to the new module neuron int originalLength = ConnectionGeneList.Count; // Don't need to check the newly added connections for (int j = 0; j < originalLength; j++) { ConnectionGene cg = ConnectionGeneList[j]; if (cg.TargetNeuronId == randomModuleInnovation) { // Copy the link ConnectionGene connection = new ConnectionGene(ea.NextInnovationId, cg.SourceNeuronId, newNeuronGene.InnovationId, cg.Weight); connectionGeneList.InsertIntoPosition(connection); } } this.outputNeuronCount++; // Increase number of outputs } }