Package org.moeaframework.core.variable
Class BinaryVariable
java.lang.Object
org.moeaframework.core.variable.BinaryVariable
- All Implemented Interfaces:
Serializable
,Variable
- Direct Known Subclasses:
BinaryIntegerVariable
Decision variable for binary strings.
- See Also:
-
Constructor Summary
ConstructorDescriptionBinaryVariable
(int numberOfBits) Constructs a binary variable with the specified number of bits. -
Method Summary
Modifier and TypeMethodDescriptionint
Returns the number of bits in this variable set totrue
.void
clear()
Sets all bits in this variable tofalse
.copy()
Returns an independent copy of this decision variable.void
Parses and loads the value of this variable from a string.encode()
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 aBitSet
representing the state of this variable.int
Returns the number of bits stored in this variable.int
hammingDistance
(BinaryVariable variable) Returns the Hamming distance between this instance and the specifiedBinaryVariable
.int
hashCode()
boolean
isEmpty()
Returnstrue
if all bits in this variable are set tofalse
;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.toString()
Returns a human-readable representation of this value.
-
Constructor Details
-
BinaryVariable
public BinaryVariable(int numberOfBits) Constructs a binary variable with the specified number of bits. All bits are initially set tofalse
.- 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 totrue
.- Returns:
- the number of bits in this variable set to
true
-
clear
public void clear()Sets all bits in this variable tofalse
. -
isEmpty
public boolean isEmpty()Returnstrue
if all bits in this variable are set tofalse
;false
otherwise.- Returns:
true
if all bits in this variable are set tofalse
;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 setvalue
- the new value of the bit being set- Throws:
IndexOutOfBoundsException
- if the index is out of bounds(index < 0) || (index >= getNumberOfBits())
-
getBitSet
Returns aBitSet
representing the state of this variable.- Returns:
- a
BitSet
representing the state of this variable
-
hammingDistance
Returns the Hamming distance between this instance and the specifiedBinaryVariable
. The Hamming distance is the number of bit positions in which the two binary strings differ.- Parameters:
variable
- the otherBinaryVariable
- 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
Description copied from interface:Variable
Returns an independent copy of this decision variable. It is required thatx.copy()
is completely independent fromx
. This means any method invoked onx.copy()
in no way alters the state ofx
and vice versa. It is typically the case thatx.copy().getClass() == x.getClass()
andx.copy().equals(x)
. -
hashCode
public int hashCode() -
equals
-
toString
Description copied from interface:Variable
Returns a human-readable representation of this value. -
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, useVariable.toString()
if a human-readable format is required. This method along withVariable.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.) -
decode
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 byVariable.encode()
. -
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.
-