Skip to content

Commit 7a275a1

Browse files
committed
Added logic for testing restricted edge in result
1 parent f5e6b09 commit 7a275a1

File tree

3 files changed

+44
-11
lines changed

3 files changed

+44
-11
lines changed

include/dijkstraTRSP/pgr_dijkstraTRSP.hpp

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,14 @@ class Pgr_dijkstraTRSP {
4646
private:
4747
void executeDijkstraTRSP(G& graph);
4848
void getFirstSolution(G& graph);
49-
void checkFirstSolution(G& graph);
49+
bool checkFirstSolution();
5050
private:
5151
typedef typename G::V V;
5252
V v_source;
5353
V v_target;
5454
int64_t m_start;
5555
int64_t m_end;
56-
std::vector<Restrict_t> m_restrictions;
56+
std::vector< std::pair<int64_t, Restrict_t> > m_restrictions;
5757
bool m_only_cost;
5858
bool m_strict;
5959

@@ -82,7 +82,7 @@ int64_t start_vertex, int64_t end_vertex, bool only_cost, bool strict) {
8282
m_start = start_vertex;
8383
m_end = end_vertex;
8484
for(auto &restriction: restrictions)
85-
m_restrictions.push_back(restriction);
85+
m_restrictions.push_back( {restriction.restricted_edges[0], restriction} );
8686
m_strict = strict;
8787
executeDijkstraTRSP(graph);
8888
return curr_result_path;
@@ -100,16 +100,50 @@ void Pgr_dijkstraTRSP< G >::getFirstSolution(G& graph) {
100100
}
101101

102102
template < class G >
103-
void Pgr_dijkstraTRSP< G >::checkFirstSolution(G& graph) {
104-
auto pathlength = curr_result_path.size();
105-
log << curr_result_path;
103+
bool Pgr_dijkstraTRSP< G >::checkFirstSolution() {
104+
auto sort_cmp = [](const std::pair<int64_t, Restrict_t>& left,
105+
const std::pair<int64_t, Restrict_t>& right) -> bool {
106+
return left.first <= right.first;
107+
};
108+
auto lower_bound_cmp = [](const std::pair<int64_t, Restrict_t>& p,
109+
int64_t target) -> bool {
110+
return p.first < target;
111+
};
112+
std::stable_sort(m_restrictions.begin(), m_restrictions.end(),
113+
sort_cmp);
114+
std::vector< int64_t > edges_in_path;
115+
for (auto &path: curr_result_path) edges_in_path.push_back(path.edge);
116+
while (edges_in_path.size() and edges_in_path.back() == -1) edges_in_path.pop_back();
117+
size_t index = 0;
118+
for (auto &edge: edges_in_path) {
119+
auto edge_index = std::lower_bound(m_restrictions.begin(),
120+
m_restrictions.end(), edge, lower_bound_cmp) - m_restrictions.begin();
121+
while (edge_index < m_restrictions.size() and
122+
m_restrictions[edge_index].first == edge) {
123+
size_t temp_edge_index = index;
124+
bool okay = true;
125+
for (auto &edge_id: m_restrictions[edge_index].second.restricted_edges) {
126+
if (edge_id == -1) break;
127+
if(edges_in_path[temp_edge_index] != edge_id) okay = false;
128+
temp_edge_index++;
129+
}
130+
if(okay)
131+
return false;
132+
edge_index++;
133+
}
134+
index++;
135+
}
136+
return true;
106137
}
107138

108139
template < class G >
109140
void Pgr_dijkstraTRSP< G >::executeDijkstraTRSP(G& graph) {
110141
clear();
111142
getFirstSolution(graph);
112-
checkFirstSolution(graph);
143+
bool sol = checkFirstSolution();
144+
std::cout<<sol<<"\n";
145+
if (sol)
146+
curr_result_path = Path();
113147
}
114148

115149
#endif // INCLUDE_DIJKSTRATRSP_PGR_DIJKSTRATRSP_HPP_

src/dijkstraTRSP/src/dijkstraTRSP.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,6 @@ process(
153153
if (*result_tuples) pfree(*result_tuples);
154154
}
155155
pgr_global_report(log_msg, notice_msg, err_msg);
156-
157156
if (edges) pfree(edges);
158157
if (log_msg) pfree(log_msg);
159158
if (notice_msg) pfree(notice_msg);

src/dijkstraTRSP/src/dijkstraTRSP_driver.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
5151
only_cost BOOLEAN DEFAULT false,
5252
strict BOOLEAN DEFAULT false
5353
***********************************************************/
54-
#if 1
54+
#if 0
5555
template < class G >
5656
static
5757
Path
@@ -120,7 +120,7 @@ do_pgr_dijkstraTRSP(
120120
end_vid,
121121
only_cost,
122122
strict);
123-
log << fn_TRSP.log.str();
123+
log << fn_TRSP.log.str().c_str();
124124
} else {
125125
log << "Working with Undirected Graph\n";
126126
pgrouting::UndirectedGraph undigraph(gType);
@@ -132,7 +132,7 @@ do_pgr_dijkstraTRSP(
132132
end_vid,
133133
only_cost,
134134
strict);
135-
log << fn_TRSP.log.str();
135+
log << fn_TRSP.log.str().c_str();
136136
}
137137

138138
auto count = path.size();

0 commit comments

Comments
 (0)