-
Notifications
You must be signed in to change notification settings - Fork 235
Expand file tree
/
Copy patha_star_epsilon.cpp
More file actions
233 lines (198 loc) · 6.17 KB
/
a_star_epsilon.cpp
File metadata and controls
233 lines (198 loc) · 6.17 KB
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
#include <fstream>
#include <iostream>
#include <boost/functional/hash.hpp>
#include <boost/program_options.hpp>
#include <libMultiRobotPlanning/a_star_epsilon.hpp>
using libMultiRobotPlanning::AStarEpsilon;
using libMultiRobotPlanning::Neighbor;
using libMultiRobotPlanning::PlanResult;
struct State {
State(int x, int y) : x(x), y(y) {}
State(const State&) = default;
State(State&&) = default;
State& operator=(const State&) = default;
State& operator=(State&&) = default;
bool operator==(const State& other) const {
return std::tie(x, y) == std::tie(other.x, other.y);
}
friend std::ostream& operator<<(std::ostream& os, const State& s) {
return os << "(" << s.x << "," << s.y << ")";
}
int x;
int y;
};
namespace std {
template <>
struct hash<State> {
size_t operator()(const State& s) const {
size_t seed = 0;
boost::hash_combine(seed, s.x);
boost::hash_combine(seed, s.y);
return seed;
}
};
} // namespace std
enum class Action {
Up,
Down,
Left,
Right,
};
std::ostream& operator<<(std::ostream& os, const Action& a) {
switch (a) {
case Action::Up:
os << "Up";
break;
case Action::Down:
os << "Down";
break;
case Action::Left:
os << "Left";
break;
case Action::Right:
os << "Right";
break;
}
return os;
}
class Environment {
public:
Environment(size_t dimx, size_t dimy, std::unordered_set<State> obstacles,
State goal)
: m_dimx(dimx),
m_dimy(dimy),
m_obstacles(std::move(obstacles)),
m_goal(std::move(goal)) // NOLINT
{}
int admissibleHeuristic(const State& s) {
return std::abs(s.x - m_goal.x) + std::abs(s.y - m_goal.y);
}
// a potentially inadmissible heuristic
int focalStateHeuristic(const State& /*s*/, int gScore) {
// prefer lower g-values
return gScore;
}
int focalTransitionHeuristic(const State& /*s1*/, const State& /*s2*/,
int gScoreS1, int gScoreS2) {
// prefer lower g-values
return gScoreS2 - gScoreS1;
}
bool isSolution(const State& s) { return s == m_goal; }
void getNeighbors(const State& s,
std::vector<Neighbor<State, Action, int> >& neighbors) {
neighbors.clear();
State up(s.x, s.y + 1);
if (stateValid(up)) {
neighbors.emplace_back(Neighbor<State, Action, int>(up, Action::Up, 1));
}
State down(s.x, s.y - 1);
if (stateValid(down)) {
neighbors.emplace_back(
Neighbor<State, Action, int>(down, Action::Down, 1));
}
State left(s.x - 1, s.y);
if (stateValid(left)) {
neighbors.emplace_back(
Neighbor<State, Action, int>(left, Action::Left, 1));
}
State right(s.x + 1, s.y);
if (stateValid(right)) {
neighbors.emplace_back(
Neighbor<State, Action, int>(right, Action::Right, 1));
}
}
void onExpandNode(const State& /*s*/, int /*fScore*/, int /*gScore*/) {}
void onDiscover(const State& /*s*/, int /*fScore*/, int /*gScore*/) {}
public:
bool stateValid(const State& s) {
return s.x >= 0 && s.x < m_dimx && s.y >= 0 && s.y < m_dimy &&
m_obstacles.find(s) == m_obstacles.end();
}
private:
int m_dimx;
int m_dimy;
std::unordered_set<State> m_obstacles;
State m_goal;
};
int main(int argc, char* argv[]) {
namespace po = boost::program_options;
// Declare the supported options.
po::options_description desc("Allowed options");
int startX, startY, goalX, goalY;
std::string mapFile;
std::string outputFile;
float w;
desc.add_options()("help", "produce help message")(
"startX", po::value<int>(&startX)->required(),
"start position x-component")("startY",
po::value<int>(&startY)->required(),
"start position y-component")(
"goalX", po::value<int>(&goalX)->required(), "goal position x-component")(
"goalY", po::value<int>(&goalY)->required(), "goal position y-component")(
"map,m", po::value<std::string>(&mapFile)->required(), "input map (txt)")(
"output,o", po::value<std::string>(&outputFile)->required(),
"output file (YAML)")("suboptimality,w", po::value<float>(&w)->required(),
"suboptimality factor");
try {
po::variables_map vm;
po::store(po::parse_command_line(argc, argv, desc), vm);
po::notify(vm);
if (vm.count("help") != 0u) {
std::cout << desc << "\n";
return 0;
}
} catch (po::error& e) {
std::cerr << e.what() << std::endl << std::endl;
std::cerr << desc << std::endl;
return 1;
}
std::unordered_set<State> obstacles;
std::ifstream map(mapFile);
int dimX = 0;
int y = 0;
while (map.good()) {
std::string line;
std::getline(map, line);
int x = 0;
for (char c : line) {
if (c == '#') {
obstacles.insert(State(x, y));
}
++x;
}
dimX = std::max(dimX, x);
++y;
}
std::cout << dimX << " " << y << std::endl;
bool success = false;
State goal(goalX, goalY);
State start(startX, startY);
Environment env(dimX, y - 1, obstacles, goal);
AStarEpsilon<State, Action, int, Environment> astar(env, w);
PlanResult<State, Action, int> solution;
if (env.stateValid(start)) {
success = astar.search(start, solution);
}
std::ofstream out(outputFile);
if (success) {
std::cout << "Planning successful! Total cost: " << solution.cost
<< std::endl;
for (size_t i = 0; i < solution.actions.size(); ++i) {
std::cout << solution.states[i].second << ": " << solution.states[i].first
<< "->" << solution.actions[i].first
<< "(cost: " << solution.actions[i].second << ")" << std::endl;
}
std::cout << solution.states.back().second << ": "
<< solution.states.back().first << std::endl;
out << "schedule:" << std::endl;
out << " agent1:" << std::endl;
for (size_t i = 0; i < solution.states.size(); ++i) {
out << " - x: " << solution.states[i].first.x << std::endl
<< " y: " << solution.states[i].first.y << std::endl
<< " t: " << i << std::endl;
}
} else {
std::cout << "Planning NOT successful!" << std::endl;
}
return 0;
}