private static Future<Double> fitness(final Genotype<DoubleGene> gt) { return CompletableFuture.supplyAsync(() -> gt.getGene().doubleValue()); }
/** * Create a new random {@code DoubleGene} with the given value and the * given range. If the {@code value} isn't within the interval [min, max), * no exception is thrown. In this case the method * {@link DoubleGene#isValid()} returns {@code false}. * * @param value the value of the gene. * @param min the minimal valid value of this gene (inclusively). * @param max the maximal valid value of this gene (exclusively). * @return a new {@code DoubleGene} with the given parameter */ public static DoubleGene of( final double value, final double min, final double max ) { return new DoubleGene(value, min, max); }
@Test public void createNumber() { DoubleGene gene = DoubleGene.of(1.2345, -1234.1234, 1234.1234); DoubleGene g2 = gene.newInstance(5.0); assertEquals(g2.getAllele().intValue(), 5); assertEquals(g2.getMin(), -1234.1234); assertEquals(g2.getMax(), 1234.1234); }
@Override public DoubleGene mean(final DoubleGene that) { return of(_value + (that._value - _value)/2.0, _min, _max); }
private void writeObject(final ObjectOutputStream out) throws IOException { out.defaultWriteObject(); out.writeInt(length()); out.writeObject(lengthRange()); out.writeDouble(_min); out.writeDouble(_max); for (DoubleGene gene : _genes) { out.writeDouble(gene.getAllele()); } }
@Test public void doubleGeneIntegerIntegerInteger() { DoubleGene gene = DoubleGene.of(1.234, 0.345, 2.123); assertEquals(gene.getAllele(), 1.234); assertEquals(gene.getMin(), 0.345); assertEquals(gene.getMax(), 2.123); try { gene = DoubleGene.of(0.1, 2.1, 4.1); assertFalse(gene.isValid()); } catch (IllegalArgumentException e) { assertTrue(true); } }
@Test(dataProvider = "doubleScalarData") public void ofDoubleScalar(final DoubleRange domain) { final Codec<Double, DoubleGene> codec = Codecs.ofScalar(domain); final Genotype<DoubleGene> gt = codec.encoding().newInstance(); Assert.assertEquals(gt.length(), 1); Assert.assertEquals(gt.getChromosome().length(), 1); Assert.assertEquals(gt.getGene().getMin(), domain.getMin()); Assert.assertEquals(gt.getGene().getMax(), domain.getMax()); final Function<Genotype<DoubleGene>, Double> f = codec.decoder(); Assert.assertEquals(f.apply(gt), gt.getGene().doubleValue()); }
@Test public void selectMinimum() { final MSeq<Phenotype<DoubleGene, Double>> population = MSeq.ofLength(100); for (int i = 0, n = population.size(); i < n; ++i) { final Genotype<DoubleGene> gt = Genotype.of(DoubleChromosome.of(DoubleGene.of(i, 0, n))); population.set(i, Phenotype.of(gt, 12, TestUtils.FF)); } final EliteSelector<DoubleGene, Double> selector = new EliteSelector<>(); final ISeq<Phenotype<DoubleGene, Double>> selected = selector.select(population, 10, Optimize.MINIMUM); Assert.assertEquals( population.get(0).getGenotype().getGene().doubleValue(), selected.get(0).getGenotype().getGene().doubleValue() ); }
static Codec<Tuple, DoubleGene> codec( final IntRange v1Domain, final LongRange v2Domain, final DoubleRange v3Domain ) { return Codec.of( Genotype.of( DoubleChromosome.of(DoubleRange.of(v1Domain.getMin(), v1Domain.getMax())), DoubleChromosome.of(DoubleRange.of(v2Domain.getMin(), v2Domain.getMax())), DoubleChromosome.of(v3Domain) ), gt -> new Tuple( gt.getChromosome(0).getGene().intValue(), gt.getChromosome(1).getGene().longValue(), gt.getChromosome(2).getGene().doubleValue() ) ); }
@Test public void worstIndividual() { final int size = 20; final MSeq<Phenotype<DoubleGene, Integer>> population = MSeq.ofLength(size); for (int i = 0; i < size; ++i) { final DoubleGene gene = DoubleGene.of(i, 0, size + 10); final DoubleChromosome ch = DoubleChromosome.of(gene); final Genotype<DoubleGene> gt = Genotype.of(ch); final Phenotype<DoubleGene, Integer> pt = Phenotype.of( gt, 1, g -> g.getGene().intValue() ); population.set(i, pt); } final TruncationSelector<DoubleGene, Integer> selector = new TruncationSelector<>(5); final ISeq<Phenotype<DoubleGene, Integer>> selected = selector.select(population.toISeq(), 10, Optimize.MINIMUM); for (Phenotype<DoubleGene, Integer> pt : selected) { Assert.assertTrue(pt.getFitness() < 5); } }
@Test public void mean() { final double min = -Double.MAX_VALUE; final double max = Double.MAX_VALUE; final DoubleGene template = DoubleGene.of(min, max); for (int i = 1; i < 500; ++i) { final DoubleGene a = template.newInstance(i - 50.0); final DoubleGene b = template.newInstance((i - 100)*3.0); final DoubleGene c = a.mean(b); assertEquals(a.getMin(), min); assertEquals(a.getMax(), max); assertEquals(b.getMin(), min); assertEquals(b.getMax(), max); assertEquals(c.getMin(), min); assertEquals(c.getMax(), max); assertEquals(c.getAllele(), ((i - 50) + ((i - 100)*3))/2.0); } }
@Override public DoubleGene newInstance(final Double value) { return of(value, _min, _max); }
/** * Return a scalar {@code Codec} for the given range. * * @param domain the domain of the returned {@code Codec} * @return a new scalar {@code Codec} with the given domain. * @throws NullPointerException if the given {@code domain} is {@code null} */ public static Codec<Double, DoubleGene> ofScalar(final DoubleRange domain) { requireNonNull(domain); return Codec.of( Genotype.of(DoubleChromosome.of(domain)), gt -> gt.getGene().getAllele() ); }
@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 selectMaximum() { final MSeq<Phenotype<DoubleGene, Double>> population = MSeq.ofLength(100); for (int i = 0, n = population.size(); i < n; ++i) { final Genotype<DoubleGene> gt = Genotype.of(DoubleChromosome.of(DoubleGene.of(i, 0, n))); population.set(i, Phenotype.of(gt, 12, TestUtils.FF)); } final EliteSelector<DoubleGene, Double> selector = new EliteSelector<>(); final ISeq<Phenotype<DoubleGene, Double>> selected = selector.select(population, 10, Optimize.MAXIMUM); Assert.assertEquals( population.get(population.length() - 1).getGenotype().getGene().doubleValue(), selected.get(0).getGenotype().getGene().doubleValue() ); }