47 #ifdef CHECK_MEMORY_LEAKS
49 #endif // CHECK_MEMORY_LEAKS
56 const std::vector<NBNode*>& junctions,
SUMOTime offset,
59 myHaveSinglePhase(false)
66 myHaveSinglePhase(false)
73 myHaveSinglePhase(false)
105 for (
unsigned int e1l = 0; e1l < e1->
getNumLanes(); e1l++) {
107 for (
unsigned 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 (!
foes(e1, (*e1c).toEdge, e2, (*e2c).toEdge)) {
129 std::pair<NBEdge*, NBEdge*>
131 std::pair<NBEdge*, NBEdge*> bestPair(static_cast<NBEdge*>(0), static_cast<NBEdge*>(0));
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 SUMOReal 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 incoming.erase(find(incoming.begin(), incoming.end(), ret.first));
180 incoming.erase(find(incoming.begin(), incoming.end(), ret.second));
197 std::vector<bool> isTurnaround;
198 std::vector<int> fromLanes;
199 unsigned int noLanesAll = 0;
200 unsigned int noLinksAll = 0;
201 for (
unsigned int i1 = 0; i1 < incoming.size(); i1++) {
202 unsigned int noLanes = incoming[i1]->getNumLanes();
203 noLanesAll += noLanes;
204 for (
unsigned int i2 = 0; i2 < noLanes; i2++) {
205 NBEdge* fromEdge = incoming[i1];
207 noLinksAll += (
unsigned int) approached.size();
208 for (
unsigned int i3 = 0; i3 < approached.size(); i3++) {
209 if (!fromEdge->
mayBeTLSControlled(i2, approached[i3].toEdge, approached[i3].toLane)) {
213 assert(i3 < approached.size());
214 NBEdge* toEdge = approached[i3].toEdge;
215 fromEdges.push_back(fromEdge);
216 fromLanes.push_back((
int)i2);
217 toEdges.push_back(toEdge);
221 isTurnaround.push_back(
true);
227 std::vector<NBNode::Crossing> crossings;
229 const std::vector<NBNode::Crossing>& c = (*i)->getCrossings();
232 (*i)->setCrossingTLIndices(noLinksAll);
234 copy(c.begin(), c.end(), std::back_inserter(crossings));
235 noLinksAll += (
unsigned int)c.size();
242 while (toProc.size() > 0) {
243 std::pair<NBEdge*, NBEdge*> chosen;
244 if (incoming.size() == 2) {
247 SUMOReal angle = fabs(
NBHelpers::relAngle(incoming[0]->getAngleAtNode(incoming[0]->getToNode()), incoming[1]->getAngleAtNode(incoming[1]->getToNode())));
250 chosen = std::pair<NBEdge*, NBEdge*>(toProc[0],
static_cast<NBEdge*
>(0));
251 toProc.erase(toProc.begin());
258 unsigned int pos = 0;
259 std::string state((
size_t) noLinksAll,
'r');
261 for (
unsigned int i1 = 0; i1 < (
unsigned int) incoming.size(); ++i1) {
262 NBEdge* fromEdge = incoming[i1];
263 const bool inChosen = fromEdge == chosen.first || fromEdge == chosen.second;
264 const unsigned int numLanes = fromEdge->
getNumLanes();
265 for (
unsigned int i2 = 0; i2 < numLanes; i2++) {
267 for (
unsigned int i3 = 0; i3 < approached.size(); ++i3) {
268 if (!fromEdge->
mayBeTLSControlled(i2, approached[i3].toEdge, approached[i3].toLane)) {
281 for (
unsigned int i1 = 0; i1 < pos; ++i1) {
282 if (state[i1] ==
'G') {
286 for (
unsigned int i2 = 0; i2 < pos && !
isForbidden; ++i2) {
287 if (state[i2] ==
'G' && !isTurnaround[i2] &&
288 (
forbids(fromEdges[i2], toEdges[i2], fromEdges[i1], toEdges[i1],
true) ||
forbids(fromEdges[i1], toEdges[i1], fromEdges[i2], toEdges[i2],
true))) {
292 if (!isForbidden && !
hasCrossing(fromEdges[i1], toEdges[i1], crossings)) {
297 bool haveForbiddenLeftMover =
false;
298 std::vector<bool> rightTurnConflicts(pos,
false);
299 for (
unsigned int i1 = 0; i1 < pos; ++i1) {
300 if (state[i1] !=
'G') {
303 for (
unsigned int i2 = 0; i2 < pos; ++i2) {
304 if ((state[i2] ==
'G' || state[i2] ==
'g')) {
306 fromEdges[i1], toEdges[i1], fromLanes[i1], fromEdges[i2], toEdges[i2], fromLanes[i2])) {
307 rightTurnConflicts[i1] =
true;
309 if (
forbids(fromEdges[i2], toEdges[i2], fromEdges[i1], toEdges[i1],
true) || rightTurnConflicts[i1]) {
312 if (!isTurnaround[i1]) {
313 haveForbiddenLeftMover =
true;
319 const std::string vehicleState = state;
322 for (
unsigned int i1 = pos; i1 < pos + crossings.size(); ++i1) {
326 if (brakingTime > 0) {
328 for (
unsigned int i1 = 0; i1 < pos; ++i1) {
329 if (state[i1] !=
'G' && state[i1] !=
'g') {
332 if ((vehicleState[i1] >=
'a' && vehicleState[i1] <=
'z') && haveForbiddenLeftMover && !rightTurnConflicts[i1]) {
338 logic->
addStep(brakingTime, state);
343 for (
unsigned int i1 = 0; i1 < pos; ++i1) {
344 if (state[i1] ==
'Y' || state[i1] ==
'y') {
348 if (state[i1] ==
'g') {
353 logic->
addStep(leftTurnTime, state);
356 if (brakingTime > 0) {
357 for (
unsigned int i1 = 0; i1 < pos; ++i1) {
358 if (state[i1] !=
'G' && state[i1] !=
'g') {
364 logic->
addStep(brakingTime, state);
371 if (totalDuration > 0) {
372 if (totalDuration > 3 * (greenTime + 2 * brakingTime + leftTurnTime)) {
388 for (std::vector<NBNode::Crossing>::const_iterator it = crossings.begin(); it != crossings.end(); it++) {
392 for (EdgeVector::const_iterator it_e = cross.
edges.begin(); it_e != cross.
edges.end(); ++it_e) {
393 const NBEdge* edge = *it_e;
394 if (edge == from || edge == to) {
406 std::string state,
const std::vector<NBNode::Crossing>& crossings,
const EdgeVector& fromEdges,
const EdgeVector& toEdges) {
409 const std::string orig = state;
413 logic->
addStep(greenTime, state);
415 const SUMOTime pedTime = greenTime - pedClearingTime;
416 if (pedTime >= minPedTime) {
418 const size_t pedStates = crossings.size();
419 logic->
addStep(pedTime, state);
420 state = state.substr(0, state.size() - pedStates) + std::string(pedStates,
'r');
421 logic->
addStep(pedClearingTime, state);
425 logic->
addStep(greenTime, state);
434 std::string result = state;
435 const unsigned int pos = (
unsigned int)(state.size() - crossings.size());
436 for (
int ic = 0; ic < (int)crossings.size(); ++ic) {
437 const int i1 = pos + ic;
440 for (
unsigned int i2 = 0; i2 < pos && !
isForbidden; ++i2) {
442 if (fromEdges[i2] != 0 && toEdges[i2] != 0 && fromEdges[i2]->getToNode() == cross.
node) {
443 for (EdgeVector::const_iterator it = cross.
edges.begin(); it != cross.
edges.end(); ++it) {
446 if (state[i2] !=
'r' && (edge == fromEdges[i2] ||
462 for (
unsigned int i1 = 0; i1 < pos; ++i1) {
463 if (result[i1] ==
'G') {
464 for (
int ic = 0; ic < (int)crossings.size(); ++ic) {
466 if (fromEdges[i1] != 0 && toEdges[i1] != 0 && fromEdges[i1]->getToNode() == crossing.
node) {
467 const int i2 = pos + ic;
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
The link is a partial left direction.
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
void closeBuilding()
closes the building process
A SUMO-compliant built logic for a traffic light.
const std::string & getProgramID() const
Returns the ProgramID.
The representation of a single edge during network building.
bool mayBeTLSControlled(int fromLane, NBEdge *toEdge, int toLane) const
void collectNodes()
Collects the nodes participating in this traffic light.
std::string time2string(SUMOTime t)
The base class for traffic light logic definitions.
static bool hasCrossing(const NBEdge *from, const NBEdge *to, const std::vector< NBNode::Crossing > &crossings)
compute whether the given connection is crossed by pedestrians
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)
NBTrafficLightLogic * computeLogicAndConts(unsigned int brakingTimeSeconds, bool onlyConts=false)
helper function for myCompute
void removeTrafficLight(NBTrafficLightDefinition *tlDef)
Removes the given traffic light from this node.
std::vector< Connection > getConnectionsFromLane(unsigned int lane) const
Returns connections from a given lane.
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.
virtual void collectEdges()
Build the list of participating edges.
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.
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.
unsigned int getNumLanes() const
Returns the number of lanes.
bool isTurningDirectionAt(const NBEdge *const edge) const
Returns whether the given edge is the opposite direction to this edge.
Storage for edges, including some functionality operating on multiple edges.
The link is a (hard) right direction.
static std::string addPedestrianPhases(NBTrafficLightLogic *logic, SUMOTime greenTime, 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
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.
int getJunctionPriority(const NBNode *const node) const
Returns the junction priority (normalised for the node currently build)
SUMOReal computeUnblockedWeightedStreamNumber(const NBEdge *const e1, const NBEdge *const e2)
Returns how many streams outgoing from the edges can pass the junction without being blocked...
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.
void setTLControllingInformation(const NBEdgeCont &ec) const
Informs edges about being controlled by a tls.
void collectLinks()
Collects the links participating in this traffic light If a link could not be found.
SUMOReal getDirectionalWeight(LinkDirection dir)
Returns the weight of a stream given its direction.
bool myNeedsContRelationReady
std::vector< NBEdge * > EdgeVector
NBTrafficLightLogic * myCompute(const NBEdgeCont &ec, unsigned int brakingTimeSeconds)
Computes the traffic light logic finally in dependence to the type.
static SUMOReal getMinAngleDiff(SUMOReal angle1, SUMOReal angle2)
Returns the minimum distance (clockwise/counter-clockwise) between both angles.
const NBNode * node
The parent node of this crossing.
NeedsContRelation myNeedsContRelation
EdgeVector edges
The edges being crossed.
Represents a single node (junction) during network building.
bool forbids(const NBEdge *const possProhibitorFrom, const NBEdge *const possProhibitorTo, const NBEdge *const possProhibitedFrom, const NBEdge *const possProhibitedTo, bool regardNonSignalisedLowerPriority) const
Returns the information whether "prohibited" flow must let "prohibitor" flow pass.
A definition of a pedestrian crossing.
void initNeedsContRelation() const
static SUMOReal relAngle(SUMOReal angle1, SUMOReal angle2)
data structure for caching needsCont information
std::vector< NBNode * > myControlledNodes
The container with participating nodes.
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.
Sorts edges by their priority within the node they end at.
void setParticipantsInformation()
Builds the list of participating nodes/edges/links.
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.
bool foes(const NBEdge *const from1, const NBEdge *const to1, const NBEdge *const from2, const NBEdge *const to2) const
Returns the information whether the given flows cross.