annotate doc/v2_planning/plugin.py @ 1128:03b41a79bd60

coding_style: Replies to James' questions / comments
author Olivier Delalleau <delallea@iro>
date Wed, 15 Sep 2010 12:06:09 -0400
parents 8cc324f388ba
children a1957faecc9b
rev   line source
1118
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
1
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
2 import time
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
3 from collections import defaultdict
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
4
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
5 inf = float('inf')
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
6
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
7 ################
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
8 ### SCHEDULE ###
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
9 ################
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
10
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
11 class Schedule(object):
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
12 def __add__(self, i):
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
13 return OffsetSchedule(self, i)
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
14 def __or__(self, s):
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
15 return UnionSchedule(self, to_schedule(s))
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
16 def __and__(self, s):
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
17 return IntersectionSchedule(self, to_schedule(s))
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
18 def __sub__(self, i):
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
19 return OffsetSchedule(self, -i)
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
20 def __ror__(self, s):
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
21 return UnionSchedule(to_schedule(s), self)
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
22 def __rand__(self, s):
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
23 return IntersectionSchedule(to_schedule(s), self)
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
24 def __invert__(self):
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
25 return NegatedSchedule(self)
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
26
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
27 def to_schedule(x):
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
28 if x in (None, False):
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
29 return never
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
30 if x is True:
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
31 return always
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
32 elif isinstance(x, (list, tuple)):
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
33 return reduce(UnionSchedule, x)
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
34 else:
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
35 return x
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
36
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
37
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
38 class ScheduleMix(Schedule):
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
39 __n__ = None
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
40 def __init__(self, *subschedules):
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
41 assert (not self.__n__) or len(subschedules) == self.__n__
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
42 self.subschedules = map(to_schedule, subschedules)
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
43
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
44 class UnionSchedule(ScheduleMix):
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
45 def __call__(self, t1, t2):
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
46 return any(s(t1, t2) for s in self.subschedules)
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
47
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
48 class IntersectionSchedule(ScheduleMix):
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
49 def __call__(self, t1, t2):
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
50 return all(s(t1, t2) for s in self.subschedules)
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
51
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
52 class DifferenceSchedule(ScheduleMix):
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
53 __n__ = 2
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
54 def __call__(self, t1, t2):
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
55 return self.subschedules[0](t1, t2) and not self.subschedules[1](t1, t2)
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
56
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
57 class NegatedSchedule(ScheduleMix):
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
58 __n__ = 1
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
59 def __call__(self, t1, t2):
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
60 return not self.subschedules[0](t1, t2)
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
61
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
62 class OffsetSchedule(Schedule):
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
63 def __init__(self, schedule, offset):
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
64 self.schedule = schedule
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
65 self.offset = offset
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
66 def __call__(self, t1, t2):
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
67 return self.schedule(t1 - self.offset, t2 - self.offset)
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
68
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
69
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
70 class AlwaysSchedule(Schedule):
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
71 def __call__(self, t1, t2):
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
72 return True
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
73
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
74 always = AlwaysSchedule()
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
75 never = ~always
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
76
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
77 class IntervalSchedule(Schedule):
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
78 def __init__(self, step, repeat = inf):
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
79 self.step = step
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
80 self.upper_bound = step * (repeat - 1)
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
81 def __call__(self, t1, t2):
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
82 if t2 < 0 or t1 > self.upper_bound:
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
83 return False
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
84 diff = t2 - t1
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
85 t1m = t1 % self.step
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
86 t2m = t2 % self.step
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
87 return (diff >= self.step
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
88 or t1m == 0
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
89 or t2m == 0
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
90 or t1m > t2m)
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
91
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
92 each = lambda step, repeat = inf: each0(step, repeat) + step
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
93 each0 = IntervalSchedule
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
94
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
95
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
96 class RangeSchedule(Schedule):
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
97 def __init__(self, low = None, high = None):
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
98 self.low = low or -inf
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
99 self.high = high or inf
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
100 def __call__(self, t1, t2):
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
101 return self.low <= t1 <= self.high \
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
102 or self.low <= t2 <= self.high
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
103
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
104 inrange = RangeSchedule
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
105
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
106
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
107 class ListSchedule(Schedule):
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
108 def __init__(self, *schedules):
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
109 self.schedules = schedules
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
110 def __call__(self, t1, t2):
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
111 for t in self.schedules:
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
112 if t1 <= t <= t2:
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
113 return True
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
114 return False
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
115
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
116 at = ListSchedule
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
117 at_start = at(-inf)
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
118 at_end = at(inf)
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
119
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
120
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
121 ##############
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
122 ### RUNNER ###
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
123 ##############
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
124
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
125 class scratchpad:
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
126 pass
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
127
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
128 # # ORIGINAL RUNNER, NO TIMELINES
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
129 # def runner(master, plugins):
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
130 # """
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
131 # master is a function which is in charge of the "this" object. It
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
132 # is in charge of updating the t1, t2 and done fields, It must
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
133 # take a single argument, this.
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
134
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
135 # plugins is a list of (schedule, function) pairs. In-between each
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
136 # execution of the master function, as well as at the very
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
137 # beginning and at the very end, the schedule will be consulted
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
138 # for the time range [t1, t2], and if there is a match, the
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
139 # function will be called with this as the argument. The order
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
140 # in which the functions are provided is respected.
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
141
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
142 # Note: the reason why we use t1 and t2 instead of just t is that it
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
143 # gives the master function the ability to run several iterations at
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
144 # once without consulting any plugins. In that situation, t1 and t2
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
145 # represent a range, and the schedule must determine if there would
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
146 # have been an event in that range (we do not distinguish between a
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
147 # single event and multiple events).
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
148
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
149 # For instance, if one is training using minibatches, one could set
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
150 # t1 and t2 to the index of the lower and higher examples, and the
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
151 # plugins' schedules would be given according to how many examples
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
152 # were seen rather than how many minibatches were processed.
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
153
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
154 # Another possibility is to use real time - t1 would be the time
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
155 # before the execution of the master function, t2 the time after
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
156 # (in, say, milliseconds). Then you can define plugins that run
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
157 # every second or every minute, but only in-between two training
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
158 # iterations.
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
159 # """
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
160
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
161 # this = scratchpad()
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
162 # this.t1 = -inf
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
163 # this.t2 = -inf
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
164 # this.started = False
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
165 # this.done = False
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
166 # while True:
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
167 # for schedule, function in plugins:
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
168 # if schedule(this.t1, this.t2):
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
169 # function(this)
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
170 # if this.done:
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
171 # break
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
172 # master(this)
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
173 # this.started = True
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
174 # if this.done:
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
175 # break
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
176 # this.t1 = inf
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
177 # this.t2 = inf
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
178 # for schedule, function in plugins:
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
179 # if schedule(this.t1, this.t2):
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
180 # function(this)
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
181
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
182
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
183
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
184
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
185 def runner(main, plugins):
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
186 """
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
187 :param main: A function which must take a single argument,
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
188 ``this``. The ``this`` argument contains a settable ``done``
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
189 flag indicating whether the iterations should keep going or
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
190 not, as well as a flag indicating whether this is the first
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
191 time runner() is calling main(). main() may store whatever it
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
192 wants in ``this``. It may also add one or more timelines in
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
193 ``this.timelines[timeline_name]``, which plugins can exploit.
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
194
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
195 :param plugins: A list of (schedule, timeline, function)
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
196 tuples. In-between each execution of the main function, as
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
197 well as at the very beginning and at the very end, the
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
198 schedule will be consulted for the time range [t1, t2] from
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
199 the appropriate timeline, and if there is a match, the
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
200 function will be called with ``this`` as the argument. The
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
201 order in which the functions are provided is respected.
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
202
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
203 For any plugin, the timeline can be
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
204 * 'iterations', where t1 == t2 == the iteration number
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
205 * 'real_time', where t1 and t2 mark the start of the last
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
206 loop and the start of the current loop, in seconds since
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
207 the beginning of training (includes time spent in plugins)
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
208 * 'algorithm_time', where t1 and t2 mark the start and end
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
209 of the last iteration of the main function (does not
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
210 include time spent in plugins)
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
211 * A main function specific timeline.
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
212
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
213 At the very beginning, the time for all timelines is
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
214 -infinity, at the very end it is +infinity.
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
215 """
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
216 start_time = time.time()
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
217
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
218 this = scratchpad()
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
219
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
220 this.timelines = defaultdict(lambda: [-inf, -inf])
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
221 realt = this.timelines['real_time']
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
222 algot = this.timelines['algorithm_time']
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
223 itert = this.timelines['iterations']
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
224
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
225 this.started = False
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
226 this.done = False
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
227
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
228 while True:
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
229
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
230 for schedule, timeline, function in plugins:
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
231 if schedule(*this.timelines[timeline]):
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
232 function(this)
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
233 if this.done:
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
234 break
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
235
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
236 t1 = time.time()
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
237 main(this)
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
238 t2 = time.time()
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
239
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
240 if not this.started:
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
241 realt[:] = [0, 0]
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
242 algot[:] = [0, 0]
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
243 itert[:] = [-1, -1]
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
244 realt[:] = [realt[1], t2 - start_time]
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
245 algot[:] = [algot[1], algot[1] + (t2 - t1)]
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
246 itert[:] = [itert[0] + 1, itert[1] + 1]
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
247
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
248 this.started = True
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
249 if this.done:
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
250 break
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
251
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
252 this.timelines = defaultdict(lambda: [inf, inf])
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
253
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
254 for schedule, timeline, function in plugins:
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
255 if schedule(*this.timelines[timeline]):
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
256 function(this)
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
257
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
258
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
259
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
260
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
261
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
262 ################
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
263 ### SHOWCASE ###
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
264 ################
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
265
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
266 def main(this):
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
267 if not this.started:
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
268 this.error = 1.0
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
269 # note: runner will automatically set this.started to true
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
270 else:
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
271 this.error /= 1.1
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
272
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
273
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
274 def welcome(this):
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
275 print "Let's start!"
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
276
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
277 def print_iter(this):
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
278 print "Now running iteration #%i" % this.timelines['iterations'][0]
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
279
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
280 def print_error(this):
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
281 print "The error rate is %s" % this.error
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
282
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
283 def maybe_stop(this):
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
284 thr = 0.01
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
285 if this.error < thr:
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
286 print "Error is below the threshold: %s <= %s" % (this.error, thr)
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
287 this.done = True
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
288
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
289 def wait_a_bit(this):
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
290 time.sleep(1./37)
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
291
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
292 def printer(txt):
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
293 def f(this):
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
294 print txt
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
295 return f
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
296
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
297 def stop_this_madness(this):
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
298 this.done = True
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
299
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
300 def byebye(this):
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
301 print "Bye bye!"
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
302
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
303 runner(main = main,
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
304 plugins = [# At the very beginning, print a welcome message
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
305 (at_start, 'iterations', welcome),
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
306 # Each iteration from 1 to 10 inclusive, OR each multiple of 10
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
307 # (except 0 - each() excludes 0, each0() includes it)
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
308 # print the error
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
309 (inrange(1, 10) | each(10), 'iterations', print_error),
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
310 # Each multiple of 10, check for stopping condition
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
311 (each(10), 'iterations', maybe_stop),
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
312 # At iteration 1000, if we ever get that far, just stop
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
313 (at(1000), 'iterations', stop_this_madness),
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
314 # Wait a bit
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
315 (each(1), 'iterations', wait_a_bit),
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
316 # Print bonk each second of real time
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
317 (each(1), 'real_time', printer('BONK')),
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
318 # Print thunk each second of time in main() (main()
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
319 # is too fast, so this does not happen for many
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
320 # iterations)
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
321 (each(1), 'algorithm_time', printer('THUNK')),
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
322 # Announce the next iteration
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
323 (each0(1), 'iterations', print_iter),
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
324 # At the very end, display a message
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
325 (at_end, 'iterations', byebye)])
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
326
8cc324f388ba proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff changeset
327