Package io.jenetics
Class PermutationChromosome<T>
- java.lang.Object
-
- io.jenetics.AbstractChromosome<EnumGene<T>>
-
- io.jenetics.PermutationChromosome<T>
-
- 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.Usablefinal 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:
EnumGene
,PartiallyMatchedCrossover
,SwapMutator
, Serialized Form- Implementation Note:
- This class is immutable and thread-safe.
-
-
Field Summary
-
Fields inherited from class io.jenetics.AbstractChromosome
_genes, _valid
-
-
Constructor Summary
Constructors Constructor Description PermutationChromosome(ISeq<EnumGene<T>> genes)
Create a newPermutationChromosome
from the givengenes
.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
isValid()
Check if this chromosome represents still a valid permutation (or subset) of the given valid alleles.PermutationChromosome<T>
newInstance()
Create a new, random chromosome.PermutationChromosome<T>
newInstance(ISeq<EnumGene<T>> genes)
A factory method which creates a newChromosome
of specific type and the givengenes
.static <T> PermutationChromosome<T>
of(ISeq<? extends T> alleles)
Create a new, random chromosome with the given valid alleles.static <T> PermutationChromosome<T>
of(ISeq<? extends T> alleles, int length)
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>
ofInteger(IntRange range, int length)
Create an integer permutation chromosome with the given range and lengthString
toString()
ISeq<T>
validAlleles()
Return the sequence of valid alleles of this chromosome.-
Methods inherited from class io.jenetics.AbstractChromosome
equals, get, hashCode, length
-
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
-
PermutationChromosome
public PermutationChromosome(ISeq<EnumGene<T>> genes)
Create a newPermutationChromosome
from the givengenes
. If the givengenes
sequence contains duplicate entries, the createdPermutationChromosome
will be invalid (ch.isValid() == false
).- Parameters:
genes
- the enum genes the new chromosome consists of- Throws:
NullPointerException
- if the givengenes
are nullIllegalArgumentException
- if the givengenes
sequence is empty
-
-
Method Detail
-
validAlleles
public ISeq<T> validAlleles()
Return the sequence of valid alleles of this chromosome.- Returns:
- the sequence of valid alleles of this chromosome
-
isValid
public boolean isValid()
Check if this chromosome represents still a valid permutation (or subset) of the given valid alleles.- Specified by:
isValid
in interfaceChromosome<T>
- Specified by:
isValid
in interfaceVerifiable
- Overrides:
isValid
in classAbstractChromosome<EnumGene<T>>
- Returns:
- true if this object is valid, false otherwise.
-
newInstance
public PermutationChromosome<T> newInstance()
Create a new, random chromosome.- Specified by:
newInstance
in interfaceFactory<T>
- Returns:
- a new instance of type T
-
newInstance
public PermutationChromosome<T> newInstance(ISeq<EnumGene<T>> genes)
Description copied from interface:Chromosome
A factory method which creates a newChromosome
of specific type and the givengenes
.- Specified by:
newInstance
in interfaceChromosome<T>
- 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.
-
of
public static <T> PermutationChromosome<T> of(ISeq<? extends T> alleles, int length)
Create a new, random chromosome with the given valid alleles and the desired length.The following example shows how to create a
PermutationChromosome
for encoding a sub-set 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 <= 0
oralleles.size()*length
will cause an integer overflow.NullPointerException
- if one of the arguments isnull
- Since:
- 3.4
-
of
public static <T> PermutationChromosome<T> of(ISeq<? extends T> alleles)
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
@SafeVarargs public static <T> PermutationChromosome<T> of(T... alleles)
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
public static PermutationChromosome<Integer> ofInteger(int length)
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
public static PermutationChromosome<Integer> ofInteger(int start, int end)
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 >= end
orstart <= 0
- Since:
- 2.0
-
ofInteger
public static PermutationChromosome<Integer> ofInteger(IntRange range, int length)
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 givenrange
isnull
IllegalArgumentException
- ifrange.getMax() - range.getMin() < length
,length <= 0
or(range.getMax() - range.getMin())*length
will cause an integer overflow.- Since:
- 3.4
-
-