-
Notifications
You must be signed in to change notification settings - Fork 31
/
postprocessing.py
160 lines (138 loc) · 6.37 KB
/
postprocessing.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
"""Postprocess the graph evolved by CGP.
- Simplify the obtained math formula.
- Visualize the expression tree corresponding to the formula that is embedded in the CGP graph.
Reference
1. [`geppy.support.simplification`](https://geppy.readthedocs.io/en/latest/geppy.support.html#module-geppy.support.simplification).
2. [Binary expression tree](https://en.wikipedia.org/wiki/Binary_expression_tree). Note that the operators are not required
to be binary though.
"""
import cgp
import sympy as sp
import operator
import math
from typing import Dict, Sequence
import networkx as nx
from settings import *
# Map Python functions to sympy counterparts for symbolic simplification.
DEFAULT_SYMBOLIC_FUNCTION_MAP = {
operator.and_.__name__: sp.And,
operator.or_.__name__: sp.Or,
operator.not_.__name__: sp.Not,
operator.add.__name__: operator.add,
operator.sub.__name__: operator.sub,
operator.mul.__name__: operator.mul,
operator.neg.__name__: operator.neg,
operator.pow.__name__: operator.pow,
operator.abs.__name__: operator.abs,
operator.floordiv.__name__: operator.floordiv,
operator.truediv.__name__: operator.truediv,
'protected_div': operator.truediv,
math.log.__name__: sp.log,
math.sin.__name__: sp.sin,
math.cos.__name__: sp.cos,
math.tan.__name__: sp.tan
}
def extract_computational_subgraph(ind: cgp.Individual) -> nx.MultiDiGraph:
"""Extract a computational subgraph of the CGP graph `ind`, which only contains active nodes.
Args:
ind (cgp.Individual): an individual in CGP
Returns:
nx.DiGraph: a acyclic directed graph denoting a computational graph
See https://www.deepideas.net/deep-learning-from-scratch-i-computational-graphs/ and
http://www.cs.columbia.edu/~mcollins/ff2.pdf for knowledge of computational graphs.
"""
# make sure that active nodes have been confirmed
if not ind._active_determined:
ind._determine_active_nodes()
ind._active_determined = True
# in the digraph, each node is identified by its index in `ind.nodes`
# if node i depends on node j, then there is an edge j->i
g = nx.MultiDiGraph() # possibly duplicated edges
for i, node in enumerate(ind.nodes):
if node.active:
f = ind.function_set[node.i_func]
g.add_node(i, func=f.name)
order = 1
for j in range(f.arity):
i_input = node.i_inputs[j]
w = node.weights[j]
g.add_edge(i_input, i, weight=w, order=order)
order += 1
return g
def simplify(g: nx.MultiDiGraph, input_names: Sequence = None, symbolic_function_map: Dict = None):
"""Compile computational graph `g` into a (possibly simplified) symbolic expression.
Args:
g (nx.MultiDiGraph): a computational graph
symbolic_function_map ([Dict], optional): Map each function to a symbolic one in `sympy`. Defaults to None.
If `None`, then the `DEFAULT_SYMBOLIC_FUNCTION_MAP` is used.
input_names (Sequence): a list of names, each for one input. If `None`, then a default name "vi" is used
for the i-th input.
Return:
a (simplified) symbol expression
For example, `add(sub(3, 3), x)` may be simplified to `x`. Note that this method is used to simplify the
**final** solution rather than during evolution.
"""
if symbolic_function_map is None:
symbolic_function_map = DEFAULT_SYMBOLIC_FUNCTION_MAP
# toplogical sort such that i appears before j if there is an edge i->j
ts = list(nx.topological_sort(g))
d = dict()
for node_id in ts:
if node_id < 0: # inputs in CGP
d[node_id] = sp.Symbol(
f"v{-node_id}" if input_names is None else input_names[-node_id - 1])
else: # a function node
inputs = []
# print(g.in_edges(node_id))
for input_node_id in g.predecessors(node_id):
# possibly parallel edges
for attr in g.get_edge_data(input_node_id, node_id).values():
inputs.append(
(input_node_id, attr["weight"], attr["order"]))
inputs.sort(key=operator.itemgetter(2))
args = (ip[1] * d[ip[0]] for ip in inputs)
func = g.nodes[node_id]["func"]
sym_func = symbolic_function_map[func]
r = sym_func(*args)
d[node_id] = sp.simplify(r) if PP_FORMULA_SIMPLIFICATION else r
# the unique output is the last node
return d[ts[-1]]
def round_expr(expr, num_digits):
# https://stackoverflow.com/questions/48491577/printing-the-output-rounded-to-3-decimals-in-sympy
return expr.xreplace({n: round(n, num_digits) for n in expr.atoms(sp.Number)})
def visualize(g: nx.MultiDiGraph, to_file: str, input_names: Sequence = None, operator_map: Dict = None):
"""Visualize an acyclic graph `g`.
Args:
g (nx.MultiDiGraph): a graph
to_file (str): file path
input_names (Sequence, optional): a list of names, each for one input. If `None`, then a default name "vi" is used
for the i-th input. Defaults to None.
operator_map (Dict, optional): Denote a function by an operator symbol for conciseness. Defaults to None. If `None`,
then +-*/ are used.
"""
from networkx.drawing.nx_agraph import to_agraph
import pygraphviz
layout = 'dot'
# label each function node with an operator
if operator_map is None:
operator_map = {operator.add.__name__: '+',
operator.sub.__name__: '-',
operator.neg.__name__: '-',
operator.mul.__name__: '*',
"protected_div": '/'}
for n in g.nodes:
attr = g.nodes[n]
if n >= 0: # function node
if attr['func'] not in operator_map:
print(
f"Operator notation of '{attr['func']}'' is not available. The node id is shown instead.")
attr['label'] = operator_map.get(attr['func'], n)
if g.out_degree(n) == 0: # the unique output node
attr['color'] = 'red'
else: # input node
attr['color'] = 'green'
attr['label'] = input_names[-n -
1] if input_names is not None else f'v{-n}'
ag: pygraphviz.agraph.AGraph = to_agraph(g)
ag.layout(layout)
ag.draw(to_file)