performance fixes

This commit is contained in:
Dennis Luxen 2011-08-07 10:56:37 +00:00
parent a5c94c4630
commit bcb39b9e69
5 changed files with 380 additions and 401 deletions

View File

@ -32,7 +32,7 @@ or see http://www.gnu.org/licenses/agpl.txt.
#include <google/sparse_hash_map> #include <google/sparse_hash_map>
#include <google/sparsetable> #include <google/sparsetable>
template< typename NodeID, typename Key, bool initialize = true > template< typename NodeID, typename Key, bool initialize = false >
class ArrayStorage { class ArrayStorage {
public: public:

View File

@ -30,58 +30,58 @@ or see http://www.gnu.org/licenses/agpl.txt.
#include "../typedefs.h" #include "../typedefs.h"
struct _HeapData { struct _HeapData {
NodeID parent; NodeID parent;
_HeapData( NodeID p ) : parent(p) { } _HeapData( NodeID p ) : parent(p) { }
}; };
struct _Statistics { struct _Statistics {
_Statistics () : insertedNodes(0), stalledNodes(0), meetingNodes(0), deleteMins(0), decreasedNodes(0), oqf(0), eqf(0), df(0), preprocTime(0) {}; _Statistics () : insertedNodes(0), stalledNodes(0), meetingNodes(0), deleteMins(0), decreasedNodes(0), oqf(0), eqf(0), df(0), preprocTime(0) {};
void Reset() { void Reset() {
insertedNodes = 0; insertedNodes = 0;
stalledNodes = 0; stalledNodes = 0;
meetingNodes = 0; meetingNodes = 0;
deleteMins = 0; deleteMins = 0;
decreasedNodes = 0; decreasedNodes = 0;
} }
unsigned insertedNodes; unsigned insertedNodes;
unsigned stalledNodes; unsigned stalledNodes;
unsigned meetingNodes; unsigned meetingNodes;
unsigned deleteMins; unsigned deleteMins;
unsigned decreasedNodes; unsigned decreasedNodes;
unsigned oqf; unsigned oqf;
unsigned eqf; unsigned eqf;
unsigned df; unsigned df;
double preprocTime; double preprocTime;
}; };
struct _InsertedNodes { struct _InsertedNodes {
NodeID forward1; NodeID forward1;
NodeID forward2; NodeID forward2;
NodeID backward1; NodeID backward1;
NodeID backward2; NodeID backward2;
_InsertedNodes() : forward1(UINT_MAX), forward2(UINT_MAX), backward1(UINT_MAX), backward2(UINT_MAX) {}; _InsertedNodes() : forward1(UINT_MAX), forward2(UINT_MAX), backward1(UINT_MAX), backward2(UINT_MAX) {};
void BackInsert(NodeID n) { void BackInsert(NodeID n) {
if(backward1 == UINT_MAX) { if(backward1 == UINT_MAX) {
backward1 = n; backward1 = n;
} else { } else {
backward2 = n; backward2 = n;
} }
} }
void ForwInsert( NodeID n) { void ForwInsert( NodeID n) {
if(forward1 == UINT_MAX) { if(forward1 == UINT_MAX) {
forward1 = n; forward1 = n;
} else { } else {
forward2 = n; forward2 = n;
} }
} }
inline bool isForwardInserted(NodeID n) { inline bool isForwardInserted(NodeID n) {
return forward1 == n || forward2 == n; return forward1 == n || forward2 == n;
} }
inline bool isBackwardInserted (NodeID n) { inline bool isBackwardInserted (NodeID n) {
return backward1 == n || backward2 == n; return backward1 == n || backward2 == n;
} }
}; };
@ -90,390 +90,391 @@ typedef BinaryHeap< NodeID, int, int, _HeapData, ArrayStorage<NodeID, NodeID> >
template<typename EdgeData, typename GraphT, typename NodeHelperT = NodeInformationHelpDesk> template<typename EdgeData, typename GraphT, typename NodeHelperT = NodeInformationHelpDesk>
class SearchEngine { class SearchEngine {
private: private:
const GraphT * _graph; const GraphT * _graph;
NodeHelperT * nodeHelpDesk; NodeHelperT * nodeHelpDesk;
std::vector<string> * _names; std::vector<string> * _names;
inline double absDouble(double input) { if(input < 0) return input*(-1); else return input;} inline double absDouble(double input) { if(input < 0) return input*(-1); else return input;}
public: public:
SearchEngine(GraphT * g, NodeHelperT * nh, vector<string> * n = new vector<string>()) : _graph(g), nodeHelpDesk(nh), _names(n) {} SearchEngine(GraphT * g, NodeHelperT * nh, vector<string> * n = new vector<string>()) : _graph(g), nodeHelpDesk(nh), _names(n) {}
~SearchEngine() {} ~SearchEngine() {}
inline const void getCoordinatesForNodeID(NodeID id, _Coordinate& result) const { inline const void getCoordinatesForNodeID(NodeID id, _Coordinate& result) const {
result.lat = nodeHelpDesk->getLatitudeOfNode(id); result.lat = nodeHelpDesk->getLatitudeOfNode(id);
result.lon = nodeHelpDesk->getLongitudeOfNode(id); result.lon = nodeHelpDesk->getLongitudeOfNode(id);
} }
unsigned int numberOfNodes() const { unsigned int numberOfNodes() const {
return nodeHelpDesk->getNumberOfNodes(); return nodeHelpDesk->getNumberOfNodes();
} }
unsigned int ComputeRoute(PhantomNodes &phantomNodes, vector<_PathData > & path) { unsigned int ComputeRoute(PhantomNodes &phantomNodes, vector<_PathData > & path) {
bool startEdgeIsReversedInGraph = false;
bool targetEdgeIsReversed = false;
unsigned int _upperbound = UINT_MAX; bool startEdgeIsReversedInGraph = false;
bool targetEdgeIsReversed = false;
if(!phantomNodes.AtLeastOnePhantomNodeIsUINTMAX()) unsigned int _upperbound = UINT_MAX;
return _upperbound;
EdgeID sourceEdgeID = _graph->FindEdgeIndicateIfReverse( phantomNodes.startPhantom.startNode, phantomNodes.startPhantom.targetNode, startEdgeIsReversedInGraph); if(!phantomNodes.AtLeastOnePhantomNodeIsUINTMAX())
if(sourceEdgeID == UINT_MAX){ return _upperbound;
return _upperbound;
}
EdgeID targetEdgeID = _graph->FindEdgeIndicateIfReverse( phantomNodes.targetPhantom.startNode, phantomNodes.targetPhantom.targetNode, targetEdgeIsReversed); EdgeID sourceEdgeID = _graph->FindEdgeIndicateIfReverse( phantomNodes.startPhantom.startNode, phantomNodes.startPhantom.targetNode, startEdgeIsReversedInGraph);
if(targetEdgeID == UINT_MAX){ if(sourceEdgeID == UINT_MAX){
return _upperbound; return _upperbound;
} }
_InsertedNodes _insertedNodes; EdgeID targetEdgeID = _graph->FindEdgeIndicateIfReverse( phantomNodes.targetPhantom.startNode, phantomNodes.targetPhantom.targetNode, targetEdgeIsReversed);
_Heap _forwardHeap(nodeHelpDesk->getNumberOfNodes()); if(targetEdgeID == UINT_MAX){
_Heap _backwardHeap(nodeHelpDesk->getNumberOfNodes()); return _upperbound;
}
NodeID middle = ( NodeID ) 0; _InsertedNodes _insertedNodes;
if( phantomNodes.PhantomsAreOnSameEdge() ) { _Heap _forwardHeap(nodeHelpDesk->getNumberOfNodes());
const EdgeData& currentEdgeData = _graph->GetEdgeData(sourceEdgeID); _Heap _backwardHeap(nodeHelpDesk->getNumberOfNodes());
EdgeWeight w = currentEdgeData.distance;
//check if target is reachable from start on same edge NodeID middle = ( NodeID ) UINT_MAX;
if(currentEdgeData.forward && currentEdgeData.backward) {
_upperbound = absDouble( w*phantomNodes.targetPhantom.ratio);
return _upperbound/10;
} else {
if((startEdgeIsReversedInGraph && (phantomNodes.startPhantom.ratio > phantomNodes.targetPhantom.ratio)) || (!startEdgeIsReversedInGraph && (phantomNodes.startPhantom.ratio < phantomNodes.targetPhantom.ratio))) {
_backwardHeap.Insert(phantomNodes.startPhantom.startNode, absDouble( w*phantomNodes.startPhantom.ratio), phantomNodes.startPhantom.startNode);
_insertedNodes.BackInsert(phantomNodes.startPhantom.startNode);
_forwardHeap.Insert(phantomNodes.startPhantom.targetNode, absDouble( w-w*phantomNodes.startPhantom.ratio), phantomNodes.startPhantom.targetNode);
_insertedNodes.ForwInsert(phantomNodes.startPhantom.targetNode);
} else {
_upperbound = absDouble( w*phantomNodes.targetPhantom.ratio);
return _upperbound/10;
}
}
}
//insert start and/or target node of start edge if( phantomNodes.PhantomsAreOnSameEdge() ) {
const EdgeData& sourceEdgeData = _graph->GetEdgeData(sourceEdgeID); const EdgeData& currentEdgeData = _graph->GetEdgeData(sourceEdgeID);
EdgeWeight sw = sourceEdgeData.distance; EdgeWeight w = currentEdgeData.distance;
if( (sourceEdgeData.backward && !startEdgeIsReversedInGraph) || (sourceEdgeData.forward && startEdgeIsReversedInGraph) ){ //check if target is reachable from start on same edge
_forwardHeap.Insert(phantomNodes.startPhantom.startNode, absDouble( sw*phantomNodes.startPhantom.ratio), phantomNodes.startPhantom.startNode); if(currentEdgeData.forward && currentEdgeData.backward) {
_insertedNodes.ForwInsert(phantomNodes.startPhantom.startNode); _upperbound = absDouble( w*phantomNodes.targetPhantom.ratio);
} return _upperbound/10;
if( (sourceEdgeData.backward && startEdgeIsReversedInGraph) || (sourceEdgeData.forward && !startEdgeIsReversedInGraph) ) { } else {
_forwardHeap.Insert(phantomNodes.startPhantom.targetNode, absDouble(sw-sw*phantomNodes.startPhantom.ratio), phantomNodes.startPhantom.targetNode); if((startEdgeIsReversedInGraph && (phantomNodes.startPhantom.ratio > phantomNodes.targetPhantom.ratio)) || (!startEdgeIsReversedInGraph && (phantomNodes.startPhantom.ratio < phantomNodes.targetPhantom.ratio))) {
_insertedNodes.ForwInsert(phantomNodes.startPhantom.targetNode); _backwardHeap.Insert(phantomNodes.startPhantom.startNode, absDouble( w*phantomNodes.startPhantom.ratio), phantomNodes.startPhantom.startNode);
} _insertedNodes.BackInsert(phantomNodes.startPhantom.startNode);
_forwardHeap.Insert(phantomNodes.startPhantom.targetNode, absDouble( w-w*phantomNodes.startPhantom.ratio), phantomNodes.startPhantom.targetNode);
_insertedNodes.ForwInsert(phantomNodes.startPhantom.targetNode);
} else {
_upperbound = absDouble( w*phantomNodes.targetPhantom.ratio);
return _upperbound/10;
}
}
}
//insert start and/or target node of target edge id //insert start and/or target node of start edge
const EdgeData& targetEdgeData = _graph->GetEdgeData(targetEdgeID); const EdgeData& sourceEdgeData = _graph->GetEdgeData(sourceEdgeID);
EdgeWeight tw = targetEdgeData.distance; EdgeWeight sw = sourceEdgeData.distance;
if( (targetEdgeData.backward && !targetEdgeIsReversed) || (targetEdgeData.forward && targetEdgeIsReversed) ) { if( (sourceEdgeData.backward && !startEdgeIsReversedInGraph) || (sourceEdgeData.forward && startEdgeIsReversedInGraph) ){
_backwardHeap.Insert(phantomNodes.targetPhantom.targetNode, absDouble( tw*phantomNodes.targetPhantom.ratio), phantomNodes.targetPhantom.targetNode); _forwardHeap.Insert(phantomNodes.startPhantom.startNode, absDouble( sw*phantomNodes.startPhantom.ratio), phantomNodes.startPhantom.startNode);
_insertedNodes.BackInsert(phantomNodes.targetPhantom.targetNode); _insertedNodes.ForwInsert(phantomNodes.startPhantom.startNode);
} }
if( (targetEdgeData.backward && targetEdgeIsReversed) || (targetEdgeData.forward && !targetEdgeIsReversed) ) { if( (sourceEdgeData.backward && startEdgeIsReversedInGraph) || (sourceEdgeData.forward && !startEdgeIsReversedInGraph) ) {
_backwardHeap.Insert(phantomNodes.targetPhantom.startNode, absDouble(tw-tw*phantomNodes.targetPhantom.ratio), phantomNodes.targetPhantom.startNode); _forwardHeap.Insert(phantomNodes.startPhantom.targetNode, absDouble(sw-sw*phantomNodes.startPhantom.ratio), phantomNodes.startPhantom.targetNode);
_insertedNodes.BackInsert(phantomNodes.targetPhantom.startNode); _insertedNodes.ForwInsert(phantomNodes.startPhantom.targetNode);
} }
//insert start and/or target node of target edge id
const EdgeData& targetEdgeData = _graph->GetEdgeData(targetEdgeID);
EdgeWeight tw = targetEdgeData.distance;
while(_forwardHeap.Size() + _backwardHeap.Size() > 0) { if( (targetEdgeData.backward && !targetEdgeIsReversed) || (targetEdgeData.forward && targetEdgeIsReversed) ) {
if ( _forwardHeap.Size() > 0 ) { _backwardHeap.Insert(phantomNodes.targetPhantom.targetNode, absDouble( tw*phantomNodes.targetPhantom.ratio), phantomNodes.targetPhantom.targetNode);
_RoutingStep( _forwardHeap, _backwardHeap, true, &middle, &_upperbound ); _insertedNodes.BackInsert(phantomNodes.targetPhantom.targetNode);
} }
if ( _backwardHeap.Size() > 0 ) { if( (targetEdgeData.backward && targetEdgeIsReversed) || (targetEdgeData.forward && !targetEdgeIsReversed) ) {
_RoutingStep( _backwardHeap, _forwardHeap, false, &middle, &_upperbound ); _backwardHeap.Insert(phantomNodes.targetPhantom.startNode, absDouble(tw-tw*phantomNodes.targetPhantom.ratio), phantomNodes.targetPhantom.startNode);
} _insertedNodes.BackInsert(phantomNodes.targetPhantom.startNode);
} }
if ( _upperbound == UINT_MAX ) { while(_forwardHeap.Size() + _backwardHeap.Size() > 0) {
return _upperbound; if ( _forwardHeap.Size() > 0 ) {
} _RoutingStep( _forwardHeap, _backwardHeap, true, &middle, &_upperbound );
}
if ( _backwardHeap.Size() > 0 ) {
_RoutingStep( _backwardHeap, _forwardHeap, false, &middle, &_upperbound );
}
}
NodeID pathNode = middle; if ( _upperbound == UINT_MAX ) {
deque< NodeID > packedPath; return _upperbound;
}
while ( false == _insertedNodes.isForwardInserted(pathNode) ) { NodeID pathNode = middle;
pathNode = _forwardHeap.GetData( pathNode ).parent; deque< NodeID > packedPath;
packedPath.push_front( pathNode );
}
packedPath.push_back( middle ); while ( false == _insertedNodes.isForwardInserted(pathNode) ) {
pathNode = middle; pathNode = _forwardHeap.GetData( pathNode ).parent;
packedPath.push_front( pathNode );
}
while ( false == _insertedNodes.isBackwardInserted(pathNode) ){ packedPath.push_back( middle );
pathNode = _backwardHeap.GetData( pathNode ).parent; pathNode = middle;
packedPath.push_back( pathNode );
}
path.push_back( _PathData(packedPath[0]) ); while ( false == _insertedNodes.isBackwardInserted(pathNode) ){
for(deque<NodeID>::size_type i = 0; i < packedPath.size()-1; i++) { pathNode = _backwardHeap.GetData( pathNode ).parent;
_UnpackEdge(packedPath[i], packedPath[i+1], path); packedPath.push_back( pathNode );
} }
packedPath.clear(); path.push_back( _PathData(packedPath[0]) );
return _upperbound/10; for(deque<NodeID>::size_type i = 0; i < packedPath.size()-1; i++) {
} _UnpackEdge(packedPath[i], packedPath[i+1], path);
}
unsigned int ComputeDistanceBetweenNodes(NodeID start, NodeID target) { packedPath.clear();
_Heap _forwardHeap(_graph->GetNumberOfNodes()); return _upperbound/10;
_Heap _backwardHeap(_graph->GetNumberOfNodes()); }
NodeID middle(UINT_MAX);
unsigned int _upperbound = UINT_MAX;
_forwardHeap.Insert(start, 0, start); unsigned int ComputeDistanceBetweenNodes(NodeID start, NodeID target) {
_backwardHeap.Insert(target, 0, target); _Heap _forwardHeap(_graph->GetNumberOfNodes());
_Heap _backwardHeap(_graph->GetNumberOfNodes());
NodeID middle(UINT_MAX);
unsigned int _upperbound = UINT_MAX;
while(_forwardHeap.Size() + _backwardHeap.Size() > 0) { _forwardHeap.Insert(start, 0, start);
if ( _forwardHeap.Size() > 0 ) { _backwardHeap.Insert(target, 0, target);
_RoutingStep( _forwardHeap, _backwardHeap, true, &middle, &_upperbound );
}
if ( _backwardHeap.Size() > 0 ) {
_RoutingStep( _backwardHeap, _forwardHeap, false, &middle, &_upperbound );
}
}
return _upperbound;
}
unsigned int ComputeDistanceBetweenNodesWithStats(NodeID start, NodeID target, _Statistics& stats) { while(_forwardHeap.Size() + _backwardHeap.Size() > 0) {
_Heap _forwardHeap(_graph->GetNumberOfNodes()); if ( _forwardHeap.Size() > 0 ) {
_Heap _backwardHeap(_graph->GetNumberOfNodes()); _RoutingStep( _forwardHeap, _backwardHeap, true, &middle, &_upperbound );
NodeID middle(UINT_MAX); }
unsigned int _upperbound = UINT_MAX; if ( _backwardHeap.Size() > 0 ) {
_RoutingStep( _backwardHeap, _forwardHeap, false, &middle, &_upperbound );
}
}
return _upperbound;
}
_forwardHeap.Insert(start, 0, start); unsigned int ComputeDistanceBetweenNodesWithStats(NodeID start, NodeID target, _Statistics& stats) {
_backwardHeap.Insert(target, 0, target); _Heap _forwardHeap(_graph->GetNumberOfNodes());
stats.insertedNodes += 2; _Heap _backwardHeap(_graph->GetNumberOfNodes());
NodeID middle(UINT_MAX);
unsigned int _upperbound = UINT_MAX;
while(_forwardHeap.Size() + _backwardHeap.Size() > 0) { _forwardHeap.Insert(start, 0, start);
if ( _forwardHeap.Size() > 0 ) { _backwardHeap.Insert(target, 0, target);
_RoutingStepWithStats( _forwardHeap, _backwardHeap, true, &middle, &_upperbound, stats ); stats.insertedNodes += 2;
}
if ( _backwardHeap.Size() > 0 ) {
_RoutingStepWithStats( _backwardHeap, _forwardHeap, false, &middle, &_upperbound, stats );
}
}
return _upperbound;
}
inline unsigned int findNearestNodeForLatLon(const _Coordinate& coord, _Coordinate& result) const while(_forwardHeap.Size() + _backwardHeap.Size() > 0) {
{ if ( _forwardHeap.Size() > 0 ) {
nodeHelpDesk->findNearestNodeCoordForLatLon( coord, result ); _RoutingStepWithStats( _forwardHeap, _backwardHeap, true, &middle, &_upperbound, stats );
return 0; }
if ( _backwardHeap.Size() > 0 ) {
_RoutingStepWithStats( _backwardHeap, _forwardHeap, false, &middle, &_upperbound, stats );
}
}
return _upperbound;
}
} inline unsigned int findNearestNodeForLatLon(const _Coordinate& coord, _Coordinate& result) const
{
nodeHelpDesk->findNearestNodeCoordForLatLon( coord, result );
return 0;
inline bool FindRoutingStarts(const _Coordinate &start, const _Coordinate &target, PhantomNodes & routingStarts) { }
nodeHelpDesk->FindRoutingStarts(start, target, routingStarts);
return true;
}
inline bool FindPhantomNodeForCoordinate(const _Coordinate &location, PhantomNode & result) { inline bool FindRoutingStarts(const _Coordinate &start, const _Coordinate &target, PhantomNodes & routingStarts) {
return nodeHelpDesk->FindPhantomNodeForCoordinate(location, result); nodeHelpDesk->FindRoutingStarts(start, target, routingStarts);
} return true;
}
inline NodeID GetNameIDForOriginDestinationNodeID(NodeID s, NodeID t) const { inline bool FindPhantomNodeForCoordinate(const _Coordinate &location, PhantomNode & result) {
//INFO("Getting nameID for s=" << s << " and t=" << t); return nodeHelpDesk->FindPhantomNodeForCoordinate(location, result);
if(s==t) }
return 0;
EdgeID e = _graph->FindEdge( s, t );
if(e == UINT_MAX)
e = _graph->FindEdge( t, s );
if(UINT_MAX == e) {
// INFO("edge not found for start " << s << ", target " << t)
return 0;
}
assert(e != UINT_MAX);
const EdgeData ed = _graph->GetEdgeData(e);
return ed.middleName.nameID;
}
inline NodeID GetWeightForOriginDestinationNodeID(NodeID s, NodeID t) const { inline NodeID GetNameIDForOriginDestinationNodeID(NodeID s, NodeID t) const {
assert(s!=t); //INFO("Getting nameID for s=" << s << " and t=" << t);
EdgeID e = _graph->FindEdge( s, t ); if(s==t)
if(e == UINT_MAX) return 0;
e = _graph->FindEdge( t, s ); EdgeID e = _graph->FindEdge( s, t );
assert(e != UINT_MAX); if(e == UINT_MAX)
const EdgeData ed = _graph->GetEdgeData(e); e = _graph->FindEdge( t, s );
return ed.distance; if(UINT_MAX == e) {
} // INFO("edge not found for start " << s << ", target " << t)
return 0;
}
assert(e != UINT_MAX);
const EdgeData ed = _graph->GetEdgeData(e);
return ed.middleName.nameID;
}
inline std::string &GetUnescapedNameForNameID(const NodeID nameID) const { inline NodeID GetWeightForOriginDestinationNodeID(NodeID s, NodeID t) const {
return (nameID >= _names->size() ? _names->at(0) : _names->at(nameID) ); assert(s!=t);
} EdgeID e = _graph->FindEdge( s, t );
if(e == UINT_MAX)
e = _graph->FindEdge( t, s );
assert(e != UINT_MAX);
const EdgeData ed = _graph->GetEdgeData(e);
return ed.distance;
}
inline std::string GetEscapedNameForOriginDestinationNodeID(NodeID s, NodeID t) const { inline std::string &GetUnescapedNameForNameID(const NodeID nameID) const {
NodeID nameID = GetNameIDForOriginDestinationNodeID(s, t); return (nameID >= _names->size() ? _names->at(0) : _names->at(nameID) );
return ( GetEscapedNameForNameID(nameID) ); }
}
inline std::string GetEscapedNameForNameID(const NodeID nameID) const { inline std::string GetEscapedNameForOriginDestinationNodeID(NodeID s, NodeID t) const {
return ( (nameID >= _names->size() || nameID == 0) ? std::string("") : HTMLEntitize(_names->at(nameID)) ); NodeID nameID = GetNameIDForOriginDestinationNodeID(s, t);
} return ( GetEscapedNameForNameID(nameID) );
}
inline short GetTypeOfEdgeForOriginDestinationNodeID(NodeID s, NodeID t) const { inline std::string GetEscapedNameForNameID(const NodeID nameID) const {
assert(s!=t); return ( (nameID >= _names->size() || nameID == 0) ? std::string("") : HTMLEntitize(_names->at(nameID)) );
EdgeID e = _graph->FindEdge( s, t ); }
if(e == UINT_MAX)
e = _graph->FindEdge( t, s );
assert(e != UINT_MAX);
const EdgeData ed = _graph->GetEdgeData(e);
return ed.type;
}
// inline void RegisterThread(const unsigned k, const unsigned v) { inline short GetTypeOfEdgeForOriginDestinationNodeID(NodeID s, NodeID t) const {
// nodeHelpDesk->RegisterThread(k,v); assert(s!=t);
// } EdgeID e = _graph->FindEdge( s, t );
if(e == UINT_MAX)
e = _graph->FindEdge( t, s );
assert(e != UINT_MAX);
const EdgeData ed = _graph->GetEdgeData(e);
return ed.type;
}
// inline void RegisterThread(const unsigned k, const unsigned v) {
// nodeHelpDesk->RegisterThread(k,v);
// }
private: private:
inline void _RoutingStep(_Heap& _forwardHeap, _Heap &_backwardHeap, const bool& forwardDirection, NodeID * middle, unsigned int * _upperbound) { inline void _RoutingStep(_Heap& _forwardHeap, _Heap &_backwardHeap, const bool& forwardDirection, NodeID * middle, unsigned int * _upperbound) {
const NodeID node = _forwardHeap.DeleteMin(); const NodeID node = _forwardHeap.DeleteMin();
const unsigned int distance = _forwardHeap.GetKey( node ); const unsigned int distance = _forwardHeap.GetKey( node );
if ( _backwardHeap.WasInserted( node ) ) { if ( _backwardHeap.WasInserted( node ) ) {
const unsigned int newDistance = _backwardHeap.GetKey( node ) + distance; const unsigned int newDistance = _backwardHeap.GetKey( node ) + distance;
if ( newDistance < *_upperbound ) { if ( newDistance < *_upperbound ) {
*middle = node; *middle = node;
*_upperbound = newDistance; *_upperbound = newDistance;
} }
} }
if ( distance > *_upperbound ) { if ( distance > *_upperbound ) {
_forwardHeap.DeleteAll(); _forwardHeap.DeleteAll();
return; return;
} }
for ( typename GraphT::EdgeIterator edge = _graph->BeginEdges( node ); edge < _graph->EndEdges(node); edge++ ) { for ( typename GraphT::EdgeIterator edge = _graph->BeginEdges( node ); edge < _graph->EndEdges(node); edge++ ) {
const NodeID to = _graph->GetTarget(edge); const NodeID to = _graph->GetTarget(edge);
const EdgeWeight edgeWeight = _graph->GetEdgeData(edge).distance; const EdgeWeight edgeWeight = _graph->GetEdgeData(edge).distance;
assert( edgeWeight > 0 ); assert( edgeWeight > 0 );
//Stalling //Stalling
bool backwardDirectionFlag = (!forwardDirection) ? _graph->GetEdgeData(edge).forward : _graph->GetEdgeData(edge).backward; bool backwardDirectionFlag = (!forwardDirection) ? _graph->GetEdgeData(edge).forward : _graph->GetEdgeData(edge).backward;
if(_forwardHeap.WasInserted( to )) { if(_forwardHeap.WasInserted( to )) {
if(backwardDirectionFlag) { if(backwardDirectionFlag) {
if(_forwardHeap.GetKey( to ) + edgeWeight < distance) { if(_forwardHeap.GetKey( to ) + edgeWeight < distance) {
return; return;
} }
} }
} }
} }
for ( typename GraphT::EdgeIterator edge = _graph->BeginEdges( node ); edge < _graph->EndEdges(node); edge++ ) { for ( typename GraphT::EdgeIterator edge = _graph->BeginEdges( node ); edge < _graph->EndEdges(node); edge++ ) {
const NodeID to = _graph->GetTarget(edge); const NodeID to = _graph->GetTarget(edge);
const EdgeWeight edgeWeight = _graph->GetEdgeData(edge).distance; const EdgeWeight edgeWeight = _graph->GetEdgeData(edge).distance;
assert( edgeWeight > 0 ); assert( edgeWeight > 0 );
const int toDistance = distance + edgeWeight; const int toDistance = distance + edgeWeight;
bool forwardDirectionFlag = (forwardDirection ? _graph->GetEdgeData(edge).forward : _graph->GetEdgeData(edge).backward ); bool forwardDirectionFlag = (forwardDirection ? _graph->GetEdgeData(edge).forward : _graph->GetEdgeData(edge).backward );
if(forwardDirectionFlag) { if(forwardDirectionFlag) {
//New Node discovered -> Add to Heap + Node Info Storage //New Node discovered -> Add to Heap + Node Info Storage
if ( !_forwardHeap.WasInserted( to ) ) { if ( !_forwardHeap.WasInserted( to ) ) {
_forwardHeap.Insert( to, toDistance, node ); _forwardHeap.Insert( to, toDistance, node );
} }
//Found a shorter Path -> Update distance //Found a shorter Path -> Update distance
else if ( toDistance < _forwardHeap.GetKey( to ) ) { else if ( toDistance < _forwardHeap.GetKey( to ) ) {
_forwardHeap.GetData( to ).parent = node; _forwardHeap.GetData( to ).parent = node;
_forwardHeap.DecreaseKey( to, toDistance ); _forwardHeap.DecreaseKey( to, toDistance );
//new parent //new parent
} }
} }
} }
} }
inline void _RoutingStepWithStats( _Heap& _forwardHeap, _Heap &_backwardHeap, const bool& forwardDirection, NodeID * middle, unsigned int * _upperbound, _Statistics& stats) { inline void _RoutingStepWithStats( _Heap& _forwardHeap, _Heap &_backwardHeap, const bool& forwardDirection, NodeID * middle, unsigned int * _upperbound, _Statistics& stats) {
const NodeID node = _forwardHeap.DeleteMin(); const NodeID node = _forwardHeap.DeleteMin();
stats.deleteMins++; stats.deleteMins++;
const unsigned int distance = _forwardHeap.GetKey( node ); const unsigned int distance = _forwardHeap.GetKey( node );
if ( _backwardHeap.WasInserted( node ) ) { if ( _backwardHeap.WasInserted( node ) ) {
const unsigned int newDistance = _backwardHeap.GetKey( node ) + distance; const unsigned int newDistance = _backwardHeap.GetKey( node ) + distance;
if ( newDistance < *_upperbound ) { if ( newDistance < *_upperbound ) {
*middle = node; *middle = node;
*_upperbound = newDistance; *_upperbound = newDistance;
} }
} }
if ( distance > *_upperbound ) { if ( distance > *_upperbound ) {
stats.meetingNodes++; stats.meetingNodes++;
_forwardHeap.DeleteAll(); _forwardHeap.DeleteAll();
return; return;
} }
for ( typename GraphT::EdgeIterator edge = _graph->BeginEdges( node ); edge < _graph->EndEdges(node); edge++ ) { for ( typename GraphT::EdgeIterator edge = _graph->BeginEdges( node ); edge < _graph->EndEdges(node); edge++ ) {
const EdgeData& ed = _graph->GetEdgeData(edge); const EdgeData& ed = _graph->GetEdgeData(edge);
const NodeID to = _graph->GetTarget(edge); const NodeID to = _graph->GetTarget(edge);
const EdgeWeight edgeWeight = ed.distance; const EdgeWeight edgeWeight = ed.distance;
assert( edgeWeight > 0 ); assert( edgeWeight > 0 );
const int toDistance = distance + edgeWeight; const int toDistance = distance + edgeWeight;
//Stalling //Stalling
if(_forwardHeap.WasInserted( to )) { if(_forwardHeap.WasInserted( to )) {
if(!forwardDirection ? ed.forward : ed.backward) { if(!forwardDirection ? ed.forward : ed.backward) {
if(_forwardHeap.GetKey( to ) + edgeWeight < distance) { if(_forwardHeap.GetKey( to ) + edgeWeight < distance) {
stats.stalledNodes++; stats.stalledNodes++;
return; return;
} }
} }
} }
if(forwardDirection ? ed.forward : ed.backward ) { if(forwardDirection ? ed.forward : ed.backward ) {
//New Node discovered -> Add to Heap + Node Info Storage //New Node discovered -> Add to Heap + Node Info Storage
if ( !_forwardHeap.WasInserted( to ) ) { if ( !_forwardHeap.WasInserted( to ) ) {
_forwardHeap.Insert( to, toDistance, node ); _forwardHeap.Insert( to, toDistance, node );
stats.insertedNodes++; stats.insertedNodes++;
} }
//Found a shorter Path -> Update distance //Found a shorter Path -> Update distance
else if ( toDistance < _forwardHeap.GetKey( to ) ) { else if ( toDistance < _forwardHeap.GetKey( to ) ) {
_forwardHeap.GetData( to ).parent = node; _forwardHeap.GetData( to ).parent = node;
_forwardHeap.DecreaseKey( to, toDistance ); _forwardHeap.DecreaseKey( to, toDistance );
stats.decreasedNodes++; stats.decreasedNodes++;
//new parent //new parent
} }
} }
} }
} }
inline bool _UnpackEdge( const NodeID source, const NodeID target, std::vector< _PathData >& path ) { inline bool _UnpackEdge( const NodeID source, const NodeID target, std::vector< _PathData >& path ) {
assert(source != target); assert(source != target);
//find edge first. //find edge first.
bool forward = true; bool forward = true;
typename GraphT::EdgeIterator smallestEdge = SPECIAL_EDGEID; typename GraphT::EdgeIterator smallestEdge = SPECIAL_EDGEID;
EdgeWeight smallestWeight = UINT_MAX; EdgeWeight smallestWeight = UINT_MAX;
for(typename GraphT::EdgeIterator eit = _graph->BeginEdges(source); eit < _graph->EndEdges(source); eit++) { for(typename GraphT::EdgeIterator eit = _graph->BeginEdges(source); eit < _graph->EndEdges(source); eit++) {
const EdgeWeight weight = _graph->GetEdgeData(eit).distance; const EdgeWeight weight = _graph->GetEdgeData(eit).distance;
if(_graph->GetTarget(eit) == target && weight < smallestWeight && _graph->GetEdgeData(eit).forward) { if(_graph->GetTarget(eit) == target && weight < smallestWeight && _graph->GetEdgeData(eit).forward) {
smallestEdge = eit; smallestWeight = weight; smallestEdge = eit; smallestWeight = weight;
} }
} }
if(smallestEdge == SPECIAL_EDGEID) { if(smallestEdge == SPECIAL_EDGEID) {
for(typename GraphT::EdgeIterator eit = _graph->BeginEdges(target); eit < _graph->EndEdges(target); eit++) { for(typename GraphT::EdgeIterator eit = _graph->BeginEdges(target); eit < _graph->EndEdges(target); eit++) {
const EdgeWeight weight = _graph->GetEdgeData(eit).distance; const EdgeWeight weight = _graph->GetEdgeData(eit).distance;
if(_graph->GetTarget(eit) == source && weight < smallestWeight && _graph->GetEdgeData(eit).backward) { if(_graph->GetTarget(eit) == source && weight < smallestWeight && _graph->GetEdgeData(eit).backward) {
smallestEdge = eit; smallestWeight = weight; smallestEdge = eit; smallestWeight = weight;
forward = false; forward = false;
} }
} }
} }
assert(smallestWeight != SPECIAL_EDGEID); //no edge found. This should not happen at all! assert(smallestWeight != SPECIAL_EDGEID); //no edge found. This should not happen at all!
const EdgeData& ed = _graph->GetEdgeData(smallestEdge); const EdgeData& ed = _graph->GetEdgeData(smallestEdge);
if(ed.shortcut) {//unpack if(ed.shortcut) {//unpack
const NodeID middle = ed.middleName.middle; const NodeID middle = ed.middleName.middle;
_UnpackEdge(source, middle, path); _UnpackEdge(source, middle, path);
_UnpackEdge(middle, target, path); _UnpackEdge(middle, target, path);
return false; return false;
} else { } else {
assert(!ed.shortcut); assert(!ed.shortcut);
path.push_back(_PathData(target) ); path.push_back(_PathData(target) );
return true; return true;
} }
} }
}; };
#endif /* SEARCHENGINE_H_ */ #endif /* SEARCHENGINE_H_ */

View File

@ -140,7 +140,7 @@ public:
double area = fabs(0.5*( descriptorState.startOfSegmentCoordinate.lon*(descriptorState.nextCoordinate.lat - descriptorState.currentCoordinate.lat) + descriptorState.nextCoordinate.lon*(descriptorState.currentCoordinate.lat - descriptorState.startOfSegmentCoordinate.lat) + descriptorState.currentCoordinate.lon*(descriptorState.startOfSegmentCoordinate.lat - descriptorState.nextCoordinate.lat) ) ); double area = fabs(0.5*( descriptorState.startOfSegmentCoordinate.lon*(descriptorState.nextCoordinate.lat - descriptorState.currentCoordinate.lat) + descriptorState.nextCoordinate.lon*(descriptorState.currentCoordinate.lat - descriptorState.startOfSegmentCoordinate.lat) + descriptorState.currentCoordinate.lon*(descriptorState.startOfSegmentCoordinate.lat - descriptorState.nextCoordinate.lat) ) );
//if route is generalization does not skip this point, add it to description //if route is generalization does not skip this point, add it to description
if( it==path.end()-1 || config.z == 19 || area >= areaThresholds[config.z] || (false == descriptorState.CurrentAndPreviousNameIDsEqual()) ) { if( config.z == 19 || area >= areaThresholds[config.z] || (false == descriptorState.CurrentAndPreviousNameIDsEqual()) ) {
//mark the beginning of the segment thats announced //mark the beginning of the segment thats announced
// appendCoordinateToString(descriptorState.currentCoordinate, descriptorState.routeGeometryString); // appendCoordinateToString(descriptorState.currentCoordinate, descriptorState.routeGeometryString);
polyline.push_back(descriptorState.currentCoordinate); polyline.push_back(descriptorState.currentCoordinate);

View File

@ -50,22 +50,7 @@ private:
HashTable<std::string, unsigned> descriptorTable; HashTable<std::string, unsigned> descriptorTable;
std::string pluginDescriptorString; std::string pluginDescriptorString;
struct _ThreadData { SearchEngine<EdgeData, StaticGraph<EdgeData> > * searchEngine;
SearchEngine<EdgeData, StaticGraph<EdgeData> > * sEngine;
std::vector< _PathData > * path;
unsigned distanceOfSegment;
PhantomNodes phantomNodesOfSegment;
_ThreadData(SearchEngine<EdgeData, StaticGraph<EdgeData> > * s) : sEngine(s), distanceOfSegment(0) {
path = new std::vector< _PathData >();
}
~_ThreadData() {
DELETE( path );
DELETE( sEngine );
}
};
std::vector<_ThreadData *> threadData;
public: public:
ViaRoutePlugin(ObjectsForQueryStruct * objects, std::string psd = "viaroute") : pluginDescriptorString(psd) { ViaRoutePlugin(ObjectsForQueryStruct * objects, std::string psd = "viaroute") : pluginDescriptorString(psd) {
@ -73,10 +58,7 @@ public:
graph = objects->graph; graph = objects->graph;
names = objects->names; names = objects->names;
unsigned maxThreads = omp_get_max_threads(); searchEngine = new SearchEngine<EdgeData, StaticGraph<EdgeData> >(graph, nodeHelpDesk, names);
for ( unsigned threadNum = 0; threadNum < maxThreads; ++threadNum ) {
threadData.push_back( new _ThreadData( new SearchEngine<EdgeData, StaticGraph<EdgeData> >(graph, nodeHelpDesk, names)) );
}
descriptorTable.Set("", 0); //default descriptor descriptorTable.Set("", 0); //default descriptor
descriptorTable.Set("kml", 0); descriptorTable.Set("kml", 0);
@ -85,9 +67,7 @@ public:
} }
virtual ~ViaRoutePlugin() { virtual ~ViaRoutePlugin() {
for ( unsigned threadNum = 0; threadNum < threadData.size(); threadNum++ ) { DELETE( searchEngine );
DELETE( threadData[threadNum] );
}
} }
std::string GetDescriptor() { return pluginDescriptorString; } std::string GetDescriptor() { return pluginDescriptorString; }
@ -144,25 +124,22 @@ public:
vector<PhantomNode> phantomNodeVector(rawRoute.rawViaNodeCoordinates.size()); vector<PhantomNode> phantomNodeVector(rawRoute.rawViaNodeCoordinates.size());
bool errorOccurredFlag = false; bool errorOccurredFlag = false;
#pragma omp parallel for
for(unsigned i = 0; i < rawRoute.rawViaNodeCoordinates.size(); i++) { for(unsigned i = 0; i < rawRoute.rawViaNodeCoordinates.size(); i++) {
threadData[omp_get_thread_num()]->sEngine->FindPhantomNodeForCoordinate( rawRoute.rawViaNodeCoordinates[i], phantomNodeVector[i]); searchEngine->FindPhantomNodeForCoordinate( rawRoute.rawViaNodeCoordinates[i], phantomNodeVector[i]);
if(!rawRoute.rawViaNodeCoordinates[i].isSet()) { if(!rawRoute.rawViaNodeCoordinates[i].isSet()) {
errorOccurredFlag = true; errorOccurredFlag = true;
} }
} }
rawRoute.Resize(); rawRoute.Resize();
unsigned distance = 0; unsigned distance = 0;
//#pragma omp parallel for reduction(+:distance)
for(unsigned i = 0; i < phantomNodeVector.size()-1 && !errorOccurredFlag; i++) { for(unsigned i = 0; i < phantomNodeVector.size()-1 && !errorOccurredFlag; i++) {
PhantomNodes & segmentPhantomNodes = threadData[omp_get_thread_num()]->phantomNodesOfSegment; PhantomNodes segmentPhantomNodes;
segmentPhantomNodes.startPhantom = phantomNodeVector[i]; segmentPhantomNodes.startPhantom = phantomNodeVector[i];
segmentPhantomNodes.targetPhantom = phantomNodeVector[i+1]; segmentPhantomNodes.targetPhantom = phantomNodeVector[i+1];
std::vector< _PathData > path; std::vector< _PathData > path;
unsigned distanceOfSegment = threadData[omp_get_thread_num()]->sEngine->ComputeRoute(segmentPhantomNodes, path); unsigned distanceOfSegment = searchEngine->ComputeRoute(segmentPhantomNodes, path);
if(UINT_MAX == distanceOfSegment ) { if(UINT_MAX == distanceOfSegment ) {
errorOccurredFlag = true; errorOccurredFlag = true;
@ -229,7 +206,9 @@ public:
phantomNodes.startPhantom = rawRoute.segmentEndCoordinates[0].startPhantom; phantomNodes.startPhantom = rawRoute.segmentEndCoordinates[0].startPhantom;
phantomNodes.targetPhantom = rawRoute.segmentEndCoordinates[rawRoute.segmentEndCoordinates.size()-1].targetPhantom; phantomNodes.targetPhantom = rawRoute.segmentEndCoordinates[rawRoute.segmentEndCoordinates.size()-1].targetPhantom;
desc->SetConfig(descriptorConfig); desc->SetConfig(descriptorConfig);
desc->Run(reply, rawRoute, phantomNodes, *threadData[0]->sEngine, distance);
desc->Run(reply, rawRoute, phantomNodes, *searchEngine, distance);
if("" != JSONParameter) { if("" != JSONParameter) {
reply.content += ")\n"; reply.content += ")\n";
} }

View File

@ -46,7 +46,6 @@ public:
BasePlugin * tempPointer = _pluginVector[i]; BasePlugin * tempPointer = _pluginVector[i];
DELETE( tempPointer ); DELETE( tempPointer );
} }
} }
void handle_request(const Request& req, Reply& rep){ void handle_request(const Request& req, Reply& rep){