rcsp_custom_vertex_id.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. //=======================================================================
  2. // Copyright (c) 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. #include <boost/config.hpp>
  10. #ifdef BOOST_MSVC
  11. // Without disabling this we get hard errors about initialialized pointers:
  12. #pragma warning(disable:4703)
  13. #endif
  14. #include <boost/graph/graph_traits.hpp>
  15. #include <boost/graph/adjacency_list.hpp>
  16. #include <boost/graph/r_c_shortest_paths.hpp>
  17. #include <boost/graph/iteration_macros.hpp>
  18. #include <vector>
  19. #include <memory>
  20. using std::vector;
  21. using std::allocator;
  22. using namespace boost;
  23. class VertexProperty {
  24. public:
  25. int property1;
  26. int property2;
  27. int id;
  28. VertexProperty() {}
  29. VertexProperty(const int property1, const int property2) : property1(property1), property2(property2) {}
  30. };
  31. class EdgeProperty {
  32. public:
  33. int cost;
  34. int id;
  35. EdgeProperty() {}
  36. EdgeProperty(const int cost) : cost(cost) {}
  37. };
  38. typedef adjacency_list<listS, listS, bidirectionalS, VertexProperty, EdgeProperty> Graph;
  39. typedef graph_traits<Graph>::vertex_descriptor Vertex;
  40. typedef graph_traits<Graph>::edge_descriptor Edge;
  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. class LabelExt {
  49. public:
  50. bool operator()(const Graph& g, ResourceCont& rc, const ResourceCont& old_rc, Edge e) const {
  51. rc.res = old_rc.res - g[e].cost;
  52. return (rc.res > 0);
  53. }
  54. };
  55. class LabelDom {
  56. public:
  57. bool operator()(const ResourceCont& rc1, const ResourceCont& rc2) const {
  58. return (rc1 == rc2 || rc1 < rc2);
  59. }
  60. };
  61. int main() {
  62. VertexProperty vp1(1, 1);
  63. VertexProperty vp2(5, 9);
  64. VertexProperty vp3(4, 3);
  65. EdgeProperty e12(1);
  66. EdgeProperty e23(2);
  67. Graph g;
  68. Vertex v1 = add_vertex(g); g[v1] = vp1;
  69. Vertex v2 = add_vertex(g); g[v2] = vp2;
  70. Vertex v3 = add_vertex(g); g[v3] = vp3;
  71. add_edge(v1, v2, e12, g);
  72. add_edge(v2, v3, e23, g);
  73. int index = 0;
  74. BGL_FORALL_VERTICES(v, g, Graph) {
  75. g[v].id = index++;
  76. }
  77. index = 0;
  78. BGL_FORALL_EDGES(e, g, Graph) {
  79. g[e].id = index++;
  80. }
  81. typedef vector<vector<Edge> > OptPath;
  82. typedef vector<ResourceCont> ParetoOpt;
  83. OptPath op;
  84. ParetoOpt ol;
  85. r_c_shortest_paths( g,
  86. get(&VertexProperty::id, g),
  87. get(&EdgeProperty::id, g),
  88. v1,
  89. v2,
  90. op,
  91. ol,
  92. ResourceCont(5),
  93. LabelExt(),
  94. LabelDom(),
  95. allocator<r_c_shortest_paths_label<Graph, ResourceCont> >(),
  96. default_r_c_shortest_paths_visitor());
  97. return 0;
  98. }