Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- std::vector<const map::map_node*> helper::shortestPathToDist(const map::map_node& source_node, const map::map_node& target_node)
- {
- std::vector<const map::map_node*> path;
- //std::vector<const map::map_node*> visited;
- std::unordered_set<const map::map_node*> visited;
- //std::vector<const map::map_node*> queue;
- using scored_node = std::pair<double, const map::map_node*>;
- std::priority_queue<scored_node, std::vector<scored_node>, std::greater<scored_node>> queue;
- std::map<const map::map_node*, const map::map_node*> previous;
- std::map<const map::map_node*, float> cost;
- std::map<const map::map_node*, float> heuristic_cost;
- queue.push({0, &source_node});
- //queue.push_back(&source_node);
- cost[&source_node] = 0;
- heuristic_cost[&source_node] = heuristic(source_node, target_node);
- while (queue.size() > 0) {
- //const map::map_node* shortest_path_node = smallestDistance(cost, heuristic_cost, queue);
- const auto shortest_path_scored_node = queue.top();
- const auto shortest_path_node = shortest_path_scored_node.second;
- //queue.pop();
- //visited.erase(shortest_path_node);
- for (int i = 0; i < shortest_path_node->num_edges(); i++) {
- map::map_edge edge = (*shortest_path_node)[i];
- const map::map_node* node_to = &edge.to();
- std::priority_queue<scored_node, std::vector<scored_node>, std::greater<scored_node>> queueCopy = queue;
- bool valid = false;
- while (!queueCopy.empty()) {
- if (queueCopy.top().second == node_to) {
- valid = true;
- }
- queueCopy.pop();
- }
- if (std::find(visited.begin(), visited.end(), node_to) == visited.end()
- && valid) {
- //queue.push_back(node_to);
- queue.push({ queue.size(), node_to });
- }
- if (cost.find(node_to) == cost.end() || cost[node_to] > cost[shortest_path_node] + edge.weight())
- {
- cost[node_to] = cost[shortest_path_node] + edge.weight();
- heuristic_cost[node_to] = heuristic(*shortest_path_node, target_node);
- previous[node_to] = shortest_path_node;
- }
- if (node_to->node_id() == target_node.node_id())
- {
- cost[node_to] = cost[shortest_path_node] + edge.weight();
- heuristic_cost[node_to] = heuristic(*shortest_path_node, target_node);
- previous[node_to] = shortest_path_node;
- break;
- }
- }
- //queue.erase(queue.begin());
- //visited.push_back(shortest_path_node);
- queue.pop();
- visited.erase(shortest_path_node);
- }
- // shortest path to target_node
- const map::map_node* current_node = nullptr;
- for (auto const& [key, val] : previous) {
- if (key != nullptr && key != NULL) {
- if (key->node_id() == target_node.node_id()) {
- current_node = val;
- break;
- }
- }
- }
- path.clear();
- if (current_node == nullptr || current_node == NULL) {
- std::cout << "could not find target node\n";
- //this->shortest_path_to_target(source_node, target_node);
- return path;
- }
- while (current_node != &source_node) {
- if (current_node != nullptr && current_node != NULL) {
- if (path.size() > 0) {
- bool found = false;
- for (auto& p : path) {
- if (p != NULL && p != nullptr && p->node_id() == current_node->node_id()) {
- found = true;
- break;
- }
- }
- if (!found) {
- path.insert(path.begin(), current_node);
- }
- }
- else {
- path.insert(path.begin(), current_node);
- }
- }
- for (auto const& [key, val] : previous) {
- if (key != nullptr && key != NULL && current_node != nullptr && current_node != NULL) {
- if (key->node_id() == current_node->node_id()) {
- current_node = val;
- break;
- }
- }
- }
- }
- return path;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement