Class EncodingUtils

java.lang.Object
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) 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 Type
    Method
    Description
    static BitSet
    Converts a binary string from binary code to gray code.
    static long
    decode(BitSet binary)
    Converts the given binary string into its long value.
    static void
    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
    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[]
    Returns the value stored in a subset decision variable with the items sorted.
    static int[]
    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 int[]
    getSubset(Variable variable)
    Returns the value stored in a subset decision variable.
    static boolean[]
    Returns the subset as a binary string, where 1 indicates the index is included in the set.
    static BitSet
    Returns the subset as a BitSet, where a set bit indicates the index is included in the set.
    static BitSet
    Converts a binary string from gray code to binary code.
    newBinary(int length)
    Returns a new binary decision variable with the specified number of bits.
    newBinaryInt(int lowerBound, int upperBound)
    Returns a new integer-valued decision variable bounded within the specified range.
    Returns a new boolean decision variable.
    newInt(int lowerBound, int upperBound)
    Returns a new integer-valued decision variable bounded within the specified range.
    newPermutation(int length)
    Returns a new permutation with the specified number of items.
    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
    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.
    static void
    setSubset(Variable variable, boolean[] values)
    Sets the value of a subset decision variable using a binary string representation.
    static void
    setSubset(Variable variable, int[] values)
    Sets the value of a subset decision variable.
    static void
    setSubset(Variable variable, BitSet bitSet)
    Sets the value of a subset decision variable using a BitSet representation.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • 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 BitSet encode(long value)
      Converts the given long value into its binary string representation.
      Parameters:
      value - the long value
      Returns:
      the binary string representation
    • decode

      public static long decode(BitSet binary)
      Converts the given binary string into its long value.
      Parameters:
      binary - the binary string
      Returns:
      the long value
    • binaryToGray

      public static BitSet binaryToGray(BitSet binary)
      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 of 1).
      Parameters:
      binary - the binary code string
      Returns:
      the gray code string
    • grayToBinary

      public static BitSet grayToBinary(BitSet gray)
      Converts a binary string from gray code to binary code.
      Parameters:
      gray - the gray code string
      Returns:
      the binary code string
    • 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. The integer value is encoded using a RealVariable.
      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
    • newBinaryInt

      public static BinaryIntegerVariable newBinaryInt(int lowerBound, int upperBound)
      Returns a new integer-valued decision variable bounded within the specified range. The integer value is encoded using a BinaryVariable.
      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
    • newSubset

      public static Subset newSubset(int k, int n)
      Returns a new fixed-size subset with the specified number of items.
      Parameters:
      k - the fixed size of the subset
      n - the total number of items in the set
      Returns:
      a new subset with the specified number of items
    • newSubset

      public static Subset newSubset(int l, int u, int n)
      Returns a new variable-size subset with the specified number of items.
      Parameters:
      l - the minimum number of members in the subset
      u - the maximum number of members in the subset
      n - the total number of items in the set
      Returns:
      a new subset 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 convertible to an integer
    • 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
    • getSubset

      public static int[] getSubset(Variable variable)
      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 type Subset
    • getOrderedSubset

      public static int[] getOrderedSubset(Variable variable)
      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 type Subset
    • getSubsetAsBinary

      public static boolean[] getSubsetAsBinary(Variable variable)
      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 type Subset
    • getSubsetAsBitSet

      public static BitSet getSubsetAsBitSet(Variable variable)
      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 type Subset
    • 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 convertible to an integer
      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 the decision variable is not convertible to an integer
      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 the decision variables are not convertible to an integer
      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
    • setSubset

      public static void setSubset(Variable variable, int[] values)
      Sets the value of a subset decision variable.
      Parameters:
      variable - the decision variable
      values - the subset to assign the subset decision variable
      Throws:
      IllegalArgumentException - if the decision variable is not of type Subset
      IllegalArgumentException - if values is not a valid subset
    • setSubset

      public static void setSubset(Variable variable, boolean[] values)
      Sets the value of a subset decision variable using a binary string representation.
      Parameters:
      variable - the decision variable
      values - the binary string representation of a subset
      Throws:
      IllegalArgumentException - if the binary string representation is not a valid subset
    • setSubset

      public static void setSubset(Variable variable, BitSet bitSet)
      Sets the value of a subset decision variable using a BitSet representation.
      Parameters:
      variable - the decision variable
      bitSet - the BitSet representation of a subset
      Throws:
      IllegalArgumentException - if the BitSet representation is not a valid subset