46#define SHORT_EDGE ((SUMOVTypeParameter::getDefault().length + SUMOVTypeParameter::getDefault().minGap) * 2)
56 myTLControl(tlcontrol), myTLLogic(tlLogic),
57 myAssumedNextSwitch(nextSwitch), myAmValid(true) {
73 int step1 = myTLLogic->getCurrentPhaseIndex();
74 SUMOTime next = myTLLogic->trySwitch();
77 next = myTLLogic->trySwitch();
79 int step2 = myTLLogic->getCurrentPhaseIndex();
81 if (myTLLogic->isActive()) {
85 myTLLogic->setTrafficLightSignals(t);
90 myAssumedNextSwitch += next;
97 if (tlLogic == myTLLogic) {
99 myAssumedNextSwitch = -1;
106 if (myTLLogic->getDefaultCycleTime() ==
DELTA_T) {
141 if (phases.size() > 1) {
142 bool haveWarnedAboutUnusedStates =
false;
143 for (
int i = 0; i < (int)phases.size(); ++i) {
145 std::vector<int> nextPhases;
146 nextPhases.push_back((i + 1) % phases.size());
147 bool iNextDefault =
true;
148 if (phases[i]->nextPhases.size() > 0) {
149 nextPhases = phases[i]->nextPhases;
150 iNextDefault =
false;
152 for (
int iNext : nextPhases) {
153 if (iNext < 0 || iNext >= (
int)phases.size()) {
157 if (iNext == i && (
int)nextPhases.size() == 1 && (
int)phases.size() > 1) {
160 const std::string optionalFrom = iNextDefault ?
"" :
" from phase " +
toString(i);
161 const std::string& state1 = phases[i]->getState();
162 const std::string& state2 = phases[iNext]->getState();
163 if (state1.size() != state2.size()) {
168 WRITE_WARNINGF(
TL(
"Unused states in tlLogic '%', program '%' in phase % after tl-index %"),
170 haveWarnedAboutUnusedStates =
true;
174 if (std::string::npos != illegal) {
179 bool haveWarned =
false;
180 for (
int j = 0; j < (int)
MIN3(state1.size(), state2.size(),
myLanes.size()) && !haveWarned; ++j) {
184 for (LaneVector::const_iterator it =
myLanes[j].begin(); it !=
myLanes[j].end(); ++it) {
187 WRITE_WARNINGF(
TL(
"Missing yellow phase in tlLogic '%', program '%' for tl-index % when switching% to phase %."),
201 std::vector<bool> foundGreen(phases.front()->getState().size(),
false);
202 for (
int i = 0; i < (int)phases.size(); ++i) {
203 const std::string& state = phases[i]->getState();
204 for (
int j = 0; j < (int)state.size(); ++j) {
207 foundGreen[j] =
true;
211 std::vector<bool> usedIndices(phases.front()->getState().size(),
false);
213 for (
const MSLink* link : lv) {
214 if (link->getTLIndex() >= 0) {
215 usedIndices[link->getTLIndex()] =
true;
219 for (
int j = 0; j < (int)foundGreen.size(); ++j) {
220 if (!foundGreen[j] && usedIndices[j]) {
226 const int numLinks = (int)
myLinks.size();
227 for (
int i = 0; i < (int)phases.size(); ++i) {
228 std::map<const MSLane*, int, ComparatorNumericalIdLess> greenLanes;
229 const std::string& state = phases[i]->getState();
230 for (
int j = 0; j < numLinks; ++j) {
233 if (link->isInternalJunctionLink()) {
237 greenLanes[link->getLane()] += 1;
241 for (
auto item : greenLanes) {
242 if (item.second > 1) {
243 WRITE_WARNINGF(
TL(
"Unsafe green phase % in tlLogic '%', program '%'. Lane '%' is targeted by % 'G'-links. (use 'g' instead)"),
263 if (mustCheck && phases.size() > 0) {
265 std::set<const MSJunction*> controlledJunctions;
266 for (
int j = 0; j < numLinks; ++j) {
267 for (
int k = 0; k < (int)
myLinks[j].size(); ++k) {
273 const std::string minor =
"gos";
274 for (
const MSJunction* junction : controlledJunctions) {
276 if (logic !=
nullptr) {
279 bool foundProblem =
false;
280 std::vector<int> tlIndex;
281 for (
int u = 0; u < logicSize && !foundProblem; u++) {
283 for (
int v = 0; v < logicSize && !foundProblem; v++) {
284 if (response.test(v)) {
287 if (tlIndex.size() == 0) {
289 tlIndex.resize(logicSize, -1);
290 for (
int j = 0; j < numLinks; ++j) {
291 for (
int k = 0; k < (int)
myLinks[j].size(); ++k) {
307 const int tlu = tlIndex[u];
308 const int tlv = tlIndex[v];
309 if (tlu >= 0 && tlv >= 0) {
312 if (minor.find(p->getState()[tlu]) != std::string::npos
313 && minor.find(p->getState()[tlv]) != std::string::npos) {
314 WRITE_WARNING(
TLF(
"Program '%' at tlLogic '%' is incompatible with logic at junction '%' (mutual conflict between link indices %,% tl indices %,% phase %).\n"
315 " To avoid deadlock/collisions, either: rebuild the signal plan with a newer version of netconvert/netedit\n"
316 " or rebuild the network with option '--tls.ignore-internal-junction-jam' or include the program when building.",
347 while ((
int)
myLinks.size() <= pos) {
353 while ((
int)
myLanes.size() <= pos) {
369std::map<MSLink*, LinkState>
371 std::map<MSLink*, LinkState> ret;
372 for (LinkVectorVector::const_iterator i1 =
myLinks.begin(); i1 !=
myLinks.end(); ++i1) {
374 for (LinkVector::const_iterator i2 = l.begin(); i2 != l.end(); ++i2) {
375 ret[*i2] = (*i2)->getState();
387 for (
int i = 0; i < (int)
myLinks.size(); i++) {
390 for (LinkVector::const_iterator j = currGroup.begin(); j != currGroup.end(); j++) {
391 (*j)->setTLState(ls, t);
400 for (LinkVectorVector::const_iterator i1 =
myLinks.begin(); i1 !=
myLinks.end(); ++i1) {
402 for (LinkVector::const_iterator i2 = l.begin(); i2 != l.end(); ++i2) {
403 assert(vals.find(*i2) != vals.end());
414 for (LinkVectorVector::const_iterator i1 =
myLinks.begin(); i1 !=
myLinks.end(); ++i1, ++index) {
416 for (LinkVector::const_iterator i2 = l.begin(); i2 != l.end(); ++i2) {
459 const int numLinks = (int)
myLinks.size();
461 assert(numLinks <= (
int)phases.front()->getState().size());
463 std::vector<double> firstRedDuration(numLinks, 0);
464 std::vector<double> redDuration(numLinks, 0);
465 std::vector<double> totalRedDuration(numLinks, 0);
466 std::vector<double> penalty(numLinks, 0);
467 for (
int i = 0; i < (int)phases.size(); ++i) {
468 const std::string& state = phases[i]->getState();
469 duration += phases[i]->duration;
471 for (
int j = 0; j < numLinks; ++j) {
472 double& red = redDuration[j];
476 totalRedDuration[j] +=
STEPS2TIME(phases[i]->duration);
477 }
else if (red > 0) {
478 if (firstRedDuration[j] == 0) {
480 firstRedDuration[j] = red;
484 penalty[j] += 0.5 * (red * red + red);
491 for (
int j = 0; j < numLinks; ++j) {
492 double red = redDuration[j] + firstRedDuration[j];
494 penalty[j] += 0.5 * (red * red + red);
498 const double durationSeconds =
STEPS2TIME(duration);
499 std::set<const MSJunction*> controlledJunctions;
500 std::set<const MSEdge*> shortEdges;;
501 for (
int j = 0; j < numLinks; ++j) {
502 for (
int k = 0; k < (int)
myLinks[j].size(); ++k) {
508 double greenFraction = (durationSeconds - totalRedDuration[j]) / durationSeconds;
514 if (greenFraction == 0.01) {
519 shortEdges.insert(&edge);
530 for (std::set<const MSJunction*>::iterator it = controlledJunctions.begin(); it != controlledJunctions.end(); ++it) {
532 for (ConstMSEdgeVector::const_iterator it_e = incoming.begin(); it_e != incoming.end(); ++it_e) {
568 for (
MSLink* link : currGroup) {
569 link->setTLLogic(
this);
584 const std::string& s = p->getState();
585 assert(linkIndex < (
int)s.size());
633 result += p->duration;
std::vector< const MSEdge * > ConstMSEdgeVector
#define WRITE_WARNINGF(...)
#define WRITE_WARNING(msg)
@ SVC_PEDESTRIAN
pedestrian
LinkState
The right-of-way state of a link between two lanes used when constructing a NBTrafficLightLogic,...
@ LINKSTATE_TL_REDYELLOW
The link has red light (must brake) but indicates upcoming green.
@ LINKSTATE_STOP
This is an uncontrolled, minor link, has to stop.
@ LINKSTATE_TL_GREEN_MAJOR
The link has green light, may pass.
@ LINKSTATE_TL_OFF_BLINKING
The link is controlled by a tls which is off and blinks, has to brake.
@ LINKSTATE_TL_RED
The link has red light (must brake)
@ LINKSTATE_TL_GREEN_MINOR
The link has green light, has to brake.
@ LINKSTATE_TL_OFF_NOSIGNAL
The link is controlled by a tls which is off, not blinking, may pass.
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
A road/street connecting two junctions.
void recalcCache()
Recalculates the cached values.
double getLength() const
return the length of the edge
const MSJunction * getFromJunction() const
const std::string & getEdgeType() const
Returns the type of the edge.
virtual void addEvent(Command *operation, SUMOTime execTimeStep=-1)
Adds an Event.
static double gTLSPenalty
scaled (minimum) time penalty for passing a tls link when routing
The base class for an intersection.
virtual const MSLogicJunction::LinkBits & getResponseFor(int linkIndex) const
Returns the response for the given link.
Representation of a lane in the micro simulation.
MSEdge & getEdge() const
Returns the lane's edge.
const std::vector< MSLink * > & getLinkCont() const
returns the container with all links !!!
bool fromInternalLane() const
return whether the fromLane of this link is an internal lane
void setMesoTLSPenalty(const SUMOTime penalty)
Sets the time penalty for passing a tls-controlled link (meso)
MSJunction * getJunction() const
void setTLState(LinkState state, SUMOTime t)
Sets the current tl-state.
MSLane * getLane() const
Returns the connected lane.
int getIndex() const
Returns the respond index (for visualization)
int getTLIndex() const
Returns the TLS index.
const MSLane * getLaneBefore() const
return the internalLaneBefore if it exists and the laneBefore otherwise
MSLane * getViaLane() const
Returns the following inner lane.
void setGreenFraction(const double fraction)
Sets the green fraction for passing a tls-controlled link (meso)
std::bitset< SUMO_MAX_CONNECTIONS > LinkBits
Container for link response and foes.
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
virtual bool isSelected(const MSTrafficLightLogic *) const
return wheter the given logic (or rather its wrapper) is selected in the GUI
MSEventControl * getBeginOfTimestepEvents()
Returns the event control for events executed at the begin of a time step.
SUMOTime getCurrentTimeStep() const
Returns the current simulation step.
const MESegment::MesoEdgeType & getMesoType(const std::string &typeID)
Returns edge type specific meso parameters if no type specific parameters have been loaded,...
bool hasInternalLinks() const
return whether the network contains internal links
The definition of a single phase of a tls logic.
SUMOTime maxDuration
The maximum duration of the phase.
SUMOTime latestEnd
The maximum time within the cycle for switching (for coordinated actuation)
SUMOTime minDuration
The minimum duration of the phase.
SUMOTime myLastSwitch
Stores the timestep of the last on-switched of the phase.
const std::string & getState() const
Returns the state within this phase.
SUMOTime duration
The duration of the phase.
SUMOTime earliestEnd
The minimum time within the cycle for switching (for coordinated actuation)
Storage for all programs of a single tls.
void executeOnSwitchActions() const
A class that stores and controls tls and switching of their programs.
Class realising the switch between the traffic light phases.
void deschedule(MSTrafficLightLogic *tlLogic)
Marks this swicth as invalid (if the phase duration has changed, f.e.)
SUMOTime shiftTime(SUMOTime currentTime, SUMOTime execTime, SUMOTime newTime)
Reschedule or deschedule the command when quick-loading state.
SwitchCommand(MSTLLogicControl &tlcontrol, MSTrafficLightLogic *tlLogic, SUMOTime nextSwitch)
Constructor.
~SwitchCommand()
Destructor.
SUMOTime getNextSwitchTime() const
Returns the assumed next switch time.
SUMOTime execute(SUMOTime currentTime)
Executes the regarded junction's "trySwitch"- method.
The parent class for traffic light logics.
virtual void adaptLinkInformationFrom(const MSTrafficLightLogic &logic)
Applies information about controlled links and lanes from the given logic.
virtual const MSPhaseDefinition & getCurrentPhaseDef() const =0
Returns the definition of the current phase.
SUMOTime myOffset
the offset parameter of the current program
int getLinkIndex(const MSLink *const link) const
Returns the index of the given link.
virtual SUMOTime mapTimeInCycle(SUMOTime t) const
map the given time into the current cycle
void setCurrentDurationIncrement(SUMOTime delay)
Delays current phase by the given delay.
SUMOTime computeCycleTime(const Phases &phases)
std::map< MSLink *, LinkState > collectLinkStates() const
Returns the (uncontrolled) states of the controlled links.
std::vector< MSLane * > LaneVector
Definition of the list of arrival lanes subjected to this tls.
virtual ~MSTrafficLightLogic()
Destructor.
virtual SUMOTime getLatestEnd(int step=-1) const
virtual void deactivateProgram()
MSTrafficLightLogic(MSTLLogicControl &tlcontrol, const std::string &id, const std::string &programID, const SUMOTime offset, const TrafficLightType logicType, const SUMOTime delay, const Parameterised::Map ¶meters)
Constructor.
std::vector< SUMOTime > myOverridingTimes
A list of duration overrides.
virtual SUMOTime getMinDur(int step=-1) const
void ignoreLinkIndex(int pos)
ignore pedestrian crossing index in mesosim
virtual void changeStepAndDuration(MSTLLogicControl &tlcontrol, SUMOTime simStep, int step, SUMOTime stepDuration)=0
Changes the current phase and her duration.
static const LaneVector myEmptyLaneVector
An empty lane vector.
const TrafficLightType myLogicType
The type of the logic.
bool isSelected() const
whether this logic is selected in the GUI
virtual const MSPhaseDefinition & getPhase(int givenstep) const =0
Returns the definition of the phase from the given position within the plan.
SUMOTime myDefaultCycleTime
The cycle time (without changes)
SUMOTime getTimeInCycle() const
return time within the current cycle
const std::string & getProgramID() const
Returns this tl-logic's id.
const std::string myProgramID
The id of the logic.
TrafficLightType getLogicType() const
Returns the type of the logic.
virtual SUMOTime getEarliestEnd(int step=-1) const
LaneVectorVector myLanes
The list of LaneVectors; each vector contains the incoming lanes that belong to the same link index.
SwitchCommand * mySwitchCommand
The current switch command.
int myNumLinks
number of controlled links
int getNumLinks() const
return the number of controlled link indices
virtual const Phases & getPhases() const =0
Returns the phases of this tls program.
SUMOTime myCurrentDurationIncrement
A value for enlarge the current duration.
virtual SUMOTime getMaxDur(int step=-1) const
virtual bool getsMajorGreen(int linkIndex) const
whether the given link index ever turns 'G'
void resetLinkStates(const std::map< MSLink *, LinkState > &vals) const
Resets the states of controlled links.
virtual void loadState(MSTLLogicControl &tlcontrol, SUMOTime t, int step, SUMOTime spentDuration)
restores the tls state
virtual void activateProgram()
called when switching programs
void addOverridingDuration(SUMOTime duration)
Changes the duration of the next phase.
bool myAmActive
whether the current program is active
SUMOTime getNextSwitchTime() const
Returns the assumed next switch time.
std::set< int > myIgnoredIndices
list of indices that are ignored in mesoscopic simulatino
SUMOTime getSpentDuration(SUMOTime simStep=-1) const
Returns the duration spent in the current phase.
bool setTrafficLightSignals(SUMOTime t) const
Applies the current signal states to controlled links.
virtual void addLink(MSLink *link, MSLane *lane, int pos)
Adds a link on building.
std::vector< MSPhaseDefinition * > Phases
Definition of a list of phases, being the junction logic.
std::vector< MSLink * > LinkVector
Definition of the list of links that are subjected to this tls.
LinkVectorVector myLinks
The list of LinkVectors; each vector contains the links that belong to the same link index.
void initMesoTLSPenalties()
initialize optional meso penalties
virtual void init(NLDetectorBuilder &nb)
Initialises the tls with information about incoming lanes.
Builds detectors for microsim.
Base class for objects which have an id.
const std::string & getID() const
Returns the id.
An upper class for objects with additional parameters.
std::map< std::string, std::string > Map
parameters map
static const std::string ALLOWED_TLS_LINKSTATES
all allowed characters for phase state
#define UNUSED_PARAMETER(x)
edge type specific meso parameters