PartialAlterer implements Alterer<G, C> { public static <G extends Gene<?, G>, C extends Comparable<? super C>> Alterer<G, C> of(final Alterer<G, C> alterer, final int... indices) { return new PartialAlterer<>(alterer, Projection.of(indices)); } private  PartialAlterer(final Alterer<G, C> alterer, final Projection projection); @Override AltererResult<G, C> alter(final Seq<Phenotype<G, C>> population, final long generation); static Alterer<G, C> of(final Alterer<G, C> alterer, final int... indices); static Alterer<G, C> of(final Alterer<G, C> alterer, final IntRange section);  }
@Test public void split() { final Phenotype<DoubleGene, Double> pt = Phenotype.of( Genotype.of( DoubleChromosome.of(0, 1), DoubleChromosome.of(1, 2), DoubleChromosome.of(2, 3), DoubleChromosome.of(3, 4), DoubleChromosome.of(4, 5), DoubleChromosome.of(5, 6), DoubleChromosome.of(6, 7), DoubleChromosome.of(7, 8), DoubleChromosome.of(8, 9), DoubleChromosome.of(9, 10) ), 0 ); final PartialAlterer.Projection projection = PartialAlterer.Projection.of(1, 3, 5); final Phenotype<DoubleGene, Double> split = projection.project(pt); Assert.assertEquals(split.genotype().length(), 3); Assert.assertEquals(split.genotype().get(0), pt.genotype().get(1)); Assert.assertEquals(split.genotype().get(1), pt.genotype().get(3)); Assert.assertEquals(split.genotype().get(2), pt.genotype().get(5)); }
@Test public void merge() { final Genotype<DoubleGene> gt = Genotype.of( DoubleChromosome.of(0, 1), DoubleChromosome.of(1, 2), DoubleChromosome.of(2, 3), DoubleChromosome.of(3, 4) ); final ISeq<Phenotype<DoubleGene, Double>> population = gt.instances() .limit(3) .map(g -> Phenotype.<DoubleGene, Double>of(g, 0)) .collect(ISeq.toISeq()); final Projection projection = PartialAlterer.Projection.of(1, 3); final Seq<Phenotype<DoubleGene, Double>> split = projection.project(population); Assert.assertEquals(split.length(), population.length()); for (int i = 0; i < population.length(); ++i) { Assert.assertEquals(split.get(i).genotype().length(), 2); } final Seq<Phenotype<DoubleGene, Double>> merged = projection.merge(split, population); Assert.assertEquals(merged, population); }
@Test(expectedExceptions = CompletionException.class) public void withEngineOutOfBounds() { final Problem<Double, DoubleGene, Double> problem = Problem.of( x -> cos(0.5 + sin(x))*cos(x), Codecs.ofScalar(DoubleRange.of(0.0, 2.0*PI)) ); final Genotype<DoubleGene> gtf = Genotype.of( DoubleChromosome.of(0, 1), DoubleChromosome.of(1, 2), DoubleChromosome.of(2, 3), DoubleChromosome.of(3, 4) ); final Engine<DoubleGene, Double> engine = Engine .builder(gt -> gt.gene().doubleValue(), gtf) .selector(new RouletteWheelSelector<>()) .alterers( PartialAlterer.of(new Mutator<DoubleGene, Double>(), 0, 2), PartialAlterer.of(new MeanAlterer<DoubleGene, Double>(), 3, 4), new GaussianMutator<>() ) .build(); engine.stream() .limit(100) .collect(EvolutionResult.toBestEvolutionResult()); }