Beispiel #1
0
    //The Step function assumes that the fitness values of all the individuals in the population have been calculated.
    public override void Step()
    {
        //criar a nova populacao
        List <Individual> new_pop = new List <Individual>();

        updateReport(); //called to get some stats
                        // fills the rest with mutations of the best!
        if (elitist)
        {
            for (int i = 0; i < populationSize - (elistismoValor * populationSize); i++)
            {
                GeneticIndividual best  = (GeneticIndividual) new TournmentSelect().selectIndividuals(population, tournamentSize).Clone();
                GeneticIndividual best2 = (GeneticIndividual) new TournmentSelect().selectIndividuals(population, tournamentSize).Clone();
                best.Crossover(best2, crossoverProbability);
                best.Mutate(mutationProbability);
                new_pop.Add(best.Clone());
                Debug.Log(new_pop.Count);
            }


            float      max2  = float.MaxValue;
            Individual elite = null;
            for (int i = 0; i < (elistismoValor * populationSize); i++)
            {
                float max = float.MinValue;
                for (int j = 0; j < populationSize; j++)
                {
                    if (population[j].Fitness > max && population[j].Fitness < max2)
                    {
                        max   = population[j].Fitness;
                        elite = (GeneticIndividual)population[j];
                    }
                }
                max2 = max;
                new_pop.Add(elite.Clone());
            }
        }
        else
        {
            for (int i = 0; i < populationSize; i++)
            {
                GeneticIndividual best  = (GeneticIndividual) new TournmentSelect().selectIndividuals(population, tournamentSize).Clone();
                GeneticIndividual best2 = (GeneticIndividual) new TournmentSelect().selectIndividuals(population, tournamentSize).Clone();
                best.Crossover(best2, crossoverProbability);
                best.Mutate(mutationProbability);
                new_pop.Add(best.Clone());
                Debug.Log(new_pop.Count);
            }
        }

        population = new_pop;

        generation++;
    }
Beispiel #2
0
    public override void Step()
    {
        List <Individual> new_pop = new List <Individual>();


        updateReport(); //called to get some stats
        if (elitist)
        {
            GeneticIndividual melhor = (GeneticIndividual)overallBest.Clone();
            new_pop.Add(melhor); //coloca o melhor de sempre na populaçao
        }

        while (new_pop.Count < populationSize)
        {
            GeneticIndividual parent1 = (GeneticIndividual)selection.torneio(population, tournamentSize); //torneio
            GeneticIndividual parent2;
            do
            {
                parent2 = (GeneticIndividual)selection.torneio(population, tournamentSize);
            } while (parent1 == parent2);//para que o pai 1 seja diferente do 2

            GeneticIndividual tmp1 = (GeneticIndividual)parent1.Clone();
            GeneticIndividual tmp2 = (GeneticIndividual)parent2.Clone();

            tmp1.Crossover(tmp2, crossoverProbability); // crossover em temporarios
            tmp2.Crossover(tmp1, crossoverProbability);

            GeneticIndividual child1 = tmp1;// apenas para clareza
            GeneticIndividual child2 = tmp2;

            child1.Mutate(mutationProbability);//mutaçoes
            child2.Mutate(mutationProbability);

            new_pop.Add(child1); //adiciona à população os filhos
            new_pop.Add(child2);
        }
        population = new_pop;

        /*
         * float maximum = population[0].Fitness;
         * for(int i = 0; i < population.Count - 1; i++)
         * {
         *  if (population[i].Fitness > maximum)
         *  {
         *      maximum = population[i].Fitness;
         *  }
         * }
         *
         * Debug.Log("Max: " + maximum);
         */

        generation++;//passa para a próxima geração
    }
Beispiel #3
0
    //The Step function assumes that the fitness values of all the individuals in the population have been calculated.
    public override void Step()
    {
        System.Random random = new System.Random();
        //You should implement the code runs in each generation here
        List <Individual> new_pop = new List <Individual>();

        updateReport();

        population.Sort((x, y) => x.Fitness.CompareTo(y.Fitness));
        population.Reverse();

        if (elitist == true)
        {
            for (int i = 0; i < elitistSize; i++)
            {
                new_pop.Add(population[i].Clone());
            }
        }

        if (tournamentSize > 0)
        {
            List <Individual> winners = fitnessSelect.selectIndividuals(population, tournamentSize, tournamentProbability);

            for (int i = new_pop.Count; i < population.Count; i++)
            {
                Individual parent1 = winners[Random.Range(0, winners.Count)];
                winners.Remove(parent1);
                Individual parent2 = winners[Random.Range(0, winners.Count)].Clone();

                parent2.Crossover(parent1, crossoverProbability);
                parent2.Mutate(mutationProbability);

                winners.Add(parent1);

                new_pop.Add(parent2);
            }
        }
        else
        {
            for (int i = new_pop.Count; i < populationSize; i++)
            {
                GeneticIndividual tmp = (GeneticIndividual)overallBest.Clone();
                tmp.Mutate(mutationProbability);
                new_pop.Add(tmp);
            }
        }
        population = new_pop;
        generation++;
    }
    public override void InitPopulation()
    {
        maxNumberOfEvaluations = Mathf.Min(maxNumberOfEvaluations, populationSize);
        populationRed          = new List <Individual>();
        populationBlue         = new List <Individual>();

        while (populationRed.Count < populationSize)
        {
            GeneticIndividual new_ind_red = new GeneticIndividual(NNTopology, maxNumberOfEvaluations, mutationMethod)
            {
                Mean  = Mean,
                Stdev = Stdev
            };
            GeneticIndividual new_ind_blue = new GeneticIndividual(NNTopology, maxNumberOfEvaluations, mutationMethod)
            {
                Mean  = Mean,
                Stdev = Stdev
            };

            if (seedPopulationFromFile)
            {
                NeuralNetwork nnRed  = getRedIndividualFromFile();
                NeuralNetwork nnBlue = getBlueIndividualFromFile();
                new_ind_red.Initialize(nnRed);
                new_ind_blue.Initialize(nnBlue);
                //only the first individual is an exact copy. the other are going to suffer mutations
                if (populationRed.Count != 0 && populationBlue.Count != 0)
                {
                    new_ind_red.Mutate(mutationProbabilityRedPopulation);
                    new_ind_blue.Mutate(mutationProbabilityBluePopulation);
                }
            }
            else
            {
                new_ind_red.Initialize();
                new_ind_blue.Initialize();
            }

            populationRed.Add(new_ind_red);
            populationBlue.Add(new_ind_blue);
        }

        switch (selectionMethod)
        {
        case SelectionType.Tournament:
            selection = new TournamentSelection(tournamentSize);
            break;
        }
    }
Beispiel #5
0
    //The Step function assumes that the fitness values of all the individuals in the population have been calculated.
    public override void Step()
    {
        List <Individual> new_pop      = new List <Individual> ();
        List <Individual> elitist_list = new List <Individual> ();

        if (elitist)
        {
            population.Sort((x, y) => x.Fitness.CompareTo(y.Fitness));
            Debug.Log("P: ");
            for (int i = 0; i < populationSize; i++)
            {
                Debug.Log(population [i].Fitness);
            }
            for (int i = 0; i < n_elitists; i++)
            {
                int pop = populationSize - 1;
                elitist_list.Add(population [pop - i].Clone());
            }
        }

        updateReport();          //called to get some stats
        // fills the rest with mutations of the best !
        for (int i = 0; i < populationSize; i++)
        {
            GeneticIndividual pai  = (GeneticIndividual)Tournament(population, tournamentSize).Clone();
            GeneticIndividual pai2 = (GeneticIndividual)Tournament(population, tournamentSize).Clone();

            pai.Crossover(pai2.Clone(), crossoverProbability, numerocortes);
            pai.Mutate(mutationProbability);
            new_pop.Add(pai.Clone());
        }

        population = new_pop;

        if (elitist)
        {
            for (int i = 0; i < n_elitists; i++)
            {
                Individual pick_to_delete = new_pop [Random.Range(0, populationSize - 1)];
                new_pop.Remove(pick_to_delete);
                new_pop.Add(elitist_list [i].Clone());
            }
        }
        generation++;
    }