Class Subset

All Implemented Interfaces:
Serializable, Copyable<Variable>, Defined, Named, Variable

public class Subset extends AbstractVariable
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

    Constructors
    Constructor
    Description
    Subset(int k, int n)
    Constructs a new decision variable for representing subsets of size k from a set of size n.
    Subset(int l, int u, int n)
    Constructs a new decision variable for representing subsets whose size ranges between l (minimum size) and u (maximum size) from a set of size n.
    Subset(String name, int k, int n)
    Constructs a new decision variable for representing subsets of size k from a set of size n with the given name.
    Subset(String name, int l, int u, int n)
    Constructs a new decision variable for representing subsets whose size ranges between l (minimum size) and u (maximum size) from a set of size n with the given name.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    add(int value)
    Adds a new member to this subset, increasing the subset size by 1.
    boolean
    contains(int value)
    Returns true if the subset contains the given member; false otherwise.
    Creates and returns a copy of this object.
    void
    decode(String value)
    Parses and loads the value of this variable from a string.
    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
    Returns the minimum number of members in this subset.
    int
    Returns the size of the original set.
    static int[]
    Returns the value stored in a subset decision variable with the items sorted.
    Returns the membership in this subset.
    static int[]
    getSubset(Variable variable)
    Returns the value stored in a subset decision variable.
    static boolean[]
    Returns the subset as a binary string, where 1 indicates the index is included in the set.
    static BitSet
    Returns the subset as a BitSet, where a set bit indicates the index is included in the set.
    int
    Returns the maximum number of members in this subset.
    int
     
    boolean
    Returns true 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
    setSubset(Variable variable, boolean[] values)
    Sets the value of a subset decision variable using a binary string representation.
    static void
    setSubset(Variable variable, int[] values)
    Sets the value of a subset decision variable.
    static void
    setSubset(Variable variable, BitSet bitSet)
    Sets the value of a subset decision variable using a BitSet representation.
    int
    Returns the current size of this subset.
    int[]
    Returns the membership in this subset as an array.
    Returns a human-readable representation of this value.

    Methods inherited from class org.moeaframework.core.variable.AbstractVariable

    getName

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • Constructor Details

    • Subset

      public Subset(int k, int n)
      Constructs a new decision variable for representing subsets of size k from a set of size n.
      Parameters:
      k - the fixed size of the subset
      n - the size of the original set (i.e., the number of candidate members)
    • Subset

      public Subset(String name, int k, int n)
      Constructs a new decision variable for representing subsets of size k from a set of size n with the given name.
      Parameters:
      name - the name of this decision variable
      k - the fixed size of the subset
      n - 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 between l (minimum size) and u (maximum size) from a set of size n.
      Parameters:
      l - the minimum size of the subset
      u - the maximum size of the subset
      n - the size of the original set (i.e., the number of candidate members)
    • Subset

      public Subset(String name, int l, int u, int n)
      Constructs a new decision variable for representing subsets whose size ranges between l (minimum size) and u (maximum size) from a set of size n with the given name.
      Parameters:
      name - the name of this decision variable
      l - the minimum size of the subset
      u - the maximum size of the subset
      n - the size of the original set (i.e., the number of candidate members)
  • Method Details

    • isValid

      public boolean isValid()
      Returns true 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 member
      newValue - 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)
      Returns true 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

      public Set<Integer> 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 class AbstractVariable
    • equals

      public boolean equals(Object obj)
      Overrides:
      equals in class AbstractVariable
    • copy

      public Subset copy()
      Description copied from interface: Copyable
      Creates and returns a copy of this object. It is required that x.copy() is completely independent from x. This means any method invoked on x.copy() in no way alters the state of x and vice versa. It is typically the case that x.copy().getClass() == x.getClass() and x.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

      public String getDefinition()
      Description copied from interface: Defined
      Returns the string representation, or definition, of this object.
      Returns:
      the definition
    • toString

      public String toString()
      Description copied from interface: Variable
      Returns a human-readable representation of this value.
      Specified by:
      toString in interface Variable
      Overrides:
      toString in class Object
      Returns:
      the value of this variable formatted as a string
    • encode

      public String 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:

      1. Only contain ASCII characters
      2. Contain no whitespace (no spaces, tabs, newlines, etc.)
      Returns:
      the encoded value as a string
    • decode

      public void decode(String value)
      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 by Variable.encode().
      Parameters:
      value - the value as a string
    • getSubset

      public static int[] getSubset(Variable variable)
      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 type Subset
    • getOrderedSubset

      public static int[] getOrderedSubset(Variable variable)
      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 type Subset
    • getSubsetAsBinary

      public static boolean[] getSubsetAsBinary(Variable variable)
      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 type Subset
    • getSubsetAsBitSet

      public static BitSet getSubsetAsBitSet(Variable variable)
      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 type Subset
    • setSubset

      public static void setSubset(Variable variable, int[] values)
      Sets the value of a subset decision variable.
      Parameters:
      variable - the decision variable
      values - the subset to assign the subset decision variable
      Throws:
      IllegalArgumentException - if the decision variable is not of type Subset
      IllegalArgumentException - if values is not a valid subset
    • setSubset

      public static void setSubset(Variable variable, boolean[] values)
      Sets the value of a subset decision variable using a binary string representation. Indices set to true are included in the subset.
      Parameters:
      variable - the decision variable
      values - the binary string representation of a subset
      Throws:
      IllegalArgumentException - if the binary string representation is not a valid subset
    • setSubset

      public static void setSubset(Variable variable, BitSet bitSet)
      Sets the value of a subset decision variable using a BitSet representation. Indices set to true are included in the subset.
      Parameters:
      variable - the decision variable
      bitSet - the BitSet representation of a subset
      Throws:
      IllegalArgumentException - if the BitSet representation is not a valid subset