public (float, float) EvaluateNetwork(float[] input) { float[] output = new float[NEAT_CONFIGS.OUTPUTS]; for (int i = 1; i < NEAT_CONFIGS.INPUTS; i++) { GetNodeGenes()[i].SetValue(input[i - 1]); } foreach (var KeyValue in GetNodeGenes()) { double sum = 0; int key = KeyValue.Key; NodeGene node = KeyValue.Value; if (key > NEAT_CONFIGS.INPUTS) { foreach (ConnectionGene con in node.GetIncomingConnections()) { if (con.GetEnabled()) { sum += (GetNodeGenes()[con.GetInputNode()].GetValue() * con.GetWeight()); } } node.SetValue(Math.Tanh(sum)); } } int j = 0; foreach (NodeGene node in GetNodeGenes().Values) { if (node.GetType() == NodeGene.TYPE.OUTPUT) { output[j] = (float)node.GetValue(); j++; } } return(output[0], output[1]); }
public void MutateAddConnection() { List <int> Keys = new List <int>(NodeGenes.Keys); int randomKey1 = Keys[random.Next(Keys.Count)]; int randomKey2 = Keys[random.Next(Keys.Count)]; NodeGene node1 = NodeGenes[randomKey1]; NodeGene node2 = NodeGenes[randomKey2]; int ATTEMPTS = 0; while ((node1.GetType() == NodeGene.TYPE.OUTPUT && node2.GetType() == NodeGene.TYPE.OUTPUT || node1.GetType() == NodeGene.TYPE.INPUT && node2.GetType() == NodeGene.TYPE.INPUT) || node1.GetID() == node2.GetID() || (node1.GetType() == NodeGene.TYPE.BIAS && node2.GetType() == NodeGene.TYPE.INPUT || node2.GetType() == NodeGene.TYPE.BIAS && node1.GetType() == NodeGene.TYPE.INPUT) || ATTEMPTS < 10) { randomKey1 = Keys[random.Next(Keys.Count)]; node1 = NodeGenes[randomKey1]; ATTEMPTS++; } if (ATTEMPTS < 10) { bool NeedsReversing = false; if ((node1.GetType() == NodeGene.TYPE.HIDDEN && node2.GetType() == NodeGene.TYPE.INPUT) || (node1.GetType() == NodeGene.TYPE.HIDDEN && node2.GetType() == NodeGene.TYPE.BIAS)) { NeedsReversing = true; } else { if (node1.GetType() == NodeGene.TYPE.OUTPUT && node2.GetType() == NodeGene.TYPE.INPUT || node1.GetType() == NodeGene.TYPE.OUTPUT && node2.GetType() == NodeGene.TYPE.BIAS || node1.GetType() == NodeGene.TYPE.OUTPUT && node2.GetType() == NodeGene.TYPE.HIDDEN) { NeedsReversing = true; } } bool ExistsInGenome = false; foreach (ConnectionGene connection in GetConnectionGenes()) { if (connection.GetInputNode() == node1.GetID() && connection.GetOutputNode() == node2.GetID()) { ExistsInGenome = true; break; } else { if (connection.GetOutputNode() == node1.GetID() && connection.GetInputNode() == node2.GetID()) { ExistsInGenome = true; } } } if (!ExistsInGenome) { bool ExistsInPopulation = false; foreach (ConnectionGene connection in PoolManager.TOTALCONNECTIONSMADETHISGENERATION) { if (connection.GetInputNode() == node1.GetID() && connection.GetOutputNode() == node2.GetID() || connection.GetOutputNode() == node1.GetID() && connection.GetInputNode() == node2.GetID()) { ExistsInPopulation = true; ConnectionGene newConnection = new ConnectionGene(NeedsReversing ? node2.GetID() : node1.GetID(), NeedsReversing ? node1.GetID() : node2.GetID(), random.NextDouble(), true, connection.GetInnovation()); ConnectionGenes.Add(newConnection); GetNodeGenes()[newConnection.GetOutputNode()].AddIncomingConnection(newConnection); break; } } if (!ExistsInPopulation) { ConnectionGene newConnection = new ConnectionGene(NeedsReversing ? node2.GetID() : node1.GetID(), NeedsReversing ? node1.GetID() : node2.GetID(), random.NextDouble(), true, NEAT_CONFIGS.GLOBAL_INNOVATION_NUMBER++); ConnectionGenes.Add(newConnection); GetNodeGenes()[newConnection.GetOutputNode()].AddIncomingConnection(newConnection); PoolManager.TOTALCONNECTIONSMADETHISGENERATION.Add(newConnection); } } } }