-
Notifications
You must be signed in to change notification settings - Fork 0
/
npuzzle_test.cpp
134 lines (120 loc) · 4.35 KB
/
npuzzle_test.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#include "src/game/NPuzzle.h"
#include "src/heuristic/NPuzzleHeuristic.h"
#include "src/agent/AstarSearchAgent.h"
#include <getopt.h>
#include <iostream>
#include <memory>
#include <chrono>
#include <string>
//Note: exposing shared_ptr<>.get() pointer is probably a bad design pattern...
int help(){
printf("Usage: ./npuzzle_test -p <astar|all> [-n: n*n dims] [-x: dim_x] [-y: dim_y] [-w: weight] [-s scrambles]\n");
return 1;
}
int main(int argc, char* argv[]){
// Parse argument on dimension of NPuzzle
int dim_x = 3; int dim_y = 3;
int scramble_num = 1000;
double weight = 1;
int c, d;
std::string algo = "None";
while((c = getopt(argc, argv, "s:x:y:n:w:p:")) != -1){
switch(c){
case 'n':
d = std::atoi(optarg);
dim_x = d;
dim_y = d;
break;
case 'x':
dim_x = std::atoi(optarg);
break;
case 'y':
dim_y = std::atoi(optarg);
break;
case 's':
scramble_num = std::atoi(optarg);
break;
case 'w':
weight = std::atoi(optarg);
break;
case 'p':
algo = std::string(optarg);
break;
case '?':
if (optopt == 'n')
fprintf (stderr, "Option -%c requires an argument.\n", optopt);
return help();
}
}
// Check if we have agent (save us some startup time)
if (algo.compare("all") && algo.compare("astar") && algo.compare("all")){
return help();
}
// Debug chosen parameters:
printf("Running %dx%d NPuzzle Test:\n",dim_x,dim_y);
printf("Scrambles: %d\n",scramble_num);
printf("Weight: %.3lf\n",weight);
printf("Algo: %s\n",algo.c_str());
std::cout << std::endl;
NPuzzle* np = new NPuzzle(dim_y, dim_x);
std::shared_ptr<State> goal = np->get_goal_state();
if (!goal.get()){
std::cout << "Goal is not specified" << std::endl;
return 1;
}
std::cout << goal.get();
std::cout << "This is the goal state: " << np->is_goal_state(goal.get()) << " (should be 1)" << std::endl;
np->scramble(scramble_num);
Heuristic* np_heu = new NPuzzleHeuristic();
std::vector<Heuristic*> hs = {np_heu};
// Start our search agent (initialize with problem & heuristic)
// Spawn search agents based on input string
std::vector<Agent*> agents;
if (algo.compare("all") == 0){
Agent* astar_search = new AstarSearchAgent(np, np_heu, weight);
agents.push_back(astar_search);
}
else if (algo.compare("astar") == 0){
Agent* astar_search = new AstarSearchAgent(np, np_heu, weight);
agents.push_back(astar_search);
}
else{
return help();
}
int status = 0;
for(int i = 0; i<agents.size(); i++){
std::shared_ptr<State> current_state = np->get_state();
std::cout << std::endl << "Original problem start state after scrambling:" << std::endl;
std::cout << np;
std::cout << "This is the goal state: " << np->is_goal_state(current_state.get()) << " (should be 0)" << std::endl;
// Solve our puzzle (hopefully)
std::vector<std::shared_ptr<Action>> ans;
auto start = std::chrono::high_resolution_clock::now();
int run_code = agents[i]->solve(ans);
auto stop = std::chrono::high_resolution_clock::now();
if (run_code){
std::cerr << "(main) Error: solver threw error code: " << run_code << std::endl;
status = run_code;
break;
}
// Print solution
NPuzzle copy(*np);
for (std::shared_ptr<Action> a: ans){
copy.play(a.get());
}
std::cout << "Found solution in " << ans.size() << " moves" << std::endl;
std::cout << "Took " << std::chrono::duration_cast<std::chrono::milliseconds>(stop - start).count() << " milliseconds" << std::endl;
current_state = copy.get_state();
std::cout << copy << std::endl;
std::cout << "This is the goal state: " << copy.is_goal_state(current_state.get()) << std::endl;
}
// Cleanup
delete np;
for(int i = 0; i<agents.size(); i++){
delete agents[i];
}
for(int i = 0; i<hs.size(); i++){
delete hs[i];
}
return status;
}