예제 #1
0
    private void GenerateInitialNetsAsCopy(NEATNet net)
    {
        generationNumber = 0; //0 since simulaiton has no been started yet
        testCounter      = 0; //none have been tested
        speciesManager   = new Species(this);

        for (int i = 0; i < populationSize; i++)
        {                                       //run through population size
            NEATNet netCopy = new NEATNet(net); //create net with consultor, perceptron information and test time
            netCopy.SetNetFitness(0f);
            netCopy.SetTimeLived(0f);
            netCopy.SetTestTime(testTime);
            netCopy.ClearNodeValues();
            netCopy.GenerateNeuralNetworkFromGenome();  //< NEW LSES ADDITION

            Population population = speciesManager.ClosestSpecies(netCopy);

            if (population == null)
            {
                population = speciesManager.CreateNewSpecie(new Color(UnityEngine.Random.Range(0f, 1f), UnityEngine.Random.Range(0f, 1f), UnityEngine.Random.Range(0f, 1f)));
                population.Add(netCopy);
            }
            else
            {
                population.Add(netCopy);
            }
        }
    }
예제 #2
0
    /// <summary>
    /// Generate initial neural network, where all inputs perceptrons are connected to all output perceptrons.
    /// Each of these nerual networks is mutated one time for slight change.
    /// And then they are paired up into species.
    /// </summary>
    private void GenerateInitialNets()
    {
        generationNumber = 0; //0 since simulaiton has no been started yet
        testCounter      = 0; //none have been tested
        speciesManager   = new Species(this);
        //species = new List<List<NEATNet>>(); //create an empty population list

        for (int i = 0; i < populationSize; i++)
        {                                                                                                        //run through population size
            NEATNet net = new NEATNet(consultor, numberOfInputPerceptrons, numberOfOutputPerceptrons, testTime); //create net with consultor, perceptron information and test time
            net.Mutate();                                                                                        //mutate once for diversity (must make sure SJW's aren't triggered)
            net.GenerateNeuralNetworkFromGenome();                                                               //< NEW LSES ADDITION

            Population population = speciesManager.ClosestSpecies(net);

            if (population == null)
            {
                population = speciesManager.CreateNewSpecie(new Color(UnityEngine.Random.Range(0f, 1f), UnityEngine.Random.Range(0f, 1f), UnityEngine.Random.Range(0f, 1f)));
                population.Add(net);
            }
            else
            {
                population.Add(net);
            }

            /*if (species.Count == 0) { //if nothing exists yet
             *  List<NEATNet> newSpecies = new List<NEATNet>(); //create a new species
             *  newSpecies.Add(net); //add net to this new species
             *  species.Add(newSpecies); //add new species to species list
             * }
             * else { //if at least one species exists
             *  int numberOfSpecies = species.Count; //save species count
             *  int location = -1; //-1 means no species match found,  0 >= will mean a species match has been found at a given index
             *
             *  for (int j = 0; j < numberOfSpecies; j++) { //run through species count
             *      int numberOfNets = species[j].Count; //number of organisum in species at index j
             *      int randomIndex = UnityEngine.Random.Range(0, numberOfNets); //pick a random network within this species
             *
             *      if (NEATNet.SameSpeciesV2(species[j][randomIndex], net)) { //check if new network and random network belong to the same species
             *          location = j; //new species can be added to index j
             *          break; //break out of loop
             *      }
             *  }
             *
             *  if (location == -1) { //if no species found
             *      //create new species and add to that
             *      List<NEATNet> newSpecies = new List<NEATNet>();
             *      newSpecies.Add(net);
             *      species.Add(newSpecies);
             *  }
             *  else { //found species that match this network
             *      species[location].Add(net); //add net to the matched species list
             *  }
             * }*/
        }
    }
    /// <summary>
    /// Generate initial neural network, where all inputs neurons are connected to all output neurons.
    /// Each of these nerual networks is mutated one time for slight change.
    /// And then they are paired up into species.
    /// </summary>
    private void GenerateInitialNets()
    {
        generationNumber = 0; //0 since simulaiton has no been started yet
        testCounter      = 0; //none have been tested
        speciesManager   = new Species(this);
        //species = new List<List<NEATNet>>(); //create an empty population list

        for (int i = 0; i < populationSize; i++)
        {                                                                                                //run through population size
            NEATNet net = new NEATNet(consultor, numberOfInputNeurons, numberOfOutputNeurons, testTime); //create net with consultor, perceptron information and test time
            net.Mutate();                                                                                //mutate once for diversity (must make sure SJW's aren't triggered)
            net.GenerateNeuralNetworkFromGenome();                                                       //< NEW LSES ADDITION

            Population population = speciesManager.ClosestSpecies(net);

            if (population == null)
            {
                population = speciesManager.CreateNewSpecie(new Color(UnityEngine.Random.Range(0f, 1f), UnityEngine.Random.Range(0f, 1f), UnityEngine.Random.Range(0f, 1f)));
            }
            population.Add(net);
        }
    }
예제 #4
0
    /// <summary>
    /// Create new generation of net's
    /// </summary>
    /// <param name="maxCap"></param>
    public void GenerateNewGeneration(int maxCap)
    {
        float        totalSharedFitness = 0f; //total shared fitness of the whole population
        float        totalNetworks      = 0f; //total number of organisums (used for distribution)
        List <float> distribution       = new List <float>();


        for (int i = 0; i < species.Count; i++)
        {
            float dist = species[i].GetDistribution(manager.beta);
            distribution.Add(dist);
            totalSharedFitness += dist;
            species[i].RemoveWorst(manager.removeWorst);
        }

        for (int i = 0; i < distribution.Count; i++)
        {
            if (totalSharedFitness <= 0f)
            {
                distribution[i] = 0;
            }
            else
            {
                distribution[i] = (int)((distribution[i] / totalSharedFitness) * maxCap);
            }
            totalNetworks += distribution[i];
        }



        if (maxCap > totalNetworks)
        {
            Debug.Log("More added: " + totalNetworks + " " + maxCap + " " + (maxCap - totalNetworks));
            for (int i = 0; i < (maxCap - totalNetworks); i++)
            {
                int highIndex         = species.Count / 2;
                int randomInsertIndex = UnityEngine.Random.Range(highIndex, species.Count);
                distribution[randomInsertIndex] = distribution[randomInsertIndex] + 1;
            }
        }
        else if (maxCap < totalNetworks)
        {
            Debug.Log("Some removed: " + totalNetworks + " " + maxCap + " " + (maxCap - totalNetworks));
            for (int i = 0; i < (totalNetworks - maxCap); i++)
            {
                bool removed = false;
                while (removed == false)
                {
                    int randomInsertIndex = UnityEngine.Random.Range(0, species.Count);
                    if (distribution[randomInsertIndex] > 0)
                    {
                        distribution[randomInsertIndex] = distribution[randomInsertIndex] - 1;
                        removed = true;
                    }
                }
            }
        }

        for (int i = species.Count - 1; i >= 0; i--)
        {
            if (distribution[i] <= 0 || species[i].GetPopulation().Count == 0)
            {
                distribution.RemoveAt(i);
                species.RemoveAt(i);
            }
        }

        totalNetworks = maxCap;


        List <Population> newSpecies = new List <Population>();

        for (int i = 0; i < distribution.Count; i++)
        {
            int        newDist   = (int)distribution[i];
            Population popOldGen = species[i];
            newSpecies.Add(new Population(species[i].GetColor()));

            for (int j = 0; j < newDist; j++)
            {
                NEATNet net = null;

                if (j > newDist * manager.elite)
                {
                    NEATNet organism1 = popOldGen.GetRandom();
                    NEATNet organism2 = popOldGen.GetRandom();

                    net = NEATNet.Corssover(organism1, organism2);
                    net.Mutate();
                }
                else
                {
                    NEATNet netL = popOldGen.GetLast();
                    if (netL == null)
                    {
                        Debug.Log(newDist + " " + popOldGen.GetPopulation().Count);
                    }
                    net = new NEATNet(popOldGen.GetLast());
                }


                //reset copied stats
                net.SetNetFitness(0f);
                net.SetTimeLived(0f);
                net.SetTestTime(manager.testTime);
                net.ClearNodeValues();

                net.GenerateNeuralNetworkFromGenome();  //< NEW LSES ADDITION

                bool added = newSpecies[i].AddIfMatch(net);

                if (added == false)
                {
                    Population foundPopulation = Species.ClosestSpecies(newSpecies, net);
                    if (foundPopulation == null)
                    {
                        Population newSpecie = Species.CreateNewSpecie(newSpecies, new Color(UnityEngine.Random.Range(0f, 1f), UnityEngine.Random.Range(0f, 1f), UnityEngine.Random.Range(0f, 1f)));
                        newSpecie.Add(net);
                    }
                    else
                    {
                        foundPopulation.Add(net);
                    }
                }
            }
        }
        this.species = newSpecies;
    }