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  if ((state1 & (LCA_URGENT)) != 0) {
227  // ... wants to go right
228  target = myCandi - 1;
229  }
230  if ((state2 & (LCA_URGENT)) != 0) {
231  // ... wants to go left
232  target = myCandi + 1;
233  }
234  MSVehicle* prohibitor = target->lead;
235  if (target->hoppedVeh != 0) {
236  SUMOReal hoppedPos = target->hoppedVeh->getPositionOnLane();
237  if (prohibitor == 0 || (hoppedPos > vehicle->getPositionOnLane() && prohibitor->getPositionOnLane() > hoppedPos)) {
238  prohibitor = 0;// !!! vehicles should not jump over more than one lanetarget->hoppedVeh;
239  }
240  }
241  if (prohibitor != 0
242  &&
243  ((prohibitor->getLaneChangeModel().getOwnState() & (LCA_URGENT/*|LCA_SPEEDGAIN*/)) != 0
244  &&
245  (prohibitor->getLaneChangeModel().getOwnState() & (LCA_LEFT | LCA_RIGHT))
246  !=
247  (vehicle->getLaneChangeModel().getOwnState() & (LCA_LEFT | LCA_RIGHT))
248  )
249  ) {
250 
251  // check for position and speed
252  if (prohibitor->getVehicleType().getLengthWithGap() - vehicle->getVehicleType().getLengthWithGap() == 0) {
253  // ok, may be swapped
254  // remove vehicle to swap with
255  MSLane::VehCont::iterator i = find(target->lane->myTmpVehicles.begin(), target->lane->myTmpVehicles.end(), prohibitor);
256  if (i != target->lane->myTmpVehicles.end()) {
257  MSVehicle* bla = *i;
258  assert(bla == prohibitor);
259  target->lane->myTmpVehicles.erase(i);
260  // set this vehicle
261  target->hoppedVeh = vehicle;
262  target->lane->myTmpVehicles.push_front(vehicle);
263  myCandi->hoppedVeh = prohibitor;
264  myCandi->lane->myTmpVehicles.push_front(prohibitor);
265 
266  // leave lane and detectors
269  // patch position and speed
270  SUMOReal p1 = vehicle->getPositionOnLane();
271  vehicle->myState.myPos = prohibitor->myState.myPos;
272  prohibitor->myState.myPos = p1;
273  p1 = vehicle->getSpeed();
274  vehicle->myState.mySpeed = prohibitor->myState.mySpeed;
275  prohibitor->myState.mySpeed = p1;
276  // enter lane and detectors
277  vehicle->enterLaneAtLaneChange(target->lane);
278  prohibitor->enterLaneAtLaneChange(myCandi->lane);
279  // mark lane change
280  vehicle->getLaneChangeModel().changed();
281  vehicle->myLastLaneChangeOffset = 0;
282  prohibitor->getLaneChangeModel().changed();
283  prohibitor->myLastLaneChangeOffset = 0;
284  (myCandi)->dens += prohibitor->getVehicleType().getLengthWithGap();
285  (target)->dens += vehicle->getVehicleType().getLengthWithGap();
286  return true;
287  }
288  }
289  }
290  }
291  // Candidate didn't change lane.
292  myCandi->lane->myTmpVehicles.push_front(veh(myCandi));
293  vehicle->myLastLaneChangeOffset += DELTA_T;
294  (myCandi)->dens += vehicle->getVehicleType().getLengthWithGap();
295  return false;
296 }
297 
298 
299 std::pair<MSVehicle* const, SUMOReal>
301  // get the leading vehicle on the lane to change to
302  MSVehicle* leader = target->lead;
303  if (leader == 0) {
304  MSLane* targetLane = target->lane;
305  MSVehicle* predP = targetLane->getPartialOccupator();
306  if (predP != 0) {
307  return std::pair<MSVehicle*, SUMOReal>(predP, targetLane->getPartialOccupatorEnd() - veh(myCandi)->getPositionOnLane());
308  }
309  const std::vector<MSLane*>& bestLaneConts = veh(myCandi)->getBestLanesContinuation();
310  MSLinkCont::const_iterator link = targetLane->succLinkSec(*veh(myCandi), 1, *targetLane, bestLaneConts);
311  if (targetLane->isLinkEnd(link)) {
312  return std::pair<MSVehicle*, SUMOReal>(static_cast<MSVehicle*>(0), -1);
313  }
314  MSLane* nextLane = (*link)->getLane();
315  if (nextLane == 0) {
316  return std::pair<MSVehicle*, SUMOReal>(static_cast<MSVehicle*>(0), -1);
317  }
318  leader = nextLane->getLastVehicle();
319  if (leader == 0) {
320  return std::pair<MSVehicle*, SUMOReal>(static_cast<MSVehicle*>(0), -1);
321  }
322  SUMOReal gap =
323  leader->getPositionOnLane() - leader->getVehicleType().getLength()
324  +
325  (myCandi->lane->getLength() - veh(myCandi)->getPositionOnLane() - veh(myCandi)->getVehicleType().getMinGap()); // !!! recheck
326  return std::pair<MSVehicle* const, SUMOReal>(leader, MAX2((SUMOReal) 0, gap));
327  } else {
328  MSVehicle* candi = veh(myCandi);
329  SUMOReal gap = leader->getPositionOnLane() - leader->getVehicleType().getLength() - candi->getPositionOnLane() - candi->getVehicleType().getMinGap();
330  return std::pair<MSVehicle* const, SUMOReal>(leader, MAX2((SUMOReal) 0, gap));
331  }
332 }
333 
334 
335 std::pair<MSVehicle* const, SUMOReal>
337  // get the leading vehicle on the lane to change to
338  MSVehicle* neighLead = target->lead;
339  // check whether the hopped vehicle got the leader
340  if (target->hoppedVeh != 0) {
341  SUMOReal hoppedPos = target->hoppedVeh->getPositionOnLane();
342  if (hoppedPos > veh(myCandi)->getPositionOnLane() && (neighLead == 0 || neighLead->getPositionOnLane() > hoppedPos)) {
343  neighLead = target->hoppedVeh;
344  }
345  }
346  if (neighLead == 0) {
347  MSLane* targetLane = target->lane;
348  MSVehicle* predP = targetLane->getPartialOccupator();
349  if (predP != 0) {
350  return std::pair<MSVehicle*, SUMOReal>(predP, targetLane->getPartialOccupatorEnd() - veh(myCandi)->getPositionOnLane() - veh(myCandi)->getVehicleType().getMinGap());
351  }
352  const std::vector<MSLane*>& bestLaneConts = veh(myCandi)->getBestLanesContinuation(myCandi->lane);
353  SUMOReal seen = myCandi->lane->getLength() - veh(myCandi)->getPositionOnLane();
354  SUMOReal speed = veh(myCandi)->getSpeed();
356  if (seen > dist) {
357  return std::pair<MSVehicle* const, SUMOReal>(static_cast<MSVehicle*>(0), -1);
358  }
359  return target->lane->getLeaderOnConsecutive(dist, seen, speed, *veh(myCandi), bestLaneConts);
360  } else {
361  MSVehicle* candi = veh(myCandi);
362  return std::pair<MSVehicle* const, SUMOReal>(neighLead, neighLead->getPositionOnLane() - neighLead->getVehicleType().getLength() - candi->getPositionOnLane() - candi->getVehicleType().getMinGap());
363  }
364 }
365 
366 
367 std::pair<MSVehicle* const, SUMOReal>
369  MSVehicle* neighFollow = veh(target);
370  // check whether the hopped vehicle got the follower
371  if (target->hoppedVeh != 0) {
372  SUMOReal hoppedPos = target->hoppedVeh->getPositionOnLane();
373  if (hoppedPos <= veh(myCandi)->getPositionOnLane() && (neighFollow == 0 || neighFollow->getPositionOnLane() > hoppedPos)) {
374  neighFollow = target->hoppedVeh;
375  }
376  }
377  if (neighFollow == 0) {
378  SUMOReal speed = target->lane->getSpeedLimit();
379  // in order to look back, we'd need the minimum braking ability of vehicles in the net...
380  // we'll assume it to be 4m/s^2
381  // !!!revisit
382  SUMOReal dist = speed * speed / (2.*4.) + SPEED2DIST(speed);
383  dist = MIN2(dist, (SUMOReal) 500.);
384  MSVehicle* candi = veh(myCandi);
385  SUMOReal seen = candi->getPositionOnLane() - candi->getVehicleType().getLength();
386  return target->lane->getFollowerOnConsecutive(dist, seen, candi->getSpeed(), candi->getPositionOnLane() - candi->getVehicleType().getLength(), 4.5);
387  } else {
388  MSVehicle* candi = veh(myCandi);
389  return std::pair<MSVehicle* const, SUMOReal>(neighFollow, candi->getPositionOnLane() - candi->getVehicleType().getLength() - neighFollow->getPositionOnLane() - neighFollow->getVehicleType().getMinGap());
390  }
391 }
392 
393 
394 
395 
396 void
397 MSLaneChanger::updateChanger(bool vehHasChanged) {
398  assert(myCandi->veh != myCandi->lane->myVehicles.rend());
399 
400  // "Push" the vehicles to the back, i.e. follower becomes vehicle,
401  // vehicle becomes leader, and leader becomes predecessor of vehicle,
402  // if it exists.
403  if (!vehHasChanged) {
404  myCandi->lead = veh(myCandi);
405  }
406  myCandi->veh = myCandi->veh + 1;
407 
408  if (veh(myCandi) == 0) {
409  assert(myCandi->follow == 0);
410  // leader already 0.
411  return;
412  }
413  if (myCandi->veh + 1 == myCandi->lane->myVehicles.rend()) {
414  myCandi->follow = 0;
415  } else {
416  myCandi->follow = *(myCandi->veh + 1);
417  }
418  return;
419 }
420 
421 
422 void
424 
425  // Update the lane's vehicle-container.
426  // First: it is bad style to change other classes members, but for
427  // this release, other attempts were too time-consuming. In a next
428  // release we will change from this lane-centered design to a vehicle-
429  // centered. This will solve many problems.
430  // Second: this swap would be faster if vehicle-containers would have
431  // been pointers, but then I had to change too much of the MSLane code.
432  for (ChangerIt ce = myChanger.begin(); ce != myChanger.end(); ++ce) {
433 
434  ce->lane->swapAfterLaneChange(t);
435  }
436 }
437 
438 
441  // Find the vehicle in myChanger with the smallest position. If there
442  // is no vehicle in myChanger (shouldn't happen) , return
443  // myChanger.end().
444  ChangerIt max = myChanger.end();
445  for (ChangerIt ce = myChanger.begin(); ce != myChanger.end(); ++ce) {
446  if (veh(ce) == 0) {
447  continue;
448  }
449  if (max == myChanger.end()) {
450  max = ce;
451  continue;
452  }
453  assert(veh(ce) != 0);
454  assert(veh(max) != 0);
455  if (veh(max)->getPositionOnLane() < veh(ce)->getPositionOnLane()) {
456  max = ce;
457  }
458  }
459  assert(max != myChanger.end());
460  assert(veh(max) != 0);
461  return max;
462 }
463 
464 
465 int
466 MSLaneChanger::change2right(const std::pair<MSVehicle* const, SUMOReal>& leader,
467  const std::pair<MSVehicle* const, SUMOReal>& rLead,
468  const std::pair<MSVehicle* const, SUMOReal>& rFollow,
469  const std::vector<MSVehicle::LaneQ>& preb) const {
470  ChangerIt target = myCandi - 1;
471  int blocked = overlapWithHopped(target)
472  ? target->hoppedVeh->getPositionOnLane() < veh(myCandi)->getPositionOnLane()
475  : 0;
476  // overlap
477  if (rFollow.first != 0 && rFollow.second < 0) {
478  blocked |= (LCA_BLOCKED_BY_RIGHT_FOLLOWER);
479  }
480  if (rLead.first != 0 && rLead.second < 0) {
481  blocked |= (LCA_BLOCKED_BY_RIGHT_LEADER);
482  }
483  // safe back gap
484  if (rFollow.first != 0) {
485  // !!! eigentlich: vsafe braucht die Max. Geschwindigkeit beider Spuren
486  if (rFollow.second < rFollow.first->getCarFollowModel().getSecureGap(rFollow.first->getSpeed(), veh(myCandi)->getSpeed(), veh(myCandi)->getCarFollowModel().getMaxDecel())) {
488  }
489  }
490 
491  // safe front gap
492  if (rLead.first != 0) {
493  // !!! eigentlich: vsafe braucht die Max. Geschwindigkeit beider Spuren
494  if (rLead.second < veh(myCandi)->getCarFollowModel().getSecureGap(veh(myCandi)->getSpeed(), rLead.first->getSpeed(), rLead.first->getCarFollowModel().getMaxDecel())) {
495  blocked |= LCA_BLOCKED_BY_RIGHT_LEADER;
496  }
497  }
498 
499  MSAbstractLaneChangeModel::MSLCMessager msg(leader.first, rLead.first, rFollow.first);
500  return blocked | veh(myCandi)->getLaneChangeModel().wantsChangeToRight(
501  msg, blocked, leader, rLead, rFollow, *(myCandi - 1)->lane, preb, &(myCandi->lastBlocked));
502 }
503 
504 
505 int
506 MSLaneChanger::change2left(const std::pair<MSVehicle* const, SUMOReal>& leader,
507  const std::pair<MSVehicle* const, SUMOReal>& rLead,
508  const std::pair<MSVehicle* const, SUMOReal>& rFollow,
509  const std::vector<MSVehicle::LaneQ>& preb) const {
510  ChangerIt target = myCandi + 1;
511  int blocked = overlapWithHopped(target)
512  ? target->hoppedVeh->getPositionOnLane() < veh(myCandi)->getPositionOnLane()
515  : 0;
516  // overlap
517  if (rFollow.first != 0 && rFollow.second < 0) {
518  blocked |= (LCA_BLOCKED_BY_LEFT_FOLLOWER);
519  }
520  if (rLead.first != 0 && rLead.second < 0) {
521  blocked |= (LCA_BLOCKED_BY_LEFT_LEADER);
522  }
523  // safe back gap
524  if (rFollow.first != 0) {
525  // !!! eigentlich: vsafe braucht die Max. Geschwindigkeit beider Spuren
526  if (rFollow.second < rFollow.first->getCarFollowModel().getSecureGap(rFollow.first->getSpeed(), veh(myCandi)->getSpeed(), veh(myCandi)->getCarFollowModel().getMaxDecel())) {
527  blocked |= LCA_BLOCKED_BY_LEFT_FOLLOWER;
528  }
529  }
530  // safe front gap
531  if (rLead.first != 0) {
532  // !!! eigentlich: vsafe braucht die Max. Geschwindigkeit beider Spuren
533  if (rLead.second < veh(myCandi)->getCarFollowModel().getSecureGap(veh(myCandi)->getSpeed(), rLead.first->getSpeed(), rLead.first->getCarFollowModel().getMaxDecel())) {
534  blocked |= LCA_BLOCKED_BY_LEFT_LEADER;
535  }
536  }
537  MSAbstractLaneChangeModel::MSLCMessager msg(leader.first, rLead.first, rFollow.first);
538  return blocked | veh(myCandi)->getLaneChangeModel().wantsChangeToLeft(
539  msg, blocked, leader, rLead, rFollow, *(myCandi + 1)->lane, preb, &(myCandi->lastBlocked));
540 }
541 
542 
543 
544 
545 /****************************************************************************/
546