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

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

Introduction

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

Prototype

public Chromosome getFittestChromosome() 

Source Link

Document

Access the fittest chromosome in this population.

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  ww w.j  ava2  s .co  m
 * @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.
 * //from   w ww .  j a va 2 s .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();
}