/** * Returns a sequential {@code Stream} of genes with this chromosome as * its source. * * @since 3.3 * * @return a sequential {@code Stream} of genes */ public default Stream<G> stream() { return IntStream.range(0, length()).mapToObj(this::getGene); }
/** * Return the number of genes this genotype consists of. This is the sum of * the number of genes of the genotype chromosomes. * * @return Return the number of genes this genotype consists of. */ public int geneCount() { int count = 0; for (int i = 0, n = _chromosomes.length(); i < n; ++i) { count += _chromosomes.get(i).length(); } return count; }
private static int ngenes(final Seq<? extends Chromosome<?>> chromosomes) { int count = 0; for (int i = 0, n = chromosomes.length(); i < n; ++i) { count += chromosomes.get(i).length(); } return count; }
private static int length(final Genotype<EnumGene<Integer>> genotype) { return IntStream.range(1, genotype.getChromosome().length()) .map(i -> dist(genotype.getChromosome(), i, i - 1)) .sum(); }
static <G extends NumericGene<?, G>> ISeq<DoubleMoments> statistics(final Seq<? extends Phenotype<G, ?>> population) { final Map<Long, DoubleMomentStatistics> statistics = new HashMap<>(); for (Phenotype<G, ?> pt : population) { final Genotype<G> gt = pt.getGenotype(); for (int i = 0; i < gt.length(); ++i) { final Chromosome<G> ch = gt.getChromosome(i); for (int j = 0; j < ch.length(); ++j) { statistics .computeIfAbsent(((long)i << 32) | (j & 0xffffffffL), k -> new DoubleMomentStatistics()) .accept(ch.getGene(j).doubleValue()); } } } return statistics.values().stream() .map(DoubleMomentStatistics::toDoubleMoments) .collect(ISeq.toISeq()); }
@Override protected MutatorResult<Chromosome<PolygonGene>> mutate( final Chromosome<PolygonGene> chromosome, final double p, final Random random ) { return MutatorResult.of( chromosome.newInstance(chromosome.toSeq().map(this::mutate)), chromosome.length() ); }
@Test public void length() { final Chromosome<G> c = factory().newInstance(); final ISeq<G> a = c.toSeq(); Assert.assertEquals(c.length(), a.length()); }
@Test public void factoryTest() { final Genotype<DoubleGene> factory = (Genotype<DoubleGene>)_factory; final Genotype<DoubleGene> gt = _factory.newInstance(); Assert.assertEquals(factory.length(), gt.length()); Assert.assertEquals(factory.geneCount(), gt.geneCount()); for (int i = 0; i < factory.length(); ++i) { Assert.assertEquals( factory.getChromosome(i).length(), gt.getChromosome(i).length() ); } }
@Test(dataProvider = "intScalarData") public void ofIntScalar(final IntRange domain) { final Codec<Integer, IntegerGene> codec = Codecs.ofScalar(domain); final Genotype<IntegerGene> gt = codec.encoding().newInstance(); Assert.assertEquals(gt.length(), 1); Assert.assertEquals(gt.getChromosome().length(), 1); Assert.assertEquals(gt.getGene().getMin().intValue(), domain.getMin()); Assert.assertEquals(gt.getGene().getMax().intValue(), domain.getMax()); final Function<Genotype<IntegerGene>, Integer> f = codec.decoder(); Assert.assertEquals(f.apply(gt).intValue(), gt.getGene().intValue()); }
@Test(dataProvider = "longScalarData") public void ofLongScalar(final LongRange domain) { final Codec<Long, LongGene> codec = Codecs.ofScalar(domain); final Genotype<LongGene> gt = codec.encoding().newInstance(); Assert.assertEquals(gt.length(), 1); Assert.assertEquals(gt.getChromosome().length(), 1); Assert.assertEquals(gt.getGene().getMin().longValue(), domain.getMin()); Assert.assertEquals(gt.getGene().getMax().longValue(), domain.getMax()); final Function<Genotype<LongGene>, Long> f = codec.decoder(); Assert.assertEquals(f.apply(gt).longValue(), gt.getGene().longValue()); }
@Test public void ofSubSet() { final Codec<ISeq<String>, EnumGene<String>> codec = Codecs.ofSubSet( ISeq.of("1", "2", "3", "4", "5"), 3 ); for (int i = 0; i < 100; ++i) { final Genotype<EnumGene<String>> gt = codec.encoding().newInstance(); final Chromosome<EnumGene<String>> ch = gt.getChromosome(); Assert.assertEquals(ch.length(), 3); Assert.assertTrue(ch.isValid()); final ISeq<String> permutation = codec.decoder().apply(gt); Assert.assertEquals(permutation.length(), 3); } }
@Test(dataProvider = "intVectorData") public void ofIntVector(final IntRange domain, final int length) { final Codec<int[], IntegerGene> codec = Codecs.ofVector(domain, length); final Genotype<IntegerGene> gt = codec.encoding().newInstance(); Assert.assertEquals(gt.length(), 1); Assert.assertEquals(gt.getChromosome().length(), length); for (IntegerGene gene : gt.getChromosome()) { Assert.assertEquals(gene.getMin().intValue(), domain.getMin()); Assert.assertEquals(gene.getMax().intValue(), domain.getMax()); } final Function<Genotype<IntegerGene>, int[]> f = codec.decoder(); final int[] value = f.apply(gt); Assert.assertEquals(value.length, length); for (int i = 0; i < length; ++i) { Assert.assertEquals(gt.get(0, i).intValue(), value[i]); } }
@Test(dataProvider = "longVectorData") public void ofLongVector(final LongRange domain, final int length) { final Codec<long[], LongGene> codec = Codecs.ofVector(domain, length); final Genotype<LongGene> gt = codec.encoding().newInstance(); Assert.assertEquals(gt.length(), 1); Assert.assertEquals(gt.getChromosome().length(), length); for (LongGene gene : gt.getChromosome()) { Assert.assertEquals(gene.getMin().longValue(), domain.getMin()); Assert.assertEquals(gene.getMax().longValue(), domain.getMax()); } final Function<Genotype<LongGene>, long[]> f = codec.decoder(); final long[] value = f.apply(gt); Assert.assertEquals(value.length, length); for (int i = 0; i < length; ++i) { Assert.assertEquals(gt.get(0, i).longValue(), value[i]); } }
@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(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(dataProvider = "longVectorDataVector") public void ofLongVectorVector(final LongRange[] domain) { final Codec<long[], LongGene> codec = Codecs.ofVector(domain); final Genotype<LongGene> gt = codec.encoding().newInstance(); Assert.assertEquals(gt.length(), domain.length); for (int i = 0; i < gt.length(); ++i) { final Chromosome<LongGene> ch = gt.getChromosome(i); Assert.assertEquals(ch.length(), 1); final LongGene gene = ch.getGene(); Assert.assertEquals(gene.getMin().longValue(), domain[i].getMin()); Assert.assertEquals(gene.getMax().longValue(), domain[i].getMax()); } final Function<Genotype<LongGene>, long[]> f = codec.decoder(); final long[] value = f.apply(gt); Assert.assertEquals(value.length, domain.length); for (int i = 0; i < domain.length; ++i) { Assert.assertEquals(gt.get(i, 0).longValue(), value[i]); } }
@Test public void ofPermutation() { final Codec<ISeq<String>, EnumGene<String>> codec = Codecs .ofPermutation(ISeq.of("foo", "bar", "zoo")); final Genotype<EnumGene<String>> gt = codec.encoding().newInstance(); Assert.assertEquals(gt.length(), 1); final Function<Genotype<EnumGene<String>>, ISeq<String>> f = codec.decoder(); final ISeq<String> value = f.apply(gt); Assert.assertEquals(value.length(), gt.getChromosome().length()); for (int i = 0; i < value.length(); ++i) { Assert.assertEquals(value.get(i), gt.get(0, i).toString()); } }
@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 public void newInstance() { final Genotype<DoubleGene> gt1 = Genotype.of( //Rotation DoubleChromosome.of(DoubleGene.of(-Math.PI, Math.PI)), //Translation DoubleChromosome.of(DoubleGene.of(-300, 300), DoubleGene.of(-300, 300)), //Shear DoubleChromosome.of(DoubleGene.of(-0.5, 0.5), DoubleGene.of(-0.5, 0.5)) ); final Genotype<DoubleGene> gt2 = gt1.newInstance(); Assert.assertEquals(gt1.length(), gt2.length()); for (int i = 0; i < gt1.length(); ++i) { Chromosome<DoubleGene> ch1 = gt1.getChromosome(i); Chromosome<DoubleGene> ch2 = gt2.getChromosome(i); Assert.assertEquals(ch1.length(), ch2.length()); } }