Jordan de la Houssaye

add andy's code

1 +import snakes.plugins
2 +snakes.plugins.load(['gv', 'ops'], 'snakes.nets', 'nets')
3 +from nets import *
4 +
5 +
6 +
7 +###############################################################################
8 +###############################################################################
9 +############################### INIT ##########################################
10 +
11 +# entities: tuple of name of the entities, initial level, tuple of decays 0
12 +# denotes unbounded decay (omega)
13 +#entities = ( ('B',4, (0,2,2,2,3)), ('P',0, (0,0)), ('C',0, (0,0)), ('G',0, (0,0)))
14 +
15 +
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)))
17 +
18 +
19 +entities = ( ('s1',0, (0,1)), ('s2',0, (0,1)), ('s3',0, (0,1)) )
20 +
21 +
22 +
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
78 + u1=us
79 + lambda1=[]
80 +
81 + # progression of time in lambda
82 + for i in range(0,len(lambdas)): lambda1.append(min(lambdas[i]+1, D))
83 +
84 + # progression of time for u (only for bounded levels)
85 + if deltas[ls] <> 0 : u1=us+1
86 +
87 + # decay
88 + if us+1 > deltas[ls] :
89 + l1 = max(0, ls -1)
90 + u1 = 0
91 +
92 + # search of obligatory activities where entity name is in results
93 + act = []
94 + for alpha in range(0, len(obligatory)) :
95 + obname = 'ob'+str(alpha)
96 + if name in obligatory[alpha][2] and inputlist[obname]>= obligatory[alpha][3]:
97 + act.append(obligatory[alpha])
98 +
99 +
100 +
101 + # computation of the effect on entity name
102 + for alpha in range(0, len(act)) :
103 + # check if the obligatory activity is enabled or not
104 + check = 0
105 + activators = act[alpha][0]
106 + for ent in activators :
107 + t = inputlist[ent]
108 + if t[0] >= activators[ent] and t[2][activators[ent]] >= act[alpha][3] : check = 1
109 + inhibitors = act[alpha][1]
110 + for ent in inhibitors :
111 + t = inputlist[ent]
112 + if t[0] < inhibitors[ent] and t[2][inhibitors[ent]] >= act[alpha][3] : check = 1
113 + # if enabled compute the effect
114 + if check :
115 + z = act[alpha][2][name]
116 + l1 = max(0, min(l1 + z, len(lambda1)-1))
117 + u1 = 0
118 +
119 + # update lambda with the proper dates
120 + temp = l1 - ls
121 + if temp > 0 :
122 + for i in range(ls+1,l1) : lambda1[i]=0
123 + if temp < 0 :
124 + for i in range(l1+1,ls) : lambda1[i]=0
125 +
126 + return (l1, u1, tuple(lambda1))
127 +
128 +
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) :
135 + check = 0
136 + activators = obligatory[name][0]
137 + for ent in activators :
138 + t = inputlist[ent]
139 + if t[0] >= activators[ent] and t[2][activators[ent]] >= obligatory[name][3] : check = 1
140 + inhibitors = obligatory[name][1]
141 + for ent in inhibitors :
142 + t = inputlist[ent]
143 + if t[0] < inhibitors[ent] and t[2][inhibitors[ent]] >= obligatory[name][3] : check = 1
144 + # if enabled compute the effect
145 + if check and w >= obligatory[name][3] : return(0)
146 + else: return(min(w+1, D))
147 +
148 +
149 +
150 +
151 +
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) :
157 + print (name, lp, up, lambdap, R)
158 + # entity is a result?
159 + if (name in R) :
160 + lambda2 = list(lambdap)
161 + levelp = max(0,min(len(lambdap)-1, lp+R[name]))
162 + change = levelp-lp
163 + if change > 0 :
164 + for i in range(lp+1,levelp+1):
165 + lambda2[i]=0
166 + if change < 0 :
167 + for i in range(levelp+1,lp+1):
168 + lambda2[i]=0
169 + return (levelp, 0,tuple(lambda2))
170 + else:
171 + return (lp, up, lambdap)
172 +
173 +############################## END ####################################
174 +###############################################################################
175 +###############################################################################
176 +
177 +
178 +####################### MAIN ##################################################
179 +
180 +# compute maximal duration of activities
181 +D=0
182 +for alpha in potential : D = max(D, alpha[3])
183 +
184 +for alpha in obligatory : D = max(D, alpha[3])
185 +
186 +n = PetriNet('andy')
187 +
188 +n.globals["obligatory"] = obligatory
189 +n.globals["D"] = D
190 +n.globals["clockt"] = clockt
191 +n.globals["clockbetat"] = clockbetat
192 +n.globals["potentialt"] = potentialt
193 +
194 +################# Places for entities
195 +for i in range(0,len(entities)):
196 + name=entities[i][0]
197 + level = entities[i][1]
198 + deltas = entities[i][2]
199 + vector = [0]*len(deltas)
200 + n.add_place(Place(name, [(level,0, tuple(vector))]))
201 +
202 +################# clock transition
203 +inputlist = dict()
204 +n.globals["inputlist"] = inputlist
205 +
206 +n.add_transition(Transition('tc'))
207 +
208 +
209 +# connect all obligatory clocks
210 +for i in range(0,len(obligatory)):
211 + # transition name
212 + obname = 'ob'+str(i)
213 + # for every obligatory activity connect corresponding place to clock
214 + n.add_place(Place('p'+obname, [0]))
215 + n.add_input('p'+obname, 'tc', Variable('w'+obname))
216 + inputlist.update({obname:'w'+obname})
217 +
218 +
219 +# all entities are connected
220 +for i in range(0,len(entities)):
221 + name=entities[i][0]
222 + deltas = entities[i][2]
223 + n.globals["deltas"+name] = deltas
224 + n.globals[name] = name
225 + n.add_input(name, 'tc', Tuple([Variable('l'+name), Variable('u'+name), Variable('lambda'+name) ]))
226 + inputlist.update({name:['l'+name, 'u'+name, 'lambda'+name ]})
227 +
228 +
229 +
230 +for i in range(0,len(entities)):
231 + name=entities[i][0]
232 + n.add_output(name, 'tc', Expression("clockt(obligatory,"+name+",l"+name+',u'+name+',lambda'+name+',deltas'+name+',inputlist,D)'))
233 +
234 +
235 +for i in range(0,len(obligatory)):
236 + obname = 'ob'+str(i)
237 + # 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)'))
239 +
240 +
241 +## potential activities
242 +for i in range(0,len(potential)):
243 + # transition name
244 + trname = 'tr'+str(i)
245 +
246 + # for every potential activity connect corresponding place to clock
247 + n.add_place(Place('p'+trname, [0]))
248 + n.add_input('p'+trname, 'tc', Variable('w'+trname))
249 + n.add_output('p'+trname, 'tc', Expression('min(D,w'+trname+'+1)'))
250 +
251 +
252 + activators = potential[i][0]
253 + inhibitors = potential[i][1]
254 + results = potential[i][2]
255 + print results
256 + n.globals["results"+trname] = results
257 + duration = potential[i][3]
258 +
259 + # compute entities involved in the activity
260 + nameactivators = activators.keys()
261 + nameinhib = inhibitors.keys()
262 + nameresults = results.keys()
263 + names = []
264 + # check they appear only once
265 + for i in nameactivators : names.append(i)
266 + for i in nameinhib:
267 + if not (i in activators) : names.append(i)
268 + for i in nameresults:
269 + if not ( (i in activators) or (i in inhibitors)) : names.append(i)
270 +
271 + # compute guard of the activity
272 + # activity may be executed once every dur
273 + guard = 'w>='+str(duration)
274 +
275 + # activators
276 + for j in range(0,len(nameactivators)) :
277 + spec = nameactivators[j]
278 + level = str(activators[nameactivators[j]])
279 + guard += ' and l'+spec+'>= '+ level + ' and lambda' +spec+'['+level+']>='+str(duration)
280 +
281 + # inhibitors
282 + for j in range(0,len(nameinhib)) :
283 + spec = nameinhib[j]
284 + level = str(inhibitors[nameinhib[j]])
285 + guard += ' and l'+spec+'< '+ level + ' and lambda' +spec+'['+level+']>='+str(duration)
286 +
287 + n.add_transition(Transition(trname, Expression(guard)))
288 + n.add_input('p'+trname, trname, Variable('w'))
289 + n.add_output('p'+trname, trname, Expression('0'))
290 +
291 + # arcs of the transition from and to involved entities
292 + for j in range(0,len(names)) :
293 + 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+')'))
295 +
296 +
297 +
298 +######## depict Petri net
299 +n.draw("repress.ps")
300 +
301 +s = StateGraph(n)
302 +s.build()
303 +
304 +def node_attr (state, graph, attr) :
305 + # attr['label'] = str(state)
306 + marking = graph[state]
307 + attr["label"] = ":".join(str(list(marking(s))[0][0])
308 + for s in ("s1", "s2", "s3"))
309 +
310 +def edge_attr (trans, mode, attr) :
311 + attr["label"] = trans.name
312 +
313 +s.draw('repressgraph.ps', node_attr=node_attr, edge_attr=edge_attr,
314 + engine="dot")
315 +
316 +g = s.draw(None, node_attr=node_attr, edge_attr=edge_attr, engine="dot")
317 +with open("repressgraph.dot", "w") as out :
318 + out.write(g.dot())
319 +
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')