SUMO - Simulation of Urban MObility
MSLCM_JE2013.cpp
Go to the documentation of this file.
1 /****************************************************************************/
9 // A lane change model developed by J. Erdmann
10 // based on the model of D. Krajzewicz developed between 2004 and 2011 (MSLCM_DK2004)
11 /****************************************************************************/
12 // SUMO, Simulation of Urban MObility; see http://sumo.dlr.de/
13 // Copyright (C) 2013-2015 DLR (http://www.dlr.de/) and contributors
14 /****************************************************************************/
15 //
16 // This file is part of SUMO.
17 // SUMO is free software: you can redistribute it and/or modify
18 // it under the terms of the GNU General Public License as published by
19 // the Free Software Foundation, either version 3 of the License, or
20 // (at your option) any later version.
21 //
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 <iostream>
36 #include <microsim/MSEdge.h>
37 #include <microsim/MSLane.h>
38 #include <microsim/MSNet.h>
39 #include "MSLCM_JE2013.h"
40 
41 #ifdef CHECK_MEMORY_LEAKS
42 #include <foreign/nvwa/debug_new.h>
43 #endif // CHECK_MEMORY_LEAKS
44 
45 //#define DEBUG_VEHICLE_GUI_SELECTION 1
46 #ifdef DEBUG_VEHICLE_GUI_SELECTION
48 #include <guisim/GUIVehicle.h>
49 #include <guisim/GUILane.h>
50 #endif
51 
52 
53 
54 // ===========================================================================
55 // variable definitions
56 // ===========================================================================
57 // 80km/h will be the threshold for dividing between long/short foresight
58 #define LOOK_FORWARD_SPEED_DIVIDER (SUMOReal)14.
59 
60 // VARIANT_1 (lf*2)
61 //#define LOOK_FORWARD_FAR 30.
62 //#define LOOK_FORWARD_NEAR 10.
63 
64 #define LOOK_FORWARD_RIGHT (SUMOReal)10.
65 #define LOOK_FORWARD_LEFT (SUMOReal)20.
66 
67 #define JAM_FACTOR (SUMOReal)1.
68 //#define JAM_FACTOR 2. // VARIANT_8 (makes vehicles more focused but also more "selfish")
69 
70 #define LCA_RIGHT_IMPATIENCE (SUMOReal)-1.
71 #define CUT_IN_LEFT_SPEED_THRESHOLD (SUMOReal)27.
72 #define MAX_ONRAMP_LENGTH (SUMOReal)200.
73 
74 #define LOOK_AHEAD_MIN_SPEED (SUMOReal)0.0
75 #define LOOK_AHEAD_SPEED_MEMORY (SUMOReal)0.9
76 #define LOOK_AHEAD_SPEED_DECREMENT 6.
77 
78 #define HELP_DECEL_FACTOR (SUMOReal)1.0
79 
80 #define HELP_OVERTAKE (SUMOReal)(10.0 / 3.6)
81 #define MIN_FALLBEHIND (SUMOReal)(14.0 / 3.6)
82 
83 #define KEEP_RIGHT_HEADWAY (SUMOReal)2.0
84 
85 #define URGENCY (SUMOReal)2.0
86 
87 #define ROUNDABOUT_DIST_BONUS (SUMOReal)100.0
88 
89 #define CHANGE_PROB_THRESHOLD_RIGHT (SUMOReal)2.0
90 #define CHANGE_PROB_THRESHOLD_LEFT (SUMOReal)0.2
91 #define KEEP_RIGHT_TIME (SUMOReal)5.0 // the number of seconds after which a vehicle should move to the right lane
92 #define KEEP_RIGHT_ACCEPTANCE (SUMOReal)7.0 // calibration factor for determining the desire to keep right
93 
94 #define RELGAIN_NORMALIZATION_MIN_SPEED (SUMOReal)10.0
95 
96 #define TURN_LANE_DIST (SUMOReal)200.0 // the distance at which a lane leading elsewhere is considered to be a turn-lane that must be avoided
97 
98 //#define DEBUG_COND (myVehicle.getID() == "1501_27271428" || myVehicle.getID() == "1502_27270000")
99 //#define DEBUG_COND (myVehicle.getID() == "175129_26220000")
100 //#define DEBUG_COND (myVehicle.getID() == "pkw150478" || myVehicle.getID() == "pkw150494" || myVehicle.getID() == "pkw150289")
101 //#define DEBUG_COND (myVehicle.getID() == "A" || myVehicle.getID() == "B") // fail change to left
102 //#define DEBUG_COND (myVehicle.getID() == "Costa_12_13") // test stops_overtaking
103 #define DEBUG_COND false
104 
105 // debug function
106 std::string
107 tryID(const MSVehicle* v) {
108  return v == 0 ? "NULL" : v->getID();
109 }
110 
111 
112 // ===========================================================================
113 // member method definitions
114 // ===========================================================================
117  mySpeedGainProbability(0),
118  myKeepRightProbability(0),
119  myLeadingBlockerLength(0),
120  myLeftSpace(0),
121  myLookAheadSpeed(LOOK_AHEAD_MIN_SPEED)
122 {}
123 
125  changed(0);
126 }
127 
128 
129 int
131  int laneOffset,
133  int blocked,
134  const std::pair<MSVehicle*, SUMOReal>& leader,
135  const std::pair<MSVehicle*, SUMOReal>& neighLead,
136  const std::pair<MSVehicle*, SUMOReal>& neighFollow,
137  const MSLane& neighLane,
138  const std::vector<MSVehicle::LaneQ>& preb,
139  MSVehicle** lastBlocked,
140  MSVehicle** firstBlocked) {
142 
143  if (gDebugFlag2) {
144  std::cout << STEPS2TIME(MSNet::getInstance()->getCurrentTimeStep())
145  << " veh=" << myVehicle.getID()
146  << " lane=" << myVehicle.getLane()->getID()
147  << " pos=" << myVehicle.getPositionOnLane()
148  << " speed=" << myVehicle.getSpeed()
149  << " considerChangeTo=" << (laneOffset == -1 ? "right" : "left")
150  << "\n";
151  }
152 
153  const int result = _wantsChange(laneOffset, msgPass, blocked, leader, neighLead, neighFollow, neighLane, preb, lastBlocked, firstBlocked);
154  if (gDebugFlag2) {
155  if (result & LCA_WANTS_LANECHANGE) {
156  std::cout << STEPS2TIME(MSNet::getInstance()->getCurrentTimeStep())
157  << " veh=" << myVehicle.getID()
158  << " wantsChangeTo=" << (laneOffset == -1 ? "right" : "left")
159  << ((result & LCA_URGENT) ? " (urgent)" : "")
160  << ((result & LCA_CHANGE_TO_HELP) ? " (toHelp)" : "")
161  << ((result & LCA_STRATEGIC) ? " (strat)" : "")
162  << ((result & LCA_COOPERATIVE) ? " (coop)" : "")
163  << ((result & LCA_SPEEDGAIN) ? " (speed)" : "")
164  << ((result & LCA_KEEPRIGHT) ? " (keepright)" : "")
165  << ((result & LCA_TRACI) ? " (traci)" : "")
166  << ((blocked & LCA_BLOCKED) ? " (blocked)" : "")
167  << ((blocked & LCA_OVERLAPPING) ? " (overlap)" : "")
168  << "\n\n\n";
169  }
170  }
171  gDebugFlag2 = false;
172  return result;
173 }
174 
175 
176 SUMOReal
177 MSLCM_JE2013::patchSpeed(const SUMOReal min, const SUMOReal wanted, const SUMOReal max, const MSCFModel& cfModel) {
179 
180  const SUMOReal newSpeed = _patchSpeed(min, wanted, max, cfModel);
181  if (gDebugFlag1) {
182  const std::string patched = (wanted != newSpeed ? " patched=" + toString(newSpeed) : "");
183  std::cout << STEPS2TIME(MSNet::getInstance()->getCurrentTimeStep())
184  << " veh=" << myVehicle.getID()
185  << " lane=" << myVehicle.getLane()->getID()
186  << " pos=" << myVehicle.getPositionOnLane()
187  << " v=" << myVehicle.getSpeed()
188  << " wanted=" << wanted
189  << patched
190  << "\n\n";
191  }
192  gDebugFlag1 = false;
193  return newSpeed;
194 }
195 
196 
197 SUMOReal
198 MSLCM_JE2013::_patchSpeed(const SUMOReal min, const SUMOReal wanted, const SUMOReal max, const MSCFModel& cfModel) {
199 
200  const SUMOReal time = STEPS2TIME(MSNet::getInstance()->getCurrentTimeStep());
201 
202  int state = myOwnState;
203 
204  // letting vehicles merge in at the end of the lane in case of counter-lane change, step#2
205  SUMOReal MAGIC_offset = 1.;
206  // if we want to change and have a blocking leader and there is enough room for him in front of us
207  if (myLeadingBlockerLength != 0) {
209  if (gDebugFlag1) {
210  std::cout << time << " veh=" << myVehicle.getID() << " myLeadingBlockerLength=" << myLeadingBlockerLength << " space=" << space << "\n";
211  }
212  if (space > 0) { // XXX space > -MAGIC_offset
213  // compute speed for decelerating towards a place which allows the blocking leader to merge in in front
214  SUMOReal safe = cfModel.stopSpeed(&myVehicle, myVehicle.getSpeed(), space);
215  // if we are approaching this place
216  if (safe < wanted) {
217  // return this speed as the speed to use
218  if (gDebugFlag1) {
219  std::cout << time << " veh=" << myVehicle.getID() << " slowing down for leading blocker, safe=" << safe << (safe + NUMERICAL_EPS < min ? " (not enough)" : "") << "\n";
220  }
221  return MAX2(min, safe);
222  }
223  }
224  }
225 
226  SUMOReal nVSafe = wanted;
227  bool gotOne = false;
228  for (std::vector<SUMOReal>::const_iterator i = myVSafes.begin(); i != myVSafes.end(); ++i) {
229  SUMOReal v = (*i);
230  if (v >= min && v <= max) {
231  nVSafe = MIN2(v, nVSafe);
232  gotOne = true;
233  if (gDebugFlag1) {
234  std::cout << time << " veh=" << myVehicle.getID() << " got nVSafe=" << nVSafe << "\n";
235  }
236  } else {
237  if (v < min) {
238  if (gDebugFlag1) {
239  std::cout << time << " veh=" << myVehicle.getID() << " ignoring low nVSafe=" << v << " min=" << min << "\n";
240  }
241  } else {
242  if (gDebugFlag1) {
243  std::cout << time << " veh=" << myVehicle.getID() << " ignoring high nVSafe=" << v << " max=" << max << "\n";
244  }
245  }
246  }
247  }
248 
249  if (gotOne && !myDontBrake) {
250  if (gDebugFlag1) {
251  std::cout << time << " veh=" << myVehicle.getID() << " got vSafe\n";
252  }
253  return nVSafe;
254  }
255 
256  // check whether the vehicle is blocked
257  if ((state & LCA_WANTS_LANECHANGE) != 0 && (state & LCA_BLOCKED) != 0) {
258  if ((state & LCA_STRATEGIC) != 0) {
259  // necessary decelerations are controlled via vSafe. If there are
260  // none it means we should speed up
261  if (gDebugFlag1) {
262  std::cout << time << " veh=" << myVehicle.getID() << " LCA_WANTS_LANECHANGE (strat, no vSafe)\n";
263  }
264  return (max + wanted) / (SUMOReal) 2.0;
265  } else if ((state & LCA_COOPERATIVE) != 0) {
266  // only minor adjustments in speed should be done
267  if ((state & LCA_BLOCKED_BY_LEADER) != 0) {
268  if (gDebugFlag1) {
269  std::cout << time << " veh=" << myVehicle.getID() << " LCA_BLOCKED_BY_LEADER (coop)\n";
270  }
271  return (min + wanted) / (SUMOReal) 2.0;
272  }
273  if ((state & LCA_BLOCKED_BY_FOLLOWER) != 0) {
274  if (gDebugFlag1) {
275  std::cout << time << " veh=" << myVehicle.getID() << " LCA_BLOCKED_BY_FOLLOWER (coop)\n";
276  }
277  return (max + wanted) / (SUMOReal) 2.0;
278  }
279  //} else { // VARIANT_16
280  // // only accelerations should be performed
281  // if ((state & LCA_BLOCKED_BY_FOLLOWER) != 0) {
282  // if (gDebugFlag1) std::cout << time << " veh=" << myVehicle.getID() << " LCA_BLOCKED_BY_FOLLOWER\n";
283  // return (max + wanted) / (SUMOReal) 2.0;
284  // }
285  }
286  }
287 
288  /*
289  // decelerate if being a blocking follower
290  // (and does not have to change lanes)
291  if ((state & LCA_AMBLOCKINGFOLLOWER) != 0) {
292  if (fabs(max - myVehicle.getCarFollowModel().maxNextSpeed(myVehicle.getSpeed(), &myVehicle)) < 0.001 && min == 0) { // !!! was standing
293  if (gDebugFlag1) std::cout << time << " veh=" << myVehicle.getID() << " LCA_AMBLOCKINGFOLLOWER (standing)\n";
294  return 0;
295  }
296  if (gDebugFlag1) std::cout << time << " veh=" << myVehicle.getID() << " LCA_AMBLOCKINGFOLLOWER\n";
297 
298  //return min; // VARIANT_3 (brakeStrong)
299  return (min + wanted) / (SUMOReal) 2.0;
300  }
301  if ((state & LCA_AMBACKBLOCKER) != 0) {
302  if (max <= myVehicle.getCarFollowModel().maxNextSpeed(myVehicle.getSpeed(), &myVehicle) && min == 0) { // !!! was standing
303  if (gDebugFlag1) std::cout << time << " veh=" << myVehicle.getID() << " LCA_AMBACKBLOCKER (standing)\n";
304  //return min; VARIANT_9 (backBlockVSafe)
305  return nVSafe;
306  }
307  }
308  if ((state & LCA_AMBACKBLOCKER_STANDING) != 0) {
309  if (gDebugFlag1) std::cout << time << " veh=" << myVehicle.getID() << " LCA_AMBACKBLOCKER_STANDING\n";
310  //return min;
311  return nVSafe;
312  }
313  */
314 
315  // accelerate if being a blocking leader or blocking follower not able to brake
316  // (and does not have to change lanes)
317  if ((state & LCA_AMBLOCKINGLEADER) != 0) {
318  if (gDebugFlag1) {
319  std::cout << time << " veh=" << myVehicle.getID() << " LCA_AMBLOCKINGLEADER\n";
320  }
321  return (max + wanted) / (SUMOReal) 2.0;
322  }
323 
324  if ((state & LCA_AMBLOCKINGFOLLOWER_DONTBRAKE) != 0) {
325  if (gDebugFlag1) {
326  std::cout << time << " veh=" << myVehicle.getID() << " LCA_AMBLOCKINGFOLLOWER_DONTBRAKE\n";
327  }
328  /*
329  // VARIANT_4 (dontbrake)
330  if (max <= myVehicle.getCarFollowModel().maxNextSpeed(myVehicle.getSpeed(), &myVehicle) && min == 0) { // !!! was standing
331  return wanted;
332  }
333  return (min + wanted) / (SUMOReal) 2.0;
334  */
335  }
336  if (myVehicle.getLane()->getEdge().getLanes().size() == 1) {
337  // remove chaning information if on a road with a single lane
338  changed(0);
339  }
340  return wanted;
341 }
342 
343 
344 void*
345 MSLCM_JE2013::inform(void* info, MSVehicle* sender) {
346  Info* pinfo = (Info*) info;
347  if (pinfo->first >= 0) {
348  myVSafes.push_back(pinfo->first);
349  }
350  //myOwnState &= 0xffffffff; // reset all bits of MyLCAEnum but only those
351  myOwnState |= pinfo->second;
352  if (gDebugFlag2 || DEBUG_COND) {
353  std::cout << STEPS2TIME(MSNet::getInstance()->getCurrentTimeStep())
354  << " veh=" << myVehicle.getID()
355  << " informedBy=" << sender->getID()
356  << " info=" << pinfo->second
357  << " vSafe=" << pinfo->first
358  << "\n";
359  }
360  delete pinfo;
361  return (void*) true;
362 }
363 
364 
365 SUMOReal
367  int blocked,
368  int dir,
369  const std::pair<MSVehicle*, SUMOReal>& neighLead,
370  SUMOReal remainingSeconds) {
371  SUMOReal plannedSpeed = MIN2(myVehicle.getSpeed(),
373  for (std::vector<SUMOReal>::const_iterator i = myVSafes.begin(); i != myVSafes.end(); ++i) {
374  SUMOReal v = (*i);
376  plannedSpeed = MIN2(plannedSpeed, v);
377  }
378  }
379  if (gDebugFlag2) {
380  std::cout << " informLeader speed=" << myVehicle.getSpeed() << " planned=" << plannedSpeed << "\n";
381  }
382 
383  if ((blocked & LCA_BLOCKED_BY_LEADER) != 0) {
384  assert(neighLead.first != 0);
385  MSVehicle* nv = neighLead.first;
386  if (gDebugFlag2) std::cout << " blocked by leader nv=" << nv->getID() << " nvSpeed=" << nv->getSpeed() << " needGap="
388  // decide whether we want to overtake the leader or follow it
389  const SUMOReal dv = plannedSpeed - nv->getSpeed();
390  const SUMOReal overtakeDist = (neighLead.second // drive to back of follower
391  + nv->getVehicleType().getLengthWithGap() // drive to front of follower
392  + myVehicle.getVehicleType().getLength() // ego back reaches follower front
393  + nv->getCarFollowModel().getSecureGap( // save gap to follower
395 
396  if (dv < 0
397  // overtaking on the right on an uncongested highway is forbidden (noOvertakeLCLeft)
399  // not enough space to overtake? (we will start to brake when approaching a dead end)
401  // not enough time to overtake?
402  || dv * remainingSeconds < overtakeDist) {
403  // cannot overtake
404  msgPass.informNeighLeader(new Info(-1, dir | LCA_AMBLOCKINGLEADER), &myVehicle);
405  // slow down smoothly to follow leader
406  const SUMOReal targetSpeed = myCarFollowModel.followSpeed(
407  &myVehicle, myVehicle.getSpeed(), neighLead.second, nv->getSpeed(), nv->getCarFollowModel().getMaxDecel());
408  if (targetSpeed < myVehicle.getSpeed()) {
409  // slow down smoothly to follow leader
411  MAX2(MIN_FALLBEHIND, (myVehicle.getSpeed() - targetSpeed) / remainingSeconds)));
412  //const SUMOReal nextSpeed = MAX2((SUMOReal)0, MIN2(plannedSpeed, myVehicle.getSpeed() - decel));
413  const SUMOReal nextSpeed = MIN2(plannedSpeed, myVehicle.getSpeed() - decel);
414  if (gDebugFlag2) {
415  std::cout << STEPS2TIME(MSNet::getInstance()->getCurrentTimeStep())
416  << " cannot overtake leader nv=" << nv->getID()
417  << " dv=" << dv
418  << " remainingSeconds=" << remainingSeconds
419  << " targetSpeed=" << targetSpeed
420  << " nextSpeed=" << nextSpeed
421  << "\n";
422  }
423  myVSafes.push_back(nextSpeed);
424  return nextSpeed;
425  } else {
426  // leader is fast enough anyway
427  if (gDebugFlag2) {
428  std::cout << STEPS2TIME(MSNet::getInstance()->getCurrentTimeStep())
429  << " cannot overtake fast leader nv=" << nv->getID()
430  << " dv=" << dv
431  << " remainingSeconds=" << remainingSeconds
432  << " targetSpeed=" << targetSpeed
433  << "\n";
434  }
435  myVSafes.push_back(targetSpeed);
436  return plannedSpeed;
437  }
438  } else {
439  if (gDebugFlag2) {
440  std::cout << STEPS2TIME(MSNet::getInstance()->getCurrentTimeStep())
441  << " wants to overtake leader nv=" << nv->getID()
442  << " dv=" << dv
443  << " remainingSeconds=" << remainingSeconds
444  << " currentGap=" << neighLead.second
446  << " overtakeDist=" << overtakeDist
447  << "\n";
448  }
449  // overtaking, leader should not accelerate
450  msgPass.informNeighLeader(new Info(nv->getSpeed(), dir | LCA_AMBLOCKINGLEADER), &myVehicle);
451  return -1;
452  }
453  } else if (neighLead.first != 0) { // (remainUnblocked)
454  // we are not blocked now. make sure we stay far enough from the leader
455  MSVehicle* nv = neighLead.first;
456  const SUMOReal nextNVSpeed = nv->getSpeed() - HELP_OVERTAKE; // conservative
457  const SUMOReal dv = SPEED2DIST(myVehicle.getSpeed() - nextNVSpeed);
458  const SUMOReal targetSpeed = myCarFollowModel.followSpeed(
459  &myVehicle, myVehicle.getSpeed(), neighLead.second - dv, nextNVSpeed, nv->getCarFollowModel().getMaxDecel());
460  myVSafes.push_back(targetSpeed);
461  if (gDebugFlag2) {
462  std::cout << " not blocked by leader nv=" << nv->getID()
463  << " nvSpeed=" << nv->getSpeed()
464  << " gap=" << neighLead.second
465  << " nextGap=" << neighLead.second - dv
467  << " targetSpeed=" << targetSpeed
468  << "\n";
469  }
470  return MIN2(targetSpeed, plannedSpeed);
471  } else {
472  // not overtaking
473  return plannedSpeed;
474  }
475 }
476 
477 
478 void
480  int blocked,
481  int dir,
482  const std::pair<MSVehicle*, SUMOReal>& neighFollow,
483  SUMOReal remainingSeconds,
484  SUMOReal plannedSpeed) {
485  if ((blocked & LCA_BLOCKED_BY_FOLLOWER) != 0) {
486  assert(neighFollow.first != 0);
487  MSVehicle* nv = neighFollow.first;
488  if (gDebugFlag2) std::cout << " blocked by follower nv=" << nv->getID() << " nvSpeed=" << nv->getSpeed() << " needGap="
490 
491  // are we fast enough to cut in without any help?
492  if (plannedSpeed - nv->getSpeed() >= HELP_OVERTAKE) {
493  const SUMOReal neededGap = nv->getCarFollowModel().getSecureGap(nv->getSpeed(), plannedSpeed, myVehicle.getCarFollowModel().getMaxDecel());
494  if ((neededGap - neighFollow.second) / remainingSeconds < (plannedSpeed - nv->getSpeed())) {
495  if (gDebugFlag2) {
496  std::cout << " wants to cut in before nv=" << nv->getID() << " without any help neededGap=" << neededGap << "\n";
497  }
498  // follower might even accelerate but not to much
499  msgPass.informNeighFollower(new Info(plannedSpeed - HELP_OVERTAKE, dir | LCA_AMBLOCKINGFOLLOWER), &myVehicle);
500  return;
501  }
502  }
503  // decide whether we will request help to cut in before the follower or allow to be overtaken
504 
505  // PARAMETERS
506  // assume other vehicle will assume the equivalent of 1 second of
507  // maximum deceleration to help us (will probably be spread over
508  // multiple seconds)
509  // -----------
510  const SUMOReal helpDecel = nv->getCarFollowModel().getMaxDecel() * HELP_DECEL_FACTOR ;
511 
512  // change in the gap between ego and blocker over 1 second (not STEP!)
513  const SUMOReal neighNewSpeed = MAX2((SUMOReal)0, nv->getSpeed() - ACCEL2SPEED(helpDecel));
514  const SUMOReal neighNewSpeed1s = MAX2((SUMOReal)0, nv->getSpeed() - helpDecel);
515  const SUMOReal dv = plannedSpeed - neighNewSpeed1s;
516  // new gap between follower and self in case the follower does brake for 1s
517  const SUMOReal decelGap = neighFollow.second + dv;
518  const SUMOReal secureGap = nv->getCarFollowModel().getSecureGap(neighNewSpeed1s, plannedSpeed, myVehicle.getCarFollowModel().getMaxDecel());
519  if (gDebugFlag2) {
520  std::cout << STEPS2TIME(MSNet::getInstance()->getCurrentTimeStep())
521  << " egoV=" << myVehicle.getSpeed()
522  << " egoNV=" << plannedSpeed
523  << " nvNewSpeed=" << neighNewSpeed
524  << " nvNewSpeed1s=" << neighNewSpeed1s
525  << " deltaGap=" << dv
526  << " decelGap=" << decelGap
527  << " secGap=" << secureGap
528  << "\n";
529  }
530  if (decelGap > 0 && decelGap >= secureGap) {
531  // if the blocking neighbor brakes it could actually help
532  // how hard does it actually need to be?
533  const SUMOReal vsafe = MAX2(neighNewSpeed, nv->getCarFollowModel().followSpeed(
534  nv, nv->getSpeed(), neighFollow.second, plannedSpeed, myVehicle.getCarFollowModel().getMaxDecel()));
535  msgPass.informNeighFollower(new Info(vsafe, dir | LCA_AMBLOCKINGFOLLOWER), &myVehicle);
536  if (gDebugFlag2) {
537  std::cout << " wants to cut in before nv=" << nv->getID()
538  << " vsafe=" << vsafe
539  << " newSecGap=" << nv->getCarFollowModel().getSecureGap(vsafe, plannedSpeed, myVehicle.getCarFollowModel().getMaxDecel())
540  << "\n";
541  }
542  } else if (dv > 0 && dv * remainingSeconds > (secureGap - decelGap + POSITION_EPS)) {
543  // decelerating once is sufficient to open up a large enough gap in time
544  msgPass.informNeighFollower(new Info(neighNewSpeed, dir | LCA_AMBLOCKINGFOLLOWER), &myVehicle);
545  if (gDebugFlag2) {
546  std::cout << " wants to cut in before nv=" << nv->getID() << " (eventually)\n";
547  }
548  } else if (dir == LCA_MRIGHT && !myAllowOvertakingRight && !nv->congested()) {
549  const SUMOReal vhelp = MAX2(neighNewSpeed, HELP_OVERTAKE);
550  msgPass.informNeighFollower(new Info(vhelp, dir | LCA_AMBLOCKINGFOLLOWER), &myVehicle);
551  if (gDebugFlag2) {
552  std::cout << " wants to cut in before nv=" << nv->getID() << " (nv cannot overtake right)\n";
553  }
554  } else {
556  if (nv->getSpeed() > myVehicle.getSpeed() &&
558  || (dir == LCA_MLEFT && plannedSpeed > CUT_IN_LEFT_SPEED_THRESHOLD) // VARIANT_22 (slowDownLeft)
559  // XXX this is a hack to determine whether the vehicles is on an on-ramp. This information should be retrieved from the network itself
560  || (dir == LCA_MLEFT && myLeftSpace > MAX_ONRAMP_LENGTH)
561  )) {
562  // let the follower slow down to increase the likelyhood that later vehicles will be slow enough to help
563  // follower should still be fast enough to open a gap
564  vhelp = MAX2(neighNewSpeed, myVehicle.getSpeed() + HELP_OVERTAKE);
565  if (gDebugFlag2) {
566  std::cout << " wants right follower to slow down a bit\n";
567  }
568  if ((nv->getSpeed() - myVehicle.getSpeed()) / helpDecel < remainingSeconds) {
569  if (gDebugFlag2) {
570  std::cout << " wants to cut in before right follower nv=" << nv->getID() << " (eventually)\n";
571  }
572  msgPass.informNeighFollower(new Info(vhelp, dir | LCA_AMBLOCKINGFOLLOWER), &myVehicle);
573  return;
574  }
575  }
576  msgPass.informNeighFollower(new Info(vhelp, dir | LCA_AMBLOCKINGFOLLOWER), &myVehicle);
577  // this follower is supposed to overtake us. slow down smoothly to allow this
578  const SUMOReal overtakeDist = (neighFollow.second // follower reaches ego back
579  + myVehicle.getVehicleType().getLengthWithGap() // follower reaches ego front
580  + nv->getVehicleType().getLength() // follower back at ego front
581  + myVehicle.getCarFollowModel().getSecureGap( // follower has safe dist to ego
582  plannedSpeed, vhelp, nv->getCarFollowModel().getMaxDecel()));
583  // speed difference to create a sufficiently large gap
584  const SUMOReal needDV = overtakeDist / remainingSeconds;
585  // make sure the deceleration is not to strong
586  myVSafes.push_back(MAX2(vhelp - needDV, myVehicle.getSpeed() - ACCEL2SPEED(myVehicle.getCarFollowModel().getMaxDecel())));
587 
588  if (gDebugFlag2) {
589  std::cout << STEPS2TIME(MSNet::getInstance()->getCurrentTimeStep())
590  << " veh=" << myVehicle.getID()
591  << " wants to be overtaken by=" << nv->getID()
592  << " overtakeDist=" << overtakeDist
593  << " vneigh=" << nv->getSpeed()
594  << " vhelp=" << vhelp
595  << " needDV=" << needDV
596  << " vsafe=" << myVSafes.back()
597  << "\n";
598  }
599  }
600  } else if (neighFollow.first != 0) {
601  // we are not blocked no, make sure it remains that way
602  MSVehicle* nv = neighFollow.first;
603  const SUMOReal vsafe = nv->getCarFollowModel().followSpeed(
604  nv, nv->getSpeed(), neighFollow.second, plannedSpeed, myVehicle.getCarFollowModel().getMaxDecel());
605  msgPass.informNeighFollower(new Info(vsafe, dir), &myVehicle);
606  if (gDebugFlag2) {
607  std::cout << " wants to cut in before non-blocking follower nv=" << nv->getID() << "\n";
608  }
609  }
610 }
611 
612 
613 void
615  // keep information about strategic change direction
618  myLeftSpace = 0;
619  myVSafes.clear();
620  myDontBrake = false;
621  // truncate to work around numerical instability between different builds
622  mySpeedGainProbability = ceil(mySpeedGainProbability * 100000.0) * 0.00001;
623  myKeepRightProbability = ceil(myKeepRightProbability * 100000.0) * 0.00001;
624 }
625 
626 
627 void
629  myOwnState = 0;
632  if (myVehicle.getBestLaneOffset() == 0) {
633  // if we are not yet on our best lane there might still be unseen blockers
634  // (during patchSpeed)
636  myLeftSpace = 0;
637  }
639  myVSafes.clear();
640  myDontBrake = false;
642 }
643 
644 
645 int
647  int laneOffset,
649  int blocked,
650  const std::pair<MSVehicle*, SUMOReal>& leader,
651  const std::pair<MSVehicle*, SUMOReal>& neighLead,
652  const std::pair<MSVehicle*, SUMOReal>& neighFollow,
653  const MSLane& neighLane,
654  const std::vector<MSVehicle::LaneQ>& preb,
655  MSVehicle** lastBlocked,
656  MSVehicle** firstBlocked) {
657  assert(laneOffset == 1 || laneOffset == -1);
658  const SUMOTime currentTime = MSNet::getInstance()->getCurrentTimeStep();
659  // compute bestLaneOffset
660  MSVehicle::LaneQ curr, neigh, best;
661  int bestLaneOffset = 0;
662  SUMOReal currentDist = 0;
663  SUMOReal neighDist = 0;
664  int currIdx = 0;
665  MSLane* prebLane = myVehicle.getLane();
666  if (prebLane->getEdge().getPurpose() == MSEdge::EDGEFUNCTION_INTERNAL) {
667  // internal edges are not kept inside the bestLanes structure
668  prebLane = prebLane->getLinkCont()[0]->getLane();
669  }
670  for (int p = 0; p < (int) preb.size(); ++p) {
671  if (preb[p].lane == prebLane && p + laneOffset >= 0) {
672  assert(p + laneOffset < (int)preb.size());
673  curr = preb[p];
674  neigh = preb[p + laneOffset];
675  currentDist = curr.length;
676  neighDist = neigh.length;
677  bestLaneOffset = curr.bestLaneOffset;
678  // VARIANT_13 (equalBest)
679  if (bestLaneOffset == 0 && preb[p + laneOffset].bestLaneOffset == 0) {
680  if (gDebugFlag2) {
681  std::cout << STEPS2TIME(currentTime)
682  << " veh=" << myVehicle.getID()
683  << " bestLaneOffsetOld=" << bestLaneOffset
684  << " bestLaneOffsetNew=" << laneOffset
685  << "\n";
686  }
687  bestLaneOffset = laneOffset;
688  }
689  best = preb[p + bestLaneOffset];
690  currIdx = p;
691  break;
692  }
693  }
694  // direction specific constants
695  const bool right = (laneOffset == -1);
696  const int lca = (right ? LCA_RIGHT : LCA_LEFT);
697  const int myLca = (right ? LCA_MRIGHT : LCA_MLEFT);
698  const int lcaCounter = (right ? LCA_LEFT : LCA_RIGHT);
699  const int myLcaCounter = (right ? LCA_MLEFT : LCA_MRIGHT);
700  const bool changeToBest = (right && bestLaneOffset < 0) || (!right && bestLaneOffset > 0);
701  // keep information about being a leader/follower
702  int ret = (myOwnState & 0xffff0000);
703  int req = 0; // the request to change or stay
704 
705  // VARIANT_5 (disableAMBACKBLOCKER1)
706  /*
707  if (leader.first != 0
708  && (myOwnState & LCA_AMBLOCKINGFOLLOWER_DONTBRAKE) != 0
709  && (leader.first->getLaneChangeModel().getOwnState() & LCA_AMBLOCKINGFOLLOWER_DONTBRAKE) != 0) {
710 
711  myOwnState &= (0xffffffff - LCA_AMBLOCKINGFOLLOWER_DONTBRAKE);
712  if (myVehicle.getSpeed() > SUMO_const_haltingSpeed) {
713  myOwnState |= LCA_AMBACKBLOCKER;
714  } else {
715  ret |= LCA_AMBACKBLOCKER;
716  myDontBrake = true;
717  }
718  }
719  */
720 
721  if (gDebugFlag2) {
722  std::cout << STEPS2TIME(currentTime)
723  << " veh=" << myVehicle.getID()
724  << " firstBlocked=" << tryID(*firstBlocked)
725  << " lastBlocked=" << tryID(*lastBlocked)
726  << " neighLead=" << tryID(neighLead.first)
727  << " neighLeadGap=" << neighLead.second
728  << " neighFollow=" << tryID(neighFollow.first)
729  << " neighFollowGap=" << neighFollow.second
730  << "\n";
731  }
732 
733  ret = slowDownForBlocked(lastBlocked, ret);
734  // VARIANT_14 (furtherBlock)
735  if (lastBlocked != firstBlocked) {
736  ret = slowDownForBlocked(firstBlocked, ret);
737  }
738 
739 
740  // we try to estimate the distance which is necessary to get on a lane
741  // we have to get on in order to keep our route
742  // we assume we need something that depends on our velocity
743  // and compare this with the free space on our wished lane
744  //
745  // if the free space is somehow less than the space we need, we should
746  // definitely try to get to the desired lane
747  //
748  // this rule forces our vehicle to change the lane if a lane changing is necessary soon
749  // lookAheadDistance:
750  // we do not want the lookahead distance to change all the time so we discrectize the speed a bit
751 
752  // VARIANT_18 (laHyst)
755  } else {
758  }
759  //myLookAheadSpeed = myVehicle.getLane()->getVehicleMaxSpeed(&myVehicle);
760 
761  //SUMOReal laDist = laSpeed > LOOK_FORWARD_SPEED_DIVIDER
762  // ? laSpeed * LOOK_FORWARD_FAR
763  // : laSpeed * LOOK_FORWARD_NEAR;
765  laDist += myVehicle.getVehicleType().getLengthWithGap() * (SUMOReal) 2.;
766  // free space that is available for changing
767  //const SUMOReal neighSpeed = (neighLead.first != 0 ? neighLead.first->getSpeed() :
768  // neighFollow.first != 0 ? neighFollow.first->getSpeed() :
769  // best.lane->getSpeedLimit());
770  // @note: while this lets vehicles change earlier into the correct direction
771  // it also makes the vehicles more "selfish" and prevents changes which are necessary to help others
772 
773  // VARIANT_15 (insideRoundabout)
774  int roundaboutEdgesAhead = 0;
775  for (std::vector<MSLane*>::iterator it = curr.bestContinuations.begin(); it != curr.bestContinuations.end(); ++it) {
776  if ((*it) != 0 && (*it)->getEdge().isRoundabout()) {
777  roundaboutEdgesAhead += 1;
778  } else if (roundaboutEdgesAhead > 0) {
779  // only check the next roundabout
780  break;
781  }
782  }
783  int roundaboutEdgesAheadNeigh = 0;
784  for (std::vector<MSLane*>::iterator it = neigh.bestContinuations.begin(); it != neigh.bestContinuations.end(); ++it) {
785  if ((*it) != 0 && (*it)->getEdge().isRoundabout()) {
786  roundaboutEdgesAheadNeigh += 1;
787  } else if (roundaboutEdgesAheadNeigh > 0) {
788  // only check the next roundabout
789  break;
790  }
791  }
792  if (roundaboutEdgesAhead > 1) {
793  currentDist += roundaboutEdgesAhead * ROUNDABOUT_DIST_BONUS;
794  neighDist += roundaboutEdgesAheadNeigh * ROUNDABOUT_DIST_BONUS;
795  }
796  if (roundaboutEdgesAhead > 0) {
797  if (gDebugFlag2) {
798  std::cout << " roundaboutEdgesAhead=" << roundaboutEdgesAhead << " roundaboutEdgesAheadNeigh=" << roundaboutEdgesAheadNeigh << "\n";
799  }
800  }
801 
802  const SUMOReal usableDist = (currentDist - myVehicle.getPositionOnLane() - best.occupation * JAM_FACTOR);
803  //- (best.lane->getVehicleNumber() * neighSpeed)); // VARIANT 9 jfSpeed
804  const SUMOReal maxJam = MAX2(preb[currIdx + laneOffset].occupation, preb[currIdx].occupation);
805  const SUMOReal neighLeftPlace = MAX2((SUMOReal) 0, neighDist - myVehicle.getPositionOnLane() - maxJam);
806 
807  if (gDebugFlag2) {
808  std::cout << STEPS2TIME(currentTime)
809  << " veh=" << myVehicle.getID()
810  << " laSpeed=" << myLookAheadSpeed
811  << " laDist=" << laDist
812  << " currentDist=" << currentDist
813  << " usableDist=" << usableDist
814  << " bestLaneOffset=" << bestLaneOffset
815  << " best.length=" << best.length
816  << " maxJam=" << maxJam
817  << " neighLeftPlace=" << neighLeftPlace
818  << "\n";
819  }
820 
821  if (changeToBest && bestLaneOffset == curr.bestLaneOffset
822  && currentDistDisallows(usableDist, bestLaneOffset, laDist)) {
824  ret = ret | lca | LCA_STRATEGIC | LCA_URGENT;
825  } else {
826  // VARIANT_20 (noOvertakeRight)
827  if (!myAllowOvertakingRight && !right && !myVehicle.congested() && neighLead.first != 0) {
828  // check for slower leader on the left. we should not overtake but
829  // rather move left ourselves (unless congested)
830  MSVehicle* nv = neighLead.first;
831  if (nv->getSpeed() < myVehicle.getSpeed()) {
832  const SUMOReal vSafe = myCarFollowModel.followSpeed(
833  &myVehicle, myVehicle.getSpeed(), neighLead.second, nv->getSpeed(), nv->getCarFollowModel().getMaxDecel());
834  myVSafes.push_back(vSafe);
835  if (vSafe < myVehicle.getSpeed()) {
837  }
838  if (gDebugFlag2) {
839  std::cout << STEPS2TIME(currentTime)
840  << " avoid overtaking on the right nv=" << nv->getID()
841  << " nvSpeed=" << nv->getSpeed()
842  << " mySpeedGainProbability=" << mySpeedGainProbability
843  << " plannedSpeed=" << myVSafes.back()
844  << "\n";
845  }
846  }
847  }
848 
849  if (!changeToBest && (currentDistDisallows(neighLeftPlace, abs(bestLaneOffset) + 2, laDist))) {
850  // the opposite lane-changing direction should be done than the one examined herein
851  // we'll check whether we assume we could change anyhow and get back in time...
852  //
853  // this rule prevents the vehicle from moving in opposite direction of the best lane
854  // unless the way till the end where the vehicle has to be on the best lane
855  // is long enough
856  if (gDebugFlag2) {
857  std::cout << " veh=" << myVehicle.getID() << " could not change back and forth in time (1) neighLeftPlace=" << neighLeftPlace << "\n";
858  }
859  ret = ret | LCA_STAY | LCA_STRATEGIC;
860  } else if (bestLaneOffset == 0 && (neighLeftPlace * 2. < laDist)) {
861  // the current lane is the best and a lane-changing would cause a situation
862  // of which we assume we will not be able to return to the lane we have to be on.
863  // this rule prevents the vehicle from leaving the current, best lane when it is
864  // close to this lane's end
865  if (gDebugFlag2) {
866  std::cout << " veh=" << myVehicle.getID() << " could not change back and forth in time (2) neighLeftPlace=" << neighLeftPlace << "\n";
867  }
868  ret = ret | LCA_STAY | LCA_STRATEGIC;
869  } else if (bestLaneOffset == 0
870  && (leader.first == 0 || !leader.first->isStopped())
871  && neigh.bestContinuations.back()->getLinkCont().size() != 0
872  && roundaboutEdgesAhead == 0
873  && neighDist < TURN_LANE_DIST) {
874  // VARIANT_21 (stayOnBest)
875  // we do not want to leave the best lane for a lane which leads elsewhere
876  // unless our leader is stopped or we are approaching a roundabout
877  if (gDebugFlag2) {
878  std::cout << " veh=" << myVehicle.getID() << " does not want to leave the bestLane (neighDist=" << neighDist << ")\n";
879  }
880  ret = ret | LCA_STAY | LCA_STRATEGIC;
881  }
882  }
883  // check for overriding TraCI requests
884  if (gDebugFlag2) {
885  std::cout << STEPS2TIME(currentTime) << " veh=" << myVehicle.getID() << " ret=" << ret;
886  }
888  if ((ret & lcaCounter) != 0) {
889  // we are not interested in traci requests for the opposite direction here
890  ret &= ~(LCA_TRACI | lcaCounter | LCA_URGENT);
891  }
892  if (gDebugFlag2) {
893  std::cout << " retAfterInfluence=" << ret << "\n";
894  }
895 
896  if ((ret & LCA_STAY) != 0) {
897  return ret;
898  }
899  if ((ret & LCA_URGENT) != 0) {
900  // prepare urgent lane change maneuver
901  // save the left space
902  myLeftSpace = currentDist - myVehicle.getPositionOnLane();
903  if (changeToBest && abs(bestLaneOffset) > 1) {
904  // there might be a vehicle which needs to counter-lane-change one lane further and we cannot see it yet
905  if (gDebugFlag2) {
906  std::cout << " reserving space for unseen blockers\n";
907  }
908  myLeadingBlockerLength = MAX2((SUMOReal)(right ? 20.0 : 40.0), myLeadingBlockerLength);
909  }
910 
911  // letting vehicles merge in at the end of the lane in case of counter-lane change, step#1
912  // if there is a leader and he wants to change to the opposite direction
913  saveBlockerLength(neighLead.first, lcaCounter);
914  if (*firstBlocked != neighLead.first) {
915  saveBlockerLength(*firstBlocked, lcaCounter);
916  }
917 
918  const SUMOReal remainingSeconds = ((ret & LCA_TRACI) == 0 ?
921  const SUMOReal plannedSpeed = informLeader(msgPass, blocked, myLca, neighLead, remainingSeconds);
922  if (plannedSpeed >= 0) {
923  // maybe we need to deal with a blocking follower
924  informFollower(msgPass, blocked, myLca, neighFollow, remainingSeconds, plannedSpeed);
925  }
926 
927  if (gDebugFlag2) {
928  std::cout << STEPS2TIME(currentTime)
929  << " veh=" << myVehicle.getID()
930  << " myLeftSpace=" << myLeftSpace
931  << " remainingSeconds=" << remainingSeconds
932  << " plannedSpeed=" << plannedSpeed
933  << "\n";
934  }
935  return ret;
936  }
937 
938  // VARIANT_15
939  if (roundaboutEdgesAhead > 1) {
940  // try to use the inner lanes of a roundabout to increase throughput
941  // unless we are approaching the exit
942  if (lca == LCA_LEFT) {
943  req = ret | lca | LCA_COOPERATIVE;
944  } else {
945  req = ret | LCA_STAY | LCA_COOPERATIVE;
946  }
947  if (!cancelRequest(req)) {
948  return ret | req;
949  }
950  }
951 
952  // let's also regard the case where the vehicle is driving on a highway...
953  // in this case, we do not want to get to the dead-end of an on-ramp
954  if (right) {
955  if (bestLaneOffset == 0 && myVehicle.getLane()->getSpeedLimit() > 80. / 3.6 && myLookAheadSpeed > SUMO_const_haltingSpeed) {
956  if (gDebugFlag2) {
957  std::cout << " veh=" << myVehicle.getID() << " does not want to get stranded on the on-ramp of a highway\n";
958  }
959  req = ret | LCA_STAY | LCA_STRATEGIC;
960  if (!cancelRequest(req)) {
961  return ret | req;
962  }
963  }
964  }
965  // --------
966 
967  // -------- make place on current lane if blocking follower
968  //if (amBlockingFollowerPlusNB()) {
969  // std::cout << myVehicle.getID() << ", " << currentDistAllows(neighDist, bestLaneOffset, laDist)
970  // << " neighDist=" << neighDist
971  // << " currentDist=" << currentDist
972  // << "\n";
973  //}
975  //&& ((myOwnState & myLcaCounter) == 0) // VARIANT_6 : counterNoHelp
976  && (changeToBest || currentDistAllows(neighDist, abs(bestLaneOffset) + 1, laDist))) {
977 
978  // VARIANT_2 (nbWhenChangingToHelp)
979  if (gDebugFlag2) {
980  std::cout << STEPS2TIME(currentTime)
981  << " veh=" << myVehicle.getID()
982  << " wantsChangeToHelp=" << (right ? "right" : "left")
983  << " state=" << myOwnState
984  << (((myOwnState & myLcaCounter) != 0) ? " (counter)" : "")
985  << "\n";
986  }
987  req = ret | lca | LCA_COOPERATIVE | LCA_URGENT ;//| LCA_CHANGE_TO_HELP;
988  if (!cancelRequest(req)) {
989  return ret | req;
990  }
991  }
992 
993  // --------
994 
995 
998  //if ((blocked & LCA_BLOCKED) != 0) {
999  // return ret;
1000  //}
1002 
1003  // -------- higher speed
1004  //if ((congested(neighLead.first) && neighLead.second < 20) || predInteraction(leader.first)) { //!!!
1005  // return ret;
1006  //}
1007  SUMOReal thisLaneVSafe = myVehicle.getLane()->getVehicleMaxSpeed(&myVehicle);
1008  SUMOReal neighLaneVSafe = neighLane.getVehicleMaxSpeed(&myVehicle);
1009  if (neighLead.first == 0) {
1010  neighLaneVSafe = MIN2(neighLaneVSafe, myCarFollowModel.followSpeed(&myVehicle, myVehicle.getSpeed(), neighDist, 0, 0));
1011  } else {
1012  // @todo: what if leader is below safe gap?!!!
1013  neighLaneVSafe = MIN2(neighLaneVSafe, myCarFollowModel.followSpeed(
1014  &myVehicle, myVehicle.getSpeed(), neighLead.second, neighLead.first->getSpeed(), neighLead.first->getCarFollowModel().getMaxDecel()));
1015  }
1016  if (leader.first == 0) {
1017  thisLaneVSafe = MIN2(thisLaneVSafe, myCarFollowModel.followSpeed(&myVehicle, myVehicle.getSpeed(), currentDist, 0, 0));
1018  } else {
1019  // @todo: what if leader is below safe gap?!!!
1020  thisLaneVSafe = MIN2(thisLaneVSafe, myCarFollowModel.followSpeed(&myVehicle, myVehicle.getSpeed(), leader.second, leader.first->getSpeed(), leader.first->getCarFollowModel().getMaxDecel()));
1021  }
1022 
1024  thisLaneVSafe = MIN2(thisLaneVSafe, vMax);
1025  neighLaneVSafe = MIN2(neighLaneVSafe, vMax);
1026  const SUMOReal relativeGain = (neighLaneVSafe - thisLaneVSafe) / MAX2(neighLaneVSafe,
1028 
1029  if (right) {
1030  // ONLY FOR CHANGING TO THE RIGHT
1031  if (thisLaneVSafe - 5 / 3.6 > neighLaneVSafe) {
1032  // ok, the current lane is faster than the right one...
1033  if (mySpeedGainProbability < 0) {
1034  mySpeedGainProbability /= 2.0;
1035  }
1036  } else {
1037  // ok, the current lane is not faster than the right one
1038  mySpeedGainProbability -= relativeGain;
1039 
1040  // honor the obligation to keep right (Rechtsfahrgebot)
1041  // XXX consider fast approaching followers on the current lane
1042  //const SUMOReal vMax = myLookAheadSpeed;
1043  const SUMOReal acceptanceTime = KEEP_RIGHT_ACCEPTANCE * vMax * MAX2((SUMOReal)1, myVehicle.getSpeed()) / myVehicle.getLane()->getSpeedLimit();
1044  SUMOReal fullSpeedGap = MAX2((SUMOReal)0, neighDist - myVehicle.getCarFollowModel().brakeGap(vMax));
1045  SUMOReal fullSpeedDrivingSeconds = MIN2(acceptanceTime, fullSpeedGap / vMax);
1046  if (neighLead.first != 0 && neighLead.first->getSpeed() < vMax) {
1047  fullSpeedGap = MAX2((SUMOReal)0, MIN2(fullSpeedGap,
1048  neighLead.second - myVehicle.getCarFollowModel().getSecureGap(
1049  vMax, neighLead.first->getSpeed(), neighLead.first->getCarFollowModel().getMaxDecel())));
1050  fullSpeedDrivingSeconds = MIN2(fullSpeedDrivingSeconds, fullSpeedGap / (vMax - neighLead.first->getSpeed()));
1051  }
1052  const SUMOReal deltaProb = (CHANGE_PROB_THRESHOLD_RIGHT
1053  * STEPS2TIME(DELTA_T)
1054  * (fullSpeedDrivingSeconds / acceptanceTime) / KEEP_RIGHT_TIME);
1055  myKeepRightProbability -= deltaProb;
1056 
1057  if (gDebugFlag2) {
1058  std::cout << STEPS2TIME(currentTime)
1059  << " veh=" << myVehicle.getID()
1060  << " vMax=" << vMax
1061  << " neighDist=" << neighDist
1062  << " brakeGap=" << myVehicle.getCarFollowModel().brakeGap(myVehicle.getSpeed())
1063  << " leaderSpeed=" << (neighLead.first == 0 ? -1 : neighLead.first->getSpeed())
1064  << " secGap=" << (neighLead.first == 0 ? -1 : myVehicle.getCarFollowModel().getSecureGap(
1065  myVehicle.getSpeed(), neighLead.first->getSpeed(), neighLead.first->getCarFollowModel().getMaxDecel()))
1066  << " acceptanceTime=" << acceptanceTime
1067  << " fullSpeedGap=" << fullSpeedGap
1068  << " fullSpeedDrivingSeconds=" << fullSpeedDrivingSeconds
1069  << " dProb=" << deltaProb
1070  << "\n";
1071  }
1073  req = ret | lca | LCA_KEEPRIGHT;
1074  if (!cancelRequest(req)) {
1075  return ret | req;
1076  }
1077  }
1078  }
1079 
1080  if (gDebugFlag2) {
1081  std::cout << STEPS2TIME(currentTime)
1082  << " veh=" << myVehicle.getID()
1083  << " speed=" << myVehicle.getSpeed()
1084  << " myKeepRightProbability=" << myKeepRightProbability
1085  << " thisLaneVSafe=" << thisLaneVSafe
1086  << " neighLaneVSafe=" << neighLaneVSafe
1087  << " relativeGain=" << relativeGain
1088  << " blocked=" << blocked
1089  << "\n";
1090  }
1091 
1093  && neighDist / MAX2((SUMOReal) .1, myVehicle.getSpeed()) > 20.) { //./MAX2((SUMOReal) .1, myVehicle.getSpeed())) { // -.1
1094  req = ret | lca | LCA_SPEEDGAIN;
1095  if (!cancelRequest(req)) {
1096  return ret | req;
1097  }
1098  }
1099  } else {
1100  // ONLY FOR CHANGING TO THE LEFT
1101  if (thisLaneVSafe > neighLaneVSafe) {
1102  // this lane is better
1103  if (mySpeedGainProbability > 0) {
1104  mySpeedGainProbability /= 2.0;
1105  }
1106  } else {
1107  // left lane is better
1108  mySpeedGainProbability += relativeGain;
1109  }
1110  // VARIANT_19 (stayRight)
1111  //if (neighFollow.first != 0) {
1112  // MSVehicle* nv = neighFollow.first;
1113  // const SUMOReal secGap = nv->getCarFollowModel().getSecureGap(nv->getSpeed(), myVehicle.getSpeed(), myVehicle.getCarFollowModel().getMaxDecel());
1114  // if (neighFollow.second < secGap * KEEP_RIGHT_HEADWAY) {
1115  // // do not change left if it would inconvenience faster followers
1116  // return ret | LCA_STAY | LCA_SPEEDGAIN;
1117  // }
1118  //}
1119  if (mySpeedGainProbability > CHANGE_PROB_THRESHOLD_LEFT && neighDist / MAX2((SUMOReal) .1, myVehicle.getSpeed()) > 20.) { // .1
1120  req = ret | lca | LCA_SPEEDGAIN;
1121  if (!cancelRequest(req)) {
1122  return ret | req;
1123  }
1124  }
1125  }
1126  // --------
1127  if (changeToBest && bestLaneOffset == curr.bestLaneOffset
1128  && (right ? mySpeedGainProbability < 0 : mySpeedGainProbability > 0)) {
1129  // change towards the correct lane, speedwise it does not hurt
1130  req = ret | lca | LCA_STRATEGIC;
1131  if (!cancelRequest(req)) {
1132  return ret | req;
1133  }
1134  }
1135  if (gDebugFlag2) {
1136  std::cout << STEPS2TIME(currentTime)
1137  << " veh=" << myVehicle.getID()
1138  << " mySpeedGainProbability=" << mySpeedGainProbability
1139  << " myKeepRightProbability=" << myKeepRightProbability
1140  << " thisLaneVSafe=" << thisLaneVSafe
1141  << " neighLaneVSafe=" << neighLaneVSafe
1142  << "\n";
1143  }
1144  return ret;
1145 }
1146 
1147 
1148 int
1150  // if this vehicle is blocking someone in front, we maybe decelerate to let him in
1151  if ((*blocked) != 0) {
1152  SUMOReal gap = (*blocked)->getPositionOnLane() - (*blocked)->getVehicleType().getLength() - myVehicle.getPositionOnLane() - myVehicle.getVehicleType().getMinGap();
1153  if (gDebugFlag2) {
1154  std::cout << STEPS2TIME(MSNet::getInstance()->getCurrentTimeStep())
1155  << " veh=" << myVehicle.getID()
1156  << " blocked=" << tryID(*blocked)
1157  << " gap=" << gap
1158  << "\n";
1159  }
1160  if (gap > POSITION_EPS) {
1161  //const bool blockedWantsUrgentRight = (((*blocked)->getLaneChangeModel().getOwnState() & LCA_RIGHT != 0)
1162  // && ((*blocked)->getLaneChangeModel().getOwnState() & LCA_URGENT != 0));
1163 
1165  //|| blockedWantsUrgentRight // VARIANT_10 (helpblockedRight)
1166  ) {
1167  if ((*blocked)->getSpeed() < SUMO_const_haltingSpeed) {
1168  state |= LCA_AMBACKBLOCKER_STANDING;
1169  } else {
1170  state |= LCA_AMBACKBLOCKER;
1171  }
1174  (SUMOReal)(gap - POSITION_EPS), (*blocked)->getSpeed(),
1175  (*blocked)->getCarFollowModel().getMaxDecel()));
1176  //(*blocked) = 0; // VARIANT_14 (furtherBlock)
1177  }
1178  }
1179  }
1180  return state;
1181 }
1182 
1183 
1184 void
1185 MSLCM_JE2013::saveBlockerLength(MSVehicle* blocker, int lcaCounter) {
1186  if (gDebugFlag2) {
1187  std::cout << STEPS2TIME(MSNet::getInstance()->getCurrentTimeStep())
1188  << " veh=" << myVehicle.getID()
1189  << " saveBlockerLength blocker=" << tryID(blocker)
1190  << " bState=" << (blocker == 0 ? "None" : toString(blocker->getLaneChangeModel().getOwnState()))
1191  << "\n";
1192  }
1193  if (blocker != 0 && (blocker->getLaneChangeModel().getOwnState() & lcaCounter) != 0) {
1194  // is there enough space in front of us for the blocker?
1197  if (blocker->getVehicleType().getLengthWithGap() <= potential) {
1198  // save at least his length in myLeadingBlockerLength
1200  if (gDebugFlag2) {
1201  std::cout << STEPS2TIME(MSNet::getInstance()->getCurrentTimeStep())
1202  << " veh=" << myVehicle.getID()
1203  << " blocker=" << tryID(blocker)
1204  << " saving myLeadingBlockerLength=" << myLeadingBlockerLength
1205  << "\n";
1206  }
1207  } else {
1208  // we cannot save enough space for the blocker. It needs to save
1209  // space for ego instead
1210  if (gDebugFlag2) {
1211  std::cout << STEPS2TIME(MSNet::getInstance()->getCurrentTimeStep())
1212  << " veh=" << myVehicle.getID()
1213  << " blocker=" << tryID(blocker)
1214  << " cannot save space=" << blocker->getVehicleType().getLengthWithGap()
1215  << " potential=" << potential
1216  << "\n";
1217  }
1219  }
1220  }
1221 }
1222 /****************************************************************************/
1223 
void * inform(void *info, MSVehicle *sender)
#define LOOK_AHEAD_MIN_SPEED
bool gDebugFlag1
global utility flags for debugging
Definition: StdDefs.cpp:99
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
MSEdge & getEdge() const
Returns the lane's edge.
Definition: MSLane.h:455
#define CHANGE_PROB_THRESHOLD_LEFT
Representation of a vehicle in the micro simulation.
Definition: MSVehicle.h:80
SUMOReal getMaxSpeed() const
Get vehicle's maximum speed [m/s].
#define SPEED2DIST(x)
Definition: SUMOTime.h:55
int slowDownForBlocked(MSVehicle **blocked, int state)
compute useful slowdowns for blocked vehicles
#define KEEP_RIGHT_TIME
#define min(a, b)
Definition: polyfonts.c:66
#define TURN_LANE_DIST
const MSCFModel & getCarFollowModel() const
Returns the vehicle's car following model definition.
Definition: MSVehicle.h:540
#define ACCEL2SPEED(x)
Definition: SUMOTime.h:61
#define URGENCY
bool currentDistAllows(SUMOReal dist, int laneOffset, SUMOReal lookForwardDist)
Definition: MSLCM_JE2013.h:164
const std::vector< MSLane * > & getLanes() const
Returns this edge's lanes.
Definition: MSEdge.h:186
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:466
bool congested() const
Definition: MSVehicle.h:393
virtual SUMOReal followSpeed(const MSVehicle *const veh, SUMOReal speed, SUMOReal gap2pred, SUMOReal predSpeed, SUMOReal predMaxDecel) const =0
Computes the vehicle's follow speed (no dawdling)
SUMOReal myLeadingBlockerLength
Definition: MSLCM_JE2013.h:181
The car-following model abstraction.
Definition: MSCFModel.h:59
void * informNeighFollower(void *info, MSVehicle *sender)
Informs the follower on the desired lane.
virtual ~MSLCM_JE2013()
SUMOReal getLength() const
Get vehicle's length [m].
SUMOReal myLeftSpace
Definition: MSLCM_JE2013.h:182
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...
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:159
T MAX2(T a, T b)
Definition: StdDefs.h:74
MSLCM_JE2013(MSVehicle &v)
std::vector< SUMOReal > myVSafes
Definition: MSLCM_JE2013.h:188
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:270
SUMOReal getPositionOnLane() const
Get the vehicle's position along the lane.
Definition: MSVehicle.h:286
#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:235
SUMOReal length
The overall length which may be driven when using this lane without a lane change.
Definition: MSVehicle.h:460
The action is urgent (to be defined by lc-model)
#define abs(a)
Definition: polyfonts.c:67
MSAbstractLaneChangeModel & getLaneChangeModel()
Definition: MSVehicle.cpp:1884
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
#define LCA_RIGHT_IMPATIENCE
std::pair< SUMOReal, int > Info
information regarding save velocity (unused) and state flags of the ego vehicle
Definition: MSLCM_JE2013.h:169
static bool myAllowOvertakingRight
whether overtaking on the right is permitted
A class responsible for exchanging messages between cars involved in lane-change interaction.
const std::string & getID() const
Returns the id.
Definition: Named.h:60
Wants go to the left.
#define max(a, b)
Definition: polyfonts.c:65
bool cancelRequest(int state)
whether the influencer cancels the given request
SUMOReal brakeGap(const SUMOReal speed) const
Returns the distance the vehicle needs to halt including driver's reaction time.
Definition: MSCFModel.h:232
#define CUT_IN_LEFT_SPEED_THRESHOLD
SUMOReal mySpeedGainProbability
a value for tracking the probability that a change to the offset with the same sign is beneficial ...
Definition: MSLCM_JE2013.h:175
SUMOReal getMinGap() const
Get the free space in front of vehicles of this class.
void prepareStep()
#define RELGAIN_NORMALIZATION_MIN_SPEED
#define JAM_FACTOR
#define LOOK_FORWARD_RIGHT
bool amBlockingFollowerPlusNB()
Definition: MSLCM_JE2013.h:158
SUMOReal informLeader(MSAbstractLaneChangeModel::MSLCMessager &msgPass, int blocked, int dir, const std::pair< MSVehicle *, SUMOReal > &neighLead, SUMOReal remainingSeconds)
SUMOReal _patchSpeed(const SUMOReal min, const SUMOReal wanted, const SUMOReal max, const MSCFModel &cfModel)
void saveBlockerLength(MSVehicle *blocker, int lcaCounter)
save space for vehicles which need to counter-lane-change
#define STEPS2TIME(x)
Definition: SUMOTime.h:65
#define MAX_ONRAMP_LENGTH
T MIN2(T a, T b)
Definition: StdDefs.h:68
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:189
SUMOReal getSpeedLimit() const
Returns the lane's maximum allowed speed.
Definition: MSLane.h:362
std::string tryID(const MSVehicle *v)
A structure representing the best lanes for continuing the route.
Definition: MSVehicle.h:456
SUMOReal getMaxDecel() const
Get the vehicle type's maximum deceleration [m/s^2].
Definition: MSCFModel.h:184
SUMOReal changeRequestRemainingSeconds(const SUMOTime currentTime) const
Return the remaining number of seconds of the current laneTimeLine assuming one exists.
Definition: MSVehicle.cpp:301
int myOwnState
The current state of the vehicle.
std::string toString(const T &t, std::streamsize accuracy=OUTPUT_ACCURACY)
Definition: ToString.h:53
void changed(int dir)
Wants go to the right.
#define HELP_OVERTAKE
virtual void saveBlockerLength(SUMOReal length)
reserve space at the end of the lane to avoid dead locks
#define HELP_DECEL_FACTOR
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:235
Influencer & getInfluencer()
Returns the velocity/lane influencer.
Definition: MSVehicle.cpp:2510
SUMOReal myKeepRightProbability
Definition: MSLCM_JE2013.h:179
#define LOOK_AHEAD_SPEED_MEMORY
#define ROUNDABOUT_DIST_BONUS
SUMOReal occupation
The overall vehicle sum on consecutive lanes which can be passed without a lane change.
Definition: MSVehicle.h:462
#define CHANGE_PROB_THRESHOLD_RIGHT
std::vector< MSLane * > bestContinuations
Consecutive lane that can be followed without a lane change (contribute to length and occupation) ...
Definition: MSVehicle.h:470
SUMOReal myLookAheadSpeed
Definition: MSLCM_JE2013.h:186
The action is due to the default of keeping right "Rechtsfahrgebot".
const MSVehicleType & getVehicleType() const
Returns the vehicle's type definition.
Definition: MSBaseVehicle.h:90
const SUMOReal SUMO_const_haltingSpeed
the speed threshold at which vehicles are considered as halting
Definition: StdDefs.h:54
Needs to stay on the current lane.
SUMOReal getSpeed() const
Returns the vehicle's current speed.
Definition: MSVehicle.h:294
int SUMOTime
Definition: SUMOTime.h:43
SUMOReal getWaitingSeconds() const
Returns the number of seconds waited (speed was lesser than 0.1m/s)
Definition: MSVehicle.h:370
#define MIN_FALLBEHIND
#define KEEP_RIGHT_ACCEPTANCE
bool gDebugFlag2
Definition: StdDefs.cpp:100
#define SUMOReal
Definition: config.h:218
#define LOOK_FORWARD_LEFT
#define NUMERICAL_EPS
Definition: config.h:162
#define DELTA_T
Definition: SUMOTime.h:50
#define DEBUG_COND
const MSLinkCont & getLinkCont() const
returns the container with all links !!!
Definition: MSLane.cpp:1026
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:331
int influenceChangeDecision(int state)
allow TraCI to influence a lane change decision
Definition: MSVehicle.cpp:2528
The edge is an internal edge.
Definition: MSEdge.h:98
Representation of a lane in the micro simulation.
Definition: MSLane.h:77
const MSCFModel & myCarFollowModel
The vehicle's car following model.
bool currentDistDisallows(SUMOReal dist, int laneOffset, SUMOReal lookForwardDist)
Definition: MSLCM_JE2013.h:161
Interface for lane-change models.
int getBestLaneOffset() const
returns the current offset from the best lane
Definition: MSVehicle.cpp:2196
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.
const std::string & getID() const
Returns the name of the vehicle.
The action is due to the wish to be faster (tactical lc)