Class AbstractAlgorithm

  • All Implemented Interfaces:
    org.moeaframework.core.Algorithm
    Direct Known Subclasses:
    AEvolutionaryAlgorithm

    public abstract class AbstractAlgorithm
    extends java.lang.Object
    implements org.moeaframework.core.Algorithm
    Abstract class providing default implementations for several Algorithm methods. All method extending this class must use the evaluate(org.moeaframework.core.Solution) method to evaluate a solution. This is mandatory to ensure the getNumberOfEvaluations() method returns the correct result.

    Subclasses should avoid overriding the step() method and instead override the initialize() and iterate() methods individually.

    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      protected AbstractAlgorithm​(org.moeaframework.core.Problem problem)
      Constructs an abstract algorithm for solving the specified problem.
    • Method Summary

      All Methods Instance Methods Abstract Methods Concrete Methods 
      Modifier and Type Method Description
      void evaluate​(org.moeaframework.core.Solution solution)  
      void evaluateAll​(java.lang.Iterable<org.moeaframework.core.Solution> solutions)
      Evaluates the specified solutions.
      void evaluateAll​(org.moeaframework.core.Solution[] solutions)
      Evaluates the specified solutions.
      int getNumberOfEvaluations()  
      org.moeaframework.core.Problem getProblem()  
      java.io.Serializable getState()  
      protected void initialize()
      Performs any initialization that is required by this algorithm.
      boolean isInitialized()
      Returns true if the initialize() method has been invoked; false otherwise.
      boolean isTerminated()  
      protected abstract void iterate()
      Performs one iteration of the algorithm.
      void setState​(java.lang.Object state)  
      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.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
      • Methods inherited from interface org.moeaframework.core.Algorithm

        getResult
    • Field Detail

      • problem

        protected final org.moeaframework.core.Problem problem
        The problem being solved.
      • initialized

        protected boolean initialized
        true if the initialize() method has been invoked; false otherwise.
      • terminated

        protected boolean terminated
        true if the terminate() method has been invoked; false otherwise.
    • Constructor Detail

      • AbstractAlgorithm

        protected AbstractAlgorithm​(org.moeaframework.core.Problem problem)
        Constructs an abstract algorithm for solving the specified problem.
        Parameters:
        problem - the problem being solved
    • Method Detail

      • evaluateAll

        public void evaluateAll​(java.lang.Iterable<org.moeaframework.core.Solution> solutions)
        Evaluates the specified solutions. This method calls 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.
        Parameters:
        solutions - the solutions to evaluate
      • evaluateAll

        public void evaluateAll​(org.moeaframework.core.Solution[] solutions)
        Evaluates the specified solutions. This method is equivalent to evaluateAll(Arrays.asList(solutions)).
        Parameters:
        solutions - the solutions to evaluate
      • evaluate

        public void evaluate​(org.moeaframework.core.Solution solution)
        Specified by:
        evaluate in interface org.moeaframework.core.Algorithm
      • getNumberOfEvaluations

        public int getNumberOfEvaluations()
        Specified by:
        getNumberOfEvaluations in interface org.moeaframework.core.Algorithm
      • getProblem

        public org.moeaframework.core.Problem getProblem()
        Specified by:
        getProblem in interface org.moeaframework.core.Algorithm
      • initialize

        protected void initialize()
        Performs any initialization that is required by this algorithm. This method is called automatically by the first invocation of step(), but may also be called manually prior to any invocations of step. Implementations should always invoke super.initialize() to ensure the hierarchy is initialized correctly.
        Throws:
        org.moeaframework.algorithm.AlgorithmInitializationException - if the algorithm has already been initialized
      • isInitialized

        public boolean isInitialized()
        Returns true if the initialize() method has been invoked; false otherwise.
        Returns:
        true if the initialize() method has been invoked; false otherwise
      • step

        public void step()
        This method first checks if the algorithm is initialized. If not, the initialize() method is invoked. If initialized, all calls to step invoke iterate(). Implementations should override the initialize and iterate methods in preference to modifying this method.
        Specified by:
        step in interface org.moeaframework.core.Algorithm
        Throws:
        org.moeaframework.algorithm.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()
        Specified by:
        isTerminated in interface org.moeaframework.core.Algorithm
      • terminate

        public void terminate()
        Implementations should always invoke super.terminate() to ensure the hierarchy is terminated correctly. This method is automatically invoked during finalization, and need only be called directly if non-Java resources are in use.
        Specified by:
        terminate in interface org.moeaframework.core.Algorithm
        Throws:
        org.moeaframework.algorithm.AlgorithmTerminationException - if the algorithm has already terminated
      • getState

        public java.io.Serializable getState()
                                      throws java.io.NotSerializableException
        Specified by:
        getState in interface org.moeaframework.core.Algorithm
        Throws:
        java.io.NotSerializableException
      • setState

        public void setState​(java.lang.Object state)
                      throws java.io.NotSerializableException
        Specified by:
        setState in interface org.moeaframework.core.Algorithm
        Throws:
        java.io.NotSerializableException