Package org.moeaframework.core.variable
Class Subset
java.lang.Object
org.moeaframework.core.variable.AbstractVariable
org.moeaframework.core.variable.Subset
Decision variable for subsets. Subsets can either be fixed-length, which must contain exactly
k
elements,
or variable-length, which can contain between l
and u
elements. Furthermore, the values stored in
a subset can range from 0
to n
, where n >= k, u
.- 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
ConstructorDescriptionSubset
(int k, int n) Constructs a new decision variable for representing subsets of sizek
from a set of sizen
.Subset
(int l, int u, int n) Constructs a new decision variable for representing subsets whose size ranges betweenl
(minimum size) andu
(maximum size) from a set of sizen
.Constructs a new decision variable for representing subsets of sizek
from a set of sizen
with the given name.Constructs a new decision variable for representing subsets whose size ranges betweenl
(minimum size) andu
(maximum size) from a set of sizen
with the given name. -
Method Summary
Modifier and TypeMethodDescriptionvoid
add
(int value) Adds a new member to this subset, increasing the subset size by 1.boolean
contains
(int value) Returnstrue
if the subset contains the given member;false
otherwise.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
void
fromArray
(int[] array) Populates this subset from an array.Returns the string representation, or definition, of this object.int
getL()
Returns the minimum number of members in this subset.int
getN()
Returns the size of the original set.static int[]
getOrderedSubset
(Variable variable) Returns the value stored in a subset decision variable with the items sorted.getSet()
Returns the membership in this subset.static int[]
Returns the value stored in a subset decision variable.static boolean[]
getSubsetAsBinary
(Variable variable) Returns the subset as a binary string, where 1 indicates the index is included in the set.static BitSet
getSubsetAsBitSet
(Variable variable) Returns the subset as a BitSet, where a set bit indicates the index is included in the set.int
getU()
Returns the maximum number of members in this subset.int
hashCode()
boolean
isValid()
Returnstrue
if this subset satisfies the size requirements and all members are valid.void
Randomly assign the value of this variable.int
Randomly pick a value that is contained in this subset.int
Randomly pick a value that is not contained in this subset.void
remove
(int value) Removes a member from this subset, decreasing the subset size by 1.void
replace
(int oldValue, int newValue) Replaces a member of this subset with another member not in this subset.static void
Sets the value of a subset decision variable using a binary string representation.static void
Sets the value of a subset decision variable.static void
Sets the value of a subset decision variable using a BitSet representation.int
size()
Returns the current size of this subset.int[]
toArray()
Returns the membership in this subset as an array.toString()
Returns a human-readable representation of this value.Methods inherited from class org.moeaframework.core.variable.AbstractVariable
getName
-
Constructor Details
-
Subset
public Subset(int k, int n) Constructs a new decision variable for representing subsets of sizek
from a set of sizen
.- Parameters:
k
- the fixed size of the subsetn
- the size of the original set (i.e., the number of candidate members)
-
Subset
Constructs a new decision variable for representing subsets of sizek
from a set of sizen
with the given name.- Parameters:
name
- the name of this decision variablek
- the fixed size of the subsetn
- the size of the original set (i.e., the number of candidate members)
-
Subset
public Subset(int l, int u, int n) Constructs a new decision variable for representing subsets whose size ranges betweenl
(minimum size) andu
(maximum size) from a set of sizen
.- Parameters:
l
- the minimum size of the subsetu
- the maximum size of the subsetn
- the size of the original set (i.e., the number of candidate members)
-
Subset
Constructs a new decision variable for representing subsets whose size ranges betweenl
(minimum size) andu
(maximum size) from a set of sizen
with the given name.- Parameters:
name
- the name of this decision variablel
- the minimum size of the subsetu
- the maximum size of the subsetn
- the size of the original set (i.e., the number of candidate members)
-
-
Method Details
-
isValid
public boolean isValid()Returnstrue
if this subset satisfies the size requirements and all members are valid.- Returns:
true
if this subset is valid;false
otherwise
-
getL
public int getL()Returns the minimum number of members in this subset.- Returns:
- the minimum number of members in this subset
-
getU
public int getU()Returns the maximum number of members in this subset.- Returns:
- the maximum number of members in this subset
-
getN
public int getN()Returns the size of the original set.- Returns:
- the size of the original set
-
size
public int size()Returns the current size of this subset.- Returns:
- the current size of this subset
-
replace
public void replace(int oldValue, int newValue) Replaces a member of this subset with another member not in this subset.- Parameters:
oldValue
- the old membernewValue
- the new member- Throws:
IllegalArgumentException
- if either value is not valid
-
add
public void add(int value) Adds a new member to this subset, increasing the subset size by 1.This operation can result in a subset that violates the subset size requirements. Use
isValid()
after making modifications to verify the resulting subset is valid.- Parameters:
value
- the new member- Throws:
IllegalArgumentException
- if the value is not a valid member
-
remove
public void remove(int value) Removes a member from this subset, decreasing the subset size by 1. This has no effect if the value is not already a member of the subset.This operation can result in a subset that violates the subset size requirements. Use
isValid()
after making modifications to verify the resulting subset is valid.- Parameters:
value
- the member to remove- Throws:
IllegalArgumentException
- if the value is not a valid member
-
contains
public boolean contains(int value) Returnstrue
if the subset contains the given member;false
otherwise.- Parameters:
value
- the member- Returns:
true
if the subset contains the given member;false
otherwise
-
getSet
Returns the membership in this subset.- Returns:
- the membership in this subset.
-
toArray
public int[] toArray()Returns the membership in this subset as an array. The ordering is non-deterministic and may change between calls.- Returns:
- the membership in this subset as an array
-
fromArray
public void fromArray(int[] array) Populates this subset from an array. Any duplicate values in the array will be ignored.- Parameters:
array
- the array containing the subset members- Throws:
IllegalArgumentException
- if the array is not a valid subset
-
hashCode
public int hashCode()- Overrides:
hashCode
in classAbstractVariable
-
equals
- Overrides:
equals
in classAbstractVariable
-
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
-
randomize
public void randomize()Description copied from interface:Variable
Randomly assign the value of this variable. -
randomMember
public int randomMember()Randomly pick a value that is contained in this subset.- Returns:
- the randomly-selected member
-
randomNonmember
public int randomNonmember()Randomly pick a value that is not contained in this subset.- Returns:
- the randomly-selected non-member
-
getDefinition
Description copied from interface:Defined
Returns the string representation, or definition, of this object.- Returns:
- the definition
-
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
-
getSubset
Returns the value stored in a subset decision variable.- Parameters:
variable
- the decision variable- Returns:
- the value stored in a subset decision variable
- Throws:
IllegalArgumentException
- if the decision variable is not of typeSubset
-
getOrderedSubset
Returns the value stored in a subset decision variable with the items sorted.- Parameters:
variable
- the decision variable- Returns:
- the value stored in a subset decision variable with the items sorted
- Throws:
IllegalArgumentException
- if the decision variable is not of typeSubset
-
getSubsetAsBinary
Returns the subset as a binary string, where 1 indicates the index is included in the set.- Parameters:
variable
- the decision variable- Returns:
- a binary string representation of the subset
- Throws:
IllegalArgumentException
- if the decision variable is not of typeSubset
-
getSubsetAsBitSet
Returns the subset as a BitSet, where a set bit indicates the index is included in the set.- Parameters:
variable
- the decision variable- Returns:
- a BitSet representation of the subset
- Throws:
IllegalArgumentException
- if the decision variable is not of typeSubset
-
setSubset
Sets the value of a subset decision variable.- Parameters:
variable
- the decision variablevalues
- the subset to assign the subset decision variable- Throws:
IllegalArgumentException
- if the decision variable is not of typeSubset
IllegalArgumentException
- ifvalues
is not a valid subset
-
setSubset
Sets the value of a subset decision variable using a binary string representation. Indices set totrue
are included in the subset.- Parameters:
variable
- the decision variablevalues
- the binary string representation of a subset- Throws:
IllegalArgumentException
- if the binary string representation is not a valid subset
-
setSubset
Sets the value of a subset decision variable using a BitSet representation. Indices set totrue
are included in the subset.- Parameters:
variable
- the decision variablebitSet
- the BitSet representation of a subset- Throws:
IllegalArgumentException
- if the BitSet representation is not a valid subset
-