Line data Source code
1 : /****************************************************************************/
2 : // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3 : // Copyright (C) 2001-2024 German Aerospace Center (DLR) and others.
4 : // This program and the accompanying materials are made available under the
5 : // terms of the Eclipse Public License 2.0 which is available at
6 : // https://www.eclipse.org/legal/epl-2.0/
7 : // This Source Code may also be made available under the following Secondary
8 : // Licenses when the conditions for such availability set forth in the Eclipse
9 : // Public License 2.0 are satisfied: GNU General Public License, version 2
10 : // or later which is available at
11 : // https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
12 : // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
13 : /****************************************************************************/
14 : /// @file NLBuilder.cpp
15 : /// @author Daniel Krajzewicz
16 : /// @author Jakob Erdmann
17 : /// @author Michael Behrisch
18 : /// @date Mon, 9 Jul 2001
19 : ///
20 : // The main interface for loading a microsim
21 : /****************************************************************************/
22 : #include <config.h>
23 :
24 : #include <iostream>
25 : #include <vector>
26 : #include <string>
27 : #include <map>
28 :
29 : #include <utils/common/MsgHandler.h>
30 : #include <utils/common/StringTokenizer.h>
31 : #include <utils/common/SystemFrame.h>
32 : #include <utils/options/Option.h>
33 : #include <utils/options/OptionsCont.h>
34 : #include <utils/options/OptionsIO.h>
35 : #include <utils/common/StringUtils.h>
36 : #include <utils/common/FileHelpers.h>
37 : #include <utils/common/SysUtils.h>
38 : #include <utils/common/ToString.h>
39 : #include <utils/vehicle/SUMORouteLoaderControl.h>
40 : #include <utils/vehicle/SUMORouteLoader.h>
41 : #include <utils/xml/XMLSubSys.h>
42 : #ifdef HAVE_FOX
43 : #include <utils/foxtools/MsgHandlerSynchronized.h>
44 : #endif
45 : #include <libsumo/Helper.h>
46 : #include <mesosim/MEVehicleControl.h>
47 : #include <microsim/MSVehicleControl.h>
48 : #include <microsim/MSVehicleTransfer.h>
49 : #include <microsim/MSNet.h>
50 : #include <microsim/devices/MSDevice.h>
51 : #include <microsim/devices/MSDevice_ToC.h>
52 : #include <microsim/devices/MSDevice_BTreceiver.h>
53 : #include <microsim/devices/MSDevice_FCDReplay.h>
54 : #include <microsim/MSEdgeControl.h>
55 : #include <microsim/MSGlobals.h>
56 : #include <microsim/output/MSDetectorControl.h>
57 : #include <microsim/MSFrame.h>
58 : #include <microsim/MSEdgeWeightsStorage.h>
59 : #include <microsim/MSStateHandler.h>
60 : #include <microsim/MSDriverState.h>
61 : #include <microsim/trigger/MSTriggeredRerouter.h>
62 : #include <traci-server/TraCIServer.h>
63 :
64 : #include "NLHandler.h"
65 : #include "NLNetShapeHandler.h"
66 : #include "NLEdgeControlBuilder.h"
67 : #include "NLJunctionControlBuilder.h"
68 : #include "NLDetectorBuilder.h"
69 : #include "NLTriggerBuilder.h"
70 : #include "NLBuilder.h"
71 :
72 :
73 : // ===========================================================================
74 : // method definitions
75 : // ===========================================================================
76 : // ---------------------------------------------------------------------------
77 : // NLBuilder::EdgeFloatTimeLineRetriever_EdgeWeight - methods
78 : // ---------------------------------------------------------------------------
79 : void
80 0 : NLBuilder::EdgeFloatTimeLineRetriever_EdgeEffort::addEdgeWeight(const std::string& id,
81 : double value, double begTime, double endTime) const {
82 0 : MSEdge* edge = MSEdge::dictionary(id);
83 0 : if (edge != nullptr) {
84 0 : myNet.getWeightsStorage().addEffort(edge, begTime, endTime, value);
85 : } else {
86 0 : WRITE_ERRORF(TL("Trying to set the effort for the unknown edge '%'."), id);
87 : }
88 0 : }
89 :
90 :
91 : // ---------------------------------------------------------------------------
92 : // NLBuilder::EdgeFloatTimeLineRetriever_EdgeTravelTime - methods
93 : // ---------------------------------------------------------------------------
94 : void
95 18 : NLBuilder::EdgeFloatTimeLineRetriever_EdgeTravelTime::addEdgeWeight(const std::string& id,
96 : double value, double begTime, double endTime) const {
97 18 : MSEdge* edge = MSEdge::dictionary(id);
98 18 : if (edge != nullptr) {
99 18 : myNet.getWeightsStorage().addTravelTime(edge, begTime, endTime, value);
100 : } else {
101 0 : WRITE_ERRORF(TL("Trying to set the travel time for the unknown edge '%'."), id);
102 : }
103 18 : }
104 :
105 :
106 : // ---------------------------------------------------------------------------
107 : // NLBuilder - methods
108 : // ---------------------------------------------------------------------------
109 43005 : NLBuilder::NLBuilder(OptionsCont& oc,
110 : MSNet& net,
111 : NLEdgeControlBuilder& eb,
112 : NLJunctionControlBuilder& jb,
113 : NLDetectorBuilder& db,
114 43005 : NLHandler& xmlHandler)
115 43005 : : myOptions(oc), myEdgeBuilder(eb), myJunctionBuilder(jb),
116 43005 : myDetectorBuilder(db),
117 43005 : myNet(net), myXMLHandler(xmlHandler) {}
118 :
119 :
120 43005 : NLBuilder::~NLBuilder() {}
121 :
122 :
123 : bool
124 43005 : NLBuilder::build() {
125 : // try to build the net
126 86010 : if (!load("net-file", true)) {
127 : return false;
128 : }
129 42626 : if (myXMLHandler.networkVersion() == MMVersion(0, 0)) {
130 0 : throw ProcessError(TL("Invalid network, no network version declared."));
131 : }
132 : // check whether the loaded net agrees with the simulation options
133 88037 : if ((myOptions.getBool("no-internal-links") || myOptions.getBool("mesosim")) && myXMLHandler.haveSeenInternalEdge() && myXMLHandler.haveSeenDefaultLength()) {
134 5588 : WRITE_WARNING(TL("Network contains internal links which are ignored. Vehicles will 'jump' across junctions and thus underestimate route lengths and travel times."));
135 : }
136 42626 : buildNet();
137 85128 : if (myOptions.isSet("alternative-net-file")) {
138 0 : for (std::string fname : myOptions.getStringVector("alternative-net-file")) {
139 0 : const long before = PROGRESS_BEGIN_TIME_MESSAGE("Loading alternative net from '" + fname + "'");
140 0 : NLNetShapeHandler nsh(fname, myNet);
141 0 : if (!XMLSubSys::runParser(nsh, fname, true)) {
142 0 : WRITE_MESSAGE("Loading of alternative net failed.");
143 : return false;
144 : }
145 0 : nsh.sortInternalShapes();
146 0 : PROGRESS_TIME_MESSAGE(before);
147 0 : }
148 : }
149 : // @note on loading order constraints:
150 : // - additional-files before route-files and state-files due to referencing
151 : // - additional-files before weight-files since the latter might contain intermodal edge data and the intermodal net depends on the stops and public transport from the additionals
152 :
153 : bool stateBeginMismatch = false;
154 85128 : if (myOptions.isSet("load-state")) {
155 : // first, load only the time
156 219 : const SUMOTime stateTime = MSStateHandler::MSStateTimeHandler::getTime(myOptions.getString("load-state"));
157 422 : if (myOptions.isDefault("begin")) {
158 298 : myOptions.set("begin", time2string(stateTime));
159 149 : if (TraCIServer::getInstance() != nullptr) {
160 18 : TraCIServer::getInstance()->stateLoaded(stateTime);
161 : }
162 : } else {
163 124 : if (stateTime != string2time(myOptions.getString("begin"))) {
164 45 : WRITE_WARNINGF(TL("State was written at a different time=% than the begin time %!"), time2string(stateTime), myOptions.getString("begin"));
165 : stateBeginMismatch = true;
166 : }
167 : }
168 : }
169 :
170 85112 : if (myOptions.getBool("junction-taz")) {
171 : // create a TAZ for every junction
172 7832 : const MSJunctionControl& junctions = myNet.getJunctionControl();
173 84984 : for (auto it = junctions.begin(); it != junctions.end(); it++) {
174 77152 : const std::string sinkID = it->first + "-sink";
175 77152 : const std::string sourceID = it->first + "-source";
176 77152 : if (MSEdge::dictionary(sinkID) == nullptr && MSEdge::dictionary(sourceID) == nullptr) {
177 : // sink must be built and added before source
178 154304 : MSEdge* sink = myEdgeBuilder.buildEdge(sinkID, SumoXMLEdgeFunc::CONNECTOR, "", "", -1, 0);
179 154304 : MSEdge* source = myEdgeBuilder.buildEdge(sourceID, SumoXMLEdgeFunc::CONNECTOR, "", "", -1, 0);
180 : sink->setOtherTazConnector(source);
181 : source->setOtherTazConnector(sink);
182 77152 : MSEdge::dictionary(sinkID, sink);
183 77152 : MSEdge::dictionary(sourceID, source);
184 77152 : sink->initialize(new std::vector<MSLane*>());
185 77152 : source->initialize(new std::vector<MSLane*>());
186 77152 : const MSJunction* junction = it->second;
187 377553 : for (const MSEdge* edge : junction->getIncoming()) {
188 300401 : if (!edge->isInternal()) {
189 172896 : const_cast<MSEdge*>(edge)->addSuccessor(sink);
190 : }
191 : }
192 377553 : for (const MSEdge* edge : junction->getOutgoing()) {
193 300401 : if (!edge->isInternal()) {
194 172896 : source->addSuccessor(const_cast<MSEdge*>(edge));
195 : }
196 : }
197 : } else {
198 0 : WRITE_WARNINGF(TL("A TAZ with id '%' already exists. Not building junction TAZ."), it->first)
199 : }
200 : }
201 : }
202 :
203 : // load additional net elements (sources, detectors, ...)
204 85112 : if (myOptions.isSet("additional-files")) {
205 58580 : if (!load("additional-files")) {
206 733 : return false;
207 : }
208 : // load shapes with separate handler
209 28557 : NLShapeHandler sh("", myNet.getShapeContainer());
210 57114 : if (!ShapeHandler::loadFiles(myOptions.getStringVector("additional-files"), sh)) {
211 : return false;
212 : }
213 28557 : if (myXMLHandler.haveSeenAdditionalSpeedRestrictions()) {
214 65 : myNet.getEdgeControl().setAdditionalRestrictions();
215 : }
216 28557 : if (MSGlobals::gUseMesoSim && myXMLHandler.haveSeenMesoEdgeType()) {
217 20 : myNet.getEdgeControl().setMesoTypes();
218 24 : for (MSTrafficLightLogic* tll : myNet.getTLSControl().getAllLogics()) {
219 4 : tll->initMesoTLSPenalties();
220 20 : }
221 : }
222 28557 : MSTriggeredRerouter::checkParkingRerouteConsistency();
223 : }
224 : // init tls after all detectors have been loaded
225 41823 : myJunctionBuilder.postLoadInitialization();
226 : // declare meandata set by options
227 83584 : buildDefaultMeanData("edgedata-output", "DEFAULT_EDGEDATA", false);
228 83584 : buildDefaultMeanData("lanedata-output", "DEFAULT_LANEDATA", true);
229 :
230 41792 : if (stateBeginMismatch && myNet.getVehicleControl().getLoadedVehicleNo() > 0) {
231 4 : throw ProcessError(TL("Loading vehicles ahead of a state file is not supported. Correct --begin option or load vehicles with option --route-files"));
232 : }
233 :
234 : // load weights if wished
235 83580 : if (myOptions.isSet("weight-files")) {
236 22 : if (!myOptions.isUsableFileList("weight-files")) {
237 0 : return false;
238 : }
239 : // build and prepare the weights handler
240 : std::vector<SAXWeightsHandler::ToRetrieveDefinition*> retrieverDefs;
241 : // travel time, first (always used)
242 11 : EdgeFloatTimeLineRetriever_EdgeTravelTime ttRetriever(myNet);
243 11 : retrieverDefs.push_back(new SAXWeightsHandler::ToRetrieveDefinition("traveltime", true, ttRetriever));
244 : // the measure to use, then
245 11 : EdgeFloatTimeLineRetriever_EdgeEffort eRetriever(myNet);
246 11 : std::string measure = myOptions.getString("weight-attribute");
247 22 : if (!myOptions.isDefault("weight-attribute")) {
248 0 : if (measure == "CO" || measure == "CO2" || measure == "HC" || measure == "PMx" || measure == "NOx" || measure == "fuel" || measure == "electricity") {
249 : measure += "_perVeh";
250 : }
251 0 : retrieverDefs.push_back(new SAXWeightsHandler::ToRetrieveDefinition(measure, true, eRetriever));
252 : }
253 : // set up handler
254 11 : SAXWeightsHandler handler(retrieverDefs, "");
255 : // start parsing; for each file in the list
256 22 : std::vector<std::string> files = myOptions.getStringVector("weight-files");
257 22 : for (std::vector<std::string>::iterator i = files.begin(); i != files.end(); ++i) {
258 : // report about loading when wished
259 33 : WRITE_MESSAGEF(TL("Loading weights from '%'..."), *i);
260 : // parse the file
261 11 : if (!XMLSubSys::runParser(handler, *i)) {
262 : return false;
263 : }
264 : }
265 22 : }
266 : // load the previous state if wished
267 83580 : if (myOptions.isSet("load-state")) {
268 209 : myNet.setCurrentTimeStep(string2time(myOptions.getString("begin")));
269 209 : const std::string& f = myOptions.getString("load-state");
270 627 : long before = PROGRESS_BEGIN_TIME_MESSAGE(TLF("Loading state from '%'", f));
271 209 : MSStateHandler h(f, string2time(myOptions.getString("load-state.offset")));
272 209 : XMLSubSys::runParser(h, f);
273 209 : if (MsgHandler::getErrorInstance()->wasInformed()) {
274 : return false;
275 : }
276 209 : PROGRESS_TIME_MESSAGE(before);
277 209 : }
278 : // routes from FCD files
279 41790 : MSDevice_FCDReplay::init();
280 : // load routes
281 145938 : if (myOptions.isSet("route-files") && string2time(myOptions.getString("route-steps")) <= 0) {
282 0 : if (!load("route-files")) {
283 : return false;
284 : }
285 : }
286 : // optionally switch off traffic lights
287 83580 : if (myOptions.getBool("tls.all-off")) {
288 91 : myNet.getTLSControl().switchOffAll();
289 : }
290 41790 : WRITE_MESSAGE(TL("Loading done."));
291 41790 : return true;
292 : }
293 :
294 :
295 : MSNet*
296 36098 : NLBuilder::init(const bool isLibsumo) {
297 36098 : OptionsCont& oc = OptionsCont::getOptions();
298 36098 : oc.clear();
299 36098 : MSFrame::fillOptions();
300 36098 : OptionsIO::getOptions();
301 36052 : if (oc.processMetaOptions(OptionsIO::getArgC() < 2)) {
302 251 : SystemFrame::close();
303 251 : return nullptr;
304 : }
305 35801 : SystemFrame::checkOptions(oc);
306 35801 : std::string validation = oc.getString("xml-validation");
307 39958 : std::string routeValidation = oc.getString("xml-validation.routes");
308 35801 : if (isLibsumo) {
309 1130 : if (oc.isDefault("xml-validation")) {
310 : validation = "never";
311 : }
312 1130 : if (oc.isDefault("xml-validation.routes")) {
313 : routeValidation = "never";
314 : }
315 : }
316 35801 : XMLSubSys::setValidation(validation, oc.getString("xml-validation.net"), routeValidation);
317 35801 : if (!MSFrame::checkOptions()) {
318 305 : throw ProcessError();
319 : }
320 : #ifdef HAVE_FOX
321 70976 : if (oc.getInt("threads") > 1) {
322 : // make the output aware of threading
323 : MsgHandler::setFactory(&MsgHandlerSynchronized::create);
324 : }
325 : #endif
326 35488 : MsgHandler::initOutputOptions();
327 35488 : initRandomness();
328 35488 : MSFrame::setMSGlobals(oc);
329 : MSVehicleControl* vc = nullptr;
330 35488 : if (MSGlobals::gUseMesoSim) {
331 2938 : vc = new MEVehicleControl();
332 : } else {
333 32550 : vc = new MSVehicleControl();
334 : }
335 35488 : MSNet* net = new MSNet(vc, new MSEventControl(), new MSEventControl(), new MSEventControl());
336 : // need to init TraCI-Server before loading routes to catch VehicleState::BUILT
337 39645 : TraCIServer::openSocket(std::map<int, TraCIServer::CmdExecutor>());
338 35482 : if (isLibsumo) {
339 565 : libsumo::Helper::registerStateListener();
340 : }
341 :
342 35482 : NLEdgeControlBuilder eb;
343 35482 : NLDetectorBuilder db(*net);
344 35482 : NLJunctionControlBuilder jb(*net, db);
345 35482 : NLTriggerBuilder tb;
346 35482 : NLHandler handler("", *net, db, tb, eb, jb);
347 35482 : tb.setHandler(&handler);
348 35482 : NLBuilder builder(oc, *net, eb, jb, db, handler);
349 35482 : MsgHandler::getErrorInstance()->clear();
350 35482 : MsgHandler::getWarningInstance()->clear();
351 35482 : MsgHandler::getMessageInstance()->clear();
352 35482 : if (builder.build()) {
353 : // preload the routes especially for TraCI
354 34516 : net->loadRoutes();
355 : return net;
356 : }
357 884 : delete net;
358 884 : throw ProcessError();
359 54672 : }
360 :
361 :
362 : void
363 43013 : NLBuilder::initRandomness() {
364 43013 : RandHelper::initRandGlobal();
365 43013 : RandHelper::initRandGlobal(MSRouteHandler::getParsingRNG());
366 43013 : RandHelper::initRandGlobal(MSDevice::getEquipmentRNG());
367 43013 : RandHelper::initRandGlobal(OUProcess::getRNG());
368 43013 : RandHelper::initRandGlobal(MSDevice_ToC::getResponseTimeRNG());
369 43013 : RandHelper::initRandGlobal(MSDevice_BTreceiver::getRecognitionRNG());
370 43013 : MSLane::initRNGs(OptionsCont::getOptions());
371 43013 : }
372 :
373 :
374 : void
375 42626 : NLBuilder::buildNet() {
376 : MSEdgeControl* edges = nullptr;
377 : MSJunctionControl* junctions = nullptr;
378 : SUMORouteLoaderControl* routeLoaders = nullptr;
379 : MSTLLogicControl* tlc = nullptr;
380 : std::vector<SUMOTime> stateDumpTimes;
381 : std::vector<std::string> stateDumpFiles;
382 : try {
383 42626 : MSFrame::buildStreams(); // ensure streams are ready for output during building
384 42584 : edges = myEdgeBuilder.build(myXMLHandler.networkVersion());
385 42584 : junctions = myJunctionBuilder.build();
386 42584 : junctions->postloadInitContainer();
387 42580 : routeLoaders = buildRouteLoaderControl(myOptions);
388 42574 : tlc = myJunctionBuilder.buildTLLogics();
389 85444 : for (std::string timeStr : myOptions.getStringVector("save-state.times")) {
390 296 : stateDumpTimes.push_back(string2time(timeStr));
391 : }
392 85148 : if (myOptions.isSet("save-state.files")) {
393 472 : stateDumpFiles = myOptions.getStringVector("save-state.files");
394 236 : if (stateDumpFiles.size() != stateDumpTimes.size()) {
395 0 : throw ProcessError(TL("Wrong number of state file names!"));
396 : }
397 : } else {
398 42338 : const std::string prefix = myOptions.getString("save-state.prefix");
399 84676 : const std::string suffix = myOptions.getString("save-state.suffix");
400 42354 : for (std::vector<SUMOTime>::iterator i = stateDumpTimes.begin(); i != stateDumpTimes.end(); ++i) {
401 16 : std::string timeStamp = time2string(*i);
402 : std::replace(timeStamp.begin(), timeStamp.end(), ':', '-');
403 32 : stateDumpFiles.push_back(prefix + "_" + timeStamp + suffix);
404 : }
405 : }
406 52 : } catch (ProcessError&) {
407 52 : MSEdge::clear();
408 52 : MSLane::clear();
409 52 : delete edges;
410 52 : delete junctions;
411 52 : delete routeLoaders;
412 52 : delete tlc;
413 52 : throw;
414 52 : }
415 : // if anthing goes wrong after this point, the net is responsible for cleaning up
416 42584 : myNet.closeBuilding(myOptions, edges, junctions, routeLoaders, tlc, stateDumpTimes, stateDumpFiles,
417 : myXMLHandler.haveSeenInternalEdge(),
418 : myXMLHandler.hasJunctionHigherSpeeds(),
419 42574 : myXMLHandler.networkVersion());
420 42688 : }
421 :
422 :
423 : bool
424 72295 : NLBuilder::load(const std::string& mmlWhat, const bool isNet) {
425 72295 : if (!myOptions.isUsableFileList(mmlWhat)) {
426 : return false;
427 : }
428 72196 : std::vector<std::string> files = myOptions.getStringVector(mmlWhat);
429 150698 : for (std::vector<std::string>::const_iterator fileIt = files.begin(); fileIt != files.end(); ++fileIt) {
430 238545 : const long before = PROGRESS_BEGIN_TIME_MESSAGE(TLF("Loading % from '%'", mmlWhat, *fileIt));
431 79515 : if (!XMLSubSys::runParser(myXMLHandler, *fileIt, isNet)) {
432 3039 : WRITE_MESSAGEF(TL("Loading of % failed."), mmlWhat);
433 : return false;
434 : }
435 78502 : PROGRESS_TIME_MESSAGE(before);
436 : }
437 : return true;
438 72196 : }
439 :
440 :
441 : SUMORouteLoaderControl*
442 42767 : NLBuilder::buildRouteLoaderControl(const OptionsCont& oc) {
443 : // build the loaders
444 42767 : SUMORouteLoaderControl* loaders = new SUMORouteLoaderControl(string2time(oc.getString("route-steps")));
445 : // check whether a list is existing
446 113341 : if (oc.isSet("route-files") && string2time(oc.getString("route-steps")) > 0) {
447 70574 : std::vector<std::string> files = oc.getStringVector("route-files");
448 70788 : for (std::vector<std::string>::const_iterator fileIt = files.begin(); fileIt != files.end(); ++fileIt) {
449 71014 : if (!FileHelpers::isReadable(*fileIt)) {
450 18 : throw ProcessError(TLF("The route file '%' is not accessible.", *fileIt));
451 : }
452 : }
453 : // open files for reading
454 70782 : for (std::vector<std::string>::const_iterator fileIt = files.begin(); fileIt != files.end(); ++fileIt) {
455 35501 : loaders->add(new SUMORouteLoader(new MSRouteHandler(*fileIt, false)));
456 : }
457 35287 : }
458 42761 : return loaders;
459 : }
460 :
461 :
462 : void
463 83584 : NLBuilder::buildDefaultMeanData(const std::string& optionName, const std::string& id, bool useLanes) {
464 83584 : if (OptionsCont::getOptions().isSet(optionName)) {
465 51 : if (useLanes && MSGlobals::gUseMesoSim && !OptionsCont::getOptions().getBool("meso-lane-queue")) {
466 4 : WRITE_WARNING(TL("LaneData requested for mesoscopic simulation but --meso-lane-queue is not active. Falling back to edgeData."));
467 : useLanes = false;
468 : }
469 : try {
470 78 : myDetectorBuilder.createEdgeLaneMeanData(id, -1, 0, -1, "traffic", useLanes, false, false,
471 39 : false, false, false, 100000, 0, SUMO_const_haltingSpeed, "", "", std::vector<MSEdge*>(), false,
472 78 : OptionsCont::getOptions().getString(optionName));
473 0 : } catch (InvalidArgument& e) {
474 0 : WRITE_ERROR(e.what());
475 0 : } catch (IOError& e) {
476 0 : WRITE_ERROR(e.what());
477 0 : }
478 : }
479 83584 : }
480 :
481 : /****************************************************************************/
|