-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
86 lines (68 loc) · 2.53 KB
/
main.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
76
77
78
79
80
81
82
83
84
85
86
from Cutset import cutset
from Backtrack import backtrack
from Map import *
import numpy as np
from timeit import default_timer as timer
import winsound
frequency = 2500
duration = 1000
NUMBER_OF_TESTS = 5
NUMBER_OF_VARIABLES = 500
STEP = 25
def test(*, minimalCutsetSize=1):
y_bt = np.zeros(int(NUMBER_OF_VARIABLES / STEP)+1)
y_cs = np.zeros(int(NUMBER_OF_VARIABLES / STEP)+1)
y_sizeCs = np.zeros(int(NUMBER_OF_VARIABLES / STEP)+1)
y_cs_h = np.zeros(int(NUMBER_OF_VARIABLES / STEP)+1)
y_sizeCs_h = np.zeros(int(NUMBER_OF_VARIABLES / STEP)+1)
x = np.arange(0, NUMBER_OF_VARIABLES+1, STEP)
for i in range(NUMBER_OF_TESTS):
for dimension in range(STEP, NUMBER_OF_VARIABLES+1, STEP):
print('testing: ' + str(dimension) + ' #' + str(i+1), end=' ')
map = generateMap(dimension, minimalCutsetSize=minimalCutsetSize)
csp = map.toCSP()
print('start')
start = timer()
solBt = backtrack(csp)
end = timer()
timeBt = end-start
start = timer()
solCs, size = cutset(csp, heuristic=False)
end = timer()
timeCs = end-start
start = timer()
solCs_h, size_h = cutset(csp)
print(size_h)
end = timer()
timeCs_h = end-start
y_sizeCs[int(dimension/STEP)] += dimension-size
y_sizeCs_h[int(dimension/STEP)] += dimension-size_h
y_bt[int(dimension/STEP)] += timeBt
y_cs[int(dimension/STEP)] += timeCs
y_cs_h[int(dimension/STEP)] += timeCs_h
winsound.Beep(frequency, duration)
y_sizeCs /= NUMBER_OF_TESTS
y_sizeCs_h /= NUMBER_OF_TESTS
y_bt /= NUMBER_OF_TESTS
y_cs /= NUMBER_OF_TESTS
y_cs_h /= NUMBER_OF_TESTS
# PLOT SOLVING TIME
plt.plot(x, y_bt)
plt.plot(x, y_cs)
plt.plot(x, y_cs_h)
plt.legend(['Backtrack', 'Cutset senza euristica', 'Cutset con euristica'])
plt.title('Tempo di risoluzione: backtrack vs cutset')
plt.xlabel('Numero di variabili')
plt.ylabel('Tempo (secondi)')
plt.show()
# PLOT CUTSET SIZE
plt.plot(x, y_sizeCs)
plt.plot(x, y_sizeCs_h)
plt.legend(['Cutset senza euristica', 'Cutset con euristica'])
plt.title('Dimensione cutset effettivo')
plt.xlabel('Numero di variabili')
plt.ylabel('Tempo (secondi)')
plt.show()
if __name__ == "__main__":
test(minimalCutsetSize=1)
test(minimalCutsetSize=2)