55#include < algorithm>
66#include < opencv2/core/core.hpp>
77#include < opencv2/highgui/highgui.hpp>
8-
8+ #include < time.h>
9+ #include < chrono>
910
1011
1112using namespace std ;
1213#define map_size 60
1314
15+ #define USE_HEAP 1
1416
1517class Node {
1618
@@ -47,7 +49,12 @@ bool comp_cost(node_pair node1, node_pair node2)
4749 return (node1.second .cost_g +node1.second .cost_h ) < (node2.second .cost_g +node2.second .cost_h );
4850 }
4951}
50-
52+ struct mylesser {
53+ bool operator ()(const node_pair& node1, const node_pair& node2) const {
54+ // printf("out put \n");
55+ return (node1.second .cost_g +node1.second .cost_h ) > (node2.second .cost_g +node2.second .cost_h );
56+ }
57+ };
5158class AStar
5259{
5360public:
@@ -106,7 +113,32 @@ class AStar
106113
107114
108115 }
116+ void print_vecp (vector<node_pair> &tmpvec)
117+ {
118+ vector<node_pair>::iterator v_iter;
119+ for (v_iter=tmpvec.begin (); v_iter!=tmpvec.end (); v_iter++) {
120+ cout<<v_iter->first <<" " <<v_iter->second .cost_g << " " <<v_iter->second .cost_h << " " << v_iter->second .cost_g +v_iter->second .cost_h << endl;
121+ }
122+ // std::cout << vcp.begin()->second.cost_g+vcp.begin()->second.cost_h << " " << vcp.end()->second.cost_g+vcp.end()->second.cost_h << std::endl;
123+ std::cout << " ================================" << std::endl;
124+
125+ }
126+ int get_new_min_f ()
127+ {
128+
129+ // print_vecp(vcp_t);
130+
131+ if (USE_HEAP)
132+ {
133+ return vcp_t [0 ].first ;
134+ }
135+ else {
136+ sort (vcp_g.begin (),vcp_g.end (),comp_cost);
137+ return vcp_g.begin ()->first ;
138+ }
109139
140+
141+ }
110142 bool verify_node (Node& node)
111143 {
112144 if (node.x < minx || node.y < miny || node.x > maxx || node.y > maxy)
@@ -120,15 +152,97 @@ class AStar
120152 return true ;
121153 }
122154
155+ void insert_node (int index,Node &node)
156+ {
157+
158+ open_nodes.insert (make_pair (index,node));
159+ if (USE_HEAP)
160+ {
161+ vcp_t .push_back (node_pair (index,node));
162+ push_heap (vcp_t .begin (),vcp_t .end (),mylesser ());
163+ }
164+ else {
165+ vcp_g.push_back (node_pair (index,node));
166+
167+ }
168+
169+
170+ }
171+
172+ void update_node (int index,Node &node)
173+ {
174+
175+ open_nodes[index] = node;
176+ vector<node_pair>::iterator iter;
177+ if (USE_HEAP)
178+ {
179+ for (iter = vcp_t .begin (); iter != vcp_t .end (); iter ++ )
180+ {
181+ if ( iter->first == index )
182+ {
183+ iter->second = node;
184+ break ;
185+ }
186+ }
187+ sort_heap (vcp_t .begin (),vcp_t .end (),mylesser ());
188+ make_heap (vcp_t .begin (),vcp_t .end (),mylesser ());
189+ }
190+ else {
191+ for (iter = vcp_g.begin (); iter != vcp_g.end (); iter ++ )
192+ {
193+ if ( iter->first == index )
194+ {
195+ iter->second = node;
196+ break ;
197+ }
198+ }
199+
200+ }
201+
202+
203+
204+ }
205+
206+ void delete_node (int index,Node &node)
207+ {
208+ open_nodes.erase (index);
209+ if (USE_HEAP)
210+ {
211+ pop_heap (vcp_t .begin (),vcp_t .end (),mylesser ());
212+ vcp_t .pop_back ();
213+ }
214+ else {
215+ vector<node_pair>::iterator iter;
216+ for (iter = vcp_g.begin (); iter != vcp_g.end (); )
217+ {
218+ if ( iter->first == index )
219+ iter = vcp_g.erase (iter);
220+ else
221+ iter ++ ;
222+ }
223+ }
224+
225+
226+ // std::cout << index << " " << vcp_g.size() <<" delet " <<open_nodes.size() << std::endl;
227+
228+ }
229+
123230 void a_star_plan (Node &node_s,Node &node_e)
124231 {
125232 int index = calc_node_index (node_s);
126- open_nodes. insert ( make_pair (index,node_s));
127-
233+
234+ insert_node (index,node_s);
128235
236+ auto beginTime = std::chrono::high_resolution_clock::now ();
129237 while (true )
130238 {
131- int c_index = get_min_f ();
239+
240+ if (open_nodes.size () <= 0 )
241+ {
242+ break ;
243+ }
244+
245+ int c_index = get_new_min_f ();
132246 Node current_node = open_nodes[c_index];
133247
134248 if (current_node.x == node_e.x && current_node.y == node_e.y )
@@ -141,10 +255,11 @@ class AStar
141255 break ;
142256 }
143257
144- open_nodes. erase (c_index);
258+ delete_node (c_index,current_node );
145259 close_nodes.insert (make_pair (c_index,current_node));
146260
147- for (int i = 0 ; i < 8 ; i++)
261+
262+ for (int i = 0 ; i < 4 ; i++)
148263 {
149264 double * tmp_m = motion[i];
150265 Node tmpNode = Node (current_node.x +tmp_m[0 ],current_node.y +tmp_m[1 ],current_node.cost_g +tmp_m[2 ],c_index);
@@ -163,18 +278,25 @@ class AStar
163278
164279 if (open_nodes.find (tmp_index) == open_nodes.end ())
165280 {
166- open_nodes.insert (make_pair (tmp_index,tmpNode));
281+
282+ insert_node (tmp_index,tmpNode);
167283 }
168284 else {
169285 Node in_node = open_nodes[tmp_index];
170286 if (in_node.cost_g > tmpNode.cost_g )
171287 {
172- open_nodes[tmp_index] = tmpNode;
288+
289+ update_node (tmp_index,tmpNode);
290+
173291 }
174292 }
175293
176294 }
295+
177296 }
297+ auto endTime = std::chrono::high_resolution_clock::now ();
298+ auto elapsedTime= std::chrono::duration_cast<std::chrono::milliseconds>(endTime - beginTime);
299+ std::cout << elapsedTime.count () << " ms" <<std::endl;
178300 }
179301
180302 void show_path ()
@@ -265,11 +387,11 @@ class AStar
265387 {
266388 obmap[i][j] = 1 ;
267389 }
268- if (i >= 20 && j == 20 )
390+ if (i >= map_size- 40 && j == 20 )
269391 {
270392 obmap[i][j] = 1 ;
271393 }
272- if (i <= 40 && j == 40 )
394+ if (i <= map_size- 20 && j == map_size- 20 )
273395 {
274396 obmap[i][j] = 1 ;
275397 }
@@ -301,17 +423,26 @@ class AStar
301423 int maxy = 0 ;
302424 int obmap[map_size][map_size] = {0 };
303425 int goals_pind = -1 ;
426+ vector<node_pair> vcp_g;
427+ vector<node_pair> vcp_t ;
304428
305429};
306430
307431int main (int argc, char **argv){
308432 std::cout << " hello" << std::endl;
309433
434+ auto beginTime_2 = std::chrono::high_resolution_clock::now ();
310435 AStar star = AStar ();
311436 Node Node_s = Node (30 ,10 ,0 ,-1 );
312- Node Node_e = Node (10 , 50 ,0 ,-1 );
437+ Node Node_e = Node (50 ,map_size- 10 ,0 ,-1 );
313438 Node_s.set_cost_h (star.calc_h (Node_s,Node_e));
439+ // 计时开始
440+ auto beginTime = std::chrono::high_resolution_clock::now ();
314441 star.a_star_plan (Node_s,Node_e);
442+ auto endTime = std::chrono::high_resolution_clock::now ();
443+ auto elapsedTime= std::chrono::duration_cast<std::chrono::milliseconds>(endTime - beginTime);
444+ auto elapsedTime2= std::chrono::duration_cast<std::chrono::milliseconds>(beginTime - beginTime_2);
445+ std::cout << " elapsed time is " << elapsedTime.count () << " ms " << elapsedTime2.count () << " ms" << std::endl;
315446 star.show_path ();
316447 // for(int i = 0; i < map_size; i++)
317448 // {
@@ -324,4 +455,4 @@ int main(int argc, char**argv){
324455
325456
326457
327- }
458+ }
0 commit comments