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
ConstructorsConstructorDescriptionSubset(int k, int n) Constructs a new decision variable for representing subsets of sizekfrom 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 sizekfrom a set of sizenwith the given name.Constructs a new decision variable for representing subsets whose size ranges betweenl(minimum size) andu(maximum size) from a set of sizenwith the given name. -
Method Summary
Modifier and TypeMethodDescriptionvoidadd(int value) Adds a new member to this subset, increasing the subset size by 1.booleancontains(int value) Returnstrueif the subset contains the given member;falseotherwise.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.booleanvoidfromArray(int[] array) Populates this subset from an array.Returns the string representation, or definition, of this object.intgetL()Returns the minimum number of members in this subset.intgetN()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 BitSetgetSubsetAsBitSet(Variable variable) Returns the subset as a BitSet, where a set bit indicates the index is included in the set.intgetU()Returns the maximum number of members in this subset.inthashCode()booleanisValid()Returnstrueif this subset satisfies the size requirements and all members are valid.voidRandomly assign the value of this variable.intRandomly pick a value that is contained in this subset.intRandomly pick a value that is not contained in this subset.voidremove(int value) Removes a member from this subset, decreasing the subset size by 1.voidreplace(int oldValue, int newValue) Replaces a member of this subset with another member not in this subset.static voidSets the value of a subset decision variable using a binary string representation.static voidSets the value of a subset decision variable.static voidSets the value of a subset decision variable using a BitSet representation.intsize()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 sizekfrom 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 sizekfrom a set of sizenwith 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 sizenwith 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()Returnstrueif this subset satisfies the size requirements and all members are valid.- Returns:
trueif this subset is valid;falseotherwise
-
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) Returnstrueif the subset contains the given member;falseotherwise.- Parameters:
value- the member- Returns:
trueif the subset contains the given member;falseotherwise
-
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:
hashCodein classAbstractVariable
-
equals
- Overrides:
equalsin classAbstractVariable
-
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
-
randomize
public void randomize()Description copied from interface:VariableRandomly 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:DefinedReturns the string representation, or definition, of this object.- Returns:
- the definition
-
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
-
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 typeSubsetIllegalArgumentException- ifvaluesis not a valid subset
-
setSubset
Sets the value of a subset decision variable using a binary string representation. Indices set totrueare 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 totrueare 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
-