MOEA Framework 2.3
API Specification

org.moeaframework.core.variable
Class EncodingUtils

java.lang.Object
  extended by org.moeaframework.core.variable.EncodingUtils

public class EncodingUtils
extends Object

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), getInt(Variable), setInt(Variable, int) methods. Internally, integers are represented by floating-point values. In order to remain consistent, only use these methods to create, set, and get the value of integer decision variables.

This class also provides methods for converting between RealVariable and BinaryVariable in both binary and gray code formats.


Method Summary
static void binaryToGray(BinaryVariable variable)
          Converts a binary variable from a binary encoding to gray encoding.
static long decode(BinaryVariable binary)
          Decodes the specified binary variable into its integer value.
static void decode(BinaryVariable binary, RealVariable real)
          Decodes the specified binary variable into its real value.
static void encode(long value, BinaryVariable binary)
          Encodes the integer into the specified binary variable.
static void encode(RealVariable real, BinaryVariable binary)
          Encodes the specified real variable into a binary variable.
static boolean[] getBinary(Variable variable)
          Returns the value stored in a binary decision variable as a boolean array.
static BitSet getBitSet(Variable variable)
          Returns the value stored in a binary decision variable as a BitSet.
static boolean getBoolean(Variable variable)
          Returns the value stored in a boolean decision variable.
static int[] getInt(Solution solution)
          Returns the array of integer-valued decision variables stored in a solution.
static int[] getInt(Solution solution, int startIndex, int endIndex)
          Returns the array of integer-valued decision variables stored in a solution between the specified indices.
static int getInt(Variable variable)
          Returns the value stored in an integer-valued decision variable.
static int[] getPermutation(Variable variable)
          Returns the value stored in a permutation decision variable.
static double[] getReal(Solution solution)
          Returns the array of floating-point decision variables stored in a solution.
static double[] getReal(Solution solution, int startIndex, int endIndex)
          Returns the array of floating-point decision variables stored in a solution between the specified indices.
static double getReal(Variable variable)
          Returns the value stored in a floating-point decision variable.
static void grayToBinary(BinaryVariable variable)
          Converts a binary variable from a gray encoding to binary encoding.
static BinaryVariable newBinary(int length)
          Returns a new binary decision variable with the specified number of bits.
static BinaryVariable newBoolean()
          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 void setBinary(Variable variable, boolean[] values)
          Sets the bits in a binary decision variable using the given boolean array.
static void setBitSet(Variable variable, BitSet bitSet)
          Sets the bits in a binary decision variable using the given BitSet.
static void setBoolean(Variable variable, boolean value)
          Sets the value of a boolean decision variable.
static void setInt(Solution solution, int[] values)
          Sets the values of all integer-valued decision variables stored in the solution.
static void setInt(Solution solution, int startIndex, int endIndex, int[] values)
          Sets the values of the integer-valued decision variables stored in a solution between the specified indices.
static void setInt(Variable variable, int value)
          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 setReal(Solution solution, double[] values)
          Sets the values of all floating-point decision variables stored in the solution.
static void setReal(Solution solution, int startIndex, int endIndex, double[] values)
          Sets the values of the floating-point decision variables stored in a solution between the specified indices.
static void setReal(Variable variable, double value)
          Sets the value of a floating-point decision variable.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

encode

public static void encode(RealVariable real,
                          BinaryVariable binary)
Encodes the specified real variable into a binary variable. The number of bits used in the encoding is binary.getNumberOfBits().

Parameters:
real - the real variable
binary - the binary variable to which the real value is encoded

decode

public static void decode(BinaryVariable binary,
                          RealVariable real)
Decodes the specified binary variable into its real value.

Parameters:
binary - the binary variable
real - the real variable to which the value is decoded

encode

public static void encode(long value,
                          BinaryVariable binary)
Encodes the integer into the specified binary variable. The number of bits used in the encoding is binary.getNumberOfBits().

Parameters:
value - an integer
binary - the binary variable to which the value is encoded

decode

public static long decode(BinaryVariable binary)
Decodes the specified binary variable into its integer value.

Parameters:
binary - the binary variable
Returns:
the integer value of the specified binary variable

binaryToGray

public static void binaryToGray(BinaryVariable variable)
Converts a binary variable from a binary encoding to gray encoding. The gray encoding ensures two adjacent values have binary representations differing in only 1 bit (a Hamming distance of 1).

Parameters:
variable - the variable to be converted

grayToBinary

public static void grayToBinary(BinaryVariable variable)
Converts a binary variable from a gray encoding to binary encoding.

Parameters:
variable - the variable to be converted

newReal

public static RealVariable newReal(double lowerBound,
                                   double upperBound)
Returns a new floating-point decision variable bounded within the specified range.

Parameters:
lowerBound - the lower bound of the floating-point value
upperBound - the upper bound of the floating-point value
Returns:
a new floating-point decision variable bounded within the specified range

newInt

public static RealVariable newInt(int lowerBound,
                                  int upperBound)
Returns a new integer-valued decision variable bounded within the specified range.

Parameters:
lowerBound - the lower bound of the integer value
upperBound - the upper bound of the integer value
Returns:
a new integer-valued decision variable bounded within the specified range

newBoolean

public static BinaryVariable newBoolean()
Returns a new boolean decision variable.

Returns:
a new boolean decision variable

newBinary

public static BinaryVariable newBinary(int length)
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

public static Permutation newPermutation(int length)
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

getReal

public static double getReal(Variable variable)
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 type RealVariable

getInt

public static int getInt(Variable variable)
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 of type RealVariable

getBitSet

public static BitSet getBitSet(Variable variable)
Returns the value stored in a binary decision variable as a BitSet.

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 type BinaryVariable

getBinary

public static boolean[] getBinary(Variable variable)
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 type BinaryVariable

getBoolean

public static boolean getBoolean(Variable variable)
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 type BinaryVariable

getPermutation

public static int[] getPermutation(Variable variable)
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 type Permutation

getReal

public static double[] getReal(Solution solution)
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 type RealVariable

getReal

public static double[] getReal(Solution solution,
                               int startIndex,
                               int endIndex)
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 solution
startIndex - 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 type RealVariable

getInt

public static int[] getInt(Solution solution)
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 type RealVariable

getInt

public static int[] getInt(Solution solution,
                           int startIndex,
                           int endIndex)
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 solution
startIndex - 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 type RealVariable

setReal

public static void setReal(Variable variable,
                           double value)
Sets the value of a floating-point decision variable.

Parameters:
variable - the decision variable
value - the value to assign the floating-point decision variable
Throws:
IllegalArgumentException - if the decision variable is not of type RealVariable
IllegalArgumentException - if the value is out of bounds (value < getLowerBound()) || (value > getUpperBound())

setReal

public static void setReal(Solution solution,
                           double[] values)
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 solution
values - the array of floating-point values to assign the solution
Throws:
IllegalArgumentException - if any decision variable contained in the solution is not of type RealVariable
IllegalArgumentException - if any of the values are out of bounds (value < getLowerBound()) || (value > getUpperBound())

setReal

public static void setReal(Solution solution,
                           int startIndex,
                           int endIndex,
                           double[] values)
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 solution
startIndex - 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 type RealVariable
IllegalArgumentException - if an invalid number of values are provided
IllegalArgumentException - if any of the values are out of bounds (value < getLowerBound()) || (value > getUpperBound())

setInt

public static void setInt(Variable variable,
                          int value)
Sets the value of an integer-valued decision variable.

Parameters:
variable - the decision variable
value - the value to assign the integer-valued decision variable
Throws:
IllegalArgumentException - if the decision variable is not of type RealVariable
IllegalArgumentException - if the value is out of bounds (value < getLowerBound()) || (value > getUpperBound())

setInt

public static void setInt(Solution solution,
                          int[] values)
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 solution
values - the array of integer values to assign the solution
Throws:
IllegalArgumentException - if any decision variable contained in the solution is not of type RealVariable
IllegalArgumentException - if any of the values are out of bounds (value < getLowerBound()) || (value > getUpperBound())

setInt

public static void setInt(Solution solution,
                          int startIndex,
                          int endIndex,
                          int[] values)
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 solution
startIndex - 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 type RealVariable
IllegalArgumentException - if an invalid number of values are provided
IllegalArgumentException - if any of the values are out of bounds (value < getLowerBound()) || (value > getUpperBound())

setBitSet

public static void setBitSet(Variable variable,
                             BitSet bitSet)
Sets the bits in a binary decision variable using the given BitSet.

Parameters:
variable - the decision variable
bitSet - the bits to set in the binary decision variable
Throws:
IllegalArgumentException - if the decision variable is not of type BinaryVariable

setBinary

public static void setBinary(Variable variable,
                             boolean[] values)
Sets the bits in a binary decision variable using the given boolean array.

Parameters:
variable - the decision variable
values - the bits to set in the binary decision variable
Throws:
IllegalArgumentException - if the decision variable is not of type BinaryVariable
IllegalArgumentException - if an invalid number of values are provided

setBoolean

public static void setBoolean(Variable variable,
                              boolean value)
Sets the value of a boolean decision variable.

Parameters:
variable - the decision variable
value - the value to assign the boolean decision variable
Throws:
IllegalArgumentException - if the decision variable is not of type BinaryVariable
IllegalArgumentException - if the number of bits in the binary variable is not 1

setPermutation

public static void setPermutation(Variable variable,
                                  int[] values)
Sets the value of a permutation decision variable.

Parameters:
variable - the decision variable
values - the permutation to assign the permutation decision variable
Throws:
IllegalArgumentException - if the decision variable is not of type Permutation
IllegalArgumentException - if values is not a valid permutation

MOEA Framework 2.3
API Specification

Copyright 2009-2014 MOEA Framework. All rights reserved.
Licensed under the GNU Lesser General Public License.
Return to the MOEA Framework homepage. Hosted by Get MOEA Framework at SourceForge.net. Fast, secure and Free Open Source software downloads