List of usage examples for org.apache.commons.math3.genetics Chromosome compareTo
public int compareTo(final Chromosome another)
From source file:eu.tsp.sal.WSN.java
/** * Algorithm run on a number of sensors = LENGTH * /*from w w w . ja v a 2s. c o m*/ * Population has POPULATION_SIZE individuals (SensorIndividual class) * each individual has LENGTH genes (~ sensor) * * * */ public static void main(String[] args) { /** * initialize a new genetic algorithm with * Crossover policy * CROSSOVER_RATE * Mutation policy * MUTATION_RATE * Selection Policy * */ SensorGeneticAlgorithm ga = new SensorGeneticAlgorithm(new NPointCrossover(2), CROSSOVER_RATE, // all selected chromosomes will be recombined (=crosssover) new SensorMutation(), MUTATION_RATE, new SensorTournamentSelection(TOURNAMENT_ARITY)); //assertEquals(0, ga.getGenerationsEvolved()); //System.out.println(ga.getGenerationsEvolved()); // initial population of POPULATION_SIZE SensorIndividual //Population initial = randomPopulation(LENGTH, POPULATION_SIZE); Population initial = randomPopulationWithFixedOA(OA_NUMBER, LENGTH, POPULATION_SIZE); printPopulation(initial); // stopping conditions StoppingCondition stopCond = new FixedGenerationCount(NUM_GENERATIONS); // best initial chromosome Chromosome bestInitial = initial.getFittestChromosome(); System.out.println("Best Individual in initial population (highest fitness) = " + bestInitial); System.out.println("Solution of the best individual = " + ((SensorIndividual) bestInitial).solution()); // run the algorithm Population finalPopulation = ga.evolve(initial, stopCond); // best SensorIndividual from the final population Chromosome bestFinal = finalPopulation.getFittestChromosome(); System.out.println("\nBest Individual in final population (highest fitness) = " + bestFinal); //System.out.println("Solution of the best individual = " + ((SensorIndividual) bestFinal).solution()); printSolution((SensorIndividual) bestFinal); // Assertion // assertTrue(bestFinal.compareTo(bestInitial) > 0); // assertEquals(NUM_GENERATIONS, ga.getGenerationsEvolved()); System.out.println((bestFinal.compareTo(bestInitial) > 0) ? "Final generation is better than the initial" : "Final generation is worse than the initial!!!!"); //System.out.println(ga.getGenerationsEvolved()); }
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/*from ww w . j a v a2s . 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); }
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); }