SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
MSLCM_LC2013.cpp
Go to the documentation of this file.
1 /****************************************************************************/
11 // A lane change model developed by D. Krajzewicz, J. Erdmann et al. between 2004 and 2013
12 /****************************************************************************/
13 // SUMO, Simulation of Urban MObility; see http://sumo-sim.org/
14 // Copyright (C) 2001-2014 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 // ===========================================================================
27 // included modules
28 // ===========================================================================
29 #ifdef _MSC_VER
30 #include <windows_config.h>
31 #else
32 #include <config.h>
33 #endif
34 
35 #include <iostream>
37 #include "MSEdge.h"
38 #include "MSLane.h"
39 #include "MSNet.h"
40 #include "MSLCM_LC2013.h"
41 
42 #ifdef CHECK_MEMORY_LEAKS
43 #include <foreign/nvwa/debug_new.h>
44 #endif // CHECK_MEMORY_LEAKS
45 
46 //#define DEBUG_VEHICLE_GUI_SELECTION 1
47 #ifdef DEBUG_VEHICLE_GUI_SELECTION
49 #include <guisim/GUIVehicle.h>
50 #include <guisim/GUILane.h>
51 #endif
52 
53 
54 
55 // ===========================================================================
56 // variable definitions
57 // ===========================================================================
58 // 80km/h will be the threshold for dividing between long/short foresight
59 #define LOOK_FORWARD_SPEED_DIVIDER (SUMOReal)14.
60 
61 #define LOOK_FORWARD_RIGHT (SUMOReal)10.
62 #define LOOK_FORWARD_LEFT (SUMOReal)20.
63 
64 #define JAM_FACTOR (SUMOReal)1.
65 
66 #define LCA_RIGHT_IMPATIENCE (SUMOReal)-1.
67 #define CUT_IN_LEFT_SPEED_THRESHOLD (SUMOReal)27.
68 
69 #define LOOK_AHEAD_MIN_SPEED (SUMOReal)0.0
70 #define LOOK_AHEAD_SPEED_MEMORY (SUMOReal)0.9
71 #define LOOK_AHEAD_SPEED_DECREMENT 6.
72 
73 #define HELP_DECEL_FACTOR (SUMOReal)1.0
74 
75 #define HELP_OVERTAKE (SUMOReal)(10.0 / 3.6)
76 #define MIN_FALLBEHIND (SUMOReal)(14.0 / 3.6)
77 
78 #define KEEP_RIGHT_HEADWAY (SUMOReal)2.0
79 
80 #define URGENCY (SUMOReal)2.0
81 
82 #define ROUNDABOUT_DIST_BONUS (SUMOReal)80.0
83 
84 #define CHANGE_PROB_THRESHOLD_RIGHT (SUMOReal)2.0
85 #define CHANGE_PROB_THRESHOLD_LEFT (SUMOReal)0.2
86 #define KEEP_RIGHT_TIME (SUMOReal)5.0 // the number of seconds after which a vehicle should move to the right lane
87 #define KEEP_RIGHT_ACCEPTANCE (SUMOReal)2.0 // calibration factor for determining the desire to keep right
88 
89 #define OVERTAKE_RIGHT_FORBIDDEN true // This holds true for german traffic but should be made configurable to model american traffic
90 
91 
92 
93 // ===========================================================================
94 // member method definitions
95 // ===========================================================================
98  mySpeedGainProbability(0),
99  myKeepRightProbability(0),
100  myLeadingBlockerLength(0),
101  myLeftSpace(0),
102  myLookAheadSpeed(LOOK_AHEAD_MIN_SPEED)
103 {}
104 
106  changed();
107 }
108 
109 
110 int
112  int laneOffset,
114  int blocked,
115  const std::pair<MSVehicle*, SUMOReal>& leader,
116  const std::pair<MSVehicle*, SUMOReal>& neighLead,
117  const std::pair<MSVehicle*, SUMOReal>& neighFollow,
118  const MSLane& neighLane,
119  const std::vector<MSVehicle::LaneQ>& preb,
120  MSVehicle** lastBlocked,
121  MSVehicle** firstBlocked) {
122  const int result = _wantsChange(laneOffset, msgPass, blocked, leader, neighLead, neighFollow, neighLane, preb, lastBlocked, firstBlocked);
123  return result;
124 }
125 
126 
127 SUMOReal
128 MSLCM_LC2013::patchSpeed(const SUMOReal min, const SUMOReal wanted, const SUMOReal max, const MSCFModel& cfModel) {
129  const SUMOReal newSpeed = _patchSpeed(min, wanted, max, cfModel);
130  return newSpeed;
131 }
132 
133 
134 SUMOReal
135 MSLCM_LC2013::_patchSpeed(const SUMOReal min, const SUMOReal wanted, const SUMOReal max, const MSCFModel& cfModel) {
136  int state = myOwnState;
137 
138  // letting vehicles merge in at the end of the lane in case of counter-lane change, step#2
139  SUMOReal MAGIC_offset = 1.;
140  // if we want to change and have a blocking leader and there is enough room for him in front of us
141  if (myLeadingBlockerLength != 0) {
143  if (space > 0) {
144  // compute speed for decelerating towards a place which allows the blocking leader to merge in in front
145  SUMOReal safe = cfModel.stopSpeed(&myVehicle, myVehicle.getSpeed(), space);
146  // if we are approaching this place
147  if (safe < wanted) {
148  // return this speed as the speed to use
149  return MAX2(min, safe);
150  }
151  }
152  }
153 
154  SUMOReal nVSafe = wanted;
155  bool gotOne = false;
156  for (std::vector<SUMOReal>::const_iterator i = myVSafes.begin(); i != myVSafes.end(); ++i) {
157  SUMOReal v = (*i);
158  if (v >= min && v <= max) {
159  nVSafe = MIN2(v, nVSafe);
160  gotOne = true;
161  } else {
162  }
163  }
164 
165  if (gotOne && !myDontBrake) {
166  return nVSafe;
167  }
168 
169  // check whether the vehicle is blocked
170  if ((state & LCA_WANTS_LANECHANGE) != 0 && (state & LCA_BLOCKED) != 0) {
171  if ((state & LCA_STRATEGIC) != 0) {
172  // necessary decelerations are controlled via vSafe. If there are
173  // none it means we should speed up
174  return (max + wanted) / (SUMOReal) 2.0;
175  } else if ((state & LCA_COOPERATIVE) != 0) {
176  // only minor adjustments in speed should be done
177  if ((state & LCA_BLOCKED_BY_LEADER) != 0) {
178  return (min + wanted) / (SUMOReal) 2.0;
179  }
180  if ((state & LCA_BLOCKED_BY_FOLLOWER) != 0) {
181  return (max + wanted) / (SUMOReal) 2.0;
182  }
183  }
184  }
185 
186  // accelerate if being a blocking leader or blocking follower not able to brake
187  // (and does not have to change lanes)
188  if ((state & LCA_AMBLOCKINGLEADER) != 0) {
189  return (max + wanted) / (SUMOReal) 2.0;
190  }
191 
192  if ((state & LCA_AMBLOCKINGFOLLOWER_DONTBRAKE) != 0) {
193  }
194  if (myVehicle.getLane()->getEdge().getLanes().size() == 1) {
195  // remove chaning information if on a road with a single lane
196  changed();
197  }
198  return wanted;
199 }
200 
201 
202 void*
203 MSLCM_LC2013::inform(void* info, MSVehicle* /* sender */) {
204  Info* pinfo = (Info*) info;
205  myVSafes.push_back(pinfo->first);
206  myOwnState |= pinfo->second;
207  delete pinfo;
208  return (void*) true;
209 }
210 
211 
212 SUMOReal
214  int blocked,
215  int dir,
216  const std::pair<MSVehicle*, SUMOReal>& neighLead,
217  SUMOReal remainingSeconds) {
218  SUMOReal plannedSpeed = MIN2(myVehicle.getSpeed(),
220  for (std::vector<SUMOReal>::const_iterator i = myVSafes.begin(); i != myVSafes.end(); ++i) {
221  SUMOReal v = (*i);
223  plannedSpeed = MIN2(plannedSpeed, v);
224  }
225  }
226  if ((blocked & LCA_BLOCKED_BY_LEADER) != 0) {
227  assert(neighLead.first != 0);
228  MSVehicle* nv = neighLead.first;
229  // decide whether we want to overtake the leader or follow it
230  const SUMOReal dv = plannedSpeed - nv->getSpeed();
231  const SUMOReal overtakeDist = (neighLead.second // drive to back of follower
232  + nv->getVehicleType().getLengthWithGap() // drive to front of follower
233  + myVehicle.getVehicleType().getLength() // ego back reaches follower front
234  + nv->getCarFollowModel().getSecureGap( // save gap to follower
236 
237  if (dv < 0
238  // overtaking on the right on an uncongested highway is forbidden (noOvertakeLCLeft)
239  || (dir == LCA_MLEFT && !myVehicle.congested())
240  // not enough space to overtake?
241  || myLeftSpace < overtakeDist
242  // not enough time to overtake?
243  || dv * remainingSeconds < overtakeDist) {
244  // cannot overtake
245  msgPass.informNeighLeader(new Info(-1, dir | LCA_AMBLOCKINGLEADER), &myVehicle);
246  // slow down smoothly to follow leader
247  const SUMOReal targetSpeed = myCarFollowModel.followSpeed(
248  &myVehicle, myVehicle.getSpeed(), neighLead.second, nv->getSpeed(), nv->getCarFollowModel().getMaxDecel());
249  if (targetSpeed < myVehicle.getSpeed()) {
250  // slow down smoothly to follow leader
252  MAX2(MIN_FALLBEHIND, (myVehicle.getSpeed() - targetSpeed) / remainingSeconds)));
253  const SUMOReal nextSpeed = MIN2(plannedSpeed, myVehicle.getSpeed() - decel);
254  myVSafes.push_back(nextSpeed);
255  return nextSpeed;
256  } else {
257  // leader is fast enough anyway
258  myVSafes.push_back(targetSpeed);
259  return plannedSpeed;
260  }
261  } else {
262  // overtaking, leader should not accelerate
263  msgPass.informNeighLeader(new Info(nv->getSpeed(), dir | LCA_AMBLOCKINGLEADER), &myVehicle);
264  return -1;
265  }
266  } else if (neighLead.first != 0) { // (remainUnblocked)
267  // we are not blocked now. make sure we stay far enough from the leader
268  MSVehicle* nv = neighLead.first;
269  const SUMOReal nextNVSpeed = nv->getSpeed() - HELP_OVERTAKE; // conservative
270  const SUMOReal dv = SPEED2DIST(myVehicle.getSpeed() - nextNVSpeed);
271  const SUMOReal targetSpeed = myCarFollowModel.followSpeed(
272  &myVehicle, myVehicle.getSpeed(), neighLead.second - dv, nextNVSpeed, nv->getCarFollowModel().getMaxDecel());
273  myVSafes.push_back(targetSpeed);
274  return MIN2(targetSpeed, plannedSpeed);
275  } else {
276  // not overtaking
277  return plannedSpeed;
278  }
279 }
280 
281 
282 void
284  int blocked,
285  int dir,
286  const std::pair<MSVehicle*, SUMOReal>& neighFollow,
287  SUMOReal remainingSeconds,
288  SUMOReal plannedSpeed) {
289  if ((blocked & LCA_BLOCKED_BY_FOLLOWER) != 0) {
290  assert(neighFollow.first != 0);
291  MSVehicle* nv = neighFollow.first;
292 
293  // are we fast enough to cut in without any help?
294  if (plannedSpeed - nv->getSpeed() >= HELP_OVERTAKE) {
295  const SUMOReal neededGap = nv->getCarFollowModel().getSecureGap(nv->getSpeed(), plannedSpeed, myVehicle.getCarFollowModel().getMaxDecel());
296  if ((neededGap - neighFollow.second) / remainingSeconds < (plannedSpeed - nv->getSpeed())) {
297  // follower might even accelerate but not to much
298  msgPass.informNeighFollower(new Info(plannedSpeed - HELP_OVERTAKE, dir | LCA_AMBLOCKINGFOLLOWER), &myVehicle);
299  return;
300  }
301  }
302  // decide whether we will request help to cut in before the follower or allow to be overtaken
303 
304  // PARAMETERS
305  // assume other vehicle will assume the equivalent of 1 second of
306  // maximum deceleration to help us (will probably be spread over
307  // multiple seconds)
308  // -----------
309  const SUMOReal helpDecel = nv->getCarFollowModel().getMaxDecel() * HELP_DECEL_FACTOR ;
310 
311  // change in the gap between ego and blocker over 1 second (not STEP!)
312  const SUMOReal neighNewSpeed = MAX2((SUMOReal)0, nv->getSpeed() - ACCEL2SPEED(helpDecel));
313  const SUMOReal neighNewSpeed1s = MAX2((SUMOReal)0, nv->getSpeed() - helpDecel);
314  const SUMOReal dv = plannedSpeed - neighNewSpeed1s;
315  // new gap between follower and self in case the follower does brake for 1s
316  const SUMOReal decelGap = neighFollow.second + dv;
317  const SUMOReal secureGap = nv->getCarFollowModel().getSecureGap(neighNewSpeed1s, plannedSpeed, myVehicle.getCarFollowModel().getMaxDecel());
318  if (decelGap > 0 && decelGap >= secureGap) {
319  // if the blocking neighbor brakes it could actually help
320  // how hard does it actually need to be?
321  const SUMOReal vsafe = MAX2(neighNewSpeed, nv->getCarFollowModel().followSpeed(
322  nv, nv->getSpeed(), neighFollow.second, plannedSpeed, myVehicle.getCarFollowModel().getMaxDecel()));
323  msgPass.informNeighFollower(new Info(vsafe, dir | LCA_AMBLOCKINGFOLLOWER), &myVehicle);
324  } else if (dv > 0 && dv * remainingSeconds > (secureGap - decelGap + POSITION_EPS)) {
325  // decelerating once is sufficient to open up a large enough gap in time
326  msgPass.informNeighFollower(new Info(neighNewSpeed, dir | LCA_AMBLOCKINGFOLLOWER), &myVehicle);
327  } else {
329  //if (dir == LCA_MRIGHT && myVehicle.getWaitingSeconds() > LCA_RIGHT_IMPATIENCE &&
330  // nv->getSpeed() > myVehicle.getSpeed()) {
331  if (nv->getSpeed() > myVehicle.getSpeed() &&
333  || (dir == LCA_MLEFT && plannedSpeed > CUT_IN_LEFT_SPEED_THRESHOLD) // VARIANT_22 (slowDownLeft)
334  )) {
335  // let the follower slow down to increase the likelyhood that later vehicles will be slow enough to help
336  // follower should still be fast enough to open a gap
337  vhelp = MAX2(neighNewSpeed, myVehicle.getSpeed() + HELP_OVERTAKE);
338  if ((nv->getSpeed() - myVehicle.getSpeed()) / helpDecel < remainingSeconds) {
339  msgPass.informNeighFollower(new Info(vhelp, dir | LCA_AMBLOCKINGFOLLOWER), &myVehicle);
340  return;
341  }
342  }
343  msgPass.informNeighFollower(new Info(vhelp, dir | LCA_AMBLOCKINGFOLLOWER), &myVehicle);
344  // this follower is supposed to overtake us. slow down smoothly to allow this
345  const SUMOReal overtakeDist = (neighFollow.second // follower reaches ego back
346  + myVehicle.getVehicleType().getLengthWithGap() // follower reaches ego front
347  + nv->getVehicleType().getLength() // follower back at ego front
348  + myVehicle.getCarFollowModel().getSecureGap( // follower has safe dist to ego
349  plannedSpeed, vhelp, nv->getCarFollowModel().getMaxDecel()));
350  // speed difference to create a sufficiently large gap
351  const SUMOReal needDV = overtakeDist / remainingSeconds;
352  // make sure the deceleration is not to strong
353  myVSafes.push_back(MAX2(vhelp - needDV, myVehicle.getSpeed() - ACCEL2SPEED(myVehicle.getCarFollowModel().getMaxDecel())));
354  }
355  }
356 }
357 
358 
359 void
361  myOwnState = 0;
363  myLeftSpace = 0;
364  myVSafes.clear();
365  myDontBrake = false;
366  // truncate to work around numerical instability between different builds
367  mySpeedGainProbability = ceil(mySpeedGainProbability * 100000.0) * 0.00001;
368  myKeepRightProbability = ceil(myKeepRightProbability * 100000.0) * 0.00001;
369 }
370 
371 
372 void
374  myOwnState = 0;
378  if (myVehicle.getBestLaneOffset() == 0) {
379  // if we are not yet on our best lane there might still be unseen blockers
380  // (during patchSpeed)
382  myLeftSpace = 0;
383  }
385  myVSafes.clear();
386  myDontBrake = false;
387 }
388 
389 
390 int
392  int laneOffset,
394  int blocked,
395  const std::pair<MSVehicle*, SUMOReal>& leader,
396  const std::pair<MSVehicle*, SUMOReal>& neighLead,
397  const std::pair<MSVehicle*, SUMOReal>& neighFollow,
398  const MSLane& neighLane,
399  const std::vector<MSVehicle::LaneQ>& preb,
400  MSVehicle** lastBlocked,
401  MSVehicle** firstBlocked) {
402  assert(laneOffset == 1 || laneOffset == -1);
403  const SUMOTime currentTime = MSNet::getInstance()->getCurrentTimeStep();
404  // compute bestLaneOffset
405  MSVehicle::LaneQ curr, neigh, best;
406  int bestLaneOffset = 0;
407  SUMOReal currentDist = 0;
408  SUMOReal neighDist = 0;
409  int currIdx = 0;
410  MSLane* prebLane = myVehicle.getLane();
411  if (prebLane->getEdge().getPurpose() == MSEdge::EDGEFUNCTION_INTERNAL) {
412  // internal edges are not kept inside the bestLanes structure
413  prebLane = prebLane->getLinkCont()[0]->getLane();
414  }
415  for (int p = 0; p < (int) preb.size(); ++p) {
416  if (preb[p].lane == prebLane && p + laneOffset >= 0) {
417  assert(p + laneOffset < (int)preb.size());
418  curr = preb[p];
419  neigh = preb[p + laneOffset];
420  currentDist = curr.length;
421  neighDist = neigh.length;
422  bestLaneOffset = curr.bestLaneOffset;
423  if (bestLaneOffset == 0 && preb[p + laneOffset].bestLaneOffset == 0) {
424  bestLaneOffset = laneOffset;
425  }
426  best = preb[p + bestLaneOffset];
427  currIdx = p;
428  break;
429  }
430  }
431  // direction specific constants
432  const bool right = (laneOffset == -1);
433  const int lca = (right ? LCA_RIGHT : LCA_LEFT);
434  const int myLca = (right ? LCA_MRIGHT : LCA_MLEFT);
435  const int lcaCounter = (right ? LCA_LEFT : LCA_RIGHT);
436  const bool changeToBest = (right && bestLaneOffset < 0) || (!right && bestLaneOffset > 0);
437  // keep information about being a leader/follower
438  int ret = (myOwnState & 0xffff0000);
439 
440  ret = slowDownForBlocked(lastBlocked, ret);
441  if (lastBlocked != firstBlocked) {
442  ret = slowDownForBlocked(firstBlocked, ret);
443  }
444 
445 
446  // we try to estimate the distance which is necessary to get on a lane
447  // we have to get on in order to keep our route
448  // we assume we need something that depends on our velocity
449  // and compare this with the free space on our wished lane
450  //
451  // if the free space is somehow less than the space we need, we should
452  // definitely try to get to the desired lane
453  //
454  // this rule forces our vehicle to change the lane if a lane changing is necessary soon
455  // lookAheadDistance:
456  // we do not want the lookahead distance to change all the time so we discrectize the speed a bit
457 
460  } else {
463  }
465  laDist += myVehicle.getVehicleType().getLengthWithGap() * (SUMOReal) 2.;
466  // free space that is available for changing
467  //const SUMOReal neighSpeed = (neighLead.first != 0 ? neighLead.first->getSpeed() :
468  // neighFollow.first != 0 ? neighFollow.first->getSpeed() :
469  // best.lane->getSpeedLimit());
470  // @note: while this lets vehicles change earlier into the correct direction
471  // it also makes the vehicles more "selfish" and prevents changes which are necessary to help others
472 
473  int roundaboutEdgesAhead = 0;
474  for (std::vector<MSLane*>::iterator it = curr.bestContinuations.begin(); it != curr.bestContinuations.end(); ++it) {
475  if ((*it) != 0 && (*it)->getEdge().isRoundabout()) {
476  roundaboutEdgesAhead += 1;
477  } else if (roundaboutEdgesAhead > 0) {
478  // only check the next roundabout
479  break;
480  }
481  }
482  int roundaboutEdgesAheadNeigh = 0;
483  for (std::vector<MSLane*>::iterator it = neigh.bestContinuations.begin(); it != neigh.bestContinuations.end(); ++it) {
484  if ((*it) != 0 && (*it)->getEdge().isRoundabout()) {
485  roundaboutEdgesAheadNeigh += 1;
486  } else if (roundaboutEdgesAheadNeigh > 0) {
487  // only check the next roundabout
488  break;
489  }
490  }
491  if (roundaboutEdgesAhead > 1) {
492  currentDist += roundaboutEdgesAhead * ROUNDABOUT_DIST_BONUS;
493  neighDist += roundaboutEdgesAheadNeigh * ROUNDABOUT_DIST_BONUS;
494  }
495 
496  const SUMOReal usableDist = (currentDist - myVehicle.getPositionOnLane() - best.occupation * JAM_FACTOR);
497  const SUMOReal maxJam = MAX2(preb[currIdx + laneOffset].occupation, preb[currIdx].occupation);
498  const SUMOReal neighLeftPlace = MAX2((SUMOReal) 0, neighDist - myVehicle.getPositionOnLane() - maxJam);
499 
500  if (changeToBest && bestLaneOffset == curr.bestLaneOffset
501  && currentDistDisallows(usableDist, bestLaneOffset, laDist)) {
503  ret = ret | lca | LCA_STRATEGIC | LCA_URGENT;
504  } else {
505 
506  if (OVERTAKE_RIGHT_FORBIDDEN && !right && !myVehicle.congested() && neighLead.first != 0) {
507  // check for slower leader on the left. we should not overtake but
508  // rather move left ourselves (unless congested)
509  MSVehicle* nv = neighLead.first;
510  if (nv->getSpeed() < myVehicle.getSpeed()) {
512  &myVehicle, myVehicle.getSpeed(), neighLead.second, nv->getSpeed(), nv->getCarFollowModel().getMaxDecel()));
513  if (nv->getSpeed() + 5 / 3.6 < myVehicle.getSpeed()) {
515  }
516  }
517  }
518 
519  if (!changeToBest && (currentDistDisallows(neighLeftPlace, abs(bestLaneOffset) + 2, laDist))) {
520  // the opposite lane-changing direction should be done than the one examined herein
521  // we'll check whether we assume we could change anyhow and get back in time...
522  //
523  // this rule prevents the vehicle from moving in opposite direction of the best lane
524  // unless the way till the end where the vehicle has to be on the best lane
525  // is long enough
526  ret = ret | LCA_STAY | LCA_STRATEGIC;
527  } else if (bestLaneOffset == 0 && (neighLeftPlace * 2. < laDist)) {
528  // the current lane is the best and a lane-changing would cause a situation
529  // of which we assume we will not be able to return to the lane we have to be on.
530  // this rule prevents the vehicle from leaving the current, best lane when it is
531  // close to this lane's end
532  ret = ret | LCA_STAY | LCA_STRATEGIC;
533  }
534  }
535  // check for overriding TraCI requests
537  if ((ret & lcaCounter) != 0) {
538  // we are not interested in traci requests for the opposite direction here
539  ret &= ~(LCA_TRACI | lcaCounter | LCA_URGENT);
540  }
541 
542  if ((ret & LCA_STAY) != 0) {
543  return ret;
544  }
545  if ((ret & LCA_URGENT) != 0) {
546  // prepare urgent lane change maneuver
547  // save the left space
548  myLeftSpace = currentDist - myVehicle.getPositionOnLane();
549  if (changeToBest && abs(bestLaneOffset) > 1) {
550  // there might be a vehicle which needs to counter-lane-change one lane further and we cannot see it yet
551  myLeadingBlockerLength = MAX2((SUMOReal)(right ? 20.0 : 40.0), myLeadingBlockerLength);
552  }
553 
554  // letting vehicles merge in at the end of the lane in case of counter-lane change, step#1
555  // if there is a leader and he wants to change to the opposite direction
556  saveBlockerLength(neighLead.first, lcaCounter);
557  if (*firstBlocked != neighLead.first) {
558  saveBlockerLength(*firstBlocked, lcaCounter);
559  }
560 
561  const SUMOReal remainingSeconds = ((ret & LCA_TRACI) == 0 ?
562  MAX2((SUMOReal)STEPS2TIME(TS), myLeftSpace / myLookAheadSpeed / abs(bestLaneOffset) / URGENCY) :
564  const SUMOReal plannedSpeed = informLeader(msgPass, blocked, myLca, neighLead, remainingSeconds);
565  if (plannedSpeed >= 0) {
566  // maybe we need to deal with a blocking follower
567  informFollower(msgPass, blocked, myLca, neighFollow, remainingSeconds, plannedSpeed);
568  }
569 
570  return ret;
571  }
572 
573  if (roundaboutEdgesAhead > 1) {
574  // try to use the inner lanes of a roundabout to increase throughput
575  // unless we are approaching the exit
576  if (lca == LCA_LEFT) {
577  return ret | lca | LCA_COOPERATIVE;
578  } else {
579  return ret | LCA_STAY | LCA_COOPERATIVE;
580  }
581  }
582 
583  // let's also regard the case where the vehicle is driving on a highway...
584  // in this case, we do not want to get to the dead-end of an on-ramp
585  if (right) {
586  if (bestLaneOffset == 0 && myVehicle.getLane()->getVehicleMaxSpeed(&myVehicle) > 80. / 3.6 && myLookAheadSpeed > SUMO_const_haltingSpeed) {
587  return ret | LCA_STAY | LCA_STRATEGIC;
588  }
589  }
590  // --------
591 
592  // -------- make place on current lane if blocking follower
593  //if (amBlockingFollowerPlusNB()) {
594  // std::cout << myVehicle.getID() << ", " << currentDistAllows(neighDist, bestLaneOffset, laDist)
595  // << " neighDist=" << neighDist
596  // << " currentDist=" << currentDist
597  // << "\n";
598  //}
600  && (changeToBest || currentDistAllows(neighDist, abs(bestLaneOffset) + 1, laDist))) {
601 
602  return ret | lca | LCA_COOPERATIVE | LCA_URGENT ;//| LCA_CHANGE_TO_HELP;
603  }
604 
605  // --------
606 
607 
610  //if ((blocked & LCA_BLOCKED) != 0) {
611  // return ret;
612  //}
614 
615  // -------- higher speed
616  //if ((congested(neighLead.first) && neighLead.second < 20) || predInteraction(leader.first)) { //!!!
617  // return ret;
618  //}
620  SUMOReal neighLaneVSafe = neighLane.getVehicleMaxSpeed(&myVehicle);
621  if (neighLead.first == 0) {
622  neighLaneVSafe = MIN2(neighLaneVSafe, myCarFollowModel.followSpeed(&myVehicle, myVehicle.getSpeed(), neighDist, 0, 0));
623  } else {
624  // @todo: what if leader is below safe gap?!!!
625  neighLaneVSafe = MIN2(neighLaneVSafe, myCarFollowModel.followSpeed(
626  &myVehicle, myVehicle.getSpeed(), neighLead.second, neighLead.first->getSpeed(), neighLead.first->getCarFollowModel().getMaxDecel()));
627  }
628  if (leader.first == 0) {
629  thisLaneVSafe = MIN2(thisLaneVSafe, myCarFollowModel.followSpeed(&myVehicle, myVehicle.getSpeed(), currentDist, 0, 0));
630  } else {
631  // @todo: what if leader is below safe gap?!!!
632  thisLaneVSafe = MIN2(thisLaneVSafe, myCarFollowModel.followSpeed(&myVehicle, myVehicle.getSpeed(), leader.second, leader.first->getSpeed(), leader.first->getCarFollowModel().getMaxDecel()));
633  }
634 
635  thisLaneVSafe = MIN3(thisLaneVSafe, myVehicle.getVehicleType().getMaxSpeed(), myVehicle.getLane()->getVehicleMaxSpeed(&myVehicle));
636  neighLaneVSafe = MIN3(neighLaneVSafe, myVehicle.getVehicleType().getMaxSpeed(), neighLane.getVehicleMaxSpeed(&myVehicle));
637 
638  const SUMOReal relativeGain = (neighLaneVSafe - thisLaneVSafe) / neighLaneVSafe;
639  if (right) {
640  // ONLY FOR CHANGING TO THE RIGHT
641  if (thisLaneVSafe - 5 / 3.6 > neighLaneVSafe) {
642  // ok, the current lane is faster than the right one...
643  if (mySpeedGainProbability < 0) {
644  mySpeedGainProbability /= 2.0;
645  //myKeepRightProbability /= 2.0;
646  }
647  } else {
648  // ok, the current lane is not faster than the right one
649  mySpeedGainProbability -= relativeGain;
650 
651  // honor the obligation to keep right (Rechtsfahrgebot)
652  // XXX consider fast approaching followers on the current lane
653  //const SUMOReal vMax = myLookAheadSpeed;
655  const SUMOReal acceptanceTime = KEEP_RIGHT_ACCEPTANCE * vMax * MAX2((SUMOReal)1, myVehicle.getSpeed()) / myVehicle.getLane()->getSpeedLimit();
656  SUMOReal fullSpeedGap = MAX2((SUMOReal)0, neighDist - myVehicle.getCarFollowModel().brakeGap(vMax));
657  SUMOReal fullSpeedDrivingSeconds = MIN2(acceptanceTime, fullSpeedGap / vMax);
658  if (neighLead.first != 0 && neighLead.first->getSpeed() < vMax) {
659  fullSpeedGap = MAX2((SUMOReal)0, MIN2(fullSpeedGap,
660  neighLead.second - myVehicle.getCarFollowModel().getSecureGap(
661  vMax, neighLead.first->getSpeed(), neighLead.first->getCarFollowModel().getMaxDecel())));
662  fullSpeedDrivingSeconds = MIN2(fullSpeedDrivingSeconds, fullSpeedGap / (vMax - neighLead.first->getSpeed()));
663  }
664  const SUMOReal deltaProb = (CHANGE_PROB_THRESHOLD_RIGHT
666  * (fullSpeedDrivingSeconds / acceptanceTime) / KEEP_RIGHT_TIME);
667  myKeepRightProbability -= deltaProb;
668 
669  if (gDebugFlag2) {
670  std::cout << STEPS2TIME(currentTime)
671  << " veh=" << myVehicle.getID()
672  << " vMax=" << vMax
673  << " neighDist=" << neighDist
674  << " brakeGap=" << myVehicle.getCarFollowModel().brakeGap(myVehicle.getSpeed())
675  << " leaderSpeed=" << (neighLead.first == 0 ? -1 : neighLead.first->getSpeed())
676  << " secGap=" << (neighLead.first == 0 ? -1 : myVehicle.getCarFollowModel().getSecureGap(
677  myVehicle.getSpeed(), neighLead.first->getSpeed(), neighLead.first->getCarFollowModel().getMaxDecel()))
678  << " acceptanceTime=" << acceptanceTime
679  << " fullSpeedGap=" << fullSpeedGap
680  << " fullSpeedDrivingSeconds=" << fullSpeedDrivingSeconds
681  << " dProb=" << deltaProb
682  << "\n";
683  }
685  return ret | lca | LCA_KEEPRIGHT;
686  }
687  }
688 
690  && neighDist / MAX2((SUMOReal) .1, myVehicle.getSpeed()) > 20.) { //./MAX2((SUMOReal) .1, myVehicle.getSpeed())) { // -.1
691  return ret | lca | LCA_SPEEDGAIN;
692  }
693  } else {
694  // ONLY FOR CHANGING TO THE LEFT
695  if (thisLaneVSafe > neighLaneVSafe) {
696  // this lane is better
697  if (mySpeedGainProbability > 0) {
698  mySpeedGainProbability /= 2.0;
699  }
700  } else {
701  // left lane is better
702  mySpeedGainProbability += relativeGain;
703  }
704  if (mySpeedGainProbability > CHANGE_PROB_THRESHOLD_LEFT && neighDist / MAX2((SUMOReal) .1, myVehicle.getSpeed()) > 20.) { // .1
705  return ret | lca | LCA_SPEEDGAIN;
706  }
707  }
708  // --------
709  if (changeToBest && bestLaneOffset == curr.bestLaneOffset
710  && (right ? mySpeedGainProbability < 0 : mySpeedGainProbability > 0)) {
711  // change towards the correct lane, speedwise it does not hurt
712  return ret | lca | LCA_STRATEGIC;
713  }
714 
715  return ret;
716 }
717 
718 
719 int
721  // if this vehicle is blocking someone in front, we maybe decelerate to let him in
722  if ((*blocked) != 0) {
723  SUMOReal gap = (*blocked)->getPositionOnLane() - (*blocked)->getVehicleType().getLength() - myVehicle.getPositionOnLane() - myVehicle.getVehicleType().getMinGap();
724  if (gap > POSITION_EPS) {
726  if ((*blocked)->getSpeed() < SUMO_const_haltingSpeed) {
728  } else {
729  state |= LCA_AMBACKBLOCKER;
730  }
733  (SUMOReal)(gap - POSITION_EPS), (*blocked)->getSpeed(),
734  (*blocked)->getCarFollowModel().getMaxDecel()));
735  }
736  }
737  }
738  return state;
739 }
740 
741 
742 void
743 MSLCM_LC2013::saveBlockerLength(MSVehicle* blocker, int lcaCounter) {
744  if (blocker != 0 && (blocker->getLaneChangeModel().getOwnState() & lcaCounter) != 0) {
745  // is there enough space in front of us for the blocker?
748  if (blocker->getVehicleType().getLengthWithGap() <= potential) {
749  // save at least his length in myLeadingBlockerLength
751  } else {
752  // we cannot save enough space for the blocker. It needs to save
753  // space for ego instead
755  }
756  }
757 }
758 /****************************************************************************/
759 
void saveBlockerLength(MSVehicle *blocker, int lcaCounter)
save space for vehicles which need to counter-lane-change
#define CHANGE_PROB_THRESHOLD_RIGHT
MSEdge & getEdge() const
Returns the lane's edge.
Definition: MSLane.h:452
Representation of a vehicle in the micro simulation.
Definition: MSVehicle.h:77
SUMOReal getMaxSpeed() const
Get vehicle's maximum speed [m/s].
MSLCM_LC2013(MSVehicle &v)
#define SPEED2DIST(x)
Definition: SUMOTime.h:55
int wantsChange(int laneOffset, MSAbstractLaneChangeModel::MSLCMessager &msgPass, int blocked, const std::pair< MSVehicle *, SUMOReal > &leader, const std::pair< MSVehicle *, SUMOReal > &neighLead, const std::pair< MSVehicle *, SUMOReal > &neighFollow, const MSLane &neighLane, const std::vector< MSVehicle::LaneQ > &preb, MSVehicle **lastBlocked, MSVehicle **firstBlocked)
Called to examine whether the vehicle wants to change using the given laneOffset. This method gets th...
void prepareStep()
#define min(a, b)
Definition: polyfonts.c:62
const MSCFModel & getCarFollowModel() const
Returns the vehicle's car following model definition.
Definition: MSVehicle.h:531
#define MIN_FALLBEHIND
SUMOReal patchSpeed(const SUMOReal min, const SUMOReal wanted, const SUMOReal max, const MSCFModel &cfModel)
Called to adapt the speed in order to allow a lane change.
#define ACCEL2SPEED(x)
Definition: SUMOTime.h:61
#define LOOK_FORWARD_RIGHT
const std::vector< MSLane * > & getLanes() const
Returns this edge's lanes.
Definition: MSEdge.h:168
The action is done to help someone else.
SUMOReal getLengthWithGap() const
Get vehicle's length including the minimum gap [m].
int bestLaneOffset
The (signed) number of lanes to be crossed to get to the lane which allows to continue the drive...
Definition: MSVehicle.h:464
#define OVERTAKE_RIGHT_FORBIDDEN
bool congested() const
Definition: MSVehicle.h:396
virtual SUMOReal followSpeed(const MSVehicle *const veh, SUMOReal speed, SUMOReal gap2pred, SUMOReal predSpeed, SUMOReal predMaxDecel) const =0
Computes the vehicle's safe speed (no dawdling)
#define KEEP_RIGHT_ACCEPTANCE
The car-following model abstraction.
Definition: MSCFModel.h:58
SUMOReal myKeepRightProbability
Definition: MSLCM_LC2013.h:181
void * informNeighFollower(void *info, MSVehicle *sender)
Informs the follower on the desired lane.
SUMOReal getLength() const
Get vehicle's length [m].
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:154
T MAX2(T a, T b)
Definition: StdDefs.h:71
SUMOReal getSecureGap(const SUMOReal speed, const SUMOReal leaderSpeed, const SUMOReal leaderMaxDecel) const
Returns the minimum gap to reserve if the leader is braking at maximum.
Definition: MSCFModel.h:232
SUMOReal getPositionOnLane() const
Get the vehicle's position along the lane.
Definition: MSVehicle.h:283
std::vector< SUMOReal > myVSafes
Definition: MSLCM_LC2013.h:190
#define TS
Definition: SUMOTime.h:52
The action is due to a TraCI request.
SUMOTime getCurrentTimeStep() const
Returns the current simulation step.
Definition: MSNet.h:210
SUMOReal length
The overall length which may be driven when using this lane without a lane change.
Definition: MSVehicle.h:458
The action is urgent (to be defined by lc-model)
#define abs(a)
Definition: polyfonts.c:63
void informFollower(MSAbstractLaneChangeModel::MSLCMessager &msgPass, int blocked, int dir, const std::pair< MSVehicle *, SUMOReal > &neighFollow, SUMOReal remainingSeconds, SUMOReal plannedSpeed)
decide whether we will try cut in before the follower or allow to be overtaken
MSAbstractLaneChangeModel & getLaneChangeModel()
Definition: MSVehicle.cpp:1740
#define ROUNDABOUT_DIST_BONUS
A class responsible for exchanging messages between cars involved in lane-change interaction.
Wants go to the left.
bool currentDistDisallows(SUMOReal dist, int laneOffset, SUMOReal lookForwardDist)
Definition: MSLCM_LC2013.h:163
bool currentDistAllows(SUMOReal dist, int laneOffset, SUMOReal lookForwardDist)
Definition: MSLCM_LC2013.h:166
#define max(a, b)
Definition: polyfonts.c:61
SUMOReal brakeGap(const SUMOReal speed) const
Returns the distance the vehicle needs to halt including driver's reaction time.
Definition: MSCFModel.h:213
SUMOReal myLeadingBlockerLength
Definition: MSLCM_LC2013.h:183
SUMOReal getMinGap() const
Get the free space in front of vehicles of this class.
#define CUT_IN_LEFT_SPEED_THRESHOLD
SUMOReal myLookAheadSpeed
Definition: MSLCM_LC2013.h:188
#define LOOK_AHEAD_SPEED_MEMORY
#define STEPS2TIME(x)
Definition: SUMOTime.h:65
int slowDownForBlocked(MSVehicle **blocked, int state)
compute useful slowdowns for blocked vehicles
T MIN2(T a, T b)
Definition: StdDefs.h:65
#define CHANGE_PROB_THRESHOLD_LEFT
virtual SUMOReal stopSpeed(const MSVehicle *const veh, const SUMOReal speed, SUMOReal gap2pred) const =0
Computes the vehicle's safe speed for approaching a non-moving obstacle (no dawdling) ...
#define POSITION_EPS
Definition: config.h:186
SUMOReal getSpeedLimit() const
Returns the lane's maximum allowed speed.
Definition: MSLane.h:362
A structure representing the best lanes for continuing the route.
Definition: MSVehicle.h:454
SUMOReal getMaxDecel() const
Get the vehicle type's maximum deceleration [m/s^2].
Definition: MSCFModel.h:165
SUMOReal changeRequestRemainingSeconds(const SUMOTime currentTime) const
Return the remaining number of seconds of the current laneTimeLine assuming one exists.
Definition: MSVehicle.cpp:297
int myOwnState
The current state of the vehicle.
SUMOReal informLeader(MSAbstractLaneChangeModel::MSLCMessager &msgPass, int blocked, int dir, const std::pair< MSVehicle *, SUMOReal > &neighLead, SUMOReal remainingSeconds)
Wants go to the right.
virtual void saveBlockerLength(SUMOReal length)
reserve space at the end of the lane to avoid dead locks
std::pair< SUMOReal, int > Info
information regarding save velocity (unused) and state flags of the ego vehicle
Definition: MSLCM_LC2013.h:171
int _wantsChange(int laneOffset, MSAbstractLaneChangeModel::MSLCMessager &msgPass, int blocked, const std::pair< MSVehicle *, SUMOReal > &leader, const std::pair< MSVehicle *, SUMOReal > &neighLead, const std::pair< MSVehicle *, SUMOReal > &neighFollow, const MSLane &neighLane, const std::vector< MSVehicle::LaneQ > &preb, MSVehicle **lastBlocked, MSVehicle **firstBlocked)
helper function for doing the actual work
MSVehicle & myVehicle
The vehicle this lane-changer belongs to.
void * informNeighLeader(void *info, MSVehicle *sender)
Informs the leader on the desired lane.
The action is needed to follow the route (navigational lc)
EdgeBasicFunction getPurpose() const
Returns the edge type (EdgeBasicFunction)
Definition: MSEdge.h:204
Influencer & getInfluencer()
Returns the velocity/lane influencer.
Definition: MSVehicle.cpp:2293
#define JAM_FACTOR
SUMOTime myLastLaneChangeOffset
information how long ago the vehicle has performed a lane-change
SUMOReal occupation
The overall vehicle sum on consecutive lanes which can be passed without a lane change.
Definition: MSVehicle.h:460
std::vector< MSLane * > bestContinuations
Consecutive lane that can be followed without a lane change (contribute to length and occupation) ...
Definition: MSVehicle.h:468
The action is due to the default of keeping right "Rechtsfahrgebot".
const MSVehicleType & getVehicleType() const
Returns the vehicle's type definition.
Definition: MSBaseVehicle.h:94
#define URGENCY
const SUMOReal SUMO_const_haltingSpeed
the speed threshold at which vehicles are considered as halting
Definition: StdDefs.h:53
Needs to stay on the current lane.
virtual ~MSLCM_LC2013()
SUMOReal getSpeed() const
Returns the vehicle's current speed.
Definition: MSVehicle.h:291
SUMOReal getWaitingSeconds() const
Returns the number of seconds waited (speed was lesser than 0.1m/s)
Definition: MSVehicle.h:373
#define LOOK_FORWARD_LEFT
SUMOReal mySpeedGainProbability
a value for tracking the probability that a change to the offset with the same sign is beneficial ...
Definition: MSLCM_LC2013.h:177
SUMOReal myLeftSpace
Definition: MSLCM_LC2013.h:184
bool gDebugFlag2
Definition: StdDefs.cpp:83
#define SUMOReal
Definition: config.h:215
T MIN3(T a, T b, T c)
Definition: StdDefs.h:78
bool amBlockingFollowerPlusNB()
Definition: MSLCM_LC2013.h:160
#define HELP_OVERTAKE
#define DELTA_T
Definition: SUMOTime.h:50
const MSLinkCont & getLinkCont() const
returns the container with all links !!!
Definition: MSLane.cpp:998
SUMOReal _patchSpeed(const SUMOReal min, const SUMOReal wanted, const SUMOReal max, const MSCFModel &cfModel)
#define LOOK_AHEAD_MIN_SPEED
#define LCA_RIGHT_IMPATIENCE
SUMOReal getVehicleMaxSpeed(const SUMOVehicle *const veh) const
Returns the lane's maximum speed, given a vehicle's speed limit adaptation.
Definition: MSLane.h:354
MSLane * getLane() const
Returns the lane the vehicle is on.
Definition: MSVehicle.h:328
int influenceChangeDecision(int state)
allow TraCI to influence a lane change decision
Definition: MSVehicle.cpp:2311
The edge is an internal edge.
Definition: MSEdge.h:90
void * inform(void *info, MSVehicle *sender)
Representation of a lane in the micro simulation.
Definition: MSLane.h:77
const MSCFModel & myCarFollowModel
The vehicle's car following model.
Interface for lane-change models.
int getBestLaneOffset() const
returns the current offset from the best lane
Definition: MSVehicle.cpp:2053
#define HELP_DECEL_FACTOR
const std::string & getID() const
Returns the name of the vehicle.
The action is due to the wish to be faster (tactical lc)
#define KEEP_RIGHT_TIME