astar-cities.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. //
  2. //=======================================================================
  3. // Copyright (c) 2004 Kristopher Beevers
  4. //
  5. // Distributed under the Boost Software License, Version 1.0. (See
  6. // accompanying file LICENSE_1_0.txt or copy at
  7. // http://www.boost.org/LICENSE_1_0.txt)
  8. //=======================================================================
  9. //
  10. #include <boost/graph/astar_search.hpp>
  11. #include <boost/graph/adjacency_list.hpp>
  12. #include <boost/graph/random.hpp>
  13. #include <boost/random.hpp>
  14. #include <boost/graph/graphviz.hpp>
  15. #include <ctime>
  16. #include <vector>
  17. #include <list>
  18. #include <iostream>
  19. #include <fstream>
  20. #include <math.h> // for sqrt
  21. using namespace boost;
  22. using namespace std;
  23. // auxiliary types
  24. struct location
  25. {
  26. float y, x; // lat, long
  27. };
  28. typedef float cost;
  29. template <class Name, class LocMap>
  30. class city_writer {
  31. public:
  32. city_writer(Name n, LocMap l, float _minx, float _maxx,
  33. float _miny, float _maxy,
  34. unsigned int _ptx, unsigned int _pty)
  35. : name(n), loc(l), minx(_minx), maxx(_maxx), miny(_miny),
  36. maxy(_maxy), ptx(_ptx), pty(_pty) {}
  37. template <class Vertex>
  38. void operator()(ostream& out, const Vertex& v) const {
  39. float px = 1 - (loc[v].x - minx) / (maxx - minx);
  40. float py = (loc[v].y - miny) / (maxy - miny);
  41. out << "[label=\"" << name[v] << "\", pos=\""
  42. << static_cast<unsigned int>(ptx * px) << ","
  43. << static_cast<unsigned int>(pty * py)
  44. << "\", fontsize=\"11\"]";
  45. }
  46. private:
  47. Name name;
  48. LocMap loc;
  49. float minx, maxx, miny, maxy;
  50. unsigned int ptx, pty;
  51. };
  52. template <class WeightMap>
  53. class time_writer {
  54. public:
  55. time_writer(WeightMap w) : wm(w) {}
  56. template <class Edge>
  57. void operator()(ostream &out, const Edge& e) const {
  58. out << "[label=\"" << wm[e] << "\", fontsize=\"11\"]";
  59. }
  60. private:
  61. WeightMap wm;
  62. };
  63. // euclidean distance heuristic
  64. template <class Graph, class CostType, class LocMap>
  65. class distance_heuristic : public astar_heuristic<Graph, CostType>
  66. {
  67. public:
  68. typedef typename graph_traits<Graph>::vertex_descriptor Vertex;
  69. distance_heuristic(LocMap l, Vertex goal)
  70. : m_location(l), m_goal(goal) {}
  71. CostType operator()(Vertex u)
  72. {
  73. CostType dx = m_location[m_goal].x - m_location[u].x;
  74. CostType dy = m_location[m_goal].y - m_location[u].y;
  75. return ::sqrt(dx * dx + dy * dy);
  76. }
  77. private:
  78. LocMap m_location;
  79. Vertex m_goal;
  80. };
  81. struct found_goal {}; // exception for termination
  82. // visitor that terminates when we find the goal
  83. template <class Vertex>
  84. class astar_goal_visitor : public boost::default_astar_visitor
  85. {
  86. public:
  87. astar_goal_visitor(Vertex goal) : m_goal(goal) {}
  88. template <class Graph>
  89. void examine_vertex(Vertex u, Graph& g) {
  90. if(u == m_goal)
  91. throw found_goal();
  92. }
  93. private:
  94. Vertex m_goal;
  95. };
  96. int main(int argc, char **argv)
  97. {
  98. // specify some types
  99. typedef adjacency_list<listS, vecS, undirectedS, no_property,
  100. property<edge_weight_t, cost> > mygraph_t;
  101. typedef property_map<mygraph_t, edge_weight_t>::type WeightMap;
  102. typedef mygraph_t::vertex_descriptor vertex;
  103. typedef mygraph_t::edge_descriptor edge_descriptor;
  104. typedef std::pair<int, int> edge;
  105. // specify data
  106. enum nodes {
  107. Troy, LakePlacid, Plattsburgh, Massena, Watertown, Utica,
  108. Syracuse, Rochester, Buffalo, Ithaca, Binghamton, Woodstock,
  109. NewYork, N
  110. };
  111. const char *name[] = {
  112. "Troy", "Lake Placid", "Plattsburgh", "Massena",
  113. "Watertown", "Utica", "Syracuse", "Rochester", "Buffalo",
  114. "Ithaca", "Binghamton", "Woodstock", "New York"
  115. };
  116. location locations[] = { // lat/long
  117. {42.73, 73.68}, {44.28, 73.99}, {44.70, 73.46},
  118. {44.93, 74.89}, {43.97, 75.91}, {43.10, 75.23},
  119. {43.04, 76.14}, {43.17, 77.61}, {42.89, 78.86},
  120. {42.44, 76.50}, {42.10, 75.91}, {42.04, 74.11},
  121. {40.67, 73.94}
  122. };
  123. edge edge_array[] = {
  124. edge(Troy,Utica), edge(Troy,LakePlacid),
  125. edge(Troy,Plattsburgh), edge(LakePlacid,Plattsburgh),
  126. edge(Plattsburgh,Massena), edge(LakePlacid,Massena),
  127. edge(Massena,Watertown), edge(Watertown,Utica),
  128. edge(Watertown,Syracuse), edge(Utica,Syracuse),
  129. edge(Syracuse,Rochester), edge(Rochester,Buffalo),
  130. edge(Syracuse,Ithaca), edge(Ithaca,Binghamton),
  131. edge(Ithaca,Rochester), edge(Binghamton,Troy),
  132. edge(Binghamton,Woodstock), edge(Binghamton,NewYork),
  133. edge(Syracuse,Binghamton), edge(Woodstock,Troy),
  134. edge(Woodstock,NewYork)
  135. };
  136. unsigned int num_edges = sizeof(edge_array) / sizeof(edge);
  137. cost weights[] = { // estimated travel time (mins)
  138. 96, 134, 143, 65, 115, 133, 117, 116, 74, 56,
  139. 84, 73, 69, 70, 116, 147, 173, 183, 74, 71, 124
  140. };
  141. // create graph
  142. mygraph_t g(N);
  143. WeightMap weightmap = get(edge_weight, g);
  144. for(std::size_t j = 0; j < num_edges; ++j) {
  145. edge_descriptor e; bool inserted;
  146. boost::tie(e, inserted) = add_edge(edge_array[j].first,
  147. edge_array[j].second, g);
  148. weightmap[e] = weights[j];
  149. }
  150. // pick random start/goal
  151. boost::mt19937 gen(std::time(0));
  152. vertex start = random_vertex(g, gen);
  153. vertex goal = random_vertex(g, gen);
  154. cout << "Start vertex: " << name[start] << endl;
  155. cout << "Goal vertex: " << name[goal] << endl;
  156. ofstream dotfile;
  157. dotfile.open("test-astar-cities.dot");
  158. write_graphviz(dotfile, g,
  159. city_writer<const char **, location*>
  160. (name, locations, 73.46, 78.86, 40.67, 44.93,
  161. 480, 400),
  162. time_writer<WeightMap>(weightmap));
  163. vector<mygraph_t::vertex_descriptor> p(num_vertices(g));
  164. vector<cost> d(num_vertices(g));
  165. try {
  166. // call astar named parameter interface
  167. astar_search_tree
  168. (g, start,
  169. distance_heuristic<mygraph_t, cost, location*>
  170. (locations, goal),
  171. predecessor_map(make_iterator_property_map(p.begin(), get(vertex_index, g))).
  172. distance_map(make_iterator_property_map(d.begin(), get(vertex_index, g))).
  173. visitor(astar_goal_visitor<vertex>(goal)));
  174. } catch(found_goal fg) { // found a path to the goal
  175. list<vertex> shortest_path;
  176. for(vertex v = goal;; v = p[v]) {
  177. shortest_path.push_front(v);
  178. if(p[v] == v)
  179. break;
  180. }
  181. cout << "Shortest path from " << name[start] << " to "
  182. << name[goal] << ": ";
  183. list<vertex>::iterator spi = shortest_path.begin();
  184. cout << name[start];
  185. for(++spi; spi != shortest_path.end(); ++spi)
  186. cout << " -> " << name[*spi];
  187. cout << endl << "Total travel time: " << d[goal] << endl;
  188. return 0;
  189. }
  190. cout << "Didn't find a path from " << name[start] << "to"
  191. << name[goal] << "!" << endl;
  192. return 0;
  193. }