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