public void AddNodeMutation() { ConnectionGene connection = connections.ElementAt(rand.Next(0, connections.Count)).Value; NodeGene inNode = nodes[connection.getInNode()]; NodeGene outNode = nodes[connection.getOutNode()]; connection.Disable(); //NodeGene newNode = new NodeGene(NodeGene.TYPE.HIDDEN, nodes.Count + 1); NodeGene newNode = new NodeGene(NodeGene.TYPE.HIDDEN, History.NodeInnovate()); ConnectionGene inToNew = new ConnectionGene(inNode.getId(), newNode.getId(), 1f, true, History.Innovate()); ConnectionGene newToOut = new ConnectionGene(newNode.getId(), outNode.getId(), connection.getWeight(), true, History.Innovate()); nodes.Add(newNode.getId(), newNode); //connections.Add(inToNew.getInnovation(), inToNew); //connections.Add(newToOut.getInnovation(), newToOut); AddConnectionGene(inToNew); AddConnectionGene(newToOut); //newNode.AddInNode(GetNodes()[inToNew.getInNode()]); //GetNodes()[newToOut.getOutNode()].AddInNode(newNode); // remove old in-node from out gene outNode.RemoveInNode(inNode); }
public void AddNodeMutation() { if (neat.maxNeurones <= nodes.Count) { return; } ConnectionGene connectionSplit = connections[Random.Range(0, connections.Count)];//choose random connection to split connectionSplit.Disable(); NodeGene nodeIn = connectionSplit.GetInNode(); NodeGene nodeOut = connectionSplit.GetOutNode(); //Debug.Log("nin" + nodeIn.GetLayer()); //Debug.Log("nout"+ nodeOut.GetLayer()); int newLayer; if (nodeOut.GetLayer() - nodeIn.GetLayer() == 1) { //Debug.Log(nodeIn.GetLayer()); //Debug.Log(nodeOut.GetLayer()); newLayer = nodeOut.GetLayer(); AddLayer(nodeOut.GetLayer()); //Debug.Log("new layer" + nodeOut.GetLayer()); //newLayer = nodeOut.GetLayer(); } else { newLayer = Random.Range(nodeIn.GetLayer() + 1, nodeOut.GetLayer() - 1); } NodeGene node = new NodeGene(NodeGene.NODETYPE.HIDDEN, track.NewId(), newLayer, Random.Range(-1.0f, 1.0f), neat.internalActiationFunction); nodes.Add(node); ConnectionGene connectionIn = new ConnectionGene(connectionSplit.GetInNode().Copy(), node, 1.0f, true, track.Innovate()); ConnectionGene connectionOut = new ConnectionGene(node, connectionSplit.GetOutNode().Copy(), connectionSplit.GetWeight(), true, track.Innovate()); connections.Add(connectionIn); connections.Add(connectionOut); }
/// <summary> /// Gets a (new) NodeGene for this Generation from existing ConnectionGene /// </summary> /// <param name="connectionOld">Connection to create Node for. Passed by reference in order to disable it</param> /// <param name="connectionNew1">OUT: New Connection from old In-Node to new Node</param> /// <param name="connectionNew2">OUT: New Connection from new Node to old Out-Node</param> /// <returns>Created NodeGene</returns> internal static NodeGene GetNode(ref ConnectionGene connectionOld, out ConnectionGene connectionNew1, out ConnectionGene connectionNew2) { connectionOld.Disable(); NodeGene newNode; if (generationNodes.ContainsKey(connectionOld.Innovation)) // Exists { newNode = generationNodes[connectionOld.Innovation]; } else { newNode = new NodeGene(NodeType.HIDDEN); // New Node (Can only be a hidden node, as it is between other nodes) } connectionNew1 = GetConnection(connectionOld.In, newNode); // Get/Create Connection from Old In to Gene connectionNew2 = GetConnection(newNode, connectionOld.Out); // Get/Create Connection from Gene to Old Out return(newNode); }
public void AddNodeMutation(Random r) //insert a node between two connected nodes { int conKey = connectionKeys[r.Next(connectionKeys.Count)]; //get a random connection ConnectionGene con = connectionList[conKey]; int node1 = con.GetInNode(); int node2 = con.GetOutNode(); con.Disable(); //disable connection NodeGene newNode = new NodeGene(nodeList.Count + 1, NodeGene.TYPE.HIDDEN); //create a new node nodeList.Add(newNode); //add new node to node list int innovation1 = InnovationGenerator.GetInnovation(); int innovation2 = InnovationGenerator.GetInnovation(); connectionKeys.Add(innovation1); connectionList.Add(innovation1, new ConnectionGene(node1, newNode.GetID(), 1f, true, innovation1)); //add new connections to connection list connectionKeys.Add(innovation2); connectionList.Add(innovation2, new ConnectionGene(newNode.GetID(), node2, con.GetWeight(), true, innovation2)); }