public static void main(final String[] args) { final Engine<DoubleGene, Double> engine = Engine .builder(DynamicGenotype::fitness, ENCODING) .alterers(new DynamicMutator<>(0.25)) .build(); final EvolutionResult<DoubleGene, Double> result = engine.stream() .limit(20) .collect(EvolutionResult.toBestEvolutionResult()); System.out.println(result.getBestFitness()); }
public static void main(final String[] args) { RandomRegistry.setRandom(new Random()); final Engine<EnumGene<Integer>, Integer> engine = Engine .builder( Sorting::length, PermutationChromosome.ofInteger(20)) .optimize(Optimize.MINIMUM) .populationSize(1000) //.survivorsSelector(new RouletteWheelSelector<>()) //.offspringSelector(new TruncationSelector<>()) .offspringFraction(0.9) .alterers( new SwapMutator<>(0.01), new PartiallyMatchedCrossover<>(0.3)) .build(); final EvolutionStatistics<Integer, DoubleMomentStatistics> statistics = EvolutionStatistics.ofNumber(); final EvolutionResult<EnumGene<Integer>, Integer> result = engine.stream() .limit(Limits.bySteadyFitness(100)) .limit(2500) .peek(statistics) .collect(EvolutionResult.toBestEvolutionResult()); System.out.println(statistics); System.out.println(result.getBestPhenotype()); }
@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()); }
/** * Create a new builder, with the current configuration. * * @since 3.1 * * @return a new builder, with the current configuration */ @Override public Builder<G, C> copy() { return new Builder<G, C>(_genotypeFactory, _fitnessFunction) .alterers(_alterer) .clock(_clock) .executor(_executor) .evaluator(_evaluator) .fitnessScaler(_fitnessScaler) .maximalPhenotypeAge(_maximalPhenotypeAge) .offspringFraction(_offspringFraction) .offspringSelector(_offspringSelector) .phenotypeValidator(_validator) .optimize(_optimize) .populationSize(_populationSize) .survivorsSelector(_survivorsSelector) .individualCreationRetries(_individualCreationRetries) .mapping(_mapper); }
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)); }
/** * Create a new evolution {@code Engine.Builder} initialized with the values * of the current evolution {@code Engine}. With this method, the evolution * engine can serve as a template for a new one. * * @return a new engine builder */ public Builder<G, C> builder() { return new Builder<G, C>(_genotypeFactory, _fitnessFunction) .alterers(_alterer) .clock(_clock) .evaluator(_evaluator) .executor(_executor.get()) .fitnessScaler(_fitnessScaler) .maximalPhenotypeAge(_maximalPhenotypeAge) .offspringFraction((double)_offspringCount/(double)getPopulationSize()) .offspringSelector(_offspringSelector) .optimize(_optimize) .phenotypeValidator(_validator) .populationSize(getPopulationSize()) .survivorsSelector(_survivorsSelector) .individualCreationRetries(_individualCreationRetries) .mapping(_mapper); }
public static void main(final String[] args) { final Engine<ProgramGene<Double>, Double> engine = Engine .builder(Example::error, CODEC) .minimizing() .alterers( new SingleNodeCrossover<>(), new Mutator<>()) .build(); final ProgramGene<Double> program = engine.stream() .limit(3000) .collect(EvolutionResult.toBestGenotype()) .getGene(); System.out.println(Tree.toString(program)); for (int i = 0; i < SAMPLES.length; ++i) { final double x = SAMPLES[i][0]; final double result = program.eval(x); System.out.println(format( "%2.2f: %2.4f, %2.4f: %2.5f", x, f(x), result, abs(f(x) - result) )); } }
public static void main(final String[] args) { final Engine<DoubleGene, Double> engine = Engine .builder( RastriginFunction::fitness, // Codec for 'x' vector. Codecs.ofVector(DoubleRange.of(-R, R), N)) .populationSize(500) .optimize(Optimize.MINIMUM) .alterers( new Mutator<>(0.03), new MeanAlterer<>(0.6)) .build(); final EvolutionStatistics<Double, ?> statistics = EvolutionStatistics.ofNumber(); final Phenotype<DoubleGene, Double> best = engine.stream() .limit(bySteadyFitness(7)) .peek(statistics) .collect(toBestPhenotype()); System.out.println(statistics); System.out.println(best); } }
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 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(MLStrategy::fitness, codec) .populationSize(λ) .survivorsSize(0) .offspringSelector(new TruncationSelector<>(μ)) .alterers(new Mutator<>(p)) .build(); System.out.println( codec.decode( engine.stream() .limit(100) .collect(EvolutionResult.toBestGenotype()) ) ); }
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); }
@Test public void initialResult() { // Problem definition. final Problem<Double, DoubleGene, Double> problem = Problem.of( x -> cos(0.5 + sin(x))*cos(x), Codecs.ofScalar(DoubleRange.of(0.0, 2.0*PI)) ); // Define the GA engine. final Engine<DoubleGene, Double> engine = Engine.builder(problem) .optimize(Optimize.MINIMUM) .offspringSelector(new RouletteWheelSelector<>()) .build(); final EvolutionResult<DoubleGene, Double> interimResult = engine.stream() .limit(Limits.bySteadyFitness(10)) .collect(EvolutionResult.toBestEvolutionResult()); engine.builder() .alterers(new Mutator<>()).build() .stream(interimResult); }
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(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 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); }
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 Engine<ProgramGene<Double>, Double> engine = Engine .builder(SymbolicRegression::error, CODEC) .minimizing() .alterers( new SingleNodeCrossover<>(), new Mutator<>()) .build(); final ProgramGene<Double> program = engine.stream() .limit(100) .collect(EvolutionResult.toBestGenotype()) .getGene(); System.out.println(program.toParenthesesString()); }
public static void main(final String[] args) { final SubsetSum problem = of(500, 15, new LCG64ShiftRandom(101010)); final Engine<EnumGene<Integer>, Integer> engine = Engine.builder(problem) .minimizing() .maximalPhenotypeAge(5) .alterers( new PartiallyMatchedCrossover<>(0.4), new Mutator<>(0.3)) .build(); final Phenotype<EnumGene<Integer>, Integer> result = engine.stream() .limit(Limits.bySteadyFitness(55)) .collect(EvolutionResult.toBestPhenotype()); System.out.print(result); }
static Engine<EnumGene<Integer>, Integer> buildEngine(Codec<ISeq<Integer>, EnumGene<Integer>> codec) { return Engine.builder(E305::fitness, codec) .offspringSelector(new RouletteWheelSelector<>()) .alterers( new SwapMutator<>(), new PartiallyMatchedCrossover<>(0.9) ) .populationSize(20) .optimize(Optimize.MINIMUM) .build(); }