@Override public Codec<ISeq<double[]>, EnumGene<double[]>> codec() { return Codecs.ofPermutation(_points); }
/** * Return a scala {@code Codec} with the given allele {@link Supplier} and * {@code Chromosome} length. The {@code supplier} is responsible for * creating new random alleles. * * @param <A> the allele type * @param supplier the allele-supplier which is used for creating new, * random alleles * @param length the vector length * @return a new {@code Codec} with the given parameters * @throws NullPointerException if one of the parameters is {@code null} * @throws IllegalArgumentException if the length of the vector is smaller * than one. */ public static <A> Codec<ISeq<A>, AnyGene<A>> ofVector( final Supplier<? extends A> supplier, final int length ) { return ofVector(supplier, Predicates.TRUE, length); }
public static void main(final String[] args) { final Engine<AnyGene<Dimension>, Double> engine = Engine .builder(Squares::area, Codecs.ofScalar(Squares::nextDimension)) .build(); final Phenotype<AnyGene<Dimension>, Double> pt = engine.stream() .limit(50) .collect(EvolutionResult.toBestPhenotype()); System.out.println(pt); }
) { requireNonNull(mapSupplier); return ofPermutation(target.size()) .map(perm -> toMapping(perm, source, target, mapSupplier));
@Test public void encoding() { final Codec<Double, DoubleGene> codec = new CompositeCodec<>( ISeq.of( Codecs.ofScalar(DoubleRange.of(0, 1)), Codecs.ofVector(DoubleRange.of(10, 100), 3), Codecs.ofScalar(DoubleRange.of(2, 3)), Codecs.ofVector(DoubleRange.of(200, 500), DoubleRange.of(200, 500)) ), values -> { final Double v1 = (Double)values[0]; final double[] v2 = (double[])values[1]; final Double v3 = (Double)values[2]; final double[] v4 = (double[])values[3]; return v1 + DoubleAdder.sum(v2) + v3 + DoubleAdder.sum(v4); } ); final Genotype<DoubleGene> gt = codec.encoding().newInstance(); final double sum = gt.stream() .mapToDouble(c -> c.stream() .mapToDouble(DoubleGene::doubleValue) .sum()) .sum(); Assert.assertEquals(sum, codec.decoder().apply(gt), 0.000001); }
/** * Create a new {@code Knapsack} definition with the given * * @param items the basic {@link Set} of knapsack items. * @param knapsackSize the maximal knapsack size * @throws NullPointerException if the {@code items} set is {@code null} */ public Knapsack(final ISeq<Item> items, final double knapsackSize) { _codec = Codecs.ofSubSet(items); _knapsackSize = knapsackSize; }
return ofMapping(source, target, HashMap::new);
@Override public Codec<ISeq<Record>, BitGene> codec() { return Codecs.ofSubSet(_records); }
@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)); }
/** * Return a scala {@code Codec} with the given allele {@link Supplier}, * allele {@code validator} and {@code Chromosome} length. The * {@code supplier} is responsible for creating new random alleles, and the * {@code validator} can verify it. * * @param <A> the allele type * @param supplier the allele-supplier which is used for creating new, * random alleles * @param validator the validator used for validating the created gene. This * predicate is used in the {@link AnyGene#isValid()} method. * @param length the vector length * @return a new {@code Codec} with the given parameters * @throws NullPointerException if one of the parameters is {@code null} * @throws IllegalArgumentException if the length of the vector is smaller * than one. */ public static <A> Codec<ISeq<A>, AnyGene<A>> ofVector( final Supplier<? extends A> supplier, final Predicate<? super A> validator, final int length ) { return ofVector( supplier, validator, Predicates.<ISeq<A>>True(), length ); }
@Test public void ofAnyScalar2() { final Codec<Integer, AnyGene<Integer>> codec = Codecs.ofScalar( () -> RandomRegistry.getRandom().nextInt(1000) ); for (int i = 0; i < 1000; ++i) { final AnyGene<Integer> gene = codec.encoding() .newInstance().getGene(); Assert.assertTrue(gene.isValid()); Assert.assertTrue(gene.getAllele() < 1000); Assert.assertTrue(gene.getAllele() >= 0); } }
@Override public Codec<ISeq<WayPoint>, EnumGene<WayPoint>> codec() { return Codecs.ofPermutation(_points); }
@Override public Codec<ISeq<Integer>, EnumGene<Integer>> codec() { return Codecs.ofSubSet(_basicSet, _size); }
@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 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); } }
@Test public void ofAnyScalar() { final Codec<Integer, AnyGene<Integer>> codec = Codecs.ofScalar( () -> RandomRegistry.getRandom().nextInt(1000), i -> i < 100 ); for (int i = 0; i < 1000; ++i) { final AnyGene<Integer> gene = codec.encoding() .newInstance().getGene(); Assert.assertEquals(gene.isValid(), gene.getAllele() < 100); Assert.assertTrue(gene.getAllele() < 1000); Assert.assertTrue(gene.getAllele() >= 0); } }
static Codec<ISeq<Integer>, EnumGene<Integer>> buildCodec(ISeq<Integer> orders) { return Codecs.ofPermutation(orders); }
@Test public void ofSubSet() { final Codec<ISeq<String>, EnumGene<String>> codec = Codecs.ofSubSet( ISeq.of("1", "2", "3", "4", "5"), 3 ); for (int i = 0; i < 100; ++i) { final Genotype<EnumGene<String>> gt = codec.encoding().newInstance(); final Chromosome<EnumGene<String>> ch = gt.getChromosome(); Assert.assertEquals(ch.length(), 3); Assert.assertTrue(ch.isValid()); final ISeq<String> permutation = codec.decoder().apply(gt); Assert.assertEquals(permutation.length(), 3); } }
@Test(dataProvider = "intVectorData") public void ofIntVector(final IntRange domain, final int length) { final Codec<int[], IntegerGene> codec = Codecs.ofVector(domain, length); final Genotype<IntegerGene> gt = codec.encoding().newInstance(); Assert.assertEquals(gt.length(), 1); Assert.assertEquals(gt.getChromosome().length(), length); for (IntegerGene gene : gt.getChromosome()) { Assert.assertEquals(gene.getMin().intValue(), domain.getMin()); Assert.assertEquals(gene.getMax().intValue(), domain.getMax()); } final Function<Genotype<IntegerGene>, int[]> f = codec.decoder(); final int[] value = f.apply(gt); Assert.assertEquals(value.length, length); for (int i = 0; i < length; ++i) { Assert.assertEquals(gt.get(0, i).intValue(), value[i]); } }
@Test(dataProvider = "intScalarData") public void ofIntScalar(final IntRange domain) { final Codec<Integer, IntegerGene> codec = Codecs.ofScalar(domain); final Genotype<IntegerGene> gt = codec.encoding().newInstance(); Assert.assertEquals(gt.length(), 1); Assert.assertEquals(gt.getChromosome().length(), 1); Assert.assertEquals(gt.getGene().getMin().intValue(), domain.getMin()); Assert.assertEquals(gt.getGene().getMax().intValue(), domain.getMax()); final Function<Genotype<IntegerGene>, Integer> f = codec.decoder(); Assert.assertEquals(f.apply(gt).intValue(), gt.getGene().intValue()); }