public void ActivateWithBrain(BloopBrain brain)
    {
        this.brain = brain;
        activate   = true;

        Invoke("Finish", 20f);
    }
Example #2
0
 void Start()
 {
     Application.runInBackground = true;
     balancers     = new GameObject[size];
     rankedFitness = new float[size, 2];
     for (int i = 0; i < size; i++)
     {
         BloopBrain brain = new BloopBrain();
         brain.GenerationRandomBrain();
         brainList.Add(brain);
     }
     Next();
 }
Example #3
0
    public BloopBrain[] Crossover(BloopBrain brain)
    {
        BloopBrain[] cross             = new BloopBrain[2];
        int          randomHiddenIndex = Random.Range(0, numberOfHidden);
        int          randomOutputIndex = Random.Range(0, numberOfOutputs);

        cross[0] = new BloopBrain();
        cross[1] = new BloopBrain();


        for (int i = 0; i < randomHiddenIndex; i++)
        {
            for (int j = 0; j < numberOfInputs; j++)
            {
                cross[0].inputWeights[i, j] = inputWeights[i, j];
                cross[1].inputWeights[i, j] = brain.inputWeights[i, j];
            }
        }

        for (int i = 0; i < randomOutputIndex; i++)
        {
            for (int j = 0; j < numberOfHidden; j++)
            {
                cross[0].hiddenWeights[i, j] = hiddenWeights[i, j];
                cross[1].hiddenWeights[i, j] = brain.hiddenWeights[i, j];
            }
        }

        for (int i = randomHiddenIndex; i < numberOfHidden; i++)
        {
            for (int j = 0; j < numberOfInputs; j++)
            {
                cross[0].inputWeights[i, j] = brain.inputWeights[i, j];
                cross[1].inputWeights[i, j] = inputWeights[i, j];
            }
        }

        for (int i = randomOutputIndex; i < numberOfOutputs; i++)
        {
            for (int j = 0; j < numberOfHidden; j++)
            {
                cross[0].hiddenWeights[i, j] = brain.hiddenWeights[i, j];
                cross[1].hiddenWeights[i, j] = hiddenWeights[i, j];
            }
        }


        cross[0].Mutate();
        cross[1].Mutate();
        return(cross);
    }
 void ActivateWithBrain(BloopBrain newBrain)
 {
     brain    = newBrain;
     activate = true;
     Invoke("Finish", 10f);
 }
Example #5
0
    public void UpdateCounter(BloopBrain brain)
    {
        lock (mutex)
        {
            brainListReturned.Add(brain);
            if (sizeCounter == size)
            {
                //Debug.Log("Done");
                brainList.Clear();
                generationNumber++;
                for (int i = 0; i < size; i++)
                {
                    rankedFitness[i, 0] = i;
                    rankedFitness[i, 1] = brainListReturned[i].fitness;
                    Destroy(balancers[i]);
                }

                BubbleSortBrains();
                Debug.Log("BEST: " + rankedFitness[size - 1, 1]);
                if (rankedFitness[size - 1, 1] > 20f && generationNumber > 10)
                {
                    Time.timeScale = 1f;
                    balancers[0]   = (GameObject)Instantiate(creaturePrefab);
                    balancers[0].transform.parent = transform;
                    balancers[0].SendMessage("ActivateWithBrainTestMode", brainListReturned[(int)rankedFitness[size - 1, 0]]);
                }
                else
                {
                    int[,] pairedIndcies = new int[size, 2];
                    for (int i = 0; i < (size / 2); i++)
                    {
                        pairedIndcies[i, 0] = Random.Range((size / 2), size);
                        pairedIndcies[i, 1] = Random.Range((size / 2), size);
                    }


                    for (int i = 0; i < (size / 2); i++)
                    {
                        int          index1 = (int)rankedFitness[pairedIndcies[i, 0], 0];
                        int          index2 = (int)rankedFitness[pairedIndcies[i, 1], 0];
                        BloopBrain[] cross  = brainListReturned[index1].Crossover(brainListReturned[index2]);
                        brainList.Add(cross[0]);
                        brainList.Add(cross[1]);
                    }
                    brainListReturned.Clear();
                    sizeCounter = 0;
                    Next();

                    /*Debug.Log("BEST: " + rankedFitness[size - 1, 1]);
                     * Time.timeScale = 1f;
                     * balancers[0] = (GameObject)Instantiate(creaturePrefab);
                     * balancers[0].transform.parent = transform;
                     * balancers[0].SendMessage("ActivateWithBrainTestMode", brainListReturned[(int)rankedFitness[size - 1, 0]]);*/
                }
            }
            else
            {
                //Debug.Log(brain.fitness);
                Next();
            }
        }
    }
 public void ActivateWithBrainTestMode(BloopBrain brain)
 {
     this.brain = brain;
     activate   = true;
     testMode   = true;
 }