Search Operators

This module contains a collection search operators extracted from several well-known metaheuristics from the literature. All the available operators are listed in __all__

Created on Tue Jan 7 14:54:31 2020

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

This module provides a library of 13 search operators extracted from well-known metaheuristics. Each operator is a function that modifies a Population object’s positions in place.

Operators are specified as 3-tuples for use in the Metaheuristic and Hyperheuristic classes:

(operator_name, parameters_dict, selector_name)

Example:

from customhys.population import Population
from customhys import operators as op

pop = Population(boundaries=([-5]*10, [5]*10), num_agents=30)
pop.initialise_positions("random")

# Apply a random-search perturbation
op.random_search(pop, scale=0.01, distribution="uniform")
customhys.operators.local_random_walk(pop, probability=0.75, scale=1.0, distribution='uniform')[source]

Apply the local random walk from Cuckoo Search (CS) to the population’s positions (pop.positions).

Parameters:
  • pop (population) – population. It is a population object.

  • probability (float) – optional. It is the probability of discovering an alien egg (change an agent’s position). The default is 0.75.

  • scale (float) – optional. It is the step scale. The default is 1.0.

  • distribution (str) – optional. It is the random distribution used to sample the stochastic variable. The default value is ‘uniform’.

Returns:

None.

Apply the random search from Random Search (RS) to the population’s positions (pop.positions).

:param population poppopulation.

It is a population object.

Parameters:
  • scale (float) – optional. It is the step scale. The default is 0.01.

  • distribution (str) – optional. It is the distribution used to perform the random search. The default is ‘uniform’.

Returns:

None.

customhys.operators.random_sample(pop)[source]

Apply the random_sample to the population’s positions (pop.positions). This operator has no memory.

Parameters:

pop (population) – population. It is a population object.

Returns:

None.

customhys.operators.random_flight(pop, scale=1.0, distribution='levy', beta=1.5)[source]

Apply the random flight from Random Search (RS) to the population’s positions (pop.positions).

:param population poppopulation.

It is a population object.

Parameters:
  • scale (float) – optional. It is the step scale. The default is 1.0.

  • distribution (str) – optional. It is the distribution to draw the random samples. The default is ‘levy’.

  • beta (float) –

    optional It is the distribution parameter between [1.0, 3.0]. This parameter only has sense when distribution=’levy’.

    The default is 1.5.

Returns:

None.

customhys.operators.differential_mutation(pop, expression='current-to-best', num_rands=1, factor=1.0)[source]

Apply the differential mutation from Differential Evolution (DE) to the population’s positions (pop.positions).

Parameters:
  • pop (population) – population. It is a population object.

  • expression (str) –

    optional. Type of DE mutation. Available mutations: ‘rand’, ‘best’, ‘current’, ‘current-to-best’, ‘rand-to-best’,

    ’rand-to-best-and-current’. The default is ‘current-to-best’.

  • num_rands (int) – optional. Number of differences between positions selected at random. The default is 1.

  • factor (float) – optional. Scale factor (F) to weight contributions from other agents. The default is 1.0.

Returns:

None.

customhys.operators.firefly_dynamic(pop, alpha=1.0, beta=1.0, gamma=100.0, distribution='uniform')[source]

Apply the firefly dynamic from Firefly algorithm (FA) to the population’s positions (pop.positions).

Parameters:
  • pop (population) – population. It is a population object.

  • alpha (float) – optional. Scale of the random value. The default is 1.0.

  • beta (float) – optional. Scale of the firefly contribution. The default is 1.0.

  • gamma (float) – optional. Light damping parameters. The default is 100.

  • distribution (str) – optional. Type of random number. Possible options: ‘gaussian’, ‘uniform’, and ‘levy’. The default is ‘uniform’.

Returns:

None.

customhys.operators.swarm_dynamic(pop, factor=1.0, self_conf=2.54, swarm_conf=2.56, version='constriction', distribution='uniform')[source]

Apply the swarm dynamic from Particle Swarm Optimisation (PSO) to the population’s positions (pop.positions).

Parameters:
  • pop (population) – population. It is a population object.

  • factor (float) – optional. Inertial or Kappa factor, depending of which PSO version is set. The default is 1.0.

  • self_conf (float) – optional. Self confidence factor. The default is 2.54.

  • swarm_conf (float) – optional. Swarm confidence factor. The default is 2.56.

  • version (str) – optional. Version of the Particle Swarm Optimisation strategy. It can be ‘constriction’ or ‘inertial’. The default is ‘constriction’.

  • distribution (str) – optional. Distribution to draw the random numbers. It can be ‘uniform’, ‘gaussian’, and ‘levy’.

Returns:

None.

Apply the gravitational search from Gravitational Search Algorithm (GSA) to the population’s positions (pop.positions).

:param population poppopulation.

It is a population object.

Parameters:
  • gravity (float) – optional. It is the initial gravitational value. The default is 1.0.

  • alpha (float) – optional. It is the gravitational damping ratio. The default is 0.02.

Returns:

None.

customhys.operators.central_force_dynamic(pop, gravity=0.001, alpha=0.01, beta=1.5, dt=1.0)[source]

Apply the central force dynamic from Central Force Optimisation (CFO) to the population’s positions (pop.positions).

Parameters:
  • pop (population) – population.

  • gravity (float) – optional. It is the gravitational constant. The default is 0.001.

  • alpha (float) – optional. It is the power mass factor. The default is 0.01.

  • beta (float) – optional. It is the power distance factor. The default is 1.5.

  • dt (float) – optional. It is the time interval between steps. The default is 1.0.

Returns:

None.

customhys.operators.spiral_dynamic(pop, radius=0.9, angle=22.5, sigma=0.1)[source]

Apply the spiral dynamic from Stochastic Spiral Optimisation (SSO) to the population’s positions (pop.positions).

Parameters:
  • pop (population) – population. It is a population object.

  • radius (float) – optional. It is the convergence rate. The default is 0.9.

  • angle (float) – optional. Rotation angle (in degrees). The default is 22.5 (degrees).

  • sigma (float) – optional. Variation of random radii. The default is 0.1. Note: if sigma equals 0.0, the operator corresponds to that from the Deterministic Spiral Algorithm.

Returns:

None.

customhys.operators.genetic_mutation(pop, scale=1.0, elite_rate=0.1, mutation_rate=0.25, distribution='uniform')[source]

Apply the genetic mutation from Genetic Algorithm (GA) to the population’s positions (pop.positions).

Parameters:
  • pop (population) – population. It is a population object.

  • scale (float) – optional. It is the scale factor of the mutations. The default is 1.0.

:param float elite_rateoptional.

It is the proportion of population to preserve. The default is 0.1.

Parameters:
  • mutation_rate (float) – optional. It is the proportion of population to mutate. The default is 0.25.

  • distribution (str) – optional. It indicates the random distribution that power the mutation. There are only two distribution available: ‘uniform’, ‘gaussian’, and ‘levy’. The default is ‘uniform’.

Returns:

None.

customhys.operators.genetic_crossover(pop, pairing='rank', crossover='blend', mating_pool_factor=0.4)[source]

Apply the genetic crossover from Genetic Algorithm (GA) to the population’s positions (pop.positions).

Parameters:
  • pop (population) – population. It is a population object.

  • pairing (str) – optional. It indicates which pairing scheme to employ. Pairing schemes available are: ‘cost’ (Roulette Wheel or Cost Weighting), ‘rank’ (Rank Weighting), ‘tournament’, ‘random’, and ‘even-odd’. When tournament is chosen, tournament size (tp) and probability (tp) can be encoded such as ‘tournament_{ts}_{tp}’, {ts} and {tp}. Writing only ‘tournament’ is similar to specify ‘tournament_3_100’. The default is ‘rank’.

  • crossover (str) –

    optional. It indicates which crossover scheme to employ. Crossover schemes available are: ‘single’, ‘two’, ‘uniform’, ‘blend’, and ‘linear’. Likewise ‘tournament’ pairing, coefficients of ‘linear’ can be enconded such as ‘linear_{coeff1}_{coeff2}’ where the offspring is determined as follows:

    offspring = coeff1 * father + coeff2 * mother

    The default is ‘blend’.

  • mating_pool_factor (float) – optional. It indicates the proportion of population to disregard. The default is 0.4.

Returns:

None.

customhys.operators.linear_system(pop, matrix=None, dt=0.1, offset='globalbest', noise=0.0)[source]
Linear-affine update using a Forward Euler approximation of the continuous-time system:

x’ = x @ A_i + c_i, with A_i = (M_i - I)/dt and c_i = b_i/dt

so that a forward-Euler step matches the discrete map x_{t+1} ≈ x_t @ M_i + b_i.

Supports per-agent matrices:
  • matrix.shape == (D, D): same matrix for all agents

  • matrix.shape == (A, D, D): distinct matrix per agent

The target equilibrium x* is chosen from offset (“subpopmean” or “globalbest”) and the affine term per agent is set to b_i = x* @ (I - M_i) (row-vector convention).