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

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

Introduction

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

Prototype

public GeneticAlgorithm(final CrossoverPolicy crossoverPolicy, final double crossoverRate,
        final MutationPolicy mutationPolicy, final double mutationRate, final SelectionPolicy selectionPolicy)
        throws OutOfRangeException 

Source Link

Document

Create a new genetic algorithm.

Usage

From source file:com.dlej.Main.java

public static void main(String[] args) {
    long startTime = System.currentTimeMillis();

    // initialize a new genetic algorithm
    GeneticAlgorithm ga = new GeneticAlgorithm(new OnePointCrossover<Character>(), CROSSOVER_RATE,
            new RandomCharacterMutation(), MUTATION_RATE, new TournamentSelection(TOURNAMENT_ARITY));

    // initial population
    Population initial = getInitialPopulation();

    // stopping condition
    StoppingCondition stoppingCondition = new StoppingCondition() {

        int generation = 0;

        @Override/* w w w .ja  va2s.c  om*/
        public boolean isSatisfied(Population population) {
            Chromosome fittestChromosome = population.getFittestChromosome();

            if (generation == 1 || generation % 10 == 0) {
                System.out.println("Generation " + generation + ": " + fittestChromosome.toString());
            }
            generation++;

            double fitness = fittestChromosome.fitness();
            if (Precision.equals(fitness, 0.0, 1e-6)) {
                return true;
            } else {
                return false;
            }
        }
    };

    System.out.println("Starting evolution ...");

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

    // Get the end time for the simulation.
    long endTime = System.currentTimeMillis();

    // best chromosome from the final population
    Chromosome best = finalPopulation.getFittestChromosome();
    System.out.println("Generation " + ga.getGenerationsEvolved() + ": " + best.toString());
    System.out.println("Total execution time: " + (endTime - startTime) + "ms");
}

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);
}

From source file:tmp.GACombGraphMoore.java

public static void main(String... args) {
    // 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,
            new RandomKeyMutation(), MUTATION_RATE, new TournamentSelection(TOURNAMENT_ARITY));

    // initial population
    Population initial = randomPopulation(args);

    System.out.println("Initial population:");
    System.out.println(initial.getFittestChromosome());
    long lastime = System.currentTimeMillis();

    // stopping conditions
    StoppingCondition stopCond = new FixedGenerationCount(NUM_GENERATIONS);
    // best initial chromosome
    Chromosome bestInitial = initial.getFittestChromosome();

    // run the algorithm
    //        Population finalPopulation = ga.evolve(initial, stopCond);
    double bestfit = initial.getFittestChromosome().fitness();
    Population current = initial;//from  w  w w.j a  va  2 s.  c  om
    int generationsEvolved = 0;
    //        while (!stopCond.isSatisfied(current)) {
    while (bestfit != 0.0) {
        current = ga.nextGeneration(current);
        generationsEvolved++;
        Chromosome bestFinal = current.getFittestChromosome();
        //            System.out.print(bestFinal);
        double atualfit = bestFinal.getFitness();
        if (atualfit > bestfit || System.currentTimeMillis() - lastime > HOUR) {
            bestfit = atualfit;
            //                String strbest = generationsEvolved + "-f=" + atualfit + "-" + ((MinPermutations) bestFinal).decode(sequence).toString().replaceAll(" ", "") + "\n";
            String strbest = generationsEvolved + "-f=" + atualfit;
            //                UtilTmp.dumpString(strbest);
            System.out.println(strbest);
            //                strbest = bestFinal.toString();
            //                UtilTmp.dumpString(strbest);
            System.out.println(strbest);
            //                System.out.println();
            lastime = System.currentTimeMillis();
        }
    }

    // best chromosome from the final population
    Chromosome bestFinal = current.getFittestChromosome();
    System.out.println("Best initial:");
    System.out.println(bestInitial);
    System.out.println(((MinPermutations) bestInitial).decode(sequence));
    System.out.println("Best result:");
    System.out.println(bestFinal);
    System.out.println(((MinPermutations) bestFinal).decode(sequence));

    // 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);
    //System.out.println(bestInitial);
    //System.out.println(bestFinal);
}

From source file:tmp.GACombPermutation.java

public static void main(String... args) {
    // 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,
            new RandomKeyMutation(), MUTATION_RATE, new TournamentSelection(TOURNAMENT_ARITY));

    // initial population
    Population initial = randomPopulation(args);
    System.out.print("Graph e-");
    System.out.print(graph.getEdgeCount());
    System.out.print(" Subgraph e-");
    System.out.println(subgraph.getEdgeCount());

    System.out.println("Initial population:");
    System.out.println(initial.getFittestChromosome());
    long lastime = System.currentTimeMillis();

    // stopping conditions
    StoppingCondition stopCond = new FixedGenerationCount(NUM_GENERATIONS);

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

    // run the algorithm
    //        Population finalPopulation = ga.evolve(initial, stopCond);
    double bestfit = initial.getFittestChromosome().fitness();
    Population current = initial;/*from w  w  w  .j  av  a  2  s .  c  o m*/
    int generationsEvolved = 0;
    //        while (!stopCond.isSatisfied(current)) {
    while (bestfit != 0.0) {
        current = ga.nextGeneration(current);
        generationsEvolved++;
        Chromosome bestFinal = current.getFittestChromosome();
        //            System.out.print(bestFinal);
        double atualfit = bestFinal.getFitness();
        if (atualfit > bestfit || System.currentTimeMillis() - lastime > UtilTmp.ALERT_HOUR) {
            lastime = System.currentTimeMillis();
            System.out.print(generationsEvolved);
            System.out.print("-");
            bestfit = atualfit;
            String strbest = bestFinal.toString() + "\n";
            UtilTmp.dumpString(strbest);
            System.out.print(strbest);
            System.out.println();
        }
    }

    // best chromosome from the final population
    Chromosome bestFinal = current.getFittestChromosome();
    System.out.println("Best result:");
    System.out.println(bestFinal);

    // 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);
    //System.out.println(bestInitial);
    //System.out.println(bestFinal);
}

From source file:tmp.GeneticAlgorithmTestPermutations.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,
            new RandomKeyMutation(), MUTATION_RATE, new TournamentSelection(TOURNAMENT_ARITY));

    // initial population
    Population initial = randomPopulation();
    System.out.print("Initial population");
    System.out.println(initial.getFittestChromosome());

    // 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();
    System.out.print("Best result: ");
    System.out.println(bestFinal);

    // 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);

    //System.out.println(bestInitial);
    //System.out.println(bestFinal);
}