@Override public Alterer<DoubleGene, Double> newAlterer(final double p) { return new SinglePointCrossover<>(p); }
@Override protected int crossover(final MSeq<G> that, final MSeq<G> other) { final Random random = RandomRegistry.getRandom(); final int index = random.nextInt(min(that.length(), other.length())); crossover(that, other, index); return 2; }
new SinglePointCrossover<>(p); final double mean = crossover.getOrder()*npopulation*p; .alter(population, 1) .getAlterations(); histogram.accept(alterations);
using(new ConstRandom(rv1), r -> { final SinglePointCrossover<CharacterGene, Double> crossover = new SinglePointCrossover<>(); crossover.crossover(g1c, g2c); MSeq<CharacterGene> g1c2 = g1.copy(); MSeq<CharacterGene> g2c2 = g2.copy(); crossover.crossover(g1c2, g2c2); Assert.assertEquals(g1c2, g2); Assert.assertEquals(g2c2, g1); MSeq<CharacterGene> g1c3 = g1.copy(); MSeq<CharacterGene> g2c3 = g2.copy(); crossover.crossover(g1c3, g2c3); Assert.assertEquals(g1c3.subSeq(0, rv3), g1.subSeq(0, rv3)); Assert.assertEquals(g1c3.subSeq(rv3), g2.subSeq(rv3)); MSeq<CharacterGene> g1c4 = g1.copy(); MSeq<CharacterGene> g2c4 = g2.copy(); crossover.crossover(g1c4, g2c); Assert.assertEquals(g1c4, g1); Assert.assertEquals(g2c4, g2);
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); }
@Test public void singlePointCrossoverConsistency() { final ISeq<Character> a = CharSeq.toISeq("1234567890"); final ISeq<Character> b = CharSeq.toISeq("ABCDEFGHIJ"); for (int i = 0; i < a.length() + 1; ++i) { final MSeq<Character> ma1 = a.copy(); final MSeq<Character> mb1 = b.copy(); final MSeq<Character> ma2 = a.copy(); final MSeq<Character> mb2 = b.copy(); MultiPointCrossover.crossover(ma1, mb1, new int[]{i}); SinglePointCrossover.crossover(ma2, mb2, i); Assert.assertEquals(ma1, ma2); Assert.assertEquals(mb1, mb2); } }
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(); }