-
Notifications
You must be signed in to change notification settings - Fork 116
/
program.py
684 lines (523 loc) · 23.5 KB
/
program.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
"""Class for symbolic expression object or program."""
import array
import warnings
from textwrap import indent
import numpy as np
from sympy.parsing.sympy_parser import parse_expr
from sympy import pretty
from dso.functions import PlaceholderConstant
from dso.const import make_const_optimizer
from dso.utils import cached_property
import dso.utils as U
def _finish_tokens(tokens):
"""
Complete a possibly unfinished string of tokens.
Parameters
----------
tokens : list of integers
A list of integers corresponding to tokens in the library. The list
defines an expression's pre-order traversal.
Returns
_______
tokens : list of ints
A list of integers corresponding to tokens in the library. The list
defines an expression's pre-order traversal. "Dangling" programs are
completed with repeated "x1" until the expression completes.
"""
n_objects = Program.n_objects
arities = np.array([Program.library.arities[t] for t in tokens])
# Number of dangling nodes, returns the cumsum up to each point
# Note that terminal nodes are -1 while functions will be >= 0 since arities - 1
dangling = 1 + np.cumsum(arities - 1)
if -n_objects in (dangling - 1):
# Chop off tokens once the cumsum reaches 0, This is the last valid point in the tokens
expr_length = 1 + np.argmax((dangling - 1) == -n_objects)
tokens = tokens[:expr_length]
else:
# Extend with valid variables until string is valid
# NOTE: This only appends onto the end of a set of tokens, even in the multi-object case!
assert n_objects == 1, "Is max length constraint turned on? Max length constraint required when n_objects > 1."
tokens = np.append(tokens, np.random.choice(Program.library.input_tokens, size=dangling[-1]))
return tokens
def from_str_tokens(str_tokens, skip_cache=False):
"""
Memoized function to generate a Program from a list of str and/or float.
See from_tokens() for details.
Parameters
----------
str_tokens : str | list of (str | float)
Either a comma-separated string of tokens and/or floats, or a list of
str and/or floats.
skip_cache : bool
See from_tokens().
Returns
-------
program : Program
See from_tokens().
"""
# Convert str to list of str
if isinstance(str_tokens, str):
str_tokens = str_tokens.split(",")
# Convert list of str|float to list of tokens
if isinstance(str_tokens, list):
traversal = []
constants = []
for s in str_tokens:
if s in Program.library.names:
t = Program.library.names.index(s.lower())
elif U.is_float(s):
assert "const" not in str_tokens, "Currently does not support both placeholder and hard-coded constants."
t = Program.library.const_token
constants.append(float(s))
else:
raise ValueError("Did not recognize token {}.".format(s))
traversal.append(t)
traversal = np.array(traversal, dtype=np.int32)
else:
raise ValueError("Input must be list or string.")
# Generate base Program (with "const" for constants)
p = from_tokens(traversal, skip_cache=skip_cache)
# Replace any constants
p.set_constants(constants)
return p
def from_tokens(tokens, skip_cache=False, on_policy=True, finish_tokens=True):
"""
Memoized function to generate a Program from a list of tokens.
Since some tokens are nonfunctional, this first computes the corresponding
traversal. If that traversal exists in the cache, the corresponding Program
is returned. Otherwise, a new Program is returned.
Parameters
----------
tokens : list of integers
A list of integers corresponding to tokens in the library. The list
defines an expression's pre-order traversal. "Dangling" programs are
completed with repeated "x1" until the expression completes.
skip_cache : bool
Whether to bypass the cache when creating the program (used for
previously learned symbolic actions in DSP).
finish_tokens: bool
Do we need to finish this token. There are instances where we have
already done this. Most likely you will want this to be True.
Returns
_______
program : Program
The Program corresponding to the tokens, either pulled from memoization
or generated from scratch.
"""
'''
Truncate expressions that complete early; extend ones that don't complete
'''
if finish_tokens:
tokens = _finish_tokens(tokens)
# For stochastic Tasks, there is no cache; always generate a new Program.
# For deterministic Programs, if the Program is in the cache, return it;
# otherwise, create a new one and add it to the cache.
if skip_cache or Program.task.stochastic:
p = Program(tokens, on_policy=on_policy)
else:
key = tokens.tostring()
try:
p = Program.cache[key]
if on_policy:
p.on_policy_count += 1
else:
p.off_policy_count += 1
except KeyError:
p = Program(tokens, on_policy=on_policy)
Program.cache[key] = p
return p
class Program(object):
"""
The executable program representing the symbolic expression.
The program comprises unary/binary operators, constant placeholders
(to-be-optimized), input variables, and hard-coded constants.
Parameters
----------
tokens : list of integers
A list of integers corresponding to tokens in the library. "Dangling"
programs are completed with repeated "x1" until the expression
completes.
Attributes
----------
traversal : list
List of operators (type: Function) and terminals (type: int, float, or
str ("const")) encoding the pre-order traversal of the expression tree.
tokens : np.ndarry (dtype: int)
Array of integers whose values correspond to indices
const_pos : list of int
A list of indicies of constant placeholders along the traversal.
float_pos : list of float
A list of indices of constants placeholders or floating-point constants
along the traversal.
sympy_expr : str
The (lazily calculated) SymPy expression corresponding to the program.
Used for pretty printing _only_.
complexity : float
The (lazily calcualted) complexity of the program.
r : float
The (lazily calculated) reward of the program.
count : int
The number of times this Program has been sampled.
str : str
String representation of tokens. Useful as unique identifier.
"""
# Static variables
task = None # Task
library = None # Library
const_optimizer = None # Function to optimize constants
cache = {}
n_objects = 1 # Number of executable objects per Program instance
# Cython-related static variables
have_cython = None # Do we have cython installed
execute = None # Link to execute. Either cython or python
cyfunc = None # Link to cyfunc lib since we do an include inline
def __init__(self, tokens=None, on_policy=True):
"""
Builds the Program from a list of of integers corresponding to Tokens.
"""
# Can be empty if we are unpickling
if tokens is not None:
self._init(tokens, on_policy)
def _init(self, tokens, on_policy=True):
self.traversal = [Program.library[t] for t in tokens]
self.const_pos = [i for i, t in enumerate(self.traversal) if isinstance(t, PlaceholderConstant)]
self.len_traversal = len(self.traversal)
if self.have_cython and self.len_traversal > 1:
self.is_input_var = array.array('i', [t.input_var is not None for t in self.traversal])
self.invalid = False
self.str = tokens.tostring()
self.tokens = tokens
self.on_policy_count = 1 if on_policy else 0
self.off_policy_count = 0 if on_policy else 1
self.originally_on_policy = on_policy # Note if a program was created on policy
if Program.n_objects > 1:
# Fill list of multi-traversals
danglings = -1 * np.arange(1, Program.n_objects + 1)
self.traversals = [] # list to keep track of each multi-traversal
i_prev = 0
arity_list = [] # list of arities for each node in the overall traversal
for i, token in enumerate(self.traversal):
arities = token.arity
arity_list.append(arities)
dangling = 1 + np.cumsum(np.array(arity_list) - 1)[-1]
if (dangling - 1) in danglings:
trav_object = self.traversal[i_prev:i+1]
self.traversals.append(trav_object)
i_prev = i+1
"""
Keep only what dangling values have not yet been calculated. Don't want dangling to go down and up (e.g hits -1, goes back up to 0 before hitting -2)
and trigger the end of a traversal at the wrong time
"""
danglings = danglings[danglings != dangling - 1]
def __getstate__(self):
have_r = "r" in self.__dict__
have_evaluate = "evaluate" in self.__dict__
possible_const = have_r or have_evaluate
state_dict = {'tokens' : self.tokens, # string rep comes out different if we cast to array, so we can get cache misses.
'have_r' : bool(have_r),
'r' : float(self.r) if have_r else float(-np.inf),
'have_evaluate' : bool(have_evaluate),
'evaluate' : self.evaluate if have_evaluate else float(-np.inf),
'const' : array.array('d', self.get_constants()) if possible_const else float(-np.inf),
'on_policy_count' : bool(self.on_policy_count),
'off_policy_count' : bool(self.off_policy_count),
'originally_on_policy' : bool(self.originally_on_policy),
'invalid' : bool(self.invalid),
'error_node' : array.array('u', "" if not self.invalid else self.error_node),
'error_type' : array.array('u', "" if not self.invalid else self.error_type)}
# In the future we might also return sympy_expr and complexity if we ever need to compute in parallel
return state_dict
def __setstate__(self, state_dict):
# Question, do we need to init everything when we have already run, or just some things?
self._init(state_dict['tokens'], state_dict['originally_on_policy'])
have_run = False
if state_dict['have_r']:
setattr(self, 'r', state_dict['r'])
have_run = True
if state_dict['have_evaluate']:
setattr(self, 'evaluate', state_dict['evaluate'])
have_run = True
if have_run:
self.set_constants(state_dict['const'].tolist())
self.invalid = state_dict['invalid']
self.error_node = state_dict['error_node'].tounicode()
self.error_type = state_dict['error_type'].tounicode()
self.on_policy_count = state_dict['on_policy_count']
self.off_policy_count = state_dict['off_policy_count']
def execute(self, X):
"""
Execute program on input X.
Parameters
==========
X : np.array
Input to execute the Program over.
Returns
=======
result : np.array or list of np.array
In a single-object Program, returns just an array. In a multi-object Program, returns a list of arrays.
"""
if Program.n_objects > 1:
if not Program.protected:
result = []
invalids = []
for trav in self.traversals:
val, invalid, self.error_node, self.error_type = Program.execute_function(trav, X)
result.append(val)
invalids.append(invalid)
self.invalid = any(invalids)
else:
result = [Program.execute_function(trav, X) for trav in self.traversals]
return result
else:
if not Program.protected:
result, self.invalid, self.error_node, self.error_type = Program.execute_function(self.traversal, X)
else:
result = Program.execute_function(self.traversal, X)
return result
def optimize(self):
"""
Optimizes PlaceholderConstant tokens against the reward function. The
optimized values are stored in the traversal.
"""
if len(self.const_pos) == 0:
return
# Define the objective function: negative reward
def f(consts):
self.set_constants(consts)
r = self.task.reward_function(self)
obj = -r # Constant optimizer minimizes the objective function
# Need to reset to False so that a single invalid call during
# constant optimization doesn't render the whole Program invalid.
self.invalid = False
return obj
# Do the optimization
x0 = np.ones(len(self.const_pos)) # Initial guess
optimized_constants = Program.const_optimizer(f, x0)
# Set the optimized constants
self.set_constants(optimized_constants)
def get_constants(self):
"""Returns the values of a Program's constants."""
return [t.value for t in self.traversal if isinstance(t, PlaceholderConstant)]
def set_constants(self, consts):
"""Sets the program's constants to the given values"""
for i, const in enumerate(consts):
assert U.is_float, "Input to program constants must be of a floating point type"
# Create a new instance of PlaceholderConstant instead of changing
# the "values" attribute, otherwise all Programs will have the same
# instance and just overwrite each other's value.
self.traversal[self.const_pos[i]] = PlaceholderConstant(const)
@classmethod
def set_n_objects(cls, n_objects):
Program.n_objects = n_objects
@classmethod
def clear_cache(cls):
"""Clears the class' cache"""
cls.cache = {}
@classmethod
def set_task(cls, task):
"""Sets the class' Task"""
Program.task = task
Program.library = task.library
@classmethod
def set_const_optimizer(cls, name, **kwargs):
"""Sets the class' constant optimizer"""
const_optimizer = make_const_optimizer(name, **kwargs)
Program.const_optimizer = const_optimizer
@classmethod
def set_complexity(cls, name):
"""Sets the class' complexity function"""
all_functions = {
# No complexity
None : lambda p : 0.0,
# Length of sequence
"length" : lambda p : len(p.traversal),
# Sum of token-wise complexities
"token" : lambda p : sum([t.complexity for t in p.traversal]),
}
assert name in all_functions, "Unrecognzied complexity function name."
Program.complexity_function = lambda p : all_functions[name](p)
@classmethod
def set_execute(cls, protected):
"""Sets which execute method to use"""
# Check if cython_execute can be imported; if not, fall back to python_execute
try:
from dso import cyfunc
from dso.execute import cython_execute
execute_function = cython_execute
Program.have_cython = True
except ImportError:
from dso.execute import python_execute
execute_function = python_execute
Program.have_cython = False
if protected:
Program.protected = True
Program.execute_function = execute_function
else:
Program.protected = False
class InvalidLog():
"""Log class to catch and record numpy warning messages"""
def __init__(self):
self.error_type = None # One of ['divide', 'overflow', 'underflow', 'invalid']
self.error_node = None # E.g. 'exp', 'log', 'true_divide'
self.new_entry = False # Flag for whether a warning has been encountered during a call to Program.execute()
def write(self, message):
"""This is called by numpy when encountering a warning"""
if not self.new_entry: # Only record the first warning encounter
message = message.strip().split(' ')
self.error_type = message[1]
self.error_node = message[-1]
self.new_entry = True
def update(self):
"""If a floating-point error was encountered, set Program.invalid
to True and record the error type and error node."""
if self.new_entry:
self.new_entry = False
return True, self.error_type, self.error_node
else:
return False, None, None
invalid_log = InvalidLog()
np.seterrcall(invalid_log) # Tells numpy to call InvalidLog.write() when encountering a warning
# Define closure for execute function
def unsafe_execute(traversal, X):
"""This is a wrapper for execute_function. If a floating-point error
would be hit, a warning is logged instead, p.invalid is set to True,
and the appropriate nan/inf value is returned. It's up to the task's
reward function to decide how to handle nans/infs."""
with np.errstate(all='log'):
y = execute_function(traversal, X)
invalid, error_node, error_type = invalid_log.update()
return y, invalid, error_node, error_type
Program.execute_function = unsafe_execute
@cached_property
def r(self):
"""Evaluates and returns the reward of the program"""
with warnings.catch_warnings():
warnings.simplefilter("ignore")
# Optimize any PlaceholderConstants
self.optimize()
# Return final reward after optimizing
return self.task.reward_function(self)
@cached_property
def complexity(self):
"""Evaluates and returns the complexity of the program"""
return Program.complexity_function(self)
@cached_property
def evaluate(self):
"""Evaluates and returns the evaluation metrics of the program."""
# Program must be optimized before computing evaluate
if "r" not in self.__dict__:
print("WARNING: Evaluating Program before computing its reward." \
"Program will be optimized first.")
self.optimize()
with warnings.catch_warnings():
warnings.simplefilter("ignore")
return self.task.evaluate(self)
@cached_property
def sympy_expr(self):
"""
Returns the attribute self.sympy_expr.
This is actually a bit complicated because we have to go: traversal -->
tree --> serialized tree --> SymPy expression
"""
if Program.n_objects == 1:
tree = self.traversal.copy()
tree = build_tree(tree)
tree = convert_to_sympy(tree)
try:
expr = parse_expr(tree.__repr__()) # SymPy expression
except:
expr = tree.__repr__()
return [expr]
else:
exprs = []
for i in range(len(self.traversals)):
tree = self.traversals[i].copy()
tree = build_tree(tree)
tree = convert_to_sympy(tree)
try:
expr = parse_expr(tree.__repr__()) # SymPy expression
except:
expr = tree.__repr__()
exprs.append(expr)
return exprs
def pretty(self):
"""Returns pretty printed string of the program"""
return [pretty(self.sympy_expr[i]) for i in range(Program.n_objects)]
def print_stats(self):
"""Prints the statistics of the program
We will print the most honest reward possible when using validation.
"""
print("\tReward: {}".format(self.r))
print("\tCount Off-policy: {}".format(self.off_policy_count))
print("\tCount On-policy: {}".format(self.on_policy_count))
print("\tOriginally on Policy: {}".format(self.originally_on_policy))
print("\tInvalid: {}".format(self.invalid))
print("\tTraversal: {}".format(self))
if Program.n_objects == 1:
print("\tExpression:")
print("{}\n".format(indent(self.pretty()[0], '\t ')))
else:
for i in range(Program.n_objects):
print("\tExpression {}:".format(i))
print("{}\n".format(indent(self.pretty()[i], '\t ')))
def __repr__(self):
"""Prints the program's traversal"""
return ','.join([repr(t) for t in self.traversal])
###############################################################################
# Everything below this line is currently only being used for pretty printing #
###############################################################################
# Possible library elements that sympy capitalizes
capital = ["add", "mul", "pow"]
class Node(object):
"""Basic tree class supporting printing"""
def __init__(self, val):
self.val = val
self.children = []
def __repr__(self):
children_repr = ",".join(repr(child) for child in self.children)
if len(self.children) == 0:
return self.val # Avoids unnecessary parantheses, e.g. x1()
return "{}({})".format(self.val, children_repr)
def build_tree(traversal):
"""Recursively builds tree from pre-order traversal"""
op = traversal.pop(0)
n_children = op.arity
val = repr(op)
if val in capital:
val = val.capitalize()
node = Node(val)
for _ in range(n_children):
node.children.append(build_tree(traversal))
return node
def convert_to_sympy(node):
"""Adjusts trees to only use node values supported by sympy"""
if node.val == "div":
node.val = "Mul"
new_right = Node("Pow")
new_right.children.append(node.children[1])
new_right.children.append(Node("-1"))
node.children[1] = new_right
elif node.val == "sub":
node.val = "Add"
new_right = Node("Mul")
new_right.children.append(node.children[1])
new_right.children.append(Node("-1"))
node.children[1] = new_right
elif node.val == "inv":
node.val = Node("Pow")
node.children.append(Node("-1"))
elif node.val == "neg":
node.val = Node("Mul")
node.children.append(Node("-1"))
elif node.val == "n2":
node.val = "Pow"
node.children.append(Node("2"))
elif node.val == "n3":
node.val = "Pow"
node.children.append(Node("3"))
elif node.val == "n4":
node.val = "Pow"
node.children.append(Node("4"))
for child in node.children:
convert_to_sympy(child)
return node