-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
144 lines (131 loc) · 5.79 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
from search import face_search_options, puzzle_piece_search_options, SearchType
from puzzle_image_solver import PuzzleImageSolver
from block_image import BlockPattern
from utils.constants import PUZZLE_OPTIONS
from getopt import getopt, GetoptError
from sys import argv, exit
from threading import Thread
from results_analyzer import ResultsAnalyzer
if __name__=="__main__":
puzzle_input = ''
face_search_input = ''
puzzle_piece_search_input = ''
csv_input = ''
puzzle_memory_loss_factor_input = 0.0
puzzle_memory_loss_counter_limit_input = 0
glance_factor_input = 1
iterations_input = 1
state_image_path_input = ''
analyze_csv_input = None
output_file = None
try:
opts, args = getopt(argv[1:], "h",
["puzzle=",
"face_search=",
"piece_search=",
"puzzle_memory_loss=",
"puzzle_memory_loss_counter_limit=",
"glance_factor=",
"iterations=",
"csv=",
"analyze=",
"output_file=",
"state_image_path=",
])
for opt, arg in opts:
if opt == '-h':
print('main.py --puzzle <[puzzle_a].png, ' +
'[puzzle_b].png, or [puzzle_c].png> ' +
'--face_search <[random_search] or [beeline_search]> ' +
'--piece_search <sequential_search> ' +
'-puzzle_memory_loss <[0-1]> ' +
'-puzzle_memory_loss_counter_limit <[>0]> ' +
'-glance_factor <[0-1]> ' +
'--iterations <[>0]> ' +
'--csv <example.csv> ' +
'--state_image_path <directory>' +
' OR --analyze <example.csv> ' +
'--output_file <file.txt>'
)
exit()
elif opt in ("--puzzle"):
puzzle_input = arg
elif opt in ("--face_search"):
face_search_input = arg
elif opt in ("--piece_search"):
puzzle_piece_search_input = arg
elif opt in ("--puzzle_memory_loss"):
puzzle_memory_loss_factor_input = float(arg)
elif opt in ("--puzzle_memory_loss_counter_limit"):
puzzle_memory_loss_counter_limit_input = int(arg)
elif opt in ("--glance_factor"):
glance_factor_input = float(arg)
elif opt in ("--iterations"):
iterations_input = int(arg)
elif opt in ("--csv"):
csv_input = arg
elif opt in ("--state_image_path"):
state_image_path_input = arg
elif opt in ("--analyze"):
analyze_csv_input = arg
elif opt in ("--output_file"):
output_file = arg
except GetoptError as err:
print(err)
exit(2)
if analyze_csv_input:
analyzer = ResultsAnalyzer(analyze_csv_input, output_file)
analyzer.analyze()
exit(0)
if puzzle_input not in PUZZLE_OPTIONS:
raise Exception("Specify puzzle: " +
"[puzzle_a].png, [puzzle_b].png, or [puzzle_c].png")
if face_search_input not in face_search_options:
raise Exception("Specify face search: " +
"random_search, memory_search, or beeline_search")
if puzzle_piece_search_input not in puzzle_piece_search_options:
raise Exception("Specify puzzle piece search: " +
"sequential_search or skip_unknown_search]")
if not 0 <= puzzle_memory_loss_factor_input <= 1:
raise Exception("Specify puzzle memory loss factor: 0-1")
if not 0 <= glance_factor_input <= 1:
raise Exception("Specify glance factor: 0-1")
face_search = face_search_options[face_search_input]
puzzle_piece_search = puzzle_piece_search_options[puzzle_piece_search_input]
puzzle_solver_config = {
'puzzle_memory_loss_factor': puzzle_memory_loss_factor_input,
'puzzle_memory_loss_counter_limit': puzzle_memory_loss_counter_limit_input,
'glance_factor': glance_factor_input,
'solvers': {
SearchType.Face: face_search,
SearchType.PuzzlePiece: puzzle_piece_search
},
'state_image_path': state_image_path_input
}
if csv_input == "default":
csv_input = "{}_{}_{}_{}_{}_{}_{}.csv".format(puzzle_input,
face_search_input,
puzzle_piece_search_input,
puzzle_memory_loss_factor_input,
puzzle_memory_loss_counter_limit_input,
glance_factor_input,
iterations_input
)
for _ in range(iterations_input):
def puzzle_solver_thread():
puzzle_solver = PuzzleImageSolver(puzzle_input, puzzle_solver_config)
puzzle_solver.solve()
puzzle_solver.action_history = [
"puzzle_name,{},face_search,{},puzzle_piece_search,{},memory_loss_factor,{},memory_loss_counter_limit,{},glance_factor,{}"
.format(puzzle_input,
face_search_input,
puzzle_piece_search_input,
puzzle_memory_loss_factor_input,
puzzle_memory_loss_counter_limit_input,
glance_factor_input)] + puzzle_solver.action_history
puzzle_solver.print_history(csv_input)
thread = Thread(target = puzzle_solver_thread)
thread.start()
thread.join()
if csv_input:
print("Solution written to {}".format(csv_input))