Example #1
0
        //Evolve population
        public static Population evolvePopulation(Population pop)
        {
            Population newPopulation = new Population(pop.size(), false);

            //keep your best individual
            if (Elitism)
            {
                newPopulation.saveIndividual(0, pop.getFittest());
            }

            //Crossover population
            int elitismOffset;

            if (Elitism)
            {
                elitismOffset = 1;
            }
            else
            {
                elitismOffset = 0;
            }

            //loop over tje population size and create new individuals with crossover
            for (int i = elitismOffset; i < pop.size(); i++)
            {
                Individual indiv1   = tournamentSelection(pop);
                Individual indiv2   = tournamentSelection(pop);
                Individual newIndiv = crossover(indiv1, indiv2);
                newPopulation.saveIndividual(i, newIndiv);
            }

            //Mutate population
            for (int i = elitismOffset; i < newPopulation.size(); i++)
            {
                mutate(newPopulation.getIndividual(i));
            }


            return(newPopulation);
        }
Example #2
0
        // Select individuals for crossover
        private static Individual tournamentSelection(Population pop)
        {
            // Create a tournament population
            Population tournament = new Population(TournamentSize, false);

            // For each place in the tournament get a random individual
            for (int i = 0; i < TournamentSize; i++)
            {
                int randomId = (int)(Rnd.NextDouble() * pop.size());
                tournament.saveIndividual(i, pop.getIndividual(randomId));
            }
            // Get the fittest
            Individual fittest = tournament.getFittest();

            return(fittest);
        }
Example #3
0
        //Evolve population
        public static Population evolvePopulation(Population pop)
        {
            Population newPopulation = new Population(pop.size(), false);

            //keep your best individual
            if (Elitism) {
                newPopulation.saveIndividual(0, pop.getFittest());
            }

            //Crossover population
            int elitismOffset;
            if (Elitism) {
                elitismOffset = 1;
            } else {
                elitismOffset = 0;
            }

            //loop over tje population size and create new individuals with crossover
            for(int i = elitismOffset; i<pop.size();i++) {
                Individual indiv1 = tournamentSelection(pop);
                Individual indiv2 = tournamentSelection(pop);
                Individual newIndiv = crossover(indiv1, indiv2);
                newPopulation.saveIndividual(i, newIndiv);

            }

            //Mutate population
            for (int i=elitismOffset; i<newPopulation.size();i++) {
                mutate(newPopulation.getIndividual(i));
            }

            return newPopulation;
        }
Example #4
0
 // Select individuals for crossover
 private static Individual tournamentSelection(Population pop)
 {
     // Create a tournament population
     Population tournament = new Population(TournamentSize, false);
     // For each place in the tournament get a random individual
     for (int i = 0; i < TournamentSize; i++) {
         int randomId = (int)(Rnd.NextDouble() * pop.size());
         tournament.saveIndividual(i, pop.getIndividual(randomId));
     }
     // Get the fittest
     Individual fittest = tournament.getFittest();
     return fittest;
 }