Extending algorithms

In jMetalPy, algorithms maintain a list of dependents or observers which are notified automatically after each iteration (think about event listeners). This is known as the observer pattern and can be used to extend the functionality of our algorithms by registering new observers.

For example, a basic observer will log the current number of evaluations, the objective(s) from the best individual in the population and the current computing time:

basic = BasicAlgorithmObserver(frequency=1.0)
algorithm.observable.register(observer=basic)

A progress bar observer will print a smart progress meter that increases, on each iteration, a fixed value (or step) until the maximum is reached:

max_evaluations = 25000

algorithm = GeneticAlgorithm(...)

progress_bar = ProgressBarObserver(max=max_evaluations)
algorithm.observable.register(progress_bar)

algorithm.run()

This will produce:

$ Progress:  50%|#####     | 12500/25000 [13:59<14:12, 14.66it/s]

A full list of all available observers can be found at the jmetal.util.observer module.

List of observers

class jmetal.util.observer.BasicObserver(frequency: int = 1)[source]

Bases: Observer

update(*args, **kwargs)[source]

Update method.

class jmetal.util.observer.PlotFrontToFileObserver(output_directory: str, step: int = 100, **kwargs)[source]

Bases: Observer

update(*args, **kwargs)[source]

Update method.

class jmetal.util.observer.PrintObjectivesObserver(frequency: int = 1)[source]

Bases: Observer

update(*args, **kwargs)[source]

Update method.

class jmetal.util.observer.ProgressBarObserver(max: int)[source]

Bases: Observer

update(*args, **kwargs)[source]

Update method.

class jmetal.util.observer.VisualizerObserver(reference_front: List[S] | None = None, reference_point: list | None = None, display_frequency: int = 1)[source]

Bases: Observer

update(*args, **kwargs)[source]

Update method.

class jmetal.util.observer.WriteFrontToFileObserver(output_directory: str)[source]

Bases: Observer

update(*args, **kwargs)[source]

Update method.

API

class jmetal.core.observer.Observable[source]

Bases: ABC

abstract deregister(observer)[source]
abstract deregister_all()[source]
abstract notify_all(*args, **kwargs)[source]
abstract register(observer)[source]

Register a virtual subclass of an ABC.

Returns the subclass, to allow usage as a class decorator.

class jmetal.core.observer.Observer[source]

Bases: ABC

abstract update(*args, **kwargs)[source]

Update method.