Shortcuts

macop.policies.classicals

Classical policies classes implementations

Classes

RandomPolicy(operators)

Policy class implementation which is used for select operator randomly from the operators list

class macop.policies.classicals.RandomPolicy(operators)[source]

Bases: macop.policies.base.Policy

Policy class implementation which is used for select operator randomly from the operators list

operators

{[Operator]} – list of selected operators for the algorithm

Example:

>>> import random
>>> random.seed(42)
>>> from macop.operators.discrete.crossovers import SimpleCrossover
>>> from macop.operators.discrete.mutators import SimpleMutation
>>> from macop.policies.classicals import RandomPolicy
>>>
>>> # create policy instance and select next operator to apply using policy
>>> policy = RandomPolicy([SimpleCrossover(), SimpleMutation()])
>>> operator = policy.select()
>>> type(operator).__name__
'SimpleCrossover'
apply(solution1, solution2=None)

Apply specific operator chosen to create new solution, compute its fitness and return solution

Parameters
  • solution1 – {Solution} – the first solution to use for generating new solution

  • solution2 – {Solution} – the second solution to use for generating new solution (in case of specific crossover, default is best solution from algorithm)

Returns

new generated solution

Return type

{Solution}

select()[source]

Select randomly the next operator to use

Returns

the selected operator

Return type

{Operator}

setAlgo(algo)
Keep into policy reference of the whole algorithm

The reason is to better manage the operator choices (use of rewards as example)

Parameters

algo – {Algorithm} – the algorithm reference runned