Class BinaryVariable

java.lang.Object
org.moeaframework.core.variable.AbstractVariable
org.moeaframework.core.variable.BinaryVariable
All Implemented Interfaces:
Serializable, Copyable<Variable>, Defined, Named, Variable
Direct Known Subclasses:
BinaryIntegerVariable

public class BinaryVariable extends AbstractVariable
Decision variable for binary strings.
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
    BinaryVariable(int numberOfBits)
    Constructs a binary variable with the specified number of bits.
    BinaryVariable(String name, int numberOfBits)
    Constructs a binary variable with the specified number of bits.
  • Method Summary

    Modifier and Type
    Method
    Description
    int
    Returns the number of bits in this variable set to true.
    void
    Sets all bits in this variable to false.
    Creates and returns a copy of this object.
    void
    decode(String value)
    Parses and loads the value of this variable from a string.
    Encodes the value of this variable as a string.
    boolean
     
    boolean
    get(int index)
    Returns the value of the bit at the specified index.
    static boolean[]
    getBinary(Variable variable)
    Returns the value stored in a binary decision variable as a boolean array.
    Returns a BitSet representing the state of this variable.
    static BitSet
    getBitSet(Variable variable)
    Returns the value stored in a binary decision variable as a BitSet.
    Returns the string representation, or definition, of this object.
    int
    Returns the number of bits stored in this variable.
    int
    Returns the Hamming distance between this instance and the specified BinaryVariable.
    int
     
    boolean
    Returns true if all bits in this variable are set to false; false otherwise.
    void
    Randomly assign the value of this variable.
    void
    set(int index, boolean value)
    Sets the value of the bit at the specified index.
    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.
    Returns a human-readable representation of this value.

    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

    • BinaryVariable

      public BinaryVariable(int numberOfBits)
      Constructs a binary variable with the specified number of bits. All bits are initially set to false.
      Parameters:
      numberOfBits - the number of bits stored in this variable
    • BinaryVariable

      public BinaryVariable(String name, int numberOfBits)
      Constructs a binary variable with the specified number of bits. All bits are initially set to false.
      Parameters:
      name - the name of this decision variable
      numberOfBits - the number of bits stored in this variable
  • Method Details

    • getNumberOfBits

      public int getNumberOfBits()
      Returns the number of bits stored in this variable.
      Returns:
      the number of bits stored in this variable
    • cardinality

      public int cardinality()
      Returns the number of bits in this variable set to true.
      Returns:
      the number of bits in this variable set to true
    • clear

      public void clear()
      Sets all bits in this variable to false.
    • isEmpty

      public boolean isEmpty()
      Returns true if all bits in this variable are set to false; false otherwise.
      Returns:
      true if all bits in this variable are set to false; false otherwise
    • get

      public boolean get(int index)
      Returns the value of the bit at the specified index.
      Parameters:
      index - the index of the bit to return
      Returns:
      the value of the bit at the specified index
      Throws:
      IndexOutOfBoundsException - if the index is out of bounds (index < 0) || (index >= getNumberOfBits())
    • set

      public void set(int index, boolean value)
      Sets the value of the bit at the specified index.
      Parameters:
      index - the index of the bit to set
      value - the new value of the bit being set
      Throws:
      IndexOutOfBoundsException - if the index is out of bounds (index < 0) || (index >= getNumberOfBits())
    • getBitSet

      public BitSet getBitSet()
      Returns a BitSet representing the state of this variable.
      Returns:
      a BitSet representing the state of this variable
    • hammingDistance

      public int hammingDistance(BinaryVariable variable)
      Returns the Hamming distance between this instance and the specified BinaryVariable. The Hamming distance is the number of bit positions in which the two binary strings differ.
      Parameters:
      variable - the other BinaryVariable
      Returns:
      the Hamming distance between this instance and the specified BinaryVariable
      Throws:
      IllegalArgumentException - if the two binary strings differ in the number of bits
    • copy

      public BinaryVariable 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).
      Returns:
      the copy
    • getDefinition

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

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

      public boolean equals(Object obj)
      Overrides:
      equals in class AbstractVariable
    • 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 Object
      Returns:
      the value of this variable formatted as a string
    • encode

      public String 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, use Variable.toString() if a human-readable format is required.

      This method along with Variable.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.)
      Returns:
      the encoded value as a string
    • decode

      public void decode(String value)
      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 by Variable.encode().
      Parameters:
      value - the value as a string
    • randomize

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