private static Observable<Double> fitness(final Genotype<DoubleGene> gt) { return Observable.just(gt.getGene().doubleValue()); }
private static Future<Double> fitness(final Genotype<DoubleGene> gt) { return CompletableFuture.supplyAsync(() -> gt.getGene().doubleValue()); }
final double[] args = new double[gt.length()]; for (int i = gt.length(); --i >= 0;) { args[i] = gt.getChromosome(i).getGene().doubleValue();
@Test public void doubleStream() { final DoubleChromosome chromosome = DoubleChromosome.of(0, 1, 1000); final double[] values = chromosome.doubleStream().toArray(); Assert.assertEquals(values.length, 1000); for (int i = 0; i < values.length; ++i) { Assert.assertEquals(chromosome.getGene(i).doubleValue(), values[i]); Assert.assertEquals(chromosome.doubleValue(i), values[i]); } }
@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 populationEvaluator() { final int populationSize = 100; final AtomicInteger count = new AtomicInteger(); final Engine<DoubleGene, Double> engine = Engine .builder(gt -> gt.getGene().doubleValue(), DoubleChromosome.of(0, 1)) .populationSize(populationSize) .evaluator((gt, ff) -> { count.compareAndSet(0, gt.length()); return gt.stream().map(ff).collect(ISeq.toISeq()); }) .build(); engine.stream() .limit(1) .collect(EvolutionResult.toBestGenotype()); Assert.assertEquals(count.get(), populationSize); }
@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]); } }
gt -> Player.of(gt.getGene().doubleValue()) );
@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(dataProvider = "executors", timeOut = 2_000L) public void executorDeadLock(final Executor executor) { try { final Engine<DoubleGene, Double> engine = Engine .builder(gt -> gt.getGene().doubleValue(), DoubleChromosome.of(0, 1)) .executor(executor) .populationSize(10) .build(); engine.stream() .limit(100) .collect(EvolutionResult.toBestEvolutionResult()); } finally { if (executor instanceof ExecutorService) { ((ExecutorService)executor).shutdown(); } } }
@Test public void constantPopulationForZeroOffspring() { final int populationSize = 20; final Engine<DoubleGene, Double> engine = Engine .builder(gt -> gt.getGene().doubleValue(), DoubleChromosome.of(0, 1)) .populationSize(populationSize) .offspringSize(0) .build(); final EvolutionResult<DoubleGene, Double> result = engine.stream() .limit(100) .peek(r -> { if (r.getPopulation().size() != populationSize) { throw new AssertionError(format( "Expected population size %d, but got %d.", populationSize, r.getPopulation().size() )); } }) .collect(EvolutionResult.toBestEvolutionResult()); }
@Test public void constantPopulationForZeroSurvivors() { final int populationSize = 20; final Engine<DoubleGene, Double> engine = Engine .builder(gt -> gt.getGene().doubleValue(), DoubleChromosome.of(0, 1)) .populationSize(populationSize) .survivorsSize(0) .build(); final EvolutionResult<DoubleGene, Double> result = engine.stream() .limit(100) .peek(r -> { if (r.getPopulation().size() != populationSize) { throw new AssertionError(format( "Expected population size %d, but got %d.", populationSize, r.getPopulation().size() )); } }) .collect(EvolutionResult.toBestEvolutionResult()); }
@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() ); }
@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() ); }
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() ) ); }