public TWEANNGenotype(TWEANN tweann) { numInputs = tweann.NumInputs(); numOutputs = tweann.NumOutputs(); archetypeIndex = tweann.ArchetypeIndex; ID = EvolutionaryHistory.NextGenotypeID(); Links = new List <LinkGene>(); Nodes = new List <NodeGene>(tweann.GetNodes().Count); List <TWEANNNode> tweannNodeList = tweann.GetNodes(); for (int i = 0; i < tweann.GetNodes().Count; i++) { NodeGene ng = new NodeGene(tweannNodeList[i].GetNType(), tweannNodeList[i].GetFType(), tweannNodeList[i].GetInnovation()); Nodes.Add(ng); List <LinkGene> tempLinks = new List <LinkGene>(); foreach (TWEANNLink l in tweannNodeList[i].GetOutputs()) { LinkGene lg = new LinkGene(tweannNodeList[i].GetInnovation(), l.GetTarget().GetInnovation(), l.GetWeight(), l.GetInnovation()); tempLinks.Add(lg); } for (int j = 0; j < tempLinks.Count; j++) { Links.Add(tempLinks[j]); } } }
private void ActivationFunctionMutation() { string debugMsg = "ActivationFunctionMutation"; int nodeRoll = Random.Range(0, Nodes.Count); NodeGene ng = Nodes[nodeRoll]; if (ng == null) { throw new System.Exception("Node not found! " + nodeRoll); } FTYPE fTYPERoll = ActivationFunctions.RandomFTYPE(); if (fTYPERoll != ng.fTYPE) { debugMsg += " - Changing node " + nodeRoll + " from " + ng.GetFTYPE() + " to " + fTYPERoll; ng.SetFTYPE(fTYPERoll); } else { debugMsg += " - Not changing node. Random node selection was same as previous function type"; } if (ArtGallery.DEBUG_LEVEL > ArtGallery.DEBUG.NONE) { Debug.Log(debugMsg); } }
public Genome(Genome genome) { ID = genome.ID; perceptrons = new List <NodeGene>(); for (int i = 0; i < genome.perceptrons.Count; i++) { NodeGene curGene = genome.perceptrons[i]; perceptrons.Add(new NodeGene(curGene.type, curGene.ID, curGene.splitValues, curGene.actResponse, curGene.recurrent)); } connections = new List <ConnectionGene>(); for (int i = 0; i < genome.connections.Count; i++) { ConnectionGene curGene = genome.connections[i]; connections.Add(new ConnectionGene(curGene.from, curGene.to, curGene.enabled, curGene.innovNum, curGene.weight, curGene.recurrent)); } inputs = genome.inputs; outputs = genome.outputs; fitness = genome.fitness; adjustedFitness = genome.adjustedFitness; amountToSpawn = genome.amountToSpawn; species = genome.species; mutPar = genome.mutPar; }
/// <summary> /// Adds Connection (Structural Mutation) to Genome from Nodes /// </summary> /// <param name="nodeFrom">From-Node for Connection</param> /// <param name="nodeTo">To-Node for Connection</param> internal void MutateAddConnection(NodeGene nodeFrom, NodeGene nodeTo) { if (!nodeFrom.IsValid) { throw new ArgumentException("NodeFrom is Invalid", "nodeFrom"); } if (!nodeTo.IsValid) { throw new ArgumentException("NodeTo is Invalid", "nodeTo"); } if (nodeFrom.Equals(nodeTo)) { throw new ArgumentException("Cannot create Connection with a single Node"); } if (!Nodes.ContainsKey(nodeFrom.Innovation)) { throw new ArgumentException("Node does not exist in Genome", "nodeFrom"); } if (!Nodes.ContainsKey(nodeTo.Innovation)) { throw new ArgumentException("Node does not exist in Genome", "nodeTo"); } if (nodeFrom.Type == NodeType.OUTPUT || nodeTo.Type == NodeType.INPUT) { throw new InvalidOperationException($"Invalid NodeTypes for Connection. Type NodeFrom: {nodeFrom.Type} Type NodeTo: {nodeTo.Type}"); } // TODO: Check if Connection does not exist already in Genome AddConnection(MutationFactory.GetConnection(nodeFrom, nodeTo)); }
public void SetGenome(int ID, List <NodeGene> perceptrons, List <ConnectionGene> connections, int inputs, int outputs, MutParameters mutPar) { nodeGeneIndex = 0; connectionGeneIndex = 0; this.ID = ID; for (int i = 0; i < perceptrons.Count; i++) { NodeGene curGene = perceptrons[i]; AddNodeGene(curGene); } for (int i = 0; i < connections.Count; i++) { ConnectionGene curGene = connections[i]; AddConnectionGeneGene(curGene); } FinishGenome(); this.inputs = inputs; this.outputs = outputs; fitness = 0; adjustedFitness = 0; amountToSpawn = 0; species = 0; this.mutPar = mutPar; }
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); }
public void Distance_ManyNodes_NoConnections_2to1() { if (!Genome.IsInitialized()) { Genome.Init(); } NodeGene nodeGene_1 = new NodeGene(1, Node.INPUT_X, Neural_Network.Node.Sigmoid); NodeGene nodeGene_2 = new NodeGene(2, Node.INPUT_X, Neural_Network.Node.Sigmoid); NodeGene nodeGene_3 = new NodeGene(1, Node.INPUT_X, Neural_Network.Node.Sigmoid); NodeGene nodeGene_4 = new NodeGene(2, Node.INPUT_X, Neural_Network.Node.Sigmoid); Random random = new Random(); Genome genome_1 = new Genome(random); genome_1.NodeGenes.Add(nodeGene_1.InnovationNumber, nodeGene_1); genome_1.NodeGenes.Add(nodeGene_2.InnovationNumber, nodeGene_2); Genome genome_2 = new Genome(random); genome_2.NodeGenes.Add(nodeGene_3.InnovationNumber, nodeGene_3); genome_2.NodeGenes.Add(nodeGene_4.InnovationNumber, nodeGene_4); Assert.AreEqual(0, genome_2.Distance(genome_1)); }
/// <summary> /// Adds Connection (Structural Mutation) to Genome from Nodes /// </summary> /// <param name="nodeFrom">From-Node (Innovation-Number) for Connection</param> /// <param name="nodeTo">To-Node (Innovation-Number) for Connection</param> internal void MutateAddConnection(ulong nodeFrom, ulong nodeTo) { if (nodeFrom == 0) { throw new ArgumentException("NodeInnovation cannot be 0", "nodeFrom"); } if (nodeTo == 0) { throw new ArgumentException("NodeInnovation cannot be 0", "nodeTo"); } if (nodeFrom == nodeTo) { throw new ArgumentException("Cannot create Connection with a single Node"); } if (!Nodes.ContainsKey(nodeFrom)) { throw new ArgumentException("Node does not exist in Genome", "nodeFrom"); } if (!Nodes.ContainsKey(nodeTo)) { throw new ArgumentException("Node does not exist in Genome", "nodeTo"); } NodeGene from = Nodes[nodeFrom]; NodeGene to = Nodes[nodeTo]; if (from.Type == NodeType.OUTPUT || to.Type == NodeType.INPUT) { throw new InvalidOperationException($"Invalid NodeTypes for Connection. Type NodeFrom: {from.Type} Type NodeTo: {to.Type}"); } // TODO: Check if Connection does not exist already in Genome AddConnection(MutationFactory.GetConnection(from, to)); }
/// <summary> /// Test if a connection between these two nodes is possible. /// This includes a check if the connection is already existing /// </summary> /// <param name="inNode">the node where the connection starts</param> /// <param name="outNode">the node where the connection ends</param> /// <returns>true if the connection is possible</returns> public bool IsConnectionPossible(NodeGene inNode, NodeGene outNode) { //Check if a node is invalid if (outNode.Type == NodeGeneType.INPUT) { return(false); } if (inNode.Type == NodeGeneType.OUTPUT) { return(false); } //TODO recurrent check if (inNode.XValue >= outNode.XValue) { return(false); } //Check fpr an existing connection foreach (ConnectionGene connection in _connections.Values) { if (connection.InNode == inNode.ID && connection.OutNode == outNode.ID) { return(false); } } return(true); }
public void SetGenome(Genome genome) { nodeGeneIndex = 0; connectionGeneIndex = 0; ID = genome.ID; for (int i = 0; i < genome.perceptrons.Count; i++) { NodeGene curGene = genome.perceptrons[i]; AddNodeGene(curGene); } for (int i = 0; i < genome.connections.Count; i++) { ConnectionGene curGene = genome.connections[i]; AddConnectionGeneGene(curGene); } FinishGenome(); inputs = genome.inputs; outputs = genome.outputs; fitness = 0; adjustedFitness = 0; amountToSpawn = 0; species = 0; mutPar = genome.mutPar; }
public void AddConnectionMutation_Test() { NodeGene node1 = parent1Genome.Nodes[3]; NodeGene node2 = parent1Genome.Nodes[5]; //Test list size before modifying Assert.AreEqual(parent1Genome.Connections.Values.Count, 6); //Add the connection ConnectionGene newConnection = parent1Genome.AddConnectionMutation(node1, node2, 11); //Test if a connection was added to the list Assert.AreEqual(parent1Genome.Connections.Values.Count, 7); Assert.NotNull(newConnection); //Test if connection is added to the list Assert.AreEqual(newConnection, parent1Genome.Connections[newConnection.InnovationNumber]); //Test if connection has correct input node Assert.AreEqual(newConnection.InNode, node1.ID); Assert.AreEqual(newConnection.OutNode, node2.ID); Assert.GreaterOrEqual(newConnection.Weight, -1.0f); Assert.LessOrEqual(newConnection.Weight, 1.0f); Assert.AreEqual(11, newConnection.InnovationNumber); }
/// <summary> /// Create the start genome /// </summary> private void SetStartGenome() { _nodeCounter = new GeneCounter(0); _connectionCounter = new GeneCounter(0); _startGenome = new Genome(); int amountPlayerInputs = PlayerController.GetAmountOfInputs(_levelViewWidht, _levelViewHeight); List <NodeGene> tmpInputNodes = new List <NodeGene>(); //Crate input nodes for (int i = 0; i < amountPlayerInputs; i++) { NodeGene node = new NodeGene(_nodeCounter.GetNewNumber(), NodeGeneType.INPUT, 0f, ActivationFunctionHelper.Function.SIGMOID); _startGenome.AddNodeGene(node); tmpInputNodes.Add(node); } //Create output nodes NodeGene outputNode1 = new NodeGene(_nodeCounter.GetNewNumber(), NodeGeneType.OUTPUT, 1f, ActivationFunctionHelper.Function.SIGMOID); NodeGene outputNode2 = new NodeGene(_nodeCounter.GetNewNumber(), NodeGeneType.OUTPUT, 1f, ActivationFunctionHelper.Function.SIGMOID); _startGenome.AddNodeGene(outputNode1); _startGenome.AddNodeGene(outputNode2); //Create connections for (int i = 0; i < amountPlayerInputs; i++) { _startGenome.AddConnectionGene(new ConnectionGene(tmpInputNodes[i].ID, outputNode1.ID, Random.Range(-1f, 1f), true, _connectionCounter.GetNewNumber())); _startGenome.AddConnectionGene(new ConnectionGene(tmpInputNodes[i].ID, outputNode2.ID, Random.Range(-1f, 1f), true, _connectionCounter.GetNewNumber())); } }
/// <summary> /// Create a fresh start for an <see cref="Neat"/> object /// </summary> private void Reset() { AllConnections.Clear(); AllNodes.Clear(); AllClients.Clear(); for (int i = 0; i < Constants.InputSize; i++) { NodeGene node = CreateNode(); node.X = 0.1;; node.Y = (i + 1) / (double)(Constants.InputSize + 1); } for (int i = 0; i < Constants.OutputSize; i++) { NodeGene node = CreateNode(); node.X = 0.9;; node.Y = (i + 1) / (double)(Constants.OutputSize + 1); ActivationEnumeration a = ActivationEnumeration.Random(); node.Activation = a.Activation; node.ActivationName = a.Name; } for (int i = 0; i < Constants.MaxClients; i++) { Client c = new Client(EmptyGenome()); AllClients.Add(c); } }
public void AddInNode(NodeGene node, ConnectionGene connection) { if (!directInNodes.Keys.Contains(node)) { directInNodes.Add(node, connection); } }
///<inheritdoc/> public NodeGene CreateNode() { NodeGene node = new NodeGene(AllNodes.Count + 1); AllNodes.Add(node); return(node); }
public NodeGene GetNode() { var n = new NodeGene(_allNodes.Count + 1); _allNodes.Add(n); return(n); }
public void Distance_Self_TwoNodes_1Connection() { if (!Genome.IsInitialized()) { Genome.Init(); } NodeGene nodeGene_1 = new NodeGene(1, Node.INPUT_X, Neural_Network.Node.Sigmoid); NodeGene nodeGene_2 = new NodeGene(2, Node.INPUT_X, Neural_Network.Node.Sigmoid); ConnectionGene connectionGene = new ConnectionGene(nodeGene_1, nodeGene_2, 1); Random random = new Random(); Genome genome_1 = new Genome(random); genome_1.NodeGenes.Add(nodeGene_1.InnovationNumber, nodeGene_1); genome_1.NodeGenes.Add(nodeGene_2.InnovationNumber, nodeGene_2); genome_1.ConnectionGenes.Add(connectionGene.InnovationNumber, connectionGene); Assert.AreEqual(0, genome_1.Distance(genome_1)); }
/// <summary> /// Sets up variables used during Calculation of Output (to speed up calculation) /// </summary> private void SetUpNetwork() { int numOutputs = 0; nodesInOrder = Nodes.Keys.ToList(); nodesInOrder.Sort(); for (int i = 0; i < nodesInOrder.Count; i++) { if (Nodes[nodesInOrder[i]].Type == NodeType.OUTPUT) { numOutputs++; } } outputCache = new double[numOutputs]; foreach (ConnectionGene connection in Connections.Values) { NodeGene node = connection.Out; if (!connection.Expressed) { continue; } if (inputConnectionsPerNode.ContainsKey(node)) { inputConnectionsPerNode[node].Add(connection); } else { inputConnectionsPerNode.Add(node, new List <ConnectionGene> { connection }); } } }
public ConnectionGene(NodeGene inNode, NodeGene outNode, float weight, bool expressed, int innovation) { this.inNode = inNode; this.outNode = outNode; this.weight = weight; this.expressed = expressed; this.innovation = innovation; }
public ConnectionGene(NodeGene inNode, NodeGene outNode, bool expressed, int innovation) { this.inNode = inNode; this.outNode = outNode; weight = Random.Range(-neat.weightRange, neat.weightRange); this.expressed = expressed; this.innovation = innovation; }
public Node(NodeGene gene, double width, double height) { InitializeComponent(); SetValue(Canvas.LeftProperty, gene.X * width); SetValue(Canvas.TopProperty, gene.Y * height); node.Fill = NodeColors.SetColor(gene.ActivationName); }
/// <summary> /// Creates a new NodeGene to track and adds it to the tracker. /// </summary> /// <returns>The created NodeGene.</returns> public NodeGene CreateNode() { NodeGene nodeGene = new NodeGene(all_nodes.Count + 1); //0 is wasted, eh all_nodes.Add(nodeGene); return(nodeGene); }
public void SetGene(NodeGene gene) { this.ID = gene.ID; this.type = gene.type; this.recurrent = gene.recurrent; this.splitValues = gene.splitValues; this.actResponse = gene.actResponse; }
/// <summary> /// Copy an existing NodeGene. Only the id and the type will be copied. /// </summary> /// <param name="nodeGene"></param> public NodeGene(NodeGene nodeGene) { _id = nodeGene.ID; _type = nodeGene.Type; _typeOfFunction = nodeGene.TypeOfActivationFunction; _xValue = nodeGene.XValue; _inputs = new List <ConnectionGene>(); }
public Node(NodeGene nodeGene) { this.id = nodeGene.id; this.activation = nodeGene.activation; this.type = nodeGene.type; value = 0f; inConnections = new List <Connection>(); outConnections = new List <Connection>(); }
/// <summary> /// Constructs a new innovation given an in node, out node, weight, and innovation number; it is enabled by default. /// </summary> /// <param name="inNode">The node at the beginning of this connection.</param> /// <param name="outNode">The node at the end of this connection.</param> /// <param name="weight">The weight of this connection.</param> /// <param name="innovation">The unique innovation number for this connection.</param> /// <param name="recurrent">True if the in node is in a layer after the out node.</param> public ConnectionGene(NodeGene inNode, NodeGene outNode, float weight, int innovation, bool recurrent) { InNode = inNode; OutNode = outNode; Weight = weight; Enabled = true; Innovation = innovation; Recurrent = recurrent; }
public ConnectionGene(NodeGene inNode, NodeGene outNode, double weight, int innovation) { this.inNode = inNode; this.outNode = outNode; this.weight = weight; this.innovation = innovation; outNode.inputs.Add(inNode); outNode.weights.Add(weight); }
public void Crossover_Self_1Connection() { if (!Genome.IsInitialized()) { Genome.Init(); } Random random = new Random(1); int nodeGene_num = 1; NodeGene nodeGene_1 = new NodeGene(nodeGene_num++, Node.INPUT_X, Node.Sigmoid); NodeGene nodeGene_2 = new NodeGene(nodeGene_num++, Node.OUTPUT_X, Node.Sigmoid); ConnectionGene connectionGene = new ConnectionGene(nodeGene_1, nodeGene_2, 4); Genome genome = new Genome(random); genome.NodeGenes.Add(nodeGene_1.InnovationNumber, nodeGene_1); genome.NodeGenes.Add(nodeGene_2.InnovationNumber, nodeGene_2); genome.ConnectionGenes.Add(connectionGene.InnovationNumber, connectionGene); Genome created_genome = genome.Crossover(genome); bool[] contains_genes = { false, false, false }; foreach (ConnectionGene cgene in created_genome.ConnectionGenes.Values) { if (cgene == connectionGene) { contains_genes[0] = true; break; } } foreach (NodeGene ngene in created_genome.NodeGenes.Values) { if (ngene == nodeGene_1) { contains_genes[1] = true; } else if (ngene == nodeGene_2) { contains_genes[2] = true; } } Assert.AreEqual(3, contains_genes.Count(x => x == true)); }
public Innovation(NodeGene node, int ID, int percepID) { this.ID = ID; this.percepID = percepID; splitValues = node.splitValues; percepType = node.type; percepIn = -1; percepOut = -1; }
public ConnectionGene(NodeGene inNode, NodeGene outNode, int innovation) { this.inNode = inNode; this.outNode = outNode; neat = GameObject.Find("Scripts").GetComponent <Neat>(); weight = Random.Range(-neat.weightRange, neat.weightRange); expressed = true; this.innovation = innovation; }