示例#1
0
        public void addNodeMutation(Random random)
        {
            // pick a random index
            int splitGeneIndex = innovationNumbers[random.Next(innovationNumbers.Count)];

            ConnectionGene c = connections[splitGeneIndex];

            c.setEnabled(false);

            NodeGene n1 = nodes[c.getIn()];
            NodeGene n2 = nodes[c.getOut()];

            // new node
            NodeGene n3 = new NodeGene(NodeGene.NodeType.HIDDEN, nodes.Count);

            // add connection genes (1) leading into the new node gets a weight of 1 (2) leading out of new node gets the same weight as old connection
            InnovationUtility.incrementGlobalInnovation();
            addConnectionGene(new ConnectionGene(n1.getNodeNumber(), n3.getNodeNumber(), 1f, true, InnovationUtility.getGlobalInnovation()));

            InnovationUtility.incrementGlobalInnovation();
            addConnectionGene(new ConnectionGene(n3.getNodeNumber(), n2.getNodeNumber(), c.getWeight(), true, InnovationUtility.getGlobalInnovation()));
            connections [splitGeneIndex] = c;

            // add node gene
            addNodeGene(n3);
        }
示例#2
0
        // ensures n1 and n2 are different
        public NodeGene getDifferentNode(Random random, NodeGene n1, Dictionary <int, NodeGene> nodes)
        {
            NodeGene n2 = nodes[nodeNumbers[random.Next(nodeNumbers.Count)]];

            while (n2.getNodeNumber() == n1.getNodeNumber())
            {
                n2 = nodes[nodeNumbers[random.Next(nodeNumbers.Count)]];
            }

            return(n2);
        }
示例#3
0
 public void addNodeGene(NodeGene node)
 {
     nodes.Add(node.getNodeNumber(), node);
     addNodeNumber(node.getNodeNumber());
 }
示例#4
0
        public bool addConnectionMutation(Random random)
        {
            bool     uniqueMatch = false;
            NodeGene n1          = null;
            NodeGene n2          = null;

            if (allConnectionsMade())
            {
                return(false);
            }

            // to be used in circular loop checker
            Utility.nodeConnections.Clear();
            foreach (KeyValuePair <int, ConnectionGene> c in connections)
            {
                //if key already exists add to hashset
                if (Utility.nodeConnections.ContainsKey(c.Value.getOut()))
                {
                    Utility.nodeConnections[c.Value.getOut()].Add(c.Value.getIn());
                }
                else
                {
                    Utility.nodeConnections.Add(c.Value.getOut(), new HashSet <int>()
                    {
                        c.Value.getIn()
                    });
                }
            }

            while (!uniqueMatch)
            {
                uniqueMatch = true;
                n1          = nodes[nodeNumbers[random.Next(nodeNumbers.Count)]];
                n2          = getDifferentNode(random, n1, nodes);

                if (circularConnectionFound(n1.getNodeNumber(), n2.getNodeNumber()))
                {
                    return(false);
                }

                // if n1 and n1 aren't INPUT nodes
                if (!(n1.getNodeType() == NodeGene.NodeType.INPUT && n2.getNodeType() == NodeGene.NodeType.INPUT))
                {
                    makeN1PointToN2(ref n1, ref n2);

                    foreach (KeyValuePair <int, ConnectionGene> c in connections)
                    {
                        // checks to see if connection already exists
                        if (c.Value.getIn() == n1.getNodeNumber() && c.Value.getOut() == n2.getNodeNumber())
                        {
                            uniqueMatch = false;
                        }
                    }
                }
                else
                {
                    uniqueMatch = false;
                }
            }

            InnovationUtility.incrementGlobalInnovation();
            addConnectionGene(new ConnectionGene(n1.getNodeNumber(), n2.getNodeNumber(), getRandomFloatBetweenPoints(random, -1.0, 1.0), true, InnovationUtility.getGlobalInnovation()));

            return(true);
        }