19 #include "../MSEdge.h"
22 #define ANALYSIS_DBG(X) {X}
24 #define ANALYSIS_DBG(X) DBG(X)
28 const std::string& programID,
const Phases& phases,
int step,
SUMOTime delay,
29 const std::map<std::string, std::string>& parameters) :
33 std::transform(pols.begin(), pols.end(), pols.begin(), ::tolower);
34 DBG(std::ostringstream str; str <<
"policies: " << pols;
WRITE_MESSAGE(str.str());)
36 if (pols.find(
"platoon") != std::string::npos) {
39 if (pols.find(
"phase") != std::string::npos) {
42 if (pols.find(
"marching") != std::string::npos) {
45 if (pols.find(
"congestion") != std::string::npos) {
63 std::ostringstream _str;
70 WRITE_ERROR(
"VEHICLE TYPES WEIGHT only works with phase policy, which is missing");
119 srand((
int) time(
nullptr));
122 MSLane* currentLane =
nullptr;
132 for (MSTrafficLightLogic::LaneVectorVector::const_iterator laneVector =
myLanes.begin();
133 laneVector !=
myLanes.end(); laneVector++) {
134 for (MSTrafficLightLogic::LaneVector::const_iterator lane = laneVector->begin(); lane != laneVector->end();
136 currentLane = (*lane);
145 WRITE_MESSAGE(
"MSSwarmTrafficLightLogic::init Intersection " +
getID() +
" pheromoneInputLanes adding " + currentLane->
getID());)
148 WRITE_MESSAGE(
"MSSwarmTrafficLightLogic::init Intersection " +
getID() +
" pheromoneInputLanes: lane " + currentLane->
getID() +
" not allowed");)
156 for (
int i = 0; i < (int)
myLinks.size(); i++) {
158 for (
int j = 0; j < (int)oneLink.size(); j++) {
159 currentLane = oneLink[j]->getLane();
165 WRITE_MESSAGE(
"MSSwarmTrafficLightLogic::init Intersection " +
getID() +
" pheromoneOutputLanes adding " + currentLane->
getID());)
168 WRITE_MESSAGE(
"MSSwarmTrafficLightLogic::init Intersection " +
getID() +
" pheromoneOutputLanes lane " + currentLane->
getID() +
" not allowed");)
179 WRITE_MESSAGE(
"*** Intersection " +
getID() +
" will run using MSSwarmTrafficLightLogic ***");
181 logData = logFileName.compare(
"") != 0;
183 swarmLogFile.open(logFileName.c_str(), std::ios::out | std::ios::binary);
204 std::string laneId = laneIterator->first;
210 std::string laneId = laneIterator->first;
297 const double beta,
const double gamma) {
300 std::ostringstream _str; _str << logString <<
" Lanes " << pheroMap.size() <<
" TL " <<
getID() <<
" .";
WRITE_MESSAGE(
time2string(
MSNet::getInstance()->getCurrentTimeStep()) +
" MSSwarmTrafficLightLogic::updatePheromoneLevels:: " + _str.str());)
302 for (MSLaneId_PheromoneMap::iterator laneIterator = pheroMap.begin(); laneIterator != pheroMap.end();
304 std::string laneId = laneIterator->first;
305 double oldPhero = laneIterator->second;
308 bool updatePheromone = (meanVehiclesSpeed > -1);
312 double derivative = 0;
316 if (updatePheromone) {
317 double currentDerivative = 0;
351 double pheroAdd =
MAX2((maxSpeed - meanVehiclesSpeed) * 10 / maxSpeed, 0.0);
360 if (updatePheromone) {
361 std::ostringstream oss;
363 oss <<
" der " << derivative <<
" phero " << pheroAdd <<
" maxS " << maxSpeed <<
" meanS " << meanVehiclesSpeed;
369 double phero = beta * oldPhero + gamma * pheroAdd * updatePheromone;
372 std::ostringstream i_str;
373 i_str <<
"MSSwarmTrafficLightLogic::updatePheromoneLevels " << logString <<
" > 10. Value: " << phero;
378 pheroMap[laneId] = phero;
381 std::ostringstream i_str;
389 i_str <<
" op " << oldPhero <<
" ms " << meanVehiclesSpeed <<
" p " << pheroAdd * updatePheromone <<
390 " pe " << oldPhero - oldPhero * beta <<
" pd " << gamma * pheroAdd * updatePheromone <<
" np " <<
421 for (
int i = 0; i < (int)
policies.size(); i++) {
425 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());)
448 for (
int i = 0; i < (int)
policies.size(); i++) {
450 double newSensitivity;
477 phero_str <<
" policy " << policy->
getName() <<
" newSensitivity " << newSensitivity <<
" ,pol.Sensitivity " << policy->
getThetaSensitivity() <<
" ,elapsedTime " << elapsedTime << lf.str() <<
" NEWERSensitivity= " << max(min(newSensitivity,
getThetaMax()),
getThetaMin()) <<
" ID " <<
getID() <<
" .";
483 }
else if (eta > 0) {
485 }
else if (eta < 0) {
504 std::string laneId = iterator->first;
505 pheroIn += iterator->second;
507 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());)
523 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());)
524 pheroOut += iterator->second;
538 std::string laneId = iterator->first;
539 sum += pow(iterator->second - average_phero_in, 2);
544 ostringstream so_str; so_str <<
" dispersionIn " << result;
WRITE_MESSAGE(
"MSSwarmTrafficLightLogic::getDispersionForInputLanes::" + so_str.str());)
555 sum += pow(iterator->second - average_phero_out, 2);
560 ostringstream so_str; so_str <<
" dispersionOut " << result;
WRITE_MESSAGE(
"MSSwarmTrafficLightLogic::getDispersionForOutputLanes::" + so_str.str());)
567 double max_phero_val_current = 0;
568 double max_phero_val_old = 0;
569 double temp_avg_other_lanes = 0;
570 std::string laneId_max;
574 std::string laneId = iterator->first;
575 double lanePhero = iterator->second;
577 max_phero_val_current = lanePhero;
581 if (lanePhero > max_phero_val_current) {
582 max_phero_val_old = max_phero_val_current;
583 max_phero_val_current = lanePhero;
584 temp_avg_other_lanes = (temp_avg_other_lanes * (counter - 1) + max_phero_val_old) / counter;
586 temp_avg_other_lanes = (temp_avg_other_lanes * (counter - 1) + lanePhero) / counter;
592 double result = max_phero_val_current - temp_avg_other_lanes;
594 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());)
602 double max_phero_val_current = 0;
603 double max_phero_val_old = 0;
604 double temp_avg_other_lanes = 0;
605 std::string laneId_max;
609 std::string laneId = iterator->first;
610 double lanePhero = iterator->second;
612 max_phero_val_current = lanePhero;
616 if (lanePhero > max_phero_val_current) {
617 max_phero_val_old = max_phero_val_current;
618 max_phero_val_current = lanePhero;
619 temp_avg_other_lanes = (temp_avg_other_lanes * (counter - 1) + max_phero_val_old) / counter;
621 temp_avg_other_lanes = (temp_avg_other_lanes * (counter - 1) + lanePhero) / counter;
627 double result = max_phero_val_current - temp_avg_other_lanes;
629 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());)
649 choosePolicy(pheroIn, pheroOut, distancePheroIn, distancePheroOut);
652 if (newPolicy != oldPolicy) {
655 if (oldPolicy->
getName().compare(
"Congestion") == 0) {
675 return 1 - (1 / ((double) factor));
681 MSLane* currentLane =
nullptr;
682 int count = 0, minIn = 0, minOut = 0, toSub, tmp;
683 bool inInit =
true, outInit =
true;
684 double eta, normalized, diff, phi, delta;
695 for (MSTrafficLightLogic::LaneVectorVector::const_iterator laneVector =
myLanes.begin();
696 laneVector !=
myLanes.end(); laneVector++) {
697 for (MSTrafficLightLogic::LaneVector::const_iterator lane = laneVector->begin(); lane != laneVector->end();
699 currentLane = (*lane);
707 std::ostringstream cars_str; cars_str <<
"Lane " << currentLane->
getID() <<
": vehicles entered - " << count;
WRITE_MESSAGE(
time2string(
MSNet::getInstance()->getCurrentTimeStep()) +
" MSSwarmTrafficLightLogic::calculateEta::" + cars_str.str());)
719 for (MSTrafficLightLogic::LinkVectorVector::const_iterator linkVector =
myLinks.begin();
720 linkVector !=
myLinks.end(); linkVector++) {
721 for (MSTrafficLightLogic::LinkVector::const_iterator link = linkVector->begin(); link != linkVector->end();
723 currentLane = (*link)->getLane();
731 std::ostringstream cars_str; cars_str <<
"Lane " << currentLane->
getID() <<
": vehicles gone out- " << count;
WRITE_MESSAGE(
time2string(
MSNet::getInstance()->getCurrentTimeStep()) +
" MSSwarmTrafficLightLogic::calculateEta::" + cars_str.str());)
741 toReset.push_back(currentLane->
getID());
745 }
else if (count <= minOut) {
759 std::string lane = (*laneId);
762 if (inInit && tmp != 0) {
766 if (tmp < minIn && tmp != 0) {
770 toReset.push_back(lane);
773 std::ostringstream cars_str; cars_str <<
"Lane " << lane <<
" passed: " << tmp;
WRITE_MESSAGE(
time2string(
MSNet::getInstance()->getCurrentTimeStep()) +
" MSSwarmTrafficLightLogic::calculateEta::" + cars_str.str());)
781 toSub = std::min(minIn, minOut);
784 while (!toReset.empty()) {
785 std::string laneId = toReset.back();
797 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());)
801 std::ostringstream eta_str; eta_str <<
"Min found:" << toSub <<
" MinIn:" << minIn <<
" MinOut:" << minOut;
WRITE_MESSAGE(
time2string(
MSNet::getInstance()->getCurrentTimeStep()) +
" MSSwarmTrafficLightLogic::calculateEta::" + eta_str.str());)
819 eta = (-normalized * (1 / phi));
857 eta = normalized * phi;
870 MSLane* currentLane =
nullptr;
871 int count = 0, minIn = 0, minOut = 0, toSub, tmp;
872 bool inInit =
true, outInit =
true;
873 double eta, ratio, phi, normalized, delta;
884 for (MSTrafficLightLogic::LaneVectorVector::const_iterator laneVector =
myLanes.begin();
885 laneVector !=
myLanes.end(); laneVector++) {
886 for (MSTrafficLightLogic::LaneVector::const_iterator lane = laneVector->begin(); lane != laneVector->end();
888 currentLane = (*lane);
896 std::ostringstream cars_str; cars_str <<
"Lane " << currentLane->
getID() <<
": vehicles entered - " << count;
WRITE_MESSAGE(
time2string(
MSNet::getInstance()->getCurrentTimeStep()) +
" MSSwarmTrafficLightLogic::calculateEta::" + cars_str.str());)
908 for (MSTrafficLightLogic::LinkVectorVector::const_iterator linkVector =
myLinks.begin();
909 linkVector !=
myLinks.end(); linkVector++) {
910 for (MSTrafficLightLogic::LinkVector::const_iterator link = linkVector->begin(); link != linkVector->end();
912 currentLane = (*link)->getLane();
920 std::ostringstream cars_str; cars_str <<
"Lane " << currentLane->
getID() <<
": vehicles gone out- " << count;
WRITE_MESSAGE(
time2string(
MSNet::getInstance()->getCurrentTimeStep()) +
" MSSwarmTrafficLightLogic::calculateEta::" + cars_str.str());)
930 toReset.push_back(currentLane->
getID());
934 }
else if (count <= minOut) {
949 std::string lane = (*laneId);
952 if (inInit && tmp != 0) {
956 if (tmp < minIn && tmp != 0) {
960 toReset.push_back(lane);
963 std::ostringstream cars_str; cars_str <<
"Lane " << lane <<
" passed: " << tmp;
WRITE_MESSAGE(
time2string(
MSNet::getInstance()->getCurrentTimeStep()) +
" MSSwarmTrafficLightLogic::calculateEta::" + cars_str.str());)
971 toSub = std::min(minIn, minOut);
974 while (!toReset.empty()) {
975 std::string laneId = toReset.back();
985 ratio = std::numeric_limits<double>::infinity();
986 normalized = std::numeric_limits<double>::infinity();
990 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());)
994 std::ostringstream eta_str; eta_str <<
"Min found:" << toSub <<
". MinIn:" << minIn <<
" MinOut:" << minOut;
WRITE_MESSAGE(
time2string(
MSNet::getInstance()->getCurrentTimeStep()) +
" MSSwarmTrafficLightLogic::calculateEta::" + eta_str.str());)
1013 eta = (-(normalized) * (1 / phi));
1049 eta = (normalized) * phi;
1057 std::ostringstream eta_str; eta_str <<
"Eta Normalized: " << eta <<
".";
WRITE_MESSAGE(
time2string(
MSNet::getInstance()->getCurrentTimeStep()) +
" MSSwarmTrafficLightLogic::calculateEta::" + eta_str.str());)
1064 MSLane* currentLane =
nullptr;
1067 for (MSTrafficLightLogic::LaneVectorVector::const_iterator laneVector =
myLanes.begin();
1068 laneVector !=
myLanes.end(); laneVector++) {
1070 for (MSTrafficLightLogic::LaneVector::const_iterator lane = laneVector->begin(); lane != laneVector->end();
1072 currentLane = (*lane);
1077 for (MSTrafficLightLogic::LinkVectorVector::const_iterator linkVector =
myLinks.begin();
1078 linkVector !=
myLinks.end(); linkVector++) {
1079 for (MSTrafficLightLogic::LinkVector::const_iterator link = linkVector->begin(); link != linkVector->end();
1081 currentLane = (*link)->getLane();
1088 double dispersion_out) {
1091 if (it.operator * ()->getName() ==
"Phase") {
1097 std::vector<double> thetaStimuli;
1098 double thetaSum = 0.0;
1100 for (
int i = 0; i < (int)
getPolicies().size(); i++) {
1101 double stimulus =
getPolicies()[i]->computeDesirability(phero_in, phero_out, dispersion_in, dispersion_out);
1102 double thetaStimulus = pow(stimulus, 2) / (pow(stimulus, 2) + pow(
getPolicies()[i]->getThetaSensitivity(), 2));
1104 thetaStimuli.push_back(thetaStimulus);
1105 thetaSum += thetaStimulus;
1109 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());)
1118 double partialSum = 0;
1119 for (
int i = 0; i < (int)
getPolicies().size(); i++) {
1120 partialSum += thetaStimuli[i];
1124 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 if (partialSum >= r) {
1146 std::string laneState =
"";
1150 laneState += state[*it];