50 myReconstructAddedConnections(false),
51 myReconstructRemovedConnections(false),
52 myPhasesLoaded(false) {
61 myReconstructAddedConnections(false),
62 myReconstructRemovedConnections(false),
63 myPhasesLoaded(false) {
69 if (sumoDef !=
nullptr) {
98 "' to edge '" + to->
getID() +
"' for traffic light '" +
getID() +
103 "' to edge '" + to->
getID() +
"' for traffic light '" +
getID() +
106 NBConnection conn(from, fromLane, to, toLane, linkIndex, linkIndex2);
141 n->removeTrafficLight(&dummy);
159 NBEdge* edge = c.getFrom();
160 if (edge !=
nullptr && edge->
getNumLanes() > c.getFromLane()) {
176 (*it).replaceFrom(removed, removedLane, by, byLane);
178 (*it).replaceTo(removed, removedLane, by, byLane);
187 const SUMOTime red,
const std::vector<int>& next,
const std::string& name) {
188 myTLLogic->
addStep(duration, state, minDur, maxDur, earliestEnd, latestEnd, vehExt, yellow, red, name, next);
207 if ((it->getFrom() == conn.
getFrom() &&
208 it->getTo() == conn.
getTo() &&
213 (it->getFrom() ==
nullptr || it->getTo() ==
nullptr))) {
250 const EdgeVector& incoming = (*i)->getIncomingEdges();
251 copy(incoming.begin(), incoming.end(), back_inserter(
myIncomingEdges));
252 const EdgeVector& outgoing = (*i)->getOutgoingEdges();
253 copy(outgoing.begin(), outgoing.end(), back_inserter(myOutgoing));
261 EdgeVector::iterator k = std::find(myOutgoing.begin(), myOutgoing.end(), edge);
262 if (k != myOutgoing.end()) {
264 bool controlled =
false;
266 if ((*it).getFrom() == edge) {
304 (*it).shiftLaneIndex(edge, offset, threshold);
319 const int numNormalLinks = noLinksAll;
320 int oldCrossings = 0;
322 bool customIndex =
false;
323 std::vector<NBNode::Crossing*> crossings;
325 const std::vector<NBNode::Crossing*>& c = (*i)->getCrossings();
327 customIndex |= (*i)->setCrossingTLIndices(
getID(), noLinksAll);
328 copy(c.begin(), c.end(), std::back_inserter(crossings));
329 noLinksAll += (int)c.size();
330 oldCrossings += (*i)->numCrossingsFromSumoNet();
332 if ((
int)crossings.size() != oldCrossings) {
335 if (phases.size() > 0 && (
336 (int)(phases.front().state.size()) < noLinksAll ||
337 ((
int)(phases.front().state.size()) > noLinksAll && !customIndex))) {
341 std::vector<int> fromLanes(size, 0);
343 const std::string crossingDefaultState(crossings.size(),
'r');
349 for (
const auto& phase : phases) {
350 const std::string state = phase.state.substr(0, numNormalLinks) + crossingDefaultState;
352 state, crossings, fromEdges, toEdges);
358 }
else if (phases.size() == 0) {
367 assert(fromEdges.size() > 0);
368 assert(fromEdges.size() == toEdges.size());
369 const int size = (int)fromEdges.size();
376 "' with " +
toString(size) +
" links.");
392 const std::vector<NBTrafficLightLogic::PhaseDefinition> phases =
myTLLogic->
getPhases();
393 for (std::vector<NBTrafficLightLogic::PhaseDefinition>::const_iterator it = phases.begin(); it != phases.end(); it++) {
394 const std::string state = (*it).state;
406 && (state[i2] ==
'G' || state[i2] ==
'g')
412 if (forbidden || rightTurnConflict) {
415 if (isFoes && state[i1] ==
's') {
452#ifdef DEBUG_RECONSTRUCTION
460 std::cout <<
" " << *it <<
"\n";
477 (*i)->removeTrafficLight(&dummy);
481 if (newLogic !=
nullptr) {
517 bool exclusive =
true;
519 if (other != con && other.
getTLIndex() == removed) {
527 if (other.getTLIndex() > removed) {
528 other.setTLIndex(other.getTLIndex() - 1);
534 if (c->customTLIndex > removed) {
542 std::string newState = phase.state;
543 newState.erase(newState.begin() + removed);
544 newLogic->
addStep(phase.duration, newState);
554#ifdef DEBUG_RECONSTRUCTION
555 if (debugPrintModified) {
556 std::cout <<
" newLinks:\n";
558 std::cout <<
" " << *it <<
"\n";
569 maxIndex =
MAX2(maxIndex, c.getTLIndex());
570 maxIndex =
MAX2(maxIndex, c.getTLIndex2());
574 maxIndex =
MAX2(maxIndex, c->tlLinkIndex);
575 maxIndex =
MAX2(maxIndex, c->tlLinkIndex2);
613 result += pd.state[index];
621 if (c.getTLIndex() == index || c.getTLIndex2() == index) {
627 if (c->tlLinkIndex == index || c->tlLinkIndex2 == index) {
635std::set<const NBEdge*>
637 std::set<const NBEdge*> result;
639 if (c.getTLIndex() == index || c.getTLIndex2() == index) {
640 result.insert(c.getFrom());
649 if (oldIndex == newIndex) {
653 if (c.getTLIndex() == oldIndex) {
654 c.setTLIndex(newIndex);
656 if (c.getTLIndex2() == oldIndex) {
657 c.setTLIndex2(newIndex);
662 if (c->tlLinkIndex == oldIndex) {
663 c->tlLinkIndex = newIndex;
665 if (c->tlLinkIndex2 == oldIndex) {
666 c->tlLinkIndex2 = newIndex;
675 std::vector<int> unusedIndices;
676 for (
int i = 0; i <= maxIndex; i++) {
681 if (edges.size() == 0) {
686 for (
int j = i + 1; j <= maxIndex; j++) {
694 unusedIndices.push_back(i);
697 for (
int i = (
int)unusedIndices.size() - 1; i >= 0; i--) {
706 for (
int i = (
int)unusedIndices.size() - 1; i >= 0; i--) {
707 if (c->customTLIndex > i) {
710 if (c->customTLIndex2 > i) {
722 std::vector<std::string> states;
731 states.push_back(
getStates(c->tlLinkIndex));
732 c->customTLIndex = index++;
734 states.push_back(
getStates(c->tlLinkIndex2));
735 c->customTLIndex2 = index++;
740 for (
int i = 0; i < (int)states.size(); i++) {
741 for (
int p = 0; p < (int)states[i].size(); p++) {
751 std::map<int, std::string> oldStates;
752 std::map<int, std::string> newStates;
756 const int newIndex = c.getTLIndex();
757 std::string states =
getStates(oldIndex);
758 oldStates[oldIndex] = states;
759 if (newStates.count(newIndex) != 0 && newStates[newIndex] != states) {
761 +
"'. Possibly unsafe program.");
763 newStates[newIndex] = states;
769 for (
int i = 0; i < (int)newStates.size(); i++) {
770 for (
int p = 0; p < (int)newStates[i].size(); p++) {
781 std::vector<int> unusedIndices;
782 for (
int i = 0; i <= maxIndex; i++) {
784 if (unusedIndices.size() > 0) {
788 unusedIndices.push_back(i);
791 for (
int i = (
int)unusedIndices.size() - 1; i >= 0; i--) {
794 if (unusedIndices.size() > 0) {
816 std::map<int, int> indexUsage;
818 indexUsage[c.getTLIndex()]++;
822 indexUsage[c->tlLinkIndex]++;
823 indexUsage[c->tlLinkIndex2]++;
826 for (
auto it : indexUsage) {
827 if (it.first >= 0 && it.second > 1) {
836 bool hasMinMaxDur =
false;
846 std::set<int> yellowIndices;
848 for (
int i = 0; i < (int)phase.state.size(); i++) {
849 if (phase.state[i] ==
'y' || phase.state[i] ==
'Y') {
850 yellowIndices.insert(i);
855 bool needMinMaxDur =
false;
857 std::set<int> greenIndices;
858 if (phase.state.find_first_of(
"yY") != std::string::npos) {
861 for (
int i = 0; i < (int)phase.state.size(); i++) {
862 if (yellowIndices.count(i) != 0 && phase.state[i] ==
'G') {
863 needMinMaxDur =
true;
864 greenIndices.insert(i);
870 if (greenIndices.count(c.getTLIndex()) != 0) {
871 maxSpeed =
MAX2(maxSpeed, c.getFrom()->getLaneSpeed(c.getFromLane()));
875 const double minDurBySpeed = maxSpeed * 3.6 / 6 - 3.3;
#define WRITE_WARNINGF(...)
#define WRITE_WARNING(msg)
std::vector< NBConnection > NBConnectionVector
Definition of a connection vector.
std::vector< NBEdge * > EdgeVector
container for (sorted) edges
LinkState
The right-of-way state of a link between two lanes used when constructing a NBTrafficLightLogic,...
#define UNUSED_PARAMETER(x)
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
NBEdge * getFrom() const
returns the from-edge (start of the connection)
int getFromLane() const
returns the from-lane
int getTLIndex() const
returns the index within the controlling tls or InvalidTLIndex if this link is unontrolled
static const int InvalidTlIndex
void setTLIndex(int tlIndex)
int getToLane() const
returns the to-lane
NBEdge * getTo() const
returns the to-edge (end of the connection)
The representation of a single edge during network building.
NBNode * getToNode() const
Returns the destination node of the edge.
void setInsideTLS(bool inside)
Marks this edge being within an intersection.
const std::string & getID() const
bool setControllingTLInformation(const NBConnection &c, const std::string &tlID)
Returns if the link could be set as to be controlled.
bool isTurningDirectionAt(const NBEdge *const edge) const
Returns whether the given edge is the opposite direction to this edge.
int getNumLanes() const
Returns the number of lanes.
bool mayBeTLSControlled(int fromLane, NBEdge *toEdge, int toLane) const
return true if certain connection must be controlled by TLS
NBNode * getFromNode() const
Returns the origin node of the edge.
bool hasConnectionTo(const NBEdge *destEdge, int destLane, int fromLane=-1) const
Retrieves info about a connection to a certain lane of a certain edge.
class for identifying connections
A loaded (complete) traffic light logic.
bool isUsed(int index) const
return whether the given link index is used by any connectons
NBTrafficLightLogic * myCompute(int brakingTimeSeconds)
Computes the traffic light logic finally in dependence to the type.
void setID(const std::string &newID)
resets the id
int getMaxIndex()
return the highest known tls link index used by any controlled connection or crossing
bool myReconstructRemovedConnections
bool hasValidIndices() const
return whether all tls link indices are valid
bool usingSignalGroups() const
whether this definition uses signal group (multiple connections with the same link index)
std::string getStates(int index)
get all states for the given link index
void ungroupSignals()
let all connections use a distinct link index
NBLoadedSUMOTLDef(const std::string &id, const std::string &programID, SUMOTime offset, TrafficLightType type)
Constructor.
void addPhase(const SUMOTime duration, const std::string &state, const SUMOTime minDur, const SUMOTime maxDur, const SUMOTime earliestEnd, const SUMOTime latestEnd, const SUMOTime vehExt, const SUMOTime yellow, const SUMOTime red, const std::vector< int > &next, const std::string &name)
Adds a phase to the logic the new phase is inserted at the end of the list of already added phases.
bool myReconstructAddedConnections
whether the logic must be reconstructed
bool rightOnRedConflict(int index, int foeIndex) const
whether the given index must yield to the foeIndex while turing right on a red light
void copyIndices(NBTrafficLightDefinition *def)
copy the assignment of link indices to connections from the given definition and rebuilt the states t...
void groupSignals()
let connections with the same state use the same link index
void remapRemoved(NBEdge *removed, const EdgeVector &incoming, const EdgeVector &outgoing)
Replaces occurrences of the removed edge in incoming/outgoing edges of all definitions.
void collectEdges()
Build the list of participating edges.
void setProgramID(const std::string &programID)
Sets the programID.
void shiftTLConnectionLaneIndex(NBEdge *edge, int offset, int threshold=-1)
patches signal plans by modifying lane indices with the given offset, only indices with a value above...
NBTrafficLightLogic * myTLLogic
phases are added directly to myTLLogic which is then returned in myCompute()
std::set< NBEdge * > myShifted
set of edges with shifted lane indices (to avoid shifting twice)
void patchIfCrossingsAdded()
repair the plan if controlled nodes received pedestrian crossings
void removeConnection(const NBConnection &conn, bool reconstruct=true)
removes the given connection from the traffic light if recontruct=true, reconstructs the logic and in...
int getMaxValidIndex()
Returns the maximum index controlled by this traffic light.
void replaceIndex(int oldIndex, int newIndex)
replace the given link index in all connections
void collectLinks()
Collects the links participating in this traffic light (only if not previously loaded)
void registerModifications(bool addedConnections, bool removedConnections)
register changes that necessitate recomputation
void joinLogic(NBTrafficLightDefinition *def)
join nodes and states from the given logic (append red state)
void setTLControllingInformation() const
Informs edges about being controlled by a tls.
void setType(TrafficLightType type)
Sets the algorithm type of this tls.
void collectEdgeVectors(EdgeVector &fromEdges, EdgeVector &toEdges, std::vector< int > &fromLanes) const
Collects the edges for each tlIndex.
void reconstructLogic()
adapt to removal or addition of connections
void addConnection(NBEdge *from, NBEdge *to, int fromLane, int toLane, int linkIndex, int linkIndex2, bool reconstruct=true)
Adds a connection and immediately informs the edges.
void guessMinMaxDuration()
heuristically add minDur and maxDur when switching from tlType fixed to actuated
std::set< const NBEdge * > getEdgesUsingIndex(int index) const
brief retrieve all edges with connections that use the given traffic light index
void replaceRemoved(NBEdge *removed, int removedLane, NBEdge *by, int byLane, bool incoming)
Replaces a removed edge/lane.
void setOffset(SUMOTime offset)
Sets the offset of this tls.
void finalChecks() const
perform optional final checks (on writing)
~NBLoadedSUMOTLDef()
Destructor.
void initNeedsContRelation() const
initialize myNeedsContRelation and set myNeedsContRelationReady to true
static bool runningNetedit()
whether netbuilding takes place in the context of netedit
A definition of a pedestrian crossing.
Represents a single node (junction) during network building.
static bool rightTurnConflict(const NBEdge *from, const NBEdge *to, int fromLane, const NBEdge *prohibitorFrom, const NBEdge *prohibitorTo, int prohibitorFromLane)
return whether the given laneToLane connection is a right turn which must yield to a bicycle crossing...
A traffic light logics which must be computed (only nodes/edges are given)
static void addPedestrianScramble(NBTrafficLightLogic *logic, int totalNumLinks, 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
static std::string addPedestrianPhases(NBTrafficLightLogic *logic, const SUMOTime greenTime, const SUMOTime minDur, const SUMOTime maxDur, const SUMOTime earliestEnd, const SUMOTime latestEnd, 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
void setTLControllingInformation() const
Informs edges about being controlled by a tls.
The base class for traffic light logic definitions.
const std::vector< NBNode * > & getNodes() const
Returns the list of controlled nodes.
const std::string & getProgramID() const
Returns the ProgramID.
const EdgeVector & getIncomingEdges() const
Returns the list of incoming edges (must be build first)
std::vector< NBNode * > myControlledNodes
The container with participating nodes.
TrafficLightType getType() const
get the algorithm type (static etc..)
virtual void setProgramID(const std::string &programID)
Sets the programID.
EdgeVector myIncomingEdges
The list of incoming edges.
virtual void addNode(NBNode *node)
Adds a node to the traffic light logic.
NBTrafficLightLogic * compute(const OptionsCont &oc)
Computes the traffic light logic.
SUMOTime getOffset()
Returns the offset.
RightOnRedConflicts myRightOnRedConflicts
TrafficLightType myType
The algorithm type for the traffic light.
EdgeVector myEdgesWithin
The list of edges within the area controlled by the tls.
static const std::string DummyID
id for temporary definitions
const NBConnectionVector & getControlledLinks() const
returns the controlled links (depends on previous call to collectLinks)
int computeBrakingTime(double minDecel) const
Computes the time vehicles may need to brake.
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.
NBConnectionVector myControlledLinks
The list of controlled links.
bool myNeedsContRelationReady
virtual void setParticipantsInformation()
Builds the list of participating nodes/edges/links.
virtual int getMaxIndex()=0
Returns the maximum index controlled by this traffic light and assigned to a connection.
void collectAllLinks(NBConnectionVector &into)
helper method for use in NBOwnTLDef and NBLoadedSUMOTLDef
SUMOTime myOffset
The offset in the program.
static const SUMOTime UNSPECIFIED_DURATION
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.
bool myRightOnRedConflictsReady
NeedsContRelation myNeedsContRelation
virtual void collectEdges()
Build the list of participating edges.
std::set< std::string > myControlledInnerEdges
Set of inner edges that shall be controlled, though.
The definition of a single phase of the logic.
A SUMO-compliant built logic for a traffic light.
void deleteStateIndex(int index)
remove the index from all phase states
void setPhaseMinDuration(int phaseIndex, SUMOTime duration)
Modifies the min duration for an existing phase (used by netedit)
void closeBuilding(bool checkVarDurations=true)
closes the building process
void setPhaseState(int phaseIndex, int tlIndex, LinkState linkState)
Modifies the state for an existing phase (used by netedit)
void setPhaseMaxDuration(int phaseIndex, SUMOTime duration)
Modifies the max duration for an existing phase (used by netedit)
int getNumLinks()
Returns the number of participating links.
void setType(TrafficLightType type)
set the algorithm type (static etc..)
void setStateLength(int numLinks, LinkState fill=LINKSTATE_TL_RED)
TrafficLightType getType() const
get the algorithm type (static etc..)
void setOffset(SUMOTime offset)
Sets the offset of this tls.
const std::vector< PhaseDefinition > & getPhases() const
Returns the phases.
void addStep(const SUMOTime duration, const std::string &state, const std::vector< int > &next=std::vector< int >(), const std::string &name="", const int index=-1)
Adds a phase to the logic (static)
void setProgramID(const std::string &programID)
Sets the programID.
virtual void setID(const std::string &newID)
resets the id
const std::string & getID() const
Returns the id.
bool getBool(const std::string &name) const
Returns the boolean-value of the named option (only for Option_Bool)
static OptionsCont & getOptions()
Retrieves the options.
const Parameterised::Map & getParametersMap() const
Returns the inner key/value map.
void updateParameters(const Parameterised::Map &mapArg)
Adds or updates all given parameters from the map.
data structure for caching needsCont information