forked from knoepfle/matrix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreateHierarchy.cpp
347 lines (301 loc) · 13.9 KB
/
createHierarchy.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
/*
open source routing machine
Copyright (C) Dennis Luxen, 2010
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU AFFERO General Public License as published by
the Free Software Foundation; either version 3 of the License, or
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 Affero General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
or see http://www.gnu.org/licenses/agpl.txt.
*/
#include "Algorithms/IteratorBasedCRC32.h"
#include "Contractor/Contractor.h"
#include "Contractor/EdgeBasedGraphFactory.h"
#include "DataStructures/BinaryHeap.h"
#include "DataStructures/DeallocatingVector.h"
#include "DataStructures/QueryEdge.h"
#include "DataStructures/StaticGraph.h"
#include "DataStructures/StaticRTree.h"
#include "Util/IniFile.h"
#include "Util/GraphLoader.h"
#include "Util/InputFileUtil.h"
#include "Util/LuaUtil.h"
#include "Util/OpenMPWrapper.h"
#include "Util/OSRMException.h"
#include "Util/SimpleLogger.h"
#include "Util/StringUtil.h"
#include "typedefs.h"
#include <boost/foreach.hpp>
#include <luabind/luabind.hpp>
#include <fstream>
#include <istream>
#include <iostream>
#include <cstring>
#include <string>
#include <vector>
typedef QueryEdge::EdgeData EdgeData;
typedef DynamicGraph<EdgeData>::InputEdge InputEdge;
typedef StaticGraph<EdgeData>::InputEdge StaticEdge;
typedef IniFile ContractorConfiguration;
std::vector<NodeInfo> internalToExternalNodeMapping;
std::vector<TurnRestriction> inputRestrictions;
std::vector<NodeID> bollardNodes;
std::vector<NodeID> trafficLightNodes;
std::vector<ImportEdge> edgeList;
int main (int argc, char *argv[]) {
try {
LogPolicy::GetInstance().Unmute();
if(argc < 3) {
SimpleLogger().Write(logWARNING) <<
"usage: \n" <<
argv[0] << " <osrm-data> <osrm-restrictions> [<profile>]";
return -1;
}
double startupTime = get_timestamp();
unsigned number_of_threads = omp_get_num_procs();
if(testDataFile("contractor.ini")) {
ContractorConfiguration contractorConfig("contractor.ini");
unsigned rawNumber = stringToInt(contractorConfig.GetParameter("Threads"));
if(rawNumber != 0 && rawNumber <= number_of_threads)
number_of_threads = rawNumber;
}
omp_set_num_threads(number_of_threads);
LogPolicy::GetInstance().Unmute();
SimpleLogger().Write() << "Using restrictions from file: " << argv[2];
std::ifstream restrictionsInstream(argv[2], std::ios::binary);
if(!restrictionsInstream.good()) {
std::cerr <<
"Could not access <osrm-restrictions> files" << std::endl;
}
TurnRestriction restriction;
UUID uuid_loaded, uuid_orig;
unsigned usableRestrictionsCounter(0);
restrictionsInstream.read((char*)&uuid_loaded, sizeof(UUID));
if( !uuid_loaded.TestPrepare(uuid_orig) ) {
SimpleLogger().Write(logWARNING) <<
".restrictions was prepared with different build.\n"
"Reprocess to get rid of this warning.";
}
restrictionsInstream.read(
(char*)&usableRestrictionsCounter,
sizeof(unsigned)
);
inputRestrictions.resize(usableRestrictionsCounter);
restrictionsInstream.read(
(char *)&(inputRestrictions[0]),
usableRestrictionsCounter*sizeof(TurnRestriction)
);
restrictionsInstream.close();
std::ifstream in;
in.open (argv[1], std::ifstream::in | std::ifstream::binary);
if (!in.is_open()) {
throw OSRMException("Cannot open osrm input file");
}
std::string nodeOut(argv[1]); nodeOut += ".nodes";
std::string edgeOut(argv[1]); edgeOut += ".edges";
std::string graphOut(argv[1]); graphOut += ".hsgr";
std::string rtree_nodes_path(argv[1]); rtree_nodes_path += ".ramIndex";
std::string rtree_leafs_path(argv[1]); rtree_leafs_path += ".fileIndex";
/*** Setup Scripting Environment ***/
if(!testDataFile( (argc > 3 ? argv[3] : "profile.lua") )) {
throw OSRMException("Cannot open profile.lua ");
}
// Create a new lua state
lua_State *myLuaState = luaL_newstate();
// Connect LuaBind to this lua state
luabind::open(myLuaState);
//open utility libraries string library;
luaL_openlibs(myLuaState);
//adjust lua load path
luaAddScriptFolderToLoadPath( myLuaState, (argc > 3 ? argv[3] : "profile.lua") );
// Now call our function in a lua script
SimpleLogger().Write() <<
"Parsing speedprofile from " <<
(argc > 3 ? argv[3] : "profile.lua");
if(0 != luaL_dofile(myLuaState, (argc > 3 ? argv[3] : "profile.lua") )) {
std::cerr <<
lua_tostring(myLuaState,-1) <<
" occured in scripting block" <<
std::endl;
}
EdgeBasedGraphFactory::SpeedProfileProperties speedProfile;
if(0 != luaL_dostring( myLuaState, "return traffic_signal_penalty\n")) {
std::cerr <<
lua_tostring(myLuaState,-1) <<
" occured in scripting block" <<
std::endl;
return -1;
}
speedProfile.trafficSignalPenalty = 10*lua_tointeger(myLuaState, -1);
if(0 != luaL_dostring( myLuaState, "return u_turn_penalty\n")) {
std::cerr <<
lua_tostring(myLuaState,-1) <<
" occured in scripting block" <<
std::endl;
return -1;
}
speedProfile.uTurnPenalty = 10*lua_tointeger(myLuaState, -1);
speedProfile.has_turn_penalty_function = lua_function_exists( myLuaState, "turn_function" );
std::vector<ImportEdge> edgeList;
NodeID nodeBasedNodeNumber = readBinaryOSRMGraphFromStream(in, edgeList, bollardNodes, trafficLightNodes, &internalToExternalNodeMapping, inputRestrictions);
in.close();
SimpleLogger().Write() <<
inputRestrictions.size() <<
" restrictions, " <<
bollardNodes.size() <<
" bollard nodes, " <<
trafficLightNodes.size() <<
" traffic lights";
if(0 == edgeList.size()) {
std::cerr <<
"The input data is broken. "
"It is impossible to do any turns in this graph" <<
std::endl;
return -1;
}
/***
* Building an edge-expanded graph from node-based input an turn restrictions
*/
SimpleLogger().Write() << "Generating edge-expanded graph representation";
EdgeBasedGraphFactory * edgeBasedGraphFactory = new EdgeBasedGraphFactory (nodeBasedNodeNumber, edgeList, bollardNodes, trafficLightNodes, inputRestrictions, internalToExternalNodeMapping, speedProfile);
std::vector<ImportEdge>().swap(edgeList);
edgeBasedGraphFactory->Run(edgeOut.c_str(), myLuaState);
std::vector<TurnRestriction>().swap(inputRestrictions);
std::vector<NodeID>().swap(bollardNodes);
std::vector<NodeID>().swap(trafficLightNodes);
NodeID edgeBasedNodeNumber = edgeBasedGraphFactory->GetNumberOfNodes();
DeallocatingVector<EdgeBasedEdge> edgeBasedEdgeList;
edgeBasedGraphFactory->GetEdgeBasedEdges(edgeBasedEdgeList);
std::vector<EdgeBasedGraphFactory::EdgeBasedNode> nodeBasedEdgeList;
edgeBasedGraphFactory->GetEdgeBasedNodes(nodeBasedEdgeList);
delete edgeBasedGraphFactory;
/***
* Writing info on original (node-based) nodes
*/
SimpleLogger().Write() << "writing node map ...";
std::ofstream mapOutFile(nodeOut.c_str(), std::ios::binary);
mapOutFile.write((char *)&(internalToExternalNodeMapping[0]), internalToExternalNodeMapping.size()*sizeof(NodeInfo));
mapOutFile.close();
std::vector<NodeInfo>().swap(internalToExternalNodeMapping);
double expansionHasFinishedTime = get_timestamp() - startupTime;
/***
* Building grid-like nearest-neighbor data structure
*/
SimpleLogger().Write() << "building r-tree ...";
StaticRTree<EdgeBasedGraphFactory::EdgeBasedNode> * rtree =
new StaticRTree<EdgeBasedGraphFactory::EdgeBasedNode>(
nodeBasedEdgeList,
rtree_nodes_path.c_str(),
rtree_leafs_path.c_str()
);
delete rtree;
IteratorbasedCRC32<std::vector<EdgeBasedGraphFactory::EdgeBasedNode> > crc32;
unsigned crc32OfNodeBasedEdgeList = crc32(nodeBasedEdgeList.begin(), nodeBasedEdgeList.end() );
nodeBasedEdgeList.clear();
SimpleLogger().Write() << "CRC32: " << crc32OfNodeBasedEdgeList;
/***
* Contracting the edge-expanded graph
*/
SimpleLogger().Write() << "initializing contractor";
Contractor* contractor = new Contractor( edgeBasedNodeNumber, edgeBasedEdgeList );
double contractionStartedTimestamp(get_timestamp());
contractor->Run();
const double contraction_duration = (get_timestamp() - contractionStartedTimestamp);
SimpleLogger().Write() <<
"Contraction took " <<
contraction_duration <<
" sec";
DeallocatingVector< QueryEdge > contractedEdgeList;
contractor->GetEdges( contractedEdgeList );
delete contractor;
/***
* Sorting contracted edges in a way that the static query graph can read some in in-place.
*/
SimpleLogger().Write() << "Building Node Array";
std::sort(contractedEdgeList.begin(), contractedEdgeList.end());
unsigned numberOfNodes = 0;
unsigned numberOfEdges = contractedEdgeList.size();
SimpleLogger().Write() <<
"Serializing compacted graph of " <<
numberOfEdges <<
" edges";
std::ofstream hsgr_output_stream(graphOut.c_str(), std::ios::binary);
hsgr_output_stream.write((char*)&uuid_orig, sizeof(UUID) );
BOOST_FOREACH(const QueryEdge & edge, contractedEdgeList) {
if(edge.source > numberOfNodes) {
numberOfNodes = edge.source;
}
if(edge.target > numberOfNodes) {
numberOfNodes = edge.target;
}
}
numberOfNodes+=1;
std::vector< StaticGraph<EdgeData>::_StrNode > _nodes;
_nodes.resize( numberOfNodes + 1 );
StaticGraph<EdgeData>::EdgeIterator edge = 0;
StaticGraph<EdgeData>::EdgeIterator position = 0;
for ( StaticGraph<EdgeData>::NodeIterator node = 0; node <= numberOfNodes; ++node ) {
StaticGraph<EdgeData>::EdgeIterator lastEdge = edge;
while ( edge < numberOfEdges && contractedEdgeList[edge].source == node )
++edge;
_nodes[node].firstEdge = position; //=edge
position += edge - lastEdge; //remove
}
++numberOfNodes;
//Serialize numberOfNodes, nodes
hsgr_output_stream.write((char*) &crc32OfNodeBasedEdgeList, sizeof(unsigned));
hsgr_output_stream.write((char*) &numberOfNodes, sizeof(unsigned));
hsgr_output_stream.write((char*) &_nodes[0], sizeof(StaticGraph<EdgeData>::_StrNode)*(numberOfNodes));
//Serialize number of Edges
hsgr_output_stream.write((char*) &position, sizeof(unsigned));
--numberOfNodes;
edge = 0;
int usedEdgeCounter = 0;
StaticGraph<EdgeData>::_StrEdge currentEdge;
for ( StaticGraph<EdgeData>::NodeIterator node = 0; node < numberOfNodes; ++node ) {
for ( StaticGraph<EdgeData>::EdgeIterator i = _nodes[node].firstEdge, e = _nodes[node+1].firstEdge; i != e; ++i ) {
assert(node != contractedEdgeList[edge].target);
currentEdge.target = contractedEdgeList[edge].target;
currentEdge.data = contractedEdgeList[edge].data;
if(currentEdge.data.distance <= 0) {
SimpleLogger().Write(logWARNING) <<
"Edge: " << i <<
",source: " << contractedEdgeList[edge].source <<
", target: " << contractedEdgeList[edge].target <<
", dist: " << currentEdge.data.distance;
SimpleLogger().Write(logWARNING) <<
"Failed at edges of node " << node <<
" of " << numberOfNodes;
return -1;
}
//Serialize edges
hsgr_output_stream.write((char*) ¤tEdge, sizeof(StaticGraph<EdgeData>::_StrEdge));
++edge;
++usedEdgeCounter;
}
}
SimpleLogger().Write() << "Preprocessing : " <<
(get_timestamp() - startupTime) << " seconds";
SimpleLogger().Write() << "Expansion : " <<
(nodeBasedNodeNumber/expansionHasFinishedTime) << " nodes/sec and " <<
(edgeBasedNodeNumber/expansionHasFinishedTime) << " edges/sec";
SimpleLogger().Write() << "Contraction: " <<
(edgeBasedNodeNumber/contraction_duration) << " nodes/sec and " <<
usedEdgeCounter/contraction_duration << " edges/sec";
hsgr_output_stream.close();
//cleanedEdgeList.clear();
_nodes.clear();
SimpleLogger().Write() << "finished preprocessing";
} catch ( const std::exception &e ) {
SimpleLogger().Write(logWARNING) <<
"Exception occured: " << e.what() << std::endl;
return -1;
}
return 0;
}