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

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

Introduction

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

Prototype

public ChromosomePair(final Chromosome c1, final Chromosome c2) 

Source Link

Document

Create a chromosome pair.

Usage

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

/**
 * Select two chromosomes from the population. Each of the two selected
 * chromosomes is selected based on n-ary tournament -- this is done by
 * drawing {@link #arity} random chromosomes without replacement from the
 * population, and then selecting the fittest chromosome among them.
 *
 * @param population the population from which the chromosomes are chosen.
 * @return the selected chromosomes./*from  w  w  w  . j  a v  a 2s.  co m*/
 * @throws MathIllegalArgumentException if the tournament arity is bigger than the population size
 */
public ChromosomePair select(final Population population) throws MathIllegalArgumentException {
    return new ChromosomePair(tournament((ListPopulation) population), tournament((ListPopulation) population));
}

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

/**
 * Evolve the given population into the next generation.
 * <p>/*from   www .  j  ava  2  s.c o m*/
 * <ol>
 *  <li>Get nextGeneration population to fill from <code>current</code>
 *      generation, using its nextGeneration method</li>
 *  <li>Loop until new generation is filled:</li>
 *  <ul><li>Apply configured SelectionPolicy to select a pair of parents
 *          from <code>current</code></li>
 *      <li>With probability = {@link #getCrossoverRate()}, apply
 *          configured {@link CrossoverPolicy} to parents</li>
 *      <li>With probability = {@link #getMutationRate()}, apply
 *          configured {@link MutationPolicy} to each of the offspring</li>
 *      <li>Add offspring individually to nextGeneration,
 *          space permitting</li>
 *  </ul>
 *  <li>Return nextGeneration</li>
 * </ol>
 *
 * @param current the current population.
 * @return the population for the next generation.
 */
public Population nextGeneration(final Population current) {
    Population nextGeneration = current.nextGeneration();

    RandomGenerator randGen = getRandomGenerator();

    while (nextGeneration.getPopulationSize() < nextGeneration.getPopulationLimit()) {
        // select parent chromosomes
        ChromosomePair pair = getSelectionPolicy().select(current);
        //System.out.println("*****" + pair);

        // crossover?
        if (randGen.nextDouble() < getCrossoverRate()) {
            // apply crossover policy to create two offspring
            pair = getCrossoverPolicy().crossover(pair.getFirst(), pair.getSecond());
        }

        // mutation?
        if (randGen.nextDouble() < getMutationRate()) {
            // apply mutation policy to the chromosomes
            pair = new ChromosomePair(getMutationPolicy().mutate(pair.getFirst()),
                    getMutationPolicy().mutate(pair.getSecond()));
        }

        // add the first chromosome to the population
        nextGeneration.addChromosome(pair.getFirst());
        // is there still a place for the second chromosome?
        if (nextGeneration.getPopulationSize() < nextGeneration.getPopulationLimit()) {
            // add the second chromosome to the population
            nextGeneration.addChromosome(pair.getSecond());
        }
    }

    System.out.println("\nGeneration (iteration): " + (generationsEvolved + 1));
    WSN.printPopulation(nextGeneration);

    return nextGeneration;
}

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

/**
 * Helper for {@link #crossover(Chromosome, Chromosome)}. Performs the actual crossover.
 *
 * @param first the first chromosome//from ww w . j  av a 2s  .co m
 * @param second the second chromosome
 * @return the pair of new chromosomes that resulted from the crossover
 * @throws DimensionMismatchException if the length of the two chromosomes is different
 * @throws NumberIsTooLargeException if the number of crossoverPoints is too large for the actual chromosomes
 */
private ChromosomePair mate(final AbstractListChromosome<T> first, final AbstractListChromosome<T> second)
        throws DimensionMismatchException, NumberIsTooLargeException {

    final int length = first.getLength();
    if (length != second.getLength()) {
        throw new DimensionMismatchException(second.getLength(), length);
    }
    if (crossoverPoints >= length) {
        throw new NumberIsTooLargeException(crossoverPoints, length, false);
    }

    // array representations of the parents
    final List<T> parent1Rep = first.getRepresentation();
    final List<T> parent2Rep = second.getRepresentation();
    // and of the children
    final List<T> child1Rep = new ArrayList<T>(length);
    final List<T> child2Rep = new ArrayList<T>(length);

    final RandomGenerator random = SensorGeneticAlgorithm.getRandomGenerator();

    List<T> c1 = child1Rep;
    List<T> c2 = child2Rep;

    int remainingPoints = crossoverPoints;
    int lastIndex = 0;
    for (int i = 0; i < crossoverPoints; i++, remainingPoints--) {
        // select the next crossover point at random
        final int crossoverIndex = 1 + lastIndex + random.nextInt(length - lastIndex - remainingPoints);

        // copy the current segment
        for (int j = lastIndex; j < crossoverIndex; j++) {
            c1.add(parent1Rep.get(j));
            c2.add(parent2Rep.get(j));
        }

        // swap the children for the next segment
        List<T> tmp = c1;
        c1 = c2;
        c2 = tmp;

        lastIndex = crossoverIndex;
    }

    // copy the last segment
    for (int j = lastIndex; j < length; j++) {
        c1.add(parent1Rep.get(j));
        c2.add(parent2Rep.get(j));
    }

    return new ChromosomePair(first.newFixedLengthChromosome(child1Rep),
            second.newFixedLengthChromosome(child2Rep));
}

From source file:org.apache.kylin.cube.cuboid.algorithm.generic.BitsOnePointCrossover.java

/**
 * Helper for {@link #crossover(Chromosome, Chromosome)}. Performs the actual crossover.
 *
 * @param first  the first chromosome./*  www . ja va 2 s . c om*/
 * @param second the second chromosome.
 * @return the pair of new chromosomes that resulted from the crossover.
 * @throws DimensionMismatchException if the length of the two chromosomes is different
 */
private ChromosomePair crossover(final BitsChromosome first, final BitsChromosome second)
        throws DimensionMismatchException {
    final int length = first.getLength();
    if (length != second.getLength()) {
        throw new DimensionMismatchException(second.getLength(), length);
    }

    final BitSet parent1Key = first.getRepresentation();
    final BitSet parent2Key = second.getRepresentation();

    final BitSet child1Key = new BitSet(length);
    final BitSet child2Key = new BitSet(length);

    // select a crossover point at random (0 and length makes no sense)
    final int crossoverIndex = 1 + (GeneticAlgorithm.getRandomGenerator().nextInt(length - 2));

    BitSet a = (BitSet) parent1Key.clone();
    a.clear(crossoverIndex, length);
    BitSet b = (BitSet) parent2Key.clone();
    b.clear(0, crossoverIndex);

    BitSet c = (BitSet) parent1Key.clone();
    c.clear(crossoverIndex, length);
    BitSet d = (BitSet) parent2Key.clone();
    d.clear(0, crossoverIndex);

    child1Key.or(a);
    child1Key.or(d);

    child2Key.or(c);
    child2Key.or(b);
    return new ChromosomePair(first.newBitsChromosome(child1Key), second.newBitsChromosome(child2Key));
}

From source file:org.apache.kylin.cube.cuboid.algorithm.generic.RouletteWheelSelection.java

@Override
public ChromosomePair select(Population population) throws IllegalArgumentException {
    // create a copy of the chromosome list
    List<Chromosome> chromosomes = Lists.newArrayList(((ListPopulation) population).getChromosomes());

    double maxFitness = 0;
    double totalFitness = 0;
    for (Chromosome o : chromosomes) {
        double fitness = o.getFitness();
        totalFitness += fitness;/*from   w  ww.  j a v  a2 s .  com*/
        if (fitness > maxFitness) {
            maxFitness = fitness;
        }
    }
    return new ChromosomePair(rouletteWheel(chromosomes, totalFitness),
            rouletteWheel(chromosomes, totalFitness));
}

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

/**
 * Evolve the given population into the next generation.
 * <p>/* w w  w .  ja  v  a2s  . c  o  m*/
 * <ol>
 * <li>Get nextGeneration population to fill from <code>current</code> generation, using its nextGeneration method</li>
 * <li>Loop until new generation is filled:</li>
 * <ul>
 * <li>Apply configured SelectionPolicy to select a pair of parents from <code>current</code></li>
 * <li>With probability = {@link #getCrossoverRate()}, apply configured {@link CrossoverPolicy} to parents</li>
 * <li>With probability = {@link #getMutationRate()}, apply configured {@link MutationPolicy} to each of the offspring</li>
 * <li>Add offspring individually to nextGeneration, space permitting</li>
 * </ul>
 * <li>Return nextGeneration</li>
 * </ol>
 * 
 * @param current
 *            the current population.
 * @return the population for the next generation.
 */
public Population nextGeneration(final Population pop) {
    SOSListPopulation current = (SOSListPopulation) pop;
    SOSListPopulation nextGeneration = (SOSListPopulation) current.nextGeneration();

    RandomGenerator randGen = getRandomGenerator();
    while (nextGeneration.getPopulationSize() < nextGeneration.getPopulationLimit()) {
        // select parent chromosomes
        ChromosomePair pair = getSelectionPolicy().select(current);

        // crossover?
        if (randGen.nextDouble() < getCrossoverRate()) {
            // apply crossover policy to create two offspring
            pair = getCrossoverPolicy().crossover(pair.getFirst(), pair.getSecond());
        }

        // mutation?
        if (randGen.nextDouble() < getMutationRate()) {
            // apply mutation policy to the chromosomes
            pair = new ChromosomePair(getMutationPolicy().mutate(pair.getFirst()),
                    getMutationPolicy().mutate(pair.getSecond()));
        }

        // add the first chromosome to the population
        nextGeneration.addChromosome(pair.getFirst());
        // is there still a place for the second chromosome?
        if (nextGeneration.getPopulationSize() < nextGeneration.getPopulationLimit()) {
            // add the second chromosome to the population
            nextGeneration.addChromosome(pair.getSecond());
        }
    }
    nextGeneration.setPopulationLimit(nextGeneration.getPopulationLimit() + current.getPopulationSize());

    nextGeneration.addChromosomes(current.getChromosomes());//adding the current population

    nextGeneration = (SOSListPopulation) sosSelect.selectNextGeneration(nextGeneration);
    nextGeneration.setPopulationLimit(current.getPopulationLimit());
    return nextGeneration;
}

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

/**
 * Select two chromosomes from the population. Each of the two selected
 * chromosomes is selected based on n-ary tournament -- this is done by
 * drawing {@link #arity} random chromosomes without replacement from the
 * population, and then selecting the fittest chromosome among them.
 * /*ww  w  . j  av a2  s. c om*/
 * @param population
 *            the population from which the chromosomes are chosen.
 * @return the selected chromosomes.
 * @throws MathIllegalArgumentException
 *             if the tournament arity is bigger than the population size
 */
@Override
public ChromosomePair select(final Population population) throws MathIllegalArgumentException {
    return new ChromosomePair(tournament((ListPopulation) population), tournament((ListPopulation) population));
}