Пример #1
0
    private reinforcementNet[] PickBestPopulation()
    {
        reinforcementNet[] newPopulation = new reinforcementNet[initialPopulation];

        for (int i = 0; i < bestAgentSelection; i++)
        {
            newPopulation[naturallySelected]         = population[i].InitialiseCopy(controller.LAYERS, controller.NEURONS);
            newPopulation[naturallySelected].fitness = 0;
            naturallySelected++;

            int f = Mathf.RoundToInt(population[i].fitness * 10);

            for (int c = 0; c < f; c++)
            {
                genePool.Add(i);
            }
        }

        for (int i = 0; i < worstAgentSelection; i++)
        {
            int last = population.Length - 1;
            last -= i;

            int f = Mathf.RoundToInt(population[last].fitness * 10);

            for (int c = 0; c < f; c++)
            {
                genePool.Add(last);
            }
        }

        return(newPopulation);
    }
    public reinforcementNet InitialiseCopy(int hiddenLayerCount, int hiddenNeuronCount)
    {
        reinforcementNet n = new reinforcementNet();

        List <Matrix <float> > newWeights = new List <Matrix <float> >();

        for (int i = 0; i < this.weights.Count; i++)
        {
            Matrix <float> currentWeight = Matrix <float> .Build.Dense(weights[i].RowCount, weights[i].ColumnCount);

            for (int x = 0; x < currentWeight.RowCount; x++)
            {
                for (int y = 0; y < currentWeight.ColumnCount; y++)
                {
                    currentWeight[x, y] = weights[i][x, y];
                }
            }

            newWeights.Add(currentWeight);
        }

        List <float> newBiases = new List <float>();

        newBiases.AddRange(biases);

        n.weights = newWeights;
        n.biases  = newBiases;

        n.InitialiseHidden(hiddenLayerCount, hiddenNeuronCount);

        return(n);
    }
Пример #3
0
 //later set starting index to end because all nets up to that point have been
 //created with other nets
 private void FillPopulationWithRandomValues(reinforcementNet[] newPopulation, int startingIndex)
 {
     while (startingIndex < initialPopulation)
     {
         newPopulation[startingIndex] = new reinforcementNet();
         newPopulation[startingIndex].Initialise(controller.LAYERS, controller.NEURONS);
         startingIndex++;
     }
 }
Пример #4
0
 private void FillPopulationWithRandomValues(reinforcementNet[] newPopulation, reinforcement2Child[] newPopulation2, int startingIndex)
 {
     while (startingIndex < initialPopulation)
     {
         newPopulation[startingIndex] = new reinforcementNet();
         newPopulation[startingIndex].Initialise(controller.LAYERS, controller.NEURONS);
         //second person
         newPopulation2[startingIndex] = new reinforcement2Child();
         newPopulation2[startingIndex].Initialise2(player2.LAYERS, player2.NEURONS);
         startingIndex++;
     }
 }
Пример #5
0
 public void Death(float fitness, reinforcementNet network)
 {
     if (currentGenome < population.Length - 1)
     {
         population[currentGenome].fitness = fitness;
         Debug.Log("death first layer");
         currentGenome++;
         Debug.Log("death active");
         ResetToCurrentGenome();
     }
     else
     {
         RePopulate();
     }
 }
Пример #6
0
 //this helps improve the performance of current genome
 public void Death(float fitness, reinforcementNet network)
 {
     if (currentGenome < population.Length - 1)
     {
         //set the previous fitness from the death of the car controller
         //fitness then stored in dec order
         population[currentGenome].fitness = fitness;
         currentGenome++;
         //reset car with different net but saved fitness
         ResetToCurrentGenome();
     }
     else
     {
         //repopulate current generation
         RePopulate();
     }
 }
Пример #7
0
 private void SortPopulation()
 {
     //i might rework this
     for (int i = 0; i < population.Length; i++)
     {
         for (int j = i; j < population.Length; j++)
         {
             if (population[i].fitness < population[j].fitness)
             {
                 //order from high to low fitness
                 reinforcementNet temp = population[i];
                 population[i] = population[j];
                 population[j] = temp;
             }
         }
     }
 }
Пример #8
0
    private reinforcementNet[] PickBestPopulation()
    {
        reinforcementNet[] newPopulation = new reinforcementNet[initialPopulation];
        //diverse gene pool to make higher chance of top network over lower network
        for (int i = 0; i < bestAgentSelection; i++)
        {
            //copy is there because without ypu would be changine the refernce population i
            //passing over the best networks
            //increments number of filled nets
            //being added to new pop
            newPopulation[naturallySelected]         = population[i].InitialiseCopy(controller.LAYERS, controller.NEURONS);
            newPopulation[naturallySelected].fitness = 0;
            //the remaining non selected have rabdomized values
            naturallySelected++;
            //take fitness, multiply by ten and add to gene pool
            //ie survival of the fittest
            int f = Mathf.RoundToInt(population[i].fitness * 10);

            for (int c = 0; c < f; c++)
            {
                //adding indexes to reference neural net
                //example newpopulation[genepool[i]] == neural net
                genePool.Add(i);
            }
        }

        for (int i = 0; i < worstAgentSelection; i++)
        {
            //loop from bottom of array
            int last = population.Length - 1;
            last -= i;

            int f = Mathf.RoundToInt(population[last].fitness * 8);

            for (int c = 0; c < f; c++)
            {
                genePool.Add(last);
            }
        }

        return(newPopulation);
    }
Пример #9
0
 private void SortPopulation()
 {
     for (int i = 0; i < population.Length; i++)
     {
         for (int j = i; j < population.Length; j++)
         {
             if (population[i].fitness < population[j].fitness)
             {
                 reinforcementNet temp = population[i];
                 population[i] = population[j];
                 population[j] = temp;
             }
             if (population2[i].fitness < population2[j].fitness)
             {
                 reinforcement2Child temp2 = population2[i];
                 population2[i] = population2[j];
                 population2[j] = temp2;
             }
         }
     }
 }
Пример #10
0
 public void ResetWithNetwork(reinforcementNet net)
 {
     network = net;
     Reset();
 }
Пример #11
0
 private void Awake()
 {
     startPosition = transform.position;
     startRotation = transform.eulerAngles;
     network       = GetComponent <reinforcementNet>();
 }
Пример #12
0
    private void Crossover(reinforcementNet[] newPopulation, reinforcement2Child[] newPopulation2)
    {
        for (int i = 0; i < numberToCrossover; i += 2)
        {
            int AIndex = i;
            int BIndex = i + 1;

            if (genePool.Count >= 1)
            {
                for (int l = 0; l < 100; l++)
                {
                    AIndex = genePool[Random.Range(0, genePool.Count)];
                    BIndex = genePool[Random.Range(0, genePool.Count)];

                    if (AIndex != BIndex)
                    {
                        break;
                    }
                }
            }

            reinforcementNet Child1 = new reinforcementNet();
            reinforcementNet Child2 = new reinforcementNet();
            //
            reinforcement2Child Child1_A = new reinforcement2Child();
            reinforcement2Child Child2_A = new reinforcement2Child();

            Child1.Initialise(controller.LAYERS, controller.NEURONS);
            Child2.Initialise(controller.LAYERS, controller.NEURONS);

            Child1_A.Initialise2(player2.LAYERS, player2.NEURONS);
            Child2_A.Initialise2(player2.LAYERS, player2.NEURONS);

            Child1.fitness = 0;
            Child2.fitness = 0;

            Child1_A.fitness = 0;
            Child2_A.fitness = 0;


            for (int w = 0; w < Child1.weights.Count; w++)
            {
                if (Random.Range(0.0f, 1.0f) < 0.5f)
                {
                    Child1.weights[w] = population[AIndex].weights[w];
                    Child2.weights[w] = population[BIndex].weights[w];
                    //second person
                    Child2_A.weights[w] = population2[AIndex].weights[w];
                    Child1_A.weights[w] = population2[BIndex].weights[w];
                }
                else
                {
                    Child2.weights[w] = population[AIndex].weights[w];
                    Child1.weights[w] = population[BIndex].weights[w];
                    //second person
                    Child1_A.weights[w] = population2[AIndex].weights[w];
                    Child2_A.weights[w] = population2[BIndex].weights[w];
                }
            }


            for (int w = 0; w < Child1.biases.Count; w++)
            {
                if (Random.Range(0.0f, 1.0f) < 0.5f)
                {
                    Child1.biases[w] = population[AIndex].biases[w];
                    Child2.biases[w] = population[BIndex].biases[w];
                    //second person
                    Child2_A.biases[w] = population2[AIndex].biases[w];
                    Child1_A.biases[w] = population2[BIndex].biases[w];
                }
                else
                {
                    Child2.biases[w] = population[AIndex].biases[w];
                    Child1.biases[w] = population[BIndex].biases[w];
                    //second person
                    Child1_A.biases[w] = population2[AIndex].biases[w];
                    Child2_A.biases[w] = population2[BIndex].biases[w];
                }
            }

            newPopulation[naturallySelected]  = Child1;
            newPopulation2[naturallySelected] = Child1_A;
            naturallySelected++;

            newPopulation[naturallySelected]  = Child2;
            newPopulation2[naturallySelected] = Child2_A;
            naturallySelected++;
        }
    }
Пример #13
0
    private void Crossover(reinforcementNet[] newPopulation)
    {
        //cross over based on gene pool
        //increment by two
        for (int i = 0; i < numberToCrossover; i += 2)
        {
            //first parent index
            int AIndex = i;
            //second parent
            int BIndex = i + 1;

            if (genePool.Count >= 1)
            {
                for (int l = 0; l < 100; l++)
                {
                    //find random elemnt in gene pool
                    AIndex = genePool[Random.Range(0, genePool.Count)];
                    BIndex = genePool[Random.Range(0, genePool.Count)];
                    //ensure parents are not the same
                    //we found what we looking for break
                    if (AIndex != BIndex)
                    {
                        break;
                    }
                }
            }

            reinforcementNet Child1 = new reinforcementNet();
            reinforcementNet Child2 = new reinforcementNet();

            Child1.Initialise(controller.LAYERS, controller.NEURONS);
            Child2.Initialise(controller.LAYERS, controller.NEURONS);

            Child1.fitness = 0;
            Child2.fitness = 0;

            //should be more subtle in practice to much transfer of info
            for (int w = 0; w < Child1.weights.Count; w++)
            {
                if (Random.Range(0.0f, 1.0f) < 0.5f)
                {
                    //taje weights from parents
                    Child1.weights[w] = population[AIndex].weights[w];
                    Child2.weights[w] = population[BIndex].weights[w];
                }
                else
                {
                    //random chance for kids
                    Child2.weights[w] = population[AIndex].weights[w];
                    Child1.weights[w] = population[BIndex].weights[w];
                }
            }

            //indv values instead of matices
            for (int w = 0; w < Child1.biases.Count; w++)
            {
                if (Random.Range(0.0f, 1.0f) < 0.5f)
                {
                    Child1.biases[w] = population[AIndex].biases[w];
                    Child2.biases[w] = population[BIndex].biases[w];
                }
                else
                {
                    Child2.biases[w] = population[AIndex].biases[w];
                    Child1.biases[w] = population[BIndex].biases[w];
                }
            }
            //add to new population
            newPopulation[naturallySelected] = Child1;
            naturallySelected++;
            //add to new population
            newPopulation[naturallySelected] = Child2;
            naturallySelected++;
        }
    }