/// <summary> /// Add a new Gene to the list of genes /// </summary> public void AddGene() { Gene g = population.GetGene(counterGenes); genes.Add(counterGenes, g); counterGenes++; //All input and output genes have been created //So the next ones will be in the middle and they will be used for inter-connections if (counterGenes > numberInput + numberOutput) { Gene i, o; float w = 1; //Select a gene from input genes i = genes[Random.Range(0, numberInput)]; //Select the next gene (output or middle one) //(The last gene created is excluded since it make no sense to be connected to itself) o = genes[Random.Range(numberInput, counterGenes)]; ConnectionGene c = population.GetConnection(i, o); if (c != null) { w = c.GetWeight(); c.SetEnabled(false); } AddConnection(i, g, w); AddConnection(g, o, 1); } }
public void RemoveConnection(ConnectionGene connection) { ////Connect the 2 extremes together //Gene from = connection.GetFromGene(); //Gene to = connection.GetToGene(); //float weight = connection.GetWeight(); //AddConnection(from, to, weight); ////Remove old connection //population.RemoveConnection(connection); connection.SetEnabled(false); }
/// <summary> /// Get connection if it exists otherwise it is created a new Connection /// </summary> /// <param name="from"></param> /// <param name="to"></param> /// <param name="weight"></param> /// <returns></returns> public ConnectionGene GetConnection(Gene from, Gene to, float weight = 0) { int index = HashFuction(from.GetID(), to.GetID()); ConnectionGene c; connections.TryGetValue(index, out c); if (c == null) { c = new ConnectionGene(index, from, to, weight); connections.Add(index, c); c.SetEnabled(true); } return(c); }
public void MutateAddNode() { ConnectionGene randomConnection = GetConnectionGenes()[random.Next(GetConnectionGenes().Count)]; NodeGene InNode = GetNodeGenes()[randomConnection.GetInputNode()]; NodeGene OutNode = GetNodeGenes()[randomConnection.GetOutputNode()]; randomConnection.SetEnabled(false); NodeGene MiddleNode = new NodeGene(GetNodeGenes().Count + 1, NodeGene.TYPE.HIDDEN, 0); ConnectionGene InputToMiddle = new ConnectionGene(InNode.GetID(), MiddleNode.GetID(), 1, true, NEAT_CONFIGS.GLOBAL_INNOVATION_NUMBER++); ConnectionGenes.Add(InputToMiddle); MiddleNode.AddIncomingConnection(InputToMiddle); ConnectionGene MiddleToOutput = new ConnectionGene(MiddleNode.GetID(), OutNode.GetID(), randomConnection.GetWeight(), true, NEAT_CONFIGS.GLOBAL_INNOVATION_NUMBER++); ConnectionGenes.Add(MiddleToOutput); OutNode.AddIncomingConnection(MiddleToOutput); //Add to the total current Pool Connections Made in this instance NodeGenes.Add(MiddleNode.GetID(), MiddleNode); }
public void DisableConnection(ConnectionGene gene) { gene.SetEnabled(false); }
public void EnableConnection(ConnectionGene gene) { gene.SetEnabled(true); }