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 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 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.get
(int index) Returns the solution at the specified index in 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
Loads a population from the specified file.static Population
loadObjectives
(File file) Loads a set of objective vectors from the specified file.protected static Population
loadObjectives
(CommentedLineReader 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.<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
Saves this population to the specified file.void
saveObjectives
(File file) Saves the objective vectors of all solutions to the specified file.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, 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
-
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
-
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
-
asList
Returns a list of all solutions in this population. This is equivalent to callingasList(boolean)
with a value oftrue
.- Returns:
- a list of all solutions in this population
-
asList
Returns a list of all solutions in this population.- Parameters:
copy
- iftrue
, copies of each solution are returned- Returns:
- a list of all solutions in this population
-
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
-
save
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 theload(java.io.File)
method.- Parameters:
file
- the file to which the solutions are written- 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
-
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
-
load
Loads a population from the specified file. Files read using this method should only have been created using thesave(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
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
-