public void AddNodeGene(NodeGene toAdd) { if (!NodeGenes.ContainsKey(toAdd.Id)) { NodeGenes.Add(toAdd.Id, toAdd); } }
public void AddNodeGene(NodeGene toAdd, int index) { if (!NodeGenes.ContainsKey(index)) { NodeGenes.Add(index, toAdd); } }
private double CalculateValue(NodeGenes nodeGene, int timesRun) { timesRun++; if (nodeGene.valueSet) { return(nodeGene.value); } if (nodeGene.nodeType == NodeType.Sensor) { throw new System.Exception("Sensor value error"); } if (timesRun > 10) { throw new System.Exception("Looping"); } List <ConnectGenes> connections = connectGenes.FindAll(x => x.outt == nodeGene.node); double value = 0; for (int j = 0; j < connections.Count; j++) { if (connections[j].enabled) { value += CalculateValue(nodeGenes.Find(x => x.node == connections[j].inn), timesRun) * connections[j].weight; } } nodeGene.SetValue(Sigmoid(value, 4.9)); return(nodeGene.value); }
public override void CreateBrain(BrainComponent brainComp, IMutatorConfig config) { var neatConfig = (NeatMutationConfig)config; // Add nodes for inputs and outputs foreach ((string namedInput, string _) in brainComp.InputMap) { // Note activation function on input nodes is not used. this.AddNamedNode(namedInput, NodeType.INPUT, ActivationFunctionType.SOFTSIGN); } foreach ((string namedOutput, string _) in brainComp.OutputMap) { this.AddNamedNode(namedOutput, NodeType.OUTPUT, (ActivationFunctionType)neatConfig.OutputActivationFunction); } // Increase connection innovation id as we loop int connInnovationId = 0; Random r = new Random(); double chanceToMakeConnection = 1d; foreach (var input in NodeGenes.FindAll((g) => g.NodeType == NodeType.INPUT || g.NodeType == NodeType.BIAS)) { foreach (var output in NodeGenes.FindAll((g) => g.NodeType == NodeType.OUTPUT)) { if (r.NextDouble() < chanceToMakeConnection) { // Add new connection gene ConnectionGenes.Add(new ConnectionGene(connInnovationId, input.InnovationId, output.InnovationId, r.Normal(0, 1.5))); connInnovationId++; } } } }
private void AddNodes(ConnectGenes checkGenes, Genome p2) { NodeGenes inNode = p2.nodeGenes.Find(x => x.node == checkGenes.inn); NodeGenes outNode = p2.nodeGenes.Find(x => x.node == checkGenes.outt); if (nodeGenes.FindAll(x => x.node == inNode.node).Count == 0) { nodeGenes.Add(new NodeGenes(inNode.node, inNode.nodeType)); } if (nodeGenes.FindAll(x => x.node == outNode.node).Count == 0) { nodeGenes.Add(new NodeGenes(outNode.node, outNode.nodeType)); } }
/// <summary> /// Make a clone of the genome /// </summary> /// <returns>The new genome</returns> public Genometype Clone() { // Copy the nodes NodeGenes[] newNodeGenes = new NodeGenes[nodeGenes.Length]; for (int i = 0; i < nodeGenes.Length; i++) { newNodeGenes[i] = nodeGenes[i]; } // Copy the connection ConnectionGenens[] newConnectionGenes = new ConnectionGenens[connectionGenes.Length]; for (int i = 0; i < connectionGenes.Length; i++) { newConnectionGenes[i] = connectionGenes[i]; } return(new Genometype(newNodeGenes, newConnectionGenes)); }