Esempio n. 1
0
 public ChessNeuron(List <ChessGene> DNA)
 {
     // Make this neuron based on the first gene, then remove it from the list
     geneCode = new List <ChessGene>();
     foreach (ChessGene gene in DNA)
     {
         if (gene.neuronType == 0)
         {
             geneCode.Add(new ChessGene(0, null, gene.CopyOuts(), 0, 0, 100));
         }
         else
         {
             geneCode.Add(new ChessGene(gene.neuronType, gene.CopyIns(), gene.CopyOuts(),
                                        gene.waitForConnection, gene.threshold, gene.outputStrength));
         }
     }
     currentGene = geneCode[0];
     geneCode.RemoveAt(0);
     neuronType = currentGene.neuronType;
     if (neuronType == 1)
     {
         MakeIntermediateNeuron();
     }
 }
Esempio n. 2
0
    private ChessAIDNA Mutate(ChessAIDNA toMutate)
    {
        for (int i = 0; i < maxMutations; i++)
        {
            // Possible mutations:
            int mutationType = Random.Range(0, 14);
            // Switch two DNA strands
            if (mutationType == 0)
            {
                int      toSwitch1 = Random.Range(0, toMutate.DNA.Count);
                int      toSwitch2 = Random.Range(0, toMutate.DNA.Count);
                ChessDNA tempDNA   = CopyDNA(toMutate.DNA[toSwitch1].chessGenes);
                toMutate.DNA[toSwitch1] = CopyDNA(toMutate.DNA[toSwitch2].chessGenes);
                toMutate.DNA[toSwitch2] = tempDNA;
            }
            // Add a neuron in the DNA
            if (mutationType == 1)
            {
                int randomDNA  = Random.Range(0, toMutate.DNA.Count);
                int insertSpot = Random.Range(1, toMutate.DNA[randomDNA].chessGenes.Count);
                highestTransmitterNumber = toMutate.highestTransmitterNumber;
                ChessGene newGene = MakeGene(1);
                toMutate.DNA[randomDNA].chessGenes.Insert(insertSpot, newGene);
            }
            // Remove a neuron from the DNA
            if (mutationType == 2)
            {
                int randomDNA = Random.Range(0, toMutate.DNA.Count);
                // Can't remove if there's no internediate neurons
                if (toMutate.DNA[randomDNA].chessGenes.Count > 2)
                {
                    int removeSpot = Random.Range(1, toMutate.DNA[randomDNA].chessGenes.Count - 1);
                    toMutate.DNA[randomDNA].chessGenes.RemoveAt(removeSpot);
                }
            }
            // Change a Threshold
            if (mutationType == 3)
            {
                int randomDNA    = Random.Range(0, toMutate.DNA.Count);
                int randomNeuron = Random.Range(0, toMutate.DNA[randomDNA].chessGenes.Count);
                toMutate.DNA[randomDNA].chessGenes[randomNeuron].threshold = Random.Range(0, 101);
            }
            // Add input
            if (mutationType == 4)
            {
                int randomDNA    = Random.Range(0, toMutate.DNA.Count);
                int randomNeuron = Random.Range(0, toMutate.DNA[randomDNA].chessGenes.Count);
                if (toMutate.DNA[randomDNA].chessGenes[randomNeuron].neuronType != 0)
                {
                    int numIn = toMutate.DNA[randomDNA].chessGenes[randomNeuron].possibleConnectionsIn.Count;
                    if (numIn < maxInput)
                    {
                        int tempTrans = Random.Range(0, highestTransmitterNumber + 2);
                        toMutate.DNA[randomDNA].chessGenes[randomNeuron].possibleConnectionsIn.Add(tempTrans);
                        if (tempTrans > highestTransmitterNumber)
                        {
                            highestTransmitterNumber = tempTrans;
                        }
                    }
                }
            }
            // Remove input
            if (mutationType == 5)
            {
                int randomDNA    = Random.Range(0, toMutate.DNA.Count);
                int randomNeuron = Random.Range(0, toMutate.DNA[randomDNA].chessGenes.Count);
                if (toMutate.DNA[randomDNA].chessGenes[randomNeuron].neuronType != 0)
                {
                    int numIn = toMutate.DNA[randomDNA].chessGenes[randomNeuron].possibleConnectionsIn.Count;
                    if (numIn > 1)
                    {
                        int toRemove = Random.Range(0, numIn);
                        toMutate.DNA[randomDNA].chessGenes[randomNeuron].possibleConnectionsIn.RemoveAt(toRemove);
                    }
                }
            }
            // Add output
            if (mutationType == 6)
            {
                int randomDNA    = Random.Range(0, toMutate.DNA.Count);
                int randomNeuron = Random.Range(0, toMutate.DNA[randomDNA].chessGenes.Count);
                if (toMutate.DNA[randomDNA].chessGenes[randomNeuron].neuronType != 2)
                {
                    int numOut = toMutate.DNA[randomDNA].chessGenes[randomNeuron].possibleConnectionsOut.Count;
                    if (numOut < maxOutput)
                    {
                        int tempTrans = Random.Range(0, highestTransmitterNumber + 2);
                        toMutate.DNA[randomDNA].chessGenes[randomNeuron].possibleConnectionsOut.Add(tempTrans);
                        if (tempTrans > highestTransmitterNumber)
                        {
                            highestTransmitterNumber = tempTrans;
                        }
                    }
                }
            }
            // Remove output
            if (mutationType == 7)
            {
                int randomDNA    = Random.Range(0, toMutate.DNA.Count);
                int randomNeuron = Random.Range(0, toMutate.DNA[randomDNA].chessGenes.Count);
                if (toMutate.DNA[randomDNA].chessGenes[randomNeuron].neuronType != 2)
                {
                    int numOut = toMutate.DNA[randomDNA].chessGenes[randomNeuron].possibleConnectionsOut.Count;
                    if (numOut > 1)
                    {
                        int toRemove = Random.Range(0, numOut);
                        toMutate.DNA[randomDNA].chessGenes[randomNeuron].possibleConnectionsOut.RemoveAt(toRemove);
                    }
                }
            }
            // Change output strength
            if (mutationType == 8)
            {
                int randomDNA    = Random.Range(0, toMutate.DNA.Count);
                int randomNeuron = Random.Range(0, toMutate.DNA[randomDNA].chessGenes.Count);
                toMutate.DNA[randomDNA].chessGenes[randomNeuron].outputStrength = Random.Range(0, 101);
            }
            // Change a transmitter value in Inputs
            if (mutationType == 9)
            {
                int randomDNA    = Random.Range(0, toMutate.DNA.Count);
                int randomNeuron = Random.Range(0, toMutate.DNA[randomDNA].chessGenes.Count);
                if (toMutate.DNA[randomDNA].chessGenes[randomNeuron].neuronType != 0)
                {
                    int randomInput    = Random.Range(0, toMutate.DNA[randomDNA].chessGenes[randomNeuron].possibleConnectionsIn.Count);
                    int transmitterNum = Random.Range(0, highestTransmitterNumber + 2);
                    toMutate.DNA[randomDNA].chessGenes[randomNeuron].possibleConnectionsIn[randomInput] = transmitterNum;
                    if (transmitterNum > highestTransmitterNumber)
                    {
                        highestTransmitterNumber = transmitterNum;
                    }
                }
            }
            // Change a transmitter value in Outputs
            if (mutationType == 10)
            {
                int randomDNA    = Random.Range(0, toMutate.DNA.Count);
                int randomNeuron = Random.Range(0, toMutate.DNA[randomDNA].chessGenes.Count);
                if (toMutate.DNA[randomDNA].chessGenes[randomNeuron].neuronType != 2)
                {
                    int randomOutput   = Random.Range(0, toMutate.DNA[randomDNA].chessGenes[randomNeuron].possibleConnectionsOut.Count);
                    int transmitterNum = Random.Range(0, highestTransmitterNumber + 2);
                    toMutate.DNA[randomDNA].chessGenes[randomNeuron].possibleConnectionsOut[randomOutput] = transmitterNum;
                    if (transmitterNum > highestTransmitterNumber)
                    {
                        highestTransmitterNumber = transmitterNum;
                    }
                }
            }
            // Add output of pre-output neuron
            if (mutationType == 11)
            {
                int randomDNA         = Random.Range(0, toMutate.DNA.Count);
                int preOutputLocation = toMutate.DNA[randomDNA].chessGenes.Count - 1;
                if (toMutate.DNA[randomDNA].chessGenes[preOutputLocation].neuronType != 2)
                {
                    Debug.Log("I f****d up captain");
                }
                int numOutputs = toMutate.DNA[randomDNA].chessGenes[preOutputLocation].possibleConnectionsOut.Count;
                if (numOutputs < 5)
                {
                    int randomOut = Random.Range(0, outputNumber);
                    toMutate.DNA[randomDNA].chessGenes[preOutputLocation].possibleConnectionsOut.Add(randomOut);
                }
            }
            // Remove output of pre-output neuron
            if (mutationType == 12)
            {
                int randomDNA         = Random.Range(0, toMutate.DNA.Count);
                int preOutputLocation = toMutate.DNA[randomDNA].chessGenes.Count - 1;
                if (toMutate.DNA[randomDNA].chessGenes[preOutputLocation].neuronType != 2)
                {
                    Debug.Log("I f****d up captain");
                }
                int numOutputs = toMutate.DNA[randomDNA].chessGenes[preOutputLocation].possibleConnectionsOut.Count;
                if (numOutputs > 1)
                {
                    int randomOut = Random.Range(0, numOutputs);
                    toMutate.DNA[randomDNA].chessGenes[preOutputLocation].possibleConnectionsOut.RemoveAt(randomOut);
                }
            }
            // Change value of pre-output output
            if (mutationType == 13)
            {
                int randomDNA         = Random.Range(0, toMutate.DNA.Count);
                int preOutputLocation = toMutate.DNA[randomDNA].chessGenes.Count - 1;
                if (toMutate.DNA[randomDNA].chessGenes[preOutputLocation].neuronType != 2)
                {
                    Debug.Log("I f****d up captain");
                }
                int randomOut = Random.Range(0, outputNumber);
                int toRandom  = Random.Range(0, toMutate.DNA[randomDNA].chessGenes[preOutputLocation].possibleConnectionsOut.Count);
                toMutate.DNA[randomDNA].chessGenes[preOutputLocation].possibleConnectionsOut[toRandom] = randomOut;
            }
        }

        return(toMutate);
    }