Example usage for org.apache.commons.math3.genetics GeneticAlgorithm getRandomGenerator

List of usage examples for org.apache.commons.math3.genetics GeneticAlgorithm getRandomGenerator

Introduction

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

Prototype

public static synchronized RandomGenerator getRandomGenerator() 

Source Link

Document

Returns the (static) random generator.

Usage

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

/**
 * Initializes a random population./*from  w  w  w.ja v  a2 s  .c o m*/
 * @param   len     lenth of chromosome
 * @param   popSize population size
 */
private static ElitisticListPopulation randomPopulation(int len, int popSize) {
    List<Chromosome> popList = new ArrayList<>();

    for (int i = 0; i < popSize; i++) {
        List<Integer> rList = new ArrayList<Integer>(len);

        // set each element randomly to 0 or 1
        for (int j = 0; j < len; j++)
            rList.add(GeneticAlgorithm.getRandomGenerator().nextInt(2));

        // count the number of 1 and set to m
        int M = 0;
        for (int e : rList)
            if (e == 1)
                M++;

        // update 0 with a random number according to the algorithm
        // random of (cj, cj + M)
        int c = 2;
        for (int j = 0; j < rList.size(); j++) {
            int e = rList.get(j);
            if (e == 1)
                continue;
            else {
                int val = c + GeneticAlgorithm.getRandomGenerator().nextInt(M);
                rList.set(j, val);
                c += M;
            }
        }

        Chromosome randChrom = new SensorIndividual(rList);
        popList.add(randChrom);

    }
    return new ElitisticListPopulation(popList, popList.size(), ELITISM_RATE);
}

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

/**
 * Initializes a random population with a fixed number of 1s (m)
 * @param   M       fixed number of OA//  w w  w.  j  a va 2s.  co m
 * @param   len     lenth of chromosome
 * @param   popSize population size
 */
private static ElitisticListPopulation randomPopulationWithFixedOA(int M, int len, int popSize) {
    List<Chromosome> popList = new ArrayList();

    for (int i = 0; i < popSize; i++) {
        List<Integer> rList = new ArrayList<Integer>(len);

        // Level 1 encoding with a fixed number of 1s
        for (int j = 0; j < len; j++)
            rList.add(0);
        int count = 0;
        while (count < M) {
            int index = GeneticAlgorithm.getRandomGenerator().nextInt(len);
            if (rList.get(index) == 0) {
                rList.set(index, 1);
                count++;
            }
        }

        // Level 2 encoding
        // update 0 with a random number according to the algorithm
        // random of (cj, cj + M)
        int c = 2;
        for (int j = 0; j < rList.size(); j++) {
            int e = rList.get(j);
            if (e == 1)
                continue;
            else {
                int val = c + GeneticAlgorithm.getRandomGenerator().nextInt(M);
                rList.set(j, val);
                c += M;
            }
        }

        Chromosome randChrom = new SensorIndividual(rList);
        popList.add(randChrom);

    }
    return new ElitisticListPopulation(popList, popList.size(), ELITISM_RATE);
}

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

/**
 * Mutate the given chromosome. Randomly changes one gene.
 *
 * @param original the original chromosome.
 * @return the mutated chromosome./*from   w  w  w. j  a  v a2s .  c  o  m*/
 * @throws IllegalArgumentException if <code>original</code> is not an instance of {@link BitsChromosome}.
 */
public Chromosome mutate(Chromosome original) throws IllegalArgumentException {
    if (!(original instanceof BitsChromosome)) {
        throw new MathIllegalArgumentException(
                new DummyLocalizable("bits mutation only works on BitsChromosome"));
    }

    BitsChromosome origChrom = (BitsChromosome) original;
    BitSet newNey = (BitSet) origChrom.getRepresentation().clone();

    // randomly select a gene
    int geneIndex = GeneticAlgorithm.getRandomGenerator().nextInt(origChrom.getLength());
    // change it
    newNey.set(geneIndex, !newNey.get(geneIndex));

    Chromosome newChrom = origChrom.newBitsChromosome(newNey);
    return newChrom;
}

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.//from  w w w .ja  va2  s  .  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
 */
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

private Chromosome rouletteWheel(final List<Chromosome> chromosomes, final double totalFitness) {
    double rnd = (GeneticAlgorithm.getRandomGenerator().nextDouble() * totalFitness);
    double runningScore = 0;
    for (Chromosome o : chromosomes) {
        if (rnd >= runningScore && rnd <= runningScore + o.getFitness()) {
            return o;
        }/*  www  .j  a  va2  s. c  o  m*/
        runningScore += o.getFitness();
    }
    return null;
}