altbacken.core.annealing

Functions

invert_fitness_function(fitness_function)

Inverts the sign of the fitness function, converting maximization to minimization or vice versa.

invert_stop_condition(stop)

Inverts the stop condition, converting maximization to minimization or vice versa.

Classes

SimulatedAnnealing(fitness, temperature, ...)

SimulatedAnnealing class represents the simulated annealing optimization algorithm.

StopCondition(*args, **kwargs)

Protocol that defines a stop condition for an annealing process.

altbacken.core.annealing.builtin_random()

random() -> x in the interval [0, 1).

class altbacken.core.annealing.StopCondition(*args, **kwargs)[source]

Bases: Protocol, Generic

Protocol that defines a stop condition for an annealing process.

This protocol is used to determine whether a simulated annealing process should terminate, based on the current state of the annealing procedure. Implementations of this protocol must define the __call__ method to evaluate the termination condition.

None
__call__(state: AnnealingState) bool[source]

Callable object to determine the termination condition of an annealing process.

This function evaluates whether the annealing process should terminate upon being called with the current state. The decision logic is embedded within the function implementation, based on the properties and status of the provided current state.

Parameters:

state (AnnealingState[T]) – The current state of the annealing process, providing the necessary information to evaluate the termination condition.

Returns:

A boolean indicating whether the annealing process should terminate.

Return type:

bool

__init__(*args, **kwargs)
altbacken.core.annealing.invert_stop_condition(stop: StopCondition) StopCondition[source]

Inverts the stop condition, converting maximization to minimization or vice versa.

altbacken.core.annealing.invert_fitness_function(fitness_function: FitnessFunction) FitnessFunction[source]

Inverts the sign of the fitness function, converting maximization to minimization or vice versa.

class altbacken.core.annealing.SimulatedAnnealing(fitness: FitnessFunction, temperature: TemperatureFunction, neighbourhood: ~altbacken.core.neighbourhood.Neighbourhood, stop_condition: ~altbacken.core.annealing.StopCondition, energy: AcceptanceFunction, random: RandomNumberGenerator = <built-in method random of Random object>)[source]

Bases: Generic

SimulatedAnnealing class represents the simulated annealing optimization algorithm.

This class performs optimization based on the simulated annealing technique, which iteratively explores a solution space to find a globally optimal solution for a given problem. The process relies on various components, such as temperature scheduling, fitness evaluation, neighborhood exploration, stop conditions, and energy functions to simulate the annealing process for optimization problems.

fitness

A function that evaluates the fitness of a solution.

Type:

FitnessFunction[T]

temperature

A generator function that determines the temperature at each iteration.

Type:

TemperatureFunction

neighbourhood

A function that generates a neighboring solution for the current solution.

Type:

Neighbourhood[T]

stop_condition

A function that determines when to stop the annealing process.

Type:

StopCondition[T]

energy

A function that computes the probability of transitioning between solutions.

Type:

EnergyFunction

random

A random number generator function, defaults to a built-in generator.

Type:

RandomNumberGenerator

__init__(fitness: FitnessFunction, temperature: TemperatureFunction, neighbourhood: ~altbacken.core.neighbourhood.Neighbourhood, stop_condition: ~altbacken.core.annealing.StopCondition, energy: AcceptanceFunction, random: RandomNumberGenerator = <built-in method random of Random object>)[source]
property tracer: Tracer
property energy: AcceptanceFunction
simulate(initial: T) tuple[T, float][source]

Simulates an optimization process using simulated annealing.

This method performs an iterative optimization process (minimization) through a simulated annealing approach. A neighborhood function generates potential solutions, and fitness values are evaluated to determine solution quality. The process continues until a stopping condition is satisfied or a temperature generator is exhausted.

Note

The algorithm is designed for minimization. For maximization problems, use invert_fitness_function to wrap your fitness function.

Parameters:

initial (T) – The initial solution to start the optimization process.

Returns:

A tuple containing the best solution found and its corresponding fitness value.

Return type:

tuple[T, float]

Raises:
  • StopIteration – If the temperature generator is exhausted before meeting

  • the stopping condition.