Class Population

java.lang.Object
org.moeaframework.core.Population
All Implemented Interfaces:
Iterable<Solution>, Stateful, Displayable, Formattable<Solution>
Direct Known Subclasses:
MSOPSRankedPopulation, NondominatedPopulation, NondominatedSortingPopulation, ReferenceVectorGuidedPopulation

public class Population extends Object implements Iterable<Solution>, Formattable<Solution>, Stateful
A collection of solutions and common methods for manipulating the collection.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Constructs an empty population.
    Population(Iterable<? extends Solution> iterable)
    Constructs a population initialized with a collection of solutions.
    Population(T[] solutions)
    Constructs a population initialized with an array of solutions.
  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
    add(Solution solution)
    Adds the specified solution to this population.
    boolean
    addAll(Iterable<? extends Solution> iterable)
    Adds a collection of solutions to this population.
    <T extends Solution>
    boolean
    addAll(T[] solutions)
    Adds an array of solutions to this population.
    Returns the contents of this population as a list.
    Returns the contents of this object as a TabularData instance, which can be used to save, print, or format the data in various ways.
    void
    Removes all solutions from this population.
    boolean
    contains(Solution solution)
    Returns true if this population contains the specified solution; false otherwise.
    boolean
    containsAll(Iterable<? extends Solution> iterable)
    Returns true if this population contains all the solutions in the specified collection; false otherwise.
    <T extends Solution>
    boolean
    containsAll(T[] solutions)
    Returns true if this population contains all the solutions in the specified array; false otherwise.
    Returns a copy of this population.
    filter(Predicate<? super Solution> predicate)
    Returns a new population containing only the solutions matching the given predicate.
    get(int index)
    Returns the solution at the specified index in this population.
    double[]
    Computes the lower bounds of this population.
    double[]
    Computes the upper bounds of this population.
    int
    indexOf(Solution solution)
    Returns the index of the specified solution in this population.
    boolean
    Returns true if this population contains no solutions; false otherwise.
    Returns an iterator for accessing the solutions in this population.
    static Population
    Loads a population from the specified file.
    static Population
    loadBinary(InputStream inputStream)
    Loads a population from the specified input stream.
    static Population
    Loads a set of objective vectors from the specified file.
    static Population
    Loads the objective vectors contained in the specified reader, returning the resulting population.
    void
    Loads the state of this object from the stream.
    void
    remove(int index)
    Removes the solution at the specified index from this population.
    boolean
    remove(Solution solution)
    Removes the specified solution from this population, if present.
    boolean
    removeAll(Iterable<? extends Solution> iterable)
    Removes all solutions in the specified collection from this population.
    boolean
    removeAll(Predicate<? super Solution> predicate)
    Removes all solutions matching the given predicate.
    <T extends Solution>
    boolean
    removeAll(T[] solutions)
    Removes all solutions in the specified array from this population.
    void
    replace(int index, Solution solution)
    Replaces the solution at the given index.
    void
    Saves this population to a file using a binary format.
    void
    saveBinary(OutputStream outputStream)
    Saves this population to an output stream using a binary format.
    void
    Saves the objective vectors of all solutions to the specified file.
    void
    Saves the objective vectors of all solutions to the writer.
    void
    Writes the state of this object to the stream.
    int
    Returns the number of solutions in this population.
    void
    sort(Comparator<? super Solution> comparator)
    Sorts the solutions in this population using the specified comparator.
    void
    truncate(int size, Comparator<? super Solution> comparator)
    Sorts this population using the specified comparator and removes the last (maximum) solutions until this population's size is within the specified size.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

    Methods inherited from interface org.moeaframework.util.format.Displayable

    display

    Methods inherited from interface org.moeaframework.util.format.Formattable

    display, display, display, save, saveCSV

    Methods inherited from interface java.lang.Iterable

    forEach, spliterator
  • Constructor Details

    • Population

      public Population()
      Constructs an empty population.
    • Population

      public Population(Iterable<? extends Solution> iterable)
      Constructs a population initialized with a collection of solutions.
      Parameters:
      iterable - the collection of solutions for initializing this population
    • Population

      public Population(T[] solutions)
      Constructs a population initialized with an array of solutions.
      Type Parameters:
      T - the type of solutions in the array
      Parameters:
      solutions - the array of solutions for initializing this population
  • Method Details

    • get

      public Solution get(int index)
      Returns the solution at the specified index in this population.
      Parameters:
      index - the index of the solution to be returned
      Returns:
      the solution at the specified index
      Throws:
      IndexOutOfBoundsException - if the index is out of range (index < 0) || (index >= size())
    • remove

      public void remove(int index)
      Removes the solution at the specified index from this population.
      Parameters:
      index - the index of the solution to be removed
      Throws:
      IndexOutOfBoundsException - if the index is out of range (index < 0) || (index >= size())
    • indexOf

      public int indexOf(Solution solution)
      Returns the index of the specified solution in this population. Invocations of certain methods on this population may alter the ordering of solutions, so the index returned should be used immediately by the get or remove methods.
      Parameters:
      solution - the solution whose index is to be returned
      Returns:
      the index of the specified solution
    • add

      public boolean add(Solution solution)
      Adds the specified solution to this population.
      Parameters:
      solution - the solution to be added
      Returns:
      true if the population was modified as a result of this method; false otherwise.
    • addAll

      public boolean addAll(Iterable<? extends Solution> iterable)
      Adds a collection of solutions to this population.
      Parameters:
      iterable - the collection of solutions to be added
      Returns:
      true if the population was modified as a result of this method; false otherwise
    • addAll

      public <T extends Solution> boolean addAll(T[] solutions)
      Adds an array of solutions to this population.
      Type Parameters:
      T - the type of solutions in the array
      Parameters:
      solutions - the solutions to be added
      Returns:
      true if the population was modified as a result of this method; false otherwise
    • replace

      public void replace(int index, Solution solution)
      Replaces the solution at the given index.
      Parameters:
      index - the index to replace
      solution - the new solution
    • clear

      public void clear()
      Removes all solutions from this population.
    • contains

      public boolean contains(Solution solution)
      Returns true if this population contains the specified solution; false otherwise.
      Parameters:
      solution - the solution whose presence is tested
      Returns:
      true if this population contains the specified solution; false otherwise
    • containsAll

      public boolean containsAll(Iterable<? extends Solution> iterable)
      Returns true if this population contains all the solutions in the specified collection; false otherwise.
      Parameters:
      iterable - the collection whose presence is tested
      Returns:
      true if this population contains all the solutions in the specified collection; false otherwise
    • containsAll

      public <T extends Solution> boolean containsAll(T[] solutions)
      Returns true if this population contains all the solutions in the specified array; false otherwise.
      Type Parameters:
      T - the type of solutions in the array
      Parameters:
      solutions - the array whose presence is tested
      Returns:
      true if this population contains all the solutions in the specified array; false otherwise
    • isEmpty

      public boolean isEmpty()
      Returns true if this population contains no solutions; false otherwise.
      Returns:
      true if this population contains no solutions; false otherwise.
    • iterator

      public Iterator<Solution> iterator()
      Returns an iterator for accessing the solutions in this population.
      Specified by:
      iterator in interface Iterable<Solution>
    • remove

      public boolean remove(Solution solution)
      Removes the specified solution from this population, if present.
      Parameters:
      solution - the solution to be removed
      Returns:
      true if this population was modified as a result of this method; false otherwise
    • removeAll

      public boolean removeAll(Iterable<? extends Solution> iterable)
      Removes all solutions in the specified collection from this population.
      Parameters:
      iterable - the collection of solutions to be removed
      Returns:
      true if this population was modified as a result of this method; false otherwise
    • removeAll

      public <T extends Solution> boolean removeAll(T[] solutions)
      Removes all solutions in the specified array from this population.
      Type Parameters:
      T - the type of solutions in the array
      Parameters:
      solutions - the array of solutions to be removed
      Returns:
      true if this population was modified as a result of this method; false otherwise
    • removeAll

      public boolean removeAll(Predicate<? super Solution> predicate)
      Removes all solutions matching the given predicate.
      Parameters:
      predicate - the filter that returns true on solutions to remove
      Returns:
      true if this population was modified as a result of this method; false otherwise
    • filter

      public Population filter(Predicate<? super Solution> predicate)
      Returns a new population containing only the solutions matching the given predicate.

      The filtered population should be treated as a "view" into this population. The structure of the filtered population can be modified without affecting this population (e.g., by adding, removing, or sorting), but any modifications to the solutions contained in either will appear in both. Use copy() to create an independent copy.

      Parameters:
      predicate - the filter that returns true on solutions to keep in the filtered population
      Returns:
      the filtered population
    • size

      public int size()
      Returns the number of solutions in this population.
      Returns:
      the number of solutions in this population
    • sort

      public void sort(Comparator<? super Solution> comparator)
      Sorts the solutions in this population using the specified comparator. Invocations of certain methods on this population may alter the ordering of solutions, so the get, remove and iteration methods should be called immediately after invoking this method.
      Parameters:
      comparator - the comparator to be used for sorting
    • getLowerBounds

      public double[] getLowerBounds()
      Computes the lower bounds of this population. Since all objectives are minimized, this is equivalent to the ideal point. Also note that all solutions, including those violating constraints, are included.
      Returns:
      the lower bounds
    • getUpperBounds

      public double[] getUpperBounds()
      Computes the upper bounds of this population. Since all objectives are minimized, this is equivalent to the Nadir point. Also note that all solutions, including those violating constraints, are included.
      Returns:
      the upper bounds
    • truncate

      public void truncate(int size, Comparator<? super Solution> comparator)
      Sorts this population using the specified comparator and removes the last (maximum) solutions until this population's size is within the specified size.
      Parameters:
      size - the target population size after truncation
      comparator - the comparator to be used for truncation
    • copy

      public Population copy()
      Returns a copy of this population. This can be thought of as a "deep copy", which creates a copy of both the population itself and copies of the individual solutions in the population. Consequently, the returned copy is completely independent, such that any modifications to the contents or order will not impact the original.

      Since creating such a "deep copy" can be expensive, prefer using the constructor Population(Iterable) or addAll(Iterable) whenever possible. These alternatives are useful when filtering or reordering the solutions, but the solutions themselves are left unchanged.

      Returns:
      the copy of this population
    • asList

      public List<Solution> asList()
      Returns the contents of this population as a list. Consider using copy() first if planning to modify the solutions in any way.
      Returns:
      the contents of this population as a list
    • asTabularData

      public TabularData<Solution> asTabularData()
      Description copied from interface: Formattable
      Returns the contents of this object as a TabularData instance, which can be used to save, print, or format the data in various ways.
      Specified by:
      asTabularData in interface Formattable<Solution>
      Returns:
      the TabularData instance
    • saveObjectives

      public void saveObjectives(File file) throws IOException
      Saves the objective vectors of all solutions to the specified file. Files created using this method should only be loaded using the loadObjectives(File) method.
      Parameters:
      file - the file to which the objective vectors are written
      Throws:
      IOException - if an I/O exception occurred
    • saveObjectives

      public void saveObjectives(Writer writer) throws IOException
      Saves the objective vectors of all solutions to the writer. Files created using this method should only be loaded using the loadObjectives(File) method. This method does not close the writer!
      Parameters:
      writer - the writer to which the objective vectors are written
      Throws:
      IOException - if an I/O exception occurred
    • loadObjectives

      public static Population loadObjectives(File file) throws IOException
      Loads a set of objective vectors from the specified file. Files read using this method should only have been created using the saveObjectives(File) method.
      Parameters:
      file - the file containing the objective vectors
      Returns:
      a population containing all objective vectors in the specified file
      Throws:
      IOException - if an I/O exception occurred
    • loadObjectives

      public static Population loadObjectives(Reader reader) throws IOException
      Loads the objective vectors contained in the specified reader, returning the resulting population. This method does not close the reader!
      Parameters:
      reader - the reader containing the objective vectors
      Returns:
      a population containing all objective vectors read
      Throws:
      IOException - if an I/O error occurred
    • saveBinary

      public void saveBinary(File file) throws IOException
      Saves this population to a file using a binary format. Unlike the other save and load methods, the binary format stores a complete copy of the solutions, including all attributes. Files written using this method can only be read using the loadBinary(java.io.File) method.
      Parameters:
      file - the file to which the solutions are written
      Throws:
      IOException - if an I/O exception occurred
    • saveBinary

      public void saveBinary(OutputStream outputStream) throws IOException
      Saves this population to an output stream using a binary format. Unlike the other save and load methods, the binary format stores a complete copy of the solutions, including all attributes. Files written using this method can only be read using the loadBinary(java.io.File) method. This method does not close the output stream!
      Parameters:
      outputStream - the outputStream to which the solutions are written
      Throws:
      IOException - if an I/O exception occurred
    • loadBinary

      public static Population loadBinary(File file) throws IOException
      Loads a population from the specified file. Files read using this method should only have been created using the saveBinary(java.io.File) method.
      Parameters:
      file - the file containing the population
      Returns:
      a population containing all solutions in the specified file
      Throws:
      IOException - if an I/O exception occurred
    • loadBinary

      public static Population loadBinary(InputStream inputStream) throws IOException
      Loads a population from the specified input stream. Files read using this method should only have been created using the saveBinary(java.io.File) method. This method does not close the input stream!
      Parameters:
      inputStream - the input stream containing the population
      Returns:
      a population containing all solutions in the specified file
      Throws:
      IOException - if an I/O exception occurred
    • saveState

      public void saveState(ObjectOutputStream stream) throws IOException
      Description copied from interface: Stateful
      Writes the state of this object to the stream. The order that objects are written to the stream is important. We recommend first calling super.saveState(stream) followed by writing each field.
      Specified by:
      saveState in interface Stateful
      Parameters:
      stream - the stream
      Throws:
      IOException - if an I/O error occurred
    • loadState

      public void loadState(ObjectInputStream stream) throws IOException, ClassNotFoundException
      Description copied from interface: Stateful
      Loads the state of this object from the stream. The order for reading objects from the stream must match the order they are written to the stream in Stateful.saveState(ObjectOutputStream).
      Specified by:
      loadState in interface Stateful
      Parameters:
      stream - the stream
      Throws:
      IOException - if an I/O error occurred
      ClassNotFoundException - if the stream referenced a class that is not defined