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

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

Introduction

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

Prototype

BinaryMutation

Source Link

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 www. j  ava 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

@Test
public void test() {
    // to test a stochastic algorithm is hard, so this will rather be an usage example

    // initialize a new genetic algorithm
    GeneticAlgorithm ga = new StatisticGeneticAlgorithm(new OnePointCrossover<Integer>(), CROSSOVER_RATE, // all selected chromosomes will be recombined (=crosssover)
            new BinaryMutation(), MUTATION_RATE, new TournamentSelection(TOURNAMENT_ARITY));

    Assert.assertEquals(0, ga.getGenerationsEvolved());

    // initial population
    Population initial = randomPopulation();
    // stopping conditions
    StoppingCondition stopCond = new FixedGenerationCount(NUM_GENERATIONS);

    // best initial chromosome
    Chromosome bestInitial = initial.getFittestChromosome();

    // run the algorithm
    Population finalPopulation = ga.evolve(initial, stopCond);

    // best chromosome from the final population
    Chromosome bestFinal = finalPopulation.getFittestChromosome();

    // the only thing we can test is whether the final solution is not worse than the initial
    // one/*  w  ww .  j a  va2 s  . c  o m*/
    // however, for some implementations of GA, this need not be true :)

    Assert.assertTrue(bestFinal.compareTo(bestInitial) > 0);
    Assert.assertEquals(NUM_GENERATIONS, ga.getGenerationsEvolved());
    System.out.println(bestFinal);

}

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

@Test
public void test() {
    // to test a stochastic algorithm is hard, so this will rather be an
    // usage example

    // initialize a new genetic algorithm
    GeneticAlgorithm ga = new GeneticAlgorithm(new OnePointCrossover<Integer>(), CROSSOVER_RATE, // all
            // selected
            // chromosomes
            // will be
            // recombined
            // (=crosssover)
            new BinaryMutation(), MUTATION_RATE, new TournamentSelection(TOURNAMENT_ARITY));

    Assert.assertEquals(0, ga.getGenerationsEvolved());

    // initial population
    Population initial = randomPopulation();
    // stopping conditions
    StoppingCondition stopCond = new FixedGenerationCount(NUM_GENERATIONS);

    // best initial chromosome
    Chromosome bestInitial = initial.getFittestChromosome();

    // run the algorithm
    Population finalPopulation = ga.evolve(initial, stopCond);

    // best chromosome from the final population
    Chromosome bestFinal = finalPopulation.getFittestChromosome();

    // the only thing we can test is whether the final solution is not worse
    // than the initial one
    // however, for some implementations of GA, this need not be true :)

    Assert.assertTrue(bestFinal.compareTo(bestInitial) > 0);
    Assert.assertEquals(NUM_GENERATIONS, ga.getGenerationsEvolved());

    System.out.println(bestFinal);
}