Metaheuristic

This module contains the Metaheuristic class.

Created on Thu Sep 26 16:56:01 2019

@author: Jorge Mario Cruz-Duarte (jcrvz.github.io), e-mail: j.m.cruzduarte@ieee.org

A Metaheuristic object combines a problem, a population, and a sequence of search operators into an iterative optimisation procedure.

Note

Population and Operators are re-exported by this module for convenience but are fully documented in their own pages: Population and Search Operators.

Lifecycle:

  1. Initialise the population (apply_initialiser).

  2. At each iteration, apply every operator in sequence (apply_search_operator).

  3. After the run, retrieve the best solution with get_solution().

Quick usage:

from customhys import benchmark_func as bf
from customhys.metaheuristic import Metaheuristic

func = bf.Sphere(5)
prob = {
    "function": func.get_func_val,
    "is_constrained": True,
    "boundaries": func.get_search_range(),
}
ops = [
    ("random_search", {"scale": 0.01, "distribution": "uniform"}, "greedy"),
]
mh = Metaheuristic(prob, ops, num_agents=30, num_iterations=100)
mh.run()
pos, fit = mh.get_solution()
class customhys.metaheuristic.Metaheuristic(problem, search_operators=None, num_agents=30, num_iterations=100, initial_scheme='random', verbose=False)[source]

Bases: object

This is the Metaheuristic class, each object corresponds to a metaheuristic implemented with a sequence of search operators from op, and it is based on a population from Population.

Parameters:
  • num_agents (int)

  • num_iterations (int)

  • initial_scheme (str)

  • verbose (bool)

__init__(problem, search_operators=None, num_agents=30, num_iterations=100, initial_scheme='random', verbose=False)[source]

Create a population-based metaheuristic by employing different simple search operators.

Parameters:
  • problem (dict) –

    This is a dictionary containing the ‘function’ that maps a 1-by-D array of real values to a real value, ‘is_constrained’ flag that indicates the solution is inside the search space, and the ‘boundaries’ (a tuple with two lists of size D). These two lists correspond to the lower and upper limits of domain, such as: boundaries = (lower_boundaries, upper_boundaries)

    Note: Dimensions (D) of search domain are read from these boundaries. The problem can be obtained from the benchmark_func module.

  • search_operators (list) – A list of available search operators. These operators must correspond to those available in the operators module. This parameter is mandatory for metaheuristic implementations, for using parts of this class, these can be provided as a list of operators.

  • num_agents (int) – Optional. Number of agents or population size. The default is 30.

  • num_iterations (int) – Optional. Number of iterations or generations that the metaheuristic is going to perform. The default is 100.

  • initial_scheme (str)

  • verbose (bool)

Returns:

None.

apply_initialiser()[source]
apply_search_operator(perturbator, selector)[source]
run()[source]

Run the metaheuristic for solving the defined problem. :return: None.

set_finalisation_conditions(conditions)[source]
finaliser()[source]
get_solution()[source]

Deliver the last position and fitness value obtained after run the metaheuristic procedure. :returns: ndarray, float

reset_historicals()[source]

Reset the historical variables. :return: None.

update_historicals()[source]

Update the historical variables. :return: None.