Line data Source code
1 : /****************************************************************************/
2 : // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3 : // Copyright (C) 2014-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 MSPModel_NonInteracting.cpp
15 : /// @author Jakob Erdmann
16 : /// @date Mon, 13 Jan 2014
17 : ///
18 : // The pedestrian following model (prototype)
19 : /****************************************************************************/
20 : #include <config.h>
21 :
22 : #include <cmath>
23 : #include <algorithm>
24 : #include <utils/common/RandHelper.h>
25 : #include <utils/geom/GeomHelper.h>
26 : #include <utils/options/OptionsCont.h>
27 : #include <utils/router/IntermodalNetwork.h>
28 : #include <microsim/MSGlobals.h>
29 : #include <microsim/MSNet.h>
30 : #include <microsim/MSEdge.h>
31 : #include <microsim/MSLane.h>
32 : #include <microsim/MSJunction.h>
33 : #include <microsim/MSEventControl.h>
34 : #include "MSPModel_NonInteracting.h"
35 :
36 :
37 : // ===========================================================================
38 : // static members
39 : // ===========================================================================
40 : const double MSPModel_NonInteracting::CState::LATERAL_OFFSET(0);
41 :
42 :
43 : // ===========================================================================
44 : // MSPModel_NonInteracting method definitions
45 : // ===========================================================================
46 7699 : MSPModel_NonInteracting::MSPModel_NonInteracting(const OptionsCont& oc, MSNet* net) :
47 7699 : myNet(net),
48 7699 : myNumActivePedestrians(0) {
49 : assert(myNet != 0);
50 : UNUSED_PARAMETER(oc);
51 7699 : }
52 :
53 :
54 15358 : MSPModel_NonInteracting::~MSPModel_NonInteracting() {
55 15358 : }
56 :
57 :
58 : MSTransportableStateAdapter*
59 153510 : MSPModel_NonInteracting::add(MSTransportable* transportable, MSStageMoving* stage, SUMOTime now) {
60 153510 : myNumActivePedestrians++;
61 153510 : MoveToNextEdge* const cmd = new MoveToNextEdge(transportable, *stage, this);
62 153510 : PState* const state = transportable->isPerson() ? new PState(cmd) : new CState(cmd);
63 153510 : myNet->getBeginOfTimestepEvents()->addEvent(cmd, now + state->computeDuration(nullptr, *stage, now));
64 153510 : return state;
65 : }
66 :
67 :
68 : MSTransportableStateAdapter*
69 10 : MSPModel_NonInteracting::loadState(MSTransportable* transportable, MSStageMoving* stage, std::istringstream& in) {
70 10 : myNumActivePedestrians++;
71 10 : MoveToNextEdge* const cmd = new MoveToNextEdge(transportable, *stage, this);
72 10 : PState* const state = transportable->isPerson() ? new PState(cmd, &in) : new CState(cmd, &in);
73 10 : myNet->getBeginOfTimestepEvents()->addEvent(cmd, state->getEventTime());
74 10 : return state;
75 : }
76 :
77 : void
78 7683 : MSPModel_NonInteracting::clearState() {
79 7683 : myNumActivePedestrians = 0;
80 7683 : }
81 :
82 : void
83 430 : MSPModel_NonInteracting::remove(MSTransportableStateAdapter* state) {
84 430 : myNumActivePedestrians--;
85 430 : dynamic_cast<PState*>(state)->getCommand()->abortWalk();
86 430 : }
87 :
88 :
89 : // ---------------------------------------------------------------------------
90 : // MSPModel_NonInteracting::MoveToNextEdge method definitions
91 : // ---------------------------------------------------------------------------
92 : SUMOTime
93 163303 : MSPModel_NonInteracting::MoveToNextEdge::execute(SUMOTime currentTime) {
94 163303 : if (myTransportable == nullptr) {
95 : return 0; // descheduled
96 : }
97 162877 : const MSEdge* old = myParent.getEdge();
98 162877 : const bool arrived = myParent.moveToNextEdge(myTransportable, currentTime, myParent.getPState()->getDirection());
99 162877 : if (arrived) {
100 151135 : myModel->registerArrived();
101 151135 : return 0;
102 : }
103 11742 : myParent.activateEntryReminders(myTransportable);
104 11742 : return static_cast<PState*>(myParent.getPState())->computeDuration(old, myParent, currentTime);
105 : }
106 :
107 :
108 : // ---------------------------------------------------------------------------
109 : // MSPModel_NonInteracting::PState method definitions
110 : // ---------------------------------------------------------------------------
111 153520 : MSPModel_NonInteracting::PState::PState(MoveToNextEdge* cmd, std::istringstream* in) : myCommand(cmd) {
112 153520 : if (in != nullptr) {
113 10 : (*in) >> myLastEntryTime >> myCurrentDuration;
114 : }
115 153520 : }
116 :
117 :
118 : SUMOTime
119 35013 : MSPModel_NonInteracting::PState::computeDuration(const MSEdge* prev, const MSStageMoving& stage, SUMOTime currentTime) {
120 35013 : myLastEntryTime = currentTime;
121 35013 : const MSEdge* edge = stage.getEdge();
122 35013 : const MSEdge* next = stage.getNextRouteEdge();
123 35013 : int dir = UNDEFINED_DIRECTION;
124 35013 : if (prev == nullptr) {
125 23271 : myCurrentBeginPos = stage.getDepartPos();
126 : } else {
127 : // default to FORWARD if not connected
128 11742 : dir = (edge->getToJunction() == prev->getToJunction() || edge->getToJunction() == prev->getFromJunction()) ? BACKWARD : FORWARD;
129 14291 : myCurrentBeginPos = dir == FORWARD ? 0 : edge->getLength();
130 : }
131 35013 : if (next == nullptr) {
132 22744 : myCurrentEndPos = stage.getArrivalPos();
133 : } else {
134 12269 : if (dir == UNDEFINED_DIRECTION) {
135 : // default to FORWARD if not connected
136 8167 : dir = (edge->getFromJunction() == next->getFromJunction() || edge->getFromJunction() == next->getToJunction()) ? BACKWARD : FORWARD;
137 : }
138 22046 : myCurrentEndPos = dir == FORWARD ? edge->getLength() : 0;
139 : }
140 : // ensure that a result > 0 is returned even if the walk ends immediately
141 : // adding 0.5ms is done to ensure proper rounding
142 35013 : myCurrentDuration = MAX2((SUMOTime)1, TIME2STEPS(fabs(myCurrentEndPos - myCurrentBeginPos) / stage.getMaxSpeed(myCommand->getTransportable())));
143 : //std::cout << std::setprecision(8) << SIMTIME << " curBeg=" << myCurrentBeginPos << " curEnd=" << myCurrentEndPos << " speed=" << stage.getMaxSpeed(myCommand->getTransportable()) << " dur=" << myCurrentDuration << "\n";
144 : // round to the next timestep to avoid systematic higher walking speed
145 35013 : if ((myCurrentDuration % DELTA_T) > 0) {
146 28840 : myCurrentDuration += DELTA_T;
147 : }
148 35013 : return myCurrentDuration;
149 : }
150 :
151 :
152 : double
153 943523 : MSPModel_NonInteracting::PState::getEdgePos(SUMOTime now) const {
154 : //std::cout << SIMTIME << " lastEntryTime=" << myLastEntryTime << " pos=" << (myCurrentBeginPos + (myCurrentEndPos - myCurrentBeginPos) / myCurrentDuration * (now - myLastEntryTime)) << "\n";
155 943523 : return myCurrentBeginPos + (myCurrentEndPos - myCurrentBeginPos) / (double)myCurrentDuration * (double)(now - myLastEntryTime);
156 : }
157 :
158 : int
159 162877 : MSPModel_NonInteracting::PState::getDirection() const {
160 162877 : if (myCurrentBeginPos == myCurrentEndPos) {
161 2283 : return UNDEFINED_DIRECTION;
162 : } else {
163 160594 : return myCurrentBeginPos < myCurrentEndPos ? FORWARD : BACKWARD;
164 : }
165 : }
166 :
167 :
168 : Position
169 189232 : MSPModel_NonInteracting::PState::getPosition(const MSStageMoving& stage, SUMOTime now) const {
170 189232 : const MSLane* lane = getSidewalk<MSEdge, MSLane>(stage.getEdge());
171 189232 : if (lane == nullptr) {
172 : //std::string error = "Pedestrian '" + myCommand->myPerson->getID() + "' could not find sidewalk on edge '" + state.getEdge()->getID() + "', time="
173 : // + time2string(MSNet::getInstance()->getCurrentTimeStep()) + ".";
174 : //if (!OptionsCont::getOptions().getBool("ignore-route-errors")) {
175 : // throw ProcessError(error);
176 : //}
177 171 : lane = stage.getEdge()->getLanes().front();
178 : }
179 189403 : const double lateral_offset = (lane->allowsVehicleClass(SVC_PEDESTRIAN) ? 0 : SIDEWALK_OFFSET
180 171 : * (MSGlobals::gLefthand ? -1 : 1));
181 189232 : return stage.getLanePosition(lane, getEdgePos(now), lateral_offset);
182 : }
183 :
184 :
185 : double
186 115650 : MSPModel_NonInteracting::PState::getAngle(const MSStageMoving& stage, SUMOTime now) const {
187 : //std::cout << SIMTIME << " rawAngle=" << stage.getEdgeAngle(stage.getEdge(), getEdgePos(stage, now)) << " angle=" << stage.getEdgeAngle(stage.getEdge(), getEdgePos(stage, now)) + (myCurrentEndPos < myCurrentBeginPos ? 180 : 0) << "\n";
188 115650 : double angle = stage.getEdgeAngle(stage.getEdge(), getEdgePos(now)) + (myCurrentEndPos < myCurrentBeginPos ? M_PI : 0);
189 115650 : if (angle > M_PI) {
190 3349 : angle -= 2 * M_PI;
191 : }
192 115650 : return angle;
193 : }
194 :
195 :
196 : double
197 184690 : MSPModel_NonInteracting::PState::getSpeed(const MSStageMoving& stage) const {
198 184690 : return stage.getMaxSpeed(myCommand->getTransportable());
199 : }
200 :
201 :
202 : const MSEdge*
203 1 : MSPModel_NonInteracting::PState::getNextEdge(const MSStageMoving& stage) const {
204 1 : return stage.getNextRouteEdge();
205 : }
206 :
207 :
208 : void
209 10 : MSPModel_NonInteracting::PState::saveState(std::ostringstream& out) {
210 20 : out << " " << myLastEntryTime << " " << myCurrentDuration;
211 10 : }
212 :
213 :
214 : // ---------------------------------------------------------------------------
215 : // MSPModel_NonInteracting::CState method definitions
216 : // ---------------------------------------------------------------------------
217 130239 : MSPModel_NonInteracting::CState::CState(MoveToNextEdge* cmd, std::istringstream* in) : PState(cmd, in) {
218 130239 : }
219 :
220 :
221 : Position
222 75 : MSPModel_NonInteracting::CState::getPosition(const MSStageMoving& stage, SUMOTime now) const {
223 : const double dist = myCurrentBeginPosition.distanceTo2D(myCurrentEndPosition); //distance between begin and end position of this tranship stage
224 75 : double pos = MIN2(STEPS2TIME(now - myLastEntryTime) * stage.getMaxSpeed(), dist); //the container shall not go beyond its end position
225 75 : return PositionVector::positionAtOffset2D(myCurrentBeginPosition, myCurrentEndPosition, pos, 0);
226 : }
227 :
228 :
229 : double
230 75 : MSPModel_NonInteracting::CState::getAngle(const MSStageMoving& stage, SUMOTime now) const {
231 75 : double angle = stage.getEdgeAngle(stage.getEdge(), getEdgePos(now)) + (myCurrentEndPos < myCurrentBeginPos ? 1.5 * M_PI : 0.5 * M_PI);
232 75 : if (angle > M_PI) {
233 25 : angle -= 2 * M_PI;
234 : }
235 75 : return angle;
236 : }
237 :
238 :
239 : SUMOTime
240 130239 : MSPModel_NonInteracting::CState::computeDuration(const MSEdge* /* prev */, const MSStageMoving& stage, SUMOTime currentTime) {
241 130239 : myLastEntryTime = currentTime;
242 :
243 130239 : myCurrentBeginPos = stage.getDepartPos();
244 130239 : myCurrentEndPos = stage.getArrivalPos();
245 :
246 130239 : const MSLane* fromLane = stage.getFromEdge()->getLanes().front(); //the lane the container starts from during its tranship stage
247 130239 : myCurrentBeginPosition = stage.getLanePosition(fromLane, myCurrentBeginPos, LATERAL_OFFSET);
248 130239 : const MSLane* toLane = stage.getEdges().back()->getLanes().front(); //the lane the container ends during its tranship stage
249 130239 : myCurrentEndPosition = stage.getLanePosition(toLane, myCurrentEndPos, LATERAL_OFFSET);
250 :
251 130239 : myCurrentDuration = MAX2((SUMOTime)1, TIME2STEPS(fabs(myCurrentEndPosition.distanceTo(myCurrentBeginPosition)) / stage.getMaxSpeed()));
252 130239 : return myCurrentDuration;
253 : }
254 :
255 :
256 : /****************************************************************************/
|