Package org.moeaframework.core.variable
Class Permutation
java.lang.Object
org.moeaframework.core.variable.Permutation
- All Implemented Interfaces:
Serializable
,Variable
Decision variable for permutations.
- See Also:
-
Constructor Summary
ConstructorDescriptionPermutation
(int size) Constructs a permutation variable with the specified number of elements.Permutation
(int[] permutation) Constructs a permutation variable using the specified permutation array. -
Method Summary
Modifier and TypeMethodDescriptioncopy()
Returns an independent copy of this decision variable.void
Parses and loads the value of this variable from a string.encode()
Encodes the value of this variable as a string.boolean
void
fromArray
(int[] permutation) Sets the permutation array.int
get
(int index) Returns the value of the permutation at the specified index.int
hashCode()
void
insert
(int i, int j) Removes thei
-th element and inserts it at thej
-th position.static boolean
isPermutation
(int[] permutation) Returnstrue
if the specified permutation is valid;false
otherwise.void
Randomly assign the value of this variable.int
size()
Returns the number of elements in this permutation.void
swap
(int i, int j) Swaps thei
-th andj
-th elements in this permutation.int[]
toArray()
Returns a copy of the permutation array.toString()
Returns a human-readable representation of this value.protected static void
validatePermutation
(int[] permutation) Validate the given array is a permutation.
-
Constructor Details
-
Permutation
public Permutation(int size) Constructs a permutation variable with the specified number of elements.- Parameters:
size
- the number of elements in the permutation
-
Permutation
public Permutation(int[] permutation) Constructs a permutation variable using the specified permutation array.- Parameters:
permutation
- the permutation array- Throws:
IllegalArgumentException
- if the permutation array is not a valid permutation
-
-
Method Details
-
copy
Description copied from interface:Variable
Returns an independent copy of this decision variable. It is required thatx.copy()
is completely independent fromx
. This means any method invoked onx.copy()
in no way alters the state ofx
and vice versa. It is typically the case thatx.copy().getClass() == x.getClass()
andx.copy().equals(x)
. -
size
public int size()Returns the number of elements in this permutation.- Returns:
- the number of elements in this permutation
-
get
public int get(int index) Returns the value of the permutation at the specified index.- Parameters:
index
- the index of the permutation value to be returned- Returns:
- the permutation element at the specified index
- Throws:
ArrayIndexOutOfBoundsException
- if the index is out of range[0, size()-1]
-
swap
public void swap(int i, int j) Swaps thei
-th andj
-th elements in this permutation.- Parameters:
i
- the first indexj
- the second index- Throws:
ArrayIndexOutOfBoundsException
- ifi
orj
is out or range @{code [0, size()-1]}
-
insert
public void insert(int i, int j) Removes thei
-th element and inserts it at thej
-th position.- Parameters:
i
- the first indexj
- the second index- Throws:
ArrayIndexOutOfBoundsException
- ifi
orj
is out or range @{code [0, size()-1]}
-
toArray
public int[] toArray()Returns a copy of the permutation array.- Returns:
- a copy of the permutation array
-
fromArray
public void fromArray(int[] permutation) Sets the permutation array.- Parameters:
permutation
- the permutation array- Throws:
IllegalArgumentException
- if the permutation array is not a valid permutation
-
validatePermutation
protected static void validatePermutation(int[] permutation) Validate the given array is a permutation.- Parameters:
permutation
- the permutation array- Throws:
IllegalArgumentException
- if the permutation array is not a valid permutation
-
isPermutation
public static boolean isPermutation(int[] permutation) Returnstrue
if the specified permutation is valid;false
otherwise.- Parameters:
permutation
- the permutation array- Returns:
true
if the specified permutation is valid;false
otherwise
-
hashCode
public int hashCode() -
equals
-
randomize
public void randomize()Description copied from interface:Variable
Randomly assign the value of this variable. In general, the randomization should follow a uniform distribution. -
toString
Description copied from interface:Variable
Returns a human-readable representation of this value. -
encode
Description copied from interface:Variable
Encodes the value of this variable as a string. This should reflect the internal representation of the value and not necessarily the actual value. For example, a binary-encoded integer should display the bits and not the integer value. Implementations should make an effort to display the value in a meaningful format, but that is not required. Instead, useVariable.toString()
if a human-readable format is required. This method along withVariable.decode(String)
are used primarily for storing values in text files. To make parsing easier, the resulting string must: 1. Only contain ASCII characters 2. Contain no whitespace (no spaces, tabs, newlines, etc.) -
decode
Description copied from interface:Variable
Parses and loads the value of this variable from a string. This must be able to process any string produced byVariable.encode()
.
-