49 #ifdef CHECK_MEMORY_LEAKS
51 #endif // CHECK_MEMORY_LEAKS
61 : myCurrentProgram(0) {
66 std::map<std::string, MSTrafficLightLogic*>::const_iterator j;
67 for (std::map<std::string, MSTrafficLightLogic*>::iterator j = myVariants.begin(); j != myVariants.end(); ++j) {
70 for (std::vector<OnSwitchAction*>::iterator i = mySwitchActions.begin(); i != mySwitchActions.end(); ++i) {
78 bool hadErrors =
false;
79 for (std::map<std::string, MSTrafficLightLogic*>::const_iterator j = myVariants.begin(); j != myVariants.end(); ++j) {
81 unsigned int linkNo = (
unsigned int)(*j).second->getLinks().size();
82 bool hadProgramErrors =
false;
83 for (MSTrafficLightLogic::Phases::const_iterator i = phases.begin(); i != phases.end(); ++i) {
84 if ((*i)->getState().length() < linkNo) {
85 hadProgramErrors =
true;
88 if (hadProgramErrors) {
89 WRITE_ERROR(
"Mismatching phase size in tls '" + (*j).second->getID() +
"', program '" + (*j).first +
"'.");
99 myOriginalLinkStates = myCurrentProgram->collectLinkStates();
106 if (myVariants.find(programID) != myVariants.end()) {
112 if (myCurrentProgram == 0) {
113 throw ProcessError(
"No initial signal plan loaded for tls '" + logic->
getID() +
"'.");
117 throw ProcessError(
"Mismatching phase size in tls '" + logic->
getID() +
"', program '" + programID +
"'.");
121 if (myVariants.size() == 0 || isNewDefault) {
122 myCurrentProgram = logic;
125 myVariants[programID] = logic;
126 if (myVariants.size() == 1 || isNewDefault) {
128 executeOnSwitchActions();
136 if (myVariants.find(programID) == myVariants.end()) {
139 return myVariants.find(programID)->second;
145 const std::string& programID) {
146 if (myVariants.find(programID) == myVariants.end()) {
147 if (programID ==
"off") {
151 throw ProcessError(
"Could not build an off-state for tls '" + myCurrentProgram->getID() +
"'.");
155 throw ProcessError(
"Can not switch tls '" + myCurrentProgram->getID() +
"' to program '" + programID +
"';\n The program is not known.");
158 return getLogic(programID);
164 const std::string& state) {
169 std::vector<MSPhaseDefinition*> phases;
170 phases.push_back(phase);
173 std::map<std::string, std::string>());
174 addLogic(
"online", logic,
true,
true);
185 mySwitchActions.push_back(c);
189 std::vector<MSTrafficLightLogic*>
191 std::vector<MSTrafficLightLogic*> ret;
192 std::map<std::string, MSTrafficLightLogic*>::const_iterator i;
193 for (i = myVariants.begin(); i != myVariants.end(); ++i) {
194 ret.push_back((*i).second);
202 return tl == myCurrentProgram;
208 return myCurrentProgram;
215 myCurrentProgram = getLogicInstantiatingOff(tlc, programID);
217 executeOnSwitchActions();
223 for (std::vector<OnSwitchAction*>::const_iterator i = mySwitchActions.begin(); i != mySwitchActions.end(); ++i) {
231 for (std::map<std::string, MSTrafficLightLogic*>::iterator i = myVariants.begin(); i != myVariants.end(); ++i) {
232 (*i).second->addLink(link, lane, pos);
247 if (val.length() == 0) {
259 return gspTime == programTime;
267 assert(toTime >= startOfPhase);
268 return toTime - startOfPhase;
275 SUMOTime diff = getDiffToStartOfPhase(logic, toTime);
317 if (isPosAtGSP(step, *myFrom)) {
319 if (mySwitchSynchron) {
322 switchToPos(step, *myTo,
TIME2STEPS(getGSPValue(*myTo)));
335 unsigned int stepTo = myTo->getIndexFromOffset(gspTo);
336 SUMOTime cycleTimeTo = myTo->getDefaultCycleTime();
337 if (gspTo == cycleTimeTo) {
341 SUMOTime currentPosTo = myTo->getOffsetFromIndex(myTo->getCurrentPhaseIndex());
342 currentPosTo += (myTo->getCurrentPhaseDef().duration - (myTo->getNextSwitchTime() - step));
343 SUMOTime diff = getDiffToStartOfPhase(*myTo, gspTo);
346 if (gspTo >= currentPosTo) {
347 deltaToStretch = (gspTo - currentPosTo);
349 deltaToStretch = (cycleTimeTo - currentPosTo + gspTo);
351 unsigned int newdur = (
unsigned int) myTo->getPhase(stepTo).duration - diff + deltaToStretch;
352 myTo->changeStepAndDuration(myControl, step, stepTo, newdur);
372 if (isPosAtGSP(step, *myFrom)) {
374 if (mySwitchSynchron) {
377 switchToPos(step, *myTo,
TIME2STEPS(getGSPValue(*myTo)));
390 SUMOTime cycleTime = myTo->getDefaultCycleTime();
392 SUMOTime posAfterSyn = myTo->getPhaseIndexAtTime(step);
395 if (posAfterSyn < gspTo) {
396 deltaToCut = posAfterSyn + cycleTime - gspTo;
398 deltaToCut = posAfterSyn - gspTo;
402 int areasNo = getStretchAreaNo(myTo);
403 for (
int i = 0; i < areasNo; i++) {
409 deltaPossible = stretchUmlaufAnz * deltaPossible;
410 if ((deltaPossible > deltaToCut) && (deltaToCut < (cycleTime / 2))) {
411 cutLogic(step, gspTo, deltaToCut);
413 SUMOTime deltaToStretch = (cycleTime - deltaToCut) % cycleTime;
414 stretchLogic(step, gspTo, deltaToStretch);
421 unsigned int actStep = myTo->getIndexFromOffset(startPos);
423 int areasNo = getStretchAreaNo(myTo);
425 for (
int i = 0; i < areasNo; i++) {
429 size_t stepOfBegin = myTo->getIndexFromOffset(begin);
430 if (stepOfBegin == actStep) {
431 if (begin < startPos) {
432 toCut = end - startPos;
436 toCut =
MIN2(allCutTime, toCut);
437 allCutTime = allCutTime - toCut;
440 SUMOTime remainingDur = myTo->getPhase(actStep).duration - getDiffToStartOfPhase(*myTo, startPos);
441 SUMOTime newDur = remainingDur - toCut;
442 myTo->changeStepAndDuration(myControl, step, actStep, newDur);
445 int currStep = (actStep + 1) % (
int)myTo->getPhases().size();
446 while (allCutTime > 0) {
447 for (
int i = currStep; i < (
int) myTo->getPhases().size(); i++) {
448 SUMOTime beginOfPhase = myTo->getOffsetFromIndex(i);
449 SUMOTime durOfPhase = myTo->getPhase(i).duration;
450 SUMOTime endOfPhase = beginOfPhase + durOfPhase;
451 for (
int i = 0; i < areasNo; i++) {
455 if ((beginOfPhase <= begin) && (endOfPhase >= end)) {
457 allCutTime = allCutTime - maxCutOfPhase;
458 durOfPhase = durOfPhase - maxCutOfPhase;
461 myTo->addOverridingDuration(durOfPhase);
469 unsigned int currStep = myTo->getIndexFromOffset(startPos);
470 SUMOTime durOfPhase = myTo->getPhase(currStep).duration;
471 SUMOTime remainingStretchTime = allStretchTime;
473 unsigned int stretchUmlaufAnz = (
unsigned int)
TplConvert::_2SUMOReal(myTo->getParameter(
"StretchUmlaufAnz",
"").c_str());
475 int areasNo = getStretchAreaNo(myTo);
476 for (
int x = 0; x < areasNo; x++) {
480 facSum *= stretchUmlaufAnz;
483 SUMOTime diffToStart = getDiffToStartOfPhase(*myTo, startPos);
484 for (
int x = 0; x < areasNo; x++) {
487 SUMOTime endOfPhase = (startPos + durOfPhase - diffToStart);
488 if (end <= endOfPhase && end >= startPos) {
491 facSum = facSum - fac;
493 remainingStretchTime = allStretchTime - StretchTimeOfPhase;
497 WRITE_WARNING(
"The computed factor sum in WAUT '" + myWAUT.id +
"' at time '" +
toString(
STEPS2TIME(step)) +
"' equals zero;\n assuming an error in WAUT definition.");
500 durOfPhase = durOfPhase - diffToStart + StretchTimeOfPhase;
501 myTo->changeStepAndDuration(myControl, step, currStep, durOfPhase);
503 currStep = (currStep + 1) % (
int)myTo->getPhases().size();
505 while (remainingStretchTime > 0) {
506 for (
unsigned int i = currStep; i < myTo->getPhases().size() && remainingStretchTime > 0; i++) {
507 durOfPhase = myTo->getPhase(i).duration;
508 SUMOTime beginOfPhase = myTo->getOffsetFromIndex(i);
509 SUMOTime endOfPhase = beginOfPhase + durOfPhase;
510 for (
int j = 0; j < areasNo && remainingStretchTime > 0; j++) {
514 if ((beginOfPhase <= end) && (endOfPhase >= end)) {
518 durOfPhase += StretchTimeOfPhase;
519 remainingStretchTime -= StretchTimeOfPhase;
522 myTo->addOverridingDuration(durOfPhase);
558 for (std::map<std::string, TLSLogicVariants*>::const_iterator i =
myLogics.begin(); i !=
myLogics.end(); ++i) {
562 for (std::map<std::string, WAUT*>::const_iterator i =
myWAUTs.begin(); i !=
myWAUTs.end(); ++i) {
570 for (std::map<std::string, TLSLogicVariants*>::const_iterator i =
myLogics.begin(); i !=
myLogics.end(); ++i) {
571 (*i).second->getActive()->setTrafficLightSignals(t);
576 std::vector<MSTrafficLightLogic*>
578 std::vector<MSTrafficLightLogic*> ret;
579 std::map<std::string, TLSLogicVariants*>::const_iterator i;
581 std::vector<MSTrafficLightLogic*> s = (*i).second->getAllLogics();
582 copy(s.begin(), s.end(), back_inserter(ret));
589 std::map<std::string, TLSLogicVariants*>::const_iterator i =
myLogics.find(
id);
599 std::map<std::string, TLSLogicVariants*>::const_iterator i =
myLogics.find(
id);
603 return (*i).second->getLogic(programID);
607 std::vector<std::string>
609 std::vector<std::string> ret;
610 for (std::map<std::string, TLSLogicVariants*>::const_iterator i =
myLogics.begin(); i !=
myLogics.end(); ++i) {
611 ret.push_back((*i).first);
623 std::map<std::string, TLSLogicVariants*>::iterator i =
myLogics.find(
id);
631 std::map<std::string, TLSLogicVariants*>::const_iterator i =
myLogics.find(
id);
641 bool hadErrors =
false;
642 for (std::map<std::string, TLSLogicVariants*>::iterator i =
myLogics.begin(); i !=
myLogics.end(); ++i) {
643 hadErrors |= !(*i).second->checkOriginalTLS();
644 (*i).second->saveInitialStates();
653 std::map<std::string, TLSLogicVariants*>::const_iterator i =
myLogics.find(tl->
getID());
657 return (*i).second->isActive(tl);
663 std::map<std::string, TLSLogicVariants*>::const_iterator i =
myLogics.find(
id);
667 return (*i).second->getActive();
674 std::map<std::string, TLSLogicVariants*>::iterator i =
myLogics.find(
id);
677 throw ProcessError(
"Could not switch tls '" +
id +
"' to program '" + programID +
"': No such tls exists.");
679 (*i).second->switchTo(*
this, programID);
685 const std::string& startProg) {
701 SUMOTime when,
const std::string& to) {
710 s.
when = (
myWAUTs[wautid]->refTime + when) % 86400000;
711 myWAUTs[wautid]->switches.push_back(s);
717 const std::string& tls,
718 const std::string& proc,
728 throw InvalidArgument(
"TLS '" + tls +
"' to switch in WAUT '" + wautid +
"' was not yet defined.");
734 myWAUTs[wautid]->junctions.push_back(j);
736 std::string initProg =
myWAUTs[wautid]->startProg;
737 std::vector<WAUTSwitch>::const_iterator first =
myWAUTs[wautid]->switches.end();
739 for (std::vector<WAUTSwitch>::const_iterator i =
myWAUTs[wautid]->switches.begin(); i !=
myWAUTs[wautid]->switches.end(); ++i) {
741 minExecTime = (*i).when;
744 if (first !=
myWAUTs[wautid]->switches.begin()) {
745 initProg = (*(first - 1)).to;
761 std::string initProg =
myWAUTs[wautid]->startProg;
763 std::vector<WAUTSwitch>::const_iterator first = w->
switches.end();
765 for (std::vector<WAUTSwitch>::const_iterator i = w->
switches.begin(); i != w->
switches.end(); ++i) {
767 minExecTime = (*i).when;
773 std::vector<WAUTSwitch>::const_iterator mbegin = w->
switches.begin();
789 const std::string& wautid = cmd.
getWAUTID();
790 unsigned int& index = cmd.
getIndex();
792 for (std::vector<WAUTJunction>::iterator i =
myWAUTs[wautid]->junctions.begin(); i !=
myWAUTs[wautid]->junctions.end(); ++i) {
798 if ((*i).procedure ==
"GSP") {
800 }
else if ((*i).procedure ==
"Stretch") {
815 if (index == static_cast<unsigned int>(
myWAUTs[wautid]->switches.size())) {
828 switchTo((*i).to->getID(), (*i).to->getProgramID());
837 std::pair<SUMOTime, MSPhaseDefinition>