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-2013 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 #ifndef NO_TRACI
141  if (vehicle->hasInfluencer() && vehicle->getInfluencer().isVTDControlled()) {
142  return false; // !!! temporary; just because it broke, here
143  }
144 #endif
145  const std::vector<MSVehicle::LaneQ>& preb = vehicle->getBestLanes();
146  assert(preb.size() == myChanger.size());
147  for (int i = 0; i < (int) myChanger.size(); ++i) {
148  ((std::vector<MSVehicle::LaneQ>&) preb)[i].occupation = myChanger[i].dens + preb[i].nextOccupation;
149  }
150 
151  vehicle->getLaneChangeModel().prepareStep();
152  std::pair<MSVehicle* const, SUMOReal> leader = getRealThisLeader(myCandi);
153  // check whether the vehicle wants and is able to change to right lane
154  int state1 = 0;
155  if (myCandi != myChanger.begin() && (myCandi - 1)->lane->allowsVehicleClass(veh(myCandi)->getVehicleType().getVehicleClass())) {
156  std::pair<MSVehicle* const, SUMOReal> rLead = getRealLeader(myCandi - 1);
157  std::pair<MSVehicle* const, SUMOReal> rFollow = getRealFollower(myCandi - 1);
158  state1 = change2right(leader, rLead, rFollow, preb);
159  if ((state1 & LCA_URGENT) != 0 || (state1 & LCA_SPEEDGAIN) != 0) {
160  state1 |= LCA_RIGHT;
161  }
162  bool changingAllowed1 = (state1 & LCA_BLOCKED) == 0;
163  // change if the vehicle wants to and is allowed to change
164  if ((state1 & LCA_RIGHT) != 0 && changingAllowed1) {
165 #ifndef NO_TRACI
166  // inform lane change model about this change
168 #endif
169  (myCandi - 1)->hoppedVeh = vehicle;
170  (myCandi - 1)->lane->myTmpVehicles.push_front(vehicle);
172  myCandi->lane->leftByLaneChange(vehicle);
173  vehicle->enterLaneAtLaneChange((myCandi - 1)->lane);
174  (myCandi - 1)->lane->enteredByLaneChange(vehicle);
175  vehicle->myLastLaneChangeOffset = 0;
176  vehicle->getLaneChangeModel().changed();
177  (myCandi - 1)->dens += (myCandi - 1)->hoppedVeh->getVehicleType().getLengthWithGap();
178  return true;
179  }
180  if ((state1 & LCA_RIGHT) != 0 && (state1 & LCA_URGENT) != 0) {
181  (myCandi - 1)->lastBlocked = vehicle;
182  }
183  }
184 
185 
186 
187  // check whether the vehicle wants and is able to change to left lane
188  int state2 = 0;
189  if ((myCandi + 1) != myChanger.end() && (myCandi + 1)->lane->allowsVehicleClass(veh(myCandi)->getVehicleType().getVehicleClass())) {
190  std::pair<MSVehicle* const, SUMOReal> lLead = getRealLeader(myCandi + 1);
191  std::pair<MSVehicle* const, SUMOReal> lFollow = getRealFollower(myCandi + 1);
192  state2 = change2left(leader, lLead, lFollow, preb);
193  if ((state2 & LCA_URGENT) != 0 || (state2 & LCA_SPEEDGAIN) != 0) {
194  state2 |= LCA_LEFT;
195  }
196  bool changingAllowed2 = (state2 & LCA_BLOCKED) == 0;
197  //vehicle->getLaneChangeModel().setOwnState(state2|state1);
198  // change if the vehicle wants to and is allowed to change
199  if ((state2 & LCA_LEFT) != 0 && changingAllowed2) {
200 #ifndef NO_TRACI
201  // inform lane change model about this change
203 #endif
204  (myCandi + 1)->hoppedVeh = veh(myCandi);
205  (myCandi + 1)->lane->myTmpVehicles.push_front(veh(myCandi));
207  myCandi->lane->leftByLaneChange(vehicle);
208  vehicle->enterLaneAtLaneChange((myCandi + 1)->lane);
209  (myCandi + 1)->lane->enteredByLaneChange(vehicle);
210  vehicle->myLastLaneChangeOffset = 0;
211  vehicle->getLaneChangeModel().changed();
212  (myCandi + 1)->dens += (myCandi + 1)->hoppedVeh->getVehicleType().getLengthWithGap();
213  return true;
214  }
215  if ((state2 & LCA_LEFT) != 0 && (state2 & LCA_URGENT) != 0) {
216  (myCandi + 1)->lastBlocked = vehicle;
217  }
218  }
219  vehicle->getLaneChangeModel().setOwnState(state2 | state1);
220 
221  if ((state1 & (LCA_URGENT)) != 0 && (state2 & (LCA_URGENT)) != 0) {
222  // ... wants to go to the left AND to the right
223  // just let them go to the right lane...
224  state2 = 0;
225  vehicle->getLaneChangeModel().setOwnState(state1);
226  }
227  // check whether the vehicles should be swapped
228  if (myAllowsSwap && ((state1 & (LCA_URGENT)) != 0 || (state2 & (LCA_URGENT)) != 0)) {
229  // get the direction ...
230  ChangerIt target;
231  if ((state1 & (LCA_URGENT)) != 0) {
232  // ... wants to go right
233  target = myCandi - 1;
234  }
235  if ((state2 & (LCA_URGENT)) != 0) {
236  // ... wants to go left
237  target = myCandi + 1;
238  }
239  MSVehicle* prohibitor = target->lead;
240  if (target->hoppedVeh != 0) {
241  SUMOReal hoppedPos = target->hoppedVeh->getPositionOnLane();
242  if (prohibitor == 0 || (hoppedPos > vehicle->getPositionOnLane() && prohibitor->getPositionOnLane() > hoppedPos)) {
243  prohibitor = 0;// !!! vehicles should not jump over more than one lanetarget->hoppedVeh;
244  }
245  }
246  if (prohibitor != 0
247  &&
248  ((prohibitor->getLaneChangeModel().getOwnState() & (LCA_URGENT/*|LCA_SPEEDGAIN*/)) != 0
249  &&
250  (prohibitor->getLaneChangeModel().getOwnState() & (LCA_LEFT | LCA_RIGHT))
251  !=
252  (vehicle->getLaneChangeModel().getOwnState() & (LCA_LEFT | LCA_RIGHT))
253  )
254  ) {
255 
256  // check for position and speed
257  if (prohibitor->getVehicleType().getLengthWithGap() - vehicle->getVehicleType().getLengthWithGap() == 0) {
258  // ok, may be swapped
259  // remove vehicle to swap with
260  MSLane::VehCont::iterator i = find(target->lane->myTmpVehicles.begin(), target->lane->myTmpVehicles.end(), prohibitor);
261  if (i != target->lane->myTmpVehicles.end()) {
262  MSVehicle* bla = *i;
263  assert(bla == prohibitor);
264  target->lane->myTmpVehicles.erase(i);
265  // set this vehicle
266  target->hoppedVeh = vehicle;
267  target->lane->myTmpVehicles.push_front(vehicle);
268  myCandi->hoppedVeh = prohibitor;
269  myCandi->lane->myTmpVehicles.push_front(prohibitor);
270 
271  // leave lane and detectors
274  // patch position and speed
275  SUMOReal p1 = vehicle->getPositionOnLane();
276  vehicle->myState.myPos = prohibitor->myState.myPos;
277  prohibitor->myState.myPos = p1;
278  p1 = vehicle->getSpeed();
279  vehicle->myState.mySpeed = prohibitor->myState.mySpeed;
280  prohibitor->myState.mySpeed = p1;
281  // enter lane and detectors
282  vehicle->enterLaneAtLaneChange(target->lane);
283  prohibitor->enterLaneAtLaneChange(myCandi->lane);
284  // mark lane change
285  vehicle->getLaneChangeModel().changed();
286  vehicle->myLastLaneChangeOffset = 0;
287  prohibitor->getLaneChangeModel().changed();
288  prohibitor->myLastLaneChangeOffset = 0;
289  (myCandi)->dens += prohibitor->getVehicleType().getLengthWithGap();
290  (target)->dens += vehicle->getVehicleType().getLengthWithGap();
291  return true;
292  }
293  }
294  }
295  }
296  // Candidate didn't change lane.
297  myCandi->lane->myTmpVehicles.push_front(veh(myCandi));
298  vehicle->myLastLaneChangeOffset += DELTA_T;
299  (myCandi)->dens += vehicle->getVehicleType().getLengthWithGap();
300  return false;
301 }
302 
303 
304 std::pair<MSVehicle* const, SUMOReal>
306  // get the leading vehicle on the lane to change to
307  MSVehicle* leader = target->lead;
308  if (leader == 0) {
309  MSLane* targetLane = target->lane;
310  MSVehicle* predP = targetLane->getPartialOccupator();
311  if (predP != 0) {
312  return std::pair<MSVehicle*, SUMOReal>(predP, targetLane->getPartialOccupatorEnd() - veh(myCandi)->getPositionOnLane());
313  }
314  const std::vector<MSLane*>& bestLaneConts = veh(myCandi)->getBestLanesContinuation();
315  MSLinkCont::const_iterator link = targetLane->succLinkSec(*veh(myCandi), 1, *targetLane, bestLaneConts);
316  if (targetLane->isLinkEnd(link)) {
317  return std::pair<MSVehicle*, SUMOReal>(static_cast<MSVehicle*>(0), -1);
318  }
319  MSLane* nextLane = (*link)->getLane();
320  if (nextLane == 0) {
321  return std::pair<MSVehicle*, SUMOReal>(static_cast<MSVehicle*>(0), -1);
322  }
323  leader = nextLane->getLastVehicle();
324  if (leader == 0) {
325  return std::pair<MSVehicle*, SUMOReal>(static_cast<MSVehicle*>(0), -1);
326  }
327  SUMOReal gap =
328  leader->getPositionOnLane() - leader->getVehicleType().getLength()
329  +
330  (myCandi->lane->getLength() - veh(myCandi)->getPositionOnLane() - veh(myCandi)->getVehicleType().getMinGap()); // !!! recheck
331  return std::pair<MSVehicle* const, SUMOReal>(leader, MAX2((SUMOReal) 0, gap));
332  } else {
333  MSVehicle* candi = veh(myCandi);
334  SUMOReal gap = leader->getPositionOnLane() - leader->getVehicleType().getLength() - candi->getPositionOnLane() - candi->getVehicleType().getMinGap();
335  return std::pair<MSVehicle* const, SUMOReal>(leader, MAX2((SUMOReal) 0, gap));
336  }
337 }
338 
339 
340 std::pair<MSVehicle* const, SUMOReal>
342  // get the leading vehicle on the lane to change to
343  MSVehicle* neighLead = target->lead;
344  // check whether the hopped vehicle got the leader
345  if (target->hoppedVeh != 0) {
346  SUMOReal hoppedPos = target->hoppedVeh->getPositionOnLane();
347  if (hoppedPos > veh(myCandi)->getPositionOnLane() && (neighLead == 0 || neighLead->getPositionOnLane() > hoppedPos)) {
348  neighLead = target->hoppedVeh;
349  }
350  }
351  if (neighLead == 0) {
352  MSLane* targetLane = target->lane;
353  MSVehicle* predP = targetLane->getPartialOccupator();
354  if (predP != 0) {
355  return std::pair<MSVehicle*, SUMOReal>(predP, targetLane->getPartialOccupatorEnd() - veh(myCandi)->getPositionOnLane() - veh(myCandi)->getVehicleType().getMinGap());
356  }
357  SUMOReal seen = myCandi->lane->getLength() - veh(myCandi)->getPositionOnLane();
358  SUMOReal speed = veh(myCandi)->getSpeed();
360  if (seen > dist) {
361  return std::pair<MSVehicle* const, SUMOReal>(static_cast<MSVehicle*>(0), -1);
362  }
363  const std::vector<MSLane*>& bestLaneConts = veh(myCandi)->getBestLanesContinuation(targetLane);
364  return target->lane->getLeaderOnConsecutive(dist, seen, speed, *veh(myCandi), bestLaneConts);
365  } else {
366  MSVehicle* candi = veh(myCandi);
367  return std::pair<MSVehicle* const, SUMOReal>(neighLead, neighLead->getPositionOnLane() - neighLead->getVehicleType().getLength() - candi->getPositionOnLane() - candi->getVehicleType().getMinGap());
368  }
369 }
370 
371 
372 std::pair<MSVehicle* const, SUMOReal>
374  MSVehicle* neighFollow = veh(target);
375  // check whether the hopped vehicle got the follower
376  if (target->hoppedVeh != 0) {
377  SUMOReal hoppedPos = target->hoppedVeh->getPositionOnLane();
378  if (hoppedPos <= veh(myCandi)->getPositionOnLane() && (neighFollow == 0 || neighFollow->getPositionOnLane() > hoppedPos)) {
379  neighFollow = target->hoppedVeh;
380  }
381  }
382  if (neighFollow == 0) {
383  SUMOReal speed = target->lane->getSpeedLimit();
384  // in order to look back, we'd need the minimum braking ability of vehicles in the net...
385  // we'll assume it to be 4m/s^2
386  // !!!revisit
387  SUMOReal dist = speed * speed / (2.*4.) + SPEED2DIST(speed);
388  dist = MIN2(dist, (SUMOReal) 500.);
389  MSVehicle* candi = veh(myCandi);
390  SUMOReal seen = candi->getPositionOnLane() - candi->getVehicleType().getLength();
391  return target->lane->getFollowerOnConsecutive(dist, seen, candi->getSpeed(), candi->getPositionOnLane() - candi->getVehicleType().getLength(), 4.5);
392  } else {
393  MSVehicle* candi = veh(myCandi);
394  return std::pair<MSVehicle* const, SUMOReal>(neighFollow, candi->getPositionOnLane() - candi->getVehicleType().getLength() - neighFollow->getPositionOnLane() - neighFollow->getVehicleType().getMinGap());
395  }
396 }
397 
398 
399 
400 
401 void
402 MSLaneChanger::updateChanger(bool vehHasChanged) {
403  assert(myCandi->veh != myCandi->lane->myVehicles.rend());
404 
405  // "Push" the vehicles to the back, i.e. follower becomes vehicle,
406  // vehicle becomes leader, and leader becomes predecessor of vehicle,
407  // if it exists.
408  if (!vehHasChanged) {
409  myCandi->lead = veh(myCandi);
410  }
411  myCandi->veh = myCandi->veh + 1;
412 
413  if (veh(myCandi) == 0) {
414  assert(myCandi->follow == 0);
415  // leader already 0.
416  return;
417  }
418  if (myCandi->veh + 1 == myCandi->lane->myVehicles.rend()) {
419  myCandi->follow = 0;
420  } else {
421  myCandi->follow = *(myCandi->veh + 1);
422  }
423  return;
424 }
425 
426 
427 void
429 
430  // Update the lane's vehicle-container.
431  // First: it is bad style to change other classes members, but for
432  // this release, other attempts were too time-consuming. In a next
433  // release we will change from this lane-centered design to a vehicle-
434  // centered. This will solve many problems.
435  // Second: this swap would be faster if vehicle-containers would have
436  // been pointers, but then I had to change too much of the MSLane code.
437  for (ChangerIt ce = myChanger.begin(); ce != myChanger.end(); ++ce) {
438 
439  ce->lane->swapAfterLaneChange(t);
440  }
441 }
442 
443 
446  // Find the vehicle in myChanger with the smallest position. If there
447  // is no vehicle in myChanger (shouldn't happen) , return
448  // myChanger.end().
449  ChangerIt max = myChanger.end();
450  for (ChangerIt ce = myChanger.begin(); ce != myChanger.end(); ++ce) {
451  if (veh(ce) == 0) {
452  continue;
453  }
454  if (max == myChanger.end()) {
455  max = ce;
456  continue;
457  }
458  assert(veh(ce) != 0);
459  assert(veh(max) != 0);
460  if (veh(max)->getPositionOnLane() < veh(ce)->getPositionOnLane()) {
461  max = ce;
462  }
463  }
464  assert(max != myChanger.end());
465  assert(veh(max) != 0);
466  return max;
467 }
468 
469 
470 int
471 MSLaneChanger::change2right(const std::pair<MSVehicle* const, SUMOReal>& leader,
472  const std::pair<MSVehicle* const, SUMOReal>& rLead,
473  const std::pair<MSVehicle* const, SUMOReal>& rFollow,
474  const std::vector<MSVehicle::LaneQ>& preb) const {
475  ChangerIt target = myCandi - 1;
476  int blocked = overlapWithHopped(target)
477  ? target->hoppedVeh->getPositionOnLane() < veh(myCandi)->getPositionOnLane()
480  : 0;
481  // overlap
482  if (rFollow.first != 0 && rFollow.second < 0) {
483  blocked |= (LCA_BLOCKED_BY_RIGHT_FOLLOWER);
484  }
485  if (rLead.first != 0 && rLead.second < 0) {
486  blocked |= (LCA_BLOCKED_BY_RIGHT_LEADER);
487  }
488  // safe back gap
489  if (rFollow.first != 0) {
490  // !!! eigentlich: vsafe braucht die Max. Geschwindigkeit beider Spuren
491  if (rFollow.second < rFollow.first->getCarFollowModel().getSecureGap(rFollow.first->getSpeed(), veh(myCandi)->getSpeed(), veh(myCandi)->getCarFollowModel().getMaxDecel())) {
493  }
494  }
495 
496  // safe front gap
497  if (rLead.first != 0) {
498  // !!! eigentlich: vsafe braucht die Max. Geschwindigkeit beider Spuren
499  if (rLead.second < veh(myCandi)->getCarFollowModel().getSecureGap(veh(myCandi)->getSpeed(), rLead.first->getSpeed(), rLead.first->getCarFollowModel().getMaxDecel())) {
500  blocked |= LCA_BLOCKED_BY_RIGHT_LEADER;
501  }
502  }
503 
504  MSAbstractLaneChangeModel::MSLCMessager msg(leader.first, rLead.first, rFollow.first);
505  return blocked | veh(myCandi)->getLaneChangeModel().wantsChangeToRight(
506  msg, blocked, leader, rLead, rFollow, *(myCandi - 1)->lane, preb, &(myCandi->lastBlocked));
507 }
508 
509 
510 int
511 MSLaneChanger::change2left(const std::pair<MSVehicle* const, SUMOReal>& leader,
512  const std::pair<MSVehicle* const, SUMOReal>& rLead,
513  const std::pair<MSVehicle* const, SUMOReal>& rFollow,
514  const std::vector<MSVehicle::LaneQ>& preb) const {
515  ChangerIt target = myCandi + 1;
516  int blocked = overlapWithHopped(target)
517  ? target->hoppedVeh->getPositionOnLane() < veh(myCandi)->getPositionOnLane()
520  : 0;
521  // overlap
522  if (rFollow.first != 0 && rFollow.second < 0) {
523  blocked |= (LCA_BLOCKED_BY_LEFT_FOLLOWER);
524  }
525  if (rLead.first != 0 && rLead.second < 0) {
526  blocked |= (LCA_BLOCKED_BY_LEFT_LEADER);
527  }
528  // safe back gap
529  if (rFollow.first != 0) {
530  // !!! eigentlich: vsafe braucht die Max. Geschwindigkeit beider Spuren
531  if (rFollow.second < rFollow.first->getCarFollowModel().getSecureGap(rFollow.first->getSpeed(), veh(myCandi)->getSpeed(), veh(myCandi)->getCarFollowModel().getMaxDecel())) {
532  blocked |= LCA_BLOCKED_BY_LEFT_FOLLOWER;
533  }
534  }
535  // safe front gap
536  if (rLead.first != 0) {
537  // !!! eigentlich: vsafe braucht die Max. Geschwindigkeit beider Spuren
538  if (rLead.second < veh(myCandi)->getCarFollowModel().getSecureGap(veh(myCandi)->getSpeed(), rLead.first->getSpeed(), rLead.first->getCarFollowModel().getMaxDecel())) {
539  blocked |= LCA_BLOCKED_BY_LEFT_LEADER;
540  }
541  }
542  MSAbstractLaneChangeModel::MSLCMessager msg(leader.first, rLead.first, rFollow.first);
543  return blocked | veh(myCandi)->getLaneChangeModel().wantsChangeToLeft(
544  msg, blocked, leader, rLead, rFollow, *(myCandi + 1)->lane, preb, &(myCandi->lastBlocked));
545 }
546 
547 
548 
549 
550 /****************************************************************************/
551