public final class PermutationChromosome<T> extends AbstractChromosome<EnumGene<T>> implements Serializable
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.
EnumGene
,
PartiallyMatchedCrossover
,
SwapMutator
,
Serialized Form_genes, _valid
Constructor and Description |
---|
PermutationChromosome(ISeq<EnumGene<T>> genes)
Create a new
PermutationChromosome from the given genes . |
Modifier and Type | Method and Description |
---|---|
ISeq<T> |
getValidAlleles()
Return the sequence of valid alleles of this chromosome.
|
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 new
Chromosome of specific type
and the given genes . |
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 length
|
String |
toString() |
equals, getGene, hashCode, iterator, length, toSeq
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
as, getGene, stream
forEach, spliterator
public PermutationChromosome(ISeq<EnumGene<T>> genes)
PermutationChromosome
from the given genes
.
If the given genes
sequence contains duplicate entries, the
created PermutationChromosome
will be invalid
(ch.isValid() == false
).genes
- the enum genes the new chromosome consists ofNullPointerException
- if the given genes
are nullIllegalArgumentException
- if the given genes
sequence is
emptypublic ISeq<T> getValidAlleles()
public boolean isValid()
isValid
in interface Verifiable
isValid
in class AbstractChromosome<EnumGene<T>>
public PermutationChromosome<T> newInstance()
newInstance
in interface Factory<Chromosome<EnumGene<T>>>
public PermutationChromosome<T> newInstance(ISeq<EnumGene<T>> genes)
Chromosome
Chromosome
of specific type
and the given genes
.newInstance
in interface Chromosome<EnumGene<T>>
genes
- the genes of the new chromosome. The given genes array is
not copied.Chromosome
of the same type with the given genes.public static <T> PermutationChromosome<T> of(ISeq<? extends T> alleles, int length)
The following example shows how to create a PermutationChromosome
for encoding a sub-set problem (of a fixed length
).
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);
T
- the allele typealleles
- the base-set of the valid alleleslength
- the length of the created chromosomesIllegalArgumentException
- if alleles.size() < length
,
length <= 0
or alleles.size()*length
will
cause an integer overflow.NullPointerException
- if one of the arguments is null
public static <T> PermutationChromosome<T> of(ISeq<? extends T> alleles)
T
- the gene type of the chromosomealleles
- the valid alleles used for this permutation arrays.IllegalArgumentException
- if the given allele sequence is empty.@SafeVarargs public static <T> PermutationChromosome<T> of(T... alleles)
T
- the gene type of the chromosomealleles
- the valid alleles used for this permutation arrays.IllegalArgumentException
- if the given allele array is empty.NullPointerException
- if one of the alleles is null
public static PermutationChromosome<Integer> ofInteger(int length)
length
- the chromosome length.IllegalArgumentException
- if length <= 0
.public static PermutationChromosome<Integer> ofInteger(int start, int end)
start
- the start of the integer range (inclusively) of the returned
chromosome.end
- the end of the integer range (exclusively) of the returned
chromosome.IllegalArgumentException
- if start >= end
or
start <= 0
public static PermutationChromosome<Integer> ofInteger(IntRange range, int length)
range
- the value rangelength
- the chromosome lengthNullPointerException
- if the given range
is null
IllegalArgumentException
- if
range.getMax() - range.getMin() < length
,
length <= 0
or
(range.getMax() - range.getMin())*length
will cause an
integer overflow.© 2007-2019 Franz Wilhelmstötter (2019-02-19 21:44)