Example usage for org.apache.commons.math3.genetics ListPopulation ListPopulation

List of usage examples for org.apache.commons.math3.genetics ListPopulation ListPopulation

Introduction

In this page you can find the example usage for org.apache.commons.math3.genetics ListPopulation ListPopulation.

Prototype

public ListPopulation(final int populationLimit) throws NotPositiveException 

Source Link

Document

Creates a new ListPopulation instance and initializes its inner chromosome list.

Usage

From source file:eu.tsp.sal.SensorTournamentSelection.java

/**
 * Helper for {@link #select(Population)}. Draw {@link #arity} random chromosomes without replacement from the
 * population, and then select the fittest chromosome among them.
 *
 * @param population the population from which the chromosomes are chosen.
 * @return the selected chromosome./*from   w w w  .  j av  a2s .c  om*/
 * @throws MathIllegalArgumentException if the tournament arity is bigger than the population size
 */
private Chromosome tournament(final ListPopulation population) throws MathIllegalArgumentException {
    if (population.getPopulationSize() < this.arity) {
        throw new MathIllegalArgumentException(LocalizedFormats.TOO_LARGE_TOURNAMENT_ARITY, arity,
                population.getPopulationSize());
    }
    // auxiliary population
    ListPopulation tournamentPopulation = new ListPopulation(this.arity) {
        public Population nextGeneration() {
            // not useful here
            return null;
        }
    };

    // create a copy of the chromosome list
    List<Chromosome> chromosomes = new ArrayList<Chromosome>(population.getChromosomes());
    for (int i = 0; i < this.arity; i++) {
        // select a random individual and add it to the tournament
        int rind = SensorGeneticAlgorithm.getRandomGenerator().nextInt(chromosomes.size());
        tournamentPopulation.addChromosome(chromosomes.get(rind));
        // do not select it again
        chromosomes.remove(rind);
    }

    //System.out.println("*****" + tournamentPopulation.getFittestChromosome());
    // the winner takes it all
    return tournamentPopulation.getFittestChromosome();
}

From source file:sos.base.util.genetic.SOSTournamentSelection.java

/**
 * Helper for {@link #select(Population)}. Draw {@link #arity} random chromosomes without replacement from the
 * population, and then select the fittest chromosome among them.
 * /*w  w  w  .  j  a v a2s  .  c  o  m*/
 * @param population
 *            the population from which the chromosomes are choosen.
 * @return the selected chromosome.
 * @throws MathIllegalArgumentException
 *             if the tournament arity is bigger than the population size
 */
private Chromosome tournament(final ListPopulation population) throws MathIllegalArgumentException {
    if (population.getPopulationSize() < this.arity) {
        throw new MathIllegalArgumentException(LocalizedFormats.TOO_LARGE_TOURNAMENT_ARITY, arity,
                population.getPopulationSize());
    }
    // auxiliary population
    ListPopulation tournamentPopulation = new ListPopulation(this.arity) {
        @Override
        public Population nextGeneration() {
            // not useful here
            return null;
        }
    };

    // create a copy of the chromosome list
    List<Chromosome> chromosomes = new ArrayList<Chromosome>(population.getChromosomes());
    for (int i = 0; i < this.arity; i++) {
        // select a random individual and add it to the tournament
        int rind = random.nextInt(chromosomes.size());
        tournamentPopulation.addChromosome(chromosomes.get(rind));
        // do not select it again
        chromosomes.remove(rind);
    }
    // the winner takes it all
    return tournamentPopulation.getFittestChromosome();
}