24 #include "../MSLane.h"
25 #include "../MSEdge.h"
29 #define ANALYSIS_DBG(X) {X}
31 #define ANALYSIS_DBG(X) DBG(X)
38 const std::string&
id,
39 const std::string& programID,
44 const std::map<std::string, std::string>& parameters)
56 const std::string&
id,
57 const std::string& programID,
62 const std::map<std::string, std::string>& parameters,
74 for (std::vector<MSPushButton*>::iterator vIt = mapIt->second.begin(); vIt != mapIt->second.end(); ++vIt) {
78 for (
int i = 0; i < (int)
myPhases.size(); i++) {
93 for (
int step = 0; step < (int)
getPhases().size(); step++) {
102 for (
int phaseStep = 0; phaseStep < (int)
getPhases().size(); phaseStep++) {
113 for (
int step = 0; step < (int)
getPhases().size(); step++) {
146 for (
int i = 0; i < lvv.size(); i++) {
149 for (
int j = 0; j < lv.size(); j++) {
159 if (
getParameter(
"USE_VEHICLE_TYPES_WEIGHTS",
"0") ==
"1") {
177 for (
int i = 0; i <
myLinks.size(); i++) {
180 for (
int j = 0; j < oneLink.size(); j++) {
182 MSLane* lane = oneLink[j]->getLane();
195 for (
int i = 0; i < (int)
myLinks.size(); i++) {
197 for (
int j = 0; j < (int)oneLink.size(); j++) {
198 MSLane* lane = oneLink[j]->getLane();
199 outLanes.push_back(lane);
203 if (outLanes.size() > 0) {
204 myLaneVector.push_back(outLanes);
206 if (myLaneVector.size() > 0) {
218 std::map<int, SUMOTime>::iterator phaseIterator =
targetPhasesCTS.find(phaseStep);
220 phaseIterator->second = 0;
230 for (std::map<int, SUMOTime>::iterator mapIterator =
targetPhasesCTS.begin();
233 int chain = mapIterator->first;
234 SUMOTime oldVal = mapIterator->second;
244 mapIterator->second += elapsedTimeSteps
248 mapIterator->second += elapsedTimeSteps
255 WRITE_ERROR(
"Unrecognized traffic threshold calculation mode");
257 std::ostringstream oss;
258 oss <<
"MSSOTLTrafficLightLogic::updateCTS->TLC " <<
getID() <<
" chain " << chain <<
" oldVal " << oldVal <<
" newVal " << mapIterator->second;
277 for (MSPhaseDefinition::LaneIdVector::const_iterator laneIterator = targetLanes.begin(); laneIterator != targetLanes.end(); laneIterator++) {
290 WRITE_ERROR(
"Unrecognized traffic threshold calculation mode");
303 std::stringstream out;
314 std::ostringstream threshold_str;
327 std::ostringstream str;
336 for (std::map<int, SUMOTime>::const_iterator iterator =
341 std::ostringstream threshold_str;
345 <<
";" << iterator->second <<
";" <<
getPhase(iterator->first).
getState() <<
";"
376 bool usedMaxCTS =
false;
377 std::vector<int> equalIndexes;
381 if (maxLastStep < it->second) {
382 maxLastStep = it->second;
383 equalIndexes.clear();
384 equalIndexes.push_back(it->first);
385 }
else if (maxLastStep == it->second) {
386 equalIndexes.push_back(it->first);
390 if (equalIndexes.size() == 0) {
392 for (std::map<int, SUMOTime>::const_iterator iterator =
targetPhasesCTS.begin();
395 if (maxCTS < iterator->second) {
396 maxCTS = iterator->second;
397 equalIndexes.clear();
398 equalIndexes.push_back(iterator->first);
399 }
else if (maxCTS == iterator->second) {
400 equalIndexes.push_back(iterator->first);
406 std::ostringstream oss;
407 oss <<
"MSSOTLTrafficLightLogic::getPhaseIndexWithMaxCTS-> TLC " <<
getID();
409 oss <<
" maxCTS " << maxCTS;
411 oss <<
" forcing selection since not selected for " << maxLastStep;
413 if (equalIndexes.size() == 1) {
414 oss <<
" phase " << equalIndexes[0];
416 return equalIndexes[0];
420 for (std::vector<int>::const_iterator it = equalIndexes.begin(); it != equalIndexes.end(); ++it) {
423 oss <<
"]. Random select " << index;
480 std::ostringstream oss;
481 oss <<
"Forced selection of the phase " <<
lastChain <<
" since its last selection was " << it->second <<
" changes ago";
485 }
else if (it->first != previousStep) {
501 std::ostringstream oss;
#define WRITE_MESSAGE(msg)
std::string time2string(SUMOTime t)
convert SUMOTime to string
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Representation of a lane in the micro simulation.
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
SUMOTime getCurrentTimeStep() const
Returns the current simulation step.
The definition of a single phase of a tls logic.
const std::string & getState() const
Returns the state within this phase.
std::vector< std::string > LaneIdVector
SUMOTime myLastSwitch
Stores the timestep of the last on-switched of the phase.
const LaneIdVector & getTargetLaneSet() const
bool isDecisional() const
A fixed traffic light logic.
Phases myPhases
The list of phases this logic uses.
const Phases & getPhases() const
Returns the phases of this tls program.
int getCurrentPhaseIndex() const
Returns the current index within the program.
const MSPhaseDefinition & getPhase(int givenstep) const
Returns the definition of the phase from the given position within the plan.
void setStep(int step)
Forces a specific step.
const MSPhaseDefinition & getCurrentPhaseDef() const
Returns the definition of the current phase.
void buildSensors(MSTrafficLightLogic::LaneVectorVector controlledLanes, NLDetectorBuilder &nb)
This function member has to be extended to properly build sensors for the input lanes Sensors has to ...
void buildCountOutSensors(MSTrafficLightLogic::LaneVectorVector controlledLanes, NLDetectorBuilder &nb)
void buildCountSensors(MSTrafficLightLogic::LaneVectorVector controlledLanes, NLDetectorBuilder &nb)
virtual void stepChanged(int newStep)
virtual int countVehicles(MSLane *lane)=0
std::map< int, SUMOTime > lastCheckForTargetPhase
~MSSOTLTrafficLightLogic()
Destructor.
int getPhaseIndexWithMaxCTS()
SUMOTime trySwitch()
Switches to the next phase.
MSSOTLE2Sensors * myCountSensors
double getOutputSensorsLength()
virtual bool canRelease()=0
double getDecayConstant()
double getSpeedThreshold()
int countVehicles(MSPhaseDefinition phase)
MSSOTLTrafficLightLogic(MSTLLogicControl &tlcontrol, const std::string &id, const std::string &programID, const TrafficLightType logicType, const Phases &phases, int step, SUMOTime delay, const std::map< std::string, std::string > ¶meters)
Constructor without sensors passed.
PhasePushButtons m_pushButtons
std::map< int, SUMOTime > targetPhasesCTS
virtual SUMOTime computeReturnTime()
std::map< int, int > targetPhasesLastSelection
void resetCTS(int phaseStep)
MSSOTLSensors * mySensors
void updateDecayThreshold()
bool isDecayThresholdActivated()
double getInputSensorsLength()
virtual int decideNextPhase()
int getTargetPhaseMaxLastSelection()
void init(NLDetectorBuilder &nb)
Initialises the tls with sensors on incoming and outgoing lanes Sensors are built in the simulation a...
SUMOTime getCurrentPhaseElapsed()
bool isPushButtonPressed()
A class that stores and controls tls and switching of their programs.
const LinkVector & getLinksAt(int i) const
Returns the list of links that are controlled by the signals at the given position.
std::vector< LaneVector > LaneVectorVector
Definition of a list that holds lists of lanes that do have the same attribute.
std::vector< MSLane * > LaneVector
Definition of the list of arrival lanes subjected to this tls.
const LaneVectorVector & getLaneVectors() const
Returns the list of lists of all lanes controlled by this tls.
const LinkVectorVector & getLinks() const
Returns the list of lists of all affected links.
LaneVectorVector myLanes
The list of LaneVectors; each vector contains the incoming lanes that belong to the same link index.
std::vector< MSPhaseDefinition * > Phases
Definition of a list of phases, being the junction logic.
std::vector< LinkVector > LinkVectorVector
Definition of a list that holds lists of links that do have the same attribute.
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.
virtual void init(NLDetectorBuilder &nb)
Initialises the tls with information about incoming lanes.
static MsgHandler * getErrorInstance()
Returns the instance to add errors to.
virtual void inform(std::string msg, bool addType=true)
adds a new error to the list
Builds detectors for microsim.
std::string myID
The name of the object.
const std::string & getID() const
Returns the id.
const std::string getParameter(const std::string &key, const std::string defaultValue="") const
Returns the value for a given key.
static double rand(std::mt19937 *rng=nullptr)
Returns a random real number in [0, 1)
static const T & getRandomFrom(const std::vector< T > &v, std::mt19937 *rng=0)
Returns a random element from the given vector.