8
8
9
9
10
10
template <typename typekey, typename Heuristic>
11
- std::vector<std::pair<size_t , size_t >> Astar (occupancyGrid[] ,
11
+ std::vector<std::pair<size_t , size_t >> Astar (nav_msgs::OccupancyGrid o_grid ,
12
12
TypeWkey<typekey> start, TypeWkey<typekey> goal, const Heuristic& heuristic)
13
13
{
14
+ std::cout << " Starting Astar" << std::endl;
14
15
// set of moves to be returned
15
16
std::vector<std::pair<size_t , size_t >> moves;
16
17
// occupancy grid width
17
- size_t width = occupancyGrid. width () ;
18
+ size_t width = o_grid. info . width ;
18
19
// occupancy grid height
19
- size_t height = occupancyGrid. height () ;
20
+ size_t height = o_grid. info . height ;
20
21
// scale cost of a single cardinal move
21
22
const double scale = 1 ;
22
23
// path cost for start
@@ -26,9 +27,9 @@ std::vector<std::pair<size_t, size_t>> Astar(occupancyGrid[],
26
27
// open priority queue, with removal and updating
27
28
UpdatePriorityQueue<TypeWkey<typekey>, size_t > openPQ;
28
29
// closed set map, maps the key for the type and the f cost for that type
29
- unordered_map<TypeWkey< typekey> , f_cost> closedSet;
30
+ unordered_map<typekey, f_cost> closedSet;
30
31
// map that maps parent with given key index
31
- unordered_map<TypeWkey< typekey>, TypeWkey< typekey> > parents;
32
+ unordered_map<typekey, typekey > parents;
32
33
// parents.insert(std::make_pair(start, start)); //parents[start] = undefined, we set the parent as itself
33
34
// inserts the first item
34
35
openPQ.insert (start, start_g, start_h); // openQ.add(start, f_score[start])
@@ -39,24 +40,30 @@ std::vector<std::pair<size_t, size_t>> Astar(occupancyGrid[],
39
40
TypeWkey<typekey> v;
40
41
// set of adjacent moves to u
41
42
std::vector<std::pair<TypeWkey<typekey>, g_cost> > adj_moves;
42
-
43
+ // std::cout << "HOLA" <<std::endl;
43
44
// while the priority queue is not empty
44
45
while (openPQ.size ())
45
46
{
47
+ // std::cout << "While Priority Queue is Not Empty, PriorityQueue size " << openPQ.size() << std::endl;
46
48
// total score, g_score + h_score for u
47
49
f_cost top_score = openPQ.getTotalVal (openPQ.top ());
50
+ // std::cout << "we got the top score " << top_score << endl;
48
51
// poping off u from the priority queue
49
52
u = openPQ.pop (); // Source node in first case
53
+ // std::cout << "popped " << top_score << endl;
50
54
if (u == goal)
51
55
{
52
56
// break and return current move list;
53
- return reconstruct_path (moves, parents, u, width, height);
57
+ return reconstruct_path<typekey> (moves, parents, u, width, height);
54
58
}
59
+ // std::cout << "not goal yet " << std::endl;
55
60
// if it isn't the goal we move the position to the closed set
56
- closedSet.insert (std::make_pair (u, top_score));
61
+ closedSet.insert (std::make_pair (u. getKey () , top_score));
57
62
63
+ // std::cout << "Inserting done " << std::endl;
58
64
// clear previous adjacent moves
59
65
adj_moves.clear ();
66
+ // std::cout << "clearing moves " << std::endl;
60
67
// NORTH
61
68
typekey temp_key = u.getKey ();
62
69
// Cardinal direction keys
@@ -76,94 +83,101 @@ std::vector<std::pair<size_t, size_t>> Astar(occupancyGrid[],
76
83
// north lookup
77
84
if (temp_key >= width)
78
85
{
79
- adj_moves.push_back (TypeWkey<typekey>(north_key), sdist);
86
+ adj_moves.push_back (std::make_pair ( TypeWkey<typekey>(north_key), sdist) );
80
87
// north west lookup
81
88
if (temp_key % width > 0 )
82
89
{
83
- adj_moves.push_back (TypeWkey<typekey>(north_w_key), ddist);
90
+ adj_moves.push_back (std::make_pair ( TypeWkey<typekey>(north_w_key), ddist) );
84
91
}
85
92
// north east lookup
86
93
if (temp_key % width < (width - 1 ))
87
94
{
88
- adj_moves.push_back (TypeWkey<typekey>(north_e_key), ddist);
95
+ adj_moves.push_back (std::make_pair ( TypeWkey<typekey>(north_e_key), ddist) );
89
96
}
90
97
}
91
98
// south
92
99
if (temp_key <= (width - 1 ) * (height))
93
100
{
94
- adj_moves.push_back (TypeWkey<typekey>(south_key), sdist);
101
+ adj_moves.push_back (std::make_pair ( TypeWkey<typekey>(south_key), sdist) );
95
102
// south west lookup
96
103
if (temp_key % width > 0 )
97
104
{
98
- adj_moves.push_back (TypeWkey<typekey>(south_w_key), ddist);
105
+ adj_moves.push_back (std::make_pair ( TypeWkey<typekey>(south_w_key), ddist) );
99
106
}
100
107
// south east lookup
101
108
if (temp_key % width < (width - 1 ))
102
109
{
103
- adj_moves.push_back (TypeWkey<typekey>(south_e_key), ddist);
110
+ adj_moves.push_back (std::make_pair ( TypeWkey<typekey>(south_e_key), ddist) );
104
111
}
105
112
}
106
113
107
114
// west lookup
108
115
if (temp_key % width > 0 )
109
116
{
110
- adj_moves.push_back (TypeWkey<typekey>(west_key), sdist);
117
+ adj_moves.push_back (std::make_pair ( TypeWkey<typekey>(west_key), sdist) );
111
118
}
112
119
// east lookup
113
120
if (temp_key % width < (width - 1 ))
114
121
{
115
- adj_moves.push_back (TypeWkey<typekey>(east_key), sdist);
122
+ adj_moves.push_back (std::make_pair ( TypeWkey<typekey>(east_key), sdist) );
116
123
}
117
124
125
+ // std::cout << "Done generating Adjacency Moves" << std::endl;
118
126
// going through all possible adjacent moves
119
127
for (size_t i = 0 ; i < adj_moves.size (); i++) // where v is still in PQ.
120
128
{
121
129
// adjacent move
122
130
v = adj_moves[i].first ;
131
+ // std::cout << "Going through adjacent moves " << i << std::endl;
123
132
// if it is inside the closed set we can ignore it. Otherwise continue
124
133
if (closedSet.find (v.getKey ()) == closedSet.end ())
125
134
{
126
135
// if the probability of blockage in the probability grid is below 2% (1%, 0%, -1 (not explored)%)
127
- if (occupancyGrid [v.getkey ()] < 2 )
136
+ if (o_grid. data [v.getKey ()] < 2 )
128
137
{
129
138
// path cost of v
130
139
g_cost path_cost = adj_moves[i].second ; // determined by edge length and weight
131
140
// heuristic value of v
132
- h_cost h_val = hueristic (v.getKey (), goal);
141
+ h_cost h_val = heuristic (v.getKey (), goal);
133
142
134
143
// if it wasn't already in the openset
135
144
if (!openPQ.find (v))
136
145
{
137
146
// we insert it
147
+ parents.insert (std::make_pair (v.getKey (), u.getKey ()));
138
148
openPQ.insert (v, path_cost, h_val);
139
149
}
140
150
// if it was already in the openset, and we found that this path is shorter
141
151
// than the one already stored
142
152
else if (path_cost < openPQ.getCostVal (v)) // A shorter path to v has been found
143
153
{
144
154
// inserting parent (where v is the child and u is the parent)
145
- parents.insert (v , u);
155
+ parents.insert (std::make_pair (v. getKey () , u. getKey ()) );
146
156
// update the value
147
157
openPQ.update (v, path_cost, h_val);
148
158
}
149
159
}
150
160
}
151
161
}
162
+ // std::cout << "Done assessing adjacent " << std::endl;
152
163
}
153
164
return moves;
154
165
}
155
166
156
167
template <typename typekey>
157
168
const std::vector<std::pair<size_t , size_t >>& reconstruct_path (
158
169
std::vector<std::pair<size_t , size_t >>& moves,
159
- const unordered_map<TypeWkey< typekey>, TypeWkey< typekey> >& parents,
170
+ const unordered_map<typekey , typekey >& parents,
160
171
const TypeWkey<typekey>& current, size_t width, size_t hieght)
161
172
{
173
+ TypeWkey<typekey> path_current = current;
174
+ std::cout << " finding parents" << std::endl;
162
175
do
163
176
{
164
- moves.push_back (current.getKey () % width, (current.getKey () / width));
165
- current = parents.at (current.getKey ());
166
- } while (parents.find (current) != parents.end ());
177
+
178
+ moves.push_back (std::make_pair (path_current.getKey () % width, (path_current.getKey () / width)));
179
+ path_current = parents.at (path_current.getKey ());
180
+ } while (parents.find (path_current.getKey ()) != parents.end ());
167
181
return moves;
168
182
}
169
183
0 commit comments