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 a list of all solutions in this population.
    asList(boolean copy)
    Returns a list of all solutions in this population.
    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.
    get(int index)
    Returns the solution at the specified index in 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
    load(File file)
    Loads a population from the specified file.
    static Population
    Loads a set of objective vectors from the specified file.
    protected 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.
    <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
    save(File file)
    Saves this population to the specified file.
    void
    Saves the objective vectors of all solutions to the specified file.
    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, 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
    • 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
    • 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
    • asList

      public List<Solution> asList()
      Returns a list of all solutions in this population. This is equivalent to calling asList(boolean) with a value of true.
      Returns:
      a list of all solutions in this population
    • asList

      public List<Solution> asList(boolean copy)
      Returns a list of all solutions in this population.
      Parameters:
      copy - if true, copies of each solution are returned
      Returns:
      a list of all solutions in this population
    • 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
    • save

      public void save(File file) throws IOException
      Saves this population to the specified file. This saves a complete copy of the solutions, including the decision variables, objectives, constraints, and attributes. Files written using this method can only be read using the load(java.io.File) method.
      Parameters:
      file - the file to which the solutions are written
      Throws:
      IOException - if an I/O exception occurred
    • loadObjectives

      protected static Population loadObjectives(CommentedLineReader 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
    • 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
    • load

      public static Population load(File file) throws IOException
      Loads a population from the specified file. Files read using this method should only have been created using the save(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
    • 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