List of usage examples for org.apache.commons.math3.genetics Population getPopulationSize
int getPopulationSize();
From source file:eu.tsp.sal.SensorGeneticAlgorithm.java
/** * Evolve the given population into the next generation. * <p>/*from w w w . j a v a2 s . co m*/ * <ol> * <li>Get nextGeneration population to fill from <code>current</code> * generation, using its nextGeneration method</li> * <li>Loop until new generation is filled:</li> * <ul><li>Apply configured SelectionPolicy to select a pair of parents * from <code>current</code></li> * <li>With probability = {@link #getCrossoverRate()}, apply * configured {@link CrossoverPolicy} to parents</li> * <li>With probability = {@link #getMutationRate()}, apply * configured {@link MutationPolicy} to each of the offspring</li> * <li>Add offspring individually to nextGeneration, * space permitting</li> * </ul> * <li>Return nextGeneration</li> * </ol> * * @param current the current population. * @return the population for the next generation. */ public Population nextGeneration(final Population current) { Population nextGeneration = current.nextGeneration(); RandomGenerator randGen = getRandomGenerator(); while (nextGeneration.getPopulationSize() < nextGeneration.getPopulationLimit()) { // select parent chromosomes ChromosomePair pair = getSelectionPolicy().select(current); //System.out.println("*****" + pair); // crossover? if (randGen.nextDouble() < getCrossoverRate()) { // apply crossover policy to create two offspring pair = getCrossoverPolicy().crossover(pair.getFirst(), pair.getSecond()); } // mutation? if (randGen.nextDouble() < getMutationRate()) { // apply mutation policy to the chromosomes pair = new ChromosomePair(getMutationPolicy().mutate(pair.getFirst()), getMutationPolicy().mutate(pair.getSecond())); } // add the first chromosome to the population nextGeneration.addChromosome(pair.getFirst()); // is there still a place for the second chromosome? if (nextGeneration.getPopulationSize() < nextGeneration.getPopulationLimit()) { // add the second chromosome to the population nextGeneration.addChromosome(pair.getSecond()); } } System.out.println("\nGeneration (iteration): " + (generationsEvolved + 1)); WSN.printPopulation(nextGeneration); return nextGeneration; }
From source file:it.units.malelab.sse.MyGeneticAlgorithm.java
@Override public Population evolve(Population initial, StoppingCondition condition) { Population current = initial; generationsEvolved = 0;/*from w w w .jav a 2s. c om*/ while (!condition.isSatisfied(current)) { current = nextGeneration(current); generationsEvolved++; //obtain stats List<EnumMap<Evaluator.ResultType, Double>> statsList = new ArrayList<>(current.getPopulationSize()); Iterator<Chromosome> iterator = current.iterator(); while (iterator.hasNext()) { OperationsChromosome chromosome = (OperationsChromosome) iterator.next(); EnumMap<Evaluator.ResultType, Double> stats = chromosome.getStats(); if (stats.containsKey(Evaluator.ResultType.OVERLAPNESS)) { statsList.add(stats); } } Collections.sort(statsList, new Comparator<EnumMap<Evaluator.ResultType, Double>>() { @Override public int compare(EnumMap<Evaluator.ResultType, Double> stats1, EnumMap<Evaluator.ResultType, Double> stats2) { return Double.compare(stats1.get(Evaluator.ResultType.OVERLAPNESS), stats2.get(Evaluator.ResultType.OVERLAPNESS)); } }); EnumMap<Evaluator.ResultType, Double> bestStats = statsList.get(0); EnumMap<Evaluator.ResultType, Double> top10Stats = mean(statsList.subList(0, 10)); EnumMap<Evaluator.ResultType, Double> allStats = mean(statsList); System.out.printf("ovp=%5.3f/%5.3f/%5.3f ", bestStats.get(Evaluator.ResultType.OVERLAPNESS), top10Stats.get(Evaluator.ResultType.OVERLAPNESS), allStats.get(Evaluator.ResultType.OVERLAPNESS)); System.out.printf("ops=%4.0f/%4.0f/%4.0f ", bestStats.get(Evaluator.ResultType.AVG_OPS), top10Stats.get(Evaluator.ResultType.AVG_OPS), allStats.get(Evaluator.ResultType.AVG_OPS)); System.out.printf("mfp=%4.0f/%4.0f/%4.0f ", bestStats.get(Evaluator.ResultType.AVG_FOOTPRINT), top10Stats.get(Evaluator.ResultType.AVG_FOOTPRINT), allStats.get(Evaluator.ResultType.AVG_FOOTPRINT)); System.out.printf("err=%5.3f/%5.3f/%5.3f ", bestStats.get(Evaluator.ResultType.ERROR_RATIO), top10Stats.get(Evaluator.ResultType.ERROR_RATIO), allStats.get(Evaluator.ResultType.ERROR_RATIO)); System.out.printf("size=%3.0f/%3.0f/%3.0f ", bestStats.get(Evaluator.ResultType.SIZE), top10Stats.get(Evaluator.ResultType.SIZE), allStats.get(Evaluator.ResultType.SIZE)); System.out.printf("evals=%8d\n", evaluator.getEvaluatedCount()); //System.out.println(evaluator.getErrorCodes()); } return current; }