Class Subset

java.lang.Object
org.moeaframework.core.variable.Subset
All Implemented Interfaces:
Serializable, Variable

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

    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
  • 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.
    Returns an independent copy of this decision variable.
    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.
    int
    Returns the minimum number of members in this subset.
    int
    Returns the size of the original set.
    Returns the membership in this subset.
    int
    Returns the maximum number of members in this subset.
    int
     
    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
    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.
    void
    Checks if this subset is valid, throwing an exception if it is not.

    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(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)
  • 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 member
      newValue - 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)
      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
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • equals

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

      public Subset copy()
      Description copied from interface: Variable
      Returns an independent copy of this decision variable. 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).
      Specified by:
      copy in interface Variable
      Returns:
      an independent copy of this decision variable
    • 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.
      Specified by:
      randomize in interface 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
    • 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.)
      Specified by:
      encode in interface Variable
      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().
      Specified by:
      decode in interface Variable
      Parameters:
      value - the value as a string