Eclipse SUMO - Simulation of Urban MObility
MSSwarmTrafficLightLogic.cpp
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2010-2019 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials
5 // are made available under the terms of the Eclipse Public License v2.0
6 // which accompanies this distribution, and is available at
7 // http://www.eclipse.org/legal/epl-v20.html
8 // SPDX-License-Identifier: EPL-2.0
9 /****************************************************************************/
16 // The class for Swarm-based logics
17 /****************************************************************************/
18 
20 #include "../MSEdge.h"
21 
22 #if 1
23 #define ANALYSIS_DBG(X) {X}
24 #else
25 #define ANALYSIS_DBG(X) DBG(X)
26 #endif
27 
29  const std::string& programID, const Phases& phases, int step, SUMOTime delay,
30  const std::map<std::string, std::string>& parameters) :
31  MSSOTLHiLevelTrafficLightLogic(tlcontrol, id, programID, TLTYPE_SWARM_BASED, phases, step, delay, parameters) {
32 
33  std::string pols = getPoliciesParam();
34  std::transform(pols.begin(), pols.end(), pols.begin(), ::tolower);
35  DBG(std::ostringstream str; str << "policies: " << pols; WRITE_MESSAGE(str.str());)
36 
37  if (pols.find("platoon") != std::string::npos) {
38  addPolicy(new MSSOTLPlatoonPolicy(new MSSOTLPolicy5DFamilyStimulus("PLATOON", parameters), parameters));
39  }
40  if (pols.find("phase") != std::string::npos) {
41  addPolicy(new MSSOTLPhasePolicy(new MSSOTLPolicy5DFamilyStimulus("PHASE", parameters), parameters));
42  }
43  if (pols.find("marching") != std::string::npos) {
44  addPolicy(new MSSOTLMarchingPolicy(new MSSOTLPolicy5DFamilyStimulus("MARCHING", parameters), parameters));
45  }
46  if (pols.find("congestion") != std::string::npos) {
47  addPolicy(new MSSOTLCongestionPolicy(new MSSOTLPolicy5DFamilyStimulus("CONGESTION", parameters), parameters));
48  }
49 
50  if (getPolicies().empty()) {
51  WRITE_ERROR("NO VALID POLICY LIST READ");
52  }
53 
54  mustChange = false;
55  skipEta = false;
56  gotTargetLane = false;
57 
58  DBG(
59  std::ostringstream d_str; d_str << getMaxCongestionDuration(); vector<MSSOTLPolicy*> policies = getPolicies();
60 
61  WRITE_MESSAGE("getMaxCongestionDuration " + d_str.str()); for (int i = 0; i < policies.size(); i++) {
62  MSSOTLPolicy* policy = policies[i];
64  std::ostringstream _str;
65  _str << policy->getName() << stim->getMessage() << " getThetaSensitivity " << policy->getThetaSensitivity() << " .";
66  WRITE_MESSAGE(_str.str());
67  })
68  congestion_steps = 0;
69  m_useVehicleTypesWeights = getParameter("USE_VEHICLE_TYPES_WEIGHTS", "0") == "1";
70  if (m_useVehicleTypesWeights && pols.find("phase") == std::string::npos) {
71  WRITE_ERROR("VEHICLE TYPES WEIGHT only works with phase policy, which is missing");
72  }
73 }
74 
76  if (logData && swarmLogFile.is_open()) {
77  swarmLogFile.close();
78  }
79  for (std::map<std::string, CircularBuffer<double>*>::iterator it = m_meanSpeedHistory.begin();
80  it != m_meanSpeedHistory.end(); ++it) {
81  delete it->second;
82  }
83  m_meanSpeedHistory.clear();
84  for (std::map<std::string, CircularBuffer<double>*>::iterator it = m_derivativeHistory.begin();
85  it != m_derivativeHistory.end(); ++it) {
86  delete it->second;
87  }
88  m_derivativeHistory.clear();
89 }
90 
92  //No walking areas
93  if (lane->getEdge().isWalkingArea()) {
94  return false;
95  }
96  //No pedestrian crossing
97  if (lane->getEdge().isCrossing()) {
98  return false;
99  }
100  //No pedestrian only lanes
101  if (lane->getPermissions() == SVC_PEDESTRIAN) {
102  return false;
103  }
104  //No bicycle only lanes
105  if (lane->getPermissions() == SVC_BICYCLE) {
106  return false;
107  }
108  //No pedestrian and bicycle only lanes
109  if (lane->getPermissions() == (SVC_PEDESTRIAN | SVC_BICYCLE)) {
110  return false;
111  }
112  return true;
113 }
114 
117  //Setting the startup policy
118  choosePolicy(0, 0, 0, 0);
119  //Initializing the random number generator to a time-dependent seed
120  srand((int) time(nullptr));
121  //Initializing pheromone maps according to input lanes
122  //For each lane insert a pair into maps
123  MSLane* currentLane = nullptr;
124 
125 // Derivative
126  const int derivativeHistorySize = StringUtils::toInt(getParameter("PHERO_DERIVATIVE_HISTORY_SIZE", "3"));
127  const int meanSpeedHistorySize = StringUtils::toInt(getParameter("PHERO_MEAN_SPEED_HISTORY_SIZE", "3"));
128  m_derivativeAlpha = StringUtils::toDouble(getParameter("PHERO_DERIVATIVE_ALPHA", "1"));
129  m_losCounter = 0;
130  m_losMaxLimit = StringUtils::toInt(getParameter("LOSS_OF_SIGNAL_LIMIT", "10"));
131 
132  int index = 0;
133  for (MSTrafficLightLogic::LaneVectorVector::const_iterator laneVector = myLanes.begin();
134  laneVector != myLanes.end(); laneVector++) {
135  for (MSTrafficLightLogic::LaneVector::const_iterator lane = laneVector->begin(); lane != laneVector->end();
136  lane++) {
137  currentLane = (*lane);
138  if (pheromoneInputLanes.find(currentLane->getID()) == pheromoneInputLanes.end()) {
139  laneCheck[currentLane] = false;
140  if (allowLine(currentLane)) {
141  pheromoneInputLanes.insert(MSLaneId_Pheromone(currentLane->getID(), 0.0));
142 // Consider the derivative only for the input lane
143  m_meanSpeedHistory.insert(std::make_pair(currentLane->getID(), new CircularBuffer<double>(meanSpeedHistorySize)));
144  m_derivativeHistory.insert(std::make_pair(currentLane->getID(), new CircularBuffer<double>(derivativeHistorySize)));
145  ANALYSIS_DBG(
146  WRITE_MESSAGE("MSSwarmTrafficLightLogic::init Intersection " + getID() + " pheromoneInputLanes adding " + currentLane->getID());)
147  } else {
148  ANALYSIS_DBG(
149  WRITE_MESSAGE("MSSwarmTrafficLightLogic::init Intersection " + getID() + " pheromoneInputLanes: lane " + currentLane->getID() + " not allowed");)
150  }
151  }
152  m_laneIndexMap[currentLane->getID()].push_back(index++);
153  }
154  }
155 
157  for (int i = 0; i < (int)myLinks.size(); i++) {
158  LinkVector oneLink = getLinksAt(i);
159  for (int j = 0; j < (int)oneLink.size(); j++) {
160  currentLane = oneLink[j]->getLane();
161  if (pheromoneOutputLanes.find(currentLane->getID()) == pheromoneOutputLanes.end()) {
162  laneCheck[currentLane] = false;
163  if (allowLine(currentLane)) {
164  pheromoneOutputLanes.insert(MSLaneId_Pheromone(currentLane->getID(), 0.0));
165  ANALYSIS_DBG(
166  WRITE_MESSAGE("MSSwarmTrafficLightLogic::init Intersection " + getID() + " pheromoneOutputLanes adding " + currentLane->getID());)
167  } else {
168  ANALYSIS_DBG(
169  WRITE_MESSAGE("MSSwarmTrafficLightLogic::init Intersection " + getID() + " pheromoneOutputLanes lane " + currentLane->getID() + " not allowed");)
170  }
171  }
172  }
173  }
174 
177  //Initializing thresholds for theta evaluations
179 
180  WRITE_MESSAGE("*** Intersection " + getID() + " will run using MSSwarmTrafficLightLogic ***");
181  std::string logFileName = getParameter("SWARMLOG", "");
182  logData = logFileName.compare("") != 0;
183  if (logData) {
184  swarmLogFile.open(logFileName.c_str(), std::ios::out | std::ios::binary);
185  }
186 // Log the initial state
187  ANALYSIS_DBG(
188  WRITE_MESSAGE("TL " + getID() + " time 0 Policy: " + getCurrentPolicy()->getName() + " (pheroIn= 0 ,pheroOut= 0 ) OldPolicy: " + getCurrentPolicy()->getName() + " .");
189 // ostringstream maplog;
190 // for(map<string, vector<int> >::const_iterator mIt = m_laneIndexMap.begin();mIt != m_laneIndexMap.end();++mIt)
191 // {
192 // maplog << mIt->first <<'[';
193 // for(vector<int>::const_iterator vIt = mIt->second.begin();vIt != mIt->second.end();++vIt)
194 // maplog<<*vIt<<", ";
195 // maplog << "] ";
196 // }
197 // WRITE_MESSAGE("Map content " + maplog.str());
198  );
199 }
200 
202  //input
203  for (MSLaneId_PheromoneMap::iterator laneIterator = pheromoneInputLanes.begin();
204  laneIterator != pheromoneInputLanes.end(); laneIterator++) {
205  std::string laneId = laneIterator->first;
206  pheromoneInputLanes[laneId] = 0;
207  }
208  //output
209  for (MSLaneId_PheromoneMap::iterator laneIterator = pheromoneOutputLanes.begin();
210  laneIterator != pheromoneOutputLanes.end(); laneIterator++) {
211  std::string laneId = laneIterator->first;
212  pheromoneOutputLanes[laneId] = 0;
213  }
214 }
215 
217 
218  DBG(
219  MsgHandler::getMessageInstance()->inform("\n" + time2string(MSNet::getInstance()->getCurrentTimeStep()) + " MSSwarmTrafficLightLogic decideNextPhase()"); std::ostringstream dnp; dnp << (MSNet::getInstance()->getCurrentTimeStep()) << " MSSwarmTrafficLightLogic::decideNextPhase:: " << "tlsid=" << getID() << " getCurrentPhaseDef().getState()=" << getCurrentPhaseDef().getState() << " is commit?" << getCurrentPhaseDef().isCommit(); MsgHandler::getMessageInstance()->inform(dnp.str());)
220  // if we're congested, it should be wise to reset and recalculate the pheromone levels after X steps
221 
222  if (getCurrentPhaseDef().isTarget()) {
224  }
225 
226  if (getCurrentPolicy()->getName().compare("Congestion") == 0 && getCurrentPhaseDef().isCommit()) {
227  congestion_steps += 1; //STEPS2TIME(getCurrentPhaseDef().duration);
228  DBG(
229  WRITE_MESSAGE("\n" + time2string(MSNet::getInstance()->getCurrentTimeStep()) + " MSSwarmTrafficLightLogic decideNextPhase()"); std: ostringstream dnp; dnp << (MSNet::getInstance()->getCurrentTimeStep()) << " MSSwarmTrafficLightLogic::decideNextPhase:: " << "tlsid=" << getID() << " congestion_steps=" << congestion_steps; WRITE_MESSAGE(dnp.str());)
231  resetPheromone();
232  congestion_steps = 0;
233  mustChange = true;
234  if (getReinforcementMode() != 0) {
235  skipEta = true;
236  }
237  DBG(
238  WRITE_MESSAGE("\n" + time2string(MSNet::getInstance()->getCurrentTimeStep()) + " MSSwarmTrafficLightLogic decideNextPhase()"); std::ostringstream dnp; dnp << (MSNet::getInstance()->getCurrentTimeStep()) << " MSSwarmTrafficLightLogic::decideNextPhase:: " << "tlsid=" << getID() << " max congestion reached, congestion_steps=" << congestion_steps; WRITE_MESSAGE(dnp.str());)
239  }
240  }
241 
242  //Update pheromone levels
244 
245  /* Since we changed the behaviour of computeReturnTime() in order to update pheromone levels every step
246  * it is now mandatory to check if the duration of a transient phase is elapsed or not*/
247  if (getCurrentPhaseDef().isTransient() && getCurrentPhaseElapsed() < getCurrentPhaseDef().duration) {
248  return getCurrentPhaseIndex();
249  }
250 
251  //Decide the current policy according to pheromone levels. this should be done only at the end of a chain, before selecting the new one
252  if (getCurrentPhaseDef().isCommit()) {
253  //Update learning and forgetting thresholds
255  decidePolicy();
256  gotTargetLane = false;
257  }
258 
259 // double phero =0;
260 // if(getCurrentPhaseDef().isDecisional())
261 // {
262 // for(LaneIdVector::const_iterator it = targetLanes.begin(); it != targetLanes.end(); ++it)
263 // {
264 // string name = (*it);
265 // phero +=pheromoneInputLanes[name];
266 // }
267 // phero /= targetLanes.size() == 0 ? 1 : targetLanes.size();
268 // if(getCurrentPhaseElapsed() >= getCurrentPhaseDef().minDuration)
269 // if(abs(phero-pheroBegin) <= 2)
270 // return getCurrentPhaseIndex() + 1;
271 // }
272  DBG(
273  std::ostringstream str; str << "tlsID=" << getID() << " currentPolicyname=" + getCurrentPolicy()->getName(); WRITE_MESSAGE(str.str());)
274 
275  //Execute current policy. congestion "policy" must maintain the commit phase, and that must be an all-red one
278 // int newStep =getCurrentPolicy()->decideNextPhase(getCurrentPhaseElapsed(), &getCurrentPhaseDef(), getCurrentPhaseIndex(),
279 // getPhaseIndexWithMaxCTS(), isThresholdPassed(), isPushButtonPressed(), countVehicles(getCurrentPhaseDef()));
280 // if(newStep != myStep)
281 // pheroBegin = phero;
282 // return newStep;
283 }
284 
286  //Updating input lanes pheromone: all input lanes without distinction
287  //BETA_NO, GAMMA_NO
289 
290  //BETA_SP, GAMMA_SP
291  //Updating output lanes pheromone: only input lanes currently having green light. Pheromone for non green lanes is "freezed"
292 // if (getCurrentPhaseDef().isDecisional()) {
294 // }
295 }
296 
298  const double beta, const double gamma) {
299  // ANALYSIS_DBG(
300  DBG(
301  std::ostringstream _str; _str << logString << " Lanes " << pheroMap.size() << " TL " << getID() << " ."; WRITE_MESSAGE(time2string(MSNet::getInstance()->getCurrentTimeStep()) + " MSSwarmTrafficLightLogic::updatePheromoneLevels:: " + _str.str());)
302 
303  for (MSLaneId_PheromoneMap::iterator laneIterator = pheroMap.begin(); laneIterator != pheroMap.end();
304  ++laneIterator) {
305  std::string laneId = laneIterator->first;
306  double oldPhero = laneIterator->second;
307  double maxSpeed = getSensors()->getMaxSpeed(laneId);
308  double meanVehiclesSpeed = getSensors()->meanVehiclesSpeed(laneId);
309  bool updatePheromone = (meanVehiclesSpeed > -1);
310  // double pheroAdd = getSensors()->countVehicles(laneId);
311 
312  //derivative
313  double derivative = 0;
314  //If i need to use the derivative for the lane
315  if (m_meanSpeedHistory.find(laneId) != m_meanSpeedHistory.end()) {
316  //Update the derivative
317  if (updatePheromone) {
318  double currentDerivative = 0;
319  m_losCounter = 0;
320  if (m_meanSpeedHistory[laneId]->size() > 0) {
321  //Calculate the current derivative mean with the old speed points
322  for (int i = 0; i < m_meanSpeedHistory[laneId]->size(); ++i)
323  if (i == 0) {
324  currentDerivative += fabs(meanVehiclesSpeed - m_meanSpeedHistory[laneId]->at(i));
325  } else {
326  currentDerivative += fabs(m_meanSpeedHistory[laneId]->at(i - 1) - m_meanSpeedHistory[laneId]->at(i));
327  }
328  currentDerivative /= m_meanSpeedHistory[laneId]->size(); //Non weighted mean
329  }
330  m_meanSpeedHistory[laneId]->push_front(meanVehiclesSpeed);
331  //Check if the current value of the derivative is above the set alpha
332  if (currentDerivative >= m_derivativeAlpha) {
333  m_derivativeHistory[laneId]->push_front(currentDerivative);
334  }
335  if (m_derivativeHistory[laneId]->size() > 0) {
336  //Calculate the mean derivative with the old derivative
337  for (int i = 0; i < m_derivativeHistory[laneId]->size(); ++i) {
338  derivative += m_derivativeHistory[laneId]->at(i);
339  }
340  derivative /= m_derivativeHistory[laneId]->size();
341  }
342  } else {
343  //Reset the values if no information is received after a timeout
344  ++m_losCounter;
345  if (m_losCounter >= m_losMaxLimit) {
346  m_derivativeHistory[laneId]->clear();
347  m_meanSpeedHistory[laneId]->clear();
348  m_meanSpeedHistory[laneId]->push_front(maxSpeed);
349  }
350  }
351  }
352  double pheroAdd = MAX2((maxSpeed - meanVehiclesSpeed) * 10 / maxSpeed, 0.0);
353 // Use the derivative only if it has a value
354  if (derivative > 0)
355 // Correct the pheromone value by dividing it for the derivative.
356  {
357  pheroAdd /= MAX2(derivative, m_derivativeAlpha);
358  }
359 // pheroAdd /= max(derivative, 1.0);
360  ANALYSIS_DBG(
361  if (updatePheromone) {
362  std::ostringstream oss;
363  oss << time2string(MSNet::getInstance()->getCurrentTimeStep()) << " l " << laneId;
364  oss << " der " << derivative << " phero " << pheroAdd << " maxS " << maxSpeed << " meanS " << meanVehiclesSpeed;
365  WRITE_MESSAGE(oss.str())
366  }
367  )
368 
369  // Evaporation + current contribute
370  double phero = beta * oldPhero + gamma * pheroAdd * updatePheromone;
371  ANALYSIS_DBG(
372  if (phero > 10) {
373  std::ostringstream i_str;
374  i_str << "MSSwarmTrafficLightLogic::updatePheromoneLevels " << logString << " > 10. Value: " << phero;
375  WRITE_MESSAGE(i_str.str())
376  });
377 
378  phero = MIN2(MAX2(phero, 0.0), getPheroMaxVal());
379  pheroMap[laneId] = phero;
380  ANALYSIS_DBG(
381  // DBG(
382  std::ostringstream i_str;
383  // i_str << " oldPheroIn " << oldPheroIn
384  // << " inMeanVehiclesSpeed " << meanVehiclesSpeed
385  // << " pheroInAdd " << pheroAdd * updatePheromoneIn
386  // << " pheroInEvaporated " << oldPheroIn-oldPheroIn*getBetaNo()
387  // << " pheroInDeposited " << getGammaNo() * pheroAdd * updatePheromoneIn
388  // <<" newPheroIn "<<pheromoneInputLanes[laneId]
389  // << " inLane "<< laneId<<" ID "<< getID() <<" .";
390  i_str << " op " << oldPhero << " ms " << meanVehiclesSpeed << " p " << pheroAdd * updatePheromone <<
391  " pe " << oldPhero - oldPhero * beta << " pd " << gamma * pheroAdd * updatePheromone << " np " <<
392  pheroMap[laneId] << " l " << laneId << " ID " << getID() << " c " << getSensors()->countVehicles(laneId) << " s " << getLaneLightState(laneId) << " ."; if (m_pheroLevelLog[laneId] != i_str.str()) {
393  m_pheroLevelLog[laneId] = i_str.str();
394  WRITE_MESSAGE(time2string(MSNet::getInstance()->getCurrentTimeStep()) + " MSSwarmTrafficLightLogic::updatePheromoneLevels:: " + logString + i_str.str());
395  })
396 
397  DBG(
398  std::ostringstream str; str << time2string(MSNet::getInstance()->getCurrentTimeStep()) << " MSSwarmTrafficLightLogic::countSensors:: lane " << laneId << " passedVeh " << getCountSensors()->getPassedVeh(laneId, false); WRITE_MESSAGE(str.str());)
399 
400 // int vehicles = getSensors()->countVehicles(laneId);
401 // double pheroIn = getBetaNo() * oldPheroIn + // Evaporation
402 // getGammaNo() * vehicles;
403 // DBG(
404 // std::ostringstream i_str;
405 // i_str << " vehicles " << getSensors()->countVehicles(laneId)<<" pheromoneInputLanes "<<pheromoneInputLanes[laneId] << " lane "<< laneId<<" ID "<< getID() <<" .";
406 // MsgHandler::getMessageInstance()->inform(time2string(MSNet::getInstance()->getCurrentTimeStep()) +" MSSwarmTrafficLightLogic::updatePheromoneLevels:: PheroIn"+i_str.str());
407 // )
408 //
409 // pheroIn = MIN2(MAX2(pheroIn, 0.0), getPheroMaxVal());
410 // pheromoneInputLanes[laneId] = pheroIn;
411  }
412 }
414  double elapsedTime = STEPS2TIME(MSNet::getInstance()->getCurrentTimeStep() - lastThetaSensitivityUpdate);
416 
418  std::vector<MSSOTLPolicy*> policies = getPolicies();
419 
420  //reset of the sensitivity thresholds in case of 0 pheromone on the input lanes
421  if (getPheromoneForInputLanes() == 0) {
422  for (int i = 0; i < (int)policies.size(); i++) {
423  policies[i]->setThetaSensitivity(getThetaInit());
424 // ANALYSIS_DBG(
425  DBG(
426  std::ostringstream phero_str; phero_str << "Policy " << policies[i]->getName() << " sensitivity reset to " << policies[i]->getThetaSensitivity() << " due to evaporated input pheromone."; WRITE_MESSAGE(time2string(MSNet::getInstance()->getCurrentTimeStep()) + " MSSwarmTrafficLightLogic::updateSensitivities::" + phero_str.str());)
427  }
428  return;
429  }
430 
431  double eta = -1.;
432  // If skipEta it means that we've had Congestion for too much time. Forcing forgetting.
433  if (!skipEta || currentPolicy->getName().compare("Congestion") != 0) {
434  switch (getReinforcementMode()) {
435  case 0:
436  if (elapsedTime == STEPS2TIME(MSNet::getInstance()->getCurrentTimeStep())) {
437  return; //we don't want to reinforce the policy selected at the beginning of the simulation since it's time-based
438  }
439  eta = elapsedTime;
440  break;
441  case 1:
442  eta = calculateEtaDiff();
443  break;
444  case 2:
445  eta = calculateEtaRatio();
446  break;
447  }
448  }
449  for (int i = 0; i < (int)policies.size(); i++) {
450  MSSOTLPolicy* policy = policies[i];
451  double newSensitivity;
452  if (eta < 0) { //bad performance
453  if (policy == currentPolicy) { // punish the current policy
454  newSensitivity = policy->getThetaSensitivity() + getForgettingCox() * (-eta);
455  } else
456  // reward the other ones
457  {
458  newSensitivity = policy->getThetaSensitivity() - getLearningCox() * (-eta);
459  }
460  } else { //good performance
461  if (policy == currentPolicy) { //reward the current policy
462  newSensitivity = policy->getThetaSensitivity() - getLearningCox() * eta;
463  } else
464  // punish the other ones
465  {
466  newSensitivity = policy->getThetaSensitivity() + getForgettingCox() * eta;
467  }
468  }
469 // ANALYSIS_DBG(
470  DBG(
471  std::ostringstream lf; std::ostringstream phero_str; if (getReinforcementMode() == 0) {
472  if (policy == currentPolicy) {
473  lf << " ,LearningCox " << getLearningCox() << " ,LCox*Time " << getLearningCox() * elapsedTime;
474  } else {
475  lf << " ,ForgettingCox " << getForgettingCox() << " ,FCox*Time " << getForgettingCox() * elapsedTime;
476  }
477 
478  phero_str << " policy " << policy->getName() << " newSensitivity " << newSensitivity << " ,pol.Sensitivity " << policy->getThetaSensitivity() << " ,elapsedTime " << elapsedTime << lf.str() << " NEWERSensitivity= " << max(min(newSensitivity, getThetaMax()), getThetaMin()) << " ID " << getID() << " .";
479  } else {
480  if (policy == currentPolicy && eta > 0) {
481  lf << " ,LearningCox " << getLearningCox() << " ,LCox*Eta " << getLearningCox() * eta;
482  } else if (policy == currentPolicy && eta < 0) {
483  lf << " ,ForgettingCox " << getForgettingCox() << " ,FCox*Eta " << getForgettingCox() * eta;
484  } else if (eta > 0) {
485  lf << " ,ForgettingCox " << getForgettingCox() << " ,FCox*Eta " << getForgettingCox() * eta;
486  } else if (eta < 0) {
487  lf << " ,LearningCox " << getLearningCox() << " ,LCox*Eta " << getLearningCox() * eta;
488  }
489  phero_str << " policy " << policy->getName() << " newSensitivity " << newSensitivity << " ,pol.Sensitivity " << policy->getThetaSensitivity() << " ,eta " << eta << " ,carsIn " << carsIn << " ,inTarget " << inTarget << " ,notTarget " << notTarget << " ,carsOut " << carsOut << lf.str() << " NEWERSensitivity= " << max(min(newSensitivity, getThetaMax()), getThetaMin()) << " ID " << getID() << " .";
490  }
491  WRITE_MESSAGE(time2string(MSNet::getInstance()->getCurrentTimeStep()) + " MSSwarmTrafficLightLogic::updateSensitivities::" + phero_str.str());)
492 
493  newSensitivity = MAX2(MIN2(newSensitivity, getThetaMax()), getThetaMin());
494  policy->setThetaSensitivity(newSensitivity);
495  }
496 }
497 
499  if (pheromoneInputLanes.size() == 0) {
500  return 0;
501  }
502  double pheroIn = 0;
503  for (MSLaneId_PheromoneMap::const_iterator iterator = pheromoneInputLanes.begin();
504  iterator != pheromoneInputLanes.end(); iterator++) {
505  std::string laneId = iterator->first;
506  pheroIn += iterator->second;
507  DBG(
508  std::ostringstream phero_str; phero_str << " lane " << iterator->first << " pheromoneIN " << iterator->second << " id " << getID() << " ."; WRITE_MESSAGE(time2string(MSNet::getInstance()->getCurrentTimeStep()) + " MSSwarmTrafficLightLogic::getPheromoneForInputLanes::" + phero_str.str());)
509  }
510 
511  DBG(
512  std::ostringstream o_str; o_str << " TOTpheromoneIN " << pheroIn << " return " << pheroIn / pheromoneInputLanes.size() << getID() << " ."; WRITE_MESSAGE(time2string(MSNet::getInstance()->getCurrentTimeStep()) + " MSSwarmTrafficLightLogic::getPheromoneForInputLanes::" + o_str.str());)
513  return pheroIn / pheromoneInputLanes.size();
514 }
515 
517  if (pheromoneOutputLanes.size() == 0) {
518  return 0;
519  }
520  double pheroOut = 0;
521  for (MSLaneId_PheromoneMap::const_iterator iterator = pheromoneOutputLanes.begin();
522  iterator != pheromoneOutputLanes.end(); iterator++) {
523  DBG(
524  std::ostringstream phero_str; phero_str << " lane " << iterator->first << " pheromoneOUT " << iterator->second << " id " << getID() << " ."; WRITE_MESSAGE(time2string(MSNet::getInstance()->getCurrentTimeStep()) + " MSSwarmTrafficLightLogic::getPheromoneForOutputLanes::" + phero_str.str());)
525  pheroOut += iterator->second;
526  }
527  DBG(
528  std::ostringstream o_str; o_str << " TOTpheromoneOUT " << pheroOut << " return " << pheroOut / pheromoneOutputLanes.size() << " id " << getID() << " ."; WRITE_MESSAGE(time2string(MSNet::getInstance()->getCurrentTimeStep()) + " MSSwarmTrafficLightLogic::getPheromoneForOutputLanes::" + o_str.str());)
529  return pheroOut / pheromoneOutputLanes.size();
530 }
531 
533  if (pheromoneInputLanes.size() == 0) {
534  return 0;
535  }
536  double sum = 0;
537  for (MSLaneId_PheromoneMap::const_iterator iterator = pheromoneInputLanes.begin();
538  iterator != pheromoneInputLanes.end(); iterator++) {
539  std::string laneId = iterator->first;
540  sum += pow(iterator->second - average_phero_in, 2);
541  }
542 
543  double result = sqrt(sum / pheromoneInputLanes.size()) * getScaleFactorDispersionIn();
544  DBG(
545  ostringstream so_str; so_str << " dispersionIn " << result; WRITE_MESSAGE("MSSwarmTrafficLightLogic::getDispersionForInputLanes::" + so_str.str());)
546  return result;
547 }
548 
550  if (pheromoneOutputLanes.size() == 0) {
551  return 0;
552  }
553  double sum = 0;
554  for (MSLaneId_PheromoneMap::const_iterator iterator = pheromoneOutputLanes.begin();
555  iterator != pheromoneOutputLanes.end(); iterator++) {
556  sum += pow(iterator->second - average_phero_out, 2);
557  }
558 
559  double result = sqrt(sum / pheromoneOutputLanes.size()) * getScaleFactorDispersionOut();
560  DBG(
561  ostringstream so_str; so_str << " dispersionOut " << result; WRITE_MESSAGE("MSSwarmTrafficLightLogic::getDispersionForOutputLanes::" + so_str.str());)
562  return result;
563 }
565  if (pheromoneInputLanes.size() == 0) {
566  return 0;
567  }
568  double max_phero_val_current = 0;
569  double max_phero_val_old = 0;
570  double temp_avg_other_lanes = 0;
571  std::string laneId_max;
572  int counter = 0;
573  for (MSLaneId_PheromoneMap::const_iterator iterator = pheromoneInputLanes.begin();
574  iterator != pheromoneInputLanes.end(); iterator++) {
575  std::string laneId = iterator->first;
576  double lanePhero = iterator->second;
577  if (counter == 0) {
578  max_phero_val_current = lanePhero;
579  counter++;
580  continue;
581  }
582  if (lanePhero > max_phero_val_current) {
583  max_phero_val_old = max_phero_val_current;
584  max_phero_val_current = lanePhero;
585  temp_avg_other_lanes = (temp_avg_other_lanes * (counter - 1) + max_phero_val_old) / counter;
586  } else {
587  temp_avg_other_lanes = (temp_avg_other_lanes * (counter - 1) + lanePhero) / counter;
588  }
589 
590  counter++;
591  }
592 
593  double result = max_phero_val_current - temp_avg_other_lanes;
594  DBG(
595  ostringstream so_str; so_str << " currentMaxPhero " << max_phero_val_current << " lane " << laneId_max << " avgOtherLanes " << temp_avg_other_lanes << " distance " << result; WRITE_MESSAGE("MSSwarmTrafficLightLogic::getDistanceOfMaxPheroForInputLanes::" + so_str.str());)
596  return result;
597 }
598 
600  if (pheromoneOutputLanes.size() == 0) {
601  return 0;
602  }
603  double max_phero_val_current = 0;
604  double max_phero_val_old = 0;
605  double temp_avg_other_lanes = 0;
606  std::string laneId_max;
607  int counter = 0;
608  for (MSLaneId_PheromoneMap::const_iterator iterator = pheromoneOutputLanes.begin();
609  iterator != pheromoneOutputLanes.end(); iterator++) {
610  std::string laneId = iterator->first;
611  double lanePhero = iterator->second;
612  if (counter == 0) {
613  max_phero_val_current = lanePhero;
614  counter++;
615  continue;
616  }
617  if (lanePhero > max_phero_val_current) {
618  max_phero_val_old = max_phero_val_current;
619  max_phero_val_current = lanePhero;
620  temp_avg_other_lanes = (temp_avg_other_lanes * (counter - 1) + max_phero_val_old) / counter;
621  } else {
622  temp_avg_other_lanes = (temp_avg_other_lanes * (counter - 1) + lanePhero) / counter;
623  }
624 
625  counter++;
626  }
627 
628  double result = max_phero_val_current - temp_avg_other_lanes;
629  DBG(
630  ostringstream so_str; so_str << " currentMaxPhero " << max_phero_val_current << " lane " << laneId_max << " avgOtherLanes " << temp_avg_other_lanes << " distance " << result; WRITE_MESSAGE("MSSwarmTrafficLightLogic::getDistanceOfMaxPheroForOutputLanes::" + so_str.str());)
631  return result;
632 }
634 // MSSOTLPolicy* currentPolicy = getCurrentPolicy();
635  // Decide if it is the case to check for another plan
636 // double sampled = (double) RandHelper::rand(RAND_MAX);
637  double sampled = RandHelper::rand();
638  double changeProb = getChangePlanProbability();
639 // changeProb = changeProb * RAND_MAX;
640 
641  if (sampled <= changeProb || mustChange) { // Check for another plan
642 
643  double pheroIn = getPheromoneForInputLanes();
644  double pheroOut = getPheromoneForOutputLanes();
645  //double dispersionIn = getDispersionForInputLanes(pheroIn);
646  //double dispersionOut = getDispersionForOutputLanes(pheroOut);
647  double distancePheroIn = getDistanceOfMaxPheroForInputLanes();
648  double distancePheroOut = getDistanceOfMaxPheroForOutputLanes();
649  MSSOTLPolicy* oldPolicy = getCurrentPolicy();
650  choosePolicy(pheroIn, pheroOut, distancePheroIn, distancePheroOut);
651  MSSOTLPolicy* newPolicy = getCurrentPolicy();
652 
653  if (newPolicy != oldPolicy) {
654  ANALYSIS_DBG(
655  SUMOTime step = MSNet::getInstance()->getCurrentTimeStep(); std::ostringstream phero_str; phero_str << " (pheroIn= " << pheroIn << " ,pheroOut= " << pheroOut << " )"; WRITE_MESSAGE("TL " + getID() + " time " + time2string(step) + " Policy: " + newPolicy->getName() + phero_str.str() + " OldPolicy: " + oldPolicy->getName() + " id " + getID() + " .");)
656  if (oldPolicy->getName().compare("Congestion") == 0) {
657  congestion_steps = 0;
658  }
659  } else { //debug purpose only
660  ANALYSIS_DBG(
661  std::ostringstream phero_str; phero_str << " (pheroIn= " << pheroIn << " ,pheroOut= " << pheroOut << " )"; SUMOTime step = MSNet::getInstance()->getCurrentTimeStep(); WRITE_MESSAGE("TL " + getID() + " time " + time2string(step) + " Policy: Nochanges" + phero_str.str() + " OldPolicy: " + oldPolicy->getName() + " id " + getID() + " .");)
662  }
663 
664  mustChange = false;
665  skipEta = false;
666  }
667 }
668 
670  if (factor == 0) {
671  return 1;
672  }
673  if (factor == 1) {
674  return 0.2;
675  } else {
676  return 1 - (1 / ((double) factor));
677  }
678 }
679 
681 
682  MSLane* currentLane = nullptr;
683  int count = 0, minIn = 0, minOut = 0, toSub, tmp;
684  bool inInit = true, outInit = true;
685  double eta, normalized, diff, phi, delta;
686  LaneIdVector toReset;
687 
688  carsIn = 0;
689  carsOut = 0;
690  inTarget = 0;
691  notTarget = 0;
692 
694 
695  // Search the incoming lane to get the count of the vehicles passed. [IN]
696  for (MSTrafficLightLogic::LaneVectorVector::const_iterator laneVector = myLanes.begin();
697  laneVector != myLanes.end(); laneVector++) {
698  for (MSTrafficLightLogic::LaneVector::const_iterator lane = laneVector->begin(); lane != laneVector->end();
699  lane++) {
700  currentLane = (*lane);
701 
702  // Map to avoid check the lane for every possible direction
703  if (laneCheck[currentLane] == false) {
704  // Get the vehicles passed from this lane.
705  count = sensors->getPassedVeh(currentLane->getID(), false);
706 
707  DBG(
708  std::ostringstream cars_str; cars_str << "Lane " << currentLane->getID() << ": vehicles entered - " << count; WRITE_MESSAGE(time2string(MSNet::getInstance()->getCurrentTimeStep()) + " MSSwarmTrafficLightLogic::calculateEta::" + cars_str.str());)
709 
710  // Increment the global count of the cars passed through the tl
711  carsIn += count;
712  // Set to true to skip similar lane since there's just one sensor
713  laneCheck[currentLane] = true;
714  }
715  }
716  }
717 
718  // Search the outgoing lane to get the count of the vehicles passed. [OUT]
719  // We use the links to get the respective lane id.
720  for (MSTrafficLightLogic::LinkVectorVector::const_iterator linkVector = myLinks.begin();
721  linkVector != myLinks.end(); linkVector++) {
722  for (MSTrafficLightLogic::LinkVector::const_iterator link = linkVector->begin(); link != linkVector->end();
723  link++) {
724  currentLane = (*link)->getLane();
725 
726  // Map to avoid check the lane for every possible direction
727  if (laneCheck[currentLane] == false) {
728  // Get the vehicles passed from this lane.
729  count = sensors->getPassedVeh(currentLane->getID(), true);
730 
731  DBG(
732  std::ostringstream cars_str; cars_str << "Lane " << currentLane->getID() << ": vehicles gone out- " << count; WRITE_MESSAGE(time2string(MSNet::getInstance()->getCurrentTimeStep()) + " MSSwarmTrafficLightLogic::calculateEta::" + cars_str.str());)
733 
734  // Increment the global count of the cars passed through the tl
735  carsOut += count;
736 
737  // Since there's no output target lanes we check here the minimum number of
738  // cars passed though the tl. This ahs to be done to all the output lanes since cars can go
739  // in any direction from a target lane. If a direction isn't reachable the sensor count will be 0.
740  // This is done to update the sensorCount value in order to don't make it grow too much.
741  if (count != 0) {
742  toReset.push_back(currentLane->getID());
743  if (outInit) {
744  minOut = count;
745  outInit = false;
746  } else if (count <= minOut) {
747  minOut = count;
748  }
749  }
750  // Set to true to skip similar lane since there's just one sensor
751  laneCheck[currentLane] = true;
752  }
753  }
754  }
755  // Reset the map to check again all the lane on the next commit.
756  resetLaneCheck();
757 
758  // We retrieve the minimum number of cars passed from the target lanes.
759  for (LaneIdVector::const_iterator laneId = targetLanes.begin(); laneId < targetLanes.end(); laneId++) {
760  std::string lane = (*laneId);
761  tmp = sensors->getPassedVeh(lane, false);
762  inTarget += tmp;
763  if (inInit && tmp != 0) {
764  minIn = tmp;
765  inInit = false;
766  }
767  if (tmp < minIn && tmp != 0) {
768  minIn = tmp;
769  }
770  if (tmp != 0) {
771  toReset.push_back(lane);
772  }
773  DBG(
774  std::ostringstream cars_str; cars_str << "Lane " << lane << " passed: " << tmp; WRITE_MESSAGE(time2string(MSNet::getInstance()->getCurrentTimeStep()) + " MSSwarmTrafficLightLogic::calculateEta::" + cars_str.str());)
775  }
776 
777  // The cars not on a target lane counted as in.
779 
780  // Calculate the min beetween the min number of cars entered the tl (minIn) and the
781  // ones that have exit the tl (minOut)
782  toSub = std::min(minIn, minOut);
783 
784  // Subtract the value to all the sensor on the target lanes.
785  while (!toReset.empty()) {
786  std::string laneId = toReset.back();
787  toReset.pop_back();
788  sensors->subtractPassedVeh(laneId, toSub);
789  }
790 
791  //Normalized to 1
792  diff = inTarget - carsOut;
793  normalized = diff / inTarget;
794 
795  // Analize difference to return an appropriate eta to reinforce/forget the policies.
796 
797  DBG(
798  std::ostringstream final_str; final_str << "Total cars in lanes: " << carsIn << " Total cars out: " << carsOut << " Difference: " << diff << " Pure eta: " << normalized; WRITE_MESSAGE(time2string(MSNet::getInstance()->getCurrentTimeStep()) + " MSSwarmTrafficLightLogic::calculateEta::" + final_str.str());)
799  DBG(
800  std::ostringstream eta_str; eta_str << "IN:" << inTarget << " OUT:" << carsOut << " R:" << notTarget; WRITE_MESSAGE(time2string(MSNet::getInstance()->getCurrentTimeStep()) + " MSSwarmTrafficLightLogic::calculateEta::" + eta_str.str());)
801  DBG(
802  std::ostringstream eta_str; eta_str << "Min found:" << toSub << " MinIn:" << minIn << " MinOut:" << minOut; WRITE_MESSAGE(time2string(MSNet::getInstance()->getCurrentTimeStep()) + " MSSwarmTrafficLightLogic::calculateEta::" + eta_str.str());)
803 
804  // IN > OUT
805  if (inTarget > carsOut) {
806  if (carsOut == 0) {
807  // We're in Congestion but not for long so we don't do nothing. When we reach max steps for
808  // Congestion the evaluation of eta is skipped and we force a forget of the policy
809  if (getCurrentPolicy()->getName().compare("Congestion") == 0) {
810  eta = 0;
811  }
812  // vehicles aren't going out and we've additional vehicle on a red lane. We set
813  // eta to -1 to forget
814  else {
815  eta = -1;
816  }
817  } else {
818  // Forget - Amplify to R
819  phi = calculatePhi(notTarget);
820  eta = (-normalized * (1 / phi));
821  if (eta < -1.0) {
822  eta = -1.0;
823  }
824  }
825  }
826 
827  // IN = OUT
828  else if (inTarget == carsOut) {
829  // Can't say nothing
830  if (inTarget == 0) {
831  eta = 0;
832  }
833 
834  // Reinforce - Attenuate to R
835  // Normalized = 0 --> use delta = 1-1/IN
836  else {
837  delta = calculatePhi(inTarget);
838  phi = calculatePhi(notTarget);
839  eta = delta * phi;
840  if (eta > 1.0) {
841  eta = 1.0;
842  }
843  }
844  }
845 
846  // IN < OUT
847  else {
848  // Can't say nothing
849  if (inTarget == 0) {
850  eta = 0;
851  }
852 
853  // Reinforce - Attenuate to R
854  else {
855  phi = calculatePhi(notTarget);
856  diff = inTarget - carsOut;
857  normalized = diff / carsOut;
858  eta = normalized * phi;
859  if (eta > 1.0) {
860  eta = 1.0;
861  }
862  }
863  }
864 
865  DBG(
866  std::ostringstream eta_str; eta_str << "Eta Normalized: " << eta; WRITE_MESSAGE(time2string(MSNet::getInstance()->getCurrentTimeStep()) + " MSSwarmTrafficLightLogic::calculateEta::" + eta_str.str());)
867  return eta;
868 }
869 
871  MSLane* currentLane = nullptr;
872  int count = 0, minIn = 0, minOut = 0, toSub, tmp;
873  bool inInit = true, outInit = true;
874  double eta, ratio, phi, normalized, delta;
875  LaneIdVector toReset;
876 
877  carsIn = 0;
878  carsOut = 0;
879  inTarget = 0;
880  notTarget = 0;
881 
883 
884  // Search the incoming lane to get the count of the vehicles passed. [IN]
885  for (MSTrafficLightLogic::LaneVectorVector::const_iterator laneVector = myLanes.begin();
886  laneVector != myLanes.end(); laneVector++) {
887  for (MSTrafficLightLogic::LaneVector::const_iterator lane = laneVector->begin(); lane != laneVector->end();
888  lane++) {
889  currentLane = (*lane);
890 
891  // Map to avoid check the lane for every possible direction
892  if (laneCheck[currentLane] == false) {
893  // Get the vehicles passed from this lane.
894  count = sensors->getPassedVeh(currentLane->getID(), false);
895 
896  DBG(
897  std::ostringstream cars_str; cars_str << "Lane " << currentLane->getID() << ": vehicles entered - " << count; WRITE_MESSAGE(time2string(MSNet::getInstance()->getCurrentTimeStep()) + " MSSwarmTrafficLightLogic::calculateEta::" + cars_str.str());)
898 
899  // Increment the global count of the cars passed through the tl
900  carsIn += count;
901  // Set to true to skip similar lane since there's just one sensor
902  laneCheck[currentLane] = true;
903  }
904  }
905  }
906 
907  // Search the outgoing lane to get the count of the vehicles passed. [OUT]
908  // We use the links to get the respective lane id.
909  for (MSTrafficLightLogic::LinkVectorVector::const_iterator linkVector = myLinks.begin();
910  linkVector != myLinks.end(); linkVector++) {
911  for (MSTrafficLightLogic::LinkVector::const_iterator link = linkVector->begin(); link != linkVector->end();
912  link++) {
913  currentLane = (*link)->getLane();
914 
915  // Map to avoid check the lane for every possible direction
916  if (laneCheck[currentLane] == false) {
917  // Get the vehicles passed from this lane.
918  count = sensors->getPassedVeh(currentLane->getID(), true);
919 
920  DBG(
921  std::ostringstream cars_str; cars_str << "Lane " << currentLane->getID() << ": vehicles gone out- " << count; WRITE_MESSAGE(time2string(MSNet::getInstance()->getCurrentTimeStep()) + " MSSwarmTrafficLightLogic::calculateEta::" + cars_str.str());)
922 
923  // Increment the global count of the cars passed through the tl
924  carsOut += count;
925 
926  // Since there's no output target lanes we check here the minimum number of
927  // cars passed though the tl. This has to be done to all the output lanes since cars can go
928  // in any direction from a target lane. If a direction isn't reachable the sensor count will be 0.
929  // This is done to update the sensorCount value in order to don't make it grow too much.
930  if (count != 0) {
931  toReset.push_back(currentLane->getID());
932  if (outInit) {
933  minOut = count;
934  outInit = false;
935  } else if (count <= minOut) {
936  minOut = count;
937  }
938  }
939 
940  // Set to true to skip similar lane since there's just one sensor
941  laneCheck[currentLane] = true;
942  }
943  }
944  }
945  // Reset the map to check again all the lane on the next commit.
946  resetLaneCheck();
947 
948  // We retrieve the minimum number of cars passed from the target lanes.
949  for (LaneIdVector::const_iterator laneId = targetLanes.begin(); laneId < targetLanes.end(); laneId++) {
950  std::string lane = (*laneId);
951  tmp = sensors->getPassedVeh(lane, false);
952  inTarget += tmp;
953  if (inInit && tmp != 0) {
954  minIn = tmp;
955  inInit = false;
956  }
957  if (tmp < minIn && tmp != 0) {
958  minIn = tmp;
959  }
960  if (tmp != 0) {
961  toReset.push_back(lane);
962  }
963  DBG(
964  std::ostringstream cars_str; cars_str << "Lane " << lane << " passed: " << tmp; WRITE_MESSAGE(time2string(MSNet::getInstance()->getCurrentTimeStep()) + " MSSwarmTrafficLightLogic::calculateEta::" + cars_str.str());)
965  }
966 
967  // The cars not on a target lane counted as in.
969 
970  // Calculate the min beetween the min number of cars entered the tl (minIn) and the
971  // ones that have exit the tl (minOut)
972  toSub = std::min(minIn, minOut);
973 
974  // Subtract the value to all the sensor on the target lanes.
975  while (!toReset.empty()) {
976  std::string laneId = toReset.back();
977  toReset.pop_back();
978  sensors->subtractPassedVeh(laneId, toSub);
979  }
980 
981  //Normalized to 1
982  if (carsOut != 0) {
983  ratio = ((double) inTarget) / carsOut;
984  normalized = ratio / (inTarget + carsOut);
985  } else {
986  ratio = std::numeric_limits<double>::infinity();
987  normalized = std::numeric_limits<double>::infinity();
988  }
989 
990  DBG(
991  std::ostringstream final_str; final_str << "Total cars in lanes: " << carsIn << " Total cars out: " << carsOut << " Ratio: " << ratio << " Pure eta: " << normalized; WRITE_MESSAGE(time2string(MSNet::getInstance()->getCurrentTimeStep()) + " MSSwarmTrafficLightLogic::calculateEta::" + final_str.str());)
992  DBG(
993  std::ostringstream eta_str; eta_str << "IN:" << inTarget << ". OUT:" << carsOut << " R:" << notTarget; WRITE_MESSAGE(time2string(MSNet::getInstance()->getCurrentTimeStep()) + " MSSwarmTrafficLightLogic::calculateEta::" + eta_str.str());)
994  DBG(
995  std::ostringstream eta_str; eta_str << "Min found:" << toSub << ". MinIn:" << minIn << " MinOut:" << minOut; WRITE_MESSAGE(time2string(MSNet::getInstance()->getCurrentTimeStep()) + " MSSwarmTrafficLightLogic::calculateEta::" + eta_str.str());)
996  // Analize ratio to return an appropriate eta to reinforce/forget the policies.
997 
998  // IN > OUT
999  if (inTarget > carsOut) {
1000  if (carsOut == 0) {
1001  // we're in Congestion but not for long so we don't do nothing. When we reach max steps for
1002  // Congestion the evaluation of eta is skipped and we force a forget of the policy
1003  if (getCurrentPolicy()->getName().compare("Congestion") == 0) {
1004  eta = 0;
1005  }
1006  // vehicles aren't going out and we've additional vehicle on a red lane. We set
1007  // eta to -1 to forget
1008  else {
1009  eta = -1;
1010  }
1011  } else {
1012  // Forget according to the ratio. Amplify due to the cars in the red lanes
1013  phi = calculatePhi(notTarget);
1014  eta = (-(normalized) * (1 / phi));
1015  if (eta < -1.0) {
1016  eta = -1.0;
1017  }
1018  }
1019  }
1020  // IN = OUT
1021  else if (inTarget == carsOut) {
1022  // We can't say nothing.
1023  if (inTarget == 0) {
1024  eta = 0;
1025  }
1026  // Reinforce - Attenuate to R
1027  // same number of vehicles that are getting IN is getting OUT
1028  // Normalized = 1/TOT ---> change to delta = 1-1/IN
1029  else {
1030  delta = calculatePhi(inTarget);
1031  phi = calculatePhi(notTarget);
1032  eta = delta * phi;
1033  if (eta > 1.0) {
1034  eta = 1.0;
1035  }
1036  }
1037  }
1038  // IN < OUT
1039  else {
1040  // We can't say nothing.
1041  if (inTarget == 0) {
1042  eta = 0;
1043  }
1044 
1045  // There was a queue and now cars are getting over it
1046  // There're vehicles on the red lanes (R)
1047  // We reinforce and attenuate according to R
1048  else {
1049  phi = calculatePhi(notTarget);
1050  eta = (normalized) * phi;
1051  if (eta > 1.0) {
1052  eta = 1.0;
1053  }
1054  }
1055  }
1056 
1057  DBG(
1058  std::ostringstream eta_str; eta_str << "Eta Normalized: " << eta << "."; WRITE_MESSAGE(time2string(MSNet::getInstance()->getCurrentTimeStep()) + " MSSwarmTrafficLightLogic::calculateEta::" + eta_str.str());)
1059  return eta;
1060 
1061 }
1062 
1064 
1065  MSLane* currentLane = nullptr;
1066 
1067  // reset both the input and the output lanes.
1068  for (MSTrafficLightLogic::LaneVectorVector::const_iterator laneVector = myLanes.begin();
1069  laneVector != myLanes.end(); laneVector++) {
1070 
1071  for (MSTrafficLightLogic::LaneVector::const_iterator lane = laneVector->begin(); lane != laneVector->end();
1072  lane++) {
1073  currentLane = (*lane);
1074  laneCheck[currentLane] = false;
1075  }
1076  }
1077 
1078  for (MSTrafficLightLogic::LinkVectorVector::const_iterator linkVector = myLinks.begin();
1079  linkVector != myLinks.end(); linkVector++) {
1080  for (MSTrafficLightLogic::LinkVector::const_iterator link = linkVector->begin(); link != linkVector->end();
1081  link++) {
1082  currentLane = (*link)->getLane();
1083  laneCheck[currentLane] = false;
1084  }
1085  }
1086 }
1087 
1088 void MSSwarmTrafficLightLogic::choosePolicy(double phero_in, double phero_out, double dispersion_in,
1089  double dispersion_out) {
1091  for (std::vector<MSSOTLPolicy*>::iterator it = getPolicies().begin(); it != getPolicies().end(); ++it) {
1092  if (it.operator * ()->getName() == "Phase") {
1093  activate(*it);
1094  return;
1095  }
1096  }
1097  }
1098  std::vector<double> thetaStimuli;
1099  double thetaSum = 0.0;
1100  // Compute stimulus for each policy
1101  for (int i = 0; i < (int)getPolicies().size(); i++) {
1102  double stimulus = getPolicies()[i]->computeDesirability(phero_in, phero_out, dispersion_in, dispersion_out);
1103  double thetaStimulus = pow(stimulus, 2) / (pow(stimulus, 2) + pow(getPolicies()[i]->getThetaSensitivity(), 2));
1104 
1105  thetaStimuli.push_back(thetaStimulus);
1106  thetaSum += thetaStimulus;
1107 
1108 // ANALYSIS_DBG(
1109  DBG(
1110  ostringstream so_str; so_str << " policy " << getPolicies()[i]->getName() << " stimulus " << stimulus << " pow(stimulus,2) " << pow(stimulus, 2) << " pow(Threshold,2) " << pow(getPolicies()[i]->getThetaSensitivity(), 2) << " thetaStimulus " << thetaStimulus << " thetaSum " << thetaSum << " TL " << getID(); WRITE_MESSAGE("MSSwarmTrafficLightLogic::choosePolicy::" + so_str.str());)
1111 
1112  }
1113 
1114  // Compute a random value between 0 and the sum of the thetaSum
1115 // double r = RandHelper::rand(RAND_MAX);
1116 // r = r / RAND_MAX * thetaSum;
1117  double r = RandHelper::rand((double)thetaSum);
1118 
1119  double partialSum = 0;
1120  for (int i = 0; i < (int)getPolicies().size(); i++) {
1121  partialSum += thetaStimuli[i];
1122 
1123 // ANALYSIS_DBG(
1124  DBG(
1125  ostringstream aao_str; aao_str << " policy " << getPolicies()[i]->getName() << " partialSum " << partialSum << " thetaStimuls " << thetaStimuli[i] << " r " << r << " TL " << getID(); WRITE_MESSAGE("MSSwarmTrafficLightLogic::choosePolicy::" + aao_str.str());)
1126 
1127  if (partialSum >= r) {
1128  activate(getPolicies()[i]);
1129  break;
1130  }
1131  }
1132 }
1133 
1134 void MSSwarmTrafficLightLogic::choosePolicy(double phero_in, double phero_out) {
1135  choosePolicy(phero_in, phero_out, 0, 0);
1136 }
1137 
1138 //never called...
1140  DBG(
1141  std::ostringstream phero_str; phero_str << "getCurrentPhaseElapsed()=" << time2string(getCurrentPhaseElapsed()) << " isThresholdPassed()=" << isThresholdPassed() << " currentPhase=" << (&getCurrentPhaseDef())->getState() << " countVehicles()=" << countVehicles(getCurrentPhaseDef()); WRITE_MESSAGE("MSSwamTrafficLightLogic::canRelease(): " + phero_str.str());)
1144 }
1145 
1146 std::string MSSwarmTrafficLightLogic::getLaneLightState(const std::string& laneId) {
1147  std::string laneState = "";
1148  if (m_laneIndexMap.find(laneId) != m_laneIndexMap.end()) {
1149  std::string state = getCurrentPhaseDef().getState();
1150  for (std::vector<int>::const_iterator it = m_laneIndexMap[laneId].begin(); it != m_laneIndexMap[laneId].end(); ++it) {
1151  laneState += state[*it];
1152  }
1153  }
1154  return laneState;
1155 }
MSSOTLPolicy::decideNextPhase
virtual int decideNextPhase(SUMOTime elapsed, const MSPhaseDefinition *stage, int currentPhaseIndex, int phaseMaxCTS, bool thresholdPassed, bool pushButtonPressed, int vehicleCount)
Definition: MSSOTLPolicy.cpp:121
CircularBuffer
Definition: MSSwarmTrafficLightLogic.h:37
SVC_PEDESTRIAN
pedestrian
Definition: SUMOVehicleClass.h:157
TLTYPE_SWARM_BASED
Definition: SUMOXMLDefinitions.h:1203
MSTrafficLightLogic::getLinksAt
const LinkVector & getLinksAt(int i) const
Returns the list of links that are controlled by the signals at the given position.
Definition: MSTrafficLightLogic.h:210
MSTrafficLightLogic::myLinks
LinkVectorVector myLinks
The list of LinkVectors; each vector contains the links that belong to the same link index.
Definition: MSTrafficLightLogic.h:415
MSTrafficLightLogic::myLanes
LaneVectorVector myLanes
The list of LaneVectors; each vector contains the incoming lanes that belong to the same link index.
Definition: MSTrafficLightLogic.h:418
MIN2
T MIN2(T a, T b)
Definition: StdDefs.h:74
MSSOTLPolicy
Class for a low-level policy.
Definition: MSSOTLPolicy.h:65
MSSOTLPolicy::getThetaSensitivity
virtual double getThetaSensitivity()
Definition: MSSOTLPolicy.h:112
MSSOTLTrafficLightLogic::getCountSensors
MSSOTLE2Sensors * getCountSensors()
Return the sensors that count the passage of vehicles in and out of the tl.
Definition: MSSOTLTrafficLightLogic.h:183
MSSwarmTrafficLightLogic::getScaleFactorDispersionIn
double getScaleFactorDispersionIn()
Definition: MSSwarmTrafficLightLogic.h:177
MSLane
Representation of a lane in the micro simulation.
Definition: MSLane.h:83
MSSOTLMarchingPolicy
Class for low-level marching policy.
Definition: MSSOTLMarchingPolicy.h:34
MSSwarmTrafficLightLogic::calculateEtaRatio
double calculateEtaRatio()
Definition: MSSwarmTrafficLightLogic.cpp:870
MSSwarmTrafficLightLogic::getGammaNo
double getGammaNo()
Definition: MSSwarmTrafficLightLogic.h:141
MSSwarmTrafficLightLogic::skipEta
bool skipEta
When true indicates that we can skip the evaluation of eta since we've a congestion policy that is la...
Definition: MSSwarmTrafficLightLogic.h:416
MSSOTLTrafficLightLogic::isThresholdPassed
bool isThresholdPassed()
Definition: MSSOTLTrafficLightLogic.cpp:306
MSSwarmTrafficLightLogic::carsOut
int carsOut
Definition: MSSwarmTrafficLightLogic.h:424
MSSwarmTrafficLightLogic::initScaleFactorDispersionOut
void initScaleFactorDispersionOut(int lanes_out)
Definition: MSSwarmTrafficLightLogic.h:361
MSSwarmTrafficLightLogic::getBetaNo
double getBetaNo()
Definition: MSSwarmTrafficLightLogic.h:137
MSSwarmTrafficLightLogic::carsIn
int carsIn
Definition: MSSwarmTrafficLightLogic.h:423
MSSOTLE2Sensors::getPassedVeh
int getPassedVeh(std::string laneId, bool out)
Definition: MSSOTLE2Sensors.cpp:208
StringUtils::toDouble
static double toDouble(const std::string &sData)
converts a string into the double value described by it by calling the char-type converter
Definition: StringUtils.cpp:313
MSSOTLPolicy5DFamilyStimulus
Definition: MSSOTLPolicy5DFamilyStimulus.h:43
DBG
#define DBG(X)
Definition: SwarmDebug.h:27
MSPhasedTrafficLightLogic::getCurrentPhaseDef
const MSPhaseDefinition & getCurrentPhaseDef() const
Returns the definition of the current phase.
Definition: MSPhasedTrafficLightLogic.cpp:132
MSTrafficLightLogic::Phases
std::vector< MSPhaseDefinition * > Phases
Definition of a list of phases, being the junction logic.
Definition: MSTrafficLightLogic.h:62
LaneIdVector
std::vector< std::string > LaneIdVector
Definition: MSSOTLDefinitions.h:77
MSSwarmTrafficLightLogic::targetLanes
LaneIdVector targetLanes
A copy of the target lanes of this phase.
Definition: MSSwarmTrafficLightLogic.h:411
MSSwarmTrafficLightLogic::~MSSwarmTrafficLightLogic
~MSSwarmTrafficLightLogic()
Definition: MSSwarmTrafficLightLogic.cpp:75
MSSwarmTrafficLightLogic::m_losMaxLimit
int m_losMaxLimit
Definition: MSSwarmTrafficLightLogic.h:444
MSLane::getPermissions
SVCPermissions getPermissions() const
Returns the vehicle class permissions for this lane.
Definition: MSLane.h:549
MsgHandler::inform
virtual void inform(std::string msg, bool addType=true)
adds a new error to the list
Definition: MsgHandler.cpp:118
SUMOTime
long long int SUMOTime
Definition: SUMOTime.h:35
MSSOTLE2Sensors::subtractPassedVeh
void subtractPassedVeh(std::string laneId, int passed)
Definition: MSSOTLE2Sensors.cpp:228
MSSwarmTrafficLightLogic::decideNextPhase
int decideNextPhase()
Definition: MSSwarmTrafficLightLogic.cpp:216
SVC_BICYCLE
vehicle is a bicycle
Definition: SUMOVehicleClass.h:180
MSSwarmTrafficLightLogic::init
void init(NLDetectorBuilder &nb)
Initialises the tls with sensors on incoming and outgoing lanes Sensors are built in the simulation a...
Definition: MSSwarmTrafficLightLogic.cpp:115
MSSOTLPolicyDesirability::getMessage
virtual std::string getMessage()=0
MSSwarmTrafficLightLogic::swarmLogFile
std::ofstream swarmLogFile
Definition: MSSwarmTrafficLightLogic.h:396
MSSOTLHiLevelTrafficLightLogic
A self-organizing high-level traffic light logic.
Definition: MSSOTLHiLevelTrafficLightLogic.h:45
MSSwarmTrafficLightLogic::allowLine
bool allowLine(MSLane *)
Check if a lane is allowed to be added to the maps pheromoneInputLanes and pheromoneOutputLanes Contr...
Definition: MSSwarmTrafficLightLogic.cpp:91
MSSOTLSensors::countVehicles
virtual int countVehicles(MSLane *lane)=0
MSSOTLTrafficLightLogic::getSensors
MSSOTLSensors * getSensors()
Definition: MSSOTLTrafficLightLogic.h:176
MSSwarmTrafficLightLogic::getBetaSp
double getBetaSp()
Definition: MSSwarmTrafficLightLogic.h:145
MSSwarmTrafficLightLogic::laneCheck
LaneCheckMap laneCheck
Map to check if a lane was already controlled during the elaboration of eta.
Definition: MSSwarmTrafficLightLogic.h:407
MSSwarmTrafficLightLogic::m_pheroLevelLog
std::map< std::string, std::string > m_pheroLevelLog
Definition: MSSwarmTrafficLightLogic.h:437
MSSwarmTrafficLightLogic::mustChange
bool mustChange
When true, indicates that the current policy MUST be changed. It's used to force the exit from the co...
Definition: MSSwarmTrafficLightLogic.h:401
MSSOTLTrafficLightLogic::isPushButtonPressed
bool isPushButtonPressed()
Definition: MSSOTLTrafficLightLogic.cpp:507
MSSwarmTrafficLightLogic::getLearningCox
double getLearningCox()
Definition: MSSwarmTrafficLightLogic.h:169
Parameterised::getParameter
const std::string getParameter(const std::string &key, const std::string &defaultValue="") const
Returns the value for a given key.
Definition: Parameterised.cpp:71
MSSOTLSensors::meanVehiclesSpeed
virtual double meanVehiclesSpeed(MSLane *lane)=0
MSSwarmTrafficLightLogic::getGammaSp
double getGammaSp()
Definition: MSSwarmTrafficLightLogic.h:149
MSSwarmTrafficLightLogic::initScaleFactorDispersionIn
void initScaleFactorDispersionIn(int lanes_in)
Definition: MSSwarmTrafficLightLogic.h:333
MSSOTLPolicy::getDesirabilityAlgorithm
MSSOTLPolicyDesirability * getDesirabilityAlgorithm()
Definition: MSSOTLPolicy.h:121
MSSwarmTrafficLightLogic::getThetaMax
double getThetaMax()
Definition: MSSwarmTrafficLightLogic.h:157
MAX2
T MAX2(T a, T b)
Definition: StdDefs.h:80
MSSOTLCongestionPolicy
Class for low-level congestion policy.
Definition: MSSOTLCongestionPolicy.h:34
RandHelper::rand
static double rand(std::mt19937 *rng=0)
Returns a random real number in [0, 1)
Definition: RandHelper.h:60
MSEdge::isCrossing
bool isCrossing() const
return whether this edge is a pedestrian crossing
Definition: MSEdge.h:238
MSSwarmTrafficLightLogic::notTarget
int notTarget
Definition: MSSwarmTrafficLightLogic.h:426
MSSOTLPolicy::getName
std::string getName()
Definition: MSSOTLPolicy.h:118
MSSwarmTrafficLightLogic::getThetaMin
double getThetaMin()
Definition: MSSwarmTrafficLightLogic.h:161
MSSwarmTrafficLightLogic::m_laneIndexMap
std::map< std::string, std::vector< int > > m_laneIndexMap
Definition: MSSwarmTrafficLightLogic.h:434
MSSwarmTrafficLightLogic::getLaneLightState
std::string getLaneLightState(const std::string &laneId)
Definition: MSSwarmTrafficLightLogic.cpp:1146
MSSwarmTrafficLightLogic::resetLaneCheck
void resetLaneCheck()
Definition: MSSwarmTrafficLightLogic.cpp:1063
MSSwarmTrafficLightLogic::m_losCounter
int m_losCounter
Definition: MSSwarmTrafficLightLogic.h:443
MSLaneId_Pheromone
std::pair< std::string, double > MSLaneId_Pheromone
Definition: MSSOTLDefinitions.h:65
MSPhaseDefinition::getState
const std::string & getState() const
Returns the state within this phase.
Definition: MSPhaseDefinition.h:200
MSSwarmTrafficLightLogic::inTarget
int inTarget
Definition: MSSwarmTrafficLightLogic.h:425
MSNet::getCurrentTimeStep
SUMOTime getCurrentTimeStep() const
Returns the current simulation step.
Definition: MSNet.h:284
STEPS2TIME
#define STEPS2TIME(x)
Definition: SUMOTime.h:57
MSSwarmTrafficLightLogic::getThetaInit
double getThetaInit()
Definition: MSSwarmTrafficLightLogic.h:165
MSPhaseDefinition::isCommit
bool isCommit() const
Definition: MSPhaseDefinition.h:293
time2string
std::string time2string(SUMOTime t)
Definition: SUMOTime.cpp:65
MSPhaseDefinition::getTargetLaneSet
const LaneIdVector & getTargetLaneSet() const
Definition: MSPhaseDefinition.h:208
MSSwarmTrafficLightLogic::getReinforcementMode
int getReinforcementMode()
Definition: MSSwarmTrafficLightLogic.h:329
MSSOTLTrafficLightLogic::getPhaseIndexWithMaxCTS
int getPhaseIndexWithMaxCTS()
Definition: MSSOTLTrafficLightLogic.cpp:370
MSSwarmTrafficLightLogic::logData
bool logData
Definition: MSSwarmTrafficLightLogic.h:395
MSSwarmTrafficLightLogic::resetPheromone
void resetPheromone()
Resets pheromone levels.
Definition: MSSwarmTrafficLightLogic.cpp:201
MSSOTLHiLevelTrafficLightLogic::getCurrentPolicy
MSSOTLPolicy * getCurrentPolicy()
Returns the low-level policy currently selected by this high-level tll.
Definition: MSSOTLHiLevelTrafficLightLogic.h:91
MSPhasedTrafficLightLogic::getCurrentPhaseIndex
int getCurrentPhaseIndex() const
Returns the current index within the program.
Definition: MSPhasedTrafficLightLogic.cpp:126
MSSwarmTrafficLightLogic.h
MSLane::getEdge
MSEdge & getEdge() const
Returns the lane's edge.
Definition: MSLane.h:670
MSEdge::isWalkingArea
bool isWalkingArea() const
return whether this edge is walking area
Definition: MSEdge.h:252
MSSOTLSensors::getMaxSpeed
virtual double getMaxSpeed(std::string laneId)=0
MSSwarmTrafficLightLogic::getPoliciesParam
std::string getPoliciesParam()
Definition: MSSwarmTrafficLightLogic.h:319
MSSwarmTrafficLightLogic::getDispersionForOutputLanes
double getDispersionForOutputLanes(double average_phero_out)
Definition: MSSwarmTrafficLightLogic.cpp:549
MSSwarmTrafficLightLogic::pheromoneOutputLanes
MSLaneId_PheromoneMap pheromoneOutputLanes
This pheromone is an indicator of congestion on output lanes. Its levels refer to the average speed o...
Definition: MSSwarmTrafficLightLogic.h:208
MSTrafficLightLogic::getLinks
const LinkVectorVector & getLinks() const
Returns the list of lists of all affected links.
Definition: MSTrafficLightLogic.h:201
MSSwarmTrafficLightLogic::gotTargetLane
bool gotTargetLane
When true indicates that we've already acquired the target lanes for this particular phase.
Definition: MSSwarmTrafficLightLogic.h:421
StringUtils::toInt
static int toInt(const std::string &sData)
converts a string into the integer value described by it by calling the char-type converter,...
Definition: StringUtils.cpp:246
MSSwarmTrafficLightLogic::m_useVehicleTypesWeights
bool m_useVehicleTypesWeights
Definition: MSSwarmTrafficLightLogic.h:445
MSSOTLPhasePolicy
Class for low-level phase policy.
Definition: MSSOTLPhasePolicy.h:34
MSNet::getInstance
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:168
MSSwarmTrafficLightLogic::pheromoneInputLanes
MSLaneId_PheromoneMap pheromoneInputLanes
This pheronome is an indicator of congestion on input lanes. Its levels refer to the average speed of...
Definition: MSSwarmTrafficLightLogic.h:200
MSSOTLE2Sensors
Definition: MSSOTLE2Sensors.h:33
MSSwarmTrafficLightLogic::MSSwarmTrafficLightLogic
MSSwarmTrafficLightLogic(MSTLLogicControl &tlcontrol, const std::string &id, const std::string &programID, const Phases &phases, int step, SUMOTime delay, const std::map< std::string, std::string > &parameters)
Constructor without sensors passed.
Definition: MSSwarmTrafficLightLogic.cpp:28
MSSwarmTrafficLightLogic::updatePheromoneLevels
void updatePheromoneLevels()
Update pheromone levels Pheromone on input lanes is costantly updated Pheromone follows a discrete-ti...
Definition: MSSwarmTrafficLightLogic.cpp:285
MSSOTLTrafficLightLogic::countVehicles
int countVehicles(MSPhaseDefinition phase)
Definition: MSSOTLTrafficLightLogic.cpp:265
MSSOTLPolicy::setThetaSensitivity
virtual void setThetaSensitivity(double val)
Definition: MSSOTLPolicy.h:115
MSLaneId_PheromoneMap
std::map< std::string, double > MSLaneId_PheromoneMap
Definition: MSSOTLDefinitions.h:70
MSSOTLHiLevelTrafficLightLogic::currentPolicy
MSSOTLPolicy * currentPolicy
Definition: MSSOTLHiLevelTrafficLightLogic.h:119
MSSwarmTrafficLightLogic::getDistanceOfMaxPheroForInputLanes
double getDistanceOfMaxPheroForInputLanes()
Definition: MSSwarmTrafficLightLogic.cpp:564
MSSOTLHiLevelTrafficLightLogic::getPolicies
std::vector< MSSOTLPolicy * > & getPolicies()
Returns the vector of the low-level policies used by this high-level tll.
Definition: MSSOTLHiLevelTrafficLightLogic.h:85
MSSOTLHiLevelTrafficLightLogic::addPolicy
void addPolicy(MSSOTLPolicy *policy)
Definition: MSSOTLHiLevelTrafficLightLogic.cpp:45
MSSwarmTrafficLightLogic::getMaxCongestionDuration
SUMOTime getMaxCongestionDuration()
Definition: MSSwarmTrafficLightLogic.h:129
MSSwarmTrafficLightLogic::updateSensitivities
void updateSensitivities()
Definition: MSSwarmTrafficLightLogic.cpp:413
MSSwarmTrafficLightLogic::getChangePlanProbability
double getChangePlanProbability()
Definition: MSSwarmTrafficLightLogic.h:153
MSSwarmTrafficLightLogic::getForgettingCox
double getForgettingCox()
Definition: MSSwarmTrafficLightLogic.h:173
MSSwarmTrafficLightLogic::congestion_steps
SUMOTime congestion_steps
Definition: MSSwarmTrafficLightLogic.h:402
MSSwarmTrafficLightLogic::m_derivativeAlpha
double m_derivativeAlpha
Definition: MSSwarmTrafficLightLogic.h:442
MSSwarmTrafficLightLogic::getDispersionForInputLanes
double getDispersionForInputLanes(double average_phero_in)
Definition: MSSwarmTrafficLightLogic.cpp:532
MSSwarmTrafficLightLogic::calculatePhi
double calculatePhi(int factor)
Method that should calculate the valor of phi a coefficient to amplify/attenuate eta based on a facto...
Definition: MSSwarmTrafficLightLogic.cpp:669
MSSOTLPolicy::canRelease
virtual bool canRelease(SUMOTime elapsed, bool thresholdPassed, bool pushButtonPressed, const MSPhaseDefinition *stage, int vehicleCount)=0
MSTLLogicControl
A class that stores and controls tls and switching of their programs.
Definition: MSTLLogicControl.h:60
MSSOTLPlatoonPolicy
Class for low-level platoon policy.
Definition: MSSOTLPlatoonPolicy.h:36
MSSwarmTrafficLightLogic::lastThetaSensitivityUpdate
SUMOTime lastThetaSensitivityUpdate
Definition: MSSwarmTrafficLightLogic.h:215
MSPhaseDefinition::isTarget
bool isTarget() const
Definition: MSPhaseDefinition.h:272
MSSwarmTrafficLightLogic::getPheromoneForInputLanes
double getPheromoneForInputLanes()
Definition: MSSwarmTrafficLightLogic.cpp:498
MSSOTLHiLevelTrafficLightLogic::init
void init(NLDetectorBuilder &nb)
Initialises the tls.
Definition: MSSOTLHiLevelTrafficLightLogic.cpp:49
MSSwarmTrafficLightLogic::canRelease
bool canRelease()
Definition: MSSwarmTrafficLightLogic.cpp:1139
MSSwarmTrafficLightLogic::getPheroMaxVal
double getPheroMaxVal()
Definition: MSSwarmTrafficLightLogic.h:133
MSSwarmTrafficLightLogic::choosePolicy
void choosePolicy(double phero_in, double phero_out, double dispersion_in, double dispersion_out)
Definition: MSSwarmTrafficLightLogic.cpp:1088
ANALYSIS_DBG
#define ANALYSIS_DBG(X)
Definition: MSSwarmTrafficLightLogic.cpp:23
MSTrafficLightLogic::LinkVectorVector
std::vector< LinkVector > LinkVectorVector
Definition of a list that holds lists of links that do have the same attribute.
Definition: MSTrafficLightLogic.h:68
MSSwarmTrafficLightLogic::calculateEtaDiff
double calculateEtaDiff()
Method that should calculate the valor of eta a coefficient to evaluate the current policy's work....
Definition: MSSwarmTrafficLightLogic.cpp:680
MSSOTLPolicyDesirability
This class determines the desirability algorithm of a MSSOTLPolicy when used in combination with a hi...
Definition: MSSOTLPolicyDesirability.h:36
MSSwarmTrafficLightLogic::getDistanceOfMaxPheroForOutputLanes
double getDistanceOfMaxPheroForOutputLanes()
Definition: MSSwarmTrafficLightLogic.cpp:599
MSSwarmTrafficLightLogic::decidePolicy
void decidePolicy()
Decide the current policy according to pheromone levels The decision reflects on currentPolicy value.
Definition: MSSwarmTrafficLightLogic.cpp:633
Named::getID
const std::string & getID() const
Returns the id.
Definition: Named.h:77
WRITE_ERROR
#define WRITE_ERROR(msg)
Definition: MsgHandler.h:245
MSTrafficLightLogic::LinkVector
std::vector< MSLink * > LinkVector
Definition of the list of links that are subjected to this tls.
Definition: MSTrafficLightLogic.h:65
WRITE_MESSAGE
#define WRITE_MESSAGE(msg)
Definition: MsgHandler.h:240
MSSwarmTrafficLightLogic::getPheromoneForOutputLanes
double getPheromoneForOutputLanes()
Definition: MSSwarmTrafficLightLogic.cpp:516
NLDetectorBuilder
Builds detectors for microsim.
Definition: NLDetectorBuilder.h:56
MSSOTLTrafficLightLogic::getCurrentPhaseElapsed
SUMOTime getCurrentPhaseElapsed()
Definition: MSSOTLTrafficLightLogic.cpp:359
MSSwarmTrafficLightLogic::getScaleFactorDispersionOut
double getScaleFactorDispersionOut()
Definition: MSSwarmTrafficLightLogic.h:181
MSSwarmTrafficLightLogic::m_meanSpeedHistory
std::map< std::string, CircularBuffer< double > * > m_meanSpeedHistory
Definition: MSSwarmTrafficLightLogic.h:440
MSSOTLHiLevelTrafficLightLogic::activate
void activate(MSSOTLPolicy *policy)
Definition: MSSOTLHiLevelTrafficLightLogic.cpp:53
MSSwarmTrafficLightLogic::m_derivativeHistory
std::map< std::string, CircularBuffer< double > * > m_derivativeHistory
Definition: MSSwarmTrafficLightLogic.h:441
MsgHandler::getMessageInstance
static MsgHandler * getMessageInstance()
Returns the instance to add normal messages to.
Definition: MsgHandler.cpp:59
MSSOTLHiLevelTrafficLightLogic::policies
std::vector< MSSOTLPolicy * > policies
Definition: MSSOTLHiLevelTrafficLightLogic.h:118