-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPigeons.py
129 lines (97 loc) · 2.77 KB
/
Pigeons.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
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
from time import *
from matplotlib import pyplot as plt
from DPLL import *
def pigeons(n):
literals = [i for i in range(1, n*(n-1)+1)]
literals_sorted = []
cnf = []
for i in range(n):
clause = []
for j in range(n-1):
clause.append(literals[i*(n-1) + j])
cnf.append(clause)
for i in range(n-1):
cage = []
for j in range(n):
cage.append(literals[j*(n-1) + i])
literals_sorted.append(cage)
for i in range(n):
for j in range(n-1):
for k in range(n-j-2):
clause = [-literals[i*(n-1) + j], -literals[i*(n-1) + j + k + 1]]
cnf.append(clause)
for i in range(n-1):
for j in range(n):
for k in range(n-j-1):
clause = [-literals_sorted[i][j], -literals_sorted[i][j + k + 1]]
cnf.append(clause)
return n, literals, cnf
def time_pigeons_once(n):
start = time()
a = pigeons(n)
end = time()
exe_time = end - start
print(len(a[2]))
return exe_time
def time_pigeons_plt(n, s=1):
x = [i for i in range(0, n, s)]
tm = []
for i in x:
tm.append(time_pigeons_once(i))
plt.plot(x, tm)
plt.xlabel('n')
plt.ylabel('Time (s)')
plt.show()
plt.loglog(x, tm)
plt.show()
def create_txt_pigeons(n):
filename = 'Pigeons ' + str(n)
file = open(filename, 'w+')
results = pigeons(n)
file.write(str(results[0]) + '\n' + str(results[1]) + '\n' + str(results[2]))
file.close()
def read(filepath):
file = open(filepath, 'r')
content = file.readlines()
n = int(content[0])
literals = content[1]
cnf_str = content[2]
cnf_str = cnf_str[1:]
cnf_str = cnf_str[:-1]
cnf = []
last = ''
for i in range(len(cnf_str)):
if cnf_str[i] == '[':
cnf.append([])
last = cnf_str[i]
continue
if cnf_str[i] == ']':
last = cnf_str[i]
continue
if last == '[':
if cnf_str[i] != ' ' and cnf_str[i] != ',' and cnf_str[i] != '-':
if cnf_str[i - 1] == '-':
cnf[-1].append(-int(cnf_str[i]))
else:
cnf[-1].append(int(cnf_str[i]))
file.close()
return n, literals, cnf
def time_dpll_pigeon_once(n, heuristic=1, want_mod=True):
cnf = pigeons(n)[2]
t = time()
DPLL(cnf, heuristic, want_mod)
t = time() - t
return t
def time_dpll_pigeons_range(n, s, heuristic=1, want_mod=True):
time_list = []
x = []
for k in range(0, n, s):
x.append(k)
time_list.append(time_dpll_pigeon_once(k, heuristic, want_mod))
return x, time_list
a = pigeons(4)
c = a[1]
b = a[2]
print(len(a[1]))
a = DPLL(b,2,want_mod=True)
print(a[1])