Class Population
java.lang.Object
org.moeaframework.core.population.Population
- All Implemented Interfaces:
Iterable<Solution>,Copyable<Population>,Stateful,Displayable,Formattable<Solution>
- Direct Known Subclasses:
MSOPSRankedPopulation,NondominatedPopulation,NondominatedSortingPopulation,ReferenceVectorGuidedPopulation
public class Population
extends Object
implements Iterable<Solution>, Formattable<Solution>, Copyable<Population>, Stateful
A collection of solutions and common methods for manipulating the collection.
-
Constructor Summary
ConstructorsConstructorDescriptionConstructs 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 TypeMethodDescriptionbooleanAdds the specified solution to this population.booleanAdds 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 aTabularDatainstance, which can be used to save, print, or format the data in various ways.voidclear()Removes all solutions from this population.booleanReturnstrueif this population contains the specified solution;falseotherwise.booleancontainsAll(Iterable<? extends Solution> iterable) Returnstrueif this population contains all the solutions in the specified collection;falseotherwise.<T extends Solution>
booleancontainsAll(T[] solutions) Returnstrueif this population contains all the solutions in the specified array;falseotherwise.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.intReturns the index of the specified solution in this population.booleanisEmpty()Returnstrueif this population contains no solutions;falseotherwise.iterator()Returns an iterator for accessing the solutions in this population.static PopulationLoads a population from the specified result file.static PopulationLoads a population from the specified reader.voidloadState(ObjectInputStream stream) Loads the state of this object from the stream.voidremove(int index) Removes the solution at the specified index from this population.booleanRemoves the specified solution from this population, if present.booleanRemoves all solutions in the specified collection from this population.booleanRemoves all solutions matching the given predicate.<T extends Solution>
booleanremoveAll(T[] solutions) Removes all solutions in the specified array from this population.voidReplaces the solution at the given index.voidSaves all solutions to the specified result file.voidSaves all solutions to the specified result file.voidsaveState(ObjectOutputStream stream) Writes the state of this object to the stream.intsize()Returns the number of solutions in this population.voidsort(Comparator<? super Solution> comparator) Sorts the solutions in this population using the specified comparator.voidtruncate(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, waitMethods inherited from interface org.moeaframework.util.format.Displayable
displayMethods inherited from interface org.moeaframework.util.format.Formattable
display, display, display, save, save, saveMethods 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 thegetorremovemethods.- 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:
trueif the population was modified as a result of this method;falseotherwise.
-
addAll
Adds a collection of solutions to this population.- Parameters:
iterable- the collection of solutions to be added- Returns:
trueif the population was modified as a result of this method;falseotherwise
-
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:
trueif the population was modified as a result of this method;falseotherwise
-
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
Returnstrueif this population contains the specified solution;falseotherwise.- Parameters:
solution- the solution whose presence is tested- Returns:
trueif this population contains the specified solution;falseotherwise
-
containsAll
Returnstrueif this population contains all the solutions in the specified collection;falseotherwise.- Parameters:
iterable- the collection whose presence is tested- Returns:
trueif this population contains all the solutions in the specified collection;falseotherwise
-
containsAll
Returnstrueif this population contains all the solutions in the specified array;falseotherwise.- Type Parameters:
T- the type of solutions in the array- Parameters:
solutions- the array whose presence is tested- Returns:
trueif this population contains all the solutions in the specified array;falseotherwise
-
isEmpty
public boolean isEmpty()Returnstrueif this population contains no solutions;falseotherwise.- Returns:
trueif this population contains no solutions;falseotherwise.
-
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:
trueif this population was modified as a result of this method;falseotherwise
-
removeAll
Removes all solutions in the specified collection from this population.- Parameters:
iterable- the collection of solutions to be removed- Returns:
trueif this population was modified as a result of this method;falseotherwise
-
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:
trueif this population was modified as a result of this method;falseotherwise
-
removeAll
Removes all solutions matching the given predicate.- Parameters:
predicate- the filter that returnstrueon solutions to remove- Returns:
trueif this population was modified as a result of this method;falseotherwise
-
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 returnstrueon 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,removeand 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.- Specified by:
copyin interfaceCopyable<Population>- 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:FormattableReturns the contents of this object as aTabularDatainstance, which can be used to save, print, or format the data in various ways.- Specified by:
asTabularDatain interfaceFormattable<Solution>- Returns:
- the
TabularDatainstance
-
save
Saves all solutions to the specified result file. Files created using this method should only be loaded using theload(File)method.- Parameters:
file- the file to which the solutions are written- Throws:
IOException- if an I/O error occurred
-
save
Saves all solutions to the specified result file. Files created using this method should only be loaded using theload(File)method.- Parameters:
writer- the writer to which the solutions are written- Throws:
IOException- if an I/O error occurred
-
load
Loads a population from the specified result file. If the file contains multiple population entries, only the last is returned. Files read using this method should only have been created usingsave(File).- Parameters:
file- the file- Returns:
- the population
- Throws:
EmptyResultFileException- if the result file was empty or not properly formattedIOException- if an I/O error occurred
-
load
Loads a population from the specified reader. If the reader contains multiple population entries, only the last is returned. This method does not close the reader!- Parameters:
reader- the reader- Returns:
- the population
- Throws:
EmptyResultFileException- if the result file was empty or not properly formattedIOException- if an I/O error occurred
-
saveState
Description copied from interface:StatefulWrites 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:
saveStatein interfaceStateful- Parameters:
stream- the stream- Throws:
IOException- if an I/O error occurred
-
loadState
Description copied from interface:StatefulLoads 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:
loadStatein interfaceStateful- Parameters:
stream- the stream- Throws:
IOException- if an I/O error occurredClassNotFoundException- if the stream referenced a class that is not defined
-