Advertisement
Guest User

Untitled

a guest
Feb 25th, 2020
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.79 KB | None | 0 0
  1. std::vector<const map::map_node*> helper::shortestPathToDist(const map::map_node& source_node, const map::map_node& target_node)
  2. {
  3. std::vector<const map::map_node*> path;
  4. //std::vector<const map::map_node*> visited;
  5. std::unordered_set<const map::map_node*> visited;
  6. //std::vector<const map::map_node*> queue;
  7. using scored_node = std::pair<double, const map::map_node*>;
  8. std::priority_queue<scored_node, std::vector<scored_node>, std::greater<scored_node>> queue;
  9.  
  10. std::map<const map::map_node*, const map::map_node*> previous;
  11. std::map<const map::map_node*, float> cost;
  12. std::map<const map::map_node*, float> heuristic_cost;
  13.  
  14. queue.push({0, &source_node});
  15. //queue.push_back(&source_node);
  16. cost[&source_node] = 0;
  17. heuristic_cost[&source_node] = heuristic(source_node, target_node);
  18.  
  19. while (queue.size() > 0) {
  20. //const map::map_node* shortest_path_node = smallestDistance(cost, heuristic_cost, queue);
  21.  
  22. const auto shortest_path_scored_node = queue.top();
  23. const auto shortest_path_node = shortest_path_scored_node.second;
  24. //queue.pop();
  25. //visited.erase(shortest_path_node);
  26.  
  27. for (int i = 0; i < shortest_path_node->num_edges(); i++) {
  28. map::map_edge edge = (*shortest_path_node)[i];
  29. const map::map_node* node_to = &edge.to();
  30.  
  31. std::priority_queue<scored_node, std::vector<scored_node>, std::greater<scored_node>> queueCopy = queue;
  32. bool valid = false;
  33.  
  34. while (!queueCopy.empty()) {
  35. if (queueCopy.top().second == node_to) {
  36. valid = true;
  37. }
  38. queueCopy.pop();
  39. }
  40.  
  41. if (std::find(visited.begin(), visited.end(), node_to) == visited.end()
  42. && valid) {
  43. //queue.push_back(node_to);
  44. queue.push({ queue.size(), node_to });
  45. }
  46.  
  47. if (cost.find(node_to) == cost.end() || cost[node_to] > cost[shortest_path_node] + edge.weight())
  48. {
  49. cost[node_to] = cost[shortest_path_node] + edge.weight();
  50. heuristic_cost[node_to] = heuristic(*shortest_path_node, target_node);
  51. previous[node_to] = shortest_path_node;
  52. }
  53.  
  54. if (node_to->node_id() == target_node.node_id())
  55. {
  56. cost[node_to] = cost[shortest_path_node] + edge.weight();
  57. heuristic_cost[node_to] = heuristic(*shortest_path_node, target_node);
  58. previous[node_to] = shortest_path_node;
  59. break;
  60. }
  61. }
  62. //queue.erase(queue.begin());
  63. //visited.push_back(shortest_path_node);
  64. queue.pop();
  65. visited.erase(shortest_path_node);
  66. }
  67.  
  68. // shortest path to target_node
  69. const map::map_node* current_node = nullptr;
  70.  
  71.  
  72. for (auto const& [key, val] : previous) {
  73. if (key != nullptr && key != NULL) {
  74. if (key->node_id() == target_node.node_id()) {
  75. current_node = val;
  76. break;
  77. }
  78. }
  79. }
  80.  
  81. path.clear();
  82.  
  83. if (current_node == nullptr || current_node == NULL) {
  84. std::cout << "could not find target node\n";
  85. //this->shortest_path_to_target(source_node, target_node);
  86. return path;
  87. }
  88.  
  89. while (current_node != &source_node) {
  90. if (current_node != nullptr && current_node != NULL) {
  91. if (path.size() > 0) {
  92. bool found = false;
  93. for (auto& p : path) {
  94. if (p != NULL && p != nullptr && p->node_id() == current_node->node_id()) {
  95. found = true;
  96. break;
  97. }
  98. }
  99.  
  100. if (!found) {
  101. path.insert(path.begin(), current_node);
  102. }
  103. }
  104. else {
  105. path.insert(path.begin(), current_node);
  106. }
  107. }
  108.  
  109. for (auto const& [key, val] : previous) {
  110. if (key != nullptr && key != NULL && current_node != nullptr && current_node != NULL) {
  111. if (key->node_id() == current_node->node_id()) {
  112. current_node = val;
  113. break;
  114. }
  115. }
  116. }
  117. }
  118.  
  119. return path;
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement