Пример #1
0
    public void AddConnectionMutation()
    {
        if (neat.maxConnections <= connections.Count)
        {
            return;
        }

        for (int tries = 0; tries < atempts; tries++)             //try to find places in the network to add connections
        {
            NodeGene node1 = nodes[Random.Range(0, nodes.Count)]; //pick 2 random nodes
            NodeGene node2 = nodes[Random.Range(0, nodes.Count)];

            bool reverse = false;

            if (node1.GetLayer() > node2.GetLayer())
            {
                reverse = true;
            }


            if (reverse)
            {//swap nodes
                NodeGene temp = node1;
                node1 = node2;
                node2 = temp;
            }
            bool connectionExists = false;
            if (node1.GetLayer() == node2.GetLayer())
            {
                connectionExists = true;
            }
            for (int i = 0; i < connections.Count; i++)
            {//check if connection exists
                if (connections[i].GetInNode() == node1 && connections[i].GetOutNode() == node2)
                {
                    connectionExists = true;
                    break;//or call method again
                }
            }
            // if (node1.GetLayer() >= node2.GetLayer()) {
            // connectionExists = true;
            // }

            if (!connectionExists)
            {
                connections.Add(new ConnectionGene(node1, node2, Random.Range(-1.0f, 1.0f), true, track.Innovate()));
                return;
            }
        }
    }
Пример #2
0
    public void AddNodeMutation()
    {
        if (neat.maxNeurones <= nodes.Count)
        {
            return;
        }

        ConnectionGene connectionSplit = connections[Random.Range(0, connections.Count)];//choose random connection to split

        connectionSplit.Disable();



        NodeGene nodeIn = connectionSplit.GetInNode();

        NodeGene nodeOut = connectionSplit.GetOutNode();

        //Debug.Log("nin" + nodeIn.GetLayer());
        //Debug.Log("nout"+ nodeOut.GetLayer());
        int newLayer;

        if (nodeOut.GetLayer() - nodeIn.GetLayer() == 1)
        {
            //Debug.Log(nodeIn.GetLayer());
            //Debug.Log(nodeOut.GetLayer());
            newLayer = nodeOut.GetLayer();
            AddLayer(nodeOut.GetLayer());
            //Debug.Log("new layer" + nodeOut.GetLayer());
            //newLayer = nodeOut.GetLayer();
        }
        else
        {
            newLayer = Random.Range(nodeIn.GetLayer() + 1, nodeOut.GetLayer() - 1);
        }


        NodeGene node = new NodeGene(NodeGene.NODETYPE.HIDDEN, track.NewId(), newLayer, Random.Range(-1.0f, 1.0f), neat.internalActiationFunction);

        nodes.Add(node);

        ConnectionGene connectionIn  = new ConnectionGene(connectionSplit.GetInNode().Copy(), node, 1.0f, true, track.Innovate());
        ConnectionGene connectionOut = new ConnectionGene(node, connectionSplit.GetOutNode().Copy(), connectionSplit.GetWeight(), true, track.Innovate());

        connections.Add(connectionIn);
        connections.Add(connectionOut);
    }
Пример #3
0
 public override string ToString()
 {
     return("innovation: " + innovation + "  Nodes: " + inNode.GetId() + " -> " + outNode.GetId() + "    weight: " + weight + "  expressed: " + expressed + "      Layer: " + inNode.GetLayer() + "- > " + outNode.GetLayer() + "  nodeIn: " + inNode + "    Nodeout: " + outNode);
 }