MetaGen
  • Understanding MetaGen
  • MetaGen in action
  • Performance tracking
  • Distributed capabilities
  • Advanced Topics
  • API Reference
    • Core Components
      • Domain
      • Solution
      • Metaheuristics
        • Base Metaheuristic
        • Basic genetic algorithm
        • Steady-state genetic algorithm
        • Random search
        • Simulated annealing
        • Tree-structured Parzen Estimator (TPE)
        • Tabu Search
        • CVOA - Coronavirus Optimization Algorithm
        • MM - Memetic Algorithm
      • Connector
    • Component Overview
    • Usage Guidelines
    • Extensibility
MetaGen
  • API Reference
  • Metaheuristics
  • Random search
  • View page source

Random search

class RandomSearch(domain: Domain, fitness_function: Callable[[Solution], float], population_size=10, max_iterations: int = 20, distributed=False, log_dir: str = 'logs/RS')

Bases: Metaheuristic

RandomSearch is a class for performing a random search optimization algorithm.

It generates and evaluates random solutions in a search space to find an optimal solution. The algorithm maintains a population of solutions and in each iteration, it mutates all solutions except the best one from the previous iteration.

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

  • fitness_function (Callable[[Solution], float]) – The fitness function used to evaluate solutions

  • population_size (int, optional) – The size of the population to maintain, defaults to 1

  • max_iterations (int, optional) – The maximum number of iterations to run, defaults to 20

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

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

Variables:

max_iterations (int) – Maximum number of iterations to run

Code example

from metagen.framework import Domain
from metagen.metaheuristics import RandomSearch

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

fitness_function = ...

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

Initialize the RandomSearch algorithm.

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

  • fitness_function (Callable[[Solution], float]) – The fitness function used to evaluate solutions

  • population_size (int, optional) – The size of the population to maintain, defaults to 1

  • max_iterations (int, optional) – The maximum number of iterations to run, defaults to 20

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

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

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

Initialize a population of random solutions.

Parameters:

num_solutions (int, optional) – Number of solutions to initialize, 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]

Execute one iteration of the random search algorithm.

In each iteration, the best solution from the previous iteration is preserved, while all other solutions are mutated and evaluated.

Parameters:

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

Returns:

A tuple containing the updated population and the best solution found

Return type:

Tuple[List[Solution], Solution]

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

Previous Next

© Copyright 2023, David Gutiérrez Avilés, José Francisco Torres, Manuel Jesús Jiménez-Navarro, and Francisco Martínez-Álvarez.

Built with Sphinx using a theme provided by Read the Docs.