Advertisement
potato_research

RC Shortest Path with Bundled Properties

Sep 4th, 2013
237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.87 KB | None | 0 0
  1. //=======================================================================
  2. // Copyright 2013 Alberto Santini
  3. // Author: Alberto Santini <alberto@santini.in>
  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/graph_traits.hpp>
  11. #include <boost/graph/adjacency_list.hpp>
  12. #include <boost/graph/r_c_shortest_paths.hpp>
  13. #include <boost/graph/iteration_macros.hpp>
  14. #include <vector>
  15. #include <memory>
  16.  
  17. using std::vector;
  18. using std::allocator;
  19. using namespace boost;
  20.  
  21. class VertexProperty {
  22. public:
  23.   int property1;
  24.   int property2;
  25.   int id;
  26.   VertexProperty() {}
  27.   VertexProperty(const int property1, const int property2) : property1(property1), property2(property2) {}
  28. };
  29.  
  30. class EdgeProperty {
  31. public:
  32.   int cost;
  33.   int id;
  34.   EdgeProperty() {}
  35.   EdgeProperty(const int cost) : cost(cost) {}
  36. };
  37.  
  38. typedef adjacency_list<listS, listS, bidirectionalS, VertexProperty, EdgeProperty> Graph;
  39. typedef graph_traits<Graph>::edge_descriptor Edge;
  40.  
  41. class ResourceCont {
  42. public:
  43.   int res;
  44.   ResourceCont(const int res = 5) : res(res) {}
  45.   bool operator==(const ResourceCont& rc) const { return (res == rc.res); }
  46.   bool operator<(const ResourceCont& rc) const { return (res > rc.res); }
  47. };
  48.  
  49. class LabelExt {
  50. public:
  51.   bool operator()(const Graph& g, ResourceCont& rc, const ResourceCont& old_rc, Edge e) const {
  52.     rc.res = old_rc.res - g[e].cost;
  53.     return (rc.res > 0);
  54.   }
  55. };
  56.  
  57. class LabelDom {
  58. public:
  59.   bool operator()(const ResourceCont& rc1, const ResourceCont& rc2) const {
  60.     return (rc1 == rc2 || rc1 < rc2);
  61.   }
  62. };
  63.  
  64. int main() {
  65.   VertexProperty vp1(1, 1);
  66.   VertexProperty vp2(5, 9);
  67.   VertexProperty vp3(4, 3);
  68.   EdgeProperty e12(1);
  69.   EdgeProperty e23(2);
  70.    
  71.   Graph g;
  72.    
  73.   auto v1 = add_vertex(g); g[v1] = vp1;
  74.   auto v2 = add_vertex(g); g[v2] = vp2;
  75.   auto v3 = add_vertex(g); g[v3] = vp3;
  76.  
  77.   add_edge(v1, v2, e12, g);
  78.   add_edge(v2, v3, e23, g);
  79.    
  80.   int index = 0;
  81.   BGL_FORALL_VERTICES(v, g, Graph) {
  82.     g[v].id = index++;
  83.   }
  84.   index = 0;
  85.   BGL_FORALL_EDGES(e, g, Graph) {
  86.     g[e].id = index++;
  87.   }
  88.  
  89.   typedef vector<vector<Edge>> OptPath;
  90.   typedef vector<ResourceCont> ParetoOpt;
  91.  
  92.   OptPath op;
  93.   ParetoOpt ol;
  94.  
  95.   r_c_shortest_paths( g,
  96.                       get(&VertexProperty::id, g),
  97.                       get(&EdgeProperty::id, g),
  98.                       v1,
  99.                       v2,
  100.                       op,
  101.                       ol,
  102.                       ResourceCont(5),
  103.                       LabelExt(),
  104.                       LabelDom(),
  105.                       allocator<r_c_shortest_paths_label<Graph, ResourceCont>>(),
  106.                       default_r_c_shortest_paths_visitor());
  107.  
  108.   return 0;
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement