Ejemplo n.º 1
0
 public void ConnectBack(ChessNeuron chessNeuron, int transmitter)
 {
     for (int i = 0; i < possibleConnectionsIn.Count; i++)
     {
         //Debug.Log(possibleConnectionsIn);
         if (possibleConnectionsIn[i] == transmitter && connectionsIn[i] == null)
         {
             connectionsIn[i] = chessNeuron;
             break;
         }
     }
 }
Ejemplo n.º 2
0
 public void ConnectToNeuron(ChessNeuron chessNeuron, int transmitter)
 {
     for (int i = 0; i < possibleConnectionsOut.Count; i++)
     {
         if (possibleConnectionsOut[i] == transmitter && connectionsOut[i] == null)
         {
             connectionsOut[i] = chessNeuron;
             break;
         }
     }
     chessNeuron.ConnectBack(this, transmitter);
 }
Ejemplo n.º 3
0
    public ChessNeuron MakeNewNeuron(int transmitter)
    {
        for (int i = 0; i < connectionsOut.Count; i++)
        {
            if (possibleConnectionsOut[i] == transmitter && connectionsOut[i] == null)
            {
                connectionsOut[i] = new ChessNeuron(FetchGenes());

                return(connectionsOut[i]);
            }
        }
        return(null);
    }
Ejemplo n.º 4
0
    public ChessAI BuildNetworkFromGene(ChessAIDNA gene)
    {
        // Keep a list of all neurons that are available to make new connections (initialize at input neurons)
        // If the neuron is set to wait, let it wait, otherwise, find a neuron that can be connected
        // If there is no neuron available with the correct transmitter, make a new neuron with the settings from the gene

        ChessAI aI = new ChessAI(gene);

        aI.inputNeurons = new List <ChessNeuron>();

        List <ChessNeuron> availableOutNeurons = new List <ChessNeuron>();
        List <ChessNeuron> availableInNeurons  = new List <ChessNeuron>();
        List <ChessNeuron> outputNeurons       = new List <ChessNeuron>();
        List <ChessNeuron> allNeurons          = new List <ChessNeuron>();

        // First, make the list of output neurons.
        outputNeurons = MakeOutputNeurons();

        foreach (ChessDNA chessDNA in gene.DNA)
        {
            ChessNeuron tempNeuron = new ChessNeuron(chessDNA.chessGenes);
            tempNeuron.MakeInputNeuron();
            aI.inputNeurons.Add(tempNeuron);
            availableOutNeurons.Add(tempNeuron);
            allNeurons.Add(tempNeuron);
        }
        while (availableOutNeurons.Count > 0)
        {
            // Build the network
            List <ChessNeuron> nextAvailableList = new List <ChessNeuron>();
            foreach (ChessNeuron neuron in availableOutNeurons)
            {
                if (neuron.CheckIfAvailable())
                {
                    List <int> possibleOut = neuron.CheckPossibleOut();
                    foreach (int i in possibleOut)
                    {
                        ChessNeuron possibleConnection = CheckPossibleIns(i, availableInNeurons);
                        if (possibleConnection != null)
                        {
                            neuron.ConnectToNeuron(possibleConnection, i);
                            if (possibleConnection.CheckIfInConnectionsFull())
                            {
                                availableInNeurons.Remove(possibleConnection);
                            }
                        }
                        else
                        {
                            ChessNeuron tempNeuron = neuron.MakeNewNeuron(i);
                            allNeurons.Add(tempNeuron);

                            if (tempNeuron.GetNeuronType() == 1)
                            {
                                neuron.ForceConnect(tempNeuron);
                                if (!tempNeuron.CheckIfInConnectionsFull())
                                {
                                    availableInNeurons.Add(tempNeuron);
                                }
                                nextAvailableList.Add(tempNeuron);
                            }
                            else if (tempNeuron.GetNeuronType() == 2)
                            {
                                tempNeuron.MakePreOutputNeuron(outputNeurons);
                                if (!tempNeuron.CheckIfInConnectionsFull())
                                {
                                    availableInNeurons.Add(tempNeuron);
                                }
                            }
                            else
                            {
                                Debug.Log("Error: unexpected Neuron type?");
                            }
                        }
                    }
                }
                else
                {
                    // put the neuron in the nextavailableList with a position equal to its wait time
                    if (nextAvailableList.Count >= neuron.waitForConnection)
                    {
                        nextAvailableList.Insert(neuron.waitForConnection, neuron);
                        neuron.waitForConnection = 0;
                    }
                    else
                    {
                        nextAvailableList.Add(neuron);
                        neuron.waitForConnection -= nextAvailableList.Count;
                    }
                }
            }
            availableOutNeurons = nextAvailableList;
        }
        aI.outputNeurons = outputNeurons;
        aI.allNeurons    = allNeurons;

        return(aI);
    }
Ejemplo n.º 5
0
 public void ForceConnect(ChessNeuron chessNeuron)
 {
     connectionsOut[0]            = chessNeuron;
     chessNeuron.connectionsIn[0] = this;
 }