public abstract class AbstractAlgorithm extends java.lang.Object implements Algorithm
Algorithm
methods.
When creating a new subclass, one should:
evaluate(org.moeaframework.core.Solution)
or evaluateAll(java.lang.Iterable<org.moeaframework.core.Solution>)
methods
provided by this class. Do not call Problem.evaluate(org.moeaframework.core.Solution)
directly as that will not count the number of function evaluations
correctly.
evaluateAll(java.lang.Iterable<org.moeaframework.core.Solution>)
. Doing so allows function evaluations to run
in parallel when enabled (see Executor#distributeOnAllCores()
).
initialize()
and
iterate()
methods.
Modifier and Type | Field and Description |
---|---|
protected boolean |
initialized
|
protected int |
numberOfEvaluations
The number of times the
evaluate(org.moeaframework.core.Solution) method was invoked. |
protected Problem |
problem
The problem being solved.
|
protected boolean |
terminated
|
Constructor and Description |
---|
AbstractAlgorithm(Problem problem)
Constructs an abstract algorithm for solving the specified problem.
|
Modifier and Type | Method and Description |
---|---|
void |
assertNotInitialized()
Throws an exception if the algorithm is initialized.
|
void |
evaluate(Solution solution)
Evaluates the specified solution for the problem being solved by this
algorithm.
|
void |
evaluateAll(java.lang.Iterable<Solution> solutions)
Evaluates the specified solutions.
|
void |
evaluateAll(Solution[] solutions)
Evaluates the specified solutions.
|
int |
getNumberOfEvaluations()
Returns the number of times the
evaluate method was invoked. |
Problem |
getProblem()
Returns the problem being solved by this algorithm.
|
protected void |
initialize()
Performs any initialization that is required by this algorithm.
|
boolean |
isInitialized()
|
boolean |
isTerminated()
Returns
true if this algorithm is terminated; false
otherwise. |
protected abstract void |
iterate()
Performs one iteration of the algorithm.
|
void |
loadState(java.io.ObjectInputStream stream)
Loads the state of this object from the stream.
|
void |
saveState(java.io.ObjectOutputStream stream)
Writes the state of this object to the stream.
|
void |
step()
This method first checks if the algorithm is initialized.
|
void |
terminate()
Implementations should always invoke
super.terminate() to ensure
the hierarchy is terminated correctly. |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
checkTypeSafety, getState, setState, writeTypeSafety
protected final Problem problem
protected int numberOfEvaluations
evaluate(org.moeaframework.core.Solution)
method was invoked.protected boolean initialized
protected boolean terminated
public AbstractAlgorithm(Problem problem)
problem
- the problem being solvedpublic void evaluateAll(java.lang.Iterable<Solution> solutions)
evaluate(Solution)
on each of the solutions. Subclasses should
prefer calling this method over evaluate
whenever possible,
as this ensures the solutions can be evaluated in parallel.solutions
- the solutions to evaluatepublic void evaluateAll(Solution[] solutions)
evaluateAll(Arrays.asList(solutions))
.solutions
- the solutions to evaluatepublic void evaluate(Solution solution)
Algorithm
evaluate
in interface Algorithm
solution
- the solution to be evaluatedProblem.evaluate(Solution)
public int getNumberOfEvaluations()
Algorithm
evaluate
method was invoked. This
is the primary measure of runtime for optimization algorithms.getNumberOfEvaluations
in interface Algorithm
evaluate
method was invokedpublic Problem getProblem()
Algorithm
getProblem
in interface Algorithm
protected void initialize()
step()
. Implementations should always invoke
super.initialize()
to ensure the algorithm is initialized
correctly.AlgorithmInitializationException
- if the algorithm has already
been initializedpublic boolean isInitialized()
true
if the initialize()
method has been
invoked; false
otherwisepublic void assertNotInitialized()
AlgorithmInitializationException
- if the algorithm is initializedpublic void step()
initialize()
method is invoked. Once initialized, all
subsequent calls to step
invoke iterate()
.
Implementations should override the initialize
and
iterate
methods in preference to modifying this method.step
in interface Algorithm
AlgorithmTerminationException
- if the algorithm has already terminatedprotected abstract void iterate()
public boolean isTerminated()
Algorithm
true
if this algorithm is terminated; false
otherwise.isTerminated
in interface Algorithm
true
if this algorithm is terminated; false
otherwiseAlgorithm.terminate()
public void terminate()
super.terminate()
to ensure
the hierarchy is terminated correctly.terminate
in interface Algorithm
AlgorithmTerminationException
- if the algorithm has already terminatedpublic void saveState(java.io.ObjectOutputStream stream) throws java.io.IOException
Stateful
super.saveState(stream)
followed by writing each
field.public void loadState(java.io.ObjectInputStream stream) throws java.io.IOException, java.lang.ClassNotFoundException
Stateful
Stateful.saveState(ObjectOutputStream)
.Copyright 2009-2024 David Hadka and other contributors. All rights reserved.
Licensed under the GNU Lesser General Public License.
Return to the MOEA Framework homepage.