comparison doc/v2_planning/plugin_JB.py @ 1211:e7ac87720fee

v2planning plugin_JB - added PRINT and POPEN to demonstrate parallel async. control flows
author James Bergstra <bergstrj@iro.umontreal.ca>
date Wed, 22 Sep 2010 00:23:07 -0400
parents cbe1fb32686c
children 478bb1f8215c
comparison
equal deleted inserted replaced
1210:cbe1fb32686c 1211:e7ac87720fee
28 - REPEAT - do something N times (and return None or maybe the last CALL?) 28 - REPEAT - do something N times (and return None or maybe the last CALL?)
29 - BUFFER_REPEAT - do something N times and accumulate the return value from each iter 29 - BUFFER_REPEAT - do something N times and accumulate the return value from each iter
30 - LOOP - do something an infinite number of times 30 - LOOP - do something an infinite number of times
31 - CHOOSE - like a switch statement (should rename to SWITCH) 31 - CHOOSE - like a switch statement (should rename to SWITCH)
32 - WEAVE - interleave execution of multiple control-flow elements 32 - WEAVE - interleave execution of multiple control-flow elements
33 - POPEN - launch a process and return its status when it's complete
34 - PRINT - a shortcut for CALL(print_obj)
33 35
34 36
35 We don't have many requirements per-se for the architecture, but I think this design respects 37 We don't have many requirements per-se for the architecture, but I think this design respects
36 and realizes all of them. 38 and realizes all of them.
37 The advantages of this approach are: 39 The advantages of this approach are:
66 """ 68 """
67 69
68 __license__ = 'TODO' 70 __license__ = 'TODO'
69 __copyright__ = 'TODO' 71 __copyright__ = 'TODO'
70 72
71 import copy, sys, cPickle 73 import cPickle, copy, subprocess, sys, time
72 import numpy 74 import numpy
73 75
74 #################################################### 76 ####################################################
75 # CONTROL-FLOW CONSTRUCTS 77 # CONTROL-FLOW CONSTRUCTS
76 78
108 #TODO make sure there is not an off-by-one error 110 #TODO make sure there is not an off-by-one error
109 if i > n_steps: 111 if i > n_steps:
110 break 112 break
111 r = self.step() 113 r = self.step()
112 return r 114 return r
113
114 115
115 class BUFFER_REPEAT(ELEMENT): 116 class BUFFER_REPEAT(ELEMENT):
116 """ 117 """
117 Accumulate a number of return values into one list / array. 118 Accumulate a number of return values into one list / array.
118 119
283 while self.elem_finished[self.idx]: 284 while self.elem_finished[self.idx]:
284 self.idx = (self.idx+1) % len(self.elements) 285 self.idx = (self.idx+1) % len(self.elements)
285 286
286 return INCOMPLETE 287 return INCOMPLETE
287 288
289 class POPEN(ELEMENT):
290 def __init__(self, args):
291 self.args = args
292 def start(self, arg):
293 self.p = subprocess.Popen(self.args)
294 def step(self):
295 r = self.p.poll()
296 if r is None:
297 return INCOMPLETE
298 return r
299
300 def PRINT(obj):
301 return CALL(print_obj, obj)
288 302
289 #################################################### 303 ####################################################
290 # [Dummy] Components involved in learning algorithms 304 # [Dummy] Components involved in learning algorithms
291 305
292 class Dataset(object): 306 class Dataset(object):
367 381
368 def cd1_update(X, layer, lr): 382 def cd1_update(X, layer, lr):
369 # update self.layer from observation X 383 # update self.layer from observation X
370 layer.w += X.mean() * lr #TODO: not exactly correct math! 384 layer.w += X.mean() * lr #TODO: not exactly correct math!
371 385
372 def simple_main(): 386
387 ###############################################################
388 # Example algorithms written in this control flow mini-language
389
390 def main_weave():
391 # Uses weave to demonstrate the interleaving of two bufferings of a single stream
373 392
374 l = [0] 393 l = [0]
375 def f(a): 394 def f(a):
376 print l 395 print l
377 l[0] += a 396 l[0] += a
380 print WEAVE(1, [ 399 print WEAVE(1, [
381 BUFFER_REPEAT(3,CALL(f,1)), 400 BUFFER_REPEAT(3,CALL(f,1)),
382 BUFFER_REPEAT(5,CALL(f,1)), 401 BUFFER_REPEAT(5,CALL(f,1)),
383 ]).run() 402 ]).run()
384 403
385 def main(): 404 def main_weave_popen():
405 # Uses weave and Popen to demonstrate the control of a program with some asynchronous
406 # parallelism
407
408 p = WEAVE(2,[
409 SEQ([POPEN(['sleep', '5']), PRINT('done 1')]),
410 SEQ([POPEN(['sleep', '10']), PRINT('done 2')]),
411 LOOP([
412 CALL(print_obj, 'polling...'),
413 CALL(time.sleep, 1)])])
414 # The LOOP would forever if the WEAVE were not configured to stop after 2 of its elements
415 # complete.
416
417 p.run()
418 # Note that the program can be run multiple times...
419 p.run()
420
421 main = main_weave_popen
422 def main_kfold_dbn():
423 # Uses many of the control-flow elements to define the k-fold evaluation of a dbn
424 # The algorithm is not quite right, but the example shows off all of the required
425 # control-flow elements I think.
426
386 # create components 427 # create components
387 dataset = Dataset(numpy.random.RandomState(123).randn(13,1)) 428 dataset = Dataset(numpy.random.RandomState(123).randn(13,1))
388 pca = PCA_Analysis() 429 pca = PCA_Analysis()
389 layer1 = Layer(w=4) 430 layer1 = Layer(w=4)
390 layer2 = Layer(w=3) 431 layer2 = Layer(w=3)