altbacken.external.stop

Classes

IterationThreshold(threshold)

Represents a stopping criterion based on a threshold iteration count.

SolutionImprovementThreshold(...)

Monitors the solution improvement status during an optimization process.

TemperatureThreshold(threshold)

Represents a temperature threshold for an annealing process.

TimeThreshold(threshold, timer, ...)

Represents a time-based threshold for tracking the progress of an annealing process.

class altbacken.external.stop.TemperatureThreshold(threshold: float)[source]

Bases: object

Represents a temperature threshold for an annealing process.

This class is used to determine whether a given annealing state’s temperature has fallen below a specified threshold. It helps in controlling the termination criteria for algorithms based on simulated annealing techniques.

threshold

Specifies the non-negative temperature value used as the limit for comparison.

Type:

float

__init__(threshold: float)[source]
__call__(state: AnnealingState) bool[source]

Call self as a function.

class altbacken.external.stop.IterationThreshold(threshold: int)[source]

Bases: object

Represents a stopping criterion based on a threshold iteration count.

This class is used to determine whether a process, such as an optimization algorithm, should stop based on the current iteration surpassing a predefined iteration threshold. It can be utilized as a callable object in these scenarios.

threshold

The iteration count threshold. The process stops when the current iteration reaches or exceeds this value.

Type:

int

__init__(threshold: int)[source]
__call__(state: AnnealingState) bool[source]

Call self as a function.

class altbacken.external.stop.SolutionImprovementThreshold(max_iterations_without_improvement: int)[source]

Bases: object

Monitors the solution improvement status during an optimization process.

This class is designed to track whether a predefined threshold of iterations without solution improvement is met. It can be used in annealing or other iterative optimization algorithms to determine when to terminate the process if sufficient improvement is not observed.

max_iterations_without_improvement

Maximum number of iterations allowed without any improvement in the solution before termination.

Type:

int

__init__(max_iterations_without_improvement: int)[source]
__call__(state: AnnealingState) bool[source]

Evaluate whether the stop condition is met.

Note

This class is stateful. Its internal counter resets automatically when state.iteration == 0, which is the case at the start of every simulate() call. Reusing the same instance across multiple simulate() calls is therefore safe. However, if you use this condition in a custom loop where the iteration counter does not start at 0, the reset will not trigger and the internal state will carry over from the previous run.

class altbacken.external.stop.TimeThreshold(threshold: ~datetime.timedelta | float, timer: ~collections.abc.Callable[[], ~datetime.datetime] = <built-in method now of type object>)[source]

Bases: object

Represents a time-based threshold for tracking the progress of an annealing process.

This class is used to determine whether a given amount of time has passed since the start of the annealing process.

__init__(threshold: ~datetime.timedelta | float, timer: ~collections.abc.Callable[[], ~datetime.datetime] = <built-in method now of type object>)[source]

Initializes the timer with a specified threshold and sets an end time based on this threshold.

Parameters:
  • threshold (timedelta | float) – Time duration for the threshold. If a float is provided, it is interpreted as seconds. It must be positive.

  • timer (Callable[[], datetime]) – A function that returns the current time. Defaults to datetime.now. Useful for deterministic testing.

Raises:

ValueError – If the given threshold is less than or equal to zero.

property threshold: timedelta

Provides access to the threshold value as a read-only property.

Returns:

The threshold value.

Return type:

timedelta

__call__(state: AnnealingState) bool[source]

Determines whether the annealing process should be terminated based on the current iteration or elapsed time.

This callable method evaluates two conditions. It resets the timer if the current iteration is the initial one (iteration 0) and ensures the optimization process continues. For all subsequent iterations, it checks if the current time has reached or surpassed the predetermined end time, determining if the termination condition has been met.

Parameters:

state (AnnealingState) – The current state of the annealing process, including its iteration count and other relevant details.

Returns:

True if the termination condition is satisfied, otherwise False.

Return type:

bool

Note

This class is stateful. The internal timer resets automatically when state.iteration == 0, which is the case at the start of every simulate() call. Reusing the same instance across multiple simulate() calls is therefore safe. However, if you use this condition in a custom loop where the iteration counter does not start at 0, the reset will not trigger and the timer will continue from where it left off.