Package org.moeaframework.core.variable
Class BinaryIntegerVariable
java.lang.Object
org.moeaframework.core.variable.AbstractVariable
org.moeaframework.core.variable.BinaryVariable
org.moeaframework.core.variable.BinaryIntegerVariable
Decision variable for integers encoded as a binary string. Note that if
upperBound-lowerBound
is not a
power of 2, then some values will occur more frequently after a variation operator.- 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
ConstructorDescriptionBinaryIntegerVariable
(int lowerBound, int upperBound) Constructs an integer-valued variable in the rangelowerBound <= x <= upperBound
.BinaryIntegerVariable
(int lowerBound, int upperBound, boolean gray) Constructs an integer-valued variable in the rangelowerBound <= x <= upperBound
.BinaryIntegerVariable
(String name, int lowerBound, int upperBound) Constructs an integer-valued variable in the rangelowerBound <= x <= upperBound
with the given name.BinaryIntegerVariable
(String name, int lowerBound, int upperBound, boolean gray) Constructs an integer-valued variable in the rangelowerBound <= x <= upperBound
with the given name. -
Method Summary
Modifier and TypeMethodDescriptioncopy()
Creates and returns a copy of this object.boolean
Returns the string representation, or definition, of this object.static int[]
Returns the array of integer-valued decision variables stored in a solution.static int[]
Returns the array of integer-valued decision variables stored in a solution between the specified indices.static int
Returns the value stored in an integer-valued decision variable.int
Returns the lower bound of this decision variable.static final int
getNumberOfBits
(int lowerBound, int upperBound) Returns the minimum number of bits required to represent an integer within the given bounds.int
Returns the upper bound of this decision variable.int
getValue()
Returns the current value of this decision variable.int
hashCode()
protected boolean
isGray()
Returnstrue
if the binary representation using gray coding.void
Randomly assign the value of this variable.static void
Sets the values of all integer-valued decision variables stored in the solution.static void
Sets the values of the integer-valued decision variables stored in a solution between the specified indices.static void
Sets the value of an integer-valued decision variable.void
setValue
(int value) Sets the value of this decision variable.toString()
Returns a human-readable representation of this value.withValue
(int value) CallssetValue(int)
and returns this instance, mainly allowing one to create and set the value of a decision variable on one line.Methods inherited from class org.moeaframework.core.variable.BinaryVariable
cardinality, clear, decode, encode, get, getBinary, getBitSet, getBitSet, getNumberOfBits, hammingDistance, isEmpty, set, setBinary, setBitSet
Methods inherited from class org.moeaframework.core.variable.AbstractVariable
getName
-
Constructor Details
-
BinaryIntegerVariable
public BinaryIntegerVariable(int lowerBound, int upperBound) Constructs an integer-valued variable in the rangelowerBound <= x <= upperBound
. Uses gray coding by default.- Parameters:
lowerBound
- the lower bound of this decision variable, inclusiveupperBound
- the upper bound of this decision variable, inclusive
-
BinaryIntegerVariable
Constructs an integer-valued variable in the rangelowerBound <= x <= upperBound
with the given name. Uses gray coding by default.- Parameters:
name
- the name of this decision variablelowerBound
- the lower bound of this decision variable, inclusiveupperBound
- the upper bound of this decision variable, inclusive
-
BinaryIntegerVariable
public BinaryIntegerVariable(int lowerBound, int upperBound, boolean gray) Constructs an integer-valued variable in the rangelowerBound <= x <= upperBound
.- Parameters:
lowerBound
- the lower bound of this decision variable, inclusiveupperBound
- the upper bound of this decision variable, inclusivegray
- if the binary representation uses gray coding
-
BinaryIntegerVariable
Constructs an integer-valued variable in the rangelowerBound <= x <= upperBound
with the given name.- Parameters:
name
- the name of this decision variablelowerBound
- the lower bound of this decision variable, inclusiveupperBound
- the upper bound of this decision variable, inclusivegray
- if the binary representation uses gray coding
-
-
Method Details
-
getNumberOfBits
public static final int getNumberOfBits(int lowerBound, int upperBound) Returns the minimum number of bits required to represent an integer within the given bounds.- Parameters:
lowerBound
- the lower boundupperBound
- the upper bound- Returns:
- the minimum number of bits required to represent an integer within the given bounds
-
getValue
public int getValue()Returns the current value of this decision variable.- Returns:
- the current value of this decision variable
-
setValue
public void setValue(int value) Sets the value of this decision variable.- Parameters:
value
- the new value for this decision variable- Throws:
IllegalArgumentException
- if the value is out of bounds(value < getLowerBound()) || (value > getUpperBound())
-
withValue
CallssetValue(int)
and returns this instance, mainly allowing one to create and set the value of a decision variable on one line.- Parameters:
value
- the new value for this decision variable- Returns:
- this decision variable
- Throws:
IllegalArgumentException
- if the value is out of bounds(value < getLowerBound()) || (value > getUpperBound())
-
isGray
protected boolean isGray()Returnstrue
if the binary representation using gray coding. Gray coding ensures that two successive values differ by only one bit.- Returns:
true
if the binary representation using gray coding;false
otherwise
-
getLowerBound
public int getLowerBound()Returns the lower bound of this decision variable.- Returns:
- the lower bound of this decision variable, inclusive
-
getUpperBound
public int getUpperBound()Returns the upper bound of this decision variable.- Returns:
- the upper bound of this decision variable, inclusive
-
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)
.- Specified by:
copy
in interfaceCopyable<Variable>
- Overrides:
copy
in classBinaryVariable
- Returns:
- the copy
-
getDefinition
Description copied from interface:Defined
Returns the string representation, or definition, of this object.- Specified by:
getDefinition
in interfaceDefined
- Overrides:
getDefinition
in classBinaryVariable
- Returns:
- the definition
-
hashCode
public int hashCode()- Overrides:
hashCode
in classBinaryVariable
-
equals
- Overrides:
equals
in classBinaryVariable
-
toString
Description copied from interface:Variable
Returns a human-readable representation of this value.- Specified by:
toString
in interfaceVariable
- Overrides:
toString
in classBinaryVariable
- Returns:
- the value of this variable formatted as a string
-
randomize
public void randomize()Description copied from interface:Variable
Randomly assign the value of this variable.- Specified by:
randomize
in interfaceVariable
- Overrides:
randomize
in classBinaryVariable
-
getInt
Returns the value stored in an integer-valued decision variable.- Parameters:
variable
- the decision variable- Returns:
- the value stored in an integer-valued decision variable
- Throws:
IllegalArgumentException
- if the decision variable is not convertible to an integer
-
getInt
Returns the array of integer-valued decision variables stored in a solution. The solution must contain only integer-valued decision variables.- Parameters:
solution
- the solution- Returns:
- the array of integer-valued decision variables stored in a solution
- Throws:
IllegalArgumentException
- if any decision variable contained in the solution is not of typeRealVariable
-
getInt
Returns the array of integer-valued decision variables stored in a solution between the specified indices. The decision variables located between the start and end index must all be integer-valued decision variables.- Parameters:
solution
- the solutionstartIndex
- the start index (inclusive)endIndex
- the end index (exclusive)- Returns:
- the array of integer-valued decision variables stored in a solution between the specified indices
- Throws:
IllegalArgumentException
- if any decision variable contained in the solution between the start and end index is not of typeRealVariable
-
setInt
Sets the value of an integer-valued decision variable.- Parameters:
variable
- the decision variablevalue
- the value to assign the integer-valued decision variable- Throws:
IllegalArgumentException
- if the decision variable is not convertible to an integerIllegalArgumentException
- if the value is out of bounds (value < getLowerBound()) || (value > getUpperBound()
)
-
setInt
Sets the values of all integer-valued decision variables stored in the solution. The solution must contain only integer-valued decision variables.- Parameters:
solution
- the solutionvalues
- the array of integer values to assign the solution- Throws:
IllegalArgumentException
- if the decision variable is not convertible to an integerIllegalArgumentException
- if any of the values are out of bounds (value < getLowerBound()) || (value > getUpperBound()
)
-
setInt
Sets the values of the integer-valued decision variables stored in a solution between the specified indices. The decision variables located between the start and end index must all be integer-valued decision variables.- Parameters:
solution
- the solutionstartIndex
- the start index (inclusive)endIndex
- the end index (exclusive)values
- the array of floating-point values to assign the decision variables- Throws:
IllegalArgumentException
- if the decision variables are not convertible to an integerIllegalArgumentException
- if an invalid number of values are providedIllegalArgumentException
- if any of the values are out of bounds (value < getLowerBound()) || (value > getUpperBound()
)
-