Base Metaheuristic

class Metaheuristic(domain: Domain, fitness_function: Callable[[Solution], float], population_size=20, warmup_iterations: int = 0, distributed=False, log_dir: str = 'logs')

Bases: ABC

Abstract base class for metaheuristic algorithms.

Parameters:
  • domain (Domain) – The problem domain.

  • fitness_function (Callable[[Solution], float]) – Function to evaluate solutions.

  • population_size (int, optional) – The size of the population (default is 1).

  • distributed (bool, optional) – Whether to use distributed computation (default is False).

  • log_dir (str, optional) – Directory for logging (default is “logs”).

Variables:
  • domain (Domain) – The problem domain.

  • fitness_function (Callable[[Solution], float]) – Function to evaluate solutions.

  • population_size (int) – The size of the population.

  • distributed (bool) – Whether distributed computation is enabled.

  • logger (Optional[TensorBoardLogger]) – Logger instance for TensorBoard, if available.

  • current_iteration (int) – The current iteration of the algorithm.

  • best_solution (Optional[Solution]) – The best solution found so far.

  • current_solutions (List[Solution]) – The current population of solutions.

  • best_solution_fitnesses (List[float]) – List of fitness values of the best solutions per iteration.

pre_execution() None

Callback executed before algorithm execution starts. Override this method to add custom pre-execution setup.

abstractmethod initialize(num_solutions=10) Tuple[List[Solution], Solution]

Initialize the population/solutions for the metaheuristic. Must set self.current_solutions and self.best_solution.

Parameters:

num_solutions (int) – The number of solutions to initialize.

Returns:

A tuple containing the population and the best individual.

Return type:

Tuple[List[Solution], Solution]

pre_iteration() None

Callback executed before each iteration. Override this method to add custom pre-iteration processing.

abstractmethod iterate(solutions: List[Solution]) Tuple[List[Solution], Solution]

Execute one iteration of the metaheuristic.

Parameters:

solutions (List[Solution]) – The current population of solutions.

Returns:

A tuple containing the updated population and the best individual.

Return type:

Tuple[List[Solution], Solution]

stopping_criterion() bool

Check if the algorithm should stop. Override this method to implement custom stopping criteria.

Returns:

True if the algorithm should stop, False otherwise.

Return type:

bool

post_iteration() None

Callback executed after each iteration. Override this method to add custom post-iteration processing.

post_execution() None

Callback executed after algorithm execution completes. Override this method to add custom post-execution cleanup.

run() Solution

Execute the metaheuristic algorithm.

Returns:

The best solution found.

Return type:

Solution