public void AddConnectionMutation(Counter innovation, int maxAttempts) { int tries = 0; bool success = false; while (tries < maxAttempts && success == false) { tries++; int listSize = Nodes.Count; NodeGene node1 = Nodes[Random.Range(0, listSize)]; NodeGene node2 = Nodes[Random.Range(0, listSize)]; bool reversed = false; if (node1.GetType().Equals(NodeGene.NodeType.HIDDEN) && node2.GetType().Equals(NodeGene.NodeType.INPUT)) { reversed = true; } else if (node1.GetType().Equals(NodeGene.NodeType.OUTPUT) && node2.GetType().Equals(NodeGene.NodeType.HIDDEN)) { reversed = true; } else if (node1.GetType().Equals(NodeGene.NodeType.OUTPUT) && node2.GetType().Equals(NodeGene.NodeType.INPUT)) { reversed = true; } bool connectionExists = false; foreach (ConnectionGene con in Connections.Values) { if (con.InNode == node1.ID && con.OutNode == node2.ID) { connectionExists = true; break; } else if (con.InNode == node2.ID && con.OutNode == node1.ID) { connectionExists = true; break; } } if (connectionExists) { return; } ConnectionGene newCon = new ConnectionGene(reversed ? node2.ID : node1.ID, reversed ? node1.ID : node2.ID, Random.Range(0f, 1f), true, 0); //Connections.Add(newCon.Innovation, newCon); AddConnectionGene(newCon); } if (success == false) { Debug.Log("Tried, but could not add more connections"); } }
public void AddConnectionMutation(Random r) { int listSize = Nodes.Count; NodeGene node1 = Nodes[Random.Range(0, listSize)]; NodeGene node2 = Nodes[Random.Range(0, listSize)]; bool reversed = false; if (node1.GetType().Equals(NodeGene.NodeType.HIDDEN) && node2.GetType().Equals(NodeGene.NodeType.INPUT)) { reversed = true; } else if (node1.GetType().Equals(NodeGene.NodeType.OUTPUT) && node2.GetType().Equals(NodeGene.NodeType.HIDDEN)) { reversed = true; } else if (node1.GetType().Equals(NodeGene.NodeType.OUTPUT) && node2.GetType().Equals(NodeGene.NodeType.INPUT)) { reversed = true; } bool connectionExists = false; foreach (ConnectionGene con in Connections.Values) { if (con.InNode == node1.ID && con.OutNode == node2.ID) { connectionExists = true; break; } else if (con.InNode == node2.ID && con.OutNode == node1.ID) { connectionExists = true; break; } } if (connectionExists) { return; } ConnectionGene newCon = new ConnectionGene(reversed ? node2.ID : node1.ID, reversed ? node1.ID : node2.ID, Random.Range(0f, 1f), true, 0); Connections.Add(newCon.Innovation, newCon); }