/** * Returns an int array containing all of the elements in this chromosome * in proper sequence. * * @since 3.0 * * @return an array containing the elements of this chromosome */ public int[] toArray() { return toArray(new int[length()]); }
/** * Return a vector {@code Codec} for the given range. All vector values * are restricted by the same domain. * * @param domain the domain of the vector values * @param length the vector length * @return a new vector {@code Codec} * @throws NullPointerException if the given {@code domain} is {@code null} * @throws IllegalArgumentException if the {@code length} is smaller than * one. */ public static Codec<int[], IntegerGene> ofVector( final IntRange domain, final int length ) { requireNonNull(domain); require.positive(length); return Codec.of( Genotype.of(IntegerChromosome.of(domain, length)), gt -> gt.getChromosome().as(IntegerChromosome.class).toArray() ); }
@Test public void variableDoubleSum() { final Problem<int[], IntegerGene, Integer> problem = Problem.of( array -> IntStream.of(array).sum(), Codec.of( Genotype.of(IntegerChromosome.of(0, 100, IntRange.of(10, 100))), gt -> gt.getChromosome().as(IntegerChromosome.class).toArray() ) ); final Engine<IntegerGene, Integer> engine = Engine.builder(problem) .alterers( new Mutator<>(), new SwapMutator<>()) .selector(new TournamentSelector<>()) .minimizing() .build(); final int[] result = problem.codec().decode( engine.stream() .limit(100) .collect(EvolutionResult.toBestGenotype()) ); Assert.assertTrue(result.length < 50, "result length: " + result.length); //System.out.println(result.length); //System.out.println(Arrays.toString(result)); }