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:

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"];
 }

Classes

Functions