saealib.Termination

class saealib.Termination(*conditions)[source]

Bases: object

Termination class to determine when to stop the optimization process.

Accepts one or more callable conditions. Each condition receives an OptimizationContext and returns True when the process should terminate. The optimization stops when any condition evaluates to True.

Parameters:

*conditions (ConditionFunc or TerminationCondition) – One or more callable conditions. Each must accept an OptimizationContext and return bool.

Raises:
  • ValueError – If no conditions are provided.

  • TypeError – If any condition is not callable.

Examples

>>> termination = Termination(max_fe(2000))
>>> termination = Termination(max_fe(2000), max_gen(100))
>>> termination = Termination(
...     max_fe(2000),
...     lambda ctx: ctx.archive.get("f").min() < 1e-6,
... )
>>> termination = Termination.all_of(max_fe(2000), max_gen(100))
>>> termination = Termination.not_(max_gen(100))

Methods

__init__

all_of

Build a Termination that stops when all conditions are met (AND).

any_of

Build a Termination that stops when any condition is met (OR).

is_terminated

Check if the composed termination condition is met.

not_

Build a Termination that stops when condition is not met (NOT).

Method Details

Termination.__init__(*conditions)[source]
Parameters:

conditions (ConditionFunc | TerminationCondition)

classmethod Termination.all_of(*conditions)[source]

Build a Termination that stops when all conditions are met (AND).

Parameters:

*conditions (ConditionFunc or TerminationCondition) – One or more callable conditions.

Returns:

A termination combining the conditions with AND.

Return type:

Termination

Raises:

ValueError – If no conditions are provided.

classmethod Termination.any_of(*conditions)[source]

Build a Termination that stops when any condition is met (OR).

Equivalent to Termination(*conditions); provided for symmetry with all_of() and not_().

Parameters:

*conditions (ConditionFunc or TerminationCondition) – One or more callable conditions.

Returns:

A termination combining the conditions with OR.

Return type:

Termination

Termination.is_terminated(ctx)[source]

Check if the composed termination condition is met.

Parameters:

ctx (OptimizationContext) – The current optimization context.

Returns:

True if the termination condition is met, False otherwise.

Return type:

bool

classmethod Termination.not_(condition)[source]

Build a Termination that stops when condition is not met (NOT).

Parameters:

condition (ConditionFunc or TerminationCondition) – The condition to negate.

Returns:

A termination that negates the given condition.

Return type:

Termination