@Override public Alterer<DoubleGene, Double> newAlterer(final double p) { return new SinglePointCrossover<>(p); }
public static void main(final String[] args) { final Engine<CharacterGene, Integer> engine = Engine.builder(PROBLEM) .populationSize(500) .survivorsSelector(new StochasticUniversalSelector<>()) .offspringSelector(new TournamentSelector<>(5)) .alterers( new Mutator<>(0.1), new SinglePointCrossover<>(0.5)) .build(); final Phenotype<CharacterGene, Integer> result = engine.stream() .limit(100) .collect(toBestPhenotype()); System.out.println(result); }
public static void main(final String[] args) { final Knapsack knapsack = Knapsack.of(15, new Random(123)); // Configure and build the evolution engine. final Engine<BitGene, Double> engine = Engine.builder(knapsack) .populationSize(500) .survivorsSelector(new TournamentSelector<>(5)) .offspringSelector(new RouletteWheelSelector<>()) .alterers( new Mutator<>(0.115), new SinglePointCrossover<>(0.16)) .build(); // Create evolution statistics consumer. final EvolutionStatistics<Double, ?> statistics = EvolutionStatistics.ofNumber(); final Phenotype<BitGene, Double> best = engine.stream() // Truncate the evolution stream after 7 "steady" // generations. .limit(bySteadyFitness(7)) // The evolution will stop after maximal 100 // generations. .limit(100) // Update the evaluation statistics after // each generation .peek(statistics) // Collect (reduce) the evolution stream to // its best phenotype. .collect(toBestPhenotype()); System.out.println(statistics); System.out.println(best); }
.alterers( new Mutator<>(0.55), new SinglePointCrossover<>(0.06)) .build();
.alterers( new Mutator<>(0.115), new SinglePointCrossover<>(0.16)) .build();
public static void main(String[] args) throws IOException { final Knapsack knapsack = Knapsack.of(15, new Random(123)); final Engine<BitGene, Double> engine = Engine.builder(knapsack) .populationSize(500) .alterers( new Mutator<>(0.115), new SinglePointCrossover<>(0.16)) .evaluator(BatchEvalKnapsack::batchEval) .evaluator(pop -> { pop.forEach(Phenotype::evaluate); return pop.asISeq(); }) .build(); final Phenotype<BitGene, Double> best = engine.stream() .limit(bySteadyFitness(20)) .collect(toBestPhenotype()); System.out.println(best); }
public static void main(final String[] args) { final Knapsack knapsack = Knapsack.of(15, new Random(123)); // The base engine tries to approximate to good solution in current // environment. final Engine<BitGene, Double> baseEngine = Engine.builder(knapsack) .populationSize(500) .alterers( new Mutator<>(0.115), new SinglePointCrossover<>(0.16)) .build(); // The 'diversity' engine tries to broaden the search space again. final Engine<BitGene, Double> diversityEngine = baseEngine.builder() .alterers(new Mutator<>(0.5)) .build(); // Concatenates the two engines into one cyclic engine. final EvolutionStreamable<BitGene, Double> engine = CyclicEngine.of( // This engine stops the evolution after 10 non-improving // generations and hands over to the diversity engine. baseEngine.limit(() -> Limits.bySteadyFitness(10)), // The higher mutation rate of this engine broadens the search // space for 15 generations and hands over to the base engine. diversityEngine.limit(15) ); final EvolutionResult<BitGene, Double> best = engine.stream() // The evolution is stopped after 50 non-improving generations. .limit(bySteadyFitness(50)) .collect(toBestEvolutionResult()); System.out.println(best.getTotalGenerations()); System.out.println(best.getBestPhenotype()); }
public static void main(final String[] args) { final RectFill problem = new RectFill(new Rect(0, 100, 0, 100)); final Engine<AnyGene<Rect>, Double> engine = Engine.builder(problem) .individualCreationRetries(10) .genotypeValidator(gt -> true) .offspringSelector(new RouletteWheelSelector<>()) .alterers( new SwapMutator<>(), new SinglePointCrossover<>()) .build(); final ISeq<Rect> best = problem.codec().decode( engine.stream() .limit(byFixedGeneration(10)) .collect(EvolutionResult.toBestGenotype()) ); System.out.println(best); }
/** * Create a new {@link Engine} for solving the {@link Knapsack} problem. The * engine is used for testing purpose. * * @see Knapsack#of(int, Random) * * @param populationSize the population size of the created engine * @param random the random engine used for creating the {@link Knapsack} * problem instance * @return a new {@link Knapsack} solving evolution {@link Engine} */ public static Engine<BitGene, Double> knapsack( final int populationSize, final Random random ) { // Search space fo 2^250 ~ 10^75. final Knapsack knapsack = Knapsack.of(250, random); // Configure and build the evolution engine. return Engine.builder(knapsack) .populationSize(populationSize) .survivorsSelector(new TournamentSelector<>(5)) .offspringSelector(new RouletteWheelSelector<>()) .alterers( new Mutator<>(0.03), new SinglePointCrossover<>(0.125)) .build(); }
using(new ConstRandom(rv1), r -> { final SinglePointCrossover<CharacterGene, Double> crossover = new SinglePointCrossover<>();