Exemplo n.º 1
0
        /**
         * Disable random ConnectionGene and add a new NodeGene in the middle
         */
        public void AddNodeMutation(Counter nodeInnovation, Counter connectionInnovation)
        {
            int listSize     = Connections.Count;
            int randomConKey = Random.Range(0, listSize - 1);

            while (!Connections.ContainsKey(randomConKey))
            {
                randomConKey = Random.Range(0, listSize - 1);
            }

            ConnectionGene con = Connections[randomConKey];

            NodeGene inNode  = Nodes[con.InNode];
            NodeGene outNode = Nodes[con.OutNode];

            con.Expressed = false;

            NodeGene       newNode  = new NodeGene(NodeGene.NodeType.HIDDEN, nodeInnovation.GetInnovation());
            ConnectionGene inToNew  = new ConnectionGene(inNode.ID, newNode.ID, 1f, true, connectionInnovation.GetInnovation());
            ConnectionGene newToOut = new ConnectionGene(newNode.ID, outNode.ID, con.Weight, true, connectionInnovation.GetInnovation());

            //Nodes.Add(newNode.ID, newNode);
            AddNodeGene(newNode);
            //Connections.Add(inToNew.Innovation, inToNew);
            AddConnectionGene(inToNew);
            //Connections.Add(newToOut.Innovation, newToOut);
            AddConnectionGene(newToOut);
        }
Exemplo n.º 2
0
 public void AddNodeGene(NodeGene gene)
 {
     Nodes.Add(gene.ID, gene);
 }
Exemplo n.º 3
0
        public void AddConnectionMutation(Counter innovation, int maxAttempts)
        {
            int  tries   = 0;
            bool success = false;

            while (tries < maxAttempts && success == false)
            {
                tries++;


                int listSize = Nodes.Count;

                int randomNodeKey1 = Random.Range(0, listSize - 1);
                int randomNodeKey2 = Random.Range(0, listSize - 1);

                while (!Nodes.ContainsKey(randomNodeKey1) || !Nodes.ContainsKey(randomNodeKey2))
                {
                    randomNodeKey1 = Random.Range(0, listSize - 1);
                    randomNodeKey2 = Random.Range(0, listSize - 1);
                }

                NodeGene node1 = Nodes[randomNodeKey1];
                NodeGene node2 = Nodes[randomNodeKey2];

                bool reversed = false;
                if (node1.GetType().Equals(NodeGene.NodeType.HIDDEN) && node2.GetType().Equals(NodeGene.NodeType.INPUT))
                {
                    reversed = true;
                }
                else if (node1.GetType().Equals(NodeGene.NodeType.OUTPUT) && node2.GetType().Equals(NodeGene.NodeType.HIDDEN))
                {
                    reversed = true;
                }
                else if (node1.GetType().Equals(NodeGene.NodeType.OUTPUT) && node2.GetType().Equals(NodeGene.NodeType.INPUT))
                {
                    reversed = true;
                }

                bool connectionImpossible = false;
                if (node1.GetType().Equals(NodeGene.NodeType.INPUT) && node2.GetType().Equals(NodeGene.NodeType.INPUT))
                {
                    connectionImpossible = true;
                }
                else if (node1.GetType().Equals(NodeGene.NodeType.OUTPUT) && node2.GetType().Equals(NodeGene.NodeType.OUTPUT))
                {
                    connectionImpossible = true;
                }

                bool connectionExists = false;
                foreach (ConnectionGene con in Connections.Values)
                {
                    if (con.InNode == node1.ID && con.OutNode == node2.ID)
                    {
                        connectionExists = true;
                        break;
                    }
                    else if (con.InNode == node2.ID && con.OutNode == node1.ID)
                    {
                        connectionExists = true;
                        break;
                    }
                }

                if (connectionExists || connectionImpossible)
                {
                    continue;
                }

                ConnectionGene newCon = new ConnectionGene(reversed ? node2.ID : node1.ID, reversed ? node1.ID : node2.ID, Random.Range(0f, 1f), true, innovation.GetInnovation());
                //Connections.Add(newCon.Innovation, newCon);
                AddConnectionGene(newCon);
                success = true;
            }
            if (success == false)
            {
                Debug.Log("Tried, but could not add more connections");
            }
        }
Exemplo n.º 4
0
 public NodeGene(NodeGene gene)
 {
     this.Type = gene.Type;
     this.ID = gene.ID;
     this.sum = 0;
 }