pg.evolution.Operation¶
Accessible via pg.evolution.Operation
.
- class Operation[source]¶
Bases:
pg.Object
Base class for evolutionary operations.
An evolutionary operation transforms a DNA list into another DNA list. This abstraction provides the flexibility to describe common evolutionary operations including but not limited to:
Selection: Select M parents from a population of size N.
Recombination: Generate M child candidates from N parents.
Mutation: Mutate the N child candidates one by one, each can produce one or K DNA, resulting in N * K child DNA.
Operations are compositional via the following operators:
x >> y: Pipeline x and y by passing the output of x to y as input. x + y: Concatenate the outputs of x and y, based on the same input. Resulted list could contain duplicates. x[m:n:p] Slice x's output from m (inclusive) to n (exclusive) with step p. x | y: Union the outputs of x and y based on the same input. Resulted list will not contain duplicated items. x & y: Intersect the outputs of x and y, based on the same input. Resulted list will not contain duplicated items. x - y: Remove elements from x's output which also appears in y's output, based on the same input. x ^ y: Keep items that are either in x's output or in y's output, but not both. Duplicated items in x's output or in y's output will be preserved. x * K: Repeat operation x for K times based on the same input, concatenate their outputs. x ** K: Pipeline operation x for K times, equivalent to `x >> .. >> x` (`x` appears K times in the formula). -x or ~x: Compute the inversion of x's output based on the input. Equivalent to `Identity() - x` x.if_true: Conditionally apply x if predicate returns True. x.if_false: Conditionally apply x if predicate returns False.
Besides, the following operations are commonly used during composition:
Identity() : Returns the input DNA list. Lambda( : Creates an Operation using lambda. lambda x: x[1:]) Choice([ : Perform x, y based on probabilities. (x, prob1), (y, prob2)]) Conditional( : Perform x if input length > 5, otherwise y. lambda x: len(x) > 5, x, y) UntilChange(x) : Repeat x until its output changes from the input. ElementWise( : Perform element-wise operation and concatenate. lambda x: sorted(x)) Flatten() : Flatten nested lists into a flat list. GlobalStateGetter( : Fetch global state by key 'species'. 'species') GlobalStateSetter( : Set global state by key 'species' from input. 'species')
The operand in compositional operations supports callable objects in signature (List[DNA], int) -> List[DNA]. For example:
pg.evolution.selectors.Last(50) >> (lambda x, s: x[:2])
Methods:
as_global_state
(key)Returns a global state setter based on the key.
call
(inputs, global_state[, step])Subclasses should override this method.
flatten
([max_level])Flatten output with a max level.
for_each
(op)For each element in the output perform the operation.
global_state
(key)Returns a global state getter based on the key.
if_false
(predicate)Conditionally applies current operation when predicate returns False.
if_true
(predicate)Conditionally applies current operation when predicate returns True.
set_global_state
(key, value)Set global state and return current output.
until_change
([max_attempts])Ensure current operation will change the inputs.
with_prob
(probability[, seed])With probability.
Attributes:
Retuns the input element type.
Returns a member method as operation.
Retuns the output element type.
- call(inputs, global_state, step=0)[source]¶
Subclasses should override this method.
The global_state and step are optional for the subclasses’ call signature.
- Return type:
- Parameters:
inputs – A list of values as inputs.
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 values as output of current operation.
- if_false(predicate)[source]¶
Conditionally applies current operation when predicate returns False.
- Parameters:
predicate – The predicate that takes the outputs from the previous operation as input, with optional keyword arguments global_state and step. Returns False if current operation needs to be enabled. Otherwise no operation will be performed.
- Returns:
A conditional operation.
- if_true(predicate)[source]¶
Conditionally applies current operation when predicate returns True.
- Parameters:
predicate – The predicate that takes the outputs from the previous operation as input, with optional keyword arguments global_state and step. Returns True if current operation needs to be enabled. Otherwise no operation will be performed.
- Returns:
A conditional operation.
- property input_element_type: None | Type[Any] | Tuple[Type[Any]][source]¶
Retuns the input element type. Subclasses can override.
- property operation_method: Callable[[...], List[Any]][source]¶
Returns a member method as operation.