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
ConstructorsConstructorDescriptionBinaryVariable(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 TypeMethodDescriptionintReturns the number of bits in this variable set totrue.voidclear()Sets all bits in this variable tofalse.copy()Creates and returns a copy of this object.voidParses and loads the value of this variable from a string.encode()Encodes the value of this variable as a string.booleanbooleanget(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 aBitSetrepresenting the state of this variable.static BitSetReturns the value stored in a binary decision variable as aBitSet.Returns the string representation, or definition, of this object.intReturns the number of bits stored in this variable.inthammingDistance(BinaryVariable variable) Returns the Hamming distance between this instance and the specifiedBinaryVariable.inthashCode()booleanisEmpty()Returnstrueif all bits in this variable are set tofalse;falseotherwise.voidRandomly assign the value of this variable.voidset(int index, boolean value) Sets the value of the bit at the specified index.static voidSets the bits in a binary decision variable using the given boolean array.static voidSets 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()Returnstrueif all bits in this variable are set tofalse;falseotherwise.- Returns:
trueif all bits in this variable are set tofalse;falseotherwise
-
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 aBitSetrepresenting the state of this variable.- Returns:
- a
BitSetrepresenting 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:CopyableCreates 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 ofxand vice versa. It is typically the case thatx.copy().getClass() == x.getClass()andx.copy().equals(x).- Returns:
- the copy
-
getDefinition
Description copied from interface:DefinedReturns the string representation, or definition, of this object.- Returns:
- the definition
-
hashCode
public int hashCode()- Overrides:
hashCodein classAbstractVariable
-
equals
- Overrides:
equalsin classAbstractVariable
-
toString
Description copied from interface:VariableReturns a human-readable representation of this value. -
encode
Description copied from interface:VariableEncodes 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:VariableParses 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:VariableRandomly 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 typeBinaryVariableIllegalArgumentException- if an invalid number of values are provided
-