-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
113 lines (91 loc) · 2.53 KB
/
main.cpp
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
#include<iostream>
#include<stack>
#include <chrono>
#include"./include/eight-puzzle.hpp"
#include"./include/solver/bfs-solver.hpp"
#include"./include/solver/ids-solver.hpp"
#include"./include/solver/ucs-solver.hpp"
#include"./include/solver/astar-solver.hpp"
#include"./include/solver/gbf-solver.hpp"
#include"./include/solver/hill-climbing-solver.hpp"
#include"./include/solver/human-solver.hpp"
#include"./include/heuristic/miss-count-heuristic.hpp"
ISolver* get_solver(char algorithm) {
switch (algorithm) {
case 'B':
return new BFSSolver();
case 'I':
return new IDSSolver();
case 'U':
return new UCSSolver();
case 'A':
return new AStarSolver();
case 'G':
return new GBFSolver(new MissCountHeuristic());
case 'H':
return new HillClimbingSolver(new MissCountHeuristic());
case 'P':
return new HumanSolver();
default:
return new BFSSolver();
}
}
void solve(EightPuzzle& puzzle, ISolver* solver, bool print_solution, bool timed) {
solution_t solution;
long long elapsed_time;
try {
auto start = std::chrono::steady_clock::now();
solution = solver->solve(puzzle);
auto end = std::chrono::steady_clock::now();
elapsed_time = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count();
}
catch (std::invalid_argument& e) {
std::cout << "Error solving puzzle: " << e.what() << std::endl;
return;
}
catch (std::logic_error& e) {
std::cout << "Something went terribly wrong: " << e.what() << std::endl;
return;
}
if (timed) {
std::cout << solution.size() << ' ' << elapsed_time << "\n";
}
else {
std::cout << solution.size() << "\n";
}
if (print_solution) {
std::cout << '\n';
puzzle.print_instance();
while (!solution.empty()) {
direction_t move = solution.front();
solution.pop();
puzzle.move(move);
puzzle.print_instance();
}
}
}
int main(int argc, char const* argv[]) {
EightPuzzle puzzle;
bool print = strncmp("PRINT", argv[argc - 1], 5) == 0;
bool timed = strncmp("TIME", argv[argc - 1], 5) == 0;
if (argc >= 2 + PUZZLE_INSTANCE_SIZE) {
puzzle = EightPuzzle::read_instance(argv + 2);
}
else {
puzzle = EightPuzzle::read_instance();
}
if (!puzzle.is_valid()) {
std::cout << "Invalid puzzle!\n";
return 1;
}
if (!puzzle.is_solvable()) {
std::cout << "Puzzle is not solvable!\n";
return 1;
}
if (argc > 1) {
char exec_option = argv[1][0];
ISolver* solver = get_solver(exec_option);
solve(puzzle, solver, print, timed);
}
return 0;
}