pg.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.mutfun has been introduced.
pg.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.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.mutfun.Function('f',
[
pg.mutfun.Assign('y', pg.mutfun.Var('x') + 1)
pg.mutfun.Assign('z', pg.mutfun.Var('x') ** 2)
pg.mutfun.Var('y') * pg.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
Evolving Functions with pg.mutfun provides an example for evolving and doing symbolic regression on mutable functions.
Class hierarchy: