Top Description Fields Constructors Methods
org.apache.commons.math3.genetics

public abstract Class RandomKey<T>

extends AbstractListChromosome<Double>
implements PermutationChromosome<T>
Class Inheritance
All Implemented Interfaces
org.apache.commons.math3.genetics.PermutationChromosome
Type Parameters
<T>
type of the permuted objects
Imports
java.util.ArrayList, .Arrays, .Collections, .Comparator, .List, org.apache.commons.math3.exception.DimensionMismatchException, .MathIllegalArgumentException, org.apache.commons.math3.exception.util.LocalizedFormats

Random Key chromosome is used for permutation representation. It is a vector of a fixed length of real numbers in [0,1] interval. The index of the i-th smallest value in the vector represents an i-th member of the permutation.

For example, the random key [0.2, 0.3, 0.8, 0.1] corresponds to the permutation of indices (3,0,1,2). If the original (unpermuted) sequence would be (a,b,c,d), this would mean the sequence (d,a,b,c).

With this representation, common operators like n-point crossover can be used, because any such chromosome represents a valid permutation.

Since the chromosome (and thus its arrayRepresentation) is immutable, the array representation is sorted only once in the constructor.

For details, see:

Since
2.0

Field Summary

Modifier and TypeField and Description
private final List<Integer>
baseSeqPermutation

Base sequence [0,1,...,n-1], permuted according to the representation (unmodifiable).

private final List<Double>
sortedRepresentation

Cache of sorted representation (unmodifiable).

Constructor Summary

AccessConstructor and Description
public
RandomKey(final List<Double>
list of [0,1] values representing the permutation
representation
)

Constructor.

public
RandomKey(final Double[]
array of [0,1] values representing the permutation
representation
)

Constructor.

Method Summary

Modifier and TypeMethod and Description
private static List<Integer>

Returns:

list of integers from 0 to l-1
baseSequence
(final int
length of list to generate
l
)

Helper for constructor.

protected void
checkValidity(final List<Double>
representation of the chromosome
chromosomeRepresentation
)

Implements abstract org.apache.commons.math3.genetics.AbstractListChromosome.checkValidity.

Asserts that representation can represent a valid chromosome.

public static <
type of the data
S
>
List<Double>

Returns:

list representation of the permutation corresponding to the parameters
comparatorPermutation
(final List<S>
list of data determining the order
data
,
final Comparator<S>
how the data will be compared
comparator
)

Generates a representation of a permutation corresponding to the data sorted by comparator.

public List<T>
decode(final List<T>
the unpermuted (original) sequence of objects
sequence
)

Implements org.apache.commons.math3.genetics.PermutationChromosome.decode.

Permutes the sequence of objects of type T according to the permutation this chromosome represents.

private static <
generic type of the sequence values
S
>
List<S>

Returns:

list with the sequence values permuted according to the representation
decodeGeneric
(final List<S>
the unpermuted sequence
sequence
,
List<Double>
representation of the permutation ([0,1] vector)
representation
,
final List<Double>
sorted representation
sortedRepr
)

Decodes a permutation represented by representation and returns a (generic) list with the permuted values.

public static final List<Double>

Returns:

representation of an identity permutation
identityPermutation
(final int
length of the permutation
l
)

Generates a representation corresponding to an identity permutation of length l which can be passed to the RandomKey constructor.

public static <
type of the data
S
>
List<Double>

Returns:

representation of a permutation corresponding to the permutation originalData -> permutedData
inducedPermutation
(final List<S>
the original, unpermuted data
originalData
,
final List<S>
the data, somehow permuted
permutedData
)

Generates a representation of a permutation corresponding to a permutation which yields permutedData when applied to originalData.

protected boolean

Returns:

true iff chromosomes encode the same permutation
isSame
(final Chromosome
chromosome to compare
another
)

Overrides org.apache.commons.math3.genetics.Chromosome.isSame.

Returns true iff another is a RandomKey and encodes the same permutation.

public static final List<Double>

Returns:

representation of a random permutation
randomPermutation
(final int
length of the permutation
l
)

Generates a representation corresponding to a random permutation of length l which can be passed to the RandomKey constructor.

public String
toString()

Overrides org.apache.commons.math3.genetics.AbstractListChromosome.toString.

Returns a string representation of the object.

Inherited from org.apache.commons.math3.genetics.AbstractListChromosome:
getLengthgetRepresentationnewFixedLengthChromosome

Field Detail

baseSeqPermutationback to summary
private final List<Integer> baseSeqPermutation

Base sequence [0,1,...,n-1], permuted according to the representation (unmodifiable).

sortedRepresentationback to summary
private final List<Double> sortedRepresentation

Cache of sorted representation (unmodifiable).

Constructor Detail

RandomKeyback to summary
public RandomKey(final List<Double> representation) throws InvalidRepresentationException

Constructor.

Parameters
representation:List<Double>

list of [0,1] values representing the permutation

Exceptions
InvalidRepresentationException:
iff the representation can not represent a valid chromosome
RandomKeyback to summary
public RandomKey(final Double[] representation) throws InvalidRepresentationException

Constructor.

Parameters
representation:Double[]

array of [0,1] values representing the permutation

Exceptions
InvalidRepresentationException:
iff the representation can not represent a valid chromosome

Method Detail

baseSequenceback to summary
private static List<Integer> baseSequence(final int l)

Helper for constructor. Generates a list of natural numbers (0,1,...,l-1).

Parameters
l:int

length of list to generate

Returns:List<Integer>

list of integers from 0 to l-1

checkValidityback to summary
protected void checkValidity(final List<Double> chromosomeRepresentation) throws InvalidRepresentationException

Implements abstract org.apache.commons.math3.genetics.AbstractListChromosome.checkValidity.

Doc from org.apache.commons.math3.genetics.AbstractListChromosome.checkValidity.

Asserts that representation can represent a valid chromosome.

Parameters
chromosomeRepresentation:List<Double>

representation of the chromosome

Annotations
@Override
Exceptions
InvalidRepresentationException:
iff the representation can not represent a valid chromosome
comparatorPermutationback to summary
public static <S> List<Double> comparatorPermutation(final List<S> data, final Comparator<S> comparator)

Generates a representation of a permutation corresponding to the data sorted by comparator. The data is not modified during the process. This is useful if you want to inject some permutations to the initial population.

Parameters
<S>
type of the data
data:List<S>

list of data determining the order

comparator:Comparator<S>

how the data will be compared

Returns:List<Double>

list representation of the permutation corresponding to the parameters

decodeback to summary
public List<T> decode(final List<T> sequence)

Implements org.apache.commons.math3.genetics.PermutationChromosome.decode.

Doc from org.apache.commons.math3.genetics.PermutationChromosome.decode.

Permutes the sequence of objects of type T according to the permutation this chromosome represents. For example, if this chromosome represents a permutation (3,0,1,2), and the unpermuted sequence is (a,b,c,d), this yields (d,a,b,c).

Parameters
sequence:List<T>

the unpermuted (original) sequence of objects

Returns:List<T>

permutation of sequence represented by this permutation

decodeGenericback to summary
private static <S> List<S> decodeGeneric(final List<S> sequence, List<Double> representation, final List<Double> sortedRepr) throws DimensionMismatchException

Decodes a permutation represented by representation and returns a (generic) list with the permuted values.

Parameters
<S>
generic type of the sequence values
sequence:List<S>

the unpermuted sequence

representation:List<Double>

representation of the permutation ([0,1] vector)

sortedRepr:List<Double>

sorted representation

Returns:List<S>

list with the sequence values permuted according to the representation

Exceptions
DimensionMismatchException:
iff the length of the sequence, representation or sortedRepr lists are not equal
identityPermutationback to summary
public static final List<Double> identityPermutation(final int l)

Generates a representation corresponding to an identity permutation of length l which can be passed to the RandomKey constructor.

Parameters
l:int

length of the permutation

Returns:List<Double>

representation of an identity permutation

inducedPermutationback to summary
public static <S> List<Double> inducedPermutation(final List<S> originalData, final List<S> permutedData) throws DimensionMismatchException, MathIllegalArgumentException

Generates a representation of a permutation corresponding to a permutation which yields permutedData when applied to originalData. This method can be viewed as an inverse to decode(List).

Parameters
<S>
type of the data
originalData:List<S>

the original, unpermuted data

permutedData:List<S>

the data, somehow permuted

Returns:List<Double>

representation of a permutation corresponding to the permutation originalData -> permutedData

Exceptions
DimensionMismatchException:
iff the length of originalData and permutedData lists are not equal
MathIllegalArgumentException:
iff the permutedData and originalData lists contain different data
isSameback to summary
protected boolean isSame(final Chromosome another)

Overrides org.apache.commons.math3.genetics.Chromosome.isSame.

Returns true iff another is a RandomKey and encodes the same permutation.

Parameters
another:Chromosome

chromosome to compare

Returns:boolean

true iff chromosomes encode the same permutation

Annotations
@Override
randomPermutationback to summary
public static final List<Double> randomPermutation(final int l)

Generates a representation corresponding to a random permutation of length l which can be passed to the RandomKey constructor.

Parameters
l:int

length of the permutation

Returns:List<Double>

representation of a random permutation

toStringback to summary
public String toString()

Overrides org.apache.commons.math3.genetics.AbstractListChromosome.toString.

Doc from org.apache.commons.math3.genetics.AbstractListChromosome.toString.

Returns a string representation of the object. Satisfying this method's contract implies a non-null result must be returned.

Returns:String

Doc from java.lang.Object.toString.

a string representation of the object

Annotations
@Override