SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
MSLaneChanger.cpp
Go to the documentation of this file.
1 /****************************************************************************/
11 // Performs lane changing of vehicles
12 /****************************************************************************/
13 // SUMO, Simulation of Urban MObility; see http://sumo.sourceforge.net/
14 // Copyright (C) 2001-2012 DLR (http://www.dlr.de/) and contributors
15 /****************************************************************************/
16 //
17 // This file is part of SUMO.
18 // SUMO is free software: you can redistribute it and/or modify
19 // it under the terms of the GNU General Public License as published by
20 // the Free Software Foundation, either version 3 of the License, or
21 // (at your option) any later version.
22 //
23 /****************************************************************************/
24 
25 // ===========================================================================
26 // included modules
27 // ===========================================================================
28 #ifdef _MSC_VER
29 #include <windows_config.h>
30 #else
31 #include <config.h>
32 #endif
33 
34 #include "MSLaneChanger.h"
35 #include "MSVehicle.h"
36 #include "MSVehicleType.h"
37 #include "MSVehicleTransfer.h"
38 #include "MSGlobals.h"
39 #include <cassert>
40 #include <iterator>
41 #include <cstdlib>
42 #include <cmath>
45 
46 #ifdef CHECK_MEMORY_LEAKS
47 #include <foreign/nvwa/debug_new.h>
48 #endif // CHECK_MEMORY_LEAKS
49 
50 //#define DEBUG_VEHICLE_GUI_SELECTION 1
51 #ifdef DEBUG_VEHICLE_GUI_SELECTION
53 #include <guisim/GUIVehicle.h>
54 #include <guisim/GUILane.h>
55 #endif
56 
57 
58 // ===========================================================================
59 // member method definitions
60 // ===========================================================================
61 MSLaneChanger::MSLaneChanger(std::vector<MSLane*>* lanes, bool allowSwap)
62  : myAllowsSwap(allowSwap) {
63  assert(lanes->size() > 1);
64 
65  // Fill the changer with the lane-data.
66  myChanger.reserve(lanes->size());
67  for (std::vector<MSLane*>::iterator lane = lanes->begin(); lane != lanes->end(); ++lane) {
68  ChangeElem ce;
69  ce.follow = 0;
70  ce.lead = 0;
71  ce.lane = *lane;
72  ce.veh = (*lane)->myVehicles.rbegin();
73  ce.hoppedVeh = 0;
74  ce.lastBlocked = 0;
75  myChanger.push_back(ce);
76  }
77 }
78 
79 
81 
82 
83 void
85  // This is what happens in one timestep. After initialization of the
86  // changer, each vehicle will try to change. After that the changer
87  // nedds an update to prevent multiple changes of one vehicle.
88  // Finally, the change-result has to be given back to the lanes.
89  initChanger();
90  while (vehInChanger()) {
91 
92  bool haveChanged = change();
93  updateChanger(haveChanged);
94  }
95  updateLanes(t);
96 }
97 
98 
99 void
101  // Prepare myChanger with a safe state.
102  for (ChangerIt ce = myChanger.begin(); ce != myChanger.end(); ++ce) {
103  ce->lead = 0;
104  ce->hoppedVeh = 0;
105  ce->lastBlocked = 0;
106  ce->dens = 0;
107 
108  MSLane::VehCont& vehicles = ce->lane->myVehicles;
109  if (vehicles.empty()) {
110  ce->veh = vehicles.rend();
111  ce->follow = 0;
112  continue;
113  }
114  ce->veh = vehicles.rbegin();
115  if (vehicles.size() == 1) {
116  ce->follow = 0;
117  continue;
118  }
119  ce->follow = *(vehicles.rbegin() + 1);
120  }
121 }
122 
123 
124 bool
126  // Find change-candidate. If it is on an allowed lane, try to change
127  // to the right (there is a rule in Germany that you have to change
128  // to the right, unless you are overtaking). If change to the right
129  // isn't possible, check if there is a possibility to overtake (on the
130  // left.
131  // If candidate isn't on an allowed lane, changing to an allowed has
132  // priority.
134  MSVehicle* vehicle = veh(myCandi);
135 #ifdef DEBUG_VEHICLE_GUI_SELECTION
136  if (gSelected.isSelected(GLO_VEHICLE, static_cast<const GUIVehicle*>(vehicle)->getGlID())) {
137  int bla = 0;
138  }
139 #endif
140  const std::vector<MSVehicle::LaneQ> &preb = vehicle->getBestLanes();
141  assert(preb.size() == myChanger.size());
142  for (int i = 0; i < (int) myChanger.size(); ++i) {
143  ((std::vector<MSVehicle::LaneQ>&) preb)[i].occupation = myChanger[i].dens + preb[i].nextOccupation;
144  }
145 
146  vehicle->getLaneChangeModel().prepareStep();
147  std::pair<MSVehicle* const, SUMOReal> leader = getRealThisLeader(myCandi);
148  // check whether the vehicle wants and is able to change to right lane
149  int state1 = 0;
150  if (myCandi != myChanger.begin() && (myCandi - 1)->lane->allowsVehicleClass(veh(myCandi)->getVehicleType().getVehicleClass())) {
151  std::pair<MSVehicle* const, SUMOReal> rLead = getRealLeader(myCandi - 1);
152  std::pair<MSVehicle* const, SUMOReal> rFollow = getRealFollower(myCandi - 1);
153  state1 = change2right(leader, rLead, rFollow, preb);
154  if ((state1 & LCA_URGENT) != 0 || (state1 & LCA_SPEEDGAIN) != 0) {
155  state1 |= LCA_RIGHT;
156  }
157  bool changingAllowed1 = (state1 & LCA_BLOCKED) == 0;
158  // change if the vehicle wants to and is allowed to change
159  if ((state1 & LCA_RIGHT) != 0 && changingAllowed1) {
160 #ifndef NO_TRACI
161  // inform lane change model about this change
163 #endif
164  (myCandi - 1)->hoppedVeh = vehicle;
165  (myCandi - 1)->lane->myTmpVehicles.push_front(vehicle);
167  myCandi->lane->leftByLaneChange(vehicle);
168  vehicle->enterLaneAtLaneChange((myCandi - 1)->lane);
169  (myCandi - 1)->lane->enteredByLaneChange(vehicle);
170  vehicle->myLastLaneChangeOffset = 0;
171  vehicle->getLaneChangeModel().changed();
172  (myCandi - 1)->dens += (myCandi - 1)->hoppedVeh->getVehicleType().getLengthWithGap();
173  return true;
174  }
175  if ((state1 & LCA_RIGHT) != 0 && (state1 & LCA_URGENT) != 0) {
176  (myCandi - 1)->lastBlocked = vehicle;
177  }
178  }
179 
180 
181 
182  // check whether the vehicle wants and is able to change to left lane
183  int state2 = 0;
184  if ((myCandi + 1) != myChanger.end() && (myCandi + 1)->lane->allowsVehicleClass(veh(myCandi)->getVehicleType().getVehicleClass())) {
185  std::pair<MSVehicle* const, SUMOReal> lLead = getRealLeader(myCandi + 1);
186  std::pair<MSVehicle* const, SUMOReal> lFollow = getRealFollower(myCandi + 1);
187  state2 = change2left(leader, lLead, lFollow, preb);
188  if ((state2 & LCA_URGENT) != 0 || (state2 & LCA_SPEEDGAIN) != 0) {
189  state2 |= LCA_LEFT;
190  }
191  bool changingAllowed2 = (state2 & LCA_BLOCKED) == 0;
192  //vehicle->getLaneChangeModel().setOwnState(state2|state1);
193  // change if the vehicle wants to and is allowed to change
194  if ((state2 & LCA_LEFT) != 0 && changingAllowed2) {
195 #ifndef NO_TRACI
196  // inform lane change model about this change
198 #endif
199  (myCandi + 1)->hoppedVeh = veh(myCandi);
200  (myCandi + 1)->lane->myTmpVehicles.push_front(veh(myCandi));
202  myCandi->lane->leftByLaneChange(vehicle);
203  vehicle->enterLaneAtLaneChange((myCandi + 1)->lane);
204  (myCandi + 1)->lane->enteredByLaneChange(vehicle);
205  vehicle->myLastLaneChangeOffset = 0;
206  vehicle->getLaneChangeModel().changed();
207  (myCandi + 1)->dens += (myCandi + 1)->hoppedVeh->getVehicleType().getLengthWithGap();
208  return true;
209  }
210  if ((state2 & LCA_LEFT) != 0 && (state2 & LCA_URGENT) != 0) {
211  (myCandi + 1)->lastBlocked = vehicle;
212  }
213  }
214  vehicle->getLaneChangeModel().setOwnState(state2 | state1);
215 
216  if ((state1 & (LCA_URGENT)) != 0 && (state2 & (LCA_URGENT)) != 0) {
217  // ... wants to go to the left AND to the right
218  // just let them go to the right lane...
219  state2 = 0;
220  vehicle->getLaneChangeModel().setOwnState(state1);
221  }
222  // check whether the vehicles should be swapped
223  if (myAllowsSwap && ((state1 & (LCA_URGENT)) != 0 || (state2 & (LCA_URGENT)) != 0)) {
224  // get the direction ...
225  ChangerIt target;
226  int dir;
227  if ((state1 & (LCA_URGENT)) != 0) {
228  // ... wants to go right
229  target = myCandi - 1;
230  dir = -1;
231  }
232  if ((state2 & (LCA_URGENT)) != 0) {
233  // ... wants to go left
234  target = myCandi + 1;
235  dir = 1;
236  }
237  MSVehicle* prohibitor = target->lead;
238  if (target->hoppedVeh != 0) {
239  SUMOReal hoppedPos = target->hoppedVeh->getPositionOnLane();
240  if (prohibitor == 0 || (hoppedPos > vehicle->getPositionOnLane() && prohibitor->getPositionOnLane() > hoppedPos)) {
241  prohibitor = 0;// !!! vehicles should not jump over more than one lanetarget->hoppedVeh;
242  }
243  }
244  if (prohibitor != 0
245  &&
246  ((prohibitor->getLaneChangeModel().getOwnState() & (LCA_URGENT/*|LCA_SPEEDGAIN*/)) != 0
247  &&
248  (prohibitor->getLaneChangeModel().getOwnState() & (LCA_LEFT | LCA_RIGHT))
249  !=
250  (vehicle->getLaneChangeModel().getOwnState() & (LCA_LEFT | LCA_RIGHT))
251  )
252  ) {
253 
254  // check for position and speed
255  if (prohibitor->getVehicleType().getLengthWithGap() - vehicle->getVehicleType().getLengthWithGap() == 0) {
256  // ok, may be swapped
257  // remove vehicle to swap with
258  MSLane::VehCont::iterator i = find(target->lane->myTmpVehicles.begin(), target->lane->myTmpVehicles.end(), prohibitor);
259  if (i != target->lane->myTmpVehicles.end()) {
260  MSVehicle* bla = *i;
261  assert(bla == prohibitor);
262  target->lane->myTmpVehicles.erase(i);
263  // set this vehicle
264  target->hoppedVeh = vehicle;
265  target->lane->myTmpVehicles.push_front(vehicle);
266  myCandi->hoppedVeh = prohibitor;
267  myCandi->lane->myTmpVehicles.push_front(prohibitor);
268 
269  // leave lane and detectors
272  // patch position and speed
273  SUMOReal p1 = vehicle->getPositionOnLane();
274  vehicle->myState.myPos = prohibitor->myState.myPos;
275  prohibitor->myState.myPos = p1;
276  p1 = vehicle->getSpeed();
277  vehicle->myState.mySpeed = prohibitor->myState.mySpeed;
278  prohibitor->myState.mySpeed = p1;
279  // enter lane and detectors
280  vehicle->enterLaneAtLaneChange(target->lane);
281  prohibitor->enterLaneAtLaneChange(myCandi->lane);
282  // mark lane change
283  vehicle->getLaneChangeModel().changed();
284  vehicle->myLastLaneChangeOffset = 0;
285  prohibitor->getLaneChangeModel().changed();
286  prohibitor->myLastLaneChangeOffset = 0;
287  (myCandi)->dens += prohibitor->getVehicleType().getLengthWithGap();
288  (target)->dens += vehicle->getVehicleType().getLengthWithGap();
289  return true;
290  }
291  }
292  }
293  }
294  // Candidate didn't change lane.
295  myCandi->lane->myTmpVehicles.push_front(veh(myCandi));
296  vehicle->myLastLaneChangeOffset += DELTA_T;
297  (myCandi)->dens += vehicle->getVehicleType().getLengthWithGap();
298  return false;
299 }
300 
301 
302 std::pair<MSVehicle* const, SUMOReal>
304  // get the leading vehicle on the lane to change to
305  MSVehicle* leader = target->lead;
306  if (leader == 0) {
307  MSLane* targetLane = target->lane;
308  MSVehicle* predP = targetLane->getPartialOccupator();
309  if (predP != 0) {
310  return std::pair<MSVehicle*, SUMOReal>(predP, targetLane->getPartialOccupatorEnd() - veh(myCandi)->getPositionOnLane());
311  }
312  const std::vector<MSLane*> &bestLaneConts = veh(myCandi)->getBestLanesContinuation();
313  MSLinkCont::const_iterator link = targetLane->succLinkSec(*veh(myCandi), 1, *targetLane, bestLaneConts);
314  if (targetLane->isLinkEnd(link)) {
315  return std::pair<MSVehicle*, SUMOReal>(static_cast<MSVehicle*>(0), -1);
316  }
317  MSLane* nextLane = (*link)->getLane();
318  if (nextLane == 0) {
319  return std::pair<MSVehicle*, SUMOReal>(static_cast<MSVehicle*>(0), -1);
320  }
321  leader = nextLane->getLastVehicle();
322  if (leader == 0) {
323  return std::pair<MSVehicle*, SUMOReal>(static_cast<MSVehicle*>(0), -1);
324  }
325  SUMOReal gap =
326  leader->getPositionOnLane() - leader->getVehicleType().getLength()
327  +
328  (myCandi->lane->getLength() - veh(myCandi)->getPositionOnLane() - veh(myCandi)->getVehicleType().getMinGap()); // !!! recheck
329  return std::pair<MSVehicle * const, SUMOReal>(leader, MAX2((SUMOReal) 0, gap));
330  } else {
331  MSVehicle* candi = veh(myCandi);
332  SUMOReal gap = leader->getPositionOnLane() - leader->getVehicleType().getLength() - candi->getPositionOnLane() - candi->getVehicleType().getMinGap();
333  return std::pair<MSVehicle * const, SUMOReal>(leader, MAX2((SUMOReal) 0, gap));
334  }
335 }
336 
337 
338 std::pair<MSVehicle* const, SUMOReal>
340  // get the leading vehicle on the lane to change to
341  MSVehicle* neighLead = target->lead;
342  // check whether the hopped vehicle got the leader
343  if (target->hoppedVeh != 0) {
344  SUMOReal hoppedPos = target->hoppedVeh->getPositionOnLane();
345  if (hoppedPos > veh(myCandi)->getPositionOnLane() && (neighLead == 0 || neighLead->getPositionOnLane() > hoppedPos)) {
346  neighLead = target->hoppedVeh;
347  }
348  }
349  if (neighLead == 0) {
350  MSLane* targetLane = target->lane;
351  MSVehicle* predP = targetLane->getPartialOccupator();
352  if (predP != 0) {
353  return std::pair<MSVehicle*, SUMOReal>(predP, targetLane->getPartialOccupatorEnd() - veh(myCandi)->getPositionOnLane() - veh(myCandi)->getVehicleType().getMinGap());
354  }
355  const std::vector<MSLane*> &bestLaneConts = veh(myCandi)->getBestLanesContinuation(myCandi->lane);
356  SUMOReal seen = myCandi->lane->getLength() - veh(myCandi)->getPositionOnLane();
357  SUMOReal speed = veh(myCandi)->getSpeed();
358  SUMOReal dist = veh(myCandi)->getCarFollowModel().brakeGap(speed);
359  if (seen > dist) {
360  return std::pair<MSVehicle * const, SUMOReal>(static_cast<MSVehicle*>(0), -1);
361  }
362  return target->lane->getLeaderOnConsecutive(dist, seen, speed, *veh(myCandi), bestLaneConts);
363  } else {
364  MSVehicle* candi = veh(myCandi);
365  return std::pair<MSVehicle * const, SUMOReal>(neighLead, neighLead->getPositionOnLane() - neighLead->getVehicleType().getLength() - candi->getPositionOnLane() - candi->getVehicleType().getMinGap());
366  }
367 }
368 
369 
370 std::pair<MSVehicle* const, SUMOReal>
372  MSVehicle* neighFollow = veh(target);
373  // check whether the hopped vehicle got the follower
374  if (target->hoppedVeh != 0) {
375  SUMOReal hoppedPos = target->hoppedVeh->getPositionOnLane();
376  if (hoppedPos <= veh(myCandi)->getPositionOnLane() && (neighFollow == 0 || neighFollow->getPositionOnLane() > hoppedPos)) {
377  neighFollow = target->hoppedVeh;
378  }
379  }
380  if (neighFollow == 0) {
381  SUMOReal speed = target->lane->getMaxSpeed();
382  // in order to look back, we'd need the minimum braking ability of vehicles in the net...
383  // we'll assume it to be 4m/s^2
384  // !!!revisit
385  SUMOReal dist = speed * speed / (2.*4.) + SPEED2DIST(speed);
386  dist = MIN2(dist, (SUMOReal) 500.);
387  MSVehicle* candi = veh(myCandi);
388  SUMOReal seen = candi->getPositionOnLane() - candi->getVehicleType().getLength();
389  return target->lane->getFollowerOnConsecutive(dist, seen, candi->getSpeed(), candi->getPositionOnLane() - candi->getVehicleType().getLength(), 4.5);
390  } else {
391  MSVehicle* candi = veh(myCandi);
392  return std::pair<MSVehicle * const, SUMOReal>(neighFollow, candi->getPositionOnLane() - candi->getVehicleType().getLength() - neighFollow->getPositionOnLane() - neighFollow->getVehicleType().getMinGap());
393  }
394 }
395 
396 
397 
398 
399 void
400 MSLaneChanger::updateChanger(bool vehHasChanged) {
401  assert(myCandi->veh != myCandi->lane->myVehicles.rend());
402 
403  // "Push" the vehicles to the back, i.e. follower becomes vehicle,
404  // vehicle becomes leader, and leader becomes predecessor of vehicle,
405  // if it exists.
406  if (!vehHasChanged) {
407  myCandi->lead = veh(myCandi);
408  }
409  myCandi->veh = myCandi->veh + 1;
410 
411  if (veh(myCandi) == 0) {
412  assert(myCandi->follow == 0);
413  // leader already 0.
414  return;
415  }
416  if (myCandi->veh + 1 == myCandi->lane->myVehicles.rend()) {
417  myCandi->follow = 0;
418  } else {
419  myCandi->follow = *(myCandi->veh + 1) ;
420  }
421  return;
422 }
423 
424 
425 void
427 
428  // Update the lane's vehicle-container.
429  // First: it is bad style to change other classes members, but for
430  // this release, other attempts were too time-consuming. In a next
431  // release we will change from this lane-centered design to a vehicle-
432  // centered. This will solve many problems.
433  // Second: this swap would be faster if vehicle-containers would have
434  // been pointers, but then I had to change too much of the MSLane code.
435  for (ChangerIt ce = myChanger.begin(); ce != myChanger.end(); ++ce) {
436 
437  ce->lane->swapAfterLaneChange(t);
438  }
439 }
440 
441 
444  // Find the vehicle in myChanger with the smallest position. If there
445  // is no vehicle in myChanger (shouldn't happen) , return
446  // myChanger.end().
447  ChangerIt max = myChanger.end();
448  for (ChangerIt ce = myChanger.begin(); ce != myChanger.end(); ++ce) {
449  if (veh(ce) == 0) {
450  continue;
451  }
452  if (max == myChanger.end()) {
453  max = ce;
454  continue;
455  }
456  assert(veh(ce) != 0);
457  assert(veh(max) != 0);
458  if (veh(max)->getPositionOnLane() < veh(ce)->getPositionOnLane()) {
459  max = ce;
460  }
461  }
462  assert(max != myChanger.end());
463  assert(veh(max) != 0);
464  return max;
465 }
466 
467 
468 int
469 MSLaneChanger::change2right(const std::pair<MSVehicle* const, SUMOReal> &leader,
470  const std::pair<MSVehicle* const, SUMOReal> &rLead,
471  const std::pair<MSVehicle* const, SUMOReal> &rFollow,
472  const std::vector<MSVehicle::LaneQ> &preb) const {
473  ChangerIt target = myCandi - 1;
474  int blocked = overlapWithHopped(target)
475  ? target->hoppedVeh->getPositionOnLane() < veh(myCandi)->getPositionOnLane()
478  : 0;
479  // overlap
480  if (rFollow.first != 0 && rFollow.second < 0) {
481  blocked |= (LCA_BLOCKED_BY_RIGHT_FOLLOWER);
482  }
483  if (rLead.first != 0 && rLead.second < 0) {
484  blocked |= (LCA_BLOCKED_BY_RIGHT_LEADER);
485  }
486  // safe back gap
487  if (rFollow.first != 0) {
488  // !!! eigentlich: vsafe braucht die Max. Geschwindigkeit beider Spuren
489  if (rFollow.second < rFollow.first->getCarFollowModel().getSecureGap(rFollow.first->getSpeed(), veh(myCandi)->getSpeed(), veh(myCandi)->getCarFollowModel().getMaxDecel())) {
491  }
492  }
493 
494  // safe front gap
495  if (rLead.first != 0) {
496  // !!! eigentlich: vsafe braucht die Max. Geschwindigkeit beider Spuren
497  if (rLead.second < veh(myCandi)->getCarFollowModel().getSecureGap(veh(myCandi)->getSpeed(), rLead.first->getSpeed(), rLead.first->getCarFollowModel().getMaxDecel())) {
498  blocked |= LCA_BLOCKED_BY_RIGHT_LEADER;
499  }
500  }
501 
502  MSAbstractLaneChangeModel::MSLCMessager msg(leader.first, rLead.first, rFollow.first);
503  return blocked | veh(myCandi)->getLaneChangeModel().wantsChangeToRight(
504  msg, blocked, leader, rLead, rFollow, *(myCandi - 1)->lane, preb, &(myCandi->lastBlocked));
505 }
506 
507 
508 int
509 MSLaneChanger::change2left(const std::pair<MSVehicle* const, SUMOReal> &leader,
510  const std::pair<MSVehicle* const, SUMOReal> &rLead,
511  const std::pair<MSVehicle* const, SUMOReal> &rFollow,
512  const std::vector<MSVehicle::LaneQ> &preb) const {
513  ChangerIt target = myCandi + 1;
514  int blocked = overlapWithHopped(target)
515  ? target->hoppedVeh->getPositionOnLane() < veh(myCandi)->getPositionOnLane()
518  : 0;
519  // overlap
520  if (rFollow.first != 0 && rFollow.second < 0) {
521  blocked |= (LCA_BLOCKED_BY_LEFT_FOLLOWER);
522  }
523  if (rLead.first != 0 && rLead.second < 0) {
524  blocked |= (LCA_BLOCKED_BY_LEFT_LEADER);
525  }
526  // safe back gap
527  if (rFollow.first != 0) {
528  // !!! eigentlich: vsafe braucht die Max. Geschwindigkeit beider Spuren
529  if (rFollow.second < rFollow.first->getCarFollowModel().getSecureGap(rFollow.first->getSpeed(), veh(myCandi)->getSpeed(), veh(myCandi)->getCarFollowModel().getMaxDecel())) {
530  blocked |= LCA_BLOCKED_BY_LEFT_FOLLOWER;
531  }
532  }
533  // safe front gap
534  if (rLead.first != 0) {
535  // !!! eigentlich: vsafe braucht die Max. Geschwindigkeit beider Spuren
536  if (rLead.second < veh(myCandi)->getCarFollowModel().getSecureGap(veh(myCandi)->getSpeed(), rLead.first->getSpeed(), rLead.first->getCarFollowModel().getMaxDecel())) {
537  blocked |= LCA_BLOCKED_BY_LEFT_LEADER;
538  }
539  }
540  MSAbstractLaneChangeModel::MSLCMessager msg(leader.first, rLead.first, rFollow.first);
541  return blocked | veh(myCandi)->getLaneChangeModel().wantsChangeToLeft(
542  msg, blocked, leader, rLead, rFollow, *(myCandi + 1)->lane, preb, &(myCandi->lastBlocked));
543 }
544 
545 
546 
547 
548 /****************************************************************************/
549