Package org.moeaframework.core
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
ConstructorDescriptionConstructs 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 TypeMethodDescriptionboolean
Adds the specified solution to this population.boolean
Adds a collection of solutions to this population.<T extends Solution>
booleanaddAll
(T[] solutions) Adds an array of solutions to this population.asList()
Returns the contents of this population as a list.Returns the contents of this object as aTabularData
instance, which can be used to save, print, or format the data in various ways.void
clear()
Removes all solutions from this population.boolean
Returnstrue
if this population contains the specified solution;false
otherwise.boolean
containsAll
(Iterable<? extends Solution> iterable) Returnstrue
if this population contains all the solutions in the specified collection;false
otherwise.<T extends Solution>
booleancontainsAll
(T[] solutions) Returnstrue
if this population contains all the solutions in the specified array;false
otherwise.copy()
Returns a copy of this population.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
Returns the index of the specified solution in this population.boolean
isEmpty()
Returnstrue
if this population contains no solutions;false
otherwise.iterator()
Returns an iterator for accessing the solutions in this population.static Population
loadBinary
(File file) Loads a population from the specified file.static Population
loadBinary
(InputStream inputStream) Loads a population from the specified input stream.static Population
loadObjectives
(File file) Loads a set of objective vectors from the specified file.static Population
loadObjectives
(Reader reader) Loads the objective vectors contained in the specified reader, returning the resulting population.void
loadState
(ObjectInputStream stream) Loads the state of this object from the stream.void
remove
(int index) Removes the solution at the specified index from this population.boolean
Removes the specified solution from this population, if present.boolean
Removes all solutions in the specified collection from this population.boolean
Removes all solutions matching the given predicate.<T extends Solution>
booleanremoveAll
(T[] solutions) Removes all solutions in the specified array from this population.void
Replaces the solution at the given index.void
saveBinary
(File file) 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
saveObjectives
(File file) Saves the objective vectors of all solutions to the specified file.void
saveObjectives
(Writer writer) Saves the objective vectors of all solutions to the writer.void
saveState
(ObjectOutputStream stream) Writes the state of this object to the stream.int
size()
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
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
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
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 theget
orremove
methods.- Parameters:
solution
- the solution whose index is to be returned- Returns:
- the index of the specified solution
-
add
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
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
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
Replaces the solution at the given index.- Parameters:
index
- the index to replacesolution
- the new solution
-
clear
public void clear()Removes all solutions from this population. -
contains
Returnstrue
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
Returnstrue
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
Returnstrue
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()Returnstrue
if this population contains no solutions;false
otherwise.- Returns:
true
if this population contains no solutions;false
otherwise.
-
iterator
Returns an iterator for accessing the solutions in this population. -
remove
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
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
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
Removes all solutions matching the given predicate.- Parameters:
predicate
- the filter that returnstrue
on solutions to remove- Returns:
true
if this population was modified as a result of this method;false
otherwise
-
filter
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 returnstrue
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
Sorts the solutions in this population using the specified comparator. Invocations of certain methods on this population may alter the ordering of solutions, so theget
,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
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 truncationcomparator
- the comparator to be used for truncation
-
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)
oraddAll(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
Returns the contents of this population as a list. Consider usingcopy()
first if planning to modify the solutions in any way.- Returns:
- the contents of this population as a list
-
asTabularData
Description copied from interface:Formattable
Returns the contents of this object as aTabularData
instance, which can be used to save, print, or format the data in various ways.- Specified by:
asTabularData
in interfaceFormattable<Solution>
- Returns:
- the
TabularData
instance
-
saveObjectives
Saves the objective vectors of all solutions to the specified file. Files created using this method should only be loaded using theloadObjectives(File)
method.- Parameters:
file
- the file to which the objective vectors are written- Throws:
IOException
- if an I/O exception occurred
-
saveObjectives
Saves the objective vectors of all solutions to the writer. Files created using this method should only be loaded using theloadObjectives(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
Loads a set of objective vectors from the specified file. Files read using this method should only have been created using thesaveObjectives(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
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
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 theloadBinary(java.io.File)
method.- Parameters:
file
- the file to which the solutions are written- Throws:
IOException
- if an I/O exception occurred
-
saveBinary
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 theloadBinary(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
Loads a population from the specified file. Files read using this method should only have been created using thesaveBinary(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
Loads a population from the specified input stream. Files read using this method should only have been created using thesaveBinary(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
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 callingsuper.saveState(stream)
followed by writing each field.- Specified by:
saveState
in interfaceStateful
- Parameters:
stream
- the stream- Throws:
IOException
- if an I/O error occurred
-
loadState
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 inStateful.saveState(ObjectOutputStream)
.- Specified by:
loadState
in interfaceStateful
- Parameters:
stream
- the stream- Throws:
IOException
- if an I/O error occurredClassNotFoundException
- if the stream referenced a class that is not defined
-