Package io.jenetics
Class AnyChromosome<A>
- java.lang.Object
-
- io.jenetics.AbstractChromosome<G>
-
- io.jenetics.AnyChromosome<A>
-
- All Implemented Interfaces:
Chromosome<AnyGene<A>>
,BaseSeq<AnyGene<A>>
,Factory<Chromosome<AnyGene<A>>>
,Verifiable
,Serializable
,Iterable<AnyGene<A>>
,RandomAccess
public class AnyChromosome<A> extends AbstractChromosome<G>
Chromosome
implementation, which allows to create genes without explicit implementing theChromosome
interface.The full example above shows how thepublic 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.gene().allele() ); // 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.- Since:
- 3.3
- Version:
- 5.2
- Author:
- Franz Wilhelmstötter
- See Also:
AnyGene
, Serialized Form- Implementation Requirements:
- This class is immutable and thread-safe.
-
-
Field Summary
-
Fields inherited from class io.jenetics.AbstractChromosome
_genes
-
-
Constructor Summary
Constructors Modifier Constructor Description protected
AnyChromosome(ISeq<AnyGene<A>> genes, Supplier<? extends A> supplier, Predicate<? super A> alleleValidator, Predicate<? super ISeq<A>> alleleSeqValidator, IntRange lengthRange)
Create a newAnyChromosome
from the givengenes
array.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
equals(Object obj)
int
hashCode()
boolean
isValid()
Check if this object is valid.IntRange
lengthRange()
Return the allowed length range of the chromosome.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 newChromosome
of specific type and the givengenes
.static <A> AnyChromosome<A>
of(Supplier<? extends A> supplier)
Create a new chromosome of typeA
with the given parameters and length one.static <A> AnyChromosome<A>
of(Supplier<? extends A> supplier, int length)
Create a new chromosome of typeA
with the given parameters.static <A> AnyChromosome<A>
of(Supplier<? extends A> supplier, IntRange lengthRange)
Create a new chromosome of typeA
with the given parameters.static <A> AnyChromosome<A>
of(Supplier<? extends A> supplier, Predicate<? super A> validator)
Create a new chromosome of typeA
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 typeA
with the given parameters.static <A> AnyChromosome<A>
of(Supplier<? extends A> supplier, Predicate<? super A> validator, IntRange lengthRange)
Create a new chromosome of typeA
with the given parameters.static <A> AnyChromosome<A>
of(Supplier<? extends A> supplier, Predicate<? super A> alleleValidator, Predicate<? super ISeq<A>> alleleSeqValidator, int length)
Create a new chromosome of typeA
with the given parameters.static <A> AnyChromosome<A>
of(Supplier<? extends A> supplier, Predicate<? super A> alleleValidator, Predicate<? super ISeq<A>> alleleSeqValidator, IntRange lengthRange)
Create a new chromosome of typeA
with the given parameters.-
Methods inherited from class io.jenetics.AbstractChromosome
get, length, toString
-
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface io.jenetics.util.BaseSeq
isEmpty, iterator, listIterator, nonEmpty, spliterator, stream
-
Methods inherited from interface io.jenetics.Chromosome
as, gene
-
-
-
-
Constructor Detail
-
AnyChromosome
protected AnyChromosome(ISeq<AnyGene<A>> genes, Supplier<? extends A> supplier, Predicate<? super A> alleleValidator, Predicate<? super ISeq<A>> alleleSeqValidator, IntRange lengthRange)
Create a newAnyChromosome
from the givengenes
array. An chromosome is valid if both, thealleleValidator
and thealleleSeqValidator
returntrue
.- Parameters:
genes
- the genes that form the chromosome.lengthRange
- the allowed length range of the chromosomesupplier
- the allele-supplier which is used for creating new, random allelesalleleValidator
- the validator used for validating the created gene. This predicate is used in theAnyGene.isValid()
method.alleleSeqValidator
- the validator used for validating the created chromosome. This predicate is used in theisValid()
method.- Throws:
NullPointerException
- if the given arguments isnull
IllegalArgumentException
- if the length of the gene sequence is empty, doesn't match with the allowed length range, the minimum or maximum of the range is smaller or equal zero or the given range size is zero.
-
-
Method Detail
-
isValid
public boolean isValid()
Description copied from interface:Verifiable
Check if this object is valid.- Specified by:
isValid
in interfaceChromosome<A>
- Specified by:
isValid
in interfaceVerifiable
- Overrides:
isValid
in classAbstractChromosome<AnyGene<A>>
- Returns:
- true if this object is valid, false otherwise.
-
newInstance
public Chromosome<AnyGene<A>> newInstance(ISeq<AnyGene<A>> genes)
Description copied from interface:Chromosome
A factory method which creates a newChromosome
of specific type and the givengenes
.- Parameters:
genes
- the genes of the new chromosome. The given genes array is not copied.- Returns:
- A new
Chromosome
of the same type with the given genes.
-
newInstance
public Chromosome<AnyGene<A>> newInstance()
Description copied from interface:Factory
Create a new instance of type T.- Returns:
- a new instance of type T
-
of
public static <A> AnyChromosome<A> of(Supplier<? extends A> supplier, Predicate<? super A> alleleValidator, Predicate<? super ISeq<A>> alleleSeqValidator, IntRange lengthRange)
Create a new chromosome of typeA
with the given parameters.- Type Parameters:
A
- the allele type- Parameters:
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 theAnyGene.isValid()
method.alleleSeqValidator
- the validator used for validating the created chromosome. This predicate is used in theisValid()
method.lengthRange
- the allowed length range of the chromosome- Returns:
- a new chromosome of allele type
A
- Throws:
NullPointerException
- if the given arguments isnull
IllegalArgumentException
- if chromosome length is smaller than one.- Since:
- 4.0
-
of
public static <A> AnyChromosome<A> of(Supplier<? extends A> supplier, Predicate<? super A> alleleValidator, Predicate<? super ISeq<A>> alleleSeqValidator, int length)
Create a new chromosome of typeA
with the given parameters.- Type Parameters:
A
- the allele type- Parameters:
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 theAnyGene.isValid()
method.alleleSeqValidator
- the validator used for validating the created chromosome. This predicate is used in theisValid()
method.length
- the length of the chromosome- Returns:
- a new chromosome of allele type
A
- Throws:
NullPointerException
- if the given arguments isnull
IllegalArgumentException
- if chromosome length is smaller than one.
-
of
public static <A> AnyChromosome<A> of(Supplier<? extends A> supplier, Predicate<? super A> validator, IntRange lengthRange)
Create a new chromosome of typeA
with the given parameters.- Type Parameters:
A
- the allele type- Parameters:
supplier
- the allele-supplier which is used for creating new, random allelesvalidator
- the validator used for validating the created gene. This predicate is used in theAnyGene.isValid()
method.lengthRange
- the allowed length range of the chromosome- Returns:
- a new chromosome of allele type
A
- Throws:
NullPointerException
- if thesupplier
orvalidator
isnull
IllegalArgumentException
- if chromosome length is smaller than one.- Since:
- 4.0
-
of
public static <A> AnyChromosome<A> of(Supplier<? extends A> supplier, Predicate<? super A> validator, int length)
Create a new chromosome of typeA
with the given parameters.- Type Parameters:
A
- the allele type- Parameters:
supplier
- the allele-supplier which is used for creating new, random allelesvalidator
- the validator used for validating the created gene. This predicate is used in theAnyGene.isValid()
method.length
- the length of the chromosome- Returns:
- a new chromosome of allele type
A
- Throws:
NullPointerException
- if thesupplier
orvalidator
isnull
IllegalArgumentException
- if chromosome length is smaller than one.
-
of
public static <A> AnyChromosome<A> of(Supplier<? extends A> supplier, Predicate<? super A> validator)
Create a new chromosome of typeA
with the given parameters and length one.- Type Parameters:
A
- the allele type- Parameters:
supplier
- the allele-supplier which is used for creating new, random allelesvalidator
- the validator used for validating the created gene. This predicate is used in theisValid()
method.- Returns:
- a new chromosome of allele type
A
- Throws:
NullPointerException
- if thesupplier
orvalidator
isnull
-
of
public static <A> AnyChromosome<A> of(Supplier<? extends A> supplier, IntRange lengthRange)
Create a new chromosome of typeA
with the given parameters. Thevalidator
predicate of the generated gene will always returntrue
.- Type Parameters:
A
- the allele type- Parameters:
supplier
- the allele-supplier which is used for creating new, random alleleslengthRange
- the allowed length range of the chromosome- Returns:
- a new chromosome of allele type
A
- Throws:
NullPointerException
- if thesupplier
isnull
IllegalArgumentException
- if chromosome length is smaller than one.- Since:
- 4.0
-
of
public static <A> AnyChromosome<A> of(Supplier<? extends A> supplier, int length)
Create a new chromosome of typeA
with the given parameters. Thevalidator
predicate of the generated gene will always returntrue
.- Type Parameters:
A
- the allele type- Parameters:
supplier
- the allele-supplier which is used for creating new, random alleleslength
- the length of the created chromosome- Returns:
- a new chromosome of allele type
A
- Throws:
NullPointerException
- if thesupplier
isnull
IllegalArgumentException
- if chromosome length is smaller than one.
-
of
public static <A> AnyChromosome<A> of(Supplier<? extends A> supplier)
Create a new chromosome of typeA
with the given parameters and length one. Thevalidator
predicate of the generated gene will always returntrue
.- Type Parameters:
A
- the allele type- Parameters:
supplier
- the allele-supplier which is used for creating new, random alleles- Returns:
- a new chromosome of allele type
A
- Throws:
NullPointerException
- if thesupplier
isnull
-
lengthRange
public IntRange lengthRange()
Return the allowed length range of the chromosome. The minimum value of the range is included and the maximum value is excluded.- Returns:
- the allowed length range of the chromosome
-
hashCode
public int hashCode()
- Overrides:
hashCode
in classAbstractChromosome<G extends Gene<?,G>>
-
equals
public boolean equals(Object obj)
- Overrides:
equals
in classAbstractChromosome<G extends Gene<?,G>>
-
-