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

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

Introduction

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

Prototype

public ElitisticListPopulation(final List<Chromosome> chromosomes, final int populationLimit,
        final double elitismRate)
        throws NullArgumentException, NotPositiveException, NumberIsTooLargeException, OutOfRangeException 

Source Link

Document

Creates a new ElitisticListPopulation instance.

Usage

From source file:it.units.malelab.sse.Main.java

public static void main(String[] args) throws IOException {
    Random random = new Random(1);
    VirtualMachine vm = new VirtualMachine(4, 4, 400);
    List<Map<Boolean, List<String>>> datasets = new ArrayList<>();
    datasets.add(Util.loadStrings("/home/eric/Documenti/esperimenti/datasets/Bills-Date.txt", random));
    datasets.add(Util.loadStrings("/home/eric/Documenti/esperimenti/datasets/Log-IP.txt", random));
    datasets.add(Util.loadStrings("/home/eric/Documenti/esperimenti/datasets/Twitter-URL.txt", random));

    Evaluator evaluator = new Evaluator(vm, datasets, 1, 10);

    MyGeneticAlgorithm ga = new MyGeneticAlgorithm(new OnePointCrossover<Integer>(), 0.2, new BinaryMutation(),
            0.6, new TournamentSelection(10), evaluator);
    MyGeneticAlgorithm.setRandomGenerator(new JDKRandomGenerator(1));

    List<Chromosome> chromosomes = new ArrayList<>();
    for (int i = 0; i < 2000; i++) {
        chromosomes.add(new OperationsChromosome(evaluator));
    }/*from w w  w . j  av a 2s. c  om*/
    Population population = new ElitisticListPopulation(chromosomes, chromosomes.size(), 0.99);
    Population finalPopulation = ga.evolve(population, new FixedGenerationCount(10000));
    List<Operation> operations = ((OperationsChromosome) finalPopulation.getFittestChromosome())
            .getOperations();
    for (int i = 0; i < operations.size(); i++) {
        System.out.printf("%4d: %s\n", i, operations.get(i));
    }

}

From source file:ga.GeneticAlgorithmTestBinary.java

/**
 * Initializes a random population./*  ww w  .j a v  a 2s .  co m*/
 */
private static ElitisticListPopulation randomPopulation() {
    List<Chromosome> popList = new LinkedList<>();

    for (int i = 0; i < POPULATION_SIZE; i++) {
        BinaryChromosome randChrom = new FindOnes(BinaryChromosome.randomBinaryRepresentation(DIMENSION));
        popList.add(randChrom);
    }
    return new ElitisticListPopulation(popList, popList.size(), ELITISM_RATE);
}

From source file:com.dlej.Main.java

public static Population getInitialPopulation() {
    List<Chromosome> popList = new LinkedList<Chromosome>();

    for (int i = 0; i < POPULATION_SIZE; i++) {
        popList.add(new StringChromosome(randomRepresentation(DIMENSION)));
    }//from   ww w.j av  a  2 s.c om
    return new ElitisticListPopulation(popList, 2 * popList.size(), ELITISM_RATE);
}

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

/**
 * Initializes a random population./*  ww w .j  a va 2 s.c  om*/
 * @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//ww  w.j a va 2  s . c  o  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.GeneticAlgorithm.java

protected Population initRandomPopulation(BitsChromosomeHelper helper) {
    List<Chromosome> chromosomeList = Lists.newArrayListWithCapacity(populationSize);

    while (chromosomeList.size() < populationSize) {
        BitSet bitSetForSelection = new BitSet(helper.getLength());

        //Initialize selection genes
        double totalSpace = 0;
        while (totalSpace < helper.spaceLimit) {
            int j = org.apache.commons.math3.genetics.GeneticAlgorithm.getRandomGenerator()
                    .nextInt(helper.getLength());
            if (!bitSetForSelection.get(j)) {
                totalSpace += helper.getCuboidSizeByBitIndex(j);
                bitSetForSelection.set(j);
            }//from   w ww .  ja v a2s  .c  o m
        }

        Chromosome chromosome = new BitsChromosome(bitSetForSelection, benefitPolicy.getInstance(), helper);
        chromosomeList.add(chromosome);
    }
    return new ElitisticListPopulation(chromosomeList, maxPopulationSize, 0.8);
}

From source file:p.lodz.playground.ApacheGeneticsTest.java

/**
 * Initializes a random population.//from   www.java 2s  .  c om
 */
private ElitisticListPopulation randomPopulation() {
    List<Chromosome> popList = new LinkedList<Chromosome>();
    for (int i = 0; i < POPULATION_SIZE; i++) {
        BinaryChromosome randChrom = new FindOnes(BinaryChromosome.randomBinaryRepresentation(DIMENSION));
        popList.add(randChrom);
    }
    return new ElitisticListPopulation(popList, popList.size(), ELITISM_RATE);
}

From source file:tmp.GACombGraphMoore.java

/**
 * Initializes a random population/*  www . j  a va2s  .c o  m*/
 */
private static ElitisticListPopulation randomPopulation(String... args) {
    List<Chromosome> popList = new ArrayList<Chromosome>();
    int i = 0;
    if (args != null && args.length > 0) {
        if (args.length == DIMENSION || ((args = args[0].split(",")) != null && args.length == DIMENSION)) {
            List<Integer> start = new ArrayList<>();
            for (String str : args) {
                start.add(Integer.parseInt(str.replaceAll("\\D", "").trim()));
            }
            System.out.println("Induced vector");
            System.out.println(start);
            Chromosome randChrom = new MinPermutations(RandomKey.inducedPermutation(sequence, start));
            System.out.println(randChrom);
            popList.add(randChrom);
            i++;
        }
    }
    for (; i < POPULATION_SIZE; i++) {
        Chromosome randChrom = new MinPermutations(RandomKey.randomPermutation(DIMENSION));
        popList.add(randChrom);
    }
    return new ElitisticListPopulation(popList, popList.size(), ELITISM_RATE);
}

From source file:tmp.GACombPermutation.java

/**
 * Initializes a random population/*from w w w. ja  v a  2 s .c o  m*/
 */
private static ElitisticListPopulation randomPopulation(String... args) {
    List<Chromosome> popList = new ArrayList<Chromosome>();
    int i = 0;
    if (args != null && args.length > 0) {
        if (args.length == DIMENSION || ((args = args[0].split(",")) != null && args.length == DIMENSION)) {
            List<Integer> start = new ArrayList<>();
            for (String str : args) {
                start.add(Integer.parseInt(str.trim().replaceAll("\\D", "")));
            }
            Chromosome randChrom = new MinPermutations(RandomKey.inducedPermutation(sequence, start));
            popList.add(randChrom);
            i++;
        }
    }
    for (; i < POPULATION_SIZE; i++) {
        Chromosome randChrom = new MinPermutations(RandomKey.randomPermutation(DIMENSION));
        popList.add(randChrom);
    }
    return new ElitisticListPopulation(popList, popList.size(), ELITISM_RATE);
}

From source file:tmp.GeneticAlgorithmTestPermutations.java

/**
 * Initializes a random population/* www  . j  a va 2 s . c o m*/
 */
private static ElitisticListPopulation randomPopulation() {
    List<Chromosome> popList = new ArrayList<Chromosome>();
    for (int i = 0; i < POPULATION_SIZE; i++) {
        Chromosome randChrom = new MinPermutations(RandomKey.randomPermutation(DIMENSION));
        popList.add(randChrom);
    }
    return new ElitisticListPopulation(popList, popList.size(), ELITISM_RATE);
}