/** * Return the chromosome at the given index. It is guaranteed, that the * returned chromosome is not null. * * @see #getChromosome(int) * @since 4.0 * * @param chromosomeIndex Chromosome index. * @return The Chromosome. * @throws IndexOutOfBoundsException if * {@code (index < 0 || index >= _length)}. */ public Chromosome<G> get(final int chromosomeIndex) { return getChromosome(chromosomeIndex); }
private static double fitness(final Genotype gt) { final DoubleChromosome dc = (DoubleChromosome)gt.getChromosome(0); final BitChromosome bc = (BitChromosome)gt.getChromosome(1); final IntegerChromosome ic = (IntegerChromosome)gt.getChromosome(2); return dc.doubleValue() + bc.bitCount() + ic.doubleValue(); }
private static Integer count(final Genotype<BitGene> gt) { return ((BitChromosome)gt.getChromosome()).bitCount(); }
/** * 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); }
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(); }
private static Integer count(final Genotype<BitGene> gt) { return gt.getChromosome() .as(BitChromosome.class) .bitCount(); }
private static int score(final Genotype<CharacterGene> gt) { final CharSequence source = (CharSequence)gt.getChromosome(); return IntStream.range(0, TARGET.length()) .map(i -> source.charAt(i) == TARGET.charAt(i) ? 1 : 0) .sum(); }
@Override public Codec<ISeq<Rect>, AnyGene<Rect>> codec() { return Codec.of( Genotype.of(AnyChromosome.of( () -> Rect.newInstance(_outer), a -> true, a -> true, MAX_RECT_COUNT )), gt -> gt.getChromosome().stream() .map(AnyGene::getAllele) .filter(r -> r != Rect.EMPTY) .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() ); }
@Override public Codec<ISeq<BitGene>, BitGene> codec() { return Codec.of( Genotype.of(BitChromosome.of(_length, _onesProbability)), gt -> gt.getChromosome().toSeq() ); }
private void onNewResult( final EvolutionResult<PolygonGene, Double> current, final EvolutionResult<PolygonGene, Double> best ) { invokeLater(() -> { final Genotype<PolygonGene> gt = best .getBestPhenotype() .getGenotype(); bestEvolutionResultPanel.update(best); currentevolutionResultPanel.update(current); _polygonPanel.setChromosome((PolygonChromosome)gt.getChromosome()); _polygonPanel.repaint(); }); }
@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 = "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 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 public void selectMinimum() { final Function<Genotype<IntegerGene>, Integer> ff = gt -> gt.getChromosome().toSeq().stream() .mapToInt(IntegerGene::intValue) .sum(); Factory<Genotype<IntegerGene>> gtf = Genotype.of(IntegerChromosome.of(0, 100, 10)); final ISeq<Phenotype<IntegerGene, Integer>> population = IntStream.range(0, 50) .mapToObj(i -> Phenotype.of(gtf.newInstance(), 50, ff)) .collect(ISeq.toISeq()); final StochasticUniversalSelector<IntegerGene, Integer> selector = new StochasticUniversalSelector<>(); final ISeq<Phenotype<IntegerGene, Integer>> selection = selector.select(population, 50, Optimize.MINIMUM); }
@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 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()); } }
public static void main(final String[] args) throws Exception { final Problem<BitChromosome, BitGene, Integer> count = Problem.of( BitChromosome::bitCount, Codec.of( Genotype.of(BitChromosome.of(10)), gt -> gt.getChromosome().as(BitChromosome.class) ) ); final Engine<BitGene, Integer> engine = Engine.builder(count).build(); final EvolutionResult<BitGene, Integer> result = engine.stream() .limit(10) .collect(EvolutionResult.toBestEvolutionResult()); final ISeq<Genotype<BitGene>> genotypes = result.getGenotypes(); Writers.write(System.out, genotypes.asList(), Writers.BitChromosome.writer()); Readers.read(System.in, Readers.BitChromosome.reader()); engine.stream(genotypes); }