pg.evolution.recombinators.Permutation

Accessible via pg.evolution.recombinators.Permutation.

class Permutation(where=Any(k=1, seed=None), seed=None)[source]

Bases: pg.evolution.Recombinator

Base for recombinators that permutate the multi-choice subchoices.

A permutation recombinator operates on target permutation decision points. A permutation point is a multi-choice decision point that has distinct but unsorted subchoices, also with a num_choices equals to the number of its candidates. A permutation decision point can be created via pg.permutate or pg.manyof(k=len(candidates), candidates).

Example:

pg.Dict(x=pg.manyof(2, range(5)), y=pg.permutate(range(3)))

contains 1 permutation decision point, as the first pg.manyof has only 2 subchoices while the number of candidates is 5.

A permutation point is targeted if it’s included in the return value of the where function when it’s specified. By default, the where function is set to where.ANY, which returns 1 random point among all the permutation points in the search space. When users specify the where argument to select more than 1 permutation points, the original DNA from each parent will be merged with each permutation proposal to generate a child. There will be a multiply effect between the number of parents and the number of proposals for each permutation, but the proposals generated from different permutation points will not be multiplied. That being said, if there are N parents, N propoals per crossover and K crossovers (K=1 by default), the max number of children will be N * M * K. Most permutation recombinators (PMX, OX, CX, etc.) operates on 2 parents and produces 2 children in a single crossover, resulting N * M * K = 2 * 2 = 4 children.

For example, if DNA([0, 1, [1, 2, 3, 0]]) and DNA([2, 3, [0, 1, 2, 3]]) are recombinated on [1, 2, 3, 0] and [0, 1, 2, 3], which outputs [0, 2, 3, 1] and [3, 1, 2, 0] as recombined results. Then there will be 4 DNA in the output:

DNA([0, 1, [0, 2, 3, 1]])
DNA([0, 1, [3, 1, 2, 0]])
DNA([2, 3, [0, 2, 3, 1]])
DNA([2, 3, [3, 1, 2, 0]])

It’s worthy noting that though common permutation operations takes 2 parents, the Permutation base class is designed to support arbitrary number of parents, use the NUM_PARENTS property to specify intended parent number if subclass needs a fixed parent number.

Methods:

permutate(multi_choice_spec, parents)

"Permutate decisions for a multi_choice_spec.

recombine(parents, global_state, step)

Generate a list of child DNA based on the list of parents given.

abstract permutate(multi_choice_spec, parents)[source]

“Permutate decisions for a multi_choice_spec.

Return type:

List[List[int]]

recombine(parents, global_state, step)[source]

Generate a list of child DNA based on the list of parents given.

User should override this method with optional keyword arguments ‘global_state’ and ‘step’.

The parents DNA contains a metadata field ‘generation’, which is the generation of the parent DNA. If the Recombinator does not assign this field for the new child DNA, the child DNA will have the maximum generation from the parents plus 1.

Return type:

List[pg.DNA]

Parameters:
  • parents – Parent trials.

  • global_state – An AttributeDict object as the global state container, which is readable/writable during the operation.

  • step – Number of examples historically proposed, which can be used for determining a cross over schedule.

Returns:

A list of generated child DNA.