pg.evolution.recombinators.PointWise

Accessible via pg.evolution.recombinators.PointWise.

class PointWise(where=All())[source]

Bases: pg.evolution.Recombinator

Base class for point-wise recombinators.

A point-wise recombinator operates on decision points (abbr. points) from the search space one by one. It crossovers the values from the parents into the offspring’s decision on each target point. For those points that are not targeted, the decisions from the parents will be copied to the offsprings. Therefore, N parents can result in N children when at least 1 decision points are not applicable for crossover, otherwise 1 child will be produced.

Example:

parents = [pg.DNA([0, 1, 0.1, 0.2]), pg.DNA([2, 3, 0.5, 0.4])]
# `children` will be [pg.DNA(0, 1, 0.3, 0.3), pg.geno(2, 3, 0.3, 0.3)].
children = pg.evolution.recombinators.Average()(parents)

parents = [pg.DNA([0.1, 0.2]), pg.DNA([0.5, 0.4])]
# `children` will be [pg.DNA(0.3, 0.3)].
children = pg.evolution.recombinators.Average()(parents)

Targeted decision points are points that are applicable for current recombinator according to its semantics, as well as passing the where statement if it’s specified. The PointWise base class depends on the applicable_decision_points method to select all applicable points from the search space. If not overridden by subclasses, the method returns all decision points in the search space, with multi-choice folded into a single decision point. When the where argument is provided by the user, it further filters out unwanted decision points, which is useful when we want to limit the range of points for crossover.

Example:

search_space = pg.Dict(
    x=pg.oneof(range(5)),
    y=pg.oneof(range(5), hints='excluded'))
parents = [pg.DNA([0, 1]), pg.DNA([2, 3])]

# The children will be one of the following:
#   [DNA([0, 1]), DNA([2, 1])] and [DNA([0, 3]), DNA([2, 3])].
children = pg.evolution.recombinators.Uniform(
    where=lambda xs: [x for in xs if x.hints != 'excluded'])(parents)

Methods:

applicable_decision_points(dna_spec, ...)

Returns applicable decision points for this recombinator.

merge(decision_point, parent_decisions, ...)

Implementation of point-wise decision making.

recombine(parents, global_state, step)

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

applicable_decision_points(dna_spec, global_state, step)[source]

Returns applicable decision points for this recombinator.

The default behavior is to return all decision points in the search space, with multi-choice subchoices folded into a single decision point. Subclasses can override this method to select applicable points according to their semantics.

Return type:

List[pg.geno.DecisionPoint]

Parameters:
  • dna_spec – The root DNASpec.

  • global_state – An optional keyword argument as the global state. Subclass can omit.

  • step – An optional keyword argument as current step. Subclass can omit.

Returns:

A list of targeted decision points for point-wise recombination, which

will be further filtered by the where statement later.

abstract merge(decision_point, parent_decisions, global_state, step)[source]

Implementation of point-wise decision making.

Return type:

Union[int, List[int], float]

Parameters:
  • decision_point – Decision point for recombination.

  • parent_decisions – A list of parent’s decisions. Each item should be an int as an active single-choice decision, a list of int as active multi- choice decisions, a float as an active float decision, or None for inactive decision point (whose parent space is not chosen).

  • global_state – An optional keyword argument as the global state. Subclass can omit.

  • step – An optional keyword argument as the current step. Subclass can omit.

Returns:

An int, list of int or float as the decision made for the decision point.

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.