/** * Create a new random {@code IntegerChromosome} of length one. * * @param min the minimal value of this chromosome (inclusively). * @param max the maximal value of this chromosome (inclusively). * @return a new random {@code IntegerChromosome} of length one */ public static IntegerChromosome of(final int min, final int max) { return of(min, max, 1); }
@Override public IntegerChromosome newInstance() { return of(_min, _max, lengthRange()); }
public static IntegerChromosome nextIntegerChromosome() { return IntegerChromosome.of(Integer.MIN_VALUE, Integer.MAX_VALUE, 20); }
public static IntegerChromosome nextIntegerChromosome() { return IntegerChromosome.of(Integer.MIN_VALUE, Integer.MAX_VALUE, 20); }
/** * Create a new random {@code IntegerChromosome}. * * @param min the min value of the {@link IntegerGene}s (inclusively). * @param max the max value of the {@link IntegerGene}s (inclusively). * @param length the length of the chromosome. * @return a new random {@code IntegerChromosome} * @throws IllegalArgumentException if the length is smaller than one */ public static IntegerChromosome of( final int min, final int max, final int length ) { return of(min, max, IntRange.of(length)); }
/** * Create a new random {@code IntegerChromosome} of length one. * * @since 3.2 * * @param range the integer range of the chromosome. * @return a new random {@code IntegerChromosome} of length one * @throws NullPointerException if the given {@code range} is {@code null} */ public static IntegerChromosome of(final IntRange range) { return of(range.getMin(), range.getMax(), 1); }
/** * Create a new random {@code IntegerChromosome}. * * @since 3.2 * * @param range the integer range of the chromosome. * @param length the length of the chromosome. * @return a new random {@code IntegerChromosome} * @throws NullPointerException if the given {@code range} is {@code null} * @throws IllegalArgumentException if the length is smaller than one */ public static IntegerChromosome of(final IntRange range, final int length) { return of(range.getMin(), range.getMax(), length); }
public static void main(final String[] args) throws Exception { final IntegerChromosome ch = IntegerChromosome.of( Integer.MIN_VALUE, Integer.MAX_VALUE, 3 ); final XMLOutputFactory factory = XMLOutputFactory.newFactory(); final XMLStreamWriter xml = factory.createXMLStreamWriter(System.out); try { write(ch, xml); } finally { xml.close(); } }
@DataProvider(name = "chromosomes") public Object[][] chromosomes() { return new Object[][] { {IntegerChromosome.of(0, 1000), IntRange.of(1)}, {IntegerChromosome.of(IntRange.of(0, 1000)), IntRange.of(1)}, {IntegerChromosome.of(0, 1000, 1), IntRange.of(1)}, {IntegerChromosome.of(0, 1000, 2), IntRange.of(2)}, {IntegerChromosome.of(0, 1000, 20), IntRange.of(20)}, {IntegerChromosome.of(0, 1000, IntRange.of(2, 10)), IntRange.of(2, 10)}, {IntegerChromosome.of(IntRange.of(0, 1000), IntRange.of(2, 10)), IntRange.of(2, 10)} }; }
/** * 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() ); }
@Test public void toUniquePopulation() { final ISeq<Genotype<IntegerGene>> genotypes = Genotype.of(IntegerChromosome.of(0, Integer.MAX_VALUE)).instances() .limit(10) .collect(ISeq.toISeq()); final EvolutionResult<IntegerGene, Integer> result = result(genotypes); Assert.assertSame( EvolutionResult.<IntegerGene, Integer>toUniquePopulation().apply(result), result ); }
@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 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 // https://github.com/jenetics/jenetics/issues/420 public void bestFitnessResult() { final Genotype<IntegerGene> genotype = Genotype.of(IntegerChromosome.of(0, 10)); final AtomicInteger ai = new AtomicInteger(); final Function<Genotype<IntegerGene>, Integer> ff = x -> ai.incrementAndGet(); final int threshold = 100; final Integer result = Engine.builder(ff, genotype) .build() .stream() .limit(Limits.byFitnessThreshold(threshold)) .collect(EvolutionResult.toBestEvolutionResult()) .getBestFitness(); Assert.assertNotNull(result); Assert.assertTrue( result >= 100, format("Expected value >= %s, but got %s", threshold, result) ); }
@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(invocationCount = 20, successPercentage = 95) public void newInstanceDistribution() { using(new Random(12345), r -> { final int min = 0; final int max = 10000000; final MinMax<Integer> mm = MinMax.of(); final Histogram<Integer> histogram = Histogram.ofInteger(min, max, 10); for (int i = 0; i < 1000; ++i) { final IntegerChromosome chromosome = IntegerChromosome.of(min, max, 500); chromosome.toSeq().forEach(g -> { mm.accept(g.getAllele()); histogram.accept(g.getAllele()); }); } Assert.assertTrue(mm.getMin().compareTo(0) >= 0); Assert.assertTrue(mm.getMax().compareTo(100) <= 100); assertUniformDistribution(histogram); }); }
@Test public void adaptInit() { final Chromosome<IntegerGene> ch = IntegerChromosome.of(IntegerGene.of(5, 0, 1000)); final Genotype<IntegerGene> gt = Genotype.of(ch); final EvolutionInit<IntegerGene> init = EvolutionInit.of( ISeq.<Genotype<IntegerGene>>of(gt), 1L ); final EvolutionStream<IntegerGene, Integer> stream = new AdaptiveEngine<IntegerGene, Integer>(r -> streamable(2)) .stream(init); final int[] array = stream .limit(Limits.byFixedGeneration(12)) .limit(10) .mapToInt(r -> r.getGenotypes().get(0).getGene().intValue()) .toArray(); Assert.assertEquals(array, new int[]{6, 7, 8, 9, 10, 11, 12, 13, 14, 15}); }
public static void main(final String[] args) { final Problem<int[][], IntegerGene, Integer> problem = Problem.of( Matrix::fitness, Codec.of( Genotype.of(IntegerChromosome.of(IntRange.of(0, 10), 3), 3), gt -> gt.stream() .map(ch -> ch.stream() .mapToInt(IntegerGene::intValue).toArray()) .toArray(int[][]::new) ) ); final Engine<IntegerGene, Integer> engine = Engine.builder(problem).build(); final Genotype<IntegerGene> gt = engine.stream() .limit(Limits.byFixedGeneration(20)) .collect(EvolutionResult.toBestGenotype()); final int[][] best = problem.codec().decode(gt); print(best); }
public static EvolutionResult<IntegerGene, Integer> result(final int value) { final Genotype<IntegerGene> genotype = Genotype.of( IntegerChromosome.of( IntegerGene.of(value, 0, Integer.MAX_VALUE) ) ); return EvolutionResult.<IntegerGene, Integer>of( Optimize.MINIMUM, ISeq.of(Phenotype.of(genotype, 1, gt -> gt.getGene().getAllele())), 1, 1, EvolutionDurations.ZERO, 1, 1, 1 ); }
private static EvolutionResult<IntegerGene, Integer> newResult( final Optimize opt, final int value ) { final int length = 1000; final Function<Genotype<IntegerGene>, Integer> ff = gt -> gt.getGene().getAllele(); final MSeq<Phenotype<IntegerGene, Integer>> pop = MSeq.ofLength(length); for (int i = 0; i < length; ++i) { final Genotype<IntegerGene> gt = Genotype.of(IntegerChromosome.of( IntegerGene.of(value, 0, length) )); pop.set(i, Phenotype.of(gt, 1, ff)); } pop.shuffle(RandomRegistry.getRandom()); return EvolutionResult .of(opt, pop.toISeq(), 0, 0, EvolutionDurations.ZERO, 0, 0, 0); }