Package org.moeaframework.core.variable
Class EncodingUtils
java.lang.Object
org.moeaframework.core.variable.EncodingUtils
Helper methods for working with various decision variable types and encodings. First, these methods perform any
necessary type checking and type conversion. Instead of writing:
double value = ((RealVariable)solution.getVariable(i)).getValue()the following simplified version is allowed:
double value = getReal(solution.getVariable(i));
Support for integer encodings is now supported using the newInt(int, int)
or
newBinaryInt(int, int)
. The former represents integers as floating-point values while the latter uses
binary strings. Both representations are accessed using getInt(Variable)
and
setInt(Variable, int)
methods.
This class also provides methods for converting between RealVariable
and BinaryVariable
in both
binary and gray code formats.
-
Method Summary
Modifier and TypeMethodDescriptionstatic BitSet
binaryToGray
(BitSet binary) Converts a binary string from binary code to gray code.static long
Converts the given binary string into its long value.static void
decode
(BinaryVariable binary, RealVariable real) Decodes the specified binary variable into its real value.static BitSet
encode
(long value) Converts the given long value into its binary string representation.static void
encode
(RealVariable real, BinaryVariable binary) Encodes the specified real variable into a binary variable.static boolean[]
Returns the value stored in a binary decision variable as a boolean array.static BitSet
Returns the value stored in a binary decision variable as aBitSet
.static boolean
getBoolean
(Variable variable) Returns the value stored in a boolean decision variable.static int[]
Returns the array of integer-valued decision variables stored in a solution.static int[]
Returns the array of integer-valued decision variables stored in a solution between the specified indices.static int
Returns the value stored in an integer-valued decision variable.static int[]
getOrderedSubset
(Variable variable) Returns the value stored in a subset decision variable with the items sorted.static int[]
getPermutation
(Variable variable) Returns the value stored in a permutation decision variable.static double[]
Returns the array of floating-point decision variables stored in a solution.static double[]
Returns the array of floating-point decision variables stored in a solution between the specified indices.static double
Returns the value stored in a floating-point decision variable.static int[]
Returns the value stored in a subset decision variable.static boolean[]
getSubsetAsBinary
(Variable variable) Returns the subset as a binary string, where 1 indicates the index is included in the set.static BitSet
getSubsetAsBitSet
(Variable variable) Returns the subset as a BitSet, where a set bit indicates the index is included in the set.static BitSet
grayToBinary
(BitSet gray) Converts a binary string from gray code to binary code.static BinaryVariable
newBinary
(int length) Returns a new binary decision variable with the specified number of bits.static BinaryIntegerVariable
newBinaryInt
(int lowerBound, int upperBound) Returns a new integer-valued decision variable bounded within the specified range.static BinaryVariable
Returns a new boolean decision variable.static RealVariable
newInt
(int lowerBound, int upperBound) Returns a new integer-valued decision variable bounded within the specified range.static Permutation
newPermutation
(int length) Returns a new permutation with the specified number of items.static RealVariable
newReal
(double lowerBound, double upperBound) Returns a new floating-point decision variable bounded within the specified range.static Subset
newSubset
(int k, int n) Returns a new fixed-size subset with the specified number of items.static Subset
newSubset
(int l, int u, int n) Returns a new variable-size subset with the specified number of items.static void
Sets the bits in a binary decision variable using the given boolean array.static void
Sets the bits in a binary decision variable using the givenBitSet
.static void
setBoolean
(Variable variable, boolean value) Sets the value of a boolean decision variable.static void
Sets the values of all integer-valued decision variables stored in the solution.static void
Sets the values of the integer-valued decision variables stored in a solution between the specified indices.static void
Sets the value of an integer-valued decision variable.static void
setPermutation
(Variable variable, int[] values) Sets the value of a permutation decision variable.static void
Sets the values of all floating-point decision variables stored in the solution.static void
Sets the values of the floating-point decision variables stored in a solution between the specified indices.static void
Sets the value of a floating-point decision variable.static void
Sets the value of a subset decision variable using a binary string representation.static void
Sets the value of a subset decision variable.static void
Sets the value of a subset decision variable using a BitSet representation.
-
Method Details
-
encode
Encodes the specified real variable into a binary variable. The number of bits used in the encoding isbinary.getNumberOfBits()
.- Parameters:
real
- the real variablebinary
- the binary variable to which the real value is encoded
-
decode
Decodes the specified binary variable into its real value.- Parameters:
binary
- the binary variablereal
- the real variable to which the value is decoded
-
encode
Converts the given long value into its binary string representation.- Parameters:
value
- the long value- Returns:
- the binary string representation
-
decode
Converts the given binary string into its long value.- Parameters:
binary
- the binary string- Returns:
- the long value
-
binaryToGray
Converts a binary string from binary code to gray code. The gray code ensures two adjacent values have binary representations differing in only one big (i.e., a Hamming distance of1
).- Parameters:
binary
- the binary code string- Returns:
- the gray code string
-
grayToBinary
Converts a binary string from gray code to binary code.- Parameters:
gray
- the gray code string- Returns:
- the binary code string
-
newReal
Returns a new floating-point decision variable bounded within the specified range.- Parameters:
lowerBound
- the lower bound of the floating-point valueupperBound
- the upper bound of the floating-point value- Returns:
- a new floating-point decision variable bounded within the specified range
-
newInt
Returns a new integer-valued decision variable bounded within the specified range. The integer value is encoded using aRealVariable
.- Parameters:
lowerBound
- the lower bound of the integer valueupperBound
- the upper bound of the integer value- Returns:
- a new integer-valued decision variable bounded within the specified range
-
newBinaryInt
Returns a new integer-valued decision variable bounded within the specified range. The integer value is encoded using aBinaryVariable
.- Parameters:
lowerBound
- the lower bound of the integer valueupperBound
- the upper bound of the integer value- Returns:
- a new integer-valued decision variable bounded within the specified range
-
newBoolean
Returns a new boolean decision variable.- Returns:
- a new boolean decision variable
-
newBinary
Returns a new binary decision variable with the specified number of bits.- Parameters:
length
- the number of bits in the binary decision variable- Returns:
- a new binary decision variable with the specified number of bits
-
newPermutation
Returns a new permutation with the specified number of items.- Parameters:
length
- the number of items in the permutation- Returns:
- a new permutation with the specified number of items
-
newSubset
Returns a new fixed-size subset with the specified number of items.- Parameters:
k
- the fixed size of the subsetn
- the total number of items in the set- Returns:
- a new subset with the specified number of items
-
newSubset
Returns a new variable-size subset with the specified number of items.- Parameters:
l
- the minimum number of members in the subsetu
- the maximum number of members in the subsetn
- the total number of items in the set- Returns:
- a new subset with the specified number of items
-
getReal
Returns the value stored in a floating-point decision variable.- Parameters:
variable
- the decision variable- Returns:
- the value stored in a floating-point decision variable
- Throws:
IllegalArgumentException
- if the decision variable is not of typeRealVariable
-
getInt
Returns the value stored in an integer-valued decision variable.- Parameters:
variable
- the decision variable- Returns:
- the value stored in an integer-valued decision variable
- Throws:
IllegalArgumentException
- if the decision variable is not convertible to an integer
-
getBitSet
Returns the value stored in a binary decision variable as aBitSet
.- Parameters:
variable
- the decision variable- Returns:
- the value stored in a binary decision variable as a
BitSet
- Throws:
IllegalArgumentException
- if the decision variable is not of typeBinaryVariable
-
getBinary
Returns the value stored in a binary decision variable as a boolean array.- Parameters:
variable
- the decision variable- Returns:
- the value stored in a binary decision variable as a boolean array
- Throws:
IllegalArgumentException
- if the decision variable is not of typeBinaryVariable
-
getBoolean
Returns the value stored in a boolean decision variable.- Parameters:
variable
- the decision variable- Returns:
- the value stored in a boolean decision variable
- Throws:
IllegalArgumentException
- if the decision variable is not of typeBinaryVariable
-
getPermutation
Returns the value stored in a permutation decision variable.- Parameters:
variable
- the decision variable- Returns:
- the value stored in a permutation decision variable
- Throws:
IllegalArgumentException
- if the decision variable is not of typePermutation
-
getSubset
Returns the value stored in a subset decision variable.- Parameters:
variable
- the decision variable- Returns:
- the value stored in a subset decision variable
- Throws:
IllegalArgumentException
- if the decision variable is not of typeSubset
-
getOrderedSubset
Returns the value stored in a subset decision variable with the items sorted.- Parameters:
variable
- the decision variable- Returns:
- the value stored in a subset decision variable with the items sorted
- Throws:
IllegalArgumentException
- if the decision variable is not of typeSubset
-
getSubsetAsBinary
Returns the subset as a binary string, where 1 indicates the index is included in the set.- Parameters:
variable
- the decision variable- Returns:
- a binary string representation of the subset
- Throws:
IllegalArgumentException
- if the decision variable is not of typeSubset
-
getSubsetAsBitSet
Returns the subset as a BitSet, where a set bit indicates the index is included in the set.- Parameters:
variable
- the decision variable- Returns:
- a BitSet representation of the subset
- Throws:
IllegalArgumentException
- if the decision variable is not of typeSubset
-
getReal
Returns the array of floating-point decision variables stored in a solution. The solution must contain only floating-point decision variables.- Parameters:
solution
- the solution- Returns:
- the array of floating-point decision variables stored in a solution
- Throws:
IllegalArgumentException
- if any decision variable contained in the solution is not of typeRealVariable
-
getReal
Returns the array of floating-point decision variables stored in a solution between the specified indices. The decision variables located between the start and end index must all be floating-point decision variables.- Parameters:
solution
- the solutionstartIndex
- the start index (inclusive)endIndex
- the end index (exclusive)- Returns:
- the array of floating-point decision variables stored in a solution between the specified indices
- Throws:
IllegalArgumentException
- if any decision variable contained in the solution between the start and end index is not of typeRealVariable
-
getInt
Returns the array of integer-valued decision variables stored in a solution. The solution must contain only integer-valued decision variables.- Parameters:
solution
- the solution- Returns:
- the array of integer-valued decision variables stored in a solution
- Throws:
IllegalArgumentException
- if any decision variable contained in the solution is not of typeRealVariable
-
getInt
Returns the array of integer-valued decision variables stored in a solution between the specified indices. The decision variables located between the start and end index must all be integer-valued decision variables.- Parameters:
solution
- the solutionstartIndex
- the start index (inclusive)endIndex
- the end index (exclusive)- Returns:
- the array of integer-valued decision variables stored in a solution between the specified indices
- Throws:
IllegalArgumentException
- if any decision variable contained in the solution between the start and end index is not of typeRealVariable
-
setReal
Sets the value of a floating-point decision variable.- Parameters:
variable
- the decision variablevalue
- the value to assign the floating-point decision variable- Throws:
IllegalArgumentException
- if the decision variable is not of typeRealVariable
IllegalArgumentException
- if the value is out of bounds (value < getLowerBound()) || (value > getUpperBound()
)
-
setReal
Sets the values of all floating-point decision variables stored in the solution. The solution must contain only floating-point decision variables.- Parameters:
solution
- the solutionvalues
- the array of floating-point values to assign the solution- Throws:
IllegalArgumentException
- if any decision variable contained in the solution is not of typeRealVariable
IllegalArgumentException
- if any of the values are out of bounds (value < getLowerBound()) || (value > getUpperBound()
)
-
setReal
Sets the values of the floating-point decision variables stored in a solution between the specified indices. The decision variables located between the start and end index must all be floating-point decision variables.- Parameters:
solution
- the solutionstartIndex
- the start index (inclusive)endIndex
- the end index (exclusive)values
- the array of floating-point values to assign the decision variables- Throws:
IllegalArgumentException
- if any decision variable contained in the solution between the start and end index is not of typeRealVariable
IllegalArgumentException
- if an invalid number of values are providedIllegalArgumentException
- if any of the values are out of bounds (value < getLowerBound()) || (value > getUpperBound()
)
-
setInt
Sets the value of an integer-valued decision variable.- Parameters:
variable
- the decision variablevalue
- the value to assign the integer-valued decision variable- Throws:
IllegalArgumentException
- if the decision variable is not convertible to an integerIllegalArgumentException
- if the value is out of bounds (value < getLowerBound()) || (value > getUpperBound()
)
-
setInt
Sets the values of all integer-valued decision variables stored in the solution. The solution must contain only integer-valued decision variables.- Parameters:
solution
- the solutionvalues
- the array of integer values to assign the solution- Throws:
IllegalArgumentException
- if the decision variable is not convertible to an integerIllegalArgumentException
- if any of the values are out of bounds (value < getLowerBound()) || (value > getUpperBound()
)
-
setInt
Sets the values of the integer-valued decision variables stored in a solution between the specified indices. The decision variables located between the start and end index must all be integer-valued decision variables.- Parameters:
solution
- the solutionstartIndex
- the start index (inclusive)endIndex
- the end index (exclusive)values
- the array of floating-point values to assign the decision variables- Throws:
IllegalArgumentException
- if the decision variables are not convertible to an integerIllegalArgumentException
- if an invalid number of values are providedIllegalArgumentException
- if any of the values are out of bounds (value < getLowerBound()) || (value > getUpperBound()
)
-
setBitSet
Sets the bits in a binary decision variable using the givenBitSet
.- Parameters:
variable
- the decision variablebitSet
- the bits to set in the binary decision variable- Throws:
IllegalArgumentException
- if the decision variable is not of typeBinaryVariable
-
setBinary
Sets the bits in a binary decision variable using the given boolean array.- Parameters:
variable
- the decision variablevalues
- the bits to set in the binary decision variable- Throws:
IllegalArgumentException
- if the decision variable is not of typeBinaryVariable
IllegalArgumentException
- if an invalid number of values are provided
-
setBoolean
Sets the value of a boolean decision variable.- Parameters:
variable
- the decision variablevalue
- the value to assign the boolean decision variable- Throws:
IllegalArgumentException
- if the decision variable is not of typeBinaryVariable
IllegalArgumentException
- if the number of bits in the binary variable is not1
-
setPermutation
Sets the value of a permutation decision variable.- Parameters:
variable
- the decision variablevalues
- the permutation to assign the permutation decision variable- Throws:
IllegalArgumentException
- if the decision variable is not of typePermutation
IllegalArgumentException
- ifvalues
is not a valid permutation
-
setSubset
Sets the value of a subset decision variable.- Parameters:
variable
- the decision variablevalues
- the subset to assign the subset decision variable- Throws:
IllegalArgumentException
- if the decision variable is not of typeSubset
IllegalArgumentException
- ifvalues
is not a valid subset
-
setSubset
Sets the value of a subset decision variable using a binary string representation.- Parameters:
variable
- the decision variablevalues
- the binary string representation of a subset- Throws:
IllegalArgumentException
- if the binary string representation is not a valid subset
-
setSubset
Sets the value of a subset decision variable using a BitSet representation.- Parameters:
variable
- the decision variablebitSet
- the BitSet representation of a subset- Throws:
IllegalArgumentException
- if the BitSet representation is not a valid subset
-