metaheuristic_designer.operators.branch_operator module#

Operator that randomly applies one operator from a list to each individual.

class BranchOperator(op_list, random_pick=True, name=None, encoding=None, rng=None, weights=None, p=None, **kwargs)[source]#

Bases: Operator

Operator that stochastically selects among several operators.

For each individual, one operator from op_list is chosen according to the configured method (random with given probability, or manually picked). This allows e.g. applying mutation with a certain probability while leaving the rest untouched.

Parameters:
op_listlist of Operator

The candidate operators.

random_pickbool, optional

Whether to pick an operator at random or by specifying an index (default True).

namestr, optional

Display name; defaults to "method(op_names)".

encodingEncoding, optional

Encoding applied to the genotype.

rngRNGLike, optional

Random number generator.

weights: VectorLike, optional

Weights of each operator when choosing at random.

pfloat, optional

Probability of selecting the first operator (default 0.5). Only applied when op_list has length 2 and no weights are specified.

**kwargs

Additional keyword arguments stored as schedulable parameters.

Attributes:
params

Access parameter values by attribute-style lookup.

Parameters:
  • op_list (Iterable[Operator])

  • random_pick (bool)

  • name (str)

  • encoding (Optional[Encoding])

  • rng (Optional[RNGLike])

  • weights (Optional[VectorLike])

  • p (float)

Methods

__call__(population)

Shorthand for evolve().

choose_index(idx)

Manually chooses the operator to use next

evolve(population)

Apply a random operator to each individual according to the branch method.

gather_params()

Collect parameters from this operator and all sub-operators.

get_params()

Return a copy of the current parameter dictionary.

get_state()

Gets the current state of the algorithm as a dictionary.

store_kwargs([progress])

Store keyword arguments and evaluate them at the given progress.

update(progress)

Update schedulable parameters and propagate to sub-operators.

update_kwargs([progress])

Add or replace parameters and immediately evaluate them.

gather_params()[source]#

Collect parameters from this operator and all sub-operators.

Return type:

dict

Returns:
dict

Flat dictionary with dotted keys.

evolve(population)[source]#

Apply a random operator to each individual according to the branch method.

Return type:

Population

Parameters:
populationPopulation

The current population.

Returns:
Population

The modified population.

Parameters:

population (Population)

choose_index(idx)[source]#

Manually chooses the operator to use next

Parameters:
idxint

Index of the operator in the list.

Parameters:

idx (ndarray[tuple[int], floating] | ndarray[tuple[int], integer] | ndarray[tuple[int], uint8 | bool] | number | float | int)

update(progress)[source]#

Update schedulable parameters and propagate to sub-operators.

Parameters:
progressfloat

Current progress of the algorithm (0-1).

Parameters:

progress (float)

get_state()[source]#

Gets the current state of the algorithm as a dictionary.

Return type:

dict

Returns:
state: dict

The complete state of the operator.