/** * Create a vector {@code Codec} for the given ranges. Each vector element * might have a different domain. The vector length is equal to the number * of domains. * * @param domains the domain ranges * @return a new vector {@code Codec} * @throws NullPointerException if one of the arguments is {@code null} * @throws IllegalArgumentException if the {@code domains} array is empty */ public static Codec<long[], LongGene> ofVector(final LongRange... domains) { if (domains.length == 0) { throw new IllegalArgumentException("Domains must not be empty."); } final ISeq<LongChromosome> chromosomes = Stream.of(domains) .peek(Objects::requireNonNull) .map(LongGene::of) .map(LongChromosome::of) .collect(ISeq.toISeq()); return Codec.of( Genotype.of(chromosomes), gt -> { final long[] args = new long[gt.length()]; for (int i = gt.length(); --i >= 0;) { args[i] = gt.getChromosome(i).getGene().longValue(); } return args; } ); }
/** * Create a vector {@code Codec} for the given ranges. Each vector element * might have a different domain. The vector length is equal to the number * of domains. * * @param domains the domain ranges * @return a new vector {@code Codec} * @throws NullPointerException if one of the arguments is {@code null} * @throws IllegalArgumentException if the {@code domains} array is empty */ public static Codec<int[], IntegerGene> ofVector(final IntRange... domains) { if (domains.length == 0) { throw new IllegalArgumentException("Domains must not be empty."); } final ISeq<IntegerChromosome> chromosomes = Stream.of(domains) .peek(Objects::requireNonNull) .map(IntegerGene::of) .map(IntegerChromosome::of) .collect(ISeq.toISeq()); return Codec.of( Genotype.of(chromosomes), gt -> { final int[] args = new int[gt.length()]; for (int i = gt.length(); --i >= 0;) { args[i] = gt.getChromosome(i).getGene().intValue(); } return args; } ); }
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()); }
@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(dataProvider = "intVectorDataVector") public void ofIntVectorVector(final IntRange[] domain) { final Codec<int[], IntegerGene> codec = Codecs.ofVector(domain); final Genotype<IntegerGene> gt = codec.encoding().newInstance(); Assert.assertEquals(gt.length(), domain.length); for (int i = 0; i < gt.length(); ++i) { final Chromosome<IntegerGene> ch = gt.getChromosome(i); Assert.assertEquals(ch.length(), 1); final IntegerGene gene = ch.getGene(); Assert.assertEquals(gene.getMin().intValue(), domain[i].getMin()); Assert.assertEquals(gene.getMax().intValue(), domain[i].getMax()); } final Function<Genotype<IntegerGene>, int[]> f = codec.decoder(); final int[] value = f.apply(gt); Assert.assertEquals(value.length, domain.length); for (int i = 0; i < domain.length; ++i) { Assert.assertEquals(gt.get(i, 0).intValue(), value[i]); } }
@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(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 = "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 = "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 = "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 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()); } }
@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()); } }
@Override protected int recombine( final MSeq<Phenotype<G, C>> population, final int[] individuals, final long generation ) { assert individuals.length == 2 : "Required order of 2"; final Random random = RandomRegistry.getRandom(); final Phenotype<G, C> pt1 = population.get(individuals[0]); final Phenotype<G, C> pt2 = population.get(individuals[1]); final Genotype<G> gt1 = pt1.getGenotype(); final Genotype<G> gt2 = pt2.getGenotype(); //Choosing the Chromosome index for crossover. final int chIndex = random.nextInt(min(gt1.length(), gt2.length())); final MSeq<Chromosome<G>> c1 = gt1.toSeq().copy(); final MSeq<Chromosome<G>> c2 = gt2.toSeq().copy(); crossover(c1, c2, chIndex); //Creating two new Phenotypes and exchanging it with the old. population.set( individuals[0], pt1.newInstance(Genotype.of(c1.toISeq()), generation) ); population.set( individuals[1], pt2.newInstance(Genotype.of(c2.toISeq()), generation) ); return getOrder(); }
@Override protected int recombine( final MSeq<Phenotype<G, C>> population, final int[] individuals, final long generation ) { final Random random = RandomRegistry.getRandom(); final Phenotype<G, C> pt1 = population.get(individuals[0]); final Phenotype<G, C> pt2 = population.get(individuals[1]); final Genotype<G> gt1 = pt1.getGenotype(); final Genotype<G> gt2 = pt2.getGenotype(); //Choosing the Chromosome index for crossover. final int cindex = random.nextInt(min(gt1.length(), gt2.length())); final MSeq<Chromosome<G>> c1 = gt1.toSeq().copy(); final ISeq<Chromosome<G>> c2 = gt2.toSeq(); // Calculate the mean value of the gene array. final MSeq<G> mean = mean( c1.get(cindex).toSeq().copy(), c2.get(cindex).toSeq() ); c1.set(cindex, c1.get(cindex).newInstance(mean.toISeq())); population.set( individuals[0], pt1.newInstance(Genotype.of(c1), generation) ); return 1; }
public int diff( final Seq<Phenotype<DoubleGene, Double>> p1, final Seq<Phenotype<DoubleGene, Double>> p2 ) { int count = 0; for (int i = 0; i < p1.size(); ++i) { final Genotype<?> gt1 = p1.get(i).getGenotype(); final Genotype<?> gt2 = p2.get(i).getGenotype(); for (int j = 0; j < gt1.length(); ++j) { final Chromosome<?> c1 = gt1.getChromosome(j); final Chromosome<?> c2 = gt2.getChromosome(j); for (int k = 0; k < c1.length(); ++k) { if (!c1.getGene(k).equals(c2.getGene(k))) { ++count; } } } } return count; }
/** * Count the number of different genes. */ public static int diff ( final Seq<Phenotype<DoubleGene, Double>> p1, final Seq<Phenotype<DoubleGene, Double>> p2 ) { int count = 0; for (int i = 0; i < p1.size(); ++i) { final Genotype<?> gt1 = p1.get(i).getGenotype(); final Genotype<?> gt2 = p2.get(i).getGenotype(); for (int j = 0; j < gt1.length(); ++j) { final Chromosome<?> c1 = gt1.getChromosome(j); final Chromosome<?> c2 = gt2.getChromosome(j); for (int k = 0; k < c1.length(); ++k) { if (!c1.getGene(k).equals(c2.getGene(k))) { ++count; } } } } return count; }