Class AbstractAlgorithm
- All Implemented Interfaces:
Algorithm,Extensible,Named,Stateful
- Direct Known Subclasses:
AbstractEvolutionaryAlgorithm,AbstractPSOAlgorithm,AbstractSimulatedAnnealingAlgorithm,CMAES,MOEAD,RandomSearch,RepeatedSingleObjective
Algorithm methods.
When creating a new subclass, one should:
- Use the
evaluate(org.moeaframework.core.Solution)orAlgorithm.evaluateAll(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
Algorithm.evaluateAll(java.lang.Iterable<org.moeaframework.core.Solution>). Doing so allows function evaluations to run in parallel when enabled. - Implement the algorithm by overriding the
initialize()anditerate()methods.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected booleanprotected intThe number of times theevaluate(org.moeaframework.core.Solution)method was invoked.protected final ProblemThe problem being solved.protected boolean -
Constructor Summary
ConstructorsConstructorDescriptionAbstractAlgorithm(Problem problem) Constructs an abstract algorithm for solving the specified problem. -
Method Summary
Modifier and TypeMethodDescriptionvoidThrows an exception if the algorithm is initialized.voidEvaluates the specified solution for the problem being solved by this algorithm.Returns the extensions associated with the algorithm.intReturns the number of times theevaluatemethod was invoked.Returns the problem being solved by this algorithm.voidPerforms any initialization that is required by this algorithm.booleanReturnstrueif this algorithm has been initialized;falseotherwise.booleanReturnstrueif this algorithm has reached its termination condition;falseotherwise.protected abstract voiditerate()Performs one iteration of the algorithm.voidloadState(ObjectInputStream stream) Loads the state of this object from the stream.voidsaveState(ObjectOutputStream stream) Writes the state of this object to the stream.voidstep()Performs one logical step of this algorithm.voidCalled when the termination condition is reached and the run is complete.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.moeaframework.algorithm.Algorithm
evaluateAll, evaluateAll, getName, getResult, run, runMethods inherited from interface org.moeaframework.algorithm.extension.Extensible
addExtension, removeExtension
-
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
-
evaluate
Description copied from interface:AlgorithmEvaluates the specified solution for the problem being solved by this algorithm. -
getNumberOfEvaluations
public int getNumberOfEvaluations()Description copied from interface:AlgorithmReturns the number of times theevaluatemethod was invoked. This is the primary measure of runtime for optimization algorithms.- Specified by:
getNumberOfEvaluationsin interfaceAlgorithm- Returns:
- the number of times the
evaluatemethod was invoked
-
getProblem
Description copied from interface:AlgorithmReturns the problem being solved by this algorithm.- Specified by:
getProblemin interfaceAlgorithm- Returns:
- the problem being solved by this algorithm
-
initialize
public void initialize()Description copied from interface:AlgorithmPerforms any initialization that is required by this algorithm. This method should only be called once, though the specific implementation may choose to no-op or throwAlgorithmInitializationExceptionif called multiple times.Implementations should always call
super.initialize()to ensure the algorithm is initialized correctly.- Specified by:
initializein interfaceAlgorithm
-
isInitialized
public boolean isInitialized()Description copied from interface:AlgorithmReturnstrueif this algorithm has been initialized;falseotherwise.- Specified by:
isInitializedin interfaceAlgorithm- Returns:
trueif this algorithm has been initialized;falseotherwise- See Also:
-
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()Performs one logical step of this algorithm. The amount of work performed depends on the implementation. One invocation of this method may produce one or many trial solutions.In general, calling this method after
Algorithm.terminate()is permitted. When this happens,Algorithm.isTerminated()is reset. We recommend checkingAlgorithm.isTerminated()after each step to detect when termination conditions are reached. However, if the implementation is unable to continue, this method should throwAlgorithmTerminationException.Avoid overriding this method in subclasses. Instead, prefer overriding
initialize()anditerate()with any algorithm-specific details. -
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:AlgorithmReturnstrueif this algorithm has reached its termination condition;falseotherwise.- Specified by:
isTerminatedin interfaceAlgorithm- Returns:
trueif this algorithm has reached its termination condition;falseotherwise- See Also:
-
terminate
public void terminate()Description copied from interface:AlgorithmCalled when the termination condition is reached and the run is complete. This method is automatically called when usingAlgorithm.run(TerminationCondition), but can also be invoked directly if executing the algorithm step-by-step withAlgorithm.step().Implementations should always call
super.terminate()to ensure the algorithm is terminated correctly. -
getExtensions
Description copied from interface:ExtensibleReturns the extensions associated with the algorithm.- Specified by:
getExtensionsin interfaceExtensible- Returns:
- the extensions
-
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
-