@Override public Codec<ISeq<BitGene>, BitGene> codec() { return Codec.of( Genotype.of(BitChromosome.of(_length, _onesProbability)), gt -> gt.getChromosome().toSeq() ); }
private Object[] groups(final Genotype<G> genotype) { final Object[] groups = new Object[_codecs.length()]; final ISeq<Chromosome<G>> chromosomes = genotype.toSeq(); int start = 0; for (int i = 0; i < _codecs.length(); ++i) { final int end = start + _lengths[i]; final Genotype<G> gt = Genotype.of(chromosomes.subSeq(start, end)); groups[i] = _codecs.get(i).decoder().apply(gt); start = end; } return groups; }
public static void main(final String[] args) { final Factory<Genotype<DoubleGene>> gtf = Genotype.of(DoubleChromosome.of(0, 1)); final Engine<DoubleGene, Double> engine = Engine .builder(/*Dummy fitness function*/gt -> Double.NaN, gtf) .evaluator(new RxEvaluator<>(RxEvaluator::fitness)) .build(); final EvolutionResult<DoubleGene, Double> result = engine.stream() .limit(100) .collect(EvolutionResult.toBestEvolutionResult()); System.out.println(result.getBestPhenotype()); }
public static void main(final String[] args) { final Factory<Genotype<DoubleGene>> gtf = Genotype.of(DoubleChromosome.of(0, 1)); final Engine<DoubleGene, Double> engine = Engine .builder(/*Dummy fitness function*/gt -> Double.NaN, gtf) .evaluator(new FutureEvaluator<>(FutureEvaluator::fitness)) .build(); final EvolutionResult<DoubleGene, Double> result = engine.stream() .limit(100) .collect(EvolutionResult.toBestEvolutionResult()); System.out.println(result.getBestPhenotype()); }
@Test public void testGenotypeGenotypeOfT() { final BitChromosome c1 = BitChromosome.of(12); final BitChromosome c2 = BitChromosome.of(12); final Genotype<BitGene> g2 = Genotype.of(c1, c2, c2); final Genotype<BitGene> g4 = g2; assertEquals(g2, g4); assertEquals(g2.hashCode(), g4.hashCode()); }
@Test public void testSetGetChromosome() { LongChromosome c1 = LongChromosome.of(0, 100, 10); LongChromosome c2 = LongChromosome.of(0, 100, 10); @SuppressWarnings("unused") LongChromosome c3 = LongChromosome.of(0, 100, 10); @SuppressWarnings("unused") Genotype<LongGene> g = Genotype.of(c1, c2); }
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 testCreate() { LongChromosome c1 = LongChromosome.of(0, 100, 10); LongChromosome c2 = LongChromosome.of(0, 100, 10); Genotype<LongGene> g1 = Genotype.of(c1, c2); Genotype<LongGene> g2 = g1.newInstance(); assertNotSame(g1, g2); assertNotEquals(g1, g2); }
@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 ); }
@Test public void numberOfGenes() { final Genotype<DoubleGene> genotype = Genotype.of( DoubleChromosome.of(0.0, 1.0, 8), DoubleChromosome.of(1.0, 2.0, 10), DoubleChromosome.of(0.0, 10.0, 9), DoubleChromosome.of(0.1, 0.9, 5) ); Assert.assertEquals(genotype.geneCount(), 32); }
@Test public void survivorsFraction() { final Function<Genotype<DoubleGene>, Double> fitnessFunction = gt -> gt.getGene().getAllele(); final Factory<Genotype<DoubleGene>> genotypeFactory = Genotype.of(DoubleChromosome.of(0, 1)); final Engine<DoubleGene, Double> engine = Engine .builder(fitnessFunction, genotypeFactory) .survivorsFraction(0.3) .build(); Assert.assertEquals(engine.getSurvivorsCount(), 15); Assert.assertEquals(engine.getOffspringCount(), 35); }
@Test public void offspringSize() { final Function<Genotype<DoubleGene>, Double> fitnessFunction = gt -> gt.getGene().getAllele(); final Factory<Genotype<DoubleGene>> genotypeFactory = Genotype.of(DoubleChromosome.of(0, 1)); final Engine<DoubleGene, Double> engine = Engine .builder(fitnessFunction, genotypeFactory) .offspringSize(35) .build(); Assert.assertEquals(engine.getSurvivorsCount(), 15); Assert.assertEquals(engine.getOffspringCount(), 35); }
public static Phenotype<DoubleGene, Double> newDoublePhenotype(final double value) { return Phenotype.of(Genotype.of( DoubleChromosome.of(DoubleGene.of(value, 0, 10))), 0, FF ).evaluate(); }
private static Phenotype<DoubleGene, Double> phenotype(final double value) { return Phenotype.of( Genotype.of(DoubleChromosome.of(DoubleGene.of(value, 0.0, 1000.0))), 1, a -> a.getGene().getAllele() ); }
private static Phenotype<DoubleGene, Double> pt(double value) { return Phenotype.of( Genotype.of(DoubleChromosome.of(DoubleGene.of(value, 0, 10))), 0, gt -> gt.getGene().getAllele() ); }
private static Phenotype<DoubleGene, Double> phenotype(final double value) { return Phenotype.of( Genotype.of(DoubleChromosome.of(DoubleGene.of(value, 0.0, 1000.0))), 1, a -> a.getGene().getAllele() ); }
private static Phenotype<DoubleGene, Double> phenotype(final double value) { return Phenotype.of( Genotype.of(DoubleChromosome.of(DoubleGene.of(value, 0.0, 1000.0))), 1, a -> a.getGene().getAllele() ); }
@Test(expectedExceptions = IllegalArgumentException.class) public void selectNegativeCountArgument() { final Factory<Genotype<DoubleGene>> gtf = Genotype.of(DoubleChromosome.of(0.0, 1.0)); final MSeq<Phenotype<DoubleGene, Double>> population = MSeq.ofLength(2); for (int i = 0, n = 2; i < n; ++i) { population.set(i, Phenotype.of(gtf.newInstance(), 12, TestUtils.FF)); } selector().select(population.toISeq(), -1, Optimize.MAXIMUM); }
@Test(expectedExceptions = NullPointerException.class) public void selectNullOptimizeArgument() { final Factory<Genotype<DoubleGene>> gtf = Genotype.of(DoubleChromosome.of(0.0, 1.0)); final MSeq<Phenotype<DoubleGene, Double>> population = MSeq.ofLength(2); for (int i = 0, n = 2; i < n; ++i) { population.set(i, Phenotype.of(gtf.newInstance(), 12, TestUtils.FF)); } selector().select(population.toISeq(), 1, null); }
private static Phenotype<DoubleGene, Double> phenotype(final double value) { return Phenotype.of( Genotype.of(DoubleChromosome.of( DoubleGene.of(value, 0.0, 1000.0), DoubleGene.of(value, 0.0, 1000.0) )), 1, a -> a.getGene().getAllele() ); }