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 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); }
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); } } } }