-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
176 lines (135 loc) · 4.3 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
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
import os
from src.gen import Gen
from src.puzzle import Nonogram
from src.state import State
import time
import tracemalloc
from src.search import BeFS
from src.search import DFS
from src.utils import heuristic_col
from src.utils import heuristic_level
grid_5_1 = [
[1, 1, 0, 1, 1],
[0, 1, 0, 0, 0],
[0, 1, 1, 1, 0],
[1, 0, 0, 0, 0],
[1, 0, 1, 1, 0]
]
grid_5_2 = [
[1, 1, 1, 0, 0],
[0, 0, 0, 0, 1],
[1, 0, 0, 1, 1],
[0, 1, 0, 0, 1],
[1, 0, 1, 0, 1]
]
grid_6_1 = [
[0, 1, 0, 1, 1, 1],
[0, 1, 1, 0, 1, 1],
[1, 1, 0, 1, 0, 0],
[1, 0, 0, 0, 1, 0],
[0, 1, 0, 1, 0, 0],
[1, 0, 1, 0, 1, 0]
]
grid_6_2 = [
[0, 0, 0, 1, 1, 1],
[0, 1, 0, 0, 1, 0],
[0, 1, 1, 1, 1, 1],
[1, 0, 1, 0, 1, 0],
[0, 1, 0, 0, 0, 1],
[1, 0, 0, 1, 1, 0]
]
grid_7_1 =[
[1, 1, 1, 0, 1, 0, 1],
[1, 1, 1, 0, 1, 1, 0],
[0, 0, 1, 1, 1, 1, 1],
[1, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 1, 0, 0, 1],
[0, 1, 0, 0, 1, 0, 1],
[1, 0, 0, 1, 0, 0, 1],
]
grid_7_2 = [
[0, 0, 1, 1, 0, 1, 1],
[0, 1, 0, 0, 1, 1, 1],
[0, 0, 0, 1, 1, 0, 0],
[1, 0, 0, 0, 0, 0, 1],
[1, 1, 0, 0, 0, 1, 1],
[1, 0, 1, 1, 0, 0, 0],
[1, 0, 1, 1, 1, 0, 1],
]
grid_list = [grid_5_1 , grid_5_2 , grid_6_1 , grid_6_2 , grid_7_1 , grid_7_2]
i = 1
if not os.path.exists('./output'):
os.mkdir('./output')
for grid in grid_list:
# For initing a random puzzle with size
# init = State(size=5)
init = State(len(grid), num=Gen.gen_grid_num(grid))
puzzle = Nonogram(init)
##################################
# DFS
## Time Elapsed
start_time = time.time()
solution_DFS = DFS(puzzle)
elapsed_time = time.time() - start_time
print('testcase ' + str(i) + ': size = ' + str(len(grid)) + '\n')
print('DFS:')
print(f"Time taken: {round(elapsed_time, 6)} seconds")
## Memory Usage
tracemalloc.start()
solution_DFS = DFS(puzzle)
current, peak = tracemalloc.get_traced_memory()
tracemalloc.stop()
current /= 1024**2
peak /= 1024**2
print(f"Memory usage: current is {round(current, 6)} MB, peak was {round(peak, 6)} MB")
# print(f"\nDFS Solution:")
# print(solution.path())
print()
##################################
# BeFS with Level Heuristic
start_time = time.time()
solution_BeFS_1 = BeFS(puzzle,heuristic_level)
elapsed_time = time.time() - start_time
print('BeFS with Level Heuristic:')
print(f"Time taken: {round(elapsed_time, 6)} seconds")
## Memory Usage
tracemalloc.start()
solution_BeFS_1 = BeFS(puzzle,heuristic_level)
current, peak = tracemalloc.get_traced_memory()
tracemalloc.stop()
current /= 1024**2
peak /= 1024**2
print(f"Memory usage: current is {round(current, 6)} MB, peak was {round(peak, 6)} MB")
# print(f"\nDFS Solution:")
# print(solution.path())
print()
##################################
# BeFS with Column Heuristic
# start_time = time.time()
# solution_BeFS_2 = BeFS(puzzle,heuristic_col)
# elapsed_time = time.time() - start_time
# print('BeFS with Column Heuristic:')
# print(f"Time taken: {round(elapsed_time, 6)} seconds")
# ## Memory Usage
# tracemalloc.start()
# solution_BeFS_2 = BeFS(puzzle,heuristic_col)
# current, peak = tracemalloc.get_traced_memory()
# tracemalloc.stop()
# current /= 1024**2
# peak /= 1024**2
# print(f"Memory usage: current is {round(current, 6)} MB, peak was {round(peak, 6)} MB")
# # print(f"\nDFS Solution:")
# # print(solution.path())
# print()
with open('output/tc'+ str(i) + '.txt','w') as f:
f.write('testcase '+ str(i) + ': size = ' + str(len(grid)) + '\n')
f.write('DFS:' + '\n')
f.write(str(solution_DFS.path()))
f.write('\n\n')
f.write('BeFS with Column Heuristic:' + '\n')
f.write(str(solution_BeFS_1.path()))
f.write('\n\n')
# f.write('BeFS with Column Heuristic:' + '\n')
# f.write(str(solution_BeFS_2.path()))
# f.write('\n\n')
i += 1