Esempio n. 1
0
    //The Step function assumes that the fitness values of all the individuals in the population have been calculated.
    public virtual void Step()
    {
        if (generation < numGenerations)
        {
            List <Individual> new_pop;

            //Store statistics in log, and sorts population
            stats.GenLog(population, generation);
            population.Sort((x, y) => y.Fitness.CompareTo(x.Fitness));

            //List<Individual> aux = new List<Individual> ();


            int new_pop_size = populationSize - elitismAffected;
            //Select parents
            new_pop = selection.selectIndividuals(population, new_pop_size);

            //Crossover par/impar
            if (new_pop_size % 2 == 1)
            {
                for (int i = 0; i < new_pop_size - 1; i += 2)
                {
                    Individual parent1 = new_pop [i];
                    Individual parent2 = new_pop [i + 1];
                    parent1.Crossover(parent2, crossoverProbability, ncortes);
                }
            }
            else
            {
                for (int i = 0; i < new_pop_size; i += 2)
                {
                    Individual parent1 = new_pop [i];
                    Individual parent2 = new_pop [i + 1];
                    parent1.Crossover(parent2, crossoverProbability, ncortes);
                }
            }
            //Mutation and Translation
            for (int i = 0; i < new_pop_size; i++)
            {
                new_pop [i].Mutate(mutationProbability);
                new_pop [i].Translate();
            }

            //Select new population
            for (int i = 0; i < elitismAffected; i++)
            {
                new_pop.Add(population [i]);
            }

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

            generation++;
        }
    }
Esempio n. 2
0
    //The Step function assumes that the fitness values of all the individuals in the population have been calculated.
    public virtual void Step()
    {
        if (generation < numGenerations)
        {
            List <Individual> new_pop;

            //Store statistics in log
            stats.GenLog(population, generation);

            //Select parents
            new_pop = selection.selectIndividuals(population, populationSize - preservedIndividualsElitism);


            //Crossover
            for (int i = 0; i < populationSize - preservedIndividualsElitism; i += 2)
            {
                Individual parent1 = new_pop [i];
                Individual parent2 = new_pop [i + 1];
                parent1.n_cuts        = N_cutsCrossover;
                parent1.CrossoverType = (int)crossoverType;
                parent1.Crossover(parent2, crossoverProbability);
            }


            //Mutation and Translation

            for (int i = 1; i < populationSize - preservedIndividualsElitism; i++)
            {
                //Debug.Log ("MutationType = " + mutationType + "/nIn int = " + (int)mutationType);
                new_pop [i].MutationType = (int)mutationType;                 //send public modifier mutationType to Individual
                new_pop [i].Mutate(mutationProbability);
                new_pop [i].Translate();
            }

            //ELITISM
            //List<Individual> elitismIndividuals = new List<Individual> ();


            //sort population by fitness
            population.Sort((x, y) => y.Fitness.CompareTo(x.Fitness));
            for (int i = 0; i < preservedIndividualsElitism; i++)
            {
                //Debug.Log(population[i].Fitness);
                new_pop.Add(population [i]);
            }
            //END ELITISM


            //Select new population
            population = new_pop;

            generation++;
        }
    }