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 MSTransportableControl.cpp
15 : /// @author Daniel Krajzewicz
16 : /// @author Jakob Erdmann
17 : /// @author Sascha Krieg
18 : /// @author Michael Behrisch
19 : /// @date Mon, 9 Jul 2001
20 : ///
21 : // Stores all persons in the net and handles their waiting for cars.
22 : /****************************************************************************/
23 : #include <config.h>
24 :
25 : #include <vector>
26 : #include <algorithm>
27 : #include <utils/iodevices/OutputDevice.h>
28 : #include <utils/iodevices/OutputDevice_String.h>
29 : #include <utils/options/OptionsCont.h>
30 : #include <microsim/transportables/MSPerson.h>
31 : #include <microsim/transportables/MSStageDriving.h>
32 : #include <microsim/transportables/MSPModel_NonInteracting.h>
33 : #ifdef JPS_VERSION
34 : #include <microsim/transportables/MSPModel_JuPedSim.h>
35 : #endif
36 : #include <microsim/transportables/MSPModel_Striping.h>
37 : #include <microsim/transportables/MSTransportableControl.h>
38 : #include <microsim/devices/MSDevice_Vehroutes.h>
39 : #include <microsim/MSNet.h>
40 : #include <microsim/MSEdge.h>
41 : #include <microsim/MSVehicle.h>
42 : #include <microsim/MSStoppingPlace.h>
43 :
44 :
45 : // ===========================================================================
46 : // method definitions
47 : // ===========================================================================
48 7776 : MSTransportableControl::MSTransportableControl(const bool isPerson):
49 7776 : myLoadedNumber(0),
50 7776 : myDiscardedNumber(0),
51 7776 : myRunningNumber(0),
52 7776 : myJammedNumber(0),
53 7776 : myWaitingForDepartureNumber(0),
54 7776 : myWaitingForVehicleNumber(0),
55 7776 : myWaitingUntilNumber(0),
56 7776 : myAccessNumber(0),
57 7776 : myEndedNumber(0),
58 7776 : myArrivedNumber(0),
59 7776 : myTeleportsAbortWait(0),
60 7776 : myTeleportsWrongDest(0),
61 7776 : myHaveNewWaiting(false) {
62 7776 : const OptionsCont& oc = OptionsCont::getOptions();
63 7776 : MSNet* const net = MSNet::getInstance();
64 7776 : myMovementModel = myNonInteractingModel = new MSPModel_NonInteracting(oc, net);
65 7776 : if (isPerson) {
66 7019 : const std::string& model = oc.getString("pedestrian.model");
67 7019 : if (model == "striping") {
68 5163 : myMovementModel = new MSPModel_Striping(oc, net);
69 : #ifdef JPS_VERSION
70 : } else if (model == "jupedsim") {
71 : myMovementModel = new MSPModel_JuPedSim(oc, net);
72 : #endif
73 1856 : } else if (model != "nonInteracting") {
74 21 : delete myNonInteractingModel;
75 63 : throw ProcessError(TLF("Unknown pedestrian model '%'", model));
76 : }
77 : }
78 15510 : if (oc.isSet("vehroute-output")) {
79 1327 : myRouteInfos.routeOut = &OutputDevice::getDeviceByOption("vehroute-output");
80 : }
81 15510 : if (oc.isSet("personroute-output")) {
82 24 : OutputDevice::createDeviceByOption("personroute-output", "routes", "routes_file.xsd");
83 12 : myRouteInfos.routeOut = &OutputDevice::getDeviceByOption("personroute-output");
84 : }
85 15510 : if (oc.isSet("personinfo-output")) {
86 36 : OutputDevice::createDeviceByOption("personinfo-output", "tripinfos", "tripinfo_file.xsd");
87 : }
88 7755 : myAbortWaitingTimeout = string2time(oc.getString("time-to-teleport.ride"));
89 7755 : myMaxTransportableNumber = isPerson ? oc.getInt("max-num-persons") : -1;
90 7776 : }
91 :
92 :
93 13886 : MSTransportableControl::~MSTransportableControl() {
94 7735 : clearState();
95 7735 : if (myMovementModel != myNonInteractingModel) {
96 5154 : delete myMovementModel;
97 : }
98 7735 : delete myNonInteractingModel;
99 13886 : }
100 :
101 :
102 : bool
103 507661 : MSTransportableControl::add(MSTransportable* transportable) {
104 507661 : const SUMOVehicleParameter& param = transportable->getParameter();
105 507661 : if (myTransportables.find(param.id) == myTransportables.end()) {
106 507645 : myTransportables[param.id] = transportable;
107 507645 : const SUMOTime step = param.depart % DELTA_T == 0 ? param.depart : (param.depart / DELTA_T + 1) * DELTA_T;
108 507645 : myWaiting4Departure[step].push_back(transportable);
109 507645 : myLoadedNumber++;
110 507645 : myWaitingForDepartureNumber++;
111 : return true;
112 : }
113 : return false;
114 : }
115 :
116 :
117 : void
118 48 : MSTransportableControl::fixLoadCount(const MSTransportable* transportable) {
119 48 : myLoadedNumber--;
120 48 : if (transportable->hasDeparted()) {
121 42 : const SUMOVehicleParameter& param = transportable->getParameter();
122 42 : const SUMOTime step = param.depart % DELTA_T == 0 ? param.depart : (param.depart / DELTA_T + 1) * DELTA_T;
123 42 : TransportableVector& waiting = myWaiting4Departure[step];
124 42 : auto it = std::find(waiting.begin(), waiting.end(), transportable);
125 42 : if (it != waiting.end()) {
126 : waiting.erase(it);
127 42 : if (waiting.size() == 0) {
128 : myWaiting4Departure.erase(step);
129 : }
130 : }
131 : }
132 48 : }
133 :
134 :
135 : MSTransportable*
136 509925 : MSTransportableControl::get(const std::string& id) const {
137 : std::map<std::string, MSTransportable*>::const_iterator i = myTransportables.find(id);
138 509925 : if (i == myTransportables.end()) {
139 : return nullptr;
140 : }
141 502804 : return (*i).second;
142 : }
143 :
144 :
145 : void
146 393638 : MSTransportableControl::erase(MSTransportable* transportable) {
147 393638 : const OptionsCont& oc = OptionsCont::getOptions();
148 787276 : if (oc.isSet("personinfo-output")) {
149 360 : transportable->tripInfoOutput(OutputDevice::getDeviceByOption("personinfo-output"));
150 786916 : } else if (oc.isSet("tripinfo-output")) {
151 67534 : transportable->tripInfoOutput(OutputDevice::getDeviceByOption("tripinfo-output"));
152 719382 : } else if (oc.getBool("duration-log.statistics")) {
153 : // collecting statistics is a sideffect
154 21591 : OutputDevice_String dev;
155 21591 : transportable->tripInfoOutput(dev);
156 21591 : }
157 784545 : if (oc.isSet("vehroute-output") || oc.isSet("personroute-output")) {
158 2879 : if (transportable->hasArrived() || oc.getBool("vehroute-output.write-unfinished")) {
159 5382 : if (oc.getBool("vehroute-output.sorted")) {
160 122 : const SUMOTime departure = oc.getBool("vehroute-output.intended-depart") ? transportable->getParameter().depart : transportable->getDeparture();
161 122 : OutputDevice_String od(1);
162 122 : transportable->routeOutput(od, oc.getBool("vehroute-output.route-length"));
163 122 : MSDevice_Vehroutes::writeSortedOutput(&myRouteInfos,
164 122 : departure, transportable->getID(), od.getString());
165 122 : } else {
166 5138 : transportable->routeOutput(*myRouteInfos.routeOut, oc.getBool("vehroute-output.route-length"));
167 : }
168 : }
169 : }
170 : const std::map<std::string, MSTransportable*>::iterator i = myTransportables.find(transportable->getID());
171 393638 : if (i != myTransportables.end()) {
172 393638 : myRunningNumber--;
173 393638 : myEndedNumber++;
174 925177 : MSNet::getInstance()->informTransportableStateListener(transportable,
175 393638 : transportable->isPerson() ? MSNet::TransportableState::PERSON_ARRIVED : MSNet::TransportableState::CONTAINER_ARRIVED);
176 393638 : delete i->second;
177 : myTransportables.erase(i);
178 : }
179 393638 : }
180 :
181 :
182 : void
183 65597 : MSTransportableControl::setWaitEnd(const SUMOTime time, MSTransportable* transportable) {
184 65597 : const SUMOTime step = time % DELTA_T == 0 ? time : (time / DELTA_T + 1) * DELTA_T;
185 : // avoid double registration
186 65597 : const TransportableVector& transportables = myWaitingUntil[step];
187 65597 : if (std::find(transportables.begin(), transportables.end(), transportable) == transportables.end()) {
188 65597 : myWaitingUntil[step].push_back(transportable);
189 65597 : myWaitingUntilNumber++;
190 : }
191 65597 : }
192 :
193 :
194 : void
195 5989669 : MSTransportableControl::checkWaiting(MSNet* net, const SUMOTime time) {
196 5989669 : myHaveNewWaiting = false;
197 12283915 : while (myWaiting4Departure.find(time) != myWaiting4Departure.end()) {
198 304613 : TransportableVector& transportables = myWaiting4Departure[time];
199 : // we cannot use an iterator here because there might be additions to the vector while proceeding
200 787300 : for (auto it = transportables.begin(); it != transportables.end();) {
201 482827 : MSTransportable* t = *it;
202 482827 : if (myMaxTransportableNumber > 0 && myRunningNumber >= myMaxTransportableNumber) {
203 104 : TransportableVector& nextStep = myWaiting4Departure[time + DELTA_T];
204 104 : nextStep.insert(nextStep.begin(), transportables.begin(), transportables.end());
205 : transportables.clear();
206 : break;
207 : }
208 : it = transportables.erase(it);
209 482723 : myWaitingForDepartureNumber--;
210 482723 : const bool isPerson = t->isPerson();
211 482723 : t->setDeparted(time);
212 482723 : if (t->proceed(net, time)) {
213 482687 : myRunningNumber++;
214 676002 : MSNet::getInstance()->informTransportableStateListener(t,
215 : isPerson ? MSNet::TransportableState::PERSON_DEPARTED : MSNet::TransportableState::CONTAINER_DEPARTED);
216 482687 : const OptionsCont& oc = OptionsCont::getOptions();
217 965374 : if (oc.getBool("vehroute-output.sorted")) {
218 132 : const SUMOTime departure = oc.getBool("vehroute-output.intended-depart") ? t->getParameter().depart : time;
219 264 : if (oc.isSet("personroute-output")) {
220 24 : myRouteInfos.departureCounts[departure]++;
221 : } else {
222 108 : MSDevice_Vehroutes::registerTransportableDepart(departure);
223 : }
224 : }
225 : } else {
226 0 : erase(t);
227 : }
228 : }
229 : myWaiting4Departure.erase(time);
230 : }
231 6053677 : while (myWaitingUntil.find(time) != myWaitingUntil.end()) {
232 : // make a copy because 0-duration stops might modify the vector
233 64051 : const TransportableVector transportables = myWaitingUntil[time];
234 : myWaitingUntil.erase(time);
235 129120 : for (MSTransportable* t : transportables) {
236 65076 : myWaitingUntilNumber--;
237 65076 : if (!t->proceed(net, time)) {
238 18267 : erase(t);
239 : }
240 : }
241 64051 : }
242 5989626 : }
243 :
244 :
245 : void
246 43 : MSTransportableControl::forceDeparture() {
247 43 : myRunningNumber++;
248 43 : }
249 :
250 :
251 : void
252 75171 : MSTransportableControl::addWaiting(const MSEdge* const edge, MSTransportable* transportable) {
253 75171 : myWaiting4Vehicle[edge].push_back(transportable);
254 75171 : myWaitingForVehicleNumber++;
255 75171 : myHaveNewWaiting = true;
256 75171 : if (myAbortWaitingTimeout >= 0) {
257 16 : transportable->setAbortWaiting(myAbortWaitingTimeout);
258 : }
259 75171 : }
260 :
261 :
262 : bool
263 83 : MSTransportableControl::hasAnyWaiting(const MSEdge* edge, SUMOVehicle* vehicle) const {
264 : const auto wait = myWaiting4Vehicle.find(edge);
265 83 : if (wait != myWaiting4Vehicle.end()) {
266 44 : for (const MSTransportable* t : wait->second) {
267 : if (t->isWaitingFor(vehicle)
268 39 : && vehicle->allowsBoarding(t)
269 78 : && vehicle->isStoppedInRange(t->getEdgePos(), MSGlobals::gStopTolerance, true)) {
270 : return true;
271 : }
272 : }
273 : }
274 : return false;
275 : }
276 :
277 :
278 : bool
279 980189 : MSTransportableControl::loadAnyWaiting(const MSEdge* edge, SUMOVehicle* vehicle, SUMOTime& timeToLoadNext, SUMOTime& stopDuration, MSTransportable* const force) {
280 : bool ret = false;
281 : const auto wait = myWaiting4Vehicle.find(edge);
282 980189 : if (wait != myWaiting4Vehicle.end()) {
283 49358 : const SUMOTime currentTime = SIMSTEP;
284 49358 : TransportableVector& transportables = wait->second;
285 413459 : for (TransportableVector::iterator i = transportables.begin(); i != transportables.end();) {
286 364101 : MSTransportable* const t = *i;
287 674356 : if (t->isWaitingFor(vehicle) && (t == force ||
288 310255 : (vehicle->allowsBoarding(t)
289 122122 : && timeToLoadNext - DELTA_T <= currentTime
290 12226 : && vehicle->isStoppedInRange(t->getEdgePos(), MSGlobals::gStopTolerance)))) {
291 9430 : edge->removeTransportable(t);
292 9430 : vehicle->addTransportable(t);
293 9430 : if (myAbortWaitingTimeout >= 0) {
294 0 : t->setAbortWaiting(-1);
295 : }
296 9430 : if (timeToLoadNext >= 0) { // meso does not have loading times
297 15000 : const SUMOTime loadingDuration = (SUMOTime)((double)vehicle->getVehicleType().getLoadingDuration(t->isPerson()) * t->getVehicleType().getBoardingFactor());
298 : //update the time point at which the next transportable can be loaded on the vehicle
299 7500 : if (timeToLoadNext > currentTime - DELTA_T) {
300 5001 : timeToLoadNext += loadingDuration;
301 : } else {
302 2499 : timeToLoadNext = currentTime + loadingDuration;
303 : }
304 : }
305 :
306 9430 : static_cast<MSStageDriving*>(t->getCurrentStage())->setVehicle(vehicle);
307 9430 : if (t->getCurrentStage()->getOriginStop() != nullptr) {
308 4782 : t->getCurrentStage()->getOriginStop()->removeTransportable(*i);
309 : }
310 : i = transportables.erase(i);
311 9430 : myWaitingForVehicleNumber--;
312 : ret = true;
313 : } else {
314 : ++i;
315 : }
316 : }
317 49358 : if (transportables.empty()) {
318 : myWaiting4Vehicle.erase(wait);
319 : }
320 49358 : if (ret && timeToLoadNext >= 0) {
321 : //if the time a transportable needs to get loaded on the vehicle extends the duration of the stop of the vehicle extend
322 : //the duration by setting it to the loading duration of the transportable
323 4901 : stopDuration = MAX2(stopDuration, timeToLoadNext - currentTime);
324 : }
325 : }
326 980189 : return ret;
327 : }
328 :
329 :
330 : bool
331 13147521 : MSTransportableControl::hasTransportables() const {
332 13147521 : return !myTransportables.empty();
333 : }
334 :
335 :
336 : bool
337 1180291 : MSTransportableControl::hasNonWaiting() const {
338 1180291 : return !myWaiting4Departure.empty() || getMovingNumber() > 0 || myWaitingUntilNumber > 0 || myHaveNewWaiting;
339 : }
340 :
341 :
342 : int
343 76752 : MSTransportableControl::getActiveCount() {
344 76752 : return (int)myWaiting4Departure.size() + myRunningNumber - myWaitingForVehicleNumber;
345 : }
346 :
347 :
348 : int
349 890270 : MSTransportableControl::getMovingNumber() const {
350 890270 : return myMovementModel->getActiveNumber() + myAccessNumber;
351 : }
352 :
353 :
354 : int
355 4752 : MSTransportableControl::getRidingNumber() const {
356 4752 : return myRunningNumber - myWaitingUntilNumber - myWaitingForVehicleNumber - getMovingNumber();
357 : }
358 :
359 : int
360 4752 : MSTransportableControl::getDepartedNumber() const {
361 4752 : return myLoadedNumber - myWaitingForDepartureNumber - myDiscardedNumber;
362 : }
363 :
364 : void
365 3986 : MSTransportableControl::abortAnyWaitingForVehicle() {
366 4281 : for (const auto& it : myWaiting4Vehicle) {
367 295 : const MSEdge* edge = it.first;
368 900 : for (MSTransportable* const p : it.second) {
369 605 : edge->removeTransportable(p);
370 605 : MSStageDriving* stage = dynamic_cast<MSStageDriving*>(p->getCurrentStage());
371 605 : const std::string waitDescription = stage == nullptr ? "waiting" : stage->getWaitingDescription();
372 1210 : WRITE_WARNING(p->getObjectType() + " '" + p->getID() + "' aborted " + waitDescription + ".");
373 605 : if (myAbortWaitingTimeout >= 0) {
374 0 : p->setAbortWaiting(-1);
375 : }
376 605 : erase(p);
377 : }
378 : }
379 : myWaiting4Vehicle.clear();
380 3986 : myWaitingForVehicleNumber = 0;
381 3986 : }
382 :
383 : void
384 90 : MSTransportableControl::abortWaitingForVehicle(MSTransportable* t) {
385 90 : const MSEdge* edge = t->getEdge();
386 : auto it = myWaiting4Vehicle.find(edge);
387 90 : if (it != myWaiting4Vehicle.end()) {
388 90 : TransportableVector& waiting = it->second;
389 90 : auto it2 = std::find(waiting.begin(), waiting.end(), t);
390 90 : if (it2 != waiting.end()) {
391 90 : if (myAbortWaitingTimeout >= 0) {
392 16 : (*it2)->setAbortWaiting(-1);
393 : }
394 : waiting.erase(it2);
395 : }
396 : }
397 90 : }
398 :
399 : void
400 43 : MSTransportableControl::abortWaiting(MSTransportable* t) {
401 86 : for (std::map<SUMOTime, TransportableVector>::iterator it = myWaiting4Departure.begin(); it != myWaiting4Departure.end(); ++it) {
402 43 : TransportableVector& ts = it->second;
403 43 : TransportableVector::iterator it2 = std::find(ts.begin(), ts.end(), t);
404 43 : if (it2 != ts.end()) {
405 : ts.erase(it2);
406 : }
407 : }
408 43 : for (std::map<SUMOTime, TransportableVector>::iterator it = myWaitingUntil.begin(); it != myWaitingUntil.end(); ++it) {
409 0 : TransportableVector& ts = it->second;
410 0 : TransportableVector::iterator it2 = std::find(ts.begin(), ts.end(), t);
411 0 : if (it2 != ts.end()) {
412 : ts.erase(it2);
413 : }
414 : }
415 43 : }
416 :
417 :
418 : MSTransportable*
419 281657 : MSTransportableControl::buildPerson(const SUMOVehicleParameter* pars, MSVehicleType* vtype, MSTransportable::MSTransportablePlan* plan,
420 : SumoRNG* rng) const {
421 281657 : const double speedFactor = vtype->computeChosenSpeedDeviation(rng);
422 281657 : return new MSPerson(pars, vtype, plan, speedFactor);
423 : }
424 :
425 :
426 : MSTransportable*
427 196780 : MSTransportableControl::buildContainer(const SUMOVehicleParameter* pars, MSVehicleType* vtype, MSTransportable::MSTransportablePlan* plan) const {
428 196780 : return new MSTransportable(pars, vtype, plan, false);
429 : }
430 :
431 :
432 : void
433 29 : MSTransportableControl::saveState(OutputDevice& out) {
434 29 : std::ostringstream oss;
435 174 : oss << myRunningNumber << " " << myLoadedNumber << " " << myEndedNumber << " " << myWaitingForDepartureNumber << " " << myArrivedNumber << " " << myDiscardedNumber;
436 116 : oss << " " << myJammedNumber << " " << myWaitingForVehicleNumber << " " << myWaitingUntilNumber << " " << myHaveNewWaiting;
437 29 : out.writeAttr(SUMO_ATTR_STATE, oss.str());
438 72 : for (const auto& it : myTransportables) {
439 43 : it.second->saveState(out);
440 : }
441 29 : }
442 :
443 :
444 : void
445 34 : MSTransportableControl::loadState(const std::string& state) {
446 34 : std::istringstream iss(state);
447 34 : iss >> myRunningNumber >> myLoadedNumber >> myEndedNumber >> myWaitingForDepartureNumber >> myArrivedNumber >> myDiscardedNumber;
448 34 : iss >> myJammedNumber >> myWaitingForVehicleNumber >> myWaitingUntilNumber >> myHaveNewWaiting;
449 34 : }
450 :
451 : void
452 7760 : MSTransportableControl::clearState() {
453 121716 : for (std::map<std::string, MSTransportable*>::iterator i = myTransportables.begin(); i != myTransportables.end(); ++i) {
454 113956 : delete (*i).second;
455 : }
456 : myTransportables.clear();
457 : myWaiting4Vehicle.clear();
458 : myWaiting4Departure.clear();
459 : myWaitingUntil.clear();
460 7760 : myLoadedNumber = 0;
461 7760 : myDiscardedNumber = 0;
462 7760 : myRunningNumber = 0;
463 7760 : myJammedNumber = 0;
464 7760 : myWaitingForDepartureNumber = 0;
465 7760 : myWaitingForVehicleNumber = 0;
466 7760 : myWaitingUntilNumber = 0;
467 7760 : myEndedNumber = 0;
468 7760 : myArrivedNumber = 0;
469 7760 : myHaveNewWaiting = false;
470 7760 : if (myMovementModel != myNonInteractingModel) {
471 5179 : myMovementModel->clearState();
472 : }
473 7760 : myNonInteractingModel->clearState();
474 7760 : }
475 :
476 : /****************************************************************************/
|