-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModel_Bounded.py
More file actions
549 lines (500 loc) · 20.6 KB
/
Model_Bounded.py
File metadata and controls
549 lines (500 loc) · 20.6 KB
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
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
#! /home/ubu/projects/probmc/storm/pycarl/env/bin/python3
import itertools
from z3 import Solver, Int, And, Or, sat, simplify, ToInt
from Parser import Parser
import time
import json
import subprocess
import copy
# from Min_Max import get_min_max_bound_only, get_min_max_probability
#
def CEX_GEN(json_data):
start_time = time.time()
#parsing parameters from json elements
jani_path = json_data['jani_path']
model_name = json_data['model_name']
model_path = json_data['model_path']
K = int(json_data['starting_bound'])
csl_prop_lb = json_data['csl_property']
csl_prop_ub = json_data['csl_property_ub']
target_var = json_data['target_variable']
target_value = int(json_data['target_value'])
max_comb = int(json_data['max_combination'])
storm_bin = json_data["storm_binary"]
#
#parse the model into Parser() object
model = Parser(model_path)
target_index = model.species_to_index_dict[target_var]
#
#generate subsets (keys for the min_max dictionary)
index_tuple = (0,)
for i in range(1,len(model.get_species_tuple())):
index_tuple = index_tuple + (i,)
subsets = get_subsets(index_tuple, 1, max_comb)
#
print("\nStarting...")
#
min_max_dict = {}
for s in subsets:
temp = 0
for e in s:
temp = temp + model.get_initial_state()[e]
min_max_dict[s] = [temp, temp]
#
while (True):
print("Bound = " + str(K))
before = time.time()
flag, min_max_dict = get_min_max(model, K, target_index, target_value, subsets, min_max_dict)
print("Generating min_max dictonary took " + str(time.time() - before) + "seconds.")
#
if flag:
JSON_Parser(model, model_name, K, jani_path, min_max_dict)
print("Calling Storm to calculate the probability... \n\n")
#running storm on the produced output
stdout_result = subprocess.run([storm_bin, "--jani", "./results/" + model_name + "/bounds/" + model_name + "_" + str(K) + ".jani", '--prop', csl_prop_lb], stdout=subprocess.PIPE)
stdout_result = stdout_result.stdout.decode('utf-8')
print(stdout_result)
else:
print("No additional witnesses found for bound " + str(K))
#
K = K + 1
print("\n \nRunning time: " + str(time.time() - start_time) + " seconds")
print("=" * 50)
#
#
def JSON_Parser(model, model_name, K, jani_path, min_max_dict):
try:
with open(jani_path, "r") as json_file:
parsed_json = json.load(json_file)
except FileNotFoundError:
print(f"File '{jani_path}' not found.")
except json.JSONDecodeError as e:
print(f"Failed to parse JSON: {e}")
species_tuple = model.get_species_tuple()
bounds_tuple = ([None, None], )
for i in range(1,len(model.get_species_tuple())):
bounds_tuple = bounds_tuple + ([None, None],)
for e in min_max_dict:
if len(e) == 1:
for i, s in enumerate(species_tuple):
if i==e[0]:
bounds_tuple[i][0] = min_max_dict[e][0]
bounds_tuple[i][1] = min_max_dict[e][1]
global_variables = parsed_json["variables"]
automata = parsed_json["automata"]
#adding lower-bound and upper-bound to global variables of the model
for gv in global_variables:
for i, s in enumerate(species_tuple):
if gv["name"] == s:
type_ = {"base" : "int",
"kind" : "bounded",
"lower-bound" : bounds_tuple[i][0],
"upper-bound" : bounds_tuple[i][1]
}
gv["type"] = type_
#
#adding lower-bound and upper-bound to local variables of an automaton (module)
for automaton in automata:
if "variables" in automaton:
local_variables = automaton["variables"]
for lv in local_variables:
for i, s in enumerate(species_tuple):
if lv["name"] == s:
type_ = {"base" : "int",
"kind" : "bounded",
"lower-bound" : bounds_tuple[i][0],
"upper-bound" : bounds_tuple[i][1]
}
lv["type"] = type_
#
#adding a new sink variable to the model
global_variables = parsed_json["variables"]
sink_variable = {
"initial-value": 0,
"name": "sink_var",
"type": {
"base": "int",
"kind": "bounded",
"lower-bound": 0,
"upper-bound": 1
}
}
global_variables.append(sink_variable)
#
#generating the guards that guarantee reducing variables does not result in variables value
#dropping below its lower-bound
for automaton in automata:
edges = automaton["edges"]
for edge in edges:
destinations = edge["destinations"]
for destination in destinations:
if "assignments" in destination:
assignments = destination["assignments"]
lower_bound_guards = []
for assignment in assignments:
for i, s in enumerate(species_tuple):
if assignment["ref"] == s:
value = assignment["value"]
if "op" not in value:
raise Exception("unsupported value assignment")
if value["op"] == "-":
rhs = value["right"]
gt_guard = {"left" : s, "op" : ">", "right" : bounds_tuple[i][0] + rhs - 1}
lower_bound_guards.append(gt_guard)
if "guard" not in edge:
# raise Exception("an edge does not have a guard")
guard = {'comment': 'generated empty guard', 'exp': True}
edge["guard"] = guard
else:
guard = edge["guard"]
flag = False
for g in lower_bound_guards:
if not flag:
guard["exp"] = {"left" : guard["exp"], "op": "∧", "right" : g}
else:
guard["exp"] = {"left" : guard["exp"]["left"], "op" : "∧", "right" : {"left": guard["exp"]["right"], "op" : "∧", "right" : g}}
flag = True
if flag:
edge["comment"] = "modified"
guard["comment"] = "modified! old comment: " + guard["comment"]
#
#generating the guards that guarantee increasing variables does not result in variables value
#going above its upper-bound
for automaton in automata:
edges = automaton["edges"]
for edge in edges:
destinations = edge["destinations"]
for destination in destinations:
if "assignments" in destination:
assignments = destination["assignments"]
upper_bound_guards = []
for assignment in assignments:
for i, s in enumerate(species_tuple):
if assignment["ref"] == s:
value = assignment["value"]
if "op" not in value:
raise Exception("unsupported value assignment")
if value["op"] == "+":
rhs = value["right"]
lt_guard = {"left" : s, "op" : "<", "right" : bounds_tuple[i][1] - rhs + 1}
upper_bound_guards.append(lt_guard)
if "guard" not in edge:
# raise Exception("an edge does not have a guard")
guard = {'comment': 'generated empty guard', 'exp': True}
edge["guard"] = guard
else:
guard = edge["guard"]
flag = False
for g in upper_bound_guards:
if (not flag) and ("modified" not in guard["comment"]):
guard["exp"] = {"left" : guard["exp"], "op": "∧", "right" : g}
else:
guard["exp"] = {"left" : guard["exp"]["left"], "op" : "∧", "right" : {"left": guard["exp"]["right"], "op" : "∧", "right" : g}}
flag = True
if flag:
edge["comment"] = "modified"
guard["comment"] = "modified! old comment: " + guard["comment"]
#
#expanding the guards for all edges with min_max constraints
min_max_exp = {}
for e in min_max_dict:
lhs = {}
if len(e) == 1:
lhs = {"left" : species_tuple[e[0]], "op" : "+", "right" : 0}
else:
flag = True
for i in e:
if flag:
lhs = {"left" : species_tuple[i], "op" : "+", "right" : 0}
flag = False
else:
lhs = {"left" : lhs, "op" : "+", "right" : species_tuple[i]}
if len(min_max_exp) == 0:
min_max_exp = {"left" : {"left" : lhs, "op": ">", "right" : min_max_dict[e][0] - 1},
"op": "∧",
"right": {"left" : lhs, "op": "<", "right" : min_max_dict[e][1] + 1}
}
else:
min_max_exp = {"left" : min_max_exp, "op" : "∧",
"right" : { "left" : {"left" : lhs, "op": ">", "right" : min_max_dict[e][0] - 1},
"op": "∧",
"right": {"left" : lhs, "op": "<", "right" : min_max_dict[e][1] + 1}
}
}
for automaton in automata:
edges = automaton["edges"]
for edge in edges:
if "guard" not in edge:
# raise Exception("an edge does not have a guard")
guard = {'comment': 'generated empty guard', 'exp': True}
edge["guard"] = guard
else:
guard = edge["guard"]
if ("modified" not in guard["comment"]):
guard["exp"] = {"left" : guard["exp"], "op": "∧", "right" : min_max_exp}
else:
guard["exp"] = {"left" : guard["exp"]["left"], "op" : "∧", "right" : {"left": guard["exp"]["right"], "op" : "∧", "right" : min_max_exp}}
guard["comment"] = "modified! old comment: " + guard["comment"]
#
#Iterating over all the edges in automata and generating a new edge
#going to sink state for each modified edge in order to maintain
#semantics of the original model
##1-generating the sink state:
sink_assignments = []
sink_assignments.append({"ref" : "sink_var", "value" : 1})
for i, s in enumerate(species_tuple):
sink_assignments.append({"ref" : s, "value" : bounds_tuple[i][0]-1})
##
for automaton in automata:
edges = automaton["edges"]
new_edges = []
for edge in edges:
if "comment" not in edge:
continue
if edge["comment"] != "modified":
continue
new_edge = copy.deepcopy(edge)
new_edge["comment"] = "semantics edge"
guard = new_edge["guard"]
guard["comment"] = "semantics guard"
guard["exp"] = {"left" : guard["exp"]["left"], "op": "∧", "right" : {"op" : "¬", "exp" : guard["exp"]["right"]}}
destinations = new_edge["destinations"]
for destination in destinations:
if "assignments" in destination:
destination["assignments"] = sink_assignments
new_edges.append(new_edge)
for new_edge in new_edges:
edges.append(new_edge)
#
#Exporting the modified model for a bound into a jani file
file_path = "./results/" + model_name + "/bounds/" + model_name + "_" + str(K) + ".jani"
try:
with open(file_path, "w") as json_file:
json.dump(parsed_json, json_file, indent=4, ensure_ascii=False)
print(f"JSON data saved to '{file_path}' successfully.")
except IOError:
print(f"Unable to save JSON data to '{file_path}'.")
#
#
#returns all the subsets of a tuple with cardinality in range [L, U]
#returns a list of tuples where each tuple is a subset
#utility functions
############ Functions Generating Min_Max via SMT-Solving ############
#returns all the subsets of a tuple with cardinality in range [L, U]
#returns a list of tuples where each tuple is a subset
############ Functions Generating Min_Max via SMT-Solving ############
#returns all the subsets of a tuple with cardinality in range [L, U]
#returns a list of tuples where each tuple is a subset
def get_subsets (tuple, L, U):
if U>len(tuple):
U = len(tuple)
return list(itertools.chain.from_iterable(itertools.combinations(tuple, r) for r in range(L, U+1)))
#
#generating a dictionary.
#Keys: combinations of variables:(s1,), (s1,s2), ...
#Values: a list with l[0] the minimum value and l[1] the maximum value for that key
def get_min_max(model, bound, target_index, target_value, subsets, min_max_prev):
min_max = {}
index_tuple = (0,)
for i in range(1,len(model.get_species_tuple())):
index_tuple = index_tuple + (i,)
for s in subsets:
min_max[s] = min_max_prev[s]
vars = []
for i, r in enumerate(model.get_reactions_vector()):
x= Int("n_i_" + str(i))
vars.append(x)
for i, r in enumerate(model.get_reactions_vector()):
x= Int("n_t_" + str(i))
vars.append(x)
constraints = []
#first constraint (n_i_0,n_t_0,n_i_1,n_t_1,... >=0)
for i in vars:
constraints.append(i>=0)
#second constraint (n_i_0+n_i_t+n_i_1+...<=bound)
sum = 0
for i in vars:
sum = sum + i
constraints.append((sum<=bound))
#third constraint (reaching the target)
vars = []
for i, r in enumerate(model.get_reactions_vector()):
if r[target_index]!=0:
vars.append([Int("n_i_" + str(i)), r[target_index]])
vars.append([Int("n_t_" + str(i)), r[target_index]])
sum = model.get_initial_state()[target_index]
for i in vars:
sum = sum + i[0]*i[1]
constraints.append(sum==target_value)
#fourth constraint (species population>=0)
for i, s in enumerate(model.get_species_tuple()):
vars_i = []
vars = []
for j, r in enumerate(model.get_reactions_vector()):
if r[i]!=0:
vars.append([Int("n_i_"+str(j)), r[i]])
vars.append([Int("n_t_"+str(j)), r[i]])
vars_i.append([Int("n_i_"+str(j)), r[i]])
sum = model.get_initial_state()[i]
for j in vars:
sum = sum + j[0]*j[1]
constraints.append(sum>=0)
sum = model.get_initial_state()[i]
for j in vars_i:
sum = sum + j[0]*j[1]
constraints.append(sum>=0)
solver = Solver()
solver.add(And(constraints))
for i, e in enumerate(min_max):
solver.push()
while (solver.check()==sat):
assignment = solver.model()
vars = []
for d in assignment.decls():
if "n_i_" in str(d.name()):
r_index = int(d.name()[4:])
vars.append([r_index, assignment[d].as_long(), d.name()])
max_value = 0
for j in e:
max_value = max_value + model.get_initial_state()[j]
for v in vars:
for j in e:
max_value = max_value + (model.get_reactions_vector()[v[0]][j] * v[1])
if max_value > min_max[e][1]:
min_max[e][1] = max_value
elif min_max[e][1] != -1:
max_value = min_max[e][1]
else:
min_max[e][1] = max_value
sum = 0
for j in e:
sum = sum + model.get_initial_state()[j]
for v in vars:
for j in e:
sum = sum + (Int(v[2]) * model.get_reactions_vector()[v[0]][j])
solver.add(sum>max_value)
solver.pop()
flag = False
for i, e in enumerate(min_max):
solver.push()
while (solver.check()==sat):
flag = True
assignment = solver.model()
vars = []
for d in assignment.decls():
if "n_i_" in str(d.name()):
r_index = int(d.name()[4:])
vars.append([r_index, assignment[d].as_long(), d.name()])
min_value = 0
for j in e:
min_value = min_value + model.get_initial_state()[j]
for v in vars:
for j in e:
min_value = min_value + (model.get_reactions_vector()[v[0]][j] * v[1])
min_max[e][0] = min_value
if min_value < min_max[e][0]:
min_max[e][0] = min_value
elif min_max[e][0] != -1:
min_value = min_max[e][0]
else:
min_max[e][0] = min_value
sum = 0
for j in e:
sum = sum + model.get_initial_state()[j]
for v in vars:
for j in e:
sum = sum + (Int(v[2]) * model.get_reactions_vector()[v[0]][j])
solver.add(sum<min_value)
solver.pop()
return flag, min_max
# def get_min_max(model, bound, subsets, min_max_dict, target_var, target_value):
# print(len(min_max_dict))
# count = 0
# flag = False
# solver = Solver()
# solver.add(initial_state_enc(model))
# for i in range(1, bound+1):
# solver.add(bound_enc(model, i))
# solver.add(target_enc(bound, target_var, target_value))
# solver.push()
# for s in subsets:
# min = min_max_dict[s][0]
# max = min_max_dict[s][1]
# #
# constraints = []
# for i in range(1, bound+1):
# sum = 0
# for e in s:
# species = model.index_to_species_dict[e]
# var = Int(species + '.' + str(i))
# sum = sum + var
# constraints.append(Or(sum < min, sum > max))
# min_max_enc = Or(constraints)
# #
# count = count + 1
# count_flag = False
# while(solver.check(min_max_enc) == sat):
# if count_flag:
# count = count + 1
# count_flag = True
# # print(min)
# # print(max)
# # print('---')
# flag = True
# for i in range(1, bound+1):
# temp = 0
# for e in s:
# species = model.index_to_species_dict[e]
# var = Int(species + '.' + str(i))
# temp = temp + int(str(solver.model()[var]))
# if temp < min :
# min = temp
# min_max_dict[s][0] = min
# elif temp > max:
# max = temp
# min_max_dict[s][1] = max
# #
# constraints = []
# for i in range(1, bound+1):
# sum = 0
# for e in s:
# species = model.index_to_species_dict[e]
# var = Int(species + '.' + str(i))
# sum = sum + var
# constraints.append(Or(sum < min, sum > max))
# min_max_enc = Or(constraints)
# #
# print("number of times solver was called: " + str(count))
# return flag, min_max_dict
# #returns constraint encoding for the initial state
# def initial_state_enc(model):
# species_vector = model.get_species_tuple()
# initial_state = model.get_initial_state()
# constraints = []
# for i, s in enumerate(species_vector):
# var_name = s + '.' + '0'
# x = Int(var_name)
# init_value = initial_state[i]
# constraints.append(x == init_value)
# return And(constraints)
# def bound_enc(model, bound):
# species_vector = model.get_species_tuple()
# constraints = []
# for j, r in enumerate(model.get_reactions_vector()):
# r_constraints = []
# for i, s in enumerate(species_vector):
# var_name_prev = s + '.' + str(bound-1)
# var_name_curr = s + '.' + str(bound)
# x = Int(var_name_prev)
# y = Int(var_name_curr)
# r_constraints.append(y == (x + model.get_reactions_vector()[j][i]))
# r_constraints.append(y >= 0)
# # reaction_var_name = 'selected_reaction.' + str(bound-1)
# # selected_reaction = Int(reaction_var_name)
# # r_constraints.append(selected_reaction == (j+1))
# constraints.append(And(r_constraints))
# return simplify(Or(constraints))
# def target_enc(bound, target_var, target_value):
# target_var_name = target_var + '.' + str(bound)
# x = Int(target_var_name)
# return (x == target_value)