Class BinaryVariable

java.lang.Object
org.moeaframework.core.variable.BinaryVariable
All Implemented Interfaces:
Serializable, Variable
Direct Known Subclasses:
BinaryIntegerVariable

public class BinaryVariable extends Object implements Variable
Decision variable for binary strings.
See Also:
  • Constructor Summary

    Constructors
    Constructor
    Description
    BinaryVariable(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.
    Returns an independent copy of this decision variable.
    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.
    Returns a BitSet representing the state of this variable.
    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.
    Returns a human-readable representation of this value.

    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
  • 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: Variable
      Returns an independent copy of this decision variable. 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 Variable
      Returns:
      an independent copy of this decision variable
    • hashCode

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

      public boolean equals(Object obj)
      Overrides:
      equals in class Object
    • 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.)
      Specified by:
      encode in interface Variable
      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().
      Specified by:
      decode in interface Variable
      Parameters:
      value - the value as a string
    • randomize

      public void randomize()
      Description copied from interface: Variable
      Randomly assign the value of this variable. In general, the randomization should follow a uniform distribution.
      Specified by:
      randomize in interface Variable