/** * Return the first gene of this chromosome. Each chromosome must contain * at least one gene. * * @return the first gene of this chromosome. */ public default G getGene() { return getGene(0); }
/** * Return the gene from the given chromosome- and gene index. This is a * shortcut for {@code gt.getChromosome(chromosomeIndex).getGene(geneIndex)}. * * @since 3.0 * * @param chromosomeIndex the chromosome index * @param geneIndex the gene index within the chromosome * @return the gene with the given indexes * @throws IndexOutOfBoundsException if the given indexes are not within the * allowed range */ public G get(final int chromosomeIndex, final int geneIndex) { return getChromosome(chromosomeIndex).getGene(geneIndex); }
/** * Return the first {@link Gene} of the first {@link Chromosome} of this * {@code Genotype}. This is a shortcut for * <pre>{@code * final Genotype<DoubleGene> gt = ... * final DoubleGene gene = gt.getChromosome(0).getGene(0); * }</pre> * * @return the first {@link Gene} of the first {@link Chromosome} of this * {@code Genotype}. */ public G getGene() { assert _chromosomes != null; assert _chromosomes.get(0) != null; return _chromosomes.get(0).getGene(); }
private MutatorResult<Chromosome<G>> mutate(final Chromosome<G> chromosome) { final TreeNode<A> tree = TreeNode.ofTree(chromosome.getGene()); mutate(tree); final FlatTreeNode<A> flat = FlatTreeNode.of(tree); final ISeq<G> genes = flat.map(t -> gene(chromosome.getGene(), t)); return MutatorResult.of(chromosome.newInstance(genes), 1); }
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()); }
/** * Return a scalar {@code Codec} for the given range. * * @param domain the domain of the returned {@code Codec} * @return a new scalar {@code Codec} with the given domain. * @throws NullPointerException if the given {@code domain} is {@code null} */ public static Codec<Integer, IntegerGene> ofScalar(final IntRange domain) { requireNonNull(domain); return Codec.of( Genotype.of(IntegerChromosome.of(domain)), gt -> gt.getChromosome().getGene().getAllele() ); }
/** * 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; } ); }
@Test public void iterator() { final Chromosome<G> c = factory().newInstance(); final ISeq<G> a = c.toSeq(); int index = 0; for (G gene : c) { Assert.assertEquals(gene, a.get(index)); Assert.assertEquals(gene, c.getGene(index)); ++index; } }
static Codec<Tuple, DoubleGene> codec( final IntRange v1Domain, final LongRange v2Domain, final DoubleRange v3Domain ) { return Codec.of( Genotype.of( DoubleChromosome.of(DoubleRange.of(v1Domain.getMin(), v1Domain.getMax())), DoubleChromosome.of(DoubleRange.of(v2Domain.getMin(), v2Domain.getMax())), DoubleChromosome.of(v3Domain) ), gt -> new Tuple( gt.getChromosome(0).getGene().intValue(), gt.getChromosome(1).getGene().longValue(), gt.getChromosome(2).getGene().doubleValue() ) ); }
private <A> void crossover( final MSeq<Chromosome<G>> c1, final MSeq<Chromosome<G>> c2, final int index ) { @SuppressWarnings("unchecked") final TreeNode<A> tree1 = (TreeNode<A>)TreeNode.ofTree(c1.get(index).getGene()); @SuppressWarnings("unchecked") final TreeNode<A> tree2 = (TreeNode<A>)TreeNode.ofTree(c2.get(index).getGene()); crossover(tree1, tree2); final FlatTreeNode<A> flat1 = FlatTreeNode.of(tree1); final FlatTreeNode<A> flat2 = FlatTreeNode.of(tree2); @SuppressWarnings("unchecked") final TreeGene<A, ?> template = (TreeGene<A, ?>)c1.get(0).getGene(); final ISeq<G> genes1 = flat1.map(tree -> gene(template, tree)); final ISeq<G> genes2 = flat2.map(tree -> gene(template, tree)); c1.set(index, c1.get(index).newInstance(genes1)); c2.set(index, c2.get(index).newInstance(genes2)); }
@Test public void maximize() { using(new Random(7345), r -> { final Function<Genotype<IntegerGene>, Integer> ff = g -> g.getChromosome().getGene().getAllele(); final Factory<Phenotype<IntegerGene, Integer>> ptf = () -> Phenotype.of(Genotype.of(IntegerChromosome.of(0, 100)), 1, ff); final ISeq<Phenotype<IntegerGene, Integer>> population = IntStream.range(0, 1000) .mapToObj(i -> ptf.newInstance()) .collect(ISeq.toISeq()); final RouletteWheelSelector<IntegerGene, Integer> selector = new RouletteWheelSelector<>(); final double[] p = selector.probabilities(population, 100, Optimize.MAXIMUM); Assert.assertTrue(RouletteWheelSelector.sum2one(p), Arrays.toString(p) + " != 1"); }); }
@Test public void minimize() { using(new Random(7345), r -> { final Function<Genotype<IntegerGene>, Integer> ff = g -> g.getChromosome().getGene().getAllele(); final Factory<Phenotype<IntegerGene, Integer>> ptf = () -> Phenotype.of(Genotype.of(IntegerChromosome.of(0, 100)), 1, ff); final ISeq<Phenotype<IntegerGene, Integer>> population = IntStream.range(0, 1000) .mapToObj(i -> ptf.newInstance()) .collect(ISeq.toISeq()); final RouletteWheelSelector<IntegerGene, Integer> selector = new RouletteWheelSelector<>(); final double[] p = selector.probabilities(population, 100, Optimize.MINIMUM); Assert.assertTrue(RouletteWheelSelector.sum2one(p), Arrays.toString(p) + " != 1"); }); }
@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 = "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 = "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]); } }
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; }