Moving cli client to tools directory

This commit is contained in:
Dennis Luxen 2013-06-27 16:08:33 -04:00
parent a0e9f59e04
commit 4194ce3095
3 changed files with 150 additions and 103 deletions

View File

@ -16,25 +16,15 @@ You should have received a copy of the GNU Affero General Public License
along with this program; if not, write to the Free Software along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
or see http://www.gnu.org/licenses/agpl.txt. or see http://www.gnu.org/licenses/agpl.txt.
Strongly connected components using Tarjan's Algorithm
*/ */
#ifndef STRONGLYCONNECTEDCOMPONENTS_H_ #ifndef STRONGLYCONNECTEDCOMPONENTS_H_
#define STRONGLYCONNECTEDCOMPONENTS_H_ #define STRONGLYCONNECTEDCOMPONENTS_H_
#include <cassert> #include "../DataStructures/Coordinate.h"
#include <stack>
#include <vector>
#include <boost/foreach.hpp>
#include <boost/make_shared.hpp>
#include <boost/unordered_map.hpp>
#include <gdal/gdal.h>
#include <gdal/ogrsf_frmts.h>
#include "../DataStructures/DeallocatingVector.h" #include "../DataStructures/DeallocatingVector.h"
#include "../DataStructures/DynamicGraph.h" #include "../DataStructures/DynamicGraph.h"
#include "../DataStructures/ImportEdge.h" #include "../DataStructures/ImportEdge.h"
@ -43,11 +33,34 @@ or see http://www.gnu.org/licenses/agpl.txt.
#include "../DataStructures/Restriction.h" #include "../DataStructures/Restriction.h"
#include "../DataStructures/TurnInstructions.h" #include "../DataStructures/TurnInstructions.h"
// Strongly connected components using Tarjan's Algorithm #include <boost/filesystem.hpp>
#include <boost/foreach.hpp>
#include <boost/integer.hpp>
#include <boost/make_shared.hpp>
#include <boost/unordered_map.hpp>
#ifdef __APPLE__
#include <gdal.h>
#include <ogrsf_frmts.h>
#else
#include <gdal/gdal.h>
#include <gdal/ogrsf_frmts.h>
#endif
#include <cassert>
#include <stack>
#include <vector>
class TarjanSCC { class TarjanSCC {
private: private:
struct _NodeBasedEdgeData {
struct TarjanNode {
TarjanNode() : index(UINT_MAX), lowlink(UINT_MAX), onStack(false) {}
unsigned index;
unsigned lowlink;
bool onStack;
};
struct TarjanEdgeData {
int distance; int distance;
unsigned edgeBasedNodeID; unsigned edgeBasedNodeID;
unsigned nameID:31; unsigned nameID:31;
@ -58,25 +71,31 @@ private:
bool backward:1; bool backward:1;
bool roundabout:1; bool roundabout:1;
bool ignoreInGrid:1; bool ignoreInGrid:1;
bool reversedEdge:1;
}; };
typedef DynamicGraph< _NodeBasedEdgeData > _NodeBasedDynamicGraph; struct TarjanStackFrame {
typedef _NodeBasedDynamicGraph::InputEdge _NodeBasedEdge; explicit TarjanStackFrame(NodeID _v, NodeID p) : v(_v), parent(p) {}
std::vector<NodeInfo> inputNodeInfoList; NodeID v;
unsigned numberOfTurnRestrictions; NodeID parent;
boost::shared_ptr<_NodeBasedDynamicGraph> _nodeBasedGraph; };
boost::unordered_map<NodeID, bool> _barrierNodes;
boost::unordered_map<NodeID, bool> _trafficLights;
typedef DynamicGraph<TarjanEdgeData> TarjanDynamicGraph;
typedef TarjanDynamicGraph::InputEdge TarjanEdge;
typedef std::pair<NodeID, NodeID> RestrictionSource; typedef std::pair<NodeID, NodeID> RestrictionSource;
typedef std::pair<NodeID, bool> RestrictionTarget; typedef std::pair<NodeID, bool> RestrictionTarget;
typedef std::vector<RestrictionTarget> EmanatingRestrictionsVector; typedef std::vector<RestrictionTarget> EmanatingRestrictionsVector;
typedef boost::unordered_map<RestrictionSource, unsigned > RestrictionMap; typedef boost::unordered_map<RestrictionSource, unsigned > RestrictionMap;
std::vector<NodeInfo> inputNodeInfoList;
unsigned numberOfTurnRestrictions;
boost::shared_ptr<TarjanDynamicGraph> _nodeBasedGraph;
boost::unordered_map<NodeID, bool> _barrierNodes;
boost::unordered_map<NodeID, bool> _trafficLights;
std::vector<EmanatingRestrictionsVector> _restrictionBucketVector; std::vector<EmanatingRestrictionsVector> _restrictionBucketVector;
RestrictionMap _restrictionMap; RestrictionMap _restrictionMap;
public:
struct EdgeBasedNode { struct EdgeBasedNode {
bool operator<(const EdgeBasedNode & other) const { bool operator<(const EdgeBasedNode & other) const {
return other.id < id; return other.id < id;
@ -95,21 +114,7 @@ public:
bool ignoreInGrid:1; bool ignoreInGrid:1;
}; };
private:
DeallocatingVector<EdgeBasedNode> edgeBasedNodes; DeallocatingVector<EdgeBasedNode> edgeBasedNodes;
struct TarjanNode {
TarjanNode() : index(UINT_MAX), lowlink(UINT_MAX), onStack(false) {}
unsigned index;
unsigned lowlink;
bool onStack;
};
struct TarjanStackFrame {
explicit TarjanStackFrame(NodeID _v, NodeID p) : v(_v), parent(p) {}
NodeID v;
NodeID parent;
};
public: public:
TarjanSCC(int nodes, std::vector<NodeBasedEdge> & inputEdges, std::vector<NodeID> & bn, std::vector<NodeID> & tl, std::vector<_Restriction> & irs, std::vector<NodeInfo> & nI) : inputNodeInfoList(nI), numberOfTurnRestrictions(irs.size()) { TarjanSCC(int nodes, std::vector<NodeBasedEdge> & inputEdges, std::vector<NodeID> & bn, std::vector<NodeID> & tl, std::vector<_Restriction> & irs, std::vector<NodeInfo> & nI) : inputNodeInfoList(nI), numberOfTurnRestrictions(irs.size()) {
BOOST_FOREACH(_Restriction & restriction, irs) { BOOST_FOREACH(_Restriction & restriction, irs) {
@ -141,10 +146,10 @@ public:
_trafficLights[id] = true; _trafficLights[id] = true;
} }
DeallocatingVector< _NodeBasedEdge > edges; DeallocatingVector< TarjanEdge > edges;
for ( std::vector< NodeBasedEdge >::const_iterator i = inputEdges.begin(); i != inputEdges.end(); ++i ) { for ( std::vector< NodeBasedEdge >::const_iterator i = inputEdges.begin(); i != inputEdges.end(); ++i ) {
_NodeBasedEdge edge; TarjanEdge edge;
if(!i->isForward()) { if(!i->isForward()) {
edge.source = i->target(); edge.source = i->target();
edge.target = i->source(); edge.target = i->source();
@ -168,21 +173,28 @@ public:
edge.data.type = i->type(); edge.data.type = i->type();
edge.data.isAccessRestricted = i->isAccessRestricted(); edge.data.isAccessRestricted = i->isAccessRestricted();
edge.data.edgeBasedNodeID = edges.size(); edge.data.edgeBasedNodeID = edges.size();
edge.data.reversedEdge = false;
edges.push_back( edge ); edges.push_back( edge );
if( edge.data.backward ) { if( edge.data.backward ) {
std::swap( edge.source, edge.target ); std::swap( edge.source, edge.target );
edge.data.forward = i->isBackward(); edge.data.forward = i->isBackward();
edge.data.backward = i->isForward(); edge.data.backward = i->isForward();
edge.data.edgeBasedNodeID = edges.size(); edge.data.edgeBasedNodeID = edges.size();
edge.data.reversedEdge = true;
edges.push_back( edge ); edges.push_back( edge );
} }
} }
std::vector<NodeBasedEdge>().swap(inputEdges); std::vector<NodeBasedEdge>().swap(inputEdges);
std::sort( edges.begin(), edges.end() ); std::sort( edges.begin(), edges.end() );
_nodeBasedGraph = boost::make_shared<_NodeBasedDynamicGraph>( nodes, edges ); _nodeBasedGraph = boost::make_shared<TarjanDynamicGraph>( nodes, edges );
} }
void Run() { void Run() {
//remove files from previous run if exist
DeleteFileIfExists("component.dbf");
DeleteFileIfExists("component.shx");
DeleteFileIfExists("component.shp");
Percent p(_nodeBasedGraph->GetNumberOfNodes()); Percent p(_nodeBasedGraph->GetNumberOfNodes());
const char *pszDriverName = "ESRI Shapefile"; const char *pszDriverName = "ESRI Shapefile";
@ -247,8 +259,8 @@ public:
// INFO("pushing " << v << " onto tarjan stack, idx[" << v << "]=" << tarjanNodes[v].index << ", lowlink["<< v << "]=" << tarjanNodes[v].lowlink); // INFO("pushing " << v << " onto tarjan stack, idx[" << v << "]=" << tarjanNodes[v].index << ", lowlink["<< v << "]=" << tarjanNodes[v].lowlink);
//Traverse outgoing edges //Traverse outgoing edges
for(_NodeBasedDynamicGraph::EdgeIterator e2 = _nodeBasedGraph->BeginEdges(v); e2 < _nodeBasedGraph->EndEdges(v); ++e2) { for(TarjanDynamicGraph::EdgeIterator e2 = _nodeBasedGraph->BeginEdges(v); e2 < _nodeBasedGraph->EndEdges(v); ++e2) {
_NodeBasedDynamicGraph::NodeIterator vprime = _nodeBasedGraph->GetTarget(e2); TarjanDynamicGraph::NodeIterator vprime = _nodeBasedGraph->GetTarget(e2);
// INFO("traversing edge (" << v << "," << vprime << ")"); // INFO("traversing edge (" << v << "," << vprime << ")");
if(UINT_MAX == tarjanNodes[vprime].index) { if(UINT_MAX == tarjanNodes[vprime].index) {
@ -306,16 +318,28 @@ public:
++singleCounter; ++singleCounter;
} }
INFO("identified " << singleCounter << " SCCs of size 1"); INFO("identified " << singleCounter << " SCCs of size 1");
uint64_t total_network_distance = 0;
p.reinit(_nodeBasedGraph->GetNumberOfNodes()); p.reinit(_nodeBasedGraph->GetNumberOfNodes());
for(_NodeBasedDynamicGraph::NodeIterator u = 0; u < _nodeBasedGraph->GetNumberOfNodes(); ++u ) { for(TarjanDynamicGraph::NodeIterator u = 0; u < _nodeBasedGraph->GetNumberOfNodes(); ++u ) {
for(_NodeBasedDynamicGraph::EdgeIterator e1 = _nodeBasedGraph->BeginEdges(u); e1 < _nodeBasedGraph->EndEdges(u); ++e1) { p.printIncrement();
_NodeBasedDynamicGraph::NodeIterator v = _nodeBasedGraph->GetTarget(e1); for(TarjanDynamicGraph::EdgeIterator e1 = _nodeBasedGraph->BeginEdges(u); e1 < _nodeBasedGraph->EndEdges(u); ++e1) {
if(_nodeBasedGraph->GetEdgeData(e1).reversedEdge) {
continue;
}
TarjanDynamicGraph::NodeIterator v = _nodeBasedGraph->GetTarget(e1);
total_network_distance += 100*ApproximateDistance(
inputNodeInfoList[u].lat,
inputNodeInfoList[u].lon,
inputNodeInfoList[v].lat,
inputNodeInfoList[v].lon
);
if(_nodeBasedGraph->GetEdgeData(e1).type != SHRT_MAX) { if(_nodeBasedGraph->GetEdgeData(e1).type != SHRT_MAX) {
assert(e1 != UINT_MAX); assert(e1 != UINT_MAX);
assert(u != UINT_MAX); assert(u != UINT_MAX);
assert(v != UINT_MAX); assert(v != UINT_MAX);
//edges that end on bollard nodes may actually be in two distinct components //edges that end on bollard nodes may actually be in two distinct components
if(std::min(vectorOfComponentSizes[componentsIndex[u]], vectorOfComponentSizes[componentsIndex[v]]) < 10) { if(std::min(vectorOfComponentSizes[componentsIndex[u]], vectorOfComponentSizes[componentsIndex[v]]) < 10) {
@ -323,7 +347,6 @@ public:
OGRLineString lineString; OGRLineString lineString;
lineString.addPoint(inputNodeInfoList[u].lon/100000., inputNodeInfoList[u].lat/100000.); lineString.addPoint(inputNodeInfoList[u].lon/100000., inputNodeInfoList[u].lat/100000.);
lineString.addPoint(inputNodeInfoList[v].lon/100000., inputNodeInfoList[v].lat/100000.); lineString.addPoint(inputNodeInfoList[v].lon/100000., inputNodeInfoList[v].lat/100000.);
OGRFeature *poFeature; OGRFeature *poFeature;
poFeature = OGRFeature::CreateFeature( poLayer->GetLayerDefn() ); poFeature = OGRFeature::CreateFeature( poLayer->GetLayerDefn() );
poFeature->SetGeometry( &lineString ); poFeature->SetGeometry( &lineString );
@ -339,7 +362,7 @@ public:
OGRDataSource::DestroyDataSource( poDS ); OGRDataSource::DestroyDataSource( poDS );
std::vector<NodeID>().swap(vectorOfComponentSizes); std::vector<NodeID>().swap(vectorOfComponentSizes);
std::vector<NodeID>().swap(componentsIndex); std::vector<NodeID>().swap(componentsIndex);
INFO("total network distance: " << total_network_distance/100/1000. << " km");
} }
private: private:
unsigned CheckForEmanatingIsOnlyTurn(const NodeID u, const NodeID v) const { unsigned CheckForEmanatingIsOnlyTurn(const NodeID u, const NodeID v) const {
@ -368,6 +391,12 @@ private:
} }
return false; return false;
} }
void DeleteFileIfExists(const std::string file_name) const {
if (boost::filesystem::exists(file_name) ) {
boost::filesystem::remove(file_name);
}
}
}; };
#endif /* STRONGLYCONNECTEDCOMPONENTS_H_ */ #endif /* STRONGLYCONNECTEDCOMPONENTS_H_ */

View File

@ -67,9 +67,6 @@ target_link_libraries( osrm-extract ${Boost_LIBRARIES} )
target_link_libraries( osrm-prepare ${Boost_LIBRARIES} ) target_link_libraries( osrm-prepare ${Boost_LIBRARIES} )
target_link_libraries( osrm-routed ${Boost_LIBRARIES} OSRM) target_link_libraries( osrm-routed ${Boost_LIBRARIES} OSRM)
add_executable ( osrm-cli simpleclient.cpp)
target_link_libraries( osrm-cli ${Boost_LIBRARIES} OSRM)
find_package ( BZip2 REQUIRED ) find_package ( BZip2 REQUIRED )
include_directories(${BZIP_INCLUDE_DIRS}) include_directories(${BZIP_INCLUDE_DIRS})
target_link_libraries (osrm-extract ${BZIP2_LIBRARIES}) target_link_libraries (osrm-extract ${BZIP2_LIBRARIES})
@ -119,4 +116,6 @@ if(WITH_TOOLS)
target_link_libraries( osrm-components ${GDAL_LIBRARIES} ) target_link_libraries( osrm-components ${GDAL_LIBRARIES} )
target_link_libraries( osrm-components ${Boost_LIBRARIES} ) target_link_libraries( osrm-components ${Boost_LIBRARIES} )
endif(GDAL_FOUND) endif(GDAL_FOUND)
add_executable ( osrm-cli Tools/simpleclient.cpp)
target_link_libraries( osrm-cli ${Boost_LIBRARIES} OSRM)
endif(WITH_TOOLS) endif(WITH_TOOLS)

View File

@ -18,23 +18,6 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
or see http://www.gnu.org/licenses/agpl.txt. or see http://www.gnu.org/licenses/agpl.txt.
*/ */
#define VERBOSE(x) x
#define VERBOSE2(x)
#ifdef NDEBUG
#undef VERBOSE
#undef VERBOSE2
#endif
#include <boost/foreach.hpp>
#include <fstream>
#include <istream>
#include <iostream>
#include <cstring>
#include <string>
#include <vector>
#include "../typedefs.h" #include "../typedefs.h"
#include "../Algorithms/StronglyConnectedComponents.h" #include "../Algorithms/StronglyConnectedComponents.h"
#include "../DataStructures/BinaryHeap.h" #include "../DataStructures/BinaryHeap.h"
@ -46,57 +29,93 @@ or see http://www.gnu.org/licenses/agpl.txt.
#include "../Util/InputFileUtil.h" #include "../Util/InputFileUtil.h"
#include "../Util/GraphLoader.h" #include "../Util/GraphLoader.h"
using namespace std; #include <boost/foreach.hpp>
#include <fstream>
#include <istream>
#include <iostream>
#include <cstring>
#include <string>
#include <vector>
typedef QueryEdge::EdgeData EdgeData; typedef QueryEdge::EdgeData EdgeData;
typedef DynamicGraph<EdgeData>::InputEdge InputEdge; typedef DynamicGraph<EdgeData>::InputEdge InputEdge;
typedef BaseConfiguration ContractorConfiguration; typedef BaseConfiguration ContractorConfiguration;
std::vector<NodeInfo> internalToExternalNodeMapping; std::vector<NodeInfo> internal_to_external_node_map;
std::vector<_Restriction> inputRestrictions; std::vector<_Restriction> restrictions_vector;
std::vector<NodeID> bollardNodes; std::vector<NodeID> bollard_node_IDs_vector;
std::vector<NodeID> trafficLightNodes; std::vector<NodeID> traffic_light_node_IDs_vector;
int main (int argc, char *argv[]) { int main (int argument_count, char *argument_values[]) {
if(argc < 3) { if(argument_count < 3) {
ERR("usage: " << std::endl << argv[0] << " <osrm-data> <osrm-restrictions>"); ERR("usage:\n" << argument_values[0] << " <osrm> <osrm.restrictions>");
} }
std::string SRTM_ROOT;
INFO("Using restrictions from file: " << argv[2]); INFO("Using restrictions from file: " << argument_values[2]);
std::ifstream restrictionsInstream(argv[2], ios::binary); std::ifstream restriction_ifstream(argument_values[2], std::ios::binary);
if(!restrictionsInstream.good()) { if(!restriction_ifstream.good()) {
ERR("Could not access <osrm-restrictions> files"); ERR("Could not access <osrm-restrictions> files");
} }
_Restriction restriction; uint32_t usable_restriction_count = 0;
unsigned usableRestrictionsCounter(0); restriction_ifstream.read(
restrictionsInstream.read((char*)&usableRestrictionsCounter, sizeof(unsigned)); (char*)&usable_restriction_count,
inputRestrictions.resize(usableRestrictionsCounter); sizeof(uint32_t)
restrictionsInstream.read((char *)&(inputRestrictions[0]), usableRestrictionsCounter*sizeof(_Restriction)); );
restrictionsInstream.close(); restrictions_vector.resize(usable_restriction_count);
std::ifstream in; restriction_ifstream.read(
in.open (argv[1], std::ifstream::in | std::ifstream::binary); (char *)&(restrictions_vector[0]),
if (!in.is_open()) { usable_restriction_count*sizeof(_Restriction)
ERR("Cannot open " << argv[1]); );
restriction_ifstream.close();
std::ifstream input_stream;
input_stream.open(
argument_values[1],
std::ifstream::in | std::ifstream::binary
);
if (!input_stream.is_open()) {
ERR("Cannot open " << argument_values[1]);
} }
std::vector<ImportEdge> edgeList; std::vector<ImportEdge> edge_list;
NodeID nodeBasedNodeNumber = readBinaryOSRMGraphFromStream(in, edgeList, bollardNodes, trafficLightNodes, &internalToExternalNodeMapping, inputRestrictions); NodeID node_based_node_count = readBinaryOSRMGraphFromStream(
in.close(); input_stream,
INFO(inputRestrictions.size() << " restrictions, " << bollardNodes.size() << " bollard nodes, " << trafficLightNodes.size() << " traffic lights"); edge_list,
bollard_node_IDs_vector,
traffic_light_node_IDs_vector,
&internal_to_external_node_map,
restrictions_vector
);
input_stream.close();
INFO(
restrictions_vector.size() << " restrictions, " <<
bollard_node_IDs_vector.size() << " bollard nodes, " <<
traffic_light_node_IDs_vector.size() << " traffic lights"
);
/*** /***
* Building an edge-expanded graph from node-based input an turn restrictions * Building an edge-expanded graph from node-based input an turn restrictions
*/ */
INFO("Starting SCC graph traversal"); INFO("Starting SCC graph traversal");
TarjanSCC * tarjan = new TarjanSCC (nodeBasedNodeNumber, edgeList, bollardNodes, trafficLightNodes, inputRestrictions, internalToExternalNodeMapping); TarjanSCC * tarjan = new TarjanSCC (
std::vector<ImportEdge>().swap(edgeList); node_based_node_count,
edge_list,
bollard_node_IDs_vector,
traffic_light_node_IDs_vector,
restrictions_vector,
internal_to_external_node_map
);
std::vector<ImportEdge>().swap(edge_list);
tarjan->Run(); tarjan->Run();
std::vector<_Restriction>().swap(inputRestrictions);
std::vector<NodeID>().swap(bollardNodes); std::vector<_Restriction>().swap(restrictions_vector);
std::vector<NodeID>().swap(trafficLightNodes); std::vector<NodeID>().swap(bollard_node_IDs_vector);
std::vector<NodeID>().swap(traffic_light_node_IDs_vector);
INFO("finished component analysis"); INFO("finished component analysis");
return 0; return 0;
} }