Interface Extension

All Known Implementing Classes:
AdaptiveTimeContinuationExtension, CallbackExtension, CheckpointExtension, EpsilonProgressContinuationExtension, InstrumentedExtension, LoggingExtension, PeriodicExtension, RuntimeCollectorExtension

public interface Extension
Extensions are a flexible alternative to wrappers when extending or augmenting the functionality of an algorithm.

A "wrapper" is a class that encloses an object, typically by passing the nested object in the constructor. For example, here we wrap the NSGAII algorithm:

     Algorithm algorithm = new NSGAII();
     Wrapper wrapper = new Wrapper(algorithm);
     wrapper.run(10000);
 
 

On the other hand, an extension adds new functionality by plugging into specific "extension points":

     Algorithm algorithm = new NSGAII();
     algorithm.addExtension(new Extension());
     algorithm.run(10000);
 

There are uses for both, as highlighted below:

  1. Wrappers can extend or replace any non-final method of the nested object, whereas extensions can only extend functionality at specific locations. However, this also means extensions provide a well-defined "contract" for how they can modify an algorithm.
  2. Extensions can be added dynamically without impacting user code. For example, we could add a default logging extension to all algorithms.
  3. Extensions leave the original type unchanged, whereas with wrappers we need to work with the wrapper type. The wrapper often needs to either re-implement methods to call the underlying class, or expose the nested instance (e.g., algorithm.getNestedAlgorithm()).
  4. Extensions offer better encapsulation. With a wrapper, we potentially reference and call methods directly on the nested instance, bypassing the wrapper and leading to unexpected results.

All of the methods in this interface have default implementations that no-op. An extension can simply override the specific methods required.

  • Method Summary

    Modifier and Type
    Method
    Description
    default void
    Called when an algorithm is being initialized.
    default void
    onRegister(Algorithm algorithm)
    Called when this extension is registered with an algorithm.
    default void
    onStep(Algorithm algorithm)
    Called after each step of the algorithm.
    default void
    Called after the termination of an algorithm.
  • Method Details

    • onRegister

      default void onRegister(Algorithm algorithm)
      Called when this extension is registered with an algorithm. This can be used to perform any type checking or initialization.
      Parameters:
      algorithm - the algorithm associated with this extension
    • onStep

      default void onStep(Algorithm algorithm)
      Called after each step of the algorithm.
      Parameters:
      algorithm - the algorithm associated with this extension
    • onInitialize

      default void onInitialize(Algorithm algorithm)
      Called when an algorithm is being initialized.
      Parameters:
      algorithm - the algorithm associated with this extension
    • onTerminate

      default void onTerminate(Algorithm algorithm)
      Called after the termination of an algorithm.
      Parameters:
      algorithm - the algorithm associated with this extension