/** * 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 genotype the argument of the fitness function * @return the fitness value */ public default C fitness(final Genotype<G> genotype) { return fitness(codec().decode(genotype)); }
/** * Create a new {@code Codec} with the mapped result type. The following * example creates a double codec who's values are not uniformly distributed * between {@code [0..1)}. Instead the values now follow an exponential * function. * * <pre>{@code * final Codec<Double, DoubleGene> c = Codecs.ofScalar(DoubleRange.of(0, 1)) * .map(Math::exp); * }</pre> * * @since 4.0 * * @param mapper the mapper function * @param <B> the new argument type of the given problem * @return a new {@code Codec} with the mapped result type * @throws NullPointerException if the mapper is {@code null}. */ public default <B> Codec<B, G> map(final Function<? super T, ? extends B> mapper) { requireNonNull(mapper); return Codec.of( encoding(), gt -> mapper.apply(decode(gt)) ); }
@Test public void ofMapping1() { final ISeq<Integer> numbers = ISeq.of(1, 2, 3, 4, 5); final ISeq<String> chars = ISeq.of("A", "B", "C"); final Codec<Map<Integer, String>, EnumGene<Integer>> codec = Codecs.ofMapping(numbers, chars); final Function<Map<Integer, String>, Integer> ff = map -> map.keySet().stream().mapToInt(Integer::intValue).sum(); Engine<EnumGene<Integer>, Integer> engine = Engine.builder(ff, codec) .build(); final Map<Integer, String> best = codec.decode( engine.stream() .limit(100) .collect(EvolutionResult.toBestGenotype()) ); Assert.assertTrue(best.containsKey(3)); Assert.assertTrue(best.containsKey(4)); Assert.assertTrue(best.containsKey(5)); }
@Test public void ofMapping2() { final ISeq<Integer> numbers = ISeq.of(1, 2, 3, 4, 5); final ISeq<String> chars = ISeq.of("A", "B", "C"); final Codec<Map<String, Integer>, EnumGene<Integer>> codec = Codecs.ofMapping(chars, numbers); final Function<Map<String, Integer>, Integer> ff = map -> map.values().stream().mapToInt(Integer::intValue).sum(); Engine<EnumGene<Integer>, Integer> engine = Engine.builder(ff, codec) .build(); final Map<String, Integer> best = codec.decode( engine.stream() .limit(100) .collect(EvolutionResult.toBestGenotype()) ); Assert.assertTrue(best.containsValue(3)); Assert.assertTrue(best.containsValue(4)); Assert.assertTrue(best.containsValue(5)); }
public static void main(final String[] args) { final ISeq<Integer> orders = buildOrders(); final Codec<ISeq<Integer>, EnumGene<Integer>> codec = buildCodec(orders); final Engine<EnumGene<Integer>, Integer> engine = buildEngine(codec); final Genotype<EnumGene<Integer>> collect = engine.stream() .limit(1000) .collect(EvolutionResult.toBestGenotype()); final ISeq<Integer> best = codec.decode(collect); System.out.println(best); System.out.println(fitness(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(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(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()) ) ); }
private Phenotype<DoubleGene, Vec<double[]>> phenotype() { return Phenotype.of( PROBLEM.codec().encoding().newInstance(), 1L, gt -> PROBLEM.fitness().apply(PROBLEM.codec().decode(gt)) ); }
private Phenotype<DoubleGene, Vec<double[]>> phenotype() { return Phenotype.of( PROBLEM.codec().encoding().newInstance(), 1L, gt -> PROBLEM.fitness().apply(PROBLEM.codec().decode(gt)) ); }
@Test public void map() { final Codec<double[], DoubleGene> codec = Codecs .ofVector(DoubleRange.of(0, 1), 10) .map(v -> { for (int i = 0; i < v.length; ++i) { v[i] = v[i]/2.0; } return v; }); for (int i = 0; i < 100; ++i) { final Genotype<DoubleGene> gt = Genotype.of(DoubleChromosome.of( DoubleGene.of(i, DoubleRange.of(0, 100)) )); Assert.assertEquals(codec.decode(gt), new double[]{i/2.0}); } }
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))); }
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 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); }
@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 = 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))); }
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))); }