@Test public void withScope() { final List<Genotype<DoubleGene>> genotypes1 = RandomRegistry.with(new Random(123), random -> Genotype.of(DoubleChromosome.of(0, 10)).instances() .limit(100) .collect(toList()) ); final List<Genotype<DoubleGene>> genotypes2 = RandomRegistry.with(new Random(123), random -> Genotype.of(DoubleChromosome.of(0, 10)).instances() .limit(100) .collect(toList()) ); assertEquals(genotypes1, genotypes2); }
private static EvolutionStart<DoubleGene, Double> start(final int populationSize, final long generation) { final ISeq<Phenotype<DoubleGene, Double>> population = Genotype.of(DoubleChromosome.of(0, 1)).instances() .map(gt -> Phenotype.of(gt, generation, SpecialEngine::fitness)) .limit(populationSize) .collect(ISeq.toISeq()); return EvolutionStart.of(population, generation); }
@Test public void toUniquePopulation() { final ISeq<Genotype<IntegerGene>> genotypes = Genotype.of(IntegerChromosome.of(0, Integer.MAX_VALUE)).instances() .limit(10) .collect(ISeq.toISeq()); final EvolutionResult<IntegerGene, Integer> result = result(genotypes); Assert.assertSame( EvolutionResult.<IntegerGene, Integer>toUniquePopulation().apply(result), result ); }
public static void main(String[] args) { // Set the PRNG used by the evolution Engine. final LCG64ShiftRandom random = new LCG64ShiftRandom(123); RandomRegistry.setRandom(random); // Configure and build the evolution Engine. final Engine<BitGene, Integer> engine = Engine .builder( RngExample::count, BitChromosome.of(20, 0.15)) .build(); // The 'Random(123)' object is used for creating a *reproducible* // initial population. The original PRNG is restored after the 'with' // block. assert RandomRegistry.getRandom() == random; final List<Genotype<BitGene>> genotypes = RandomRegistry.with(new Random(123), r -> { assert RandomRegistry.getRandom() == r; return Genotype.of(BitChromosome.of(20, 0.15)) .instances() .limit(50) .collect(Collectors.toList()); }); assert RandomRegistry.getRandom() == random; // The evolution process uses the global 'random' instance. final Phenotype<BitGene, Integer> best = engine.stream(genotypes) .limit(bySteadyFitness(20)) .limit(100) .collect(toBestPhenotype()); System.out.println(best); }
@Test public void toUniquePopulation3() { final ISeq<Genotype<IntegerGene>> genotypes = Genotype.of(IntegerChromosome.of(0, 10)).instances() .limit(100) .collect(ISeq.toISeq()); final UnaryOperator<EvolutionResult<IntegerGene, Integer>> unifier = EvolutionResult.toUniquePopulation( Genotype.of(IntegerChromosome.of(0, 10))); final EvolutionResult<IntegerGene, Integer> result = result(genotypes); final EvolutionResult<IntegerGene, Integer> unified = unifier.apply(result); Assert.assertNotEquals(unified, result); Assert.assertTrue( unified.getGenotypes().stream().collect(Collectors.toSet()).size() < unified.getPopulation().size() ); Assert.assertEquals( result.getPopulation().size(), unified.getPopulation().size() ); }
@Test public void toUniquePopulation2() { final ISeq<Genotype<IntegerGene>> genotypes = Genotype.of(IntegerChromosome.of(0, 10)).instances() .limit(100) .collect(ISeq.toISeq()); final UnaryOperator<EvolutionResult<IntegerGene, Integer>> unifier = EvolutionResult.toUniquePopulation( Genotype.of(IntegerChromosome.of(0, Integer.MAX_VALUE))); final EvolutionResult<IntegerGene, Integer> result = result(genotypes); final EvolutionResult<IntegerGene, Integer> unified = unifier.apply(result); Assert.assertNotEquals(unified, result); Assert.assertEquals( unified.getGenotypes().stream().collect(Collectors.toSet()).size(), unified.getPopulation().size() ); Assert.assertEquals( result.getPopulation().size(), unified.getPopulation().size() ); }