/** * Create a new chromosome of type {@code A} with the given parameters and * length one. The {@code validator} predicate of the generated gene will * always return {@code true}. * * @param <A> the allele type * @param supplier the allele-supplier which is used for creating new, * random alleles * @return a new chromosome of allele type {@code A} * @throws NullPointerException if the {@code supplier} is {@code null} */ public static <A> AnyChromosome<A> of(final Supplier<? extends A> supplier) { return of(supplier, 1); }
@Override public Chromosome<AnyGene<A>> newInstance(final ISeq<AnyGene<A>> genes) { return new AnyChromosome<>( genes, _supplier, _alleleValidator, _alleleSeqValidator, lengthRange() ); }
@Override public Chromosome<AnyGene<A>> newInstance() { return of(_supplier, _alleleValidator, _alleleSeqValidator, lengthRange()); }
final IntRange lengthRange ) { return new AnyChromosome<>( AnyGene.seq(lengthRange, supplier, alleleValidator), supplier,
@Override public boolean isValid() { Boolean valid = (_alleleValidator == Predicates.TRUE && _alleleSeqValidator == Predicates.<ISeq<A>>True()) ? Boolean.TRUE : _valid; if (valid == null) { final ISeq<A> alleles = stream() .map(Gene::getAllele) .collect(ISeq.toISeq()); valid = _alleleSeqValidator.test(alleles) && alleles.forAll(_alleleValidator); } return _valid = valid; }
/** * Create a new chromosome of type {@code A} with the given parameters. The * {@code validator} predicate of the generated gene will always return * {@code true}. * * @since 4.0 * * @param <A> the allele type * @param supplier the allele-supplier which is used for creating new, * random alleles * @param lengthRange the allowed length range of the chromosome * @return a new chromosome of allele type {@code A} * @throws NullPointerException if the {@code supplier} is {@code null} * @throws IllegalArgumentException if chromosome length is smaller than one. */ public static <A> AnyChromosome<A> of( final Supplier<? extends A> supplier, final IntRange lengthRange ) { return of(supplier, Predicates.TRUE, lengthRange); }
/** * Create a new chromosome of type {@code A} with the given parameters and * length one. * * @param <A> the allele type * @param supplier the allele-supplier which is used for creating new, * random alleles * @param validator the validator used for validating the created gene. This * predicate is used in the {@link #isValid()} method. * @return a new chromosome of allele type {@code A} * @throws NullPointerException if the {@code supplier} or {@code validator} * is {@code null} */ public static <A> AnyChromosome<A> of( final Supplier<? extends A> supplier, final Predicate<? super A> validator ) { return of(supplier, validator, 1); }
/** * Create a new chromosome of type {@code A} with the given parameters. The * {@code validator} predicate of the generated gene will always return * {@code true}. * * @param <A> the allele type * @param supplier the allele-supplier which is used for creating new, * random alleles * @param length the length of the created chromosome * @return a new chromosome of allele type {@code A} * @throws NullPointerException if the {@code supplier} is {@code null} * @throws IllegalArgumentException if chromosome length is smaller than one. */ public static <A> AnyChromosome<A> of( final Supplier<? extends A> supplier, final int length ) { return of(supplier, Predicates.TRUE, length); }
/** * Create a new chromosome of type {@code A} with the given parameters. * * @param <A> the allele type * @param supplier the allele-supplier which is used for creating new, * random alleles * @param alleleValidator the validator used for validating the created gene. * This predicate is used in the {@link AnyGene#isValid()} method. * @param alleleSeqValidator the validator used for validating the created * chromosome. This predicate is used in the * {@link AnyChromosome#isValid()} method. * @param length the length of the chromosome * @return a new chromosome of allele type {@code A} * @throws NullPointerException if the given arguments is {@code null} * @throws IllegalArgumentException if chromosome length is smaller than one. */ public static <A> AnyChromosome<A> of( final Supplier<? extends A> supplier, final Predicate<? super A> alleleValidator, final Predicate<? super ISeq<A>> alleleSeqValidator, final int length ) { return of(supplier, alleleValidator, alleleSeqValidator, IntRange.of(length)); }
/** * Create a new chromosome of type {@code A} with the given parameters. * * @param <A> the allele type * @param supplier the allele-supplier which is used for creating new, * random alleles * @param validator the validator used for validating the created gene. This * predicate is used in the {@link AnyGene#isValid()} method. * @param length the length of the chromosome * @return a new chromosome of allele type {@code A} * @throws NullPointerException if the {@code supplier} or {@code validator} * is {@code null} * @throws IllegalArgumentException if chromosome length is smaller than one. */ public static <A> AnyChromosome<A> of( final Supplier<? extends A> supplier, final Predicate<? super A> validator, final int length ) { return of(supplier, validator, Predicates.<ISeq<A>>True(), length); }
/** * Create a new chromosome of type {@code A} with the given parameters. * * @since 4.0 * * @param <A> the allele type * @param supplier the allele-supplier which is used for creating new, * random alleles * @param validator the validator used for validating the created gene. This * predicate is used in the {@link AnyGene#isValid()} method. * @param lengthRange the allowed length range of the chromosome * @return a new chromosome of allele type {@code A} * @throws NullPointerException if the {@code supplier} or {@code validator} * is {@code null} * @throws IllegalArgumentException if chromosome length is smaller than one. */ public static <A> AnyChromosome<A> of( final Supplier<? extends A> supplier, final Predicate<? super A> validator, final IntRange lengthRange ) { return of(supplier, validator, Predicates.<ISeq<A>>True(), lengthRange); }
@Test public void create() { AnyChromosome.of( () -> "foo", AnyChromosomeTest::alleleValidator ); AnyChromosome.of( () -> "foo", AnyChromosomeTest::alleleValidator, 3 ); AnyChromosome.of( () -> "foo", a -> true, AnyChromosomeTest::alleleSeqValidator, 3 ); AnyChromosome.of( () -> "foo", a -> true, a -> true, 3 ); AnyChromosome.of( () -> "foo", AnyChromosomeTest::alleleValidator, AnyChromosomeTest::alleleSeqValidator, 3 ); }
@Override protected Factory<Chromosome<AnyGene<Integer>>> factory() { return () -> AnyChromosome.of(RandomRegistry.getRandom()::nextInt, 10); }
@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()) ); }
.of(supplier, alleleValidator, alleleSeqValidator, length)), gt -> gt.getChromosome().stream() .map(Gene::getAllele)
/** * Return a scala {@code Codec} with the given allele {@link Supplier} and * allele {@code validator}. The {@code supplier} is responsible for * creating new random alleles. * * @see #ofScalar(Supplier, Predicate) * @see AnyGene#of(Supplier) * @see AnyChromosome#of(Supplier) * * @param <A> the allele type * @param supplier the allele-supplier which is used for creating new, * random alleles * @return a new {@code Codec} with the given parameters * @throws NullPointerException if the parameter is {@code null} */ public static <A> Codec<A, AnyGene<A>> ofScalar( final Supplier<? extends A> supplier ) { return Codec.of( Genotype.of(AnyChromosome.of(supplier)), gt -> gt.getGene().getAllele() ); }
) { return Codec.of( Genotype.of(AnyChromosome.of(supplier, validator)), gt -> gt.getGene().getAllele() );