/// <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 ConnectionGene(ConnectionGene copy) { inNode = copy.GetInNode(); outNode = copy.GetOutNode(); weight = copy.GetWeight(); expressed = copy.IsExpressed(); innovation = copy.GetInnovation(); }
public ConnectionGene(ConnectionGene Connection) { if (Connection != null) { this.InputNode = Connection.GetInputNode(); this.OutputNode = Connection.GetOutputNode(); this.Weight = Connection.GetWeight(); this.IsEnabled = Connection.GetEnabled(); this.Innovation = Connection.GetInnovation(); } }
public void MutateWeight(ConnectionGene gene) { double randomValueUpDown = random.NextDouble() * (0.1 - 0.01) + 0.01; bool UpDown; if (random.NextDouble() > 0.5) { UpDown = true; } else { UpDown = false; } gene.SetWeight(gene.GetWeight() + (randomValueUpDown * (UpDown ? 1 : -1))); }
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)); }
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 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); }