/** * 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); }
/** * 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 ); }
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(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 = "longVectorData") public void ofLongVector(final LongRange domain, final int length) { final Codec<long[], LongGene> codec = Codecs.ofVector(domain, length); final Genotype<LongGene> gt = codec.encoding().newInstance(); Assert.assertEquals(gt.length(), 1); Assert.assertEquals(gt.getChromosome().length(), length); for (LongGene gene : gt.getChromosome()) { Assert.assertEquals(gene.getMin().longValue(), domain.getMin()); Assert.assertEquals(gene.getMax().longValue(), domain.getMax()); } final Function<Genotype<LongGene>, long[]> f = codec.decoder(); final long[] value = f.apply(gt); Assert.assertEquals(value.length, length); for (int i = 0; i < length; ++i) { Assert.assertEquals(gt.get(0, i).longValue(), value[i]); } }
@Test(dataProvider = "longVectorDataVector") public void ofLongVectorVector(final LongRange[] domain) { final Codec<long[], LongGene> codec = Codecs.ofVector(domain); final Genotype<LongGene> gt = codec.encoding().newInstance(); Assert.assertEquals(gt.length(), domain.length); for (int i = 0; i < gt.length(); ++i) { final Chromosome<LongGene> ch = gt.getChromosome(i); Assert.assertEquals(ch.length(), 1); final LongGene gene = ch.getGene(); Assert.assertEquals(gene.getMin().longValue(), domain[i].getMin()); Assert.assertEquals(gene.getMax().longValue(), domain[i].getMax()); } final Function<Genotype<LongGene>, long[]> f = codec.decoder(); final long[] value = f.apply(gt); Assert.assertEquals(value.length, domain.length); for (int i = 0; i < domain.length; ++i) { Assert.assertEquals(gt.get(i, 0).longValue(), value[i]); } }
@Test(dataProvider = "intVectorDataVector") public void ofIntVectorVector(final IntRange[] domain) { final Codec<int[], IntegerGene> codec = Codecs.ofVector(domain); final Genotype<IntegerGene> gt = codec.encoding().newInstance(); Assert.assertEquals(gt.length(), domain.length); for (int i = 0; i < gt.length(); ++i) { final Chromosome<IntegerGene> ch = gt.getChromosome(i); Assert.assertEquals(ch.length(), 1); final IntegerGene gene = ch.getGene(); Assert.assertEquals(gene.getMin().intValue(), domain[i].getMin()); Assert.assertEquals(gene.getMax().intValue(), domain[i].getMax()); } final Function<Genotype<IntegerGene>, int[]> f = codec.decoder(); final int[] value = f.apply(gt); Assert.assertEquals(value.length, domain.length); for (int i = 0; i < domain.length; ++i) { Assert.assertEquals(gt.get(i, 0).intValue(), value[i]); } }
@Test(dataProvider = "doubleVectorData") public void ofDoubleVector(final DoubleRange domain, final int length) { final Codec<double[], DoubleGene> codec = Codecs.ofVector(domain, length); final Genotype<DoubleGene> gt = codec.encoding().newInstance(); Assert.assertEquals(gt.length(), 1); Assert.assertEquals(gt.getChromosome().length(), length); for (DoubleGene gene : gt.getChromosome()) { Assert.assertEquals(gene.getMin(), domain.getMin()); Assert.assertEquals(gene.getMax(), domain.getMax()); } final Function<Genotype<DoubleGene>, double[]> f = codec.decoder(); final double[] value = f.apply(gt); Assert.assertEquals(value.length, length); for (int i = 0; i < length; ++i) { Assert.assertEquals(gt.get(0, i).doubleValue(), value[i]); } }
@Test public void ofAnyVector() { final int length = 23; final Codec<ISeq<Integer>, AnyGene<Integer>> codec = Codecs.ofVector( () -> RandomRegistry.getRandom().nextInt(1000), (Predicate<Integer>) i -> i < 100, length ); for (int i = 0; i < 100; ++i) { final Chromosome<AnyGene<Integer>> ch = codec.encoding() .newInstance().getChromosome(); Assert.assertEquals(ch.length(), length); for (AnyGene<Integer> gene : ch) { Assert.assertEquals(gene.isValid(), gene.getAllele() < 100); if (!gene.isValid()) { Assert.assertFalse(ch.isValid()); } Assert.assertTrue(gene.getAllele() < 1000); Assert.assertTrue(gene.getAllele() >= 0); } } }
@Test(dataProvider = "doubleVectorDataVector") public void ofDoubleVectorVector(final DoubleRange[] domain) { final Codec<double[], DoubleGene> codec = Codecs.ofVector(domain); final Genotype<DoubleGene> gt = codec.encoding().newInstance(); Assert.assertEquals(gt.length(), domain.length); for (int i = 0; i < gt.length(); ++i) { final Chromosome<DoubleGene> ch = gt.getChromosome(i); Assert.assertEquals(ch.length(), 1); final DoubleGene gene = ch.getGene(); Assert.assertEquals(gene.getMin(), domain[i].getMin()); Assert.assertEquals(gene.getMax(), domain[i].getMax()); } final Function<Genotype<DoubleGene>, double[]> f = codec.decoder(); final double[] value = f.apply(gt); Assert.assertEquals(value.length, domain.length); for (int i = 0; i < domain.length; ++i) { Assert.assertEquals(gt.get(i, 0).doubleValue(), value[i]); } }
@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}); } }
@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); }
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))); }
final Problem<double[], DoubleGene, Vec<double[]>> problem = Problem.of( v -> Vec.of(v[0]*cos(v[1]), v[0]*sin(v[1])), Codecs.ofVector( DoubleRange.of(0, 1), DoubleRange.of(0, 2*PI)
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))); }