public static float[] GetGeneInfo(Genome genome1, Genome genome2) { int matchingGenes = 0; float weightDiff = 0; int excessGenes = 0; int disjointGenes = 0; List <int> nodeKeys1 = genome1.GetNodes().Keys.ToList(); nodeKeys1.Sort(); List <int> nodeKeys2 = genome2.GetNodes().Keys.ToList(); nodeKeys2.Sort(); int highestInnovation1 = nodeKeys1[nodeKeys1.Count() - 1]; int highestInnovation2 = nodeKeys2[nodeKeys2.Count() - 1]; int minHighestInnovation = Math.Min(highestInnovation1, highestInnovation2); for (int i = 1; i <= minHighestInnovation; i++) { bool node1 = genome1.GetNodes().TryGetValue(i, out NodeGene value); bool node2 = genome2.GetNodes().TryGetValue(i, out NodeGene value2); if (node1 && node2) // both exist { matchingGenes++; } else if ((!node1) != (!node2)) // only one exists { disjointGenes++; } } List <int> connectionKeys1 = genome1.GetConnections().Keys.ToList(); connectionKeys1.Sort(); List <int> connectionKeys2 = genome2.GetConnections().Keys.ToList(); connectionKeys2.Sort(); highestInnovation1 = connectionKeys1[connectionKeys1.Count() - 1]; highestInnovation2 = connectionKeys2[connectionKeys2.Count() - 1]; minHighestInnovation = Math.Min(highestInnovation1, highestInnovation2); for (int i = 1; i <= minHighestInnovation; i++) { ConnectionGene value = null; ConnectionGene connection1 = (genome1.GetConnections().TryGetValue(i, out value))?genome1.GetConnections()[i]:null; ConnectionGene connection2 = (genome2.GetConnections().TryGetValue(i, out value))?genome2.GetConnections()[i]:null;; if (connection1 != null && connection2 != null) // both exist { matchingGenes++; weightDiff = Math.Abs(connection1.getWeight() - connection2.getWeight()); } else if ((connection1 == null) != (connection2 == null)) // only one exists { disjointGenes++; } } return(new float[] { matchingGenes, weightDiff / matchingGenes, excessGenes, disjointGenes }); }
public float GetActivation(float[] inputs) { if (type == TYPE.SENSOR) { return(inputs[id - 1]); } else { float summedActivation = 0; foreach (NodeGene node in directInNodes.Keys) { ConnectionGene connectionIn = directInNodes[node]; summedActivation += (connectionIn.getExpressed())? node.GetActivation(inputs) * connectionIn.getWeight() : 0; } return(History.Sigmoid(summedActivation)); } }
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); }