20 #include "../MSEdge.h"
23 #define ANALYSIS_DBG(X) {X}
25 #define ANALYSIS_DBG(X) DBG(X)
29 const std::string& programID,
const Phases& phases,
int step,
SUMOTime delay,
30 const std::map<std::string, std::string>& parameters) :
34 std::transform(pols.begin(), pols.end(), pols.begin(), ::tolower);
35 DBG(std::ostringstream str; str <<
"policies: " << pols;
WRITE_MESSAGE(str.str());)
37 if (pols.find(
"platoon") != std::string::npos) {
40 if (pols.find(
"phase") != std::string::npos) {
43 if (pols.find(
"marching") != std::string::npos) {
46 if (pols.find(
"congestion") != std::string::npos) {
64 std::ostringstream _str;
71 WRITE_ERROR(
"VEHICLE TYPES WEIGHT only works with phase policy, which is missing");
120 srand((
int) time(
nullptr));
123 MSLane* currentLane =
nullptr;
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();
137 currentLane = (*lane);
146 WRITE_MESSAGE(
"MSSwarmTrafficLightLogic::init Intersection " +
getID() +
" pheromoneInputLanes adding " + currentLane->
getID());)
149 WRITE_MESSAGE(
"MSSwarmTrafficLightLogic::init Intersection " +
getID() +
" pheromoneInputLanes: lane " + currentLane->
getID() +
" not allowed");)
157 for (
int i = 0; i < (int)
myLinks.size(); i++) {
159 for (
int j = 0; j < (int)oneLink.size(); j++) {
160 currentLane = oneLink[j]->getLane();
166 WRITE_MESSAGE(
"MSSwarmTrafficLightLogic::init Intersection " +
getID() +
" pheromoneOutputLanes adding " + currentLane->
getID());)
169 WRITE_MESSAGE(
"MSSwarmTrafficLightLogic::init Intersection " +
getID() +
" pheromoneOutputLanes lane " + currentLane->
getID() +
" not allowed");)
180 WRITE_MESSAGE(
"*** Intersection " +
getID() +
" will run using MSSwarmTrafficLightLogic ***");
182 logData = logFileName.compare(
"") != 0;
184 swarmLogFile.open(logFileName.c_str(), std::ios::out | std::ios::binary);
205 std::string laneId = laneIterator->first;
211 std::string laneId = laneIterator->first;
298 const double beta,
const double gamma) {
301 std::ostringstream _str; _str << logString <<
" Lanes " << pheroMap.size() <<
" TL " <<
getID() <<
" .";
WRITE_MESSAGE(
time2string(
MSNet::getInstance()->getCurrentTimeStep()) +
" MSSwarmTrafficLightLogic::updatePheromoneLevels:: " + _str.str());)
303 for (MSLaneId_PheromoneMap::iterator laneIterator = pheroMap.begin(); laneIterator != pheroMap.end();
305 std::string laneId = laneIterator->first;
306 double oldPhero = laneIterator->second;
309 bool updatePheromone = (meanVehiclesSpeed > -1);
313 double derivative = 0;
317 if (updatePheromone) {
318 double currentDerivative = 0;
352 double pheroAdd =
MAX2((maxSpeed - meanVehiclesSpeed) * 10 / maxSpeed, 0.0);
361 if (updatePheromone) {
362 std::ostringstream oss;
364 oss <<
" der " << derivative <<
" phero " << pheroAdd <<
" maxS " << maxSpeed <<
" meanS " << meanVehiclesSpeed;
370 double phero = beta * oldPhero + gamma * pheroAdd * updatePheromone;
373 std::ostringstream i_str;
374 i_str <<
"MSSwarmTrafficLightLogic::updatePheromoneLevels " << logString <<
" > 10. Value: " << phero;
379 pheroMap[laneId] = phero;
382 std::ostringstream i_str;
390 i_str <<
" op " << oldPhero <<
" ms " << meanVehiclesSpeed <<
" p " << pheroAdd * updatePheromone <<
391 " pe " << oldPhero - oldPhero * beta <<
" pd " << gamma * pheroAdd * updatePheromone <<
" np " <<
422 for (
int i = 0; i < (int)
policies.size(); i++) {
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());)
449 for (
int i = 0; i < (int)
policies.size(); i++) {
451 double newSensitivity;
478 phero_str <<
" policy " << policy->
getName() <<
" newSensitivity " << newSensitivity <<
" ,pol.Sensitivity " << policy->
getThetaSensitivity() <<
" ,elapsedTime " << elapsedTime << lf.str() <<
" NEWERSensitivity= " << max(min(newSensitivity,
getThetaMax()),
getThetaMin()) <<
" ID " <<
getID() <<
" .";
484 }
else if (eta > 0) {
486 }
else if (eta < 0) {
505 std::string laneId = iterator->first;
506 pheroIn += iterator->second;
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());)
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;
539 std::string laneId = iterator->first;
540 sum += pow(iterator->second - average_phero_in, 2);
545 ostringstream so_str; so_str <<
" dispersionIn " << result;
WRITE_MESSAGE(
"MSSwarmTrafficLightLogic::getDispersionForInputLanes::" + so_str.str());)
556 sum += pow(iterator->second - average_phero_out, 2);
561 ostringstream so_str; so_str <<
" dispersionOut " << result;
WRITE_MESSAGE(
"MSSwarmTrafficLightLogic::getDispersionForOutputLanes::" + so_str.str());)
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;
575 std::string laneId = iterator->first;
576 double lanePhero = iterator->second;
578 max_phero_val_current = lanePhero;
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;
587 temp_avg_other_lanes = (temp_avg_other_lanes * (counter - 1) + lanePhero) / counter;
593 double result = max_phero_val_current - temp_avg_other_lanes;
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());)
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;
610 std::string laneId = iterator->first;
611 double lanePhero = iterator->second;
613 max_phero_val_current = lanePhero;
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;
622 temp_avg_other_lanes = (temp_avg_other_lanes * (counter - 1) + lanePhero) / counter;
628 double result = max_phero_val_current - temp_avg_other_lanes;
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());)
650 choosePolicy(pheroIn, pheroOut, distancePheroIn, distancePheroOut);
653 if (newPolicy != oldPolicy) {
656 if (oldPolicy->
getName().compare(
"Congestion") == 0) {
676 return 1 - (1 / ((double) factor));
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;
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();
700 currentLane = (*lane);
708 std::ostringstream cars_str; cars_str <<
"Lane " << currentLane->
getID() <<
": vehicles entered - " << count;
WRITE_MESSAGE(
time2string(
MSNet::getInstance()->getCurrentTimeStep()) +
" MSSwarmTrafficLightLogic::calculateEta::" + cars_str.str());)
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();
724 currentLane = (*link)->getLane();
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());)
742 toReset.push_back(currentLane->
getID());
746 }
else if (count <= minOut) {
760 std::string lane = (*laneId);
763 if (inInit && tmp != 0) {
767 if (tmp < minIn && tmp != 0) {
771 toReset.push_back(lane);
774 std::ostringstream cars_str; cars_str <<
"Lane " << lane <<
" passed: " << tmp;
WRITE_MESSAGE(
time2string(
MSNet::getInstance()->getCurrentTimeStep()) +
" MSSwarmTrafficLightLogic::calculateEta::" + cars_str.str());)
782 toSub = std::min(minIn, minOut);
785 while (!toReset.empty()) {
786 std::string laneId = toReset.back();
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());)
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());)
820 eta = (-normalized * (1 / phi));
858 eta = normalized * phi;
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;
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();
889 currentLane = (*lane);
897 std::ostringstream cars_str; cars_str <<
"Lane " << currentLane->
getID() <<
": vehicles entered - " << count;
WRITE_MESSAGE(
time2string(
MSNet::getInstance()->getCurrentTimeStep()) +
" MSSwarmTrafficLightLogic::calculateEta::" + cars_str.str());)
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();
913 currentLane = (*link)->getLane();
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());)
931 toReset.push_back(currentLane->
getID());
935 }
else if (count <= minOut) {
950 std::string lane = (*laneId);
953 if (inInit && tmp != 0) {
957 if (tmp < minIn && tmp != 0) {
961 toReset.push_back(lane);
964 std::ostringstream cars_str; cars_str <<
"Lane " << lane <<
" passed: " << tmp;
WRITE_MESSAGE(
time2string(
MSNet::getInstance()->getCurrentTimeStep()) +
" MSSwarmTrafficLightLogic::calculateEta::" + cars_str.str());)
972 toSub = std::min(minIn, minOut);
975 while (!toReset.empty()) {
976 std::string laneId = toReset.back();
986 ratio = std::numeric_limits<double>::infinity();
987 normalized = std::numeric_limits<double>::infinity();
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());)
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());)
1014 eta = (-(normalized) * (1 / phi));
1050 eta = (normalized) * phi;
1058 std::ostringstream eta_str; eta_str <<
"Eta Normalized: " << eta <<
".";
WRITE_MESSAGE(
time2string(
MSNet::getInstance()->getCurrentTimeStep()) +
" MSSwarmTrafficLightLogic::calculateEta::" + eta_str.str());)
1065 MSLane* currentLane =
nullptr;
1068 for (MSTrafficLightLogic::LaneVectorVector::const_iterator laneVector =
myLanes.begin();
1069 laneVector !=
myLanes.end(); laneVector++) {
1071 for (MSTrafficLightLogic::LaneVector::const_iterator lane = laneVector->begin(); lane != laneVector->end();
1073 currentLane = (*lane);
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();
1082 currentLane = (*link)->getLane();
1089 double dispersion_out) {
1092 if (it.operator * ()->getName() ==
"Phase") {
1098 std::vector<double> thetaStimuli;
1099 double thetaSum = 0.0;
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));
1105 thetaStimuli.push_back(thetaStimulus);
1106 thetaSum += thetaStimulus;
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());)
1119 double partialSum = 0;
1120 for (
int i = 0; i < (int)
getPolicies().size(); i++) {
1121 partialSum += thetaStimuli[i];
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());)
1127 if (partialSum >= r) {
1147 std::string laneState =
"";
1151 laneState += state[*it];