Example usage for org.apache.commons.math3.exception.util LocalizedFormats TOO_LARGE_TOURNAMENT_ARITY

List of usage examples for org.apache.commons.math3.exception.util LocalizedFormats TOO_LARGE_TOURNAMENT_ARITY

Introduction

In this page you can find the example usage for org.apache.commons.math3.exception.util LocalizedFormats TOO_LARGE_TOURNAMENT_ARITY.

Prototype

LocalizedFormats TOO_LARGE_TOURNAMENT_ARITY

To view the source code for org.apache.commons.math3.exception.util LocalizedFormats TOO_LARGE_TOURNAMENT_ARITY.

Click Source Link

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  ww  . ja  va 2  s .  c o 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.
 * /*w w  w.  j a  v a 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();
}