Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ tools/template/mycreate.sh
.DS_Store
.vagrant
tools/vagrant/packaging.sh
tools/testers/pg_prove_dijkstraTRSP_tests.sh
.directory
notUsed
113 changes: 66 additions & 47 deletions include/dijkstraTRSP/pgr_dijkstraTRSP.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#pragma once

#include "dijkstra/pgr_dijkstra.hpp"
#include "dijkstraTRSP/restriction.h"

#include <sstream>
#include <deque>
Expand All @@ -30,30 +31,30 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#include "cpp_common/pgr_assert.h"
#include "cpp_common/basePath_SSEC.hpp"

#include "c_types/restrict_t.h"

template < class G >
class Pgr_dijkstraTRSP {
public:
Path dijkstraTRSP(
G& graph,
const std::vector<Restrict_t>& restrictions,
const std::vector< Restriction >& restrictions,
int64_t source,
int64_t target,
bool only_cost,
bool strict);
void clear();
private:
void executeDijkstraTRSP(G& graph);
void getFirstSolution(G& graph);
bool checkFirstSolution();
void getDijkstraSolution(G& graph);
bool has_restriction();
bool has_a_restriction(int64_t edge, int64_t index);
private:
typedef typename G::V V;
V v_source;
V v_target;
int64_t m_start;
int64_t m_end;
std::vector< std::pair<int64_t, Restrict_t> > m_restrictions;
std::vector< Restriction > m_restrictions;
std::vector< int64_t > m_edges_in_path;
bool m_only_cost;
bool m_strict;

Expand All @@ -69,7 +70,7 @@ void Pgr_dijkstraTRSP< G >::clear() {

template < class G>
Path
Pgr_dijkstraTRSP< G >::dijkstraTRSP(G& graph, const std::vector<Restrict_t>& restrictions,
Pgr_dijkstraTRSP< G >::dijkstraTRSP(G& graph, const std::vector< Restriction >& restrictions,
int64_t start_vertex, int64_t end_vertex, bool only_cost, bool strict) {
if (start_vertex == end_vertex)
return Path();
Expand All @@ -81,15 +82,14 @@ int64_t start_vertex, int64_t end_vertex, bool only_cost, bool strict) {
v_target = graph.get_V(end_vertex);
m_start = start_vertex;
m_end = end_vertex;
for(auto &restriction: restrictions)
m_restrictions.push_back( {restriction.restricted_edges[0], restriction} );
m_restrictions = restrictions;
m_strict = strict;
executeDijkstraTRSP(graph);
return curr_result_path;
}

template < class G >
void Pgr_dijkstraTRSP< G >::getFirstSolution(G& graph) {
void Pgr_dijkstraTRSP< G >::getDijkstraSolution(G& graph) {
Path path;

Pgr_dijkstra< G > fn_dijkstra;
Expand All @@ -100,56 +100,75 @@ void Pgr_dijkstraTRSP< G >::getFirstSolution(G& graph) {
}

template < class G >
bool Pgr_dijkstraTRSP< G >::checkFirstSolution() {
#if 0
auto sort_cmp = [](const std::pair<int64_t, Restrict_t>& left,
const std::pair<int64_t, Restrict_t>& right) -> bool {
return left.first <= right.first;
};
auto lower_bound_cmp = [](const std::pair<int64_t, Restrict_t>& p,
int64_t target) -> bool {
return p.first < target;
bool Pgr_dijkstraTRSP< G >::has_a_restriction(int64_t edge, int64_t index) {
auto lower_bound_cmp = [](const Restriction& r, const int64_t& target) -> bool {
return r.restrict_edges()[0] < target;
};
auto edge_index = std::lower_bound(m_restrictions.begin(),
m_restrictions.end(), edge, lower_bound_cmp) - m_restrictions.begin();
log << "\nResult generated from lower_bound\n";
while (edge_index < m_restrictions.size()) {
auto r_edges = m_restrictions[edge_index].restrict_edges();
if (r_edges[0] != edge) break;
log << m_restrictions[edge_index] << "\n";
bool okay = true;
size_t temp_edge_index = index;

for (auto &edge_id: r_edges) {
if (temp_edge_index >= m_edges_in_path.size() or
m_edges_in_path[temp_edge_index] != edge_id) {
okay = false;
break;
}
temp_edge_index++;
}
log << "\nokay value = " << okay <<"\n";
if (okay) return true;
edge_index++;
}
log << "Ends Here\n";
return false;
}

template < class G >
bool Pgr_dijkstraTRSP< G >::has_restriction() {
auto sort_cmp = [](const Restriction& left,
const Restriction& right) -> bool {
return left.restrict_edges()[0] <= right.restrict_edges()[0];
};
std::stable_sort(m_restrictions.begin(), m_restrictions.end(),
sort_cmp);
std::vector< int64_t > edges_in_path;
for (auto &path: curr_result_path) edges_in_path.push_back(path.edge);
if (edges_in_path.size() and edges_in_path[0] == -1) reverse(edges_in_path.begin(), edges_in_path.end());
while (edges_in_path.size() and edges_in_path.back() == -1) edges_in_path.pop_back();
log << "\nRestriction array after sorting.\n";
for (auto &it: m_restrictions) log << it << "\n";
log << "\nEnd\n";
size_t index = 0;
for (auto &edge: edges_in_path) {
auto edge_index = std::lower_bound(m_restrictions.begin(),
m_restrictions.end(), edge, lower_bound_cmp) - m_restrictions.begin();
while (edge_index < m_restrictions.size() and
m_restrictions[edge_index].first == edge) {
size_t temp_edge_index = index;
bool okay = true;
for (auto &edge_id: m_restrictions[edge_index].second.restricted_edges) {
if (edge_id == -1) break;
if(edges_in_path[temp_edge_index] != edge_id) okay = false;
temp_edge_index++;
}
if (okay) {
log << "Restriction is not applicable to this case.";
return false;
}
edge_index++;
}
for (auto &edge: m_edges_in_path) {
if (has_a_restriction(edge, index))
return true;
index++;
}
#endif
log << "Restriction is applicable to this case.";
return false;
}

template < class G >
void Pgr_dijkstraTRSP< G >::executeDijkstraTRSP(G& graph) {
clear();
getFirstSolution(graph);
getDijkstraSolution(graph);
log << curr_result_path;
bool sol = checkFirstSolution();
if (sol)
curr_result_path = Path();

for (auto &path: curr_result_path) {
m_edges_in_path.push_back(path.edge);
}
while (m_edges_in_path.size() and m_edges_in_path.back() == -1) {
m_edges_in_path.pop_back();
}

log << "Edges in m_edges_in_path:-------------------\n";
for(auto &it: m_edges_in_path) log << it << "\n";
log << "---------------------------------------------\n";
bool sol = has_restriction();
log << "Result of valid solution: " << sol << "\n";
if (sol) curr_result_path = Path();
}

#endif // INCLUDE_DIJKSTRATRSP_PGR_DIJKSTRATRSP_HPP_
53 changes: 53 additions & 0 deletions include/dijkstraTRSP/restriction.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*PGR-GNU*****************************************************************
File: restriction.h
Copyright (c) 2017 Celia Virginia Vergara Castillo
Mail: vicky_vergara@hotmail.com
------
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
********************************************************************PGR-GNU*/

#ifndef INCLUDE_DIJKSTRATRSP_RESTRICTION_H
#define INCLUDE_DIJKSTRATRSP_RESTRICTION_H
#pragma once

#include <sstream>
#include <deque>
#include <vector>
#include <set>
#include <limits>

#include "c_types/restrict_t.h"

class Restriction {
private:
int64_t m_id;
std::vector< int64_t > m_restrict_edges;
double m_cost;

public:
Restriction() = default;
Restriction(const Restrict_t &r);

int64_t id() const {return m_id;}
void id(const int64_t& value) {m_id = value;}

double cost() const {return m_cost;}
void cost(const double& value) {m_cost = value;}

std::vector< int64_t > restrict_edges() const {return m_restrict_edges;}
void restrict_edges(const int64_t& value) {m_restrict_edges.push_back(value);}

friend std::ostream& operator << (std::ostream &log, const Restriction &r);
};

#endif // INCLUDE_DIJKSTRATRSP_RESTRICTION_H
Loading