Class PeriodicAction

java.lang.Object
org.moeaframework.core.PeriodicAction
All Implemented Interfaces:
Algorithm, Stateful
Direct Known Subclasses:
AdaptiveTimeContinuation, Checkpoints, InstrumentedAlgorithm

public abstract class PeriodicAction extends Object implements Algorithm
Decorates an algorithm to perform some action periodically throughout the execution of the algorithm. Note that due to the underlying implementation of the algorithm, the action may be invoked less frequently than requested or not at all.
  • Field Details

    • algorithm

      protected final Algorithm algorithm
      The underlying algorithm being decorated.
    • frequency

      protected int frequency
      The frequency that the doAction() method is invoked.
    • frequencyType

      protected final PeriodicAction.FrequencyType frequencyType
      The type of frequency.
    • iteration

      protected int iteration
      The number of invocations of the Algorithm.step() method. Only used if the frequency type is STEPS.
    • lastInvocation

      protected int lastInvocation
      The last invocation doAction() was invoked, either as iterations or evaluations depending on the frequency type. A value of -1 indicates the run hasn't started yet.
  • Constructor Details

    • PeriodicAction

      public PeriodicAction(Algorithm algorithm, int frequency, PeriodicAction.FrequencyType frequencyType)
      Decorates an algorithm to perform a period action.
      Parameters:
      algorithm - the algorithm being decorated
      frequency - the frequency the doAction() method is invoked
      frequencyType - the type of frequency
  • Method Details

    • getAlgorithm

      protected Algorithm getAlgorithm()
      Returns the algorithm that is wrapped by this periodic action.
      Returns:
      the inner algorithm
    • getProblem

      public Problem getProblem()
      Description copied from interface: Algorithm
      Returns the problem being solved by this algorithm.
      Specified by:
      getProblem in interface Algorithm
      Returns:
      the problem being solved by this algorithm
    • getResult

      public NondominatedPopulation getResult()
      Description copied from interface: Algorithm
      Returns the current best-known result.
      Specified by:
      getResult in interface Algorithm
      Returns:
      the current best-known result
    • step

      public void step()
      Description copied from interface: Algorithm
      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.

      This method should not be invoked when Algorithm.isTerminated() returns true.

      Specified by:
      step in interface Algorithm
    • evaluate

      public void evaluate(Solution solution)
      Description copied from interface: Algorithm
      Evaluates the specified solution for the problem being solved by this algorithm.
      Specified by:
      evaluate in interface Algorithm
      Parameters:
      solution - the solution to be evaluated
      See Also:
    • getNumberOfEvaluations

      public int getNumberOfEvaluations()
      Description copied from interface: Algorithm
      Returns the number of times the evaluate method was invoked. This is the primary measure of runtime for optimization algorithms.
      Specified by:
      getNumberOfEvaluations in interface Algorithm
      Returns:
      the number of times the evaluate method was invoked
    • isTerminated

      public boolean isTerminated()
      Description copied from interface: Algorithm
      Returns true if this algorithm is terminated; false otherwise.
      Specified by:
      isTerminated in interface Algorithm
      Returns:
      true if this algorithm is terminated; false otherwise
      See Also:
    • terminate

      public void terminate()
      Description copied from interface: Algorithm
      Terminates this algorithm. Implementations should use this method to free any underlying resources; however, the Algorithm.getResult() and Algorithm.getNumberOfEvaluations() methods are still required to work after termination.
      Specified by:
      terminate in interface Algorithm
    • doAction

      public abstract void doAction()
      Invoked periodically by this class to perform some function. This function should not depend on the frequency that it is invoked, since it is not guaranteed that this method is invoked at any specific frequency and, in some cases, may not be invoked at all.
    • saveState

      public void saveState(ObjectOutputStream stream) throws IOException
      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 calling super.saveState(stream) followed by writing each field.
      Specified by:
      saveState in interface Stateful
      Parameters:
      stream - the stream
      Throws:
      IOException - if an I/O error occurred
    • loadState

      public void loadState(ObjectInputStream stream) throws IOException, ClassNotFoundException
      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 in Stateful.saveState(ObjectOutputStream).
      Specified by:
      loadState in interface Stateful
      Parameters:
      stream - the stream
      Throws:
      IOException - if an I/O error occurred
      ClassNotFoundException - if the stream referenced a class that is not defined