public class AnyChromosome<A> extends AbstractChromosome<AnyGene<A>>
Chromosome implementation, which allows to create genes without
explicit implementing the Chromosome interface.
public class LastMonday {
// First monday of 2015.
private static final LocalDate MIN_MONDAY = LocalDate.of(2015, 1, 5);
// The used Codec.
private static final Codec<LocalDate, AnyGene<LocalDate>> CODEC = Codec.of(
Genotype.of(AnyChromosome.of(LastMonday::nextRandomMonday)),
gt -> gt.getGene().getAllele()
);
// Supplier of random 'LocalDate' objects. The implementation is responsible
// for guaranteeing the desired allele restriction. In this case we will
// generate only mondays.
private static LocalDate nextRandomMonday() {
return MIN_MONDAY.plusWeeks(RandomRegistry.getRandom().nextInt(1000));
}
// The fitness function: find a monday at the end of the month.
private static double fitness(final LocalDate date) {
return date.getDayOfMonth();
}
public static void main(final String[] args) {
final Engine<AnyGene<LocalDate>, Double> engine = Engine
.builder(LastMonday::fitness, CODEC)
.offspringSelector(new RouletteWheelSelector<>())
.build();
final Phenotype<AnyGene<LocalDate>, Double> best = engine.stream()
.limit(50)
.collect(EvolutionResult.toBestPhenotype());
System.out.println(best);
}
}AnyChromosome is used
to use for an allele-type with no predefined gene- and chromosome type._genes| Modifier | Constructor and Description |
|---|---|
protected |
AnyChromosome(ISeq<AnyGene<A>> genes,
Supplier<? extends A> supplier,
Predicate<? super A> alleleValidator,
Predicate<? super ISeq<? super A>> alleleSeqValidator)
Create a new
AnyChromosome from the given genes
array. |
| Modifier and Type | Method and Description |
|---|---|
boolean |
isValid()
Check if this object is valid.
|
Chromosome<AnyGene<A>> |
newInstance()
Create a new instance of type T.
|
Chromosome<AnyGene<A>> |
newInstance(ISeq<AnyGene<A>> genes)
A factory method which creates a new
Chromosome of specific type
and the given genes. |
static <A> AnyChromosome<A> |
of(Supplier<? extends A> supplier)
Create a new chromosome of type
A with the given parameters and
length one. |
static <A> AnyChromosome<A> |
of(Supplier<? extends A> supplier,
int length)
Create a new chromosome of type
A with the given parameters. |
static <A> AnyChromosome<A> |
of(Supplier<? extends A> supplier,
Predicate<? super A> validator)
Create a new chromosome of type
A with the given parameters and
length one. |
static <A> AnyChromosome<A> |
of(Supplier<? extends A> supplier,
Predicate<? super A> validator,
int length)
Create a new chromosome of type
A with the given parameters. |
static <A> AnyChromosome<A> |
of(Supplier<? extends A> supplier,
Predicate<? super A> alleleValidator,
Predicate<? super ISeq<? super A>> alleleSeqValidator,
int length)
Create a new chromosome of type
A with the given parameters. |
equals, getGene, hashCode, iterator, length, toSeq, toStringclone, finalize, getClass, notify, notifyAll, wait, wait, waitgetGene, streamforEach, spliteratorprotected AnyChromosome(ISeq<AnyGene<A>> genes, Supplier<? extends A> supplier, Predicate<? super A> alleleValidator, Predicate<? super ISeq<? super A>> alleleSeqValidator)
AnyChromosome from the given genes
array. An chromosome is valid if both, the alleleValidator and
the alleleSeqValidator return true.genes - the genes that form the chromosome.supplier - the allele-supplier which is used for creating new,
random allelesalleleValidator - the validator used for validating the created gene.
This predicate is used in the AnyGene.isValid() method.alleleSeqValidator - the validator used for validating the created
chromosome. This predicate is used in the
isValid() method.NullPointerException - if the given arguments is nullIllegalArgumentException - if the length of the gene array is
smaller than one.public boolean isValid()
VerifiableisValid in interface VerifiableisValid in class AbstractChromosome<AnyGene<A>>public Chromosome<AnyGene<A>> newInstance(ISeq<AnyGene<A>> genes)
ChromosomeChromosome of specific type
and the given genes.genes - the genes of the new chromosome. The given genes array is
not copied.Chromosome of the same type with the given genes.public Chromosome<AnyGene<A>> newInstance()
Factorypublic static <A> AnyChromosome<A> of(Supplier<? extends A> supplier, Predicate<? super A> alleleValidator, Predicate<? super ISeq<? super A>> alleleSeqValidator, int length)
A with the given parameters.A - the allele typesupplier - the allele-supplier which is used for creating new,
random allelesalleleValidator - the validator used for validating the created gene.
This predicate is used in the AnyGene.isValid() method.alleleSeqValidator - the validator used for validating the created
chromosome. This predicate is used in the
isValid() method.length - the length of the created chromosomeANullPointerException - if the given arguments is nullIllegalArgumentException - if chromosome length is smaller than one.public static <A> AnyChromosome<A> of(Supplier<? extends A> supplier, Predicate<? super A> validator, int length)
A with the given parameters.A - the allele typesupplier - the allele-supplier which is used for creating new,
random allelesvalidator - the validator used for validating the created gene. This
predicate is used in the AnyGene.isValid() method.length - the length of the created chromosomeANullPointerException - if the supplier or validator
is nullIllegalArgumentException - if chromosome length is smaller than one.public static <A> AnyChromosome<A> of(Supplier<? extends A> supplier, Predicate<? super A> validator)
A with the given parameters and
length one.A - the allele typesupplier - the allele-supplier which is used for creating new,
random allelesvalidator - the validator used for validating the created gene. This
predicate is used in the isValid() method.ANullPointerException - if the supplier or validator
is nullpublic static <A> AnyChromosome<A> of(Supplier<? extends A> supplier, int length)
A with the given parameters. The
validator predicate of the generated gene will always return
true.A - the allele typesupplier - the allele-supplier which is used for creating new,
random alleleslength - the length of the created chromosomeANullPointerException - if the supplier is nullIllegalArgumentException - if chromosome length is smaller than one.public static <A> AnyChromosome<A> of(Supplier<? extends A> supplier)
A with the given parameters and
length one. The validator predicate of the generated gene will
always return true.A - the allele typesupplier - the allele-supplier which is used for creating new,
random allelesANullPointerException - if the supplier is null© 2007-2016 Franz Wilhelmstötter (2016-04-24 10:25)