bingo.symbolic_regression.agraph package#
Subpackages#
- bingo.symbolic_regression.agraph.evaluation_backend package
- bingo.symbolic_regression.agraph.simplification_backend package
- Submodules
- bingo.symbolic_regression.agraph.simplification_backend.automatic_simplification module
- bingo.symbolic_regression.agraph.simplification_backend.constant_folding module
- bingo.symbolic_regression.agraph.simplification_backend.expression module
- bingo.symbolic_regression.agraph.simplification_backend.interpreter module
- bingo.symbolic_regression.agraph.simplification_backend.optional_expression_modification module
- bingo.symbolic_regression.agraph.simplification_backend.simplification_backend module
- bingo.symbolic_regression.agraph.simplification_backend.simplify module
- Module contents
Submodules#
bingo.symbolic_regression.agraph.agraph module#
Acyclic graph representation of an equation.
This module contains most of the code necessary for the representation of an acyclic graph (linear stack) in symbolic regression.
Stack#
The stack is represented as Nx3 integer array. Where each row of the array corresponds to a single command with form:
Node |
Parameter 1 |
Parameter 2 |
Where the parameters are a reference to the result of previously executed commands (referenced by row number in the stack). The result of the last (N’th) command in the stack is the evaluation of the equation.
Note: Parameter values have special meaning for two of the nodes (0 and 1).
Nodes#
An integer to node mapping is how the command stack is parsed. The current map is outlined below.
Node |
Name |
Math |
---|---|---|
0 |
load p1’th column of x |
\(x_{p1}\) |
1 |
load p1’th constant |
\(c_{p1}\) |
2 |
addition |
\(p1 + p2\) |
3 |
subtraction |
\(p1 - p2\) |
4 |
multiplication |
\(p1 - p2\) |
5 |
division (not divide-by-zero protected) |
\(p1 / p2\) |
6 |
sine |
\(sin(p1)\) |
7 |
cosine |
\(cos(p1)\) |
8 |
exponential |
\(exp(p1)\) |
9 |
logarithm |
\(log(|p1|)\) |
10 |
power |
\(p1^{p2}\) |
11 |
absolute value |
\(|p1|\) |
12 |
square root |
\(sqrt(|p1|)\) |
13 |
safe |
\(|p1|^{p2}\) |
14 |
hyperbolic sine |
\(sinh(p1)\) |
15 |
hyperbolic cosine |
\(cosh(p1)\) |
- class bingo.symbolic_regression.agraph.agraph.AGraph(use_simplification=False, equation=None)#
Bases:
Equation
Acyclic graph representation of an equation.
AGraph is initialized with with empty command array and no constants.
- Parameters:
use_simplification (bool, optional) – Whether to use cas-simplification or not.
equation (equation str or sympy expression, optional) – An equation str or sympy expression to build the AGraph from.
- command_array#
The command stack associated with an equation. N is the number of commands in the stack. This is read-only.
- Type:
Nx3 numpy array of int.
- mutable_command_array#
The version of the command array that must be used if modifications are to be made.
- Type:
Nx3 numpy array of int.
- constants#
numeric constants that are used in the equation
- Type:
tuple of numeric
- property command_array#
acyclic graph stack.
Notes
Setting the command stack automatically resets fitness
- Type:
Nx3 array of int
- property constants#
The numerical constants in the equation.
- distance(other)#
Computes the distance to another AGraph
Distance is a measure of similarity of the two command_arrays
- Parameters:
other (AGraph) – The individual to which distance will be calculated
- Returns:
distance from self to individual
- Return type:
int
- property engine#
Identification of the code location
- evaluate_equation_at(x)#
Evaluate the AGraph equation.
evaluation of the AGraph equation at points x.
- Parameters:
x (MxD array of numeric.) – Values at which to evaluate the equations. D is the number of dimensions in x and M is the number of data points in x.
- Returns:
\(f(x)\)
- Return type:
Mx1 array of numeric
- evaluate_equation_with_local_opt_gradient_at(x)#
Evaluate AGraph and get its derivatives.
Evaluate the AGraph equation at x and get the gradient of constants. Constants are of length L.
- Parameters:
x (MxD array of numeric.) – Values at which to evaluate the equations. D is the number of dimensions in x and M is the number of data points in x.
- Returns:
\(f(x)\) and \(df(x)/dc_i\)
- Return type:
tuple(Mx1 array of numeric, MxL array of numeric)
- evaluate_equation_with_x_gradient_at(x)#
Evaluate AGraph and get its derivatives.
Evaluate the AGraph equation at x and the gradient of x.
- Parameters:
x (MxD array of numeric.) – Values at which to evaluate the equations. D is the number of dimensions in x and M is the number of data points in x.
- Returns:
\(f(x)\) and \(df(x)/dx_i\)
- Return type:
tuple(Mx1 array of numeric, MxD array of numeric)
- get_complexity()#
Calculate complexity of agraph equation.
- Returns:
number of utilized commands in stack
- Return type:
int
- get_formatted_string(format_, raw=False)#
Output a string description of the the AGraph in a given format.
- Parameters:
format (str) – The requested format of the equation. Options are “console”, “latex”, and “stack”.
raw (bool) – (optional) Output of the raw command array rather than the processed version. Default False.
- Returns:
Equation in specified form
- Return type:
str
- get_local_optimization_params()#
get parameters for local optimization
Count constants and set up for optimization
- Return type:
list
- get_number_local_optimization_params()#
Number of parameters for local optimization
Count constants and set up for optimization
- Returns:
Number of constants that need to be optimized
- Return type:
int
- get_utilized_commands()#
Find which commands are utilized.
Find the commands in the command array of the AGraph upon which the last command relies. This is inclusive of the last command.
- Returns:
Boolean values for whether each command is utilized.
- Return type:
list of bool of length N
- property mutable_command_array#
acyclic graph stack.
Notes
Setting the command stack automatically resets fitness
- Type:
Nx3 array of int
- needs_local_optimization()#
The AGraph needs local optimization.
Find out whether constants need optimization.
- Returns:
Constants need optimization
- Return type:
bool
- set_local_optimization_params(params)#
Set the local optimization parameters.
Manually set optimized constants.
- Parameters:
params (list of numeric) – Values to set constants
- bingo.symbolic_regression.agraph.agraph.force_use_of_python_backends()#
When c++ backends are available, this can be used to force the use of python backends
- bingo.symbolic_regression.agraph.agraph.force_use_of_python_simplification()#
When c++ simplification is available, this can be used to force the use of python simplification
bingo.symbolic_regression.agraph.component_generator module#
Component Generator for Agraph equations.
This module covers the random generation of components of an Acyclic graph command stack. It can generate full commands or sub-components such as operators, terminals, and their associated parameters.
- class bingo.symbolic_regression.agraph.component_generator.ComponentGenerator(input_x_dimension, num_initial_load_statements=1, terminal_probability=0.1, constant_probability=None)#
Bases:
object
Generates commands or components of a command for an agraph stack
- Parameters:
input_x_dimension (int) – number of independent variables
num_initial_load_statements (int) – number of commands at the beginning of stack which are required to be terminals. Default 1
terminal_probability (float [0.0-1.0]) – probability that a new node will be a terminal. Default 0.1
constant_probability (float [0.0-1.0] (optional)) – probability that a new terminal will be a constant
- input_x_dimension#
number of independent variables
- Type:
int
- add_operator(operator_to_add, operator_weight=None)#
Add an operator number to the set of possible operators
- Parameters:
operator_to_add (int, str) – operator integer code (e.g. 2, 3) defined in Agraph operator maps or an operator string description (e.g. “+”, “addition”)
operator_weight (number) – relative weight of operator probability
- get_number_of_operators()#
Gets number of possible operators
- Returns:
number of operators
- Return type:
int
- get_number_of_terminals()#
Gets number of possible terminals
- Returns:
number of terminals
- Return type:
int
- random_command(stack_location)#
Get a random command
- Parameters:
stack_location (int) – location in the stack for the command
- Returns:
a random command in the form [node, parameter 1, parameter 2]
- Return type:
array of int
- random_operator()#
Get a random operator
Get a random operator from the list of possible operators.
- Returns:
an operator number
- Return type:
int
- random_operator_command(stack_location)#
Get a random operator (non-terminal) command
- Parameters:
stack_location (int) – location in the stack for the command
- Returns:
a random command in the form [node, parameter 1, parameter 2]
- Return type:
array of int
- static random_operator_parameter(stack_location)#
Get random operator parameter
- Parameters:
stack_location (int) – location of command in stack
- Returns:
parameter to be used in an operator command
- Return type:
int
Notes
The returned random operator parameter is guaranteed to be less than stack_location.
- random_terminal()#
Get a random terminal
Get a random VARIABLE or CONSTANT terminal.
- Returns:
terminal number (VARIABLE or CONSTANT)
- Return type:
int
- random_terminal_command(_=None)#
Get a random terminal (non-operator) command
- Returns:
a random command in the form [node, parameter 1, parameter 2]
- Return type:
array of int
- random_terminal_parameter(terminal_number)#
Get random terminal parameter
- Parameters:
terminal_number (int) – terminal number for which random parameter should be generated
- Returns:
parameter to be used in a terminal command
- Return type:
int
bingo.symbolic_regression.agraph.crossover module#
Definition of crossover between two acyclic graph individuals
This module contains the implementation of single point crossover between acyclic graph individuals.
- class bingo.symbolic_regression.agraph.crossover.AGraphCrossover#
Bases:
Crossover
Crossover between acyclic graph individuals
- types#
an iterable of the possible crossover types
- Type:
iterable of str
- last_crossover_types#
the crossover type (or None) that happened to create the first child and second child, respectively
- Type:
tuple(str, str)
bingo.symbolic_regression.agraph.generator module#
Generator of acyclic graph individuals.
This module contains the implementation of the generation of random acyclic graph individuals.
- class bingo.symbolic_regression.agraph.generator.AGraphGenerator(agraph_size, component_generator, use_python=False, use_simplification=False)#
Bases:
Generator
Generates acyclic graph individuals
- Parameters:
agraph_size (int) – command array size of the generated acyclic graphs
component_generator (agraph.ComponentGenerator) – Generator of stack components of agraphs
bingo.symbolic_regression.agraph.mutation module#
Mutation of acyclic graph individuals.
This module contains the implementation of mutation for acyclic graph individuals, which is composed of 4 possible mutation strategies: command mutation, node mutation, parameter mutation and pruning.
- class bingo.symbolic_regression.agraph.mutation.AGraphMutation(component_generator, command_probability=0.2, node_probability=0.2, parameter_probability=0.2, prune_probability=0.2, fork_probability=0.2)#
Bases:
Mutation
Mutation of acyclic graph individual
Mutation of an agraph individual my modification of its command array. Mutation randomly takes one of the following 5 forms
command mutation: An entire command (row) of the command array is replaced by a new random one
node mutation: The node of a command is replaced by a random new one. Change from a terminal to operator (or reversed) will automatically have updated parameters to maintain consistency.
parameter mutation: The parameters of a command are randomly changed.
pruning: The command array is adjusted to remove an operator from the evaluation of the AGraph. Pruning a terminal does nothing.
forking: A random command is selected to become the child of a new command (e.g., X_0 -> sin(X_0)). Forking on an equation with no non-utilized commands does nothing.
- Parameters:
command_probability (float) – probability of command mutation. Default 0.2
node_probability (float) – probability of node mutation. Default 0.2
parameter_probability (float) – probability of parameter mutation. Default 0.2
prune_probability (float) – probability of pruning. Default 0.2
fork_probability (float) – probability of forking. Default 0.2
- types#
an iterable of the possible mutation types
- Type:
iterable of str
- last_mutation_type#
the last mutation type that happened (or None)
- Type:
str
Notes
The input probabilities are normalized if their sum is not equal to 1.
- Mutation can result in no change if, for instance,
a prune mutation is executed on a AGraph utilizing only a single terminal.
a parameter mutation occurs on a AGraph utilizing only a single constant.
bingo.symbolic_regression.agraph.operator_definitions module#
- bingo.symbolic_regression.agraph.operator_definitions.IS_ARITY_2_MAP#
A map of node number to boolean that states whether the node has arity 2 (as opposed to 1)
- Type:
dict {int: bool}
- bingo.symbolic_regression.agraph.operator_definitions.IS_TERMINAL_MAP#
A map of node number to boolean that states whether the node is a terminal
- Type:
dict {int: bool}
- bingo.symbolic_regression.agraph.operator_definitions.OPERATOR_NAMES#
A map of node number to common names for the node
- Type:
dict{int: list(string)}
bingo.symbolic_regression.agraph.string_generation module#
- bingo.symbolic_regression.agraph.string_generation.STACK_PRINT_MAP#
A map of node number to a format string for stack output
- Type:
dict {int: str}
- bingo.symbolic_regression.agraph.string_generation.LATEX_PRINT_MAP#
A map of node number to a format string for latex output
- Type:
dict {int: str}
- bingo.symbolic_regression.agraph.string_generation.SYMPY_PRINT_MAP#
A map of node number to a format string for sympy output
- Type:
dict {int: str}
- bingo.symbolic_regression.agraph.string_generation.CONSOLE_PRINT_MAP#
A map of node number to a format string for console output
- Type:
dict {int: str}
- bingo.symbolic_regression.agraph.string_generation.get_formatted_string(eq_format, command_array, constants)#
Builds a formatted string from command array and constants
- Parameters:
eq_format (str) – “stack”, “latex”, “sympy”, or “console”
command_array (Nx3 array of int) – stack representation of an equation
constants (list(float)) – list of numerical constants in the equation
- Returns:
equation formatted in the way specified
- Return type:
str
bingo.symbolic_regression.agraph.string_parsing module#
Tools for parsing strings into AGraphs
- bingo.symbolic_regression.agraph.string_parsing.eq_string_to_command_array_and_constants(eq_string)#
Converts an equation string to its corresponding command array and list of constants
- Parameters:
eq_string (str) – A string corresponding to an equation
- Returns:
command_array, constants – A command array and list of constants corresponding to the expression given by eq_string
- Return type:
Nx3 numpy array of int, list of numeric
- bingo.symbolic_regression.agraph.string_parsing.eq_string_to_infix_tokens(eq_string)#
Converts an equation string to infix_tokens
- Parameters:
eq_string (str) – A string corresponding to an equation
- Returns:
infix_tokens – A list of string tokens that correspond to the expression given by eq_string
- Return type:
list of str
- bingo.symbolic_regression.agraph.string_parsing.infix_to_postfix(infix_tokens)#
Converts a list of infix tokens into its corresponding list of postfix tokens (e.g. [“a”, “+”, “b”] -> [“a”, “b”, “+”])
Based on the Dijkstra’s Shunting-yard algorithm
- Parameters:
infix_tokens (list of str) – A list of infix string tokens
- Returns:
postfix_tokens – A list of postfix string tokens corresponding to the expression given by infix_tokens
- Return type:
list of str
- bingo.symbolic_regression.agraph.string_parsing.postfix_to_command_array_and_constants(postfix_tokens)#
Converts a list of postfix tokens to its corresponding command array and list of constants
- Parameters:
postfix_tokens (list of str) – A list of postfix string tokens
- Returns:
command_array, constants – A command array and list of constants corresponding to the expression given by the postfix_tokens
- Return type:
Nx3 numpy array of int, list of numeric