- All Implemented Interfaces:
Chromosome<EnumGene<T>>,BaseSeq<EnumGene<T>>,Factory<Chromosome<EnumGene<T>>>,Verifiable,Serializable,Iterable<EnumGene<T>>,RandomAccess
public final class PermutationChromosome<T>
extends AbstractChromosome<EnumGene<T>>
implements Serializable
This chromosome can be used to model permutations of a given (sub) set of
alleles.
Usable
final ISeq<String> alleles = ISeq.of("one", "two", "three", "four", "five");
// Create a new randomly permuted chromosome from the given alleles.
final PermutationChromosome<String> ch1 = PermutationChromosome.of(alleles);
System.out.println(ch1);
System.out.println(ch1.newInstance());
// Create a new randomly permuted chromosome from a subset of the given alleles.
final PermutationChromosome<String> ch2 = PermutationChromosome.of(alleles, 3);
System.out.println(ch2);
System.out.println(ch2.newInstance());
// Console output:
// > three|two|one|five|four
// > two|one|four|five|three
// > three|one|five
// > five|three|one
Alterer for this chromosome:
Implementation note 1:
The factory methods of the AbstractChromosome has been overridden so
that no invalid permutation will be created.
Implementation note 2:
This class uses an algorithm for choosing subsets which is based on a
FORTRAN77 version, originally implemented by Albert Nijenhuis, Herbert Wilf.
The actual Java implementation is based on the C++ version by John Burkardt.
Reference:
Albert Nijenhuis, Herbert Wilf,
Combinatorial Algorithms for Computers and Calculators,
Second Edition,
Academic Press, 1978,
ISBN: 0-12-519260-6,
LC: QA164.N54.
- Since:
- 1.0
- Version:
- 6.0
- See Also:
- Implementation Note:
- This class is immutable and thread-safe.
-
Field Summary
Fields inherited from class io.jenetics.AbstractChromosome
_genes, _valid -
Constructor Summary
ConstructorsConstructorDescriptionPermutationChromosome(ISeq<EnumGene<T>> genes) Create a newPermutationChromosomefrom the givengenes. -
Method Summary
Modifier and TypeMethodDescriptionbooleanisValid()Check if this chromosome represents still a valid permutation (or subset) of the given valid alleles.Create a new, random chromosome.newInstance(ISeq<EnumGene<T>> genes) A factory method which creates a newChromosomeof specific type and the givengenes.static <T> PermutationChromosome<T> Create a new, random chromosome with the given valid alleles.static <T> PermutationChromosome<T> Create a new, random chromosome with the given valid alleles and the desired length.static <T> PermutationChromosome<T> of(T... alleles) Create a new, random chromosome with the given valid alleles.static PermutationChromosome<Integer> ofInteger(int length) Create a integer permutation chromosome with the given length.static PermutationChromosome<Integer> ofInteger(int start, int end) Create an integer permutation chromosome with the given range.static PermutationChromosome<Integer> Create an integer permutation chromosome with the given range and lengthtoString()Return the sequence of the valid alleles of this chromosome.Methods inherited from class io.jenetics.AbstractChromosome
equals, get, hashCode, lengthMethods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface io.jenetics.util.BaseSeq
forEach, isEmpty, iterator, listIterator, nonEmpty, spliterator, streamMethods inherited from interface io.jenetics.Chromosome
as, gene
-
Constructor Details
-
PermutationChromosome
Create a newPermutationChromosomefrom the givengenes. If the givengenessequence contains duplicate entries, the createdPermutationChromosomewill be invalid (ch.isValid() == false).- Parameters:
genes- the enum genes the new chromosome consists of- Throws:
NullPointerException- if the givengenesare nullIllegalArgumentException- if the givengenessequence is empty
-
-
Method Details
-
validAlleles
Return the sequence of the valid alleles of this chromosome.- Returns:
- the sequence of valid alleles of this chromosome
-
isValid
Check if this chromosome represents still a valid permutation (or subset) of the given valid alleles.- Specified by:
isValidin interfaceChromosome<T>- Specified by:
isValidin interfaceVerifiable- Overrides:
isValidin classAbstractChromosome<EnumGene<T>>- Returns:
- true if this object is valid, false otherwise.
-
newInstance
Create a new, random chromosome.- Specified by:
newInstancein interfaceFactory<T>- Returns:
- a new instance of type T
-
newInstance
Description copied from interface:ChromosomeA factory method which creates a newChromosomeof specific type and the givengenes.- Specified by:
newInstancein interfaceChromosome<T>- Parameters:
genes- the genes of the new chromosome. The given genes array is not copied.- Returns:
- A new
Chromosomeof the same type with the given genes.
-
toString
- Overrides:
toStringin classAbstractChromosome<EnumGene<T>>
-
of
Create a new, random chromosome with the given valid alleles and the desired length.The following example shows how to create a
PermutationChromosomefor encoding a subset problem (of a fixedlength).final ISeq<String> basicSet = ISeq.of("a", "b", "c", "d", "e", "f"); // The chromosome has a length of 3 and will only contain values from the // given basic-set, with no duplicates. final PermutationChromosome<String> ch = PermutationChromosome.of(basicSet, 3);- Type Parameters:
T- the allele type- Parameters:
alleles- the base-set of the valid alleleslength- the length of the created chromosomes- Returns:
- a new chromosome with the given valid alleles and the desired length
- Throws:
IllegalArgumentException- ifalleles.size() < length,length <= 0oralleles.size()*lengthwill cause an integer overflow.NullPointerException- if one of the arguments isnull- Since:
- 3.4
-
of
Create a new, random chromosome with the given valid alleles.- Type Parameters:
T- the gene type of the chromosome- Parameters:
alleles- the valid alleles used for this permutation arrays.- Returns:
- a new chromosome with the given alleles
- Throws:
IllegalArgumentException- if the given allele sequence is empty.
-
of
Create a new, random chromosome with the given valid alleles.- Type Parameters:
T- the gene type of the chromosome- Parameters:
alleles- the valid alleles used for this permutation arrays.- Returns:
- a new chromosome with the given alleles
- Throws:
IllegalArgumentException- if the given allele array is empty.NullPointerException- if one of the alleles isnull- Since:
- 2.0
-
ofInteger
Create a integer permutation chromosome with the given length.- Parameters:
length- the chromosome length.- Returns:
- a integer permutation chromosome with the given length.
- Throws:
IllegalArgumentException- iflength <= 0.
-
ofInteger
Create an integer permutation chromosome with the given range.- Parameters:
start- the start of the integer range (inclusively) of the returned chromosome.end- the end of the integer range (exclusively) of the returned chromosome.- Returns:
- a integer permutation chromosome with the given integer range values.
- Throws:
IllegalArgumentException- ifstart >= endorstart <= 0- Since:
- 2.0
-
ofInteger
Create an integer permutation chromosome with the given range and length- Parameters:
range- the value rangelength- the chromosome length- Returns:
- a new integer permutation chromosome
- Throws:
NullPointerException- if the givenrangeisnullIllegalArgumentException- ifrange.getMax() - range.getMin() < length,length <= 0or(range.getMax() - range.getMin())*lengthwill cause an integer overflow.- Since:
- 3.4
-