Package org.moeaframework.core.variable
Class BinaryVariable
java.lang.Object
org.moeaframework.core.variable.AbstractVariable
org.moeaframework.core.variable.BinaryVariable
- Direct Known Subclasses:
BinaryIntegerVariable
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
ConstructorDescriptionBinaryVariable
(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 TypeMethodDescriptionint
Returns the number of bits in this variable set totrue
.void
clear()
Sets all bits in this variable tofalse
.copy()
Creates and returns a copy of this object.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.static boolean[]
Returns the value stored in a binary decision variable as a boolean array.Returns aBitSet
representing the state of this variable.static BitSet
Returns the value stored in a binary decision variable as aBitSet
.Returns the string representation, or definition, of this object.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.static void
Sets the bits in a binary decision variable using the given boolean array.static void
Sets the bits in a binary decision variable using the givenBitSet
.toString()
Returns a human-readable representation of this value.Methods inherited from class org.moeaframework.core.variable.AbstractVariable
getName
-
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
-
BinaryVariable
Constructs a binary variable with the specified number of bits. All bits are initially set tofalse
.- Parameters:
name
- the name of this decision variablenumberOfBits
- 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:Copyable
Creates and returns a copy of this object. 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)
.- Returns:
- the copy
-
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 classAbstractVariable
-
equals
- Overrides:
equals
in classAbstractVariable
-
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, 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:- Only contain ASCII characters
- Contain no whitespace (no spaces, tabs, newlines, etc.)
- Returns:
- the encoded value as a string
-
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()
.- Parameters:
value
- the value as a string
-
randomize
public void randomize()Description copied from interface:Variable
Randomly assign the value of this variable. -
getBitSet
Returns the value stored in a binary decision variable as aBitSet
.- 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 typeBinaryVariable
-
getBinary
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 typeBinaryVariable
-
setBitSet
Sets the bits in a binary decision variable using the givenBitSet
.- Parameters:
variable
- the decision variablebitSet
- the bits to set in the binary decision variable- Throws:
IllegalArgumentException
- if the decision variable is not of typeBinaryVariable
-
setBinary
Sets the bits in a binary decision variable using the given boolean array.- Parameters:
variable
- the decision variablevalues
- the bits to set in the binary decision variable- Throws:
IllegalArgumentException
- if the decision variable is not of typeBinaryVariable
IllegalArgumentException
- if an invalid number of values are provided
-