pg.evolve

Accessible via pg.evolve, pg.hyper.evolve.

evolve(initial_value, node_transform, *, weights=None, name=None, hints=None)[source]

An evolvable symbolic value.

Example:

@pg.symbolize
@dataclasses.dataclass
class Foo:
  x: int
  y: int

@pg.symbolize
@dataclasses.dataclass
class Bar:
  a: int
  b: int

# Defines possible transitions.
def node_transform(location, value, parent):
  if isinstance(value, Foo)
    return Bar(value.x, value.y)
  if location.key == 'x':
    return random.choice([1, 2, 3])
  if location.key == 'y':
    return random.choice([3, 4, 5])

v = pg.evolve(Foo(1, 3), node_transform)

See also: :rtype: pg.hyper.Evolvable

Parameters:
  • initial_value – The initial value to evolve.

  • node_transform – A callable object that takes information of the value to operate (e.g. location, old value, parent node) and returns a new value as a replacement for the node. Such information allows users to not only access the mutation node, but the entire symbolic tree if needed, allowing complex mutation rules to be written with ease - for example - check adjacent nodes while modifying a list element. This function is designed to take care of both node replacements and node insertions. When insertion happens, the old value for the location will be pg.MISSING_VALUE. See pg.composing.SeenObjectReplacer as an example.

  • weights – An optional callable object that returns the unnormalized (e.g. the sum of all probabilities don’t have to sum to 1.0) mutation probabilities for all the nodes in the symbolic tree, based on (mutation type, location, old value, parent node), If None, all the locations and mutation types will be sampled uniformly.

  • name – An optional name of the decision point.

  • hints – An optional hints for the decision point.

Returns:

A pg.hyper.Evolvable object.