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); }
public ConnectionGene(ConnectionGene copy) { inNode = copy.GetInNode(); outNode = copy.GetOutNode(); weight = copy.GetWeight(); expressed = copy.IsExpressed(); innovation = copy.GetInnovation(); }
public Genome InitGenome() { Genome g1 = new Genome(gt); for (int i = 0; i < inputSize + outputSize; i++) { NodeGene node = GetDeafultGenome().GetNodeGenes()[i].Copy(); if (node.GetNodeType() == NodeGene.NODETYPE.OUTPUT || node.GetNodeType() == NodeGene.NODETYPE.HIDDEN) { node.SetBias(Random.Range(-1.0f, 1.0f)); //node.SetBias(1.0f); } g1.AddNodeGene(node); //g1.GetNodeGenes()[inputSize + i].SetBias(Random.Range(-1.0f, 1.0f)); } for (int i = 0; i < startConnections; i++)//sample n connections { ConnectionGene con = connections[Random.Range(0, connections.Count)].Copy(); //ConnectionGene con = connections[i].Copy(); con.SetWeight(Random.Range(-weightRange, weightRange)); //con.SetWeight(1.0f); int inId = con.GetInNode().GetId(); con.SetInNode(g1.GetNodeGenes()[inId]); int outId = con.GetOutNode().GetId(); //Debug.Log(con.GetOutNode()); //Debug.Log(outId); con.SetOutNode(g1.GetNodeGenes()[outId]); g1.AddConnectionGene(con); } if (speciation) { InsertGenomeIntoSpecies(g1, species); impf.StatsSpecies(species.Count); } genomes.Add(g1); return(g1); }
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)); }