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()
    {
        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++;
    }
Beispiel #4
0
            public void DoIteration()
            {
                int size = Individuals.Count;

                for (var i = 0; i < size; i++)
                {
                    for (var j = 0; j < i; j++)
                    {
                        Individuals.Add(GeneticIndividual.Crossover(Individuals[i], Individuals[j]));
                    }
                    for (var j = 0; j < 100; j++)
                    {
                        Individuals.Add(GeneticIndividual.Mutation(Individuals[i]));
                    }
                }
                Individuals = Individuals
                              .Where(x => !x.IsInvalid())
                              .OrderBy(x => x.Fitness())
                              .Reverse()
                              .Take(MaxSize)
                              .ToList();
            }