pygx.algo.mutfun¶
General mutable functions for evolution and symbolic regression.
mutfun
¶
General mutable functions for evolution and symbolic regression.
While users can use hyper primitives such as pg.oneof to fine-tune specific
parts of pre-existing programs, there are other use cases where it's necessary
to create a program from scratch or transform an existing program into something
drastically different. For example, one may need to find an activation formula
for neural networks or symbolically regress a function with only a few
observations. To address these needs, pg.algo.mutfun has been introduced.
pg.algo.mutfun provides functions and instructions represented by symbolic
objects that allow for maximum flexibility in manipulating a program.
This includes but is not limited to inserting new lines, deleting existing ones,
replacing operations, and creating new functions. With pg.algo.mutfun, users have
access to APIs that make these tasks more manageable. They can easily identify
all downstream instructions that depend on the current instruction, or all
upstream instructions that the current instruction depends on, or finding out
all defined variables up to current instruction.
A mutfun program is a Function object
illustrated as below::
f = pg.algo.mutfun.Function('f', [ pg.algo.mutfun.Assign('y', pg.algo.mutfun.Var('x') + 1) pg.algo.mutfun.Assign('z', pg.algo.mutfun.Var('x') ** 2) pg.algo.mutfun.Var('y') * pg.algo.mutfun.Var('z') ], args=['x'])
assert f(2) == (2 + 1) * 2 ** 2 print(f)
def f(x): y = x + 1 z = x ** 2 return y + z
:doc:../../../notebooks/evolution/function_regression provides an example for
evolving and doing symbolic regression on mutable functions.
Class hierarchy:
.. graphviz:: :align: center
digraph codetypes {
node [shape="box"];
edge [arrowtail="empty" arrowhead="none" dir="back" style="dashed"];
code [label="Code" href="code.html"]
symbol_def [label="SymbolDefinition" href="symbol_definition.html"];
assign [label="Assign" href="assign.html"];
function [label="Function" href="function.html"];
instruction [label="Instruction" href="instruction.html"];
symbol_ref [label="SymbolReference" href="symbol_reference.html"];
var [label="Var" href="var.html"];
function_call [label="FunctionCall" href="function_call.html"]
operator [label="Operator", href="operator.html"]
unary_operator [label="UnaryOperator", href="unary_operator.html"]
binary_operator [label="BinaryOperator", href="binary_operator.html"]
user_defined [label="<User-defined instructions>"]
code -> symbol_def;
code -> instruction;
symbol_def -> assign;
symbol_def -> function;
instruction -> symbol_ref;
instruction -> operator;
instruction -> user_defined;
symbol_ref -> var;
symbol_ref -> function_call;
operator -> unary_operator;
operator -> binary_operator;
function -> code [arrowtail="diamond" style="none" label="body"];
}
Assign
¶
Code
¶
Bases: Object
Interface for code entity.
eq=False lets Code instances be stored in dict/set by their ID.
pg.eq/pg.ne remain available for symbolic comparison.
evaluate
abstractmethod
¶
python_repr
abstractmethod
¶
format
¶
Overrides pg.Symbolic.format to support Python program representation.
Source code in pygx/algo/mutfun/_base.py
parent_code
¶
parent_code() -> Optional[Code]
Returns the parent code entity of current code entity.
parent_func
¶
parent_func() -> Optional[Function]
Returns the parent function of current code entity.
line
¶
line() -> Code
Returns the top-level code entity of current line.
Source code in pygx/algo/mutfun/_base.py
line_number
¶
Returns the 0-based line number of current line within its function.
preceding_lines_reversed
¶
preceding_lines_reversed() -> Iterable[Code]
Iterates the preceding lines in reversed order (recent first).
Source code in pygx/algo/mutfun/_base.py
succeeding_lines
¶
succeeding_lines() -> Iterable[Code]
Iterates the top-level instructions of succeeding lines.
Source code in pygx/algo/mutfun/_base.py
input_vars
¶
Returns the input context from this code entity.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
transitive
|
bool
|
If True, transitive input context will be included. |
False
|
Returns:
| Type | Description |
|---|---|
set[str]
|
A set of context. |
Source code in pygx/algo/mutfun/_base.py
output_vars
¶
Returns the output context from this instruction.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
transitive
|
bool
|
If True, transitive output context will be included. |
False
|
Returns:
| Type | Description |
|---|---|
set[str]
|
A set of output variable names. |
Source code in pygx/algo/mutfun/_base.py
seen_vars
¶
Returns seen context prior to this instruction.
Source code in pygx/algo/mutfun/_base.py
input_defs
¶
input_defs(transitive: bool = True) -> list[SymbolDefinition]
Returns the symbol definitions for the inputs of this code entity.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
transitive
|
bool
|
If True, transitive inputs will be included. Otherwise, only the direct dependencies will be included. |
True
|
Returns:
| Type | Description |
|---|---|
list[SymbolDefinition]
|
A list of |
list[SymbolDefinition]
|
the inputs required for current code entity. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If this entity is not inside a |
Source code in pygx/algo/mutfun/_base.py
output_refs
¶
output_refs(transitive: bool = True) -> list[SymbolReference]
Returns the references to the symbols that this code outputs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
transitive
|
bool
|
If True, transitive symbol references will be included. Otherwise, only the direct dependencies will be included. |
True
|
Returns:
| Type | Description |
|---|---|
list[SymbolReference]
|
A list of |
list[SymbolReference]
|
consume the outputs of current instruction. Users can use |
list[SymbolReference]
|
|
Source code in pygx/algo/mutfun/_base.py
compile
¶
Compiles current instruction.
Function
¶
Function(name: str, body: list[Code], args: list[str] | None = None, **kwargs)
Bases: SymbolDefinition
A function that contains a list of instructions.
Source code in pygx/algo/mutfun/_base.py
compile
¶
Compiles current function.
Source code in pygx/algo/mutfun/_base.py
prune
¶
Prune useless instructions.
Source code in pygx/algo/mutfun/_base.py
FunctionCall
¶
Instruction
¶
Bases: Code
Base class for all instructions.
parent_instruction
¶
parent_instruction() -> Optional[Instruction]
Returns the parent instruction of current instruction.
Source code in pygx/algo/mutfun/_base.py
select_types
classmethod
¶
select_types(
where: Callable[[type[Instruction]], bool] = lambda x: True,
) -> Iterable[type[Instruction]]
Selects all instruction types that match the condition.
Source code in pygx/algo/mutfun/_base.py
SymbolDefinition
¶
SymbolReference
¶
Var
¶
Add
¶
BinaryOperator
¶
Divide
¶
Equals
¶
FloorDivide
¶
GreaterThan
¶
LessThan
¶
Mod
¶
Multiply
¶
Negate
¶
NotEquals
¶
Operator
¶
Bases: Instruction
Base class for operators.
maybe_parenthesize
¶
Maybe add parenthesis to a child expression.
Source code in pygx/algo/mutfun/_basic_ops.py
Power
¶
Substract
¶
UnaryOperator
¶
evaluate
¶
python_repr
¶
options: show_root_heading: false show_root_toc_entry: false members_order: source heading_level: 2