Package org.moeaframework.algorithm
Class AbstractAlgorithm
java.lang.Object
org.moeaframework.algorithm.AbstractAlgorithm
- Direct Known Subclasses:
AbstractEvolutionaryAlgorithm
,AbstractPSOAlgorithm
,AbstractSimulatedAnnealingAlgorithm
,CMAES
,MOEAD
,RandomSearch
,RepeatedSingleObjective
Abstract class providing default implementations for several
Algorithm
methods.
When creating a new subclass, one should:
- Use the
evaluate(org.moeaframework.core.Solution)
orevaluateAll(java.lang.Iterable<org.moeaframework.core.Solution>)
methods provided by this class. Do not callProblem.evaluate(org.moeaframework.core.Solution)
directly as that will not count the number of function evaluations correctly. - When possible, prefer evaluating all solutions at once by calling
evaluateAll(java.lang.Iterable<org.moeaframework.core.Solution>)
. Doing so allows function evaluations to run in parallel when enabled (seeExecutor#distributeOnAllCores()
). - Implement the algorithm by overriding the
initialize()
anditerate()
methods.
-
Field Summary
Modifier and TypeFieldDescriptionprotected boolean
protected int
The number of times theevaluate(org.moeaframework.core.Solution)
method was invoked.protected final Problem
The problem being solved.protected boolean
-
Constructor Summary
ConstructorDescriptionAbstractAlgorithm
(Problem problem) Constructs an abstract algorithm for solving the specified problem. -
Method Summary
Modifier and TypeMethodDescriptionvoid
Throws an exception if the algorithm is initialized.void
Evaluates the specified solution for the problem being solved by this algorithm.void
evaluateAll
(Iterable<Solution> solutions) Evaluates the specified solutions.void
evaluateAll
(Solution[] solutions) Evaluates the specified solutions.int
Returns the number of times theevaluate
method was invoked.Returns the problem being solved by this algorithm.protected void
Performs any initialization that is required by this algorithm.boolean
boolean
Returnstrue
if this algorithm is terminated;false
otherwise.protected abstract void
iterate()
Performs one iteration of the algorithm.void
loadState
(ObjectInputStream stream) Loads the state of this object from the stream.void
saveState
(ObjectOutputStream stream) Writes the state of this object to the stream.void
step()
This method first checks if the algorithm is initialized.void
Implementations should always invokesuper.terminate()
to ensure the hierarchy is terminated correctly.
-
Field Details
-
problem
The problem being solved. -
numberOfEvaluations
protected int numberOfEvaluationsThe number of times theevaluate(org.moeaframework.core.Solution)
method was invoked. -
initialized
protected boolean initialized -
terminated
protected boolean terminated
-
-
Constructor Details
-
AbstractAlgorithm
Constructs an abstract algorithm for solving the specified problem.- Parameters:
problem
- the problem being solved
-
-
Method Details
-
evaluateAll
Evaluates the specified solutions. This method callsevaluate(Solution)
on each of the solutions. Subclasses should prefer calling this method overevaluate
whenever possible, as this ensures the solutions can be evaluated in parallel.- Parameters:
solutions
- the solutions to evaluate
-
evaluateAll
Evaluates the specified solutions. This method is equivalent toevaluateAll(Arrays.asList(solutions))
.- Parameters:
solutions
- the solutions to evaluate
-
evaluate
Description copied from interface:Algorithm
Evaluates the specified solution for the problem being solved by this algorithm. -
getNumberOfEvaluations
public int getNumberOfEvaluations()Description copied from interface:Algorithm
Returns the number of times theevaluate
method was invoked. This is the primary measure of runtime for optimization algorithms.- Specified by:
getNumberOfEvaluations
in interfaceAlgorithm
- Returns:
- the number of times the
evaluate
method was invoked
-
getProblem
Description copied from interface:Algorithm
Returns the problem being solved by this algorithm.- Specified by:
getProblem
in interfaceAlgorithm
- Returns:
- the problem being solved by this algorithm
-
initialize
protected void initialize()Performs any initialization that is required by this algorithm. This method is called automatically on the first invocation ofstep()
. Implementations should always invokesuper.initialize()
to ensure the algorithm is initialized correctly.- Throws:
AlgorithmInitializationException
- if the algorithm has already been initialized
-
isInitialized
public boolean isInitialized()- Returns:
true
if theinitialize()
method has been invoked;false
otherwise
-
assertNotInitialized
public void assertNotInitialized()Throws an exception if the algorithm is initialized. Use this anywhere to check and fail if the algorithm is already initialized.- Throws:
AlgorithmInitializationException
- if the algorithm is initialized
-
step
public void step()This method first checks if the algorithm is initialized. If not, theinitialize()
method is invoked. Once initialized, all subsequent calls tostep
invokeiterate()
. Implementations should override theinitialize
anditerate
methods in preference to modifying this method.- Specified by:
step
in interfaceAlgorithm
- Throws:
AlgorithmTerminationException
- if the algorithm has already terminated
-
iterate
protected abstract void iterate()Performs one iteration of the algorithm. This method should be overridden by implementations to perform each logical iteration of the algorithm. -
isTerminated
public boolean isTerminated()Description copied from interface:Algorithm
Returnstrue
if this algorithm is terminated;false
otherwise.- Specified by:
isTerminated
in interfaceAlgorithm
- Returns:
true
if this algorithm is terminated;false
otherwise- See Also:
-
terminate
public void terminate()Implementations should always invokesuper.terminate()
to ensure the hierarchy is terminated correctly.- Specified by:
terminate
in interfaceAlgorithm
- Throws:
AlgorithmTerminationException
- if the algorithm has already terminated
-
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
-