@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()) ); }
/** * Create a permutation {@code Codec} of integer in the range * {@code [0, length)}. * * @param length the number of permutation elements * @return a permutation {@code Codec} of integers * @throws IllegalArgumentException if the {@code length} is smaller than * one. */ public static Codec<int[], EnumGene<Integer>> ofPermutation(final int length) { require.positive(length); return Codec.of( Genotype.of(PermutationChromosome.ofInteger(length)), gt -> gt.getChromosome().stream() .mapToInt(EnumGene::getAllele) .toArray() ); }
/** * Create a permutation {@code Codec} with the given alleles. * * @param alleles the alleles of the permutation * @param <T> the allele type * @return a new permutation {@code Codec} * @throws IllegalArgumentException if the given allele array is empty * @throws NullPointerException if one of the alleles is {@code null} */ public static <T> Codec<ISeq<T>, EnumGene<T>> ofPermutation(final ISeq<? extends T> alleles) { if (alleles.isEmpty()) { throw new IllegalArgumentException( "Empty allele array is not allowed." ); } return Codec.of( Genotype.of(PermutationChromosome.of(alleles)), gt -> gt.getChromosome().stream() .map(EnumGene::getAllele) .collect(ISeq.toISeq()) ); }
/** * Mutates the given chromosome. * * @see #mutate(Gene, Random) * * @param chromosome the chromosome to mutate * @param p the mutation probability for the underlying genetic objects * @param random the random engine used for the genotype mutation * @return the mutation result */ protected MutatorResult<Chromosome<G>> mutate( final Chromosome<G> chromosome, final double p, final Random random ) { final int P = probability.toInt(p); final ISeq<MutatorResult<G>> result = chromosome.stream() .map(gene -> random.nextInt() < P ? MutatorResult.of(mutate(gene, random), 1) : MutatorResult.of(gene)) .collect(ISeq.toISeq()); return MutatorResult.of( chromosome.newInstance(result.map(MutatorResult::getResult)), result.stream().mapToInt(MutatorResult::getMutations).sum() ); }
Genotype.of(AnyChromosome .of(supplier, alleleValidator, alleleSeqValidator, length)), gt -> gt.getChromosome().stream() .map(Gene::getAllele) .collect(ISeq.toISeq())
gt -> gt.getChromosome().stream() .map(EnumGene::getAllele) .collect(ISeq.toISeq())
@Test public void encoding() { final Codec<Double, DoubleGene> codec = new CompositeCodec<>( ISeq.of( Codecs.ofScalar(DoubleRange.of(0, 1)), Codecs.ofVector(DoubleRange.of(10, 100), 3), Codecs.ofScalar(DoubleRange.of(2, 3)), Codecs.ofVector(DoubleRange.of(200, 500), DoubleRange.of(200, 500)) ), values -> { final Double v1 = (Double)values[0]; final double[] v2 = (double[])values[1]; final Double v3 = (Double)values[2]; final double[] v4 = (double[])values[3]; return v1 + DoubleAdder.sum(v2) + v3 + DoubleAdder.sum(v4); } ); final Genotype<DoubleGene> gt = codec.encoding().newInstance(); final double sum = gt.stream() .mapToDouble(c -> c.stream() .mapToDouble(DoubleGene::doubleValue) .sum()) .sum(); Assert.assertEquals(sum, codec.decoder().apply(gt), 0.000001); }
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); }