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 // https://github.com/jenetics/jenetics/issues/318 public void initialFitnessConvergence() { final Problem<Double, DoubleGene, Double> problem = Problem.of( d -> 1.0, Codecs.ofScalar(DoubleRange.of(0, 1)) ); final Engine<DoubleGene, Double> engine = Engine.builder(problem).build(); final AtomicInteger count = new AtomicInteger(); final EvolutionResult<DoubleGene, Double> result = engine.stream() .limit(Limits.byFitnessConvergence(1, 2, 0.03)) .peek(er -> count.incrementAndGet()) .collect(EvolutionResult.toBestEvolutionResult()); Assert.assertNotNull(result); Assert.assertEquals(count.get(), 1); Assert.assertEquals(result.getTotalGenerations(), 1); Assert.assertEquals(result.getGeneration(), 1); }
public static void main(final String[] args) { final Problem<int[][], IntegerGene, Integer> problem = Problem.of( Matrix::fitness, Codec.of( Genotype.of(IntegerChromosome.of(IntRange.of(0, 10), 3), 3), gt -> gt.stream() .map(ch -> ch.stream() .mapToInt(IntegerGene::intValue).toArray()) .toArray(int[][]::new) ) ); final Engine<IntegerGene, Integer> engine = Engine.builder(problem).build(); final Genotype<IntegerGene> gt = engine.stream() .limit(Limits.byFixedGeneration(20)) .collect(EvolutionResult.toBestGenotype()); final int[][] best = problem.codec().decode(gt); print(best); }
/** * Converts the given {@link Genotype} to the target type {@link T}. This is * a shortcut for * <pre>{@code * final Problem<SomeObject, DoubleGene, Double> problem = ... * final Genotype<DoubleGene> gt = problem.codec().encoding().newInstance(); * * final SomeObject arg = problem.decode(gt); * }</pre> * * @since 4.2 * * @see Codec#decode(Genotype) * * @param genotype the genotype to be converted * @return the converted genotype * @throws NullPointerException if the given {@code genotype} is {@code null} */ public default T decode(final Genotype<G> genotype) { return codec().decode(genotype); }
/** * Returns the fitness value for the given argument. * * @since 4.1 * * @param arg the argument of the fitness function * @return the fitness value */ public default C fitness(final T arg) { return fitness().apply(arg); }
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 // https://github.com/jenetics/jenetics/issues/318 public void initialPopulationConvergence() { final Problem<Double, DoubleGene, Double> problem = Problem.of( d -> 1.0, Codecs.ofScalar(DoubleRange.of(0, 1)) ); final Engine<DoubleGene, Double> engine = Engine.builder(problem).build(); final AtomicInteger count = new AtomicInteger(); final EvolutionResult<DoubleGene, Double> result = engine.stream() .limit(Limits.byPopulationConvergence(0.03)) .peek(er -> count.incrementAndGet()) .collect(EvolutionResult.toBestEvolutionResult()); Assert.assertNotNull(result); Assert.assertEquals(count.get(), 1); Assert.assertEquals(result.getTotalGenerations(), 1); Assert.assertEquals(result.getGeneration(), 1); }
@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(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))); }
/** * Create a new evolution {@code Engine.Builder} for the given * {@link Problem}. * * @since 3.4 * * @param problem the problem to be solved by the evolution {@code Engine} * @param <T> the (<i>native</i>) argument type of the problem fitness function * @param <G> the gene type the evolution engine is working with * @param <C> the result type of the fitness function * @return Create a new evolution {@code Engine.Builder} */ public static <T, G extends Gene<?, G>, C extends Comparable<? super C>> Builder<G, C> builder(final Problem<T, G, C> problem) { return builder(problem.fitness(), problem.codec()); }
@Test // https://github.com/jenetics/jenetics/issues/318 public void initialFitnessConvergence() { final Problem<Double, DoubleGene, Double> problem = Problem.of( d -> 1.0, Codecs.ofScalar(DoubleRange.of(0, 1)) ); final Engine<DoubleGene, Double> engine = Engine.builder(problem).build(); final AtomicInteger count = new AtomicInteger(); final EvolutionResult<DoubleGene, Double> result = engine.stream() .limit(Limits.byFitnessThreshold(0.3)) .peek(er -> count.incrementAndGet()) .collect(EvolutionResult.toBestEvolutionResult()); Assert.assertNotNull(result); Assert.assertEquals(count.get(), 1); Assert.assertEquals(result.getTotalGenerations(), 1); Assert.assertEquals(result.getGeneration(), 1); }
@Test // https://github.com/jenetics/jenetics/issues/318 public void initialGeneConvergence() { RandomRegistry.using(Random32.of(() -> 234), random -> { final Problem<Double, DoubleGene, Double> problem = Problem.of( d -> 1.0, Codecs.ofScalar(DoubleRange.of(0, 1)) ); final Engine<DoubleGene, Double> engine = Engine .builder(problem) .executor(Runnable::run) .build(); final AtomicInteger count = new AtomicInteger(); final EvolutionResult<DoubleGene, Double> result = engine.stream() .limit(Limits.byGeneConvergence(0.03, 0.03)) .peek(er -> count.incrementAndGet()) .collect(EvolutionResult.toBestEvolutionResult()); Assert.assertNotNull(result); Assert.assertEquals(count.get(), 1); Assert.assertEquals(result.getTotalGenerations(), 1); Assert.assertEquals(result.getGeneration(), 1); }); }
@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()); }
@Test public void streamWithInitialGenotypes() { final Problem<Integer, IntegerGene, Integer> problem = Problem.of( a -> a, Codec.of( Genotype.of(IntegerChromosome.of(0, 1000)), g -> g.getGene().getAllele() ) ); final int genotypeCount = 10; final int max = 1000; final ISeq<Genotype<IntegerGene>> genotypes = IntRange.of(1, genotypeCount) .stream() .mapToObj(i -> IntegerChromosome.of(IntegerGene.of(max, 0, max))) .map(Genotype::of) .collect(ISeq.toISeq()); final Engine<IntegerGene, Integer> engine = Engine.builder(problem) .build(); final EvolutionResult<IntegerGene, Integer> result = engine.stream(genotypes) .limit(1) .collect(EvolutionResult.toBestEvolutionResult()); final long maxCount = result.getPopulation().stream() .filter(pt -> pt.getFitness() == max) .count(); Assert.assertTrue(maxCount >= genotypeCount); }
@Test(invocationCount = 5, successPercentage = 80) public void collect() { final Problem<double[], DoubleGene, Vec<double[]>> problem = Problem.of( v -> Vec.of(v[0]*cos(v[1]), v[0]*sin(v[1])), Codecs.ofVector(
@Test(invocationCount = 5) public void onesCountLimit() { final Problem<ISeq<BitGene>, BitGene, Integer> problem = Problem.of( genes -> (int)genes.stream().filter(BitGene::getBit).count(), Codec.of( Genotype.of(BitChromosome.of(20, 0.125)), gt -> gt.getChromosome().toSeq() ) ); final Engine<BitGene, Integer> engine = Engine.builder(problem) .build(); final EvolutionResult<BitGene, Integer> result = engine.stream() .limit(Limits.byFitnessConvergence(5, 10, 0.01)) .collect(EvolutionResult.toBestEvolutionResult()); Assert.assertTrue( result.getTotalGenerations() < 50, "Gen: " + result.getTotalGenerations() ); }
@Test(invocationCount = 5) public void onesCountLimit() { final Problem<ISeq<BitGene>, BitGene, Integer> problem = Problem.of( genes -> (int)genes.stream().filter(BitGene::getBit).count(), Codec.of( Genotype.of(BitChromosome.of(20, 0.125)), gt -> gt.getChromosome().toSeq() ) ); final Engine<BitGene, Integer> engine = Engine.builder(problem) .build(); final EvolutionResult<BitGene, Integer> result = engine.stream() .limit(Limits.byPopulationConvergence(0.015)) .collect(toBestEvolutionResult()); Assert.assertTrue( result.getTotalGenerations() < 2901, "Gen: " + result.getTotalGenerations() ); }