public static void main(final String[] args) throws IOException { final EvolutionResult<DoubleGene, Double> rescue = ENGINE.stream() .limit(Limits.bySteadyFitness(10)) .collect(EvolutionResult.toBestEvolutionResult()); final Path path = Paths.get("result.bin"); IO.object.write(rescue, path); @SuppressWarnings("unchecked") final EvolutionResult<DoubleGene, Double> result = ENGINE .stream((EvolutionResult<DoubleGene, Double>)IO.object.read(path)) .limit(Limits.bySteadyFitness(20)) .collect(EvolutionResult.toBestEvolutionResult()); System.out.println(result); }
public static void main(final String[] args) { final Genotype<DoubleGene> best = EvolutionStream .of(() -> start(50, 0), SpecialEngine::evolve) .limit(Limits.bySteadyFitness(10)) .limit(1000) .collect(EvolutionResult.toBestGenotype()); System.out.println(String.format("Best Genotype: %s", best)); } }
.limit(bySteadyFitness(25))
public static void main(String[] args) { // Set the PRNG used by the evolution Engine. final LCG64ShiftRandom random = new LCG64ShiftRandom(123); RandomRegistry.setRandom(random); // Configure and build the evolution Engine. final Engine<BitGene, Integer> engine = Engine .builder( RngExample::count, BitChromosome.of(20, 0.15)) .build(); // The 'Random(123)' object is used for creating a *reproducible* // initial population. The original PRNG is restored after the 'with' // block. assert RandomRegistry.getRandom() == random; final List<Genotype<BitGene>> genotypes = RandomRegistry.with(new Random(123), r -> { assert RandomRegistry.getRandom() == r; return Genotype.of(BitChromosome.of(20, 0.15)) .instances() .limit(50) .collect(Collectors.toList()); }); assert RandomRegistry.getRandom() == random; // The evolution process uses the global 'random' instance. final Phenotype<BitGene, Integer> best = engine.stream(genotypes) .limit(bySteadyFitness(20)) .limit(100) .collect(toBestPhenotype()); System.out.println(best); }
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()); }
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); }
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); } }
.limit(bySteadyFitness(7))
.limit(bySteadyFitness(7))
.limit(bySteadyFitness(7))
.limit(bySteadyFitness(7))
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()); }
.limit(Limits.bySteadyFitness(50)) .peek(er -> population.set(er.getPopulation())) .collect(EvolutionResult.toBestGenotype())
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 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); }
@Test public void streamWithSerializedPopulation() throws IOException { // 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()); final ByteArrayOutputStream out = new ByteArrayOutputStream(); IO.object.write(interimResult, out); final ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); @SuppressWarnings("unchecked") final EvolutionResult<DoubleGene, Double> loadedResult = (EvolutionResult<DoubleGene, Double>)IO.object.read(in); final EvolutionResult<DoubleGene, Double> result = engine .stream(loadedResult) .limit(10) .collect(EvolutionResult.toBestEvolutionResult()); }
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.Builder<DoubleGene, Double> builder = Engine .builder(problem) .minimizing(); final Genotype<DoubleGene> result = AdaptiveEngine.<DoubleGene, Double>of(er -> engine(er, builder)) .stream() .limit(Limits.bySteadyFitness(50)) .collect(EvolutionResult.toBestGenotype()); System.out.println(result + ": " + problem.fitness().apply(problem.codec().decode(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); }
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))); }