47 #ifdef CHECK_MEMORY_LEAKS
49 #endif // CHECK_MEMORY_LEAKS
66 : myTLControl(tlcontrol), myTLLogic(tlLogic),
67 myAssumedNextSwitch(nextSwitch), myAmValid(true) {}
81 const bool isActive = myTLControl.isActive(myTLLogic);
82 int step1 = myTLLogic->getCurrentPhaseIndex();
83 SUMOTime next = myTLLogic->trySwitch();
84 int step2 = myTLLogic->getCurrentPhaseIndex();
90 myTLLogic->setTrafficLightSignals(t);
95 myAssumedNextSwitch += next;
102 if (tlLogic == myTLLogic) {
104 myAssumedNextSwitch = -1;
113 const std::string& programID,
SUMOTime delay,
const std::map<std::string, std::string>& parameters) :
130 if (phases.size() > 1) {
131 bool haveWarnedAboutUnusedStates =
false;
132 std::vector<bool> foundGreen(phases.front()->getState().size(),
false);
133 for (
int i = 0; i < (int)phases.size(); ++i) {
135 const int iNext = (i + 1) % phases.size();
136 const std::string& state1 = phases[i]->getState();
137 const std::string& state2 = phases[iNext]->getState();
138 assert(state1.size() == state2.size());
139 if (!haveWarnedAboutUnusedStates && state1.size() >
myLanes.size()) {
143 haveWarnedAboutUnusedStates =
true;
146 for (
int j = 0; j < (int)
MIN3(state1.size(), state2.size(),
myLanes.size()); ++j) {
150 for (LaneVector::const_iterator it =
myLanes[j].begin(); it !=
myLanes[j].end(); ++it) {
154 +
" when switching to phase " +
toString(iNext));
161 for (
int j = 0; j < (int)state1.size(); ++j) {
164 foundGreen[j] =
true;
168 for (
int j = 0; j < (int)foundGreen.size(); ++j) {
169 if (!foundGreen[j]) {
190 while ((
int)
myLinks.size() <= pos) {
196 while ((
int)
myLanes.size() <= pos) {
211 std::map<MSLink*, LinkState>
213 std::map<MSLink*, LinkState> ret;
214 for (LinkVectorVector::const_iterator i1 =
myLinks.begin(); i1 !=
myLinks.end(); ++i1) {
216 for (LinkVector::const_iterator i2 = l.begin(); i2 != l.end(); ++i2) {
217 ret[*i2] = (*i2)->getState();
229 for (
int i = 0; i < (int)
myLinks.size(); i++) {
232 for (LinkVector::const_iterator j = currGroup.begin(); j != currGroup.end(); j++) {
233 (*j)->setTLState(ls, t);
242 for (LinkVectorVector::const_iterator i1 =
myLinks.begin(); i1 !=
myLinks.end(); ++i1) {
244 for (LinkVector::const_iterator i2 = l.begin(); i2 != l.end(); ++i2) {
245 assert(vals.find(*i2) != vals.end());
256 for (LinkVectorVector::const_iterator i1 =
myLinks.begin(); i1 !=
myLinks.end(); ++i1, ++index) {
258 for (LinkVector::const_iterator i2 = l.begin(); i2 != l.end(); ++i2) {
292 const int numLinks = (int)phases.front()->getState().size();
293 assert((
int)
myLinks.size() >= numLinks);
295 std::vector<SUMOReal> redDuration(numLinks, 0);
296 std::vector<SUMOReal> penalty(numLinks, 0);
297 for (
int i = 0; i < (int)phases.size(); ++i) {
298 const std::string& state = phases[i]->getState();
299 duration += phases[i]->duration;
301 for (
int j = 0; j < numLinks; ++j) {
304 redDuration[j] +=
STEPS2TIME(phases[i]->duration);
305 }
else if (redDuration[j] > 0) {
306 penalty[j] += 0.5 * (redDuration[j] * redDuration[j] + redDuration[j]);
312 for (
int j = 0; j < numLinks; ++j) {
313 if (redDuration[j] > 0) {
314 penalty[j] += 0.5 * (redDuration[j] * redDuration[j] + redDuration[j]);
319 std::set<const MSJunction*> controlledJunctions;
320 for (
int j = 0; j < numLinks; ++j) {
321 for (
int k = 0; k < (int)
myLinks[j].size(); ++k) {
323 controlledJunctions.insert(
myLinks[j][k]->getLane()->getEdge().getFromJunction());
329 for (std::set<const MSJunction*>::iterator it = controlledJunctions.begin(); it != controlledJunctions.end(); ++it) {
331 for (ConstMSEdgeVector::const_iterator it_e = incoming.begin(); it_e != incoming.end(); ++it_e) {
SUMOTime myCurrentDurationIncrement
A value for enlarge the current duration.
The link has green light, may pass.
void resetLinkStates(const std::map< MSLink *, LinkState > &vals) const
Resets the states of controlled links.
virtual const MSPhaseDefinition & getCurrentPhaseDef() const =0
Returns the definition of the current phase.
Builds detectors for microsim.
void initMesoTLSPenalties()
initialize optional meso penalties
const std::string & getState() const
Returns the state within this phase.
bool setTrafficLightSignals(SUMOTime t) const
Applies the current signal states to controlled links.
Storage for all programs of a single tls.
std::vector< SUMOTime > myOverridingTimes
A list of duration overrides.
int getLinkIndex(const MSLink *const link) const
Returns the index of the given link.
SwitchCommand(MSTLLogicControl &tlcontrol, MSTrafficLightLogic *tlLogic, SUMOTime nextSwitch)
Constructor.
std::string myProgramID
The id of the logic.
The link has green light, has to brake.
void recalcCache()
Recalculates the cached values.
static const LaneVector myEmptyLaneVector
An empty lane vector.
void setTLState(LinkState state, SUMOTime t)
Sets the current tl-state.
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
MSTrafficLightLogic(MSTLLogicControl &tlcontrol, const std::string &id, const std::string &programID, SUMOTime delay, const std::map< std::string, std::string > ¶meters)
Constructor.
std::vector< const MSEdge * > ConstMSEdgeVector
SUMOTime getCurrentTimeStep() const
Returns the current simulation step.
#define WRITE_WARNING(msg)
LaneVectorVector myLanes
The list of LaneVectors; each vector contains the incoming lanes that belong to the same link index...
virtual ~MSTrafficLightLogic()
Destructor.
SUMOTime getNextSwitchTime() const
Returns the assumed next switch time.
A class that stores and controls tls and switching of their programs.
const std::string & getID() const
Returns the id.
A road/street connecting two junctions.
void deschedule(MSTrafficLightLogic *tlLogic)
Marks this swicth as invalid (if the phase duration has changed, f.e.)
Class realising the switch between the traffic light phases.
~SwitchCommand()
Destructor.
SUMOTime myDefaultCycleTime
The cycle time (without changes)
virtual void init(NLDetectorBuilder &nb)
Initialises the tls with information about incoming lanes.
MSEventControl * getBeginOfTimestepEvents()
Returns the event control for events executed at the begin of a time step.
virtual void adaptLinkInformationFrom(const MSTrafficLightLogic &logic)
Applies information about controlled links and lanes from the given logic.
LinkState
The right-of-way state of a link between two lanes used when constructing a NBTrafficLightLogic, in MSLink and GNEInternalLane.
std::string toString(const T &t, std::streamsize accuracy=OUTPUT_ACCURACY)
An upper class for objects with additional parameters.
virtual SUMOTime addEvent(Command *operation, SUMOTime execTimeStep, AdaptType type)
Adds an Event.
void setCurrentDurationIncrement(SUMOTime delay)
Delays current phase by the given delay.
Base class for objects which have an id.
std::vector< MSLink * > LinkVector
Definition of the list of links that participate in this tl-light.
SUMOTime getNextSwitchTime() const
Returns the assumed next switch time.
std::vector< MSPhaseDefinition * > Phases
Definition of a list of phases, being the junction logic.
virtual const Phases & getPhases() const =0
Returns the phases of this tls program.
void addOverridingDuration(SUMOTime duration)
Changes the duration of the next phase.
LinkVectorVector myLinks
The list of LinkVectors; each vector contains the links that belong to the same link index...
std::vector< MSLane * > LaneVector
Definition of the list of links that participate in this tl-light.
void executeOnSwitchActions() const
The link has red light (must brake)
virtual void addLink(MSLink *link, MSLane *lane, int pos)
Adds a link on building.
SwitchCommand * mySwitchCommand
The current switch command.
The parent class for traffic light logics.
const std::string & getProgramID() const
Returns this tl-logic's id.
static SUMOReal gMesoTLSPenalty
Representation of a lane in the micro simulation.
std::map< MSLink *, LinkState > collectLinkStates() const
Returns the (uncontrolled) states of the controlled links.
The link has red light (must brake) but indicates upcoming green.
SUMOTime execute(SUMOTime currentTime)
Executes the regarded junction's "trySwitch"- method.