Class BinaryIntegerVariable

All Implemented Interfaces:
Serializable, Copyable<Variable>, Defined, Named, Variable

public class BinaryIntegerVariable extends BinaryVariable
Decision variable for integers encoded as a binary string. Note that if upperBound-lowerBound is not a power of 2, then some values will occur more frequently after a variation operator.
See Also:
  • Nested Class Summary

    Nested classes/interfaces inherited from interface org.moeaframework.core.Defined

    Defined.ConstructorComparator
  • Field Summary

    Fields inherited from class org.moeaframework.core.variable.AbstractVariable

    name
  • Constructor Summary

    Constructors
    Constructor
    Description
    BinaryIntegerVariable(int lowerBound, int upperBound)
    Constructs an integer-valued variable in the range lowerBound <= x <= upperBound.
    BinaryIntegerVariable(int lowerBound, int upperBound, boolean gray)
    Constructs an integer-valued variable in the range lowerBound <= x <= upperBound.
    BinaryIntegerVariable(String name, int lowerBound, int upperBound)
    Constructs an integer-valued variable in the range lowerBound <= x <= upperBound with the given name.
    BinaryIntegerVariable(String name, int lowerBound, int upperBound, boolean gray)
    Constructs an integer-valued variable in the range lowerBound <= x <= upperBound with the given name.
  • Method Summary

    Modifier and Type
    Method
    Description
    Creates and returns a copy of this object.
    boolean
     
    Returns the string representation, or definition, of this object.
    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.
    int
    Returns the lower bound of this decision variable.
    static final int
    getNumberOfBits(int lowerBound, int upperBound)
    Returns the minimum number of bits required to represent an integer within the given bounds.
    int
    Returns the upper bound of this decision variable.
    int
    Returns the current value of this decision variable.
    int
     
    protected boolean
    Returns true if the binary representation using gray coding.
    void
    Randomly assign the value of this 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.
    void
    setValue(int value)
    Sets the value of this decision variable.
    Returns a human-readable representation of this value.
    withValue(int value)
    Calls setValue(int) and returns this instance, mainly allowing one to create and set the value of a decision variable on one line.

    Methods inherited from class org.moeaframework.core.variable.BinaryVariable

    cardinality, clear, decode, encode, get, getBinary, getBitSet, getBitSet, getNumberOfBits, hammingDistance, isEmpty, set, setBinary, setBitSet

    Methods inherited from class org.moeaframework.core.variable.AbstractVariable

    getName

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • Constructor Details

    • BinaryIntegerVariable

      public BinaryIntegerVariable(int lowerBound, int upperBound)
      Constructs an integer-valued variable in the range lowerBound <= x <= upperBound. Uses gray coding by default.
      Parameters:
      lowerBound - the lower bound of this decision variable, inclusive
      upperBound - the upper bound of this decision variable, inclusive
    • BinaryIntegerVariable

      public BinaryIntegerVariable(String name, int lowerBound, int upperBound)
      Constructs an integer-valued variable in the range lowerBound <= x <= upperBound with the given name. Uses gray coding by default.
      Parameters:
      name - the name of this decision variable
      lowerBound - the lower bound of this decision variable, inclusive
      upperBound - the upper bound of this decision variable, inclusive
    • BinaryIntegerVariable

      public BinaryIntegerVariable(int lowerBound, int upperBound, boolean gray)
      Constructs an integer-valued variable in the range lowerBound <= x <= upperBound.
      Parameters:
      lowerBound - the lower bound of this decision variable, inclusive
      upperBound - the upper bound of this decision variable, inclusive
      gray - if the binary representation uses gray coding
    • BinaryIntegerVariable

      public BinaryIntegerVariable(String name, int lowerBound, int upperBound, boolean gray)
      Constructs an integer-valued variable in the range lowerBound <= x <= upperBound with the given name.
      Parameters:
      name - the name of this decision variable
      lowerBound - the lower bound of this decision variable, inclusive
      upperBound - the upper bound of this decision variable, inclusive
      gray - if the binary representation uses gray coding
  • Method Details

    • getNumberOfBits

      public static final int getNumberOfBits(int lowerBound, int upperBound)
      Returns the minimum number of bits required to represent an integer within the given bounds.
      Parameters:
      lowerBound - the lower bound
      upperBound - the upper bound
      Returns:
      the minimum number of bits required to represent an integer within the given bounds
    • getValue

      public int getValue()
      Returns the current value of this decision variable.
      Returns:
      the current value of this decision variable
    • setValue

      public void setValue(int value)
      Sets the value of this decision variable.
      Parameters:
      value - the new value for this decision variable
      Throws:
      IllegalArgumentException - if the value is out of bounds (value < getLowerBound()) || (value > getUpperBound())
    • withValue

      public BinaryIntegerVariable withValue(int value)
      Calls setValue(int) and returns this instance, mainly allowing one to create and set the value of a decision variable on one line.
      Parameters:
      value - the new value for this decision variable
      Returns:
      this decision variable
      Throws:
      IllegalArgumentException - if the value is out of bounds (value < getLowerBound()) || (value > getUpperBound())
    • isGray

      protected boolean isGray()
      Returns true if the binary representation using gray coding. Gray coding ensures that two successive values differ by only one bit.
      Returns:
      true if the binary representation using gray coding; false otherwise
    • getLowerBound

      public int getLowerBound()
      Returns the lower bound of this decision variable.
      Returns:
      the lower bound of this decision variable, inclusive
    • getUpperBound

      public int getUpperBound()
      Returns the upper bound of this decision variable.
      Returns:
      the upper bound of this decision variable, inclusive
    • copy

      public BinaryIntegerVariable copy()
      Description copied from interface: Copyable
      Creates and returns a copy of this object. It is required that x.copy() is completely independent from x. This means any method invoked on x.copy() in no way alters the state of x and vice versa. It is typically the case that x.copy().getClass() == x.getClass() and x.copy().equals(x).
      Specified by:
      copy in interface Copyable<Variable>
      Overrides:
      copy in class BinaryVariable
      Returns:
      the copy
    • getDefinition

      public String getDefinition()
      Description copied from interface: Defined
      Returns the string representation, or definition, of this object.
      Specified by:
      getDefinition in interface Defined
      Overrides:
      getDefinition in class BinaryVariable
      Returns:
      the definition
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class BinaryVariable
    • equals

      public boolean equals(Object obj)
      Overrides:
      equals in class BinaryVariable
    • toString

      public String toString()
      Description copied from interface: Variable
      Returns a human-readable representation of this value.
      Specified by:
      toString in interface Variable
      Overrides:
      toString in class BinaryVariable
      Returns:
      the value of this variable formatted as a string
    • randomize

      public void randomize()
      Description copied from interface: Variable
      Randomly assign the value of this variable.
      Specified by:
      randomize in interface Variable
      Overrides:
      randomize in class BinaryVariable
    • 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
    • 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
    • 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())