List of usage examples for org.apache.commons.math3.genetics Chromosome getFitness
public double getFitness()
From source file:it.units.malelab.sse.OperationsChromosome.java
@Override public int compareTo(Chromosome another) { int fitnessComparison = -Double.compare(getFitness(), another.getFitness()); int errorRatioComparison = -Double.compare(getStats().get(Evaluator.ResultType.ERROR_RATIO), ((OperationsChromosome) another).getStats().get(Evaluator.ResultType.ERROR_RATIO)); int opsComparison = -Double.compare(getStats().get(Evaluator.ResultType.AVG_OPS), ((OperationsChromosome) another).getStats().get(Evaluator.ResultType.AVG_OPS)); int sizeComparison = -Double.compare(getStats().get(Evaluator.ResultType.SIZE), ((OperationsChromosome) another).getStats().get(Evaluator.ResultType.SIZE)); if (fitnessComparison != 0) { return fitnessComparison; }/* w w w . jav a 2 s .c o m*/ if (errorRatioComparison != 0) { return errorRatioComparison; } if (opsComparison != 0) { return opsComparison; } return sizeComparison; }
From source file:org.apache.kylin.cube.cuboid.algorithm.generic.RouletteWheelSelection.java
@Override public ChromosomePair select(Population population) throws IllegalArgumentException { // create a copy of the chromosome list List<Chromosome> chromosomes = Lists.newArrayList(((ListPopulation) population).getChromosomes()); double maxFitness = 0; double totalFitness = 0; for (Chromosome o : chromosomes) { double fitness = o.getFitness(); totalFitness += fitness;/*from www.ja va 2s . c om*/ if (fitness > maxFitness) { maxFitness = fitness; } } return new ChromosomePair(rouletteWheel(chromosomes, totalFitness), rouletteWheel(chromosomes, totalFitness)); }
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; }//from ww w. j a va 2s.com runningScore += o.getFitness(); } return null; }
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 www. ja v a2 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 > 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 ww w. j av a 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 > 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); }