Package org.moeaframework.core.variable
Class Subset
java.lang.Object
org.moeaframework.core.variable.Subset
- All Implemented Interfaces:
Serializable
,Variable
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:
-
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
-
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()
Returns an independent copy of this decision variable.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.int
getL()
Returns the minimum number of members in this subset.int
getN()
Returns the size of the original set.getSet()
Returns the membership in this subset.int
getU()
Returns the maximum number of members in this subset.int
hashCode()
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.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.void
validate()
Checks if this subset is valid, throwing an exception if it is not.
-
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
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)
-
-
Method Details
-
validate
public void validate()Checks if this subset is valid, throwing an exception if it is not.- Throws:
FrameworkException
- if this subset is not valid
-
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
-
add
public void add(int value) Adds a new member to this subset, increasing the subset size by 1.- Parameters:
value
- the new member
-
remove
public void remove(int value) Removes a member from this subset, decreasing the subset size by 1.- Parameters:
value
- the member to remove
-
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
-
hashCode
public int hashCode() -
equals
-
copy
Description copied from interface:Variable
Returns an independent copy of this decision variable. 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)
. -
randomize
public void randomize()Description copied from interface:Variable
Randomly assign the value of this variable. In general, the randomization should follow a uniform distribution. -
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
-
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, useVariable.toString()
if a human-readable format is required. This method along withVariable.decode(String)
are used primarily for storing values in text files. To make parsing easier, the resulting string must: 1. Only contain ASCII characters 2. Contain no whitespace (no spaces, tabs, newlines, etc.) -
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()
.
-