-
Notifications
You must be signed in to change notification settings - Fork 0
/
pydag.py
executable file
·502 lines (390 loc) · 13.6 KB
/
pydag.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import division
import re
import os
from collections import OrderedDict, defaultdict
import logging
logger = logging.getLogger("pyDAG")
logger.addHandler(logging.StreamHandler())
import numpy as np
import numpy.distutils.__config__
import theano as th
import theano.tensor as T
__all__ = ["DAG"]
class Node(object):
"""
superclass for all types of nodes.
the children are ordered (important for non-commutative operations)
and the `get_*` methods return the values of the weighted children.
the `value` method needs to be overwritten and is crucial for
evaluating the entire graph.
"""
def __init__(self):
self.children = []
def add_child(self, node, factor=1.):
self.children.append((factor, node))
def get_input(self, i):
f, v = self.children[i]
return f * v.value() if f != 1. else v.value()
def get_all_input(self):
p = lambda (f, v): f * v.value() if f != 1. else v.value()
return map(p, self.children)
def update(self, **attrs):
self.__dict__.update(attrs)
def value(self):
raise Exception("child node has to implement suitable val(...) method")
class Operator(Node):
def __init__(self, op):
super(Operator, self).__init__()
self.op = op
# maybe updated via an attribute with a float-array
self.data = None
def add(self):
nbch = len(self.children)
assert nbch >= 1
if nbch == 1:
s = self.get_input(0)
elif nbch == 2:
s = self.get_input(0) + self.get_input(1)
else:
# s = sum(self.get_all_input()) # <- that's probably worse
s = T.stack(*self.get_all_input()).sum()
if self.data is not None:
s += self.data[0]
return s
def mult(self):
nbch = len(self.children)
assert nbch >= 1
if nbch == 1:
s = self.get_input(0)
elif nbch == 2:
m = self.get_input(0) * self.get_input(1)
else:
m = T.stack(*self.get_all_input()).prod()
if self.data is not None:
m *= self.data[0]
return m
def square(self):
assert len(self.children) == 1
v = self.get_input(0)
if self.data is not None:
v += self.data[0]
return v ** 2.
def power(self):
assert len(self.children) == 2
basis = self.get_input(0)
exponent = self.get_input(1)
return T.pow(basis, exponent)
def division(self):
"""
Note: This maps to the `true_div` operation.
"""
assert len(self.children) == 2
n = self.get_input(0)
d = self.get_input(1)
return T.true_div(n, d)
def sqrt(self):
assert len(self.children) == 1
v = self.get_input(0)
if self.data is not None:
v += self.data[0]
return T.sqrt(v)
def min_op(self):
logger.warning("min op is not tested")
vals = self.get_all_input()
if self.data is not None:
vals.append(self.data[0])
return vals.min()
def max_op(self):
logger.warning("max op is not tested")
vals = self.get_all_input()
if self.data is not None:
vals.append(self.data[0])
return vals.max()
def sin(self):
assert len(self.children) == 1
return T.sin(self.get_input(0))
def cos(self):
assert len(self.children) == 1
return T.cos(self.get_input(0))
def exp(self):
assert len(self.children) == 1
return T.exp(self.get_input(0))
# this is a bit awkward, but useful in the parser function to know
# which operations are actually defined.
op_functions = {
'+': add,
'*': mult,
'2': square,
'sqrt': sqrt,
'^': power,
'/': division,
'min': min_op,
'max': max_op,
'sin': sin,
'cos': cos,
'exp': exp
}
def value(self):
return self.op_functions[self.op](self)
def __str__(self):
return "op[%s]" % self.op
class Variable(Node):
def __init__(self):
super(Variable, self).__init__()
self.var = T.scalar()
self.bound = [-np.Inf, np.Inf] # default
def set_name(self, name):
self.var.name = name
@property
def name(self):
return self.var.name
def value(self):
return self.var
def __str__(self):
return "Variable[%s]" % self.var
class Constant(Node):
def __init__(self, v):
super(Constant, self).__init__()
# necessary, e.g., if the obj fctn is constant
self.v = T.constant(v)
def value(self):
return self.v
def __str__(self):
return "Constant[%s]" % self.v
# END Classes in a Node
# START DAG specific classes
class Objective(object):
def __init__(self, name=None):
self.minimize = True
self.name = name
self.expression = None
self.add = None
self.mult = None
self.node = None
def set_node(self, node):
"""
the node in the dag
"""
self.node = node
def value(self):
# obj = (mult * f(x)) + add
v = self.node.value()
if self.mult is not None:
v *= self.mult
if self.add is not None:
v += self.add
return v
def __str__(self):
return "Objective{%s, add=%s, mult=%s, node=%s}" \
% ("min" if self.minimize else "max", self.add, self.mult, self.node)
class Constraint(object):
def __init__(self, node_id):
self.node_id = node_id # will be replaced in set_expression later!
self.name = None
def set_name(self, name):
self.name = name
def set_expression(self, ex):
if self.name is not None:
ex.name = self.name
self.expression = ex
@property
def bound(self):
return self.expression.bound
def value(self):
return self.expression.value()
def set_name(self, name):
self.name = name
# END DAG specific classes
def parse_bound(b):
"""
helper function to translate "[xxxx,yyyy]" into a tuple for an interval
"""
def parse_number(n):
if n == "I":
return np.Inf
elif n == "-I":
return -np.Inf
else:
return float(n)
return map(parse_number, parse_bound.bnd.match(b).groups())
parse_bound.bnd = re.compile(r"\[([^,]+),([^\]]+)\]")
def parse_attributes(tokens):
"""
helper function
each node has a list of possible attributes.
they are like [ "b [1,1]", "d 0.5", ...]
- b: bounds and they are parsed
- d: data and a list of parsed floats
"""
attrs = {}
for token in tokens:
op = token[0]
data = token[2:]
if op == 'd':
# "d 0.4,22.2,...
assert "d" not in attrs
attrs["data"] = map(float, data.split(","))
elif op == 'b':
# bounds like "[1,1]"
attrs["bound"] = parse_bound(data)
else:
raise Exception("unknown op: '%s' in parse_attributes" % what)
return attrs
class DAG(object):
"""
A Python representation of a (simplified) COCONUT Dag based on "theano".
"""
def __init__(self):
self.objective = Objective()
self.variables = defaultdict(Variable)
self.constraints = {}
self.func = None # will be the theano function
self.bounds = None
def __str__(self):
return "DAG@%s" % id(self)
@property
def dimensions(self):
return len(self.variables)
def __call__(self, *args):
return self.func(*args)
@staticmethod
def parse(fn):
#cls = re.compile(r"^<([^>]+)>")
nodes = {}
edges = OrderedDict() # edgeN : operator | constant
constr_mapping = {} # [(constr_idx <-> node_id), ...]
# instructions in "N" nodes: V 2, M 0 min, c 0 9, ... ?
globs = [] # just for debugging
dag = DAG()
for line in open(fn, "r"):
i = line.find("> ") # first split at the end of the node id
token0 = line[1:i]
tokens = line[i + 2:].split(": ")
if token0 == "N":
globs.append(tokens[1:])
attrs = parse_attributes(tokens[1:])
data = tokens[0].split()
t = data.pop(0)
if t == 'V':
# ignored!
pass
elif t == "M":
assert data[1] in ["min", "max"] # m00 not supported
dag.objective.set_node(nodes[int(data[0])])
dag.objective.minimize = data[1] == "min"
elif t == "N": # N <number> 'variable name'
dag.variables[int(data[0])].set_name(data[1][1:-1])
elif t == 'C':
print dag.constraints[int(data[0])].set_name(data[1][1:-1])
elif t == "O":
# objective: O 'oname' : d obj_add [, obj_mult]
logger.info("O oname '%s' ignored" % data[0])
if "data" in attrs:
d = attrs["data"]
if len(d) >= 1:
dag.objective.add = float(d[0])
if len(d) == 2:
dag.objective.mult = float(d[1])
elif t == 'c':
idx, node_id = map(int, data)
assert idx not in constr_mapping
# node is not defined so far, hence just the node_id
# number!
dag.constraints[idx] = Constraint(node_id)
else:
raise Exception("unknown node type '%s'" % t)
elif token0 == "E":
src, targ, val = tokens.pop().split()
k = int(src), int(targ)
edges[k] = float(val)
else: # token0 must be a number
n = int(token0)
assert n not in nodes
ops = tokens.pop().split()
# print "op: ", op
# print "tokens:", tokens
if ops[0] == 'V':
idx = int(ops[1]) # index in "V <idx>"
nodes[n] = dag.variables[idx]
elif ops[0] == 'C':
nodes[n] = Constant(float(ops[1]))
elif ops[0] in Operator.op_functions.keys():
nodes[n] = Operator(ops[0])
else:
raise Exception("Unknown operator '%s'" % ops[0])
# finally, update attributes like "data" in the node
attrs = parse_attributes(tokens)
nodes[n].update(**attrs)
print "globs:"
print globs
# start processing
# x is the vector of variables, ordering is important!
# the following translates the dictionary into an ordered vector.
dag.variables = [dag.variables[i] for i in range(len(dag.variables))]
bounds = [None] * len(dag.variables)
for idx, var in enumerate(dag.variables):
bounds[idx] = var.bound
bounds = dag.bounds = np.array(bounds)
print
print "Variables:"
for idx, var in enumerate(dag.variables):
print "%3d: [%5s] in %s" % (idx, var.name, bounds[idx])
for (src, targ), val in edges.iteritems():
# print src, targ, val
nodes[src].add_child(nodes[targ], val)
obj = dag.objective.value()
# print "DEBUG Constraints"
# for idx in constr_nodes:
# print nodes[idx]
# print [ n.children for (f, n) in nodes[idx].children]
# that should be true, right?
assert sorted(dag.constraints.keys()) == range(len(dag.constraints))
# set the constraints of the dag
for c in dag.constraints.values():
c.set_expression(nodes[c.node_id])
dag.constraints = [dag.constraints[i]
for i in range(len(dag.constraints))]
outputs = [obj]
if len(dag.constraints) > 0:
cs = T.stack(*[c.value() for c in dag.constraints])
outputs.append(cs)
f = th.function(inputs=[_.var for _ in dag.variables],
outputs=outputs)
dag.func = f
# just makes one single array, index 0 is the objective
#f = lambda *x : np.r_[fc(*x)]
print
print "Objective:", dag.objective
print th.printing.pp(obj)
for i, c in enumerate(dag.constraints):
print
print "Constraint %d [name: %s]:" % (i, c.expression.name)
print " bound:", c.bound
print " expr:", th.printing.pp(c.value())
# print
# print dag.constraints
print
print "10 random evaluations:"
b = bounds.copy()
# fix +/- infinity
b[np.isinf(b[:, 0]), 0] = -1000
b[np.isinf(b[:, 1]), 1] = 1000
w = b[:, 1] - b[:, 0]
for _ in range(10):
arg = b[:, 0] + w * np.random.rand(b.shape[0])
print "f(%s) = %s" % (arg, f(*arg))
#outfile = os.path.expanduser("%s.png" % os.path.splitext(os.path.basename(fn))[0])
#th.printing.pydotprint(f, outfile=outfile)
print
print "Debug Printing of compiled f:"
print th.printing.debugprint(f)
return dag # END parse()
if __name__ == "__main__":
import sys
from glob import glob
for arg in sys.argv[1:]:
for fn in glob(arg):
print fn
print DAG.parse(fn)