@Test(dataProvider = "engineParams") public <G extends Gene<?, G>> void variableLengthChromosomes( final Genotype<G> gtf, final Alterer<G, Double> alterer, final Selector<G, Double> selector ) { final Random random = new Random(123); final Engine<G, Double> engine = Engine .builder(gt -> random.nextDouble(), gtf) .alterers(alterer) .selector(selector) .build(); final EvolutionResult<G, Double> result = engine.stream() .limit(50) .collect(EvolutionResult.toBestEvolutionResult()); }
BitChromosome.of(20, 0.15)) .populationSize(500) .selector(new RouletteWheelSelector<>()) .alterers( new Mutator<>(0.55),
public static void main(final String[] args) { final int μ = 5; final int λ = 20; final double p = 0.2; final Codec<Double, DoubleGene> codec = Codecs .ofScalar(DoubleRange.of(0, 1)); final Engine<DoubleGene, Double> engine = Engine .builder(MpLStrategy::fitness, codec) .populationSize(λ) .survivorsSize(μ) .selector(new TruncationSelector<>(μ)) .alterers(new Mutator<>(p)) .build(); System.out.println( codec.decode( engine.stream() .limit(100) .collect(EvolutionResult.toBestGenotype()) ) ); }
public static void main(final String[] args) { final Problem<double[], DoubleGene, Double> problem = Problem.of( v -> Math.sin(v[0])*Math.cos(v[1]), Codecs.ofVector(DoubleRange.of(0, 2*Math.PI), 2) ); final Engine<DoubleGene, Double> engine1 = Engine.builder(problem) .minimizing() .alterers(new Mutator<>(0.2)) .selector(new MonteCarloSelector<>()) .build(); final Engine<DoubleGene, Double> engine2 = Engine.builder(problem) .minimizing() .alterers( new Mutator<>(0.1), new MeanAlterer<>()) .selector(new RouletteWheelSelector<>()) .build(); final Genotype<DoubleGene> result = CyclicEngine.of( engine1.limit(50), engine2.limit(() -> Limits.bySteadyFitness(30))) .stream() .limit(Limits.bySteadyFitness(1000)) .collect(EvolutionResult.toBestGenotype()); System.out.println(result + ": " + problem.fitness().apply(problem.codec().decode(result))); }
public static void main(final String[] args) { final Problem<double[], DoubleGene, Double> problem = Problem.of( v -> Math.sin(v[0])*Math.cos(v[1]), Codecs.ofVector(DoubleRange.of(0, 2*Math.PI), 2) ); final Engine<DoubleGene, Double> engine1 = Engine.builder(problem) .minimizing() .alterers(new Mutator<>(0.2)) .selector(new MonteCarloSelector<>()) .build(); final Engine<DoubleGene, Double> engine2 = Engine.builder(problem) .minimizing() .alterers( new Mutator<>(0.1), new MeanAlterer<>()) .selector(new RouletteWheelSelector<>()) .build(); final Genotype<DoubleGene> result = ConcatEngine.of( engine1.limit(50), engine2.limit(() -> Limits.bySteadyFitness(30))) .stream() .collect(EvolutionResult.toBestGenotype()); System.out.println(result + ": " + problem.fitness().apply(problem.codec().decode(result))); }
@Test public void variableDoubleSum() { final Problem<int[], IntegerGene, Integer> problem = Problem.of( array -> IntStream.of(array).sum(), Codec.of( Genotype.of(IntegerChromosome.of(0, 100, IntRange.of(10, 100))), gt -> gt.getChromosome().as(IntegerChromosome.class).toArray() ) ); final Engine<IntegerGene, Integer> engine = Engine.builder(problem) .alterers( new Mutator<>(), new SwapMutator<>()) .selector(new TournamentSelector<>()) .minimizing() .build(); final int[] result = problem.codec().decode( engine.stream() .limit(100) .collect(EvolutionResult.toBestGenotype()) ); Assert.assertTrue(result.length < 50, "result length: " + result.length); //System.out.println(result.length); //System.out.println(Arrays.toString(result)); }
public static void main(String[] args) throws Exception { final CharSeq chars = CharSeq.of("A-Z "); final Factory<Genotype<CharacterGene>> gtf = Genotype.of( CharacterChromosome.of(chars, TARGET.length()) ); final Engine<CharacterGene, Integer> engine = Engine .builder(WeaselProgram::score, gtf) .populationSize(150) .selector(new WeaselSelector<>()) .offspringFraction(1) .alterers(new WeaselMutator<>(0.05)) .build(); final Phenotype<CharacterGene, Integer> result = engine.stream() .limit(byFitnessThreshold(TARGET.length() - 1)) .peek(r -> System.out.println( r.getTotalGenerations() + ": " + r.getBestPhenotype())) .collect(toBestPhenotype()); System.out.println(result); }
private static EvolutionStreamable<DoubleGene, Double> engine( final EvolutionResult<DoubleGene, Double> result, final Engine.Builder<DoubleGene, Double> builder ) { return var(result) < 0.2 ? builder.copy() .alterers(new Mutator<>(0.5)) .selector(new MonteCarloSelector<>()) .build() .limit(5) : builder.copy() .alterers( new Mutator<>(0.05), new MeanAlterer<>()) .selector(new RouletteWheelSelector<>()) .build() .limit(15); }
public static void main(final String[] args) throws InterruptedException { final Runner2<Long, BitGene, Double> runner = Runner2.of( KNAPSACK.builder() .selector(new MonteCarloSelector<>()) .build(), Limits::byFixedGeneration, KNAPSACK, Limits::byFixedGeneration, TRIAL_METER, Writer.text().map(Object::toString), Reader.text().map(Long::parseLong), args ); runner.start(); runner.join(); }