47 #define MIN_GREEN_TIME 5 50 #define DEBUGCOND true 56 const std::vector<NBNode*>& junctions,
SUMOTime offset,
59 myHaveSinglePhase(false) {
105 for (
int e1l = 0; e1l < e1->
getNumLanes(); e1l++) {
107 for (
int e2l = 0; e2l < e2->
getNumLanes(); e2l++) {
109 for (std::vector<NBEdge::Connection>::iterator e1c = approached1.begin(); e1c != approached1.end(); ++e1c) {
113 for (std::vector<NBEdge::Connection>::iterator e2c = approached2.begin(); e2c != approached2.end(); ++e2c) {
117 if (!
forbids(e1, (*e1c).toEdge, e2, (*e2c).toEdge,
true)) {
129 std::pair<NBEdge*, NBEdge*>
131 std::pair<NBEdge*, NBEdge*> bestPair(static_cast<NBEdge*>(0), static_cast<NBEdge*>(0));
132 double bestValue = -1;
133 for (EdgeVector::const_iterator i = edges.begin(); i != edges.end(); ++i) {
134 for (EdgeVector::const_iterator j = i + 1; j != edges.end(); ++j) {
136 if (value > bestValue) {
138 bestPair = std::pair<NBEdge*, NBEdge*>(*i, *j);
139 }
else if (value == bestValue) {
141 const double oa =
GeomHelper::getMinAngleDiff(bestPair.first->getAngleAtNode(bestPair.first->getToNode()), bestPair.second->getAngleAtNode(bestPair.second->getToNode()));
143 if (bestPair.first->getID() < (*i)->getID()) {
144 bestPair = std::pair<NBEdge*, NBEdge*>(*i, *j);
146 }
else if (oa < ca) {
147 bestPair = std::pair<NBEdge*, NBEdge*>(*i, *j);
156 std::pair<NBEdge*, NBEdge*>
158 if (incoming.size() == 1) {
160 std::pair<NBEdge*, NBEdge*> ret(*incoming.begin(),
static_cast<NBEdge*
>(0));
168 used.push_back(*incoming.begin());
171 for (EdgeVector::iterator i = incoming.begin() + 1; i != incoming.end() && prio ==
getToPrio(*i); ++i) {
175 if (used.size() < 2) {
179 #ifdef DEBUG_STREAM_ORDERING 185 incoming.erase(find(incoming.begin(), incoming.end(), ret.first));
186 incoming.erase(find(incoming.begin(), incoming.end(), ret.second));
207 std::vector<bool> isTurnaround;
208 std::vector<int> fromLanes;
211 for (
int i1 = 0; i1 < (int)incoming.size(); i1++) {
212 int noLanes = incoming[i1]->getNumLanes();
213 noLanesAll += noLanes;
214 for (
int i2 = 0; i2 < noLanes; i2++) {
215 NBEdge* fromEdge = incoming[i1];
217 noLinksAll += (int) approached.size();
218 for (
int i3 = 0; i3 < (int)approached.size(); i3++) {
219 if (!fromEdge->
mayBeTLSControlled(i2, approached[i3].toEdge, approached[i3].toLane)) {
223 assert(i3 < (
int)approached.size());
224 NBEdge* toEdge = approached[i3].toEdge;
225 fromEdges.push_back(fromEdge);
226 fromLanes.push_back((
int)i2);
227 toEdges.push_back(toEdge);
231 isTurnaround.push_back(
true);
237 std::vector<NBNode::Crossing*> crossings;
239 const std::vector<NBNode::Crossing*>& c = (*i)->getCrossings();
242 (*i)->setCrossingTLIndices(
getID(), noLinksAll);
244 copy(c.begin(), c.end(), std::back_inserter(crossings));
245 noLinksAll += (int)c.size();
253 std::vector<int> greenPhases;
254 std::vector<bool> hadGreenMajor(noLinksAll,
false);
255 while (toProc.size() > 0) {
256 std::pair<NBEdge*, NBEdge*> chosen;
257 if (incoming.size() == 2) {
260 double angle = fabs(
NBHelpers::relAngle(incoming[0]->getAngleAtNode(incoming[0]->getToNode()), incoming[1]->getAngleAtNode(incoming[1]->getToNode())));
263 chosen = std::pair<NBEdge*, NBEdge*>(toProc[0],
static_cast<NBEdge*
>(0));
264 toProc.erase(toProc.begin());
272 std::string state((
int) noLinksAll,
'r');
275 for (
int i1 = 0; i1 < (int) incoming.size(); ++i1) {
276 NBEdge* fromEdge = incoming[i1];
277 const bool inChosen = fromEdge == chosen.first || fromEdge == chosen.second;
279 for (
int i2 = 0; i2 < numLanes; i2++) {
281 for (
int i3 = 0; i3 < (int)approached.size(); ++i3) {
282 if (!fromEdge->
mayBeTLSControlled(i2, approached[i3].toEdge, approached[i3].toLane)) {
297 for (
int i1 = 0; i1 < pos; ++i1) {
298 if (state[i1] ==
'G') {
303 if (state[i2] ==
'G' && !isTurnaround[i2] &&
304 (
forbids(fromEdges[i2], toEdges[i2], fromEdges[i1], toEdges[i1],
true) ||
forbids(fromEdges[i1], toEdges[i1], fromEdges[i2], toEdges[i2],
true))) {
308 if (!isForbidden && !
hasCrossing(fromEdges[i1], toEdges[i1], crossings)) {
314 bool haveForbiddenLeftMover =
false;
315 std::vector<bool> rightTurnConflicts(pos,
false);
316 state =
correctConflicting(state, fromEdges, toEdges, isTurnaround, fromLanes, hadGreenMajor, haveForbiddenLeftMover, rightTurnConflicts);
317 for (
int i1 = 0; i1 < pos; ++i1) {
318 if (state[i1] ==
'G') {
319 hadGreenMajor[i1] =
true;
323 const std::string vehicleState = state;
324 greenPhases.push_back((
int)logic->
getPhases().size());
325 state =
addPedestrianPhases(logic, greenTime, minDur, maxDur, state, crossings, fromEdges, toEdges);
327 for (
int i1 = pos; i1 < pos + (int)crossings.size(); ++i1) {
330 const bool buildLeftGreenPhase = haveForbiddenLeftMover && !
myHaveSinglePhase && leftTurnTime > 0;
331 if (brakingTime > 0) {
333 for (
int i1 = 0; i1 < pos; ++i1) {
334 if (state[i1] !=
'G' && state[i1] !=
'g') {
337 if ((vehicleState[i1] >=
'a' && vehicleState[i1] <=
'z') && buildLeftGreenPhase && !rightTurnConflicts[i1]) {
343 logic->
addStep(brakingTime, state);
349 if (buildLeftGreenPhase) {
351 for (
int i1 = 0; i1 < pos; ++i1) {
352 if (state[i1] ==
'Y' || state[i1] ==
'y') {
356 if (state[i1] ==
'g') {
361 state =
correctConflicting(state, fromEdges, toEdges, isTurnaround, fromLanes, hadGreenMajor, haveForbiddenLeftMover, rightTurnConflicts);
364 logic->
addStep(leftTurnTime, state, minDur, maxDur);
367 if (brakingTime > 0) {
368 for (
int i1 = 0; i1 < pos; ++i1) {
369 if (state[i1] !=
'G' && state[i1] !=
'g') {
375 logic->
addStep(brakingTime, state);
382 if (crossings.size() > 0) {
386 if (logic->
getPhases().size() == 2 && brakingTime > 0
389 logic->
addStep(redTime, std::string(noLinksAll,
'r'));
398 for (std::vector<int>::const_iterator it = greenPhases.begin(); it != greenPhases.end(); ++it) {
400 greenPhaseTime += dur;
401 minGreenDuration =
MIN2(minGreenDuration, dur);
403 const int patchSeconds = (int)(
STEPS2TIME(cycleTime - totalDuration) / greenPhases.size());
404 const int patchSecondsRest = (int)(
STEPS2TIME(cycleTime - totalDuration)) - patchSeconds * (
int)greenPhases.size();
408 || greenPhases.size() == 0) {
414 for (std::vector<int>::const_iterator it = greenPhases.begin(); it != greenPhases.end(); ++it) {
417 if (greenPhases.size() > 0) {
427 if (totalDuration > 0) {
428 if (totalDuration > 3 * (greenTime + 2 * brakingTime + leftTurnTime)) {
443 for (
auto c : crossings) {
447 for (EdgeVector::const_iterator it_e = cross.
edges.begin(); it_e != cross.
edges.end(); ++it_e) {
448 const NBEdge* edge = *it_e;
449 if (edge == from || edge == to) {
462 std::string state,
const std::vector<NBNode::Crossing*>& crossings,
const EdgeVector& fromEdges,
const EdgeVector& toEdges) {
465 const std::string orig = state;
469 logic->
addStep(greenTime, state, minDur, maxDur);
471 const SUMOTime pedTime = greenTime - pedClearingTime;
472 if (pedTime >= minPedTime) {
474 const int pedStates = (int)crossings.size();
475 logic->
addStep(pedTime, state, minDur, maxDur);
476 state = state.substr(0, state.size() - pedStates) + std::string(pedStates,
'r');
477 logic->
addStep(pedClearingTime, state);
481 logic->
addStep(greenTime, state, minDur, maxDur);
490 std::string result = state;
491 const int pos = (int)(state.size() - crossings.size());
492 for (
int ic = 0; ic < (int)crossings.size(); ++ic) {
493 const int i1 = pos + ic;
498 if (fromEdges[i2] != 0 && toEdges[i2] != 0 && fromEdges[i2]->getToNode() == cross.
node) {
499 for (EdgeVector::const_iterator it = cross.
edges.begin(); it != cross.
edges.end(); ++it) {
502 if (state[i2] !=
'r' && (edge == fromEdges[i2] ||
518 for (
int i1 = 0; i1 < pos; ++i1) {
519 if (result[i1] ==
'G') {
520 for (
int ic = 0; ic < (int)crossings.size(); ++ic) {
522 if (fromEdges[i1] != 0 && toEdges[i1] != 0 && fromEdges[i1]->getToNode() == crossing.
node) {
523 const int i2 = pos + ic;
576 (*i)->removeTrafficLight(&dummy);
587 for (EdgeVector::iterator it = result.begin(); it != result.end();) {
588 if ((*it)->getConnections().size() == 0 || (*it)->isInnerEdge()) {
589 it = result.erase(it);
603 for (
int i1 = 0; i1 < (int)fromEdges.size(); ++i1) {
604 if (state[i1] ==
'G') {
607 bool followsChosen =
false;
608 for (
int i2 = 0; i2 < (int)fromEdges.size() && !followsChosen; ++i2) {
609 if (state[i2] ==
'G' && fromEdges[i1] == toEdges[i2]) {
610 followsChosen =
true;
625 const std::vector<bool>& isTurnaround,
626 const std::vector<int>& fromLanes,
627 const std::vector<bool>& hadGreenMajor,
628 bool& haveForbiddenLeftMover,
629 std::vector<bool>& rightTurnConflicts) {
631 for (
int i1 = 0; i1 < (int)fromEdges.size(); ++i1) {
632 if (state[i1] ==
'G') {
633 for (
int i2 = 0; i2 < (int)fromEdges.size(); ++i2) {
634 if ((state[i2] ==
'G' || state[i2] ==
'g')) {
636 fromEdges[i1], toEdges[i1], fromLanes[i1], fromEdges[i2], toEdges[i2], fromLanes[i2])) {
637 rightTurnConflicts[i1] =
true;
639 if (
forbids(fromEdges[i2], toEdges[i2], fromEdges[i1], toEdges[i1],
true, controlledWithin) || rightTurnConflicts[i1]) {
642 if (!isTurnaround[i1] && !hadGreenMajor[i1]) {
643 haveForbiddenLeftMover =
true;
649 if (state[i1] ==
'r') {
651 fromEdges[i1]->getToNode()->getDirection(fromEdges[i1], toEdges[i1]) ==
LINKDIR_RIGHT) {
654 for (
int i2 = 0; i2 < (int)fromEdges.size(); ++i2) {
655 if (state[i2] ==
'G' && !isTurnaround[i2] &&
656 (
forbids(fromEdges[i2], toEdges[i2], fromEdges[i1], toEdges[i1],
true) ||
forbids(fromEdges[i1], toEdges[i1], fromEdges[i2], toEdges[i2],
true))) {
669 const std::vector<NBNode::Crossing*>& crossings,
const EdgeVector& fromEdges,
const EdgeVector& toEdges) {
670 const int vehLinks = noLinksAll - (int)crossings.size();
671 std::vector<bool> foundGreen(crossings.size(),
false);
672 const std::vector<NBTrafficLightLogic::PhaseDefinition>& phases = logic->
getPhases();
673 for (
int i = 0; i < (int)phases.size(); ++i) {
674 const std::string state = phases[i].state;
675 for (
int j = 0; j < (int)crossings.size(); ++j) {
678 foundGreen[j] =
true;
682 for (
int j = 0; j < (int)foundGreen.size(); ++j) {
683 if (!foundGreen[j]) {
686 if (phases.size() > 0) {
687 bool needYellowPhase =
false;
688 std::string state = phases.back().state;
689 for (
int i1 = 0; i1 < vehLinks; ++i1) {
690 if (state[i1] ==
'G' || state[i1] ==
'g') {
692 needYellowPhase =
true;
696 if (needYellowPhase && brakingTime > 0) {
697 logic->
addStep(brakingTime, state);
709 if (allRedTime > 0) {
711 std::string allRedState = state;
712 for (
int i1 = 0; i1 < (int)state.size(); ++i1) {
713 if (allRedState[i1] ==
'Y' || allRedState[i1] ==
'y') {
714 allRedState[i1] =
'r';
717 logic->
addStep(allRedTime, allRedState);
static double relAngle(double angle1, double angle2)
computes the relative angle between the two angles
static double getMinAngleDiff(double angle1, double angle2)
Returns the minimum distance (clockwise/counter-clockwise) between both angles.
The link is a partial left direction.
The link has green light, may pass.
virtual void setParticipantsInformation()
Builds the list of participating nodes/edges/links.
bool setControllingTLInformation(const NBConnection &c, const std::string &tlID)
Returns if the link could be set as to be controlled.
TrafficLightType myType
The algorithm type for the traffic light.
void collectAllLinks()
helper method for use in NBOwnTLDef and NBLoadedSUMOTLDef
static const std::string DummyID
id for temporary definitions
static std::string patchStateForCrossings(const std::string &state, const std::vector< NBNode::Crossing * > &crossings, const EdgeVector &fromEdges, const EdgeVector &toEdges)
compute phase state in regard to pedestrian crossings
std::vector< Connection > getConnectionsFromLane(int lane) const
Returns connections from a given lane.
RightOnRedConflicts myRightOnRedConflicts
A SUMO-compliant built logic for a traffic light.
TrafficLightType getType() const
get the algorithm type (static etc..)
The link has green light, has to brake.
bool getBool(const std::string &name) const
Returns the boolean-value of the named option (only for Option_Bool)
const std::string & getProgramID() const
Returns the ProgramID.
std::string correctConflicting(std::string state, const EdgeVector &fromEdges, const EdgeVector &toEdges, const std::vector< bool > &isTurnaround, const std::vector< int > &fromLanes, const std::vector< bool > &hadGreenMajor, bool &haveForbiddenLeftMover, std::vector< bool > &rightTurnConflicts)
change 'G' to 'g' for conflicting connections
The representation of a single edge during network building.
std::string allowFollowersOfChosen(std::string state, const EdgeVector &fromEdges, const EdgeVector &toEdges)
allow connections that follow on of the chosen edges
bool mayBeTLSControlled(int fromLane, NBEdge *toEdge, int toLane) const
return true if certain connection must be controlled by TLS
std::string time2string(SUMOTime t)
The base class for traffic light logic definitions.
static const SUMOTime UNSPECIFIED_DURATION
static std::string getIDSecure(const T *obj, const std::string &fallBack="NULL")
get an identifier for Named-like object which may be Null
bool isForbidden(SVCPermissions permissions)
Returns whether an edge with the given permission is a forbidden edge.
const EdgeVector & getIncomingEdges() const
Returns the list of incoming edges (must be build first)
static EdgeVector getConnectedOuterEdges(const EdgeVector &incoming)
get edges that have connections
SUMOTime myOffset
The offset in the program.
NBEdge * getFrom() const
returns the from-edge (start of the connection)
The link is a (hard) left direction.
#define WRITE_WARNING(msg)
std::pair< NBEdge *, NBEdge * > getBestPair(EdgeVector &incoming)
Returns the combination of two edges from the given which has most unblocked streams.
static OptionsCont & getOptions()
Retrieves the options.
static bool rightTurnConflict(const NBEdge *from, const NBEdge *to, int fromLane, const NBEdge *prohibitorFrom, const NBEdge *prohibitorTo, int prohibitorFromLane, bool lefthand=false)
return whether the given laneToLane connection is a right turn which must yield to a bicycle crossing...
LinkDirection
The different directions a link between two lanes may take (or a stream between two edges)...
The link is a straight direction.
const std::string & getID() const
Returns the id.
static std::string addPedestrianPhases(NBTrafficLightLogic *logic, SUMOTime greenTime, SUMOTime minDur, SUMOTime maxDur, std::string state, const std::vector< NBNode::Crossing * > &crossings, const EdgeVector &fromEdges, const EdgeVector &toEdges)
add 1 or 2 phases depending on the presence of pedestrian crossings
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
const std::vector< PhaseDefinition > & getPhases() const
Returns the phases.
static void addPedestrianScramble(NBTrafficLightLogic *logic, int noLinksAll, SUMOTime greenTime, SUMOTime yellowTime, const std::vector< NBNode::Crossing * > &crossings, const EdgeVector &fromEdges, const EdgeVector &toEdges)
add an additional pedestrian phase if there are crossings that did not get green yet ...
bool mustBrakeForCrossing(const NBEdge *const from, const NBEdge *const to, const Crossing &crossing) const
Returns the information whether the described flow must brake for the given crossing.
void replaceRemoved(NBEdge *removed, int removedLane, NBEdge *by, int byLane)
Replaces a removed edge/lane.
double computeUnblockedWeightedStreamNumber(const NBEdge *const e1, const NBEdge *const e2)
Returns how many streams outgoing from the edges can pass the junction without being blocked...
std::pair< NBEdge *, NBEdge * > getBestCombination(const EdgeVector &edges)
Returns the combination of two edges from the given which has most unblocked streams.
bool myHaveSinglePhase
Whether left-mover should not have an additional phase.
int getNumLanes() const
Returns the number of lanes.
LinkState
The right-of-way state of a link between two lanes used when constructing a NBTrafficLightLogic, in MSLink and GNEInternalLane.
bool isTurningDirectionAt(const NBEdge *const edge) const
Returns whether the given edge is the opposite direction to this edge.
The link is a (hard) right direction.
static const std::string DefaultProgramID
LinkDirection getDirection(const NBEdge *const incoming, const NBEdge *const outgoing, bool leftHand=false) const
Returns the representation of the described stream's direction.
The link is a partial right direction.
SUMOTime getDuration() const
Returns the duration of the complete cycle.
void buildAllRedState(SUMOTime allRedTime, NBTrafficLightLogic *logic, const std::string &state)
int getJunctionPriority(const NBNode *const node) const
Returns the junction priority (normalised for the node currently build)
bool forbids(const NBEdge *const possProhibitorFrom, const NBEdge *const possProhibitorTo, const NBEdge *const possProhibitedFrom, const NBEdge *const possProhibitedTo, bool regardNonSignalisedLowerPriority, bool sameNodeOnly=false) const
Returns the information whether "prohibited" flow must let "prohibitor" flow pass.
void closeBuilding(bool checkVarDurations=true)
closes the building process
bool myRightOnRedConflictsReady
NBOwnTLDef(const std::string &id, const std::vector< NBNode * > &junctions, SUMOTime offset, TrafficLightType type)
Constructor.
NBNode * getToNode() const
Returns the destination node of the edge.
NBTrafficLightLogic * myCompute(int brakingTimeSeconds)
Computes the traffic light logic finally in dependence to the type.
NBTrafficLightLogic * computeLogicAndConts(int brakingTimeSeconds, bool onlyConts=false)
helper function for myCompute
void collectLinks()
Collects the links participating in this traffic light If a link could not be found.
bool myNeedsContRelationReady
std::vector< NBEdge * > EdgeVector
container for (sorted) edges
const NBNode * node
The parent node of this crossing.
NeedsContRelation myNeedsContRelation
void setPhaseDuration(int phaseIndex, SUMOTime duration)
Modifies the duration for an existing phase (used by NETEDIT)
double getDirectionalWeight(LinkDirection dir)
Returns the weight of a stream given its direction.
EdgeVector edges
The edges being crossed.
Represents a single node (junction) during network building.
A definition of a pedestrian crossing.
void initNeedsContRelation() const
data structure for caching needsCont information
std::vector< NBNode * > myControlledNodes
The container with participating nodes.
static bool hasCrossing(const NBEdge *from, const NBEdge *to, const std::vector< NBNode::Crossing * > &crossings)
compute whether the given connection is crossed by pedestrians
A traffic light logics which must be computed (only nodes/edges are given)
void addStep(SUMOTime duration, const std::string &state, int index=-1)
Adds a phase to the logic.
int getInt(const std::string &name) const
Returns the int-value of the named option (only for Option_Integer)
Sorts edges by their priority within the node they end at.
void setTLControllingInformation() const
Informs edges about being controlled by a tls.
NBConnectionVector myControlledLinks
The list of controlled links.
NBEdge * getTurnDestination(bool possibleDestination=false) const
int getToPrio(const NBEdge *const e)
Returns this edge's priority at the node it ends at.
void remapRemoved(NBEdge *removed, const EdgeVector &incoming, const EdgeVector &outgoing)
Replaces occurences of the removed edge in incoming/outgoing edges of all definitions.
NBNode * getFromNode() const
Returns the origin node of the edge.