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, all upstream instructions that the current
instruction depends on, or 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)
which prints:
The Function Regression notebook provides an example for evolving and doing symbolic regression on mutable functions.
Class hierarchy:
classDiagram
Code <|-- SymbolDefinition
Code <|-- Instruction
SymbolDefinition <|-- Assign
SymbolDefinition <|-- Function
Instruction <|-- SymbolReference
Instruction <|-- Operator
Instruction <|-- UserDefinedInstructions
SymbolReference <|-- Var
SymbolReference <|-- FunctionCall
Operator <|-- UnaryOperator
Operator <|-- BinaryOperator
Function *-- Code : 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
¶
preceding_lines() -> Iterable[Code]
Iterates the preceding lines (earliest first).
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 names of variables that this code entity depends on.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
transitive
|
bool
|
If True, variables that the direct inputs transitively depend on will be included. |
False
|
Returns:
| Type | Description |
|---|---|
set[str]
|
A set of input variable names. |
Source code in pygx/algo/mutfun/_base.py
output_vars
¶
Returns the names of variables that this code entity outputs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
transitive
|
bool
|
If True, variables computed from this entity's outputs by succeeding lines 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 the names of variables visible prior to this code entity.
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]
|
order that consume the outputs of current code entity. Users can |
list[SymbolReference]
|
use |
Source code in pygx/algo/mutfun/_base.py
compile
¶
Validates this code entity; raises ValueError on undefined vars.
Source code in pygx/algo/mutfun/_base.py
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
¶
Validates the function body; raises ValueError on undefined vars.
Source code in pygx/algo/mutfun/_base.py
prune
¶
Removes lines that don't contribute to the return value.
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
¶
Subtract
¶
UnaryOperator
¶
evaluate
¶
python_repr
¶
options: show_root_heading: false show_root_toc_entry: false members_order: source heading_level: 2