77 #include <mesosim/MESegment.h>
78 #include <mesosim/MELoop.h>
82 #ifdef CHECK_MEMORY_LEAKS
84 #endif // CHECK_MEMORY_LEAKS
87 #ifdef DEBUG_VEHICLE_GUI_SELECTION
94 #define BUS_STOP_OFFSET 0.5
96 #define CRLL_LOOK_AHEAD 5
127 return (myPos != state.
myPos ||
139 myPos(pos), mySpeed(speed) {}
147 mySpeedAdaptationStarted(true),
148 myConsiderSafeVelocity(true),
149 myConsiderMaxAcceleration(true),
150 myConsiderMaxDeceleration(true),
151 myAmVTDControlled(false),
165 mySpeedAdaptationStarted =
true;
166 mySpeedTimeLine = speedTimeLine;
172 myLaneTimeLine = laneTimeLine;
179 myOriginalSpeed = speed;
181 while (mySpeedTimeLine.size() == 1 || (mySpeedTimeLine.size() > 1 && currentTime > mySpeedTimeLine[1].first)) {
182 mySpeedTimeLine.erase(mySpeedTimeLine.begin());
185 if (mySpeedTimeLine.size() < 2 || currentTime < mySpeedTimeLine[0].first) {
189 if (!mySpeedAdaptationStarted) {
190 mySpeedTimeLine[0].second = speed;
191 mySpeedAdaptationStarted =
true;
195 speed = mySpeedTimeLine[0].second - (mySpeedTimeLine[0].second - mySpeedTimeLine[1].second) * td;
196 if (myConsiderSafeVelocity) {
197 speed =
MIN2(speed, vSafe);
199 if (myConsiderMaxAcceleration) {
200 speed =
MIN2(speed, vMax);
202 if (myConsiderMaxDeceleration) {
203 speed =
MAX2(speed, vMin);
212 while (myLaneTimeLine.size() == 1 || (myLaneTimeLine.size() > 1 && currentTime > myLaneTimeLine[1].first)) {
213 myLaneTimeLine.erase(myLaneTimeLine.begin());
217 if (myLaneTimeLine.size() >= 2 && currentTime >= myLaneTimeLine[0].first) {
218 const unsigned int destinationLaneIndex = myLaneTimeLine[1].second;
219 if (destinationLaneIndex < (
unsigned int)currentEdge.
getLanes().size()) {
220 if (currentLaneIndex > destinationLaneIndex) {
222 }
else if (currentLaneIndex < destinationLaneIndex) {
234 mode = myStrategicLC;
236 mode = myCooperativeLC;
238 mode = mySpeedGainLC;
240 mode = myRightDriveLC;
248 state &= ~LCA_WANTS_LANECHANGE_OR_STAY;
256 state &= ~LCA_WANTS_LANECHANGE_OR_STAY;
277 switch (changeRequest) {
293 assert(myLaneTimeLine.size() >= 2);
294 assert(currentTime >= myLaneTimeLine[0].first);
295 return STEPS2TIME(myLaneTimeLine[1].first - currentTime);
301 myConsiderSafeVelocity = value;
307 myConsiderMaxAcceleration = value;
323 myConsiderMaxDeceleration = value;
331 if (myVTDRoute.size() != 0) {
335 if (myVTDPos > myVTDLane->getLength()) {
336 myVTDPos = myVTDLane->getLength();
338 myVTDLane->forceVehicleInsertion(v, myVTDPos);
340 myAmVTDControlled =
false;
354 (*i)->resetPartialOccupation(
this);
358 if ((*i).myLink != 0) {
359 (*i).myLink->removeApproaching(
this);
394 for (std::vector<SUMOVehicleParameter::Stop>::iterator i = pars->
stops.begin(); i != pars->
stops.end(); ++i) {
397 "' on lane '" + i->lane +
"' is not downstream the current route.");
400 for (std::vector<SUMOVehicleParameter::Stop>::const_iterator i = route->
getStops().begin(); i != route->
getStops().end(); ++i) {
403 "' on lane '" + i->lane +
"' is not downstream the current route.");
406 const MSLane*
const depLane = (*myCurrEdge)->getDepartLane(*
this);
408 throw ProcessError(
"Invalid departlane definition for vehicle '" + pars->
id +
"'.");
412 WRITE_WARNING(
"Choosing new speed factor for vehicle '" + pars->
id +
"' to match departure speed.");
416 "' is too high for the departure lane '" + depLane->
getID() +
"'.");
421 "' is too high for the vehicle type '" + type->
getID() +
"'.");
432 if ((*i).myLink != 0) {
433 (*i).myLink->removeApproaching(
this);
477 for (std::list<Stop>::iterator iter =
myStops.begin(); iter !=
myStops.end();) {
478 if (find(edges.begin(), edges.end(), &iter->lane->getEdge()) == edges.end()) {
481 iter->edge = find(edges.begin(), edges.end(), &iter->lane->getEdge());
537 if (!rem->first->notifyMove(*
this, oldPos + rem->second, newPos + rem->second, newSpeed)) {
539 if (myTraceMoveReminders) {
540 traceMoveReminder(
"notifyMove", rem->first, rem->second,
false);
546 if (myTraceMoveReminders) {
547 traceMoveReminder(
"notifyMove", rem->first, rem->second,
true);
562 rem->second += oldLaneLength;
564 if (myTraceMoveReminders) {
565 traceMoveReminder(
"adaptedPos", rem->first, rem->second,
true);
587 if (offset == 0. && !changingLanes) {
610 atan2(p1.
x() - p2.
x(), p2.
y() - p1.
y()) * 180. /
PI :
631 if (stop.
until != -1) {
632 stop.
until += untilOffset;
644 std::list<Stop>::iterator iter =
myStops.begin();
647 prevStopEdge =
myStops.back().edge;
648 prevStopPos =
myStops.back().endPos;
654 while (iter !=
myStops.end() && (iter->edge < stop.
edge ||
655 (iter->endPos < stop.
endPos && iter->edge == stop.
edge))) {
656 prevStopEdge = iter->edge;
657 prevStopPos = iter->endPos;
661 int index = stopPar.
index;
663 prevStopEdge = iter->edge;
664 prevStopPos = iter->endPos;
672 (prevStopEdge == stop.
edge && prevStopPos > stop.
endPos)) {
685 pos += (*myCurrEdge)->getLength();
722 return currentVelocity;
733 for (std::vector<MSPerson*>::const_iterator i = persons.begin(); i != persons.end(); ++i) {
775 bool busStopsMustHaveSpace =
true;
780 busStopsMustHaveSpace =
false;
789 if (stop.
until >= 0) {
805 return currentVelocity;
819 #ifdef DEBUG_VEHICLE_GUI_SELECTION
825 for (DriveItemVector::iterator i = lfLinks.begin(); i != lfLinks.end(); ++i) {
826 if ((*i).myLink != 0) {
827 (*i).myLink->removeApproaching(
this);
831 #ifdef HAVE_INTERNAL_LANES
832 myLinkLeaders.clear();
854 assert(bestLaneConts.size() > 0);
855 #ifdef HAVE_INTERNAL_LANES
856 bool hadNonInternal =
false;
858 bool hadNonInternal =
true;
863 unsigned int view = 0;
873 std::pair<const MSVehicle*, SUMOReal> leaderInfo = std::make_pair(pred, gap);
879 adaptToLeader(leaderInfo, seen, lastLink, lane, v, vLinkPass);
890 v =
MIN2(v, stopSpeed);
897 MSLinkCont::const_iterator link =
myLane->
succLinkSec(*
this, view + 1, *lane, bestLaneConts);
936 (*link)->getViaLaneOrLane()->getParallelLane(-
getLaneChangeModel().getLaneChangeDirection()) == 0)) {
938 const SUMOReal va = seen / timeRemaining;
946 const bool setRequest = v > 0;
956 #ifdef HAVE_INTERNAL_LANES
959 for (MSLink::LinkLeaders::const_iterator it = linkLeaders.begin(); it != linkLeaders.end(); ++it) {
962 if (leader->myLinkLeaders.count(
getID()) == 0) {
964 myLinkLeaders.insert(leader->
getID());
981 if (!(*link)->havePriority() && stopDist > cfModel.
getMaxDecel()) {
987 + (seen - (v1 * v1 - arrivalSpeed * arrivalSpeed) * 0.5 / cfModel.
getMaxDecel()) / vLinkWait);
990 const SUMOReal accelTime = (vLinkPass - v) / accel;
991 const SUMOReal accelWay = accelTime * (vLinkPass + v) * 0.5;
1003 arrivalSpeedBraking = arrivalSpeed;
1008 arrivalSpeedBraking =
MIN2(arrivalSpeedBraking, arrivalSpeed);
1009 arrivalTimeBraking =
MAX2(arrivalTime, t +
TIME2STEPS(seen / ((v + arrivalSpeedBraking) * 0.5)));
1012 arrivalTime, arrivalSpeed,
1013 arrivalTimeBraking, arrivalSpeedBraking,
1016 #ifdef HAVE_INTERNAL_LANES
1017 if ((*link)->getViaLane() == 0) {
1018 hadNonInternal =
true;
1025 if (!setRequest || ((v <= 0 || seen > dist) && hadNonInternal && seenNonInternal > vehicleLength *
CRLL_LOOK_AHEAD)) {
1029 lane = (*link)->getViaLaneOrLane();
1041 lastLink = &lfLinks.back();
1051 if (leaderInfo.first != 0) {
1054 if (leaderInfo.second >= 0) {
1055 vsafeLeader = cfModel.
followSpeed(
this,
getSpeed(), leaderInfo.second, leaderInfo.first->getSpeed(), leaderInfo.first->getCarFollowModel().getMaxDecel());
1061 if (lastLink != 0) {
1064 v =
MIN2(v, vsafeLeader);
1065 vLinkPass =
MIN2(vLinkPass, vsafeLeader);
1072 #ifdef DEBUG_VEHICLE_GUI_SELECTION
1093 DriveItemVector::iterator i;
1094 bool braking =
false;
1095 bool lastWasGreenCont =
false;
1097 MSLink* link = (*i).myLink;
1099 if (link != 0 && (*i).mySetRequest) {
1105 vSafe = (*i).myVLinkWait;
1107 lastWasGreenCont =
false;
1112 const bool opened = yellow || link->
opened((*i).myArrivalTime, (*i).myArrivalSpeed, (*i).getLeaveSpeed(),
1115 if (opened && !lastWasGreenCont && !link->
havePriority()) {
1117 vSafe = (*i).myVLinkWait;
1119 lastWasGreenCont =
false;
1137 vSafe = (*i).myVLinkPass;
1144 lastWasGreenCont =
false;
1145 vSafe = (*i).myVLinkWait;
1153 vSafe = (*i).myVLinkWait;
1209 std::vector<MSLane*> passedLanes;
1211 passedLanes.push_back(*i);
1213 if (passedLanes.size() == 0 || passedLanes.back() !=
myLane) {
1214 passedLanes.push_back(
myLane);
1229 MSLink* link = (*i).myLink;
1239 if (approachedLane !=
myLane && approachedLane != 0) {
1247 WRITE_WARNING(
"Vehicle '" +
getID() +
"' could not finish continuous lane change (turn lane) time=" +
1258 passedLanes.push_back(approachedLane);
1264 (*i)->resetPartialOccupation(
this);
1284 std::vector<MSLane*>::reverse_iterator i = passedLanes.rbegin() + 1;
1285 while (leftLength > 0 && i != passedLanes.rend()) {
1287 leftLength -= (*i)->setPartialOccupation(
this, leftLength);
1309 for (MSLane::VehCont::const_iterator i = vehs.begin(); i != vehs.end(); ++i) {
1311 foundStopped =
true;
1312 const SUMOReal ret = (*i)->getPositionOnLane() - (*i)->getVehicleType().getLengthWithGap() - lengths;
1316 lengths += (*i)->getVehicleType().getLengthWithGap();
1325 #ifdef DEBUG_VEHICLE_GUI_SELECTION
1333 #ifdef HAVE_INTERNAL_LANES
1335 bool hadVehicle =
false;
1336 SUMOReal seenSpace = -lengthsInFront;
1338 bool foundStopped =
false;
1341 for (
unsigned int i = 0; i < lfLinks.size(); ++i) {
1344 if (item.
myLink == 0 || foundStopped) {
1350 const MSLane* approachedLane = item.
myLink->getViaLane();
1351 if (approachedLane != 0) {
1375 foundStopped =
true;
1378 seenSpace += approachedLane->
getLength();
1392 foundStopped =
true;
1400 #ifdef DEBUG_VEHICLE_GUI_SELECTION
1406 for (
int i = (
int)(lfLinks.size() - 1); i > 0; --i) {
1412 bool allowsContinuation = item.
myLink == 0 || item.
myLink->
isCont() || !lfLinks[i].hadVehicle || opened;
1413 if (!opened && item.
myLink != 0) {
1417 allowsContinuation =
true;
1421 if (allowsContinuation) {
1427 int removalBegin = -1;
1428 for (
unsigned int i = 0; hadVehicle && i < lfLinks.size() && removalBegin < 0; ++i) {
1442 if (leftSpace < 0/* && item.myLink->willHaveBlockedFoe()*/) {
1449 if (leftSpace < -impatienceCorrection / 10. && item.myLink->isCrossing()) {
1457 while (removalBegin < (
int)(lfLinks.size())) {
1459 lfLinks[removalBegin].myVLinkPass = lfLinks[removalBegin].myVLinkWait;
1461 lfLinks[removalBegin].mySetRequest =
false;
1470 for (DriveItemVector::iterator i = lfLinks.begin(); i != lfLinks.end(); ++i) {
1471 if ((*i).myLink != 0) {
1475 (*i).myLink->setApproaching(
this, (*i).myArrivalTime, (*i).myArrivalSpeed, (*i).getLeaveSpeed(),
1476 (*i).mySetRequest, (*i).myArrivalTimeBraking, (*i).myArrivalSpeedBraking,
getWaitingTime());
1485 if (rem->first->getLane() != 0 && rem->first->getLane() !=
getLane()) {
1487 if (myTraceMoveReminders) {
1488 traceMoveReminder(
"notifyEnter_skipped", rem->first, rem->second,
true);
1493 if (rem->first->notifyEnter(*
this, reason)) {
1495 if (myTraceMoveReminders) {
1496 traceMoveReminder(
"notifyEnter", rem->first, rem->second,
true);
1502 if (myTraceMoveReminders) {
1503 traceMoveReminder(
"notifyEnter", rem->first, rem->second,
false);
1533 if (!onTeleporting) {
1569 while (i != route.
begin() && leftLength > 0) {
1576 leftLength -= (lane)->setPartialOccupation(
this, leftLength);
1617 MSLane* clane = enteredLane;
1618 while (leftLength > 0) {
1624 leftLength -= (clane)->setPartialOccupation(
this, leftLength);
1632 if (rem->first->notifyLeave(*
this,
myState.
myPos + rem->second, reason)) {
1634 if (myTraceMoveReminders) {
1635 traceMoveReminder(
"notifyLeave", rem->first, rem->second,
true);
1641 if (myTraceMoveReminders) {
1642 traceMoveReminder(
"notifyLeave", rem->first, rem->second,
false);
1650 (*i)->resetPartialOccupation(
this);
1672 const std::vector<MSVehicle::LaneQ>&
1674 #ifdef DEBUG_VEHICLE_GUI_SELECTION
1681 if (startLane == 0) {
1684 assert(startLane != 0);
1687 std::vector<LaneQ>::iterator i;
1688 for (i = lanes.begin(); i != lanes.end(); ++i) {
1690 for (std::vector<MSLane*>::const_iterator j = (*i).bestContinuations.begin() + 1; j != (*i).bestContinuations.end(); ++j) {
1691 nextOccupation += (*j)->getBruttoVehLenSum();
1693 (*i).nextOccupation = nextOccupation;
1694 if ((*i).lane == startLane) {
1713 std::vector<LaneQ>& lanes = *it;
1714 assert(lanes.size() > 0);
1715 if (&(lanes[0].lane->getEdge()) == nextEdge) {
1717 std::vector<LaneQ> oldLanes = lanes;
1719 const std::vector<MSLane*>& sourceLanes = startLane->
getEdge().
getLanes();
1720 for (std::vector<MSLane*>::const_iterator it_source = sourceLanes.begin(); it_source != sourceLanes.end(); ++it_source) {
1721 for (std::vector<LaneQ>::iterator it_lane = oldLanes.begin(); it_lane != oldLanes.end(); ++it_lane) {
1722 if ((*it_source)->getLinkCont()[0]->getLane() == (*it_lane).lane) {
1723 lanes.push_back(*it_lane);
1730 for (
int i = 0; i < (
int)lanes.size(); ++i) {
1731 if (i + lanes[i].bestLaneOffset < 0) {
1732 lanes[i].bestLaneOffset = -i;
1734 if (i + lanes[i].bestLaneOffset >= (
int)lanes.size()) {
1735 lanes[i].bestLaneOffset = (
int)lanes.size() - i - 1;
1737 assert(i + lanes[i].bestLaneOffset >= 0);
1738 assert(i + lanes[i].bestLaneOffset < (
int)lanes.size());
1739 if (lanes[i].bestContinuations[0] != 0) {
1741 lanes[i].bestContinuations.insert(lanes[i].bestContinuations.begin(), (
MSLane*)0);
1743 if (startLane->
getLinkCont()[0]->getLane() == lanes[i].lane) {
1746 assert(&(lanes[i].lane->getEdge()) == nextEdge);
1758 myLastBestLanesEdge = &startLane->
getEdge();
1762 const MSEdge* nextStopEdge = 0;
1763 const MSLane* nextStopLane = 0;
1767 nextStopLane = nextStop.
lane;
1768 nextStopEdge = &nextStopLane->
getEdge();
1776 if (nextStopEdge != 0) {
1785 bool progress =
true;
1787 std::vector<LaneQ> currentLanes;
1788 const std::vector<MSLane*>* allowed = 0;
1789 const MSEdge* nextEdge = 0;
1791 nextEdge = *(ce + 1);
1794 const std::vector<MSLane*>& lanes = (*ce)->getLanes();
1795 for (std::vector<MSLane*>::const_iterator i = lanes.begin(); i != lanes.end(); ++i) {
1802 q.
allowsContinuation = allowed == 0 || find(allowed->begin(), allowed->end(), cl) != allowed->end();
1803 currentLanes.push_back(q);
1806 if (nextStopEdge == *ce) {
1808 for (std::vector<LaneQ>::iterator q = currentLanes.begin(); q != currentLanes.end(); ++q) {
1809 if (nextStopLane != 0 && nextStopLane != (*q).lane) {
1810 (*q).allowsContinuation =
false;
1811 (*q).length = nextStopPos;
1818 seenLength += currentLanes[0].lane->getLength();
1820 progress &= (seen <= 4 || seenLength < 3000);
1821 progress &= seen <= 8;
1833 int bestThisIndex = 0;
1836 for (std::vector<LaneQ>::iterator j = last.begin(); j != last.end(); ++j, ++index) {
1837 if ((*j).length > bestLength) {
1838 bestLength = (*j).length;
1839 bestThisIndex = index;
1843 for (std::vector<LaneQ>::iterator j = last.begin(); j != last.end(); ++j, ++index) {
1844 if ((*j).length < bestLength) {
1845 (*j).bestLaneOffset = bestThisIndex - index;
1852 for (std::vector<std::vector<LaneQ> >::reverse_iterator i =
myBestLanes.rbegin() + 1; i !=
myBestLanes.rend(); ++i) {
1853 std::vector<LaneQ>& nextLanes = (*(i - 1));
1854 std::vector<LaneQ>& clanes = (*i);
1855 MSEdge& cE = clanes[0].lane->getEdge();
1859 for (std::vector<LaneQ>::iterator j = nextLanes.begin(); j != nextLanes.end(); ++j, ++index) {
1860 if ((*j).lane->isApproachedFrom(&cE) && bestConnectedLength < (*j).length) {
1861 bestConnectedLength = (*j).length;
1863 if (bestLength < (*j).length) {
1864 bestLength = (*j).length;
1868 int bestThisIndex = 0;
1869 if (bestConnectedLength > 0) {
1871 for (std::vector<LaneQ>::iterator j = clanes.begin(); j != clanes.end(); ++j, ++index) {
1872 LaneQ bestConnectedNext;
1873 bestConnectedNext.
length = -1;
1874 if ((*j).allowsContinuation) {
1875 for (std::vector<LaneQ>::const_iterator m = nextLanes.begin(); m != nextLanes.end(); ++m) {
1876 if ((*m).lane->isApproachedFrom(&cE, (*j).lane)) {
1877 if (bestConnectedNext.
length < (*m).length || (bestConnectedNext.
length == (*m).length &&
abs(bestConnectedNext.
bestLaneOffset) >
abs((*m).bestLaneOffset))) {
1878 bestConnectedNext = *m;
1883 (*j).
length += bestLength;
1885 (*j).length += bestConnectedNext.
length;
1889 if (clanes[bestThisIndex].length < (*j).length || (clanes[bestThisIndex].length == (*j).length &&
abs(clanes[bestThisIndex].bestLaneOffset) >
abs((*j).bestLaneOffset))) {
1890 bestThisIndex = index;
1896 int bestNextIndex = 0;
1897 int bestDistToNeeded = (
int) clanes.size();
1899 for (std::vector<LaneQ>::iterator j = clanes.begin(); j != clanes.end(); ++j, ++index) {
1900 if ((*j).allowsContinuation) {
1902 for (std::vector<LaneQ>::const_iterator m = nextLanes.begin(); m != nextLanes.end(); ++m, ++nextIndex) {
1903 if ((*m).lane->isApproachedFrom(&cE, (*j).lane)) {
1904 if (bestDistToNeeded >
abs((*m).bestLaneOffset)) {
1905 bestDistToNeeded =
abs((*m).bestLaneOffset);
1906 bestThisIndex = index;
1907 bestNextIndex = nextIndex;
1913 clanes[bestThisIndex].length += nextLanes[bestNextIndex].length;
1914 copy(nextLanes[bestNextIndex].bestContinuations.begin(), nextLanes[bestNextIndex].bestContinuations.end(), back_inserter(clanes[bestThisIndex].bestContinuations));
1919 for (std::vector<LaneQ>::iterator j = clanes.begin(); j != clanes.end(); ++j, ++index) {
1920 if ((*j).length < clanes[bestThisIndex].length || ((*j).length == clanes[bestThisIndex].length &&
abs((*j).bestLaneOffset) >
abs(clanes[bestThisIndex].bestLaneOffset))) {
1921 (*j).bestLaneOffset = bestThisIndex - index;
1923 (*j).bestLaneOffset = 0;
1929 std::vector<LaneQ>& currLanes = *
myBestLanes.begin();
1930 std::vector<LaneQ>::iterator i;
1931 for (i = currLanes.begin(); i != currLanes.end(); ++i) {
1933 for (std::vector<MSLane*>::const_iterator j = (*i).bestContinuations.begin() + 1; j != (*i).bestContinuations.end(); ++j) {
1934 nextOccupation += (*j)->getBruttoVehLenSum();
1936 (*i).nextOccupation = nextOccupation;
1937 if ((*i).lane == startLane) {
1945 const std::vector<MSLane*>&
1950 return (*myCurrentLaneInBestLanes).bestContinuations;
1954 const std::vector<MSLane*>&
1965 if ((*i).lane == lane) {
1966 return (*i).bestContinuations;
1978 return (*myCurrentLaneInBestLanes).bestLaneOffset;
1996 #ifdef DEBUG_VEHICLE_GUI_SELECTION
2001 if (
isOnRoad() && destEdge != NULL) {
2065 unsigned int numExpected = (
unsigned int)
myStops.front().awaitedPersons.size();
2066 if (numExpected != 0) {
2071 numExpected = (
unsigned int)
myStops.front().awaitedPersons.size();
2073 if (numExpected == 0) {
2099 switch ((*link)->getDirection()) {
2144 for (std::list<Stop>::iterator iter =
myStops.begin(); iter !=
myStops.end(); iter++) {
2145 if (iter->lane == lane && fabs(iter->endPos - pos) <
POSITION_EPS) {
2146 if (duration == 0 && !iter->reached) {
2149 iter->duration = duration;
2222 std::vector<int> internals;
2235 throw ProcessError(
"Error: Invalid vehicles in state (may be a meso state)!");
2237 unsigned int routeOffset;
2242 myDeparture -= offset;