Exemple #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);
        }
Exemple #2
0
        public virtual void reset(int input_size, int output_size, int clients)
        {
            this.input_size  = input_size;
            this.output_size = output_size;
            this.max_clients = clients;

            all_connections.Clear();
            all_nodes.clear();
            this.clients.clear();

            for (int i = 0; i < input_size; i++)
            {
                NodeGene n = Node;
                n.X = 0.1;
                n.Y = (i + 1) / (double)(input_size + 1);
            }

            for (int i = 0; i < output_size; i++)
            {
                NodeGene n = Node;
                n.X = 0.9;
                n.Y = (i + 1) / (double)(output_size + 1);
            }

            for (int i = 0; i < max_clients; i++)
            {
                Client c = new Client();
                c.Genome = empty_genome();
                c.generate_calculator();
                this.clients.add(c);
            }
        }
        public void Make_Node_One_Point_To_Node_Two()
        {
            NodeGene n1 = new NodeGene(NodeGene.NodeType.HIDDEN, 0);
            NodeGene n2 = new NodeGene(NodeGene.NodeType.INPUT, 1);

            genome.makeN1PointToN2(ref n1, ref n2);
            Assert.AreEqual(NodeGene.NodeType.HIDDEN, n2.getNodeType());
        }
        public void Get_Different_Node_From_Nodes()
        {
            NodeGene n1 = genome.getNodes()[3];

            if (genome.getDifferentNode(random, n1, genome.getNodes()) != n1 && genome.getDifferentNode(random, n1, genome.getNodes()) != null)
            {
                Assert.Pass();
            }
            Assert.Fail();
        }
Exemple #5
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);
        }
Exemple #6
0
 public void makeN1PointToN2(ref NodeGene n1, ref NodeGene n2)
 {
     if (n1.getNodeType() == NodeGene.NodeType.HIDDEN && n2.getNodeType() == NodeGene.NodeType.INPUT ||
         n1.getNodeType() == NodeGene.NodeType.OUTPUT && n2.getNodeType() == NodeGene.NodeType.INPUT ||
         n1.getNodeType() == NodeGene.NodeType.OUTPUT && n2.getNodeType() == NodeGene.NodeType.HIDDEN)
     {
         NodeGene temp = n1;
         n1 = n2;
         n2 = temp;
     }
 }
Exemple #7
0
        public virtual int getReplaceIndex(NodeGene node1, NodeGene node2)
        {
            ConnectionGene con  = new ConnectionGene(node1, node2);
            ConnectionGene data = all_connections[con];

            if (data == null)
            {
                return(0);
            }
            return(data.ReplaceIndex);
        }
Exemple #8
0
        public virtual ConnectionGene getConnection(NodeGene node1, NodeGene node2)
        {
            ConnectionGene connectionGene = new ConnectionGene(node1, node2);

            if (all_connections.ContainsKey(connectionGene))
            {
                connectionGene.Innovation_number = all_connections[connectionGene].Innovation_number;
            }
            else
            {
                connectionGene.Innovation_number = all_connections.Count + 1;
                all_connections[connectionGene]  = connectionGene;
            }

            return(connectionGene);
        }
        // matching genes are inherited randomly, disjoint and excess (unmatching genes) genes are inherited from the more fit parent
        // parent1 is the more fit parent
        public static Genome crossover(Random random, Genome parent1, Genome parent2)
        {
            Genome offspring = new Genome();

            /**
             * NodeGene Crossover
             */

            // add NodeGenes from parent1 (fittest parent) to the offspring
            foreach (KeyValuePair <int, NodeGene> n in parent1.getNodes())
            {
                NodeGene oNode = new NodeGene(n.Value);
                offspring.addNodeGene(oNode);
            }

            /**
             * ConnectionGene Crossover
             */

            foreach (KeyValuePair <int, ConnectionGene> p1Gene in parent1.getConnections())
            {
                // when parent connection genes match one must be inherited by the offspring randomly
                if (parent2.getConnections().ContainsKey(p1Gene.Key))
                {
                    if (random.Next(100) <= 50)
                    {
                        // takes a copy of parent2 gene
                        offspring.addConnectionGene(new ConnectionGene(parent2.getConnections()[p1Gene.Key]));
                    }
                    else
                    {
                        //takes a copy of parent1 gene
                        offspring.addConnectionGene(new ConnectionGene(p1Gene.Value));
                    }
                }
                else   // if they don't match (disjoint or excess), copy from more fit parent

                {
                    offspring.addConnectionGene(new ConnectionGene(p1Gene.Value));
                }
            }

            return(offspring);
        }
Exemple #10
0
 public void addNodeGene(NodeGene node)
 {
     nodes.Add(node.getNodeNumber(), node);
     addNodeNumber(node.getNodeNumber());
 }
Exemple #11
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);
        }
Exemple #12
0
 public virtual void setReplaceIndex(NodeGene node1, NodeGene node2, int index)
 {
     all_connections[new ConnectionGene(node1, node2)].ReplaceIndex = index;
 }
 public NodeGene(NodeGene another)
 {
     this.type       = another.type;
     this.nodeNumber = another.nodeNumber;
 }