|
| 1 | +/*PGR-GNU***************************************************************** |
| 2 | +File: pgr_dijkstraTRSP.hpp |
| 3 | +Copyright (c) 2017 Celia Virginia Vergara Castillo |
| 4 | +Mail: vicky_vergara@hotmail.com |
| 5 | +------ |
| 6 | +This program is free software; you can redistribute it and/or modify |
| 7 | +it under the terms of the GNU General Public License as published by |
| 8 | +the Free Software Foundation; either version 2 of the License, or |
| 9 | +(at your option) any later version. |
| 10 | +This program is distributed in the hope that it will be useful, |
| 11 | +but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | +GNU General Public License for more details. |
| 14 | +You should have received a copy of the GNU General Public License |
| 15 | +along with this program; if not, write to the Free Software |
| 16 | +Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 17 | +********************************************************************PGR-GNU*/ |
| 18 | +#ifndef INCLUDE_DIJKSTRATRSP_PGR_DIJKSTRATRSP_HPP_ |
| 19 | +#define INCLUDE_DIJKSTRATRSP_PGR_DIJKSTRATRSP_HPP_ |
| 20 | +#pragma once |
| 21 | + |
| 22 | +#include "dijkstra/pgr_dijkstra.hpp" |
| 23 | + |
| 24 | +#include <sstream> |
| 25 | +#include <deque> |
| 26 | +#include <vector> |
| 27 | +#include <set> |
| 28 | +#include <limits> |
| 29 | + |
| 30 | +#include "cpp_common/pgr_assert.h" |
| 31 | +#include "cpp_common/basePath_SSEC.hpp" |
| 32 | + |
| 33 | +#include "c_types/restrict_t.h" |
| 34 | + |
| 35 | +template < class G > |
| 36 | +class Pgr_dijkstraTRSP { |
| 37 | + public: |
| 38 | + Path dijkstraTRSP( |
| 39 | + G& graph, |
| 40 | + const std::vector<Restrict_t>& restrictions, |
| 41 | + int64_t source, |
| 42 | + int64_t target, |
| 43 | + bool only_cost, |
| 44 | + bool strict); |
| 45 | + void clear(); |
| 46 | + private: |
| 47 | + void executeDijkstraTRSP(G& graph); |
| 48 | + void getFirstSolution(G& graph); |
| 49 | + bool checkFirstSolution(); |
| 50 | + private: |
| 51 | + typedef typename G::V V; |
| 52 | + V v_source; |
| 53 | + V v_target; |
| 54 | + int64_t m_start; |
| 55 | + int64_t m_end; |
| 56 | + std::vector< std::pair<int64_t, Restrict_t> > m_restrictions; |
| 57 | + bool m_only_cost; |
| 58 | + bool m_strict; |
| 59 | + |
| 60 | + Path curr_result_path; |
| 61 | + |
| 62 | + public: |
| 63 | + std::ostringstream log; |
| 64 | +}; |
| 65 | + |
| 66 | +template < class G > |
| 67 | +void Pgr_dijkstraTRSP< G >::clear() { |
| 68 | +} |
| 69 | + |
| 70 | +template < class G> |
| 71 | +Path |
| 72 | +Pgr_dijkstraTRSP< G >::dijkstraTRSP(G& graph, const std::vector<Restrict_t>& restrictions, |
| 73 | +int64_t start_vertex, int64_t end_vertex, bool only_cost, bool strict) { |
| 74 | + if (start_vertex == end_vertex) |
| 75 | + return Path(); |
| 76 | + if (!graph.has_vertex(start_vertex) || !graph.has_vertex(end_vertex)) |
| 77 | + return Path(); |
| 78 | + |
| 79 | + m_only_cost = only_cost; |
| 80 | + v_source = graph.get_V(start_vertex); |
| 81 | + v_target = graph.get_V(end_vertex); |
| 82 | + m_start = start_vertex; |
| 83 | + m_end = end_vertex; |
| 84 | + for(auto &restriction: restrictions) |
| 85 | + m_restrictions.push_back( {restriction.restricted_edges[0], restriction} ); |
| 86 | + m_strict = strict; |
| 87 | + executeDijkstraTRSP(graph); |
| 88 | + return curr_result_path; |
| 89 | +} |
| 90 | + |
| 91 | +template < class G > |
| 92 | +void Pgr_dijkstraTRSP< G >::getFirstSolution(G& graph) { |
| 93 | + Path path; |
| 94 | + |
| 95 | + Pgr_dijkstra< G > fn_dijkstra; |
| 96 | + path = fn_dijkstra.dijkstra(graph, m_start, m_end); |
| 97 | + |
| 98 | + if (path.empty()) return; |
| 99 | + curr_result_path = path; |
| 100 | +} |
| 101 | + |
| 102 | +template < class G > |
| 103 | +bool Pgr_dijkstraTRSP< G >::checkFirstSolution() { |
| 104 | +#if 0 |
| 105 | + auto sort_cmp = [](const std::pair<int64_t, Restrict_t>& left, |
| 106 | + const std::pair<int64_t, Restrict_t>& right) -> bool { |
| 107 | + return left.first <= right.first; |
| 108 | + }; |
| 109 | + auto lower_bound_cmp = [](const std::pair<int64_t, Restrict_t>& p, |
| 110 | + int64_t target) -> bool { |
| 111 | + return p.first < target; |
| 112 | + }; |
| 113 | + std::stable_sort(m_restrictions.begin(), m_restrictions.end(), |
| 114 | + sort_cmp); |
| 115 | + std::vector< int64_t > edges_in_path; |
| 116 | + for (auto &path: curr_result_path) edges_in_path.push_back(path.edge); |
| 117 | + if (edges_in_path.size() and edges_in_path[0] == -1) reverse(edges_in_path.begin(), edges_in_path.end()); |
| 118 | + while (edges_in_path.size() and edges_in_path.back() == -1) edges_in_path.pop_back(); |
| 119 | + size_t index = 0; |
| 120 | + for (auto &edge: edges_in_path) { |
| 121 | + auto edge_index = std::lower_bound(m_restrictions.begin(), |
| 122 | + m_restrictions.end(), edge, lower_bound_cmp) - m_restrictions.begin(); |
| 123 | + while (edge_index < m_restrictions.size() and |
| 124 | + m_restrictions[edge_index].first == edge) { |
| 125 | + size_t temp_edge_index = index; |
| 126 | + bool okay = true; |
| 127 | + for (auto &edge_id: m_restrictions[edge_index].second.restricted_edges) { |
| 128 | + if (edge_id == -1) break; |
| 129 | + if(edges_in_path[temp_edge_index] != edge_id) okay = false; |
| 130 | + temp_edge_index++; |
| 131 | + } |
| 132 | + if (okay) { |
| 133 | + log << "Restriction is not applicable to this case."; |
| 134 | + return false; |
| 135 | + } |
| 136 | + edge_index++; |
| 137 | + } |
| 138 | + index++; |
| 139 | + } |
| 140 | +#endif |
| 141 | + log << "Restriction is applicable to this case."; |
| 142 | + return false; |
| 143 | +} |
| 144 | + |
| 145 | +template < class G > |
| 146 | +void Pgr_dijkstraTRSP< G >::executeDijkstraTRSP(G& graph) { |
| 147 | + clear(); |
| 148 | + getFirstSolution(graph); |
| 149 | + log << curr_result_path; |
| 150 | + bool sol = checkFirstSolution(); |
| 151 | + if (sol) |
| 152 | + curr_result_path = Path(); |
| 153 | +} |
| 154 | + |
| 155 | +#endif // INCLUDE_DIJKSTRATRSP_PGR_DIJKSTRATRSP_HPP_ |
0 commit comments