forked from yuemning/dc-checking
-
Notifications
You must be signed in to change notification settings - Fork 0
/
evaluation.py
75 lines (60 loc) · 1.98 KB
/
evaluation.py
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
from dc_checking.temporal_network import SimpleTemporalConstraint, SimpleContingentTemporalConstraint, TemporalNetwork
from random import randint
from dc_checking.dc_milp import DCCheckerMILP
from dc_checking.dc_be import DCCheckerBE
import timeit
def generate_network(num_cont=5):
"""
Generate a somewhat randomized network where NUM_CONT
is the number of contingent links
"""
network = TemporalNetwork()
# Add contingent links
for i in range(num_cont):
from_event = 'cont:start:' + str(i)
to_event = 'cont:end:' + str(i)
network.add_constraint(SimpleContingentTemporalConstraint(from_event, to_event, 0, randint(1, 4), 'cont:' + str(i)))
# Add requirement links
idx = 0
for i in range(num_cont):
for j in range(num_cont):
for k in range(2):
for l in range(2):
if i < j and randint(0, 4 * num_cont - 1) == 0:
if k == 0:
from_event = 'cont:start:' + str(i)
else:
from_event = 'cont:end:' + str(i)
if l == 0:
to_event = 'cont:start:' + str(j)
else:
to_event = 'cont:end:' + str(j)
network.add_constraint(SimpleTemporalConstraint(from_event, to_event, 0, randint(1, 4), 'req:' + str(idx)))
idx += 1
return network
def run_random_stnus(num_trials, num_cont=5):
"""
Generate NUM_TRIALS networks and run it on BE and MILP checking
"""
# Generate networks
networks = []
for i in range(num_trials):
networks.append(generate_network(num_cont))
# Run BE algorithm
start = timeit.default_timer()
for network in networks:
checker = DCCheckerBE(network)
controllable, conflict = checker.is_controllable()
# print(controllable, conflict)
stop = timeit.default_timer()
print('BE Time: ', stop - start)
# Run MILP algorithm
start = timeit.default_timer()
for network in networks:
checker = DCCheckerMILP(network)
controllable, _ = checker.is_controllable()
# print(controllable)
stop = timeit.default_timer()
print('MILP Time: ', stop - start)
if __name__ == '__main__':
run_random_stnus(3, 10)