Jordan de la Houssaye

cleaned andy's code

...@@ -3,115 +3,64 @@ snakes.plugins.load(['gv', 'ops'], 'snakes.nets', 'nets') ...@@ -3,115 +3,64 @@ snakes.plugins.load(['gv', 'ops'], 'snakes.nets', 'nets')
3 from nets import * 3 from nets import *
4 4
5 5
6 -
7 -###############################################################################
8 ############################################################################### 6 ###############################################################################
9 -############################### INIT ########################################## 7 +## AUXILIARY FUNCTIONS ########################################################
10 - 8 +
11 -# entities: tuple of name of the entities, initial level, tuple of decays 0 9 +def clockt(obligatory, name, ls, us, lambdas, deltas, inputlist, D):
12 -# denotes unbounded decay (omega) 10 + '''This function computes the action of clock + decay + obligatory
13 -#entities = ( ('B',4, (0,2,2,2,3)), ('P',0, (0,0)), ('C',0, (0,0)), ('G',0, (0,0))) 11 + activities.
14 - 12 + obligatory = set of obligatory activities
15 - 13 + name = entity concerned
16 -#entities = ( ('Sugar',1, (0,2)), ('Aspartame',0, (0,2)), ('Glycemia',2, (0,2,2,2)), ('Glucagon',0, (0,2)), ('Insulin',0,(0,2,2))) 14 + ls = ls of entity
17 - 15 + us = us of entity
18 - 16 + lambdas = lambdas of entity
19 -entities = ( ('s1',0, (0,1)), ('s2',0, (0,1)), ('s3',0, (0,1)) ) 17 + deltas = list of the decays duration of entity
20 - 18 + inputlist = tuples for all other entities'''
21 - 19 +
22 - 20 + #print (obligatory, name, ls, us, lambdas, deltas, inputlist,D)
23 -# Activities: Tuple of (activators, inhibitors, results, duration)
24 -# activators, inhibitors are dictionaries of pairs (entity, level)
25 -# results are dictionaries of pairs (entity, +z)
26 -
27 -# potential activities
28 -#potential = ((dict([('P',0)]),dict([('P',1)]),dict([('P',1)]),0),
29 -# (dict([('P',1)]),dict(),dict([('P',-1)]),0),
30 -# (dict([('C',0)]),dict([('C',1)]),dict([('C',1)]),0),
31 -# (dict([('C',1)]),dict(),dict([('C',-1)]),0),
32 -# (dict([('G',0)]),dict([('G',1)]),dict([('G',1)]),0),
33 -# (dict([('G',1)]),dict(),dict([('G',-1)]),0) )
34 -
35 -# potential = ((dict([('Sugar',1)]),dict(),dict([('Insulin',1),('Glycemia',1)]),0),
36 -# (dict([('Aspartame',1)]),dict(),dict([('Insulin',1)]),0),
37 -# (dict(),dict([('Glycemia',1)]),dict([('Glucagon',1)]),0),
38 -# (dict([('Glycemia',3)]),dict(),dict([('Insulin',1)]),0),
39 -# (dict([('Insulin',2)]),dict(),dict([('Glycemia',-1)]),0),
40 -# (dict([('Insulin',1),('Glycemia',3)]), dict(), dict([('Glycemia',-1)]),0),
41 -# (dict([('Insulin',1)]),dict([('Glycemia',2)]),dict([('Glycemia',-1)]),0),
42 -# (dict([('Glucagon',1)]),dict(),dict([('Glycemia',+1)]),0) )
43 -
44 -potential = ( (dict(),dict([('s1',1)]),dict([('s2',1)]), 1),
45 - (dict(),dict([('s2',1)]),dict([('s3',1)]), 1),
46 - (dict(),dict([('s3',1)]),dict([('s1',1)]), 1) )
47 -
48 -
49 -
50 -# obligatory activities
51 -#obligatory = ( (dict([('P',1)]),dict(),dict([('B',1)]),1),
52 -# (dict([('C',1)]),dict(),dict([('B',-1)]),3),
53 -# (dict([('G',1)]),dict(),dict([('B',-2)]),3))
54 -
55 -obligatory = ()
56 -############################### END ##########################################
57 -###############################################################################
58 -###############################################################################
59 -
60 -
61 -###############################################################################
62 -###############################################################################
63 -############################ AUXILIARY FUNCTIONS ##############################
64 -
65 -# This function computes the action of clock + decay + obligatory activities
66 -# obligatory = set of obligatory activities
67 -# name = entity concerned
68 -# ls = ls of entity
69 -# us = us of entity
70 -# lambdas = lambdas of entity
71 -# deltas = list of the decays duration of entity
72 -# inputlist = tuples for all other entities
73 -def clockt (obligatory, name, ls, us, lambdas, deltas, inputlist,D) :
74 -
75 -
76 - print (obligatory, name, ls, us, lambdas, deltas, inputlist,D)
77 l1=ls 21 l1=ls
78 u1=us 22 u1=us
79 lambda1=[] 23 lambda1=[]
80 24
81 # progression of time in lambda 25 # progression of time in lambda
82 - for i in range(0,len(lambdas)): lambda1.append(min(lambdas[i]+1, D)) 26 + for i in range(0,len(lambdas)):
27 + lambda1.append(min(lambdas[i]+1, D))
83 28
84 # progression of time for u (only for bounded levels) 29 # progression of time for u (only for bounded levels)
85 - if deltas[ls] <> 0 : u1=us+1 30 + if deltas[ls] <> 0:
31 + u1=us+1
86 32
87 # decay 33 # decay
88 - if us+1 > deltas[ls] : 34 + if us+1 > deltas[ls]:
89 l1 = max(0, ls -1) 35 l1 = max(0, ls -1)
90 u1 = 0 36 u1 = 0
91 37
92 # search of obligatory activities where entity name is in results 38 # search of obligatory activities where entity name is in results
93 act = [] 39 act = []
94 - for alpha in range(0, len(obligatory)) : 40 + for alpha in range(0, len(obligatory)):
95 obname = 'ob'+str(alpha) 41 obname = 'ob'+str(alpha)
96 - if name in obligatory[alpha][2] and inputlist[obname]>= obligatory[alpha][3]: 42 + if name in obligatory[alpha][2] \
43 + and inputlist[obname]>= obligatory[alpha][3]:
97 act.append(obligatory[alpha]) 44 act.append(obligatory[alpha])
98 45
99 -
100 -
101 # computation of the effect on entity name 46 # computation of the effect on entity name
102 - for alpha in range(0, len(act)) : 47 + for alpha in range(0, len(act)):
103 # check if the obligatory activity is enabled or not 48 # check if the obligatory activity is enabled or not
104 check = 0 49 check = 0
105 activators = act[alpha][0] 50 activators = act[alpha][0]
106 - for ent in activators : 51 + for ent in activators:
107 t = inputlist[ent] 52 t = inputlist[ent]
108 - if t[0] >= activators[ent] and t[2][activators[ent]] >= act[alpha][3] : check = 1 53 + if t[0] >= activators[ent] \
54 + and t[2][activators[ent]] >= act[alpha][3]:
55 + check = 1
109 inhibitors = act[alpha][1] 56 inhibitors = act[alpha][1]
110 - for ent in inhibitors : 57 + for ent in inhibitors:
111 t = inputlist[ent] 58 t = inputlist[ent]
112 - if t[0] < inhibitors[ent] and t[2][inhibitors[ent]] >= act[alpha][3] : check = 1 59 + if t[0] < inhibitors[ent] \
60 + and t[2][inhibitors[ent]] >= act[alpha][3]:
61 + check = 1
113 # if enabled compute the effect 62 # if enabled compute the effect
114 - if check : 63 + if check:
115 z = act[alpha][2][name] 64 z = act[alpha][2][name]
116 l1 = max(0, min(l1 + z, len(lambda1)-1)) 65 l1 = max(0, min(l1 + z, len(lambda1)-1))
117 u1 = 0 66 u1 = 0
...@@ -119,95 +68,102 @@ def clockt (obligatory, name, ls, us, lambdas, deltas, inputlist,D) : ...@@ -119,95 +68,102 @@ def clockt (obligatory, name, ls, us, lambdas, deltas, inputlist,D) :
119 # update lambda with the proper dates 68 # update lambda with the proper dates
120 temp = l1 - ls 69 temp = l1 - ls
121 if temp > 0 : 70 if temp > 0 :
122 - for i in range(ls+1,l1) : lambda1[i]=0 71 + for i in range(ls+1,l1):
123 - if temp < 0 : 72 + lambda1[i]=0
124 - for i in range(l1+1,ls) : lambda1[i]=0 73 + if temp < 0:
74 + for i in range(l1+1,ls):
75 + lambda1[i]=0
125 76
126 return (l1, u1, tuple(lambda1)) 77 return (l1, u1, tuple(lambda1))
127 78
128 79
129 -# This function computes the action of clock on obligatory activities places
130 -# obligatory = set of obligatory activities
131 -# name = obligatory activity under consideration
132 -# w = current value
133 -# inputlist = tuples for all other entities
134 def clockbetat (obligatory, name, w, inputlist,D) : 80 def clockbetat (obligatory, name, w, inputlist,D) :
81 + '''This function computes the action of clock on obligatory activities
82 + places.
83 + obligatory = set of obligatory activities
84 + name = obligatory activity under consideration
85 + w = current value
86 + inputlist = tuples for all other entities'''
87 +
135 check = 0 88 check = 0
136 activators = obligatory[name][0] 89 activators = obligatory[name][0]
137 - for ent in activators : 90 + for ent in activators:
138 t = inputlist[ent] 91 t = inputlist[ent]
139 - if t[0] >= activators[ent] and t[2][activators[ent]] >= obligatory[name][3] : check = 1 92 + if t[0] >= activators[ent] \
93 + and t[2][activators[ent]] >= obligatory[name][3]:
94 + check = 1
140 inhibitors = obligatory[name][1] 95 inhibitors = obligatory[name][1]
141 - for ent in inhibitors : 96 + for ent in inhibitors:
142 t = inputlist[ent] 97 t = inputlist[ent]
143 - if t[0] < inhibitors[ent] and t[2][inhibitors[ent]] >= obligatory[name][3] : check = 1 98 + if t[0] < inhibitors[ent] \
99 + and t[2][inhibitors[ent]] >= obligatory[name][3]:
100 + check = 1
144 # if enabled compute the effect 101 # if enabled compute the effect
145 - if check and w >= obligatory[name][3] : return(0) 102 + if check and w >= obligatory[name][3]:
146 - else: return(min(w+1, D)) 103 + return(0)
147 - 104 + else:
148 - 105 + return(min(w+1, D))
149 -
150 -
151 106
152 -# this function computes the action on an entity of a potential activity
153 -# name = entity under consideration
154 -# lp, up, lambdap = its values
155 -# R = set of results of the activity
156 def potentialt (name, lp, up, lambdap, R) : 107 def potentialt (name, lp, up, lambdap, R) :
157 - print (name, lp, up, lambdap, R) 108 + '''This function computes the action on an entity of a potential activity.
158 - # entity is a result? 109 + name = entity under consideration
159 - if (name in R) : 110 + lp, up, lambdap = its values
111 + R = set of results of the activity'''
112 +
113 + #print (name, lp, up, lambdap, R)
114 + # is entity a result?
115 + if name in R:
160 lambda2 = list(lambdap) 116 lambda2 = list(lambdap)
161 levelp = max(0,min(len(lambdap)-1, lp+R[name])) 117 levelp = max(0,min(len(lambdap)-1, lp+R[name]))
162 change = levelp-lp 118 change = levelp-lp
163 - if change > 0 : 119 + if change > 0:
164 for i in range(lp+1,levelp+1): 120 for i in range(lp+1,levelp+1):
165 lambda2[i]=0 121 lambda2[i]=0
166 - if change < 0 : 122 + if change < 0:
167 for i in range(levelp+1,lp+1): 123 for i in range(levelp+1,lp+1):
168 lambda2[i]=0 124 lambda2[i]=0
169 - return (levelp, 0,tuple(lambda2)) 125 + return (levelp, 0, tuple(lambda2))
170 else: 126 else:
171 return (lp, up, lambdap) 127 return (lp, up, lambdap)
172 128
173 -############################## END #################################### 129 +## END ########################################################################
174 ############################################################################### 130 ###############################################################################
175 -###############################################################################
176 -
177 131
178 -####################### MAIN ##################################################
179 132
180 -# compute maximal duration of activities 133 +###############################################################################
181 -D=0 134 +## MAIN #######################################################################
182 -for alpha in potential : D = max(D, alpha[3]) 135 +def andy2snakes(entities, potential, obligatory):
136 + # compute maximal duration of activities
137 + D=0
138 + for alpha in potential : D = max(D, alpha[3])
183 139
184 -for alpha in obligatory : D = max(D, alpha[3]) 140 + for alpha in obligatory : D = max(D, alpha[3])
185 141
186 -n = PetriNet('andy') 142 + n = PetriNet('andy')
187 143
188 -n.globals["obligatory"] = obligatory 144 + n.globals["obligatory"] = obligatory
189 -n.globals["D"] = D 145 + n.globals["D"] = D
190 -n.globals["clockt"] = clockt 146 + n.globals["clockt"] = clockt
191 -n.globals["clockbetat"] = clockbetat 147 + n.globals["clockbetat"] = clockbetat
192 -n.globals["potentialt"] = potentialt 148 + n.globals["potentialt"] = potentialt
193 149
194 -################# Places for entities 150 + ################# Places for entities
195 -for i in range(0,len(entities)): 151 + for i in range(0,len(entities)):
196 name=entities[i][0] 152 name=entities[i][0]
197 level = entities[i][1] 153 level = entities[i][1]
198 deltas = entities[i][2] 154 deltas = entities[i][2]
199 vector = [0]*len(deltas) 155 vector = [0]*len(deltas)
200 n.add_place(Place(name, [(level,0, tuple(vector))])) 156 n.add_place(Place(name, [(level,0, tuple(vector))]))
201 157
202 -################# clock transition 158 + ################# clock transition
203 -inputlist = dict() 159 + inputlist = dict()
204 -n.globals["inputlist"] = inputlist 160 + n.globals["inputlist"] = inputlist
205 161
206 -n.add_transition(Transition('tc')) 162 + n.add_transition(Transition('tc'))
207 163
208 164
209 -# connect all obligatory clocks 165 + # connect all obligatory clocks
210 -for i in range(0,len(obligatory)): 166 + for i in range(0,len(obligatory)):
211 # transition name 167 # transition name
212 obname = 'ob'+str(i) 168 obname = 'ob'+str(i)
213 # for every obligatory activity connect corresponding place to clock 169 # for every obligatory activity connect corresponding place to clock
...@@ -216,8 +172,8 @@ for i in range(0,len(obligatory)): ...@@ -216,8 +172,8 @@ for i in range(0,len(obligatory)):
216 inputlist.update({obname:'w'+obname}) 172 inputlist.update({obname:'w'+obname})
217 173
218 174
219 -# all entities are connected 175 + # all entities are connected
220 -for i in range(0,len(entities)): 176 + for i in range(0,len(entities)):
221 name=entities[i][0] 177 name=entities[i][0]
222 deltas = entities[i][2] 178 deltas = entities[i][2]
223 n.globals["deltas"+name] = deltas 179 n.globals["deltas"+name] = deltas
...@@ -227,19 +183,19 @@ for i in range(0,len(entities)): ...@@ -227,19 +183,19 @@ for i in range(0,len(entities)):
227 183
228 184
229 185
230 -for i in range(0,len(entities)): 186 + for i in range(0,len(entities)):
231 name=entities[i][0] 187 name=entities[i][0]
232 n.add_output(name, 'tc', Expression("clockt(obligatory,"+name+",l"+name+',u'+name+',lambda'+name+',deltas'+name+',inputlist,D)')) 188 n.add_output(name, 'tc', Expression("clockt(obligatory,"+name+",l"+name+',u'+name+',lambda'+name+',deltas'+name+',inputlist,D)'))
233 189
234 190
235 -for i in range(0,len(obligatory)): 191 + for i in range(0,len(obligatory)):
236 obname = 'ob'+str(i) 192 obname = 'ob'+str(i)
237 # for every obligatory activity connect corresponding place to clock 193 # for every obligatory activity connect corresponding place to clock
238 n.add_output('p'+obname, 'tc', Expression("clockbetat(obligatory,"+str(i)+',w'+obname+',inputlist,D)')) 194 n.add_output('p'+obname, 'tc', Expression("clockbetat(obligatory,"+str(i)+',w'+obname+',inputlist,D)'))
239 195
240 196
241 -## potential activities 197 + ## potential activities
242 -for i in range(0,len(potential)): 198 + for i in range(0,len(potential)):
243 # transition name 199 # transition name
244 trname = 'tr'+str(i) 200 trname = 'tr'+str(i)
245 201
...@@ -293,39 +249,83 @@ for i in range(0,len(potential)): ...@@ -293,39 +249,83 @@ for i in range(0,len(potential)):
293 n.add_input(names[j], trname, Tuple([Variable('l'+names[j]), Variable('u'+names[j]), Variable('lambda'+names[j]) ])) 249 n.add_input(names[j], trname, Tuple([Variable('l'+names[j]), Variable('u'+names[j]), Variable('lambda'+names[j]) ]))
294 n.add_output(names[j], trname, Expression("potentialt(" +names[j]+",l"+names[j]+',u'+names[j]+',lambda'+names[j]+', results'+trname+')')) 250 n.add_output(names[j], trname, Expression("potentialt(" +names[j]+",l"+names[j]+',u'+names[j]+',lambda'+names[j]+', results'+trname+')'))
295 251
296 - 252 + return n
297 253
298 ######## depict Petri net 254 ######## depict Petri net
299 -n.draw("repress.ps") 255 +def draw_net(net, out_name='repress'):
300 - 256 + net.draw(out_name+'.ps')
301 -s = StateGraph(n)
302 -s.build()
303 257
304 -def node_attr (state, graph, attr) : 258 +def draw_stategraph(net, entities_names, out_name='repressgraph',
305 - # attr['label'] = str(state) 259 + with_dot=True):
260 + def node_attr (state, graph, attr) :
306 marking = graph[state] 261 marking = graph[state]
307 - attr["label"] = ":".join(str(list(marking(s))[0][0]) 262 + attr["label"] = ":".join( str(list(marking(s))[0][0])
308 - for s in ("s1", "s2", "s3")) 263 + for s in entities_names )
309 - 264 + def edge_attr (trans, mode, attr) :
310 -def edge_attr (trans, mode, attr) :
311 attr["label"] = trans.name 265 attr["label"] = trans.name
312 266
313 -s.draw('repressgraph.ps', node_attr=node_attr, edge_attr=edge_attr, 267 + s = StateGraph(net)
314 - engine="dot") 268 + s.build()
269 +
270 + s.draw(out_name+'.ps', node_attr=node_attr, edge_attr=edge_attr,
271 + engine='dot')
315 272
316 -g = s.draw(None, node_attr=node_attr, edge_attr=edge_attr, engine="dot") 273 + if with_dot:
317 -with open("repressgraph.dot", "w") as out : 274 + g = s.draw(None, node_attr=node_attr, edge_attr=edge_attr,
275 + engine='dot')
276 + with open(out_name+".dot", "w") as out:
318 out.write(g.dot()) 277 out.write(g.dot())
278 + g.render(out_name+"-layout.dot", engine="dot")
279 +
280 +if __name__=='__main__':
281 + # entities: tuple of name of the entities, initial level, tuple of decays 0
282 + # denotes unbounded decay (omega)
283 + # examples:
284 + # entities = ( ('B',4, (0,2,2,2,3)), ('P',0, (0,0)), ('C',0, (0,0)),
285 + # ('G',0, (0,0)) )
286 + # entities = ( ('Sugar',1, (0,2)), ('Aspartame',0, (0,2)),
287 + # ('Glycemia',2, (0,2,2,2)), ('Glucagon',0, (0,2)),
288 + # ('Insulin',0,(0,2,2)) )
289 +
290 + entities = ( ('s1',0, (0,1)), ('s2',0, (0,1)), ('s3',0, (0,1)) )
291 +
292 + # Activities: Tuple of (activators, inhibitors, results, duration)
293 + # activators, inhibitors are dictionaries of pairs
294 + # (entity, level)
295 + # results are dictionaries of pairs (entity, +z)
296 +
297 + # potential activities examples:
298 + # potential = ( (dict([('P',0)]),dict([('P',1)]),dict([('P',1)]),0),
299 + # (dict([('P',1)]),dict(),dict([('P',-1)]),0),
300 + # (dict([('C',0)]),dict([('C',1)]),dict([('C',1)]),0),
301 + # (dict([('C',1)]),dict(),dict([('C',-1)]),0),
302 + # (dict([('G',0)]),dict([('G',1)]),dict([('G',1)]),0),
303 + # (dict([('G',1)]),dict(),dict([('G',-1)]),0) )
304 + # potential = ( (dict([('Sugar',1)]),dict(),
305 + # dict([('Insulin',1),('Glycemia',1)]),0),
306 + # (dict([('Aspartame',1)]),dict(),dict([('Insulin',1)]),0),
307 + # (dict(),dict([('Glycemia',1)]),dict([('Glucagon',1)]),0),
308 + # (dict([('Glycemia',3)]),dict(),dict([('Insulin',1)]),0),
309 + # (dict([('Insulin',2)]),dict(),dict([('Glycemia',-1)]),0),
310 + # (dict([('Insulin',1),('Glycemia',3)]), dict(),
311 + # dict([('Glycemia',-1)]),0),
312 + # (dict([('Insulin',1)]),dict([('Glycemia',2)]),
313 + # dict([('Glycemia',-1)]),0),
314 + # (dict([('Glucagon',1)]),dict(),dict([('Glycemia',+1)]),0)
315 + # )
316 +
317 + potential = ( (dict(), dict([('s1',1)]), dict([('s2',1)]), 1),
318 + (dict(), dict([('s2',1)]), dict([('s3',1)]), 1),
319 + (dict(), dict([('s3',1)]), dict([('s1',1)]), 1) )
320 +
321 + # obligatory activities examples:
322 + # obligatory = ( (dict([('P',1)]),dict(),dict([('B',1)]),1),
323 + # (dict([('C',1)]),dict(),dict([('B',-1)]),3),
324 + # (dict([('G',1)]),dict(),dict([('B',-2)]),3))
325 +
326 + obligatory = ()
327 +
328 + net = andy2snakes(entities, potential, obligatory)
329 + draw_net(net, out_name="repress")
330 + draw_stategraph(net, ("s1", "s2", "s3"), out_name="repressgraph")
319 331
320 -g.render("repressgraph-layout.dot", engine="dot")
321 -
322 -# t=n.transition('tr0')
323 -# m=t.modes()
324 -# print m
325 -# t.fire(m[0])
326 -# n.draw('repr1.ps')
327 -# t1=n.transition('tc')
328 -# m1=t1.modes()
329 -# print m1
330 -# t1.fire(m1[0])
331 -# n.draw('repr1.ps')
......