39 #include <boost/format.hpp> 49 Grid2DNode(
int x,
int y,
bool occupied): x_(x), y_(y), occupied_(occupied) {}
52 return x_ == other.
x_ && y_ == other.
y_ ;
72 std::vector<Grid2DGraph::StatePtr> ret;
73 if (target_state->getX() - 1 > 0) {
74 if (state_map_[target_state->getX() - 1][target_state->getY()] &&
75 !state_map_[target_state->getX() - 1][target_state->getY()]->isOccupied()) {
76 ret.push_back(state_map_[target_state->getX() - 1][target_state->getY()]);
79 if (target_state->getX() + 1 < 1024) {
80 if (state_map_[target_state->getX() + 1][target_state->getY()] &&
81 !state_map_[target_state->getX() + 1][target_state->getY()]->isOccupied()) {
82 ret.push_back(state_map_[target_state->getX() + 1][target_state->getY()]);
85 if (target_state->getY() - 1 > 0) {
86 if (state_map_[target_state->getX()][target_state->getY() - 1] &&
87 !state_map_[target_state->getX()][target_state->getY() - 1]->isOccupied()) {
88 ret.push_back(state_map_[target_state->getX()][target_state->getY() - 1]);
91 if (target_state->getY() + 1 < 1024) {
92 if (state_map_[target_state->getX()][target_state->getY() + 1] &&
93 !state_map_[target_state->getX()][target_state->getY() + 1]->isOccupied()) {
94 ret.push_back(state_map_[target_state->getX()][target_state->getY() + 1]);
102 return prev_cost + 1;
107 return goal_state_ == state;
113 state_map_[state->getX()][state->getY()] = state;
118 return state_map_[x][y];
132 int main(
int argc,
char** argv)
137 std::string map_file = argv[1];
138 std::ifstream map_stream(map_file.c_str());
141 while (std::getline(map_stream, input)) {
142 for (
size_t i = 0;
i < input.length();
i++) {
145 graph->addNode(node);
149 graph->setStartState(graph->findNode(1, 1));
150 graph->setGoalState(graph->findNode(61, 28));
153 const size_t trials = 1000;
154 for (
size_t i = 0;
i < trials;
i++) {
158 std::vector<SolverNode<Grid2DNode, Grid2DGraph>::Ptr>
path 161 std::cout <<
"A* time to solve:" << acc.
mean() << std::endl;
165 std::vector<SolverNode<Grid2DNode, Grid2DGraph>::Ptr>
path 168 std::set<Grid2DNode::Ptr> path_states;
169 for (
size_t i = 0;
i < path.size();
i++) {
170 path_states.insert(path[
i]->getState());
173 for (
size_t j = 0; j < 1024; j++) {
174 bool is_valid_line =
false;
175 for (
size_t i = 0;
i < 1024;
i++) {
178 if (node->isOccupied()) {
181 else if (*(graph->getStartState()) == *node) {
184 else if (*(graph->getGoalState()) == *node) {
187 else if (path_states.find(node) != path_states.end()) {
193 is_valid_line =
true;
197 std::cout << std::endl;
boost::shared_ptr< Grid2DGraph > Ptr
virtual bool isGoal(StatePtr state)
double heuristicFunction(AStarSolver< Grid2DGraph >::SolverNodePtr node, Grid2DGraph::Ptr graph)
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
bool operator==(Grid2DNode &other)
boost::shared_ptr< Grid2DNode > Ptr
virtual StatePtr findNode(int x, int y)
int main(int argc, char **argv)
virtual std::vector< Grid2DGraph::StatePtr > successors(StatePtr target_state)
virtual double pathCost(StatePtr from, StatePtr to, double prev_cost)
Grid2DNode(int x, int y, bool occupied)
virtual void addNode(StatePtr state)