checker.py
14.8 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
import netverifcar
import libsverifcar
import time
#______________________PROPOSITIONS & HEURISTICS______________________
#___________Arrival Order___________
def arrival_i_before_j(state,i,j) :
vehicles = list(state["vehicles"].items())
for v in vehicles :
if v.id == i :
pi = v.posX
if v.id == j :
pj = v.posX
if pi == libsverifcar.LengthX and pi > pj :
return True
return False
#Heuristic for arrival_i_before_j
def distance_i_j(state,i,j) :
vehicles = list(state["vehicles"].items())
for v in vehicles :
if v.id == i :
pi = v.posX
if v.id == j :
pj = v.posX
return pi - pj
#Negation of arrival_i_before_j
def not_arrival_i_before_j(state,i,j) :
vehicles = list(state["vehicles"].items())
for v in vehicles :
if v.id == i :
pi = v.posX
if v.id == j :
pj = v.posX
if pi == libsverifcar.LengthX and pi > pj :
return False
return True
#___________Travel Time___________
#Travel time > n
def travel_time_i_sup_n(state,i,n) :
vehicles = list(state["vehicles"].items())
for v in vehicles :
if v.id == i and v.posX == libsverifcar.LengthX :
return v.nb_updates > n
return False
#Heuristic for travel_time_i_sup_n
def hestimated_travel_time(state,i,n) :#n for compilation
vehicles = list(state["vehicles"].items())
for v in vehicles :
if v.id == i and v.nb_updates > 0:
return v.nb_updates+(((libsverifcar.LengthX-v.posX)/v.speed)*10)
return 0
#Travel time < n
def travel_time_i_inf_n(state,i,n) :
vehicles = list(state["vehicles"].items())
for v in vehicles :
if v.id == i and v.posX == libsverifcar.LengthX :
return v.nb_updates < n
return False
#Heuristic for travel_time_i_inf_n
def hestimated_travel_time_inv(state,i,n) :#n for compilation
vehicles = list(state["vehicles"].items())
for v in vehicles :
if v.id == i and v.nb_updates > 0:
return -(v.nb_updates+(((libsverifcar.LengthX-v.posX)/v.speed)*10))
return 0
#___________Time-to-collision___________
#TTC < n
def ttc_i_j_inf_n(state,i,j,n) :
vehicles = list(state["vehicles"].items())
for v in vehicles :
if v.id == i :
vi = v
if v.posX == libsverifcar.LengthX :
return False
if v.id == j :
vj = v
if v.posX == libsverifcar.LengthX :
return False
return netverifcar.time_to_collision(vi, vj) < n
#Heuristic for ttc_i_j_inf_n
def time_to_overtake_i_j_inv(state,i,j,n) :#n for compilation
vehicles = list(state["vehicles"].items())
for v in vehicles :
if v.id == i :
vi = v
if v.posX == libsverifcar.LengthX :
return -100*libsverifcar.scale
if v.id == j :
vj = v
if v.posX == libsverifcar.LengthX :
return -100*libsverifcar.scale
if vi.posX > vj.posX :
if vi.speed < vj.speed :
return -(vi.posX-vj.posX)/(vj.speed-vi.speed)
else :
return -100*libsverifcar.scale
elif vj.posX > vi.posX :
if vj.speed < vi.speed :
return -(vj.posX-vi.posX)/(vi.speed-vj.speed)
else :
return -100*libsverifcar.scale
else :
return 0
#TTC > n
def ttc_i_j_sup_n(state,i,j,n) :
vehicles = list(state["vehicles"].items())
for v in vehicles :
if v.id == i :
vi = v
if v.posX == libsverifcar.LengthX :
return True
if v.id == j :
vj = v
if v.posX == libsverifcar.LengthX :
return True
return netverifcar.time_to_collision(vi, vj) > n
#Heuristic for ttc_i_j_inf_n
def time_to_overtake_i_j(state,i,j,n) :#n for compilation
vehicles = list(state["vehicles"].items())
for v in vehicles :
if v.id == i :
vi = v
if v.posX == libsverifcar.LengthX :
return 100*libsverifcar.scale
if v.id == j :
vj = v
if v.posX == libsverifcar.LengthX :
return 100*libsverifcar.scale
if vi.posX > vj.posX :
if vi.speed < vj.speed :
return (vi.posX-vj.posX)/(vj.speed-vi.speed)
else :
return 100*libsverifcar.scale
elif vj.posX > vi.posX :
if vj.speed < vi.speed :
return (vj.posX-vi.posX)/(vi.speed-vj.speed)
else :
return 100*libsverifcar.scale
else :
return 0
#______________________EXPLORATION ALGORITHMS______________________
#___________Sub-functions___________
def strong_equal(state1,state2) :
v1 = list(state1["vehicles"].items())
v2 = list(state2["vehicles"].items())
for i in range(len(v1)):
if not v1[i].strong_equal(v2[i]) :
return False
return True
def final_state(state) :
vehicles = list(state["vehicles"].items())
for v in vehicles :
if v.posX != libsverifcar.LengthX :
return False
return True
def next_border(init) :
finals = set()
exploring = set()
exploring.add(init)
while exploring :
for i in exploring.copy() :
itersucc = list(netverifcar.itersucc(i))
exploring.remove(i)
for s in itersucc :
marking = i-s[2]+s[3]
if s[0] == 'update' :
#if s[0] == 'signal' :
finals.add(marking)
else :
exploring.add(marking)
return finals
def next_border_set(init_set) :
finals = []
exploring = init_set
while exploring :
for i in exploring.copy() :
itersucc = list(netverifcar.itersucc(i))
exploring.remove(i)
for s in itersucc :
marking = i-s[2]+s[3]
if s[0] == 'update' :
#if s[0] == 'signal' :
state_added = False
for aset in finals :
if strong_equal(aset.copy().pop(),marking) :
aset.add(marking)
state_added = True
if not state_added :
finals.append({marking})
else :
exploring.add(marking)
return finals
#___________Full Exploration Algorithms___________
#Standard width first exploration
def basic_width() :
start_time = time.time()
exploring = [netverifcar.init()]
finals = set()
state_counter = 0
while exploring :
#print(len(exploring))
succ = netverifcar.succ(exploring.pop(0))
state_counter = state_counter + 1
for s in succ :
if final_state(s) :
finals.add(s)
print("--- %s seconds ---" % (time.time() - start_time))
elif s not in exploring :
exploring.append(s)
else :
print("Found interliving")
print("Exploration is over. Number of states explored : ", state_counter)
print("--- %s seconds ---" % (time.time() - start_time))
return finals
#Standard depth first exploration (no results to be expected)
def basic_depth() :
start_time = time.time()
exploring = [netverifcar.init()]
finals = set()
state_counter = 0
while exploring :
succ = netverifcar.succ(exploring.pop())
state_counter = state_counter + 1
for s in succ :
if final_state(s) :
finals.add(s)
print(len(exploring))
print("--- %s seconds ---" % (time.time() - start_time))
else :
exploring.append(s)
print("Exploration is over. Number of states explored : ", state_counter)
print("--- %s seconds ---" % (time.time() - start_time))
return finals
#Layered depth first exploration without weak variables
def strong_explore_depth() :
start_time = time.time()
exploring = [netverifcar.init()]
finals = set()
state_counter = 0
while exploring :
#print(len(exploring))
succ_set = next_border(exploring.pop())
state_counter = state_counter + 1
for s in succ_set :
if final_state(s) :
finals.add(s)
print(len(exploring))
print("--- %s seconds ---" % (time.time() - start_time))
else :
exploring.append(s)
print("Exploration is over. Number of states explored : ", state_counter)
print("--- %s seconds ---" % (time.time() - start_time))
return finals
#Layered depth first exploration with weak variables
def explore_depth() :
start_time = time.time()
exploring = [{netverifcar.init()}]
finals = set()
state_counter = 0
final_counter = 0
while exploring :
#print(len(exploring))
succ_set = next_border_set(exploring.pop())
state_counter = state_counter + 1
for aset in succ_set :
explore_set = set()
for s in aset :
if final_state(s) :
finals.add(s)
final_counter = final_counter + 1
print("Found %s th final state" % (final_counter))
print("--- %s seconds ---" % (time.time() - start_time))
else :
explore_set.add(s)
if explore_set :
exploring.append(explore_set)
print("Exploration is over. Number of states explored : ", state_counter)
print("--- %s seconds ---" % (time.time() - start_time))
return finals
#Width first exploration for paths extrema of a set of indicators (any number of indicator can be added)
def bounds_value(value1,value1_args,value2,value2_args,value3,value3_args) :
start_time = time.time()
init = netverifcar.init()
v1 = value1(init,*value1_args)
v2 = value2(init,*value2_args)
v3 = value3(init,*value3_args)
exploring = [(init,v1,v1,v2,v2,v3,v3)]
finals = set()
state_counter = 0
while exploring :
#print(len(exploring))
explored = exploring.pop(0)
succ = next_border(explored[0])
state_counter = state_counter + 1
for s in succ :
state_value1 = value1(s,*value1_args)
min_path_value1 = min(explored[1],state_value1)
max_path_value1 = max(explored[2],state_value1)
state_value2 = value2(s,*value2_args)
min_path_value2 = min(explored[3],state_value2)
max_path_value2 = max(explored[4],state_value2)
state_value3 = value3(s,*value3_args)
min_path_value3 = min(explored[5],state_value3)
max_path_value3 = max(explored[6],state_value3)
if final_state(s) :
finals.add((s,min_path_value1,max_path_value1,min_path_value2,max_path_value2,min_path_value3,max_path_value3))
elif (s,min_path_value1,max_path_value1,min_path_value2,max_path_value2,min_path_value3,max_path_value3) not in exploring :
exploring.append((s,min_path_value1,max_path_value1,min_path_value2,max_path_value2,min_path_value3,max_path_value3))
else :
print("Found interliving")
print("--- %s seconds ---" % (time.time() - start_time))
print("Exploration is over. Number of states explored : ", state_counter)
return finals
#___________Reachability Algorithms___________
#EF p in depth first with heuristic
def exist_state(prop,heuristic,func_args) :
start_time = time.time()
exploring = [({netverifcar.init()},heuristic(netverifcar.init(),*func_args))]
state_counter = 0
while exploring :
#print(len(exploring))
succ_set = next_border_set(exploring.pop()[0])
state_counter = state_counter + 1
for s in succ_set :
state=s.copy().pop()
if prop(state,*func_args) :
print("Exploration is over. Number of states explored : ", state_counter)
print("--- %s seconds ---" % (time.time() - start_time))
print(s)
return True
elif not final_state(state) :
weight = heuristic(state,*func_args)
index = len(exploring)
if index == 0 :
exploring.append((s,weight))
elif exploring[0][1] > weight :
exploring.insert(0,(s,weight))
else :
while exploring[index-1][1] > weight and index > 1 :#to improve with dichotomy
index = index - 1
exploring.insert(index,(s,weight))
print("Exploration is over. Number of states explored : ", state_counter)
print("--- %s seconds ---" % (time.time() - start_time))
return False
#EF p in width first
def width_exist(func,func_args) :
start_time = time.time()
exploring = [netverifcar.init()]
state_counter = 0
while exploring :
#print(len(exploring))
succ_set = next_border(exploring.pop(0))
state_counter = state_counter + 1
for s in succ_set :
if func(s,*func_args) :
print("Exploration is over. Number of states explored : ", state_counter)
print("--- %s seconds ---" % (time.time() - start_time))
print(s)
return True
if s not in exploring and not final_state(s):
exploring.append(s)
print("Exploration is over. Number of states explored : ", state_counter)
print("--- %s seconds ---" % (time.time() - start_time))
return False
#EG p in depth first with heuristic
def exist_path(prop,heuristic,func_args) :
start_time = time.time()
exploring = [({netverifcar.init()},heuristic(netverifcar.init(),*func_args))]
state_counter = 0
while exploring :
#print(len(exploring))
succ_set = next_border_set(exploring.pop()[0])
state_counter = state_counter + 1
for s in succ_set :
state=s.copy().pop()
if prop(state,*func_args) :
if final_state(state) :
print("Exploration is over. Number of states explored : ", state_counter)
print("--- %s seconds ---" % (time.time() - start_time))
print(s)
return True
else :
weight = heuristic(state,*func_args)
index = len(exploring)
if index == 0 :
exploring.append((s,weight))
elif exploring[0][1] > weight :
exploring.insert(0,(s,weight))
else :
while exploring[index-1][1] > weight and index > 1 :#to improve with dichotomy
index = index - 1
exploring.insert(index,(s,weight))
print("Exploration is over. Number of states explored : ", state_counter)
print("--- %s seconds ---" % (time.time() - start_time))
return False
#EG p in width first
def width_path(func,func_args) :
start_time = time.time()
exploring = [netverifcar.init()]
state_counter = 0
while exploring :
#print(len(exploring))
succ_set = next_border(exploring.pop(0))
state_counter = state_counter + 1
for s in succ_set :
if func(s,*func_args) :
if final_state(s) :
print("Exploration is over. Number of states explored : ", state_counter)
print("--- %s seconds ---" % (time.time() - start_time))
return True
elif s not in exploring :
exploring.append(s)
else :
print("Found interliving")
print("Exploration is over. Number of states explored : ", state_counter)
print("--- %s seconds ---" % (time.time() - start_time))
return False
#EF (p and EF q) in depth first
def EFpEFq(prop1,prop1_args,prop2,prop2_args) :
start_time = time.time()
exploring = [({netverifcar.init()},False)]
state_counter = 0
while exploring :
#print(len(exploring))
main_succ = exploring.pop()
succ_set = next_border_set(main_succ[0])
state_counter = state_counter + 1
for s in succ_set :
state=s.copy().pop()
mark = False
if prop1(state,*prop1_args) or main_succ[1] :
mark = True
if mark and prop2(state,*prop2_args) :
print("Exploration is over. Number of states explored : ", state_counter)
print("--- %s seconds ---" % (time.time() - start_time))
print(s)
return True
elif not final_state(state) :
exploring.append((s,mark))
print("Exploration is over. Number of states explored : ", state_counter)
print("--- %s seconds ---" % (time.time() - start_time))
return False
#EF (p and EG q) in depth first
def EFpEGq(prop1,prop1_args,prop2,prop2_args) :
start_time = time.time()
exploring = [({netverifcar.init()},False)]
state_counter = 0
while exploring :
#print(len(exploring))
main_succ = exploring.pop()
succ_set = next_border_set(main_succ[0])
state_counter = state_counter + 1
for s in succ_set :
state=s.copy().pop()
mark = False
if (prop1(state,*prop1_args) or main_succ[1]) and prop2(state,*prop2_args) :
mark = True
if mark and final_state(state) :
print("Exploration is over. Number of states explored : ", state_counter)
print("--- %s seconds ---" % (time.time() - start_time))
print(s)
return True
elif not final_state(state) :
exploring.append((s,mark))
print("Exploration is over. Number of states explored : ", state_counter)
print("--- %s seconds ---" % (time.time() - start_time))
return False