Tree-structured Parzen Estimator (TPE)

class TPE(domain: Domain, fitness_function: Callable[[Solution], float], max_iterations: int = 50, warmup_iterations: int = 10, candidate_pool_size: int = 24, gamma_config: GammaConfig | None = None, distributed=False, log_dir: str = 'logs/TPE')

Bases: Metaheuristic

Tree-structured Parzen Estimator (TPE) algorithm for optimization problems.

This class implements the TPE metaheuristic which uses kernel density estimation to model the probability of good and bad solutions. The algorithm iteratively samples new solutions from regions of the search space that are more likely to contain good solutions.

Parameters:
  • domain (Domain) – The problem domain that defines the solution space

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

  • population_size (int, optional) – Size of the population to maintain, defaults to 10

  • max_iterations (int, optional) – Maximum number of iterations to run, defaults to 50

  • gamma (float, optional) – Fraction of best solutions to consider when building models, defaults to 0.25

  • distributed (bool, optional) – Whether to use distributed computation, defaults to False

  • log_dir (str, optional) – Directory for logging, defaults to “logs/TPE”

Variables:
  • max_iterations (int) – Maximum number of iterations

  • gamma (float) – Fraction of best solutions for model building

Code example

from metagen.framework import Domain
from metagen.metaheuristics import TPE

domain = Domain()
domain.defineInteger(0, 1)

fitness_function = ...

search = TPE(domain, fitness_function, population_size=50, max_iterations=100)
optimal_solution = search.run()

Initialize the TPE algorithm.

Parameters:
  • domain (Domain) – The problem domain that defines the solution space

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

  • population_size (int, optional) – Size of the population to maintain, defaults to 10

  • max_iterations (int, optional) – Maximum number of iterations to run, defaults to 50

  • gamma (float, optional) – Fraction of best solutions to consider when building models, defaults to 0.25

  • distributed (bool, optional) – Whether to use distributed computation, defaults to False

  • log_dir (str, optional) – Directory for logging, defaults to “logs/TPE”

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

Initialize the population with random solutions.

Parameters:

num_solutions (int, optional) – Number of initial solutions to generate, defaults to 10

Returns:

A tuple containing the list of solutions and the best solution found

Return type:

Tuple[List[Solution], Solution]

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

Executes one iteration of the TPE algorithm while controlling solution history growth.

stopping_criterion() bool

Check if the algorithm should stop.

The algorithm stops when the current iteration reaches the maximum number of iterations.

Returns:

True if the maximum number of iterations is reached, False otherwise

Return type:

bool

post_iteration() None

Additional processing after each generation