Line data Source code
1 : #include "Dynamic/Env/Connection.hpp" 2 : 3 : #include <stdexcept> 4 : #include <string> 5 : 6 : #include "Dynamic/Env/Lane.hpp" 7 : #include "Dynamic/Env/TrafficLight.hpp" 8 : 9 : using namespace std; 10 : using namespace Dynamic; 11 : using namespace Dynamic::Env; 12 : 13 : const Time Connection::CRITICAL_GAP = 2.5; 14 : 15 0 : Connection::Connection(ID id_, Lane &fromLane_, Lane &toLane_): 16 0 : id(id_), fromLane(fromLane_), toLane(toLane_) {} 17 : 18 0 : void Connection::addMoreImportant(Connection &otherConnection) { 19 0 : moreImportant.insert(otherConnection); 20 0 : otherConnection.lessImportant.insert(*this); 21 0 : } 22 : 23 0 : bool Connection::operator==(const Connection &connection) const { 24 0 : return id == connection.id; 25 : } 26 : 27 0 : bool Connection::operator!=(const Connection &connection) const { 28 0 : return !(*this == connection); 29 : } 30 : 31 0 : bool Connection::isRed() const { 32 0 : if(!trafficLight.has_value()) 33 : return false; 34 : 35 0 : TrafficLight &tl = trafficLight.value(); 36 : 37 0 : TrafficLight::Phase::State state = tl.currentPhase->state.at(tlLinkIndex.value()); 38 : 39 0 : switch(state) { 40 : case TrafficLight::Phase::State::GREEN: 41 : case TrafficLight::Phase::State::YELLOW: 42 : return false; 43 0 : case TrafficLight::Phase::State::RED: 44 0 : return true; 45 0 : default: 46 0 : throw logic_error("Connection::isRed: unknown state " + to_string(static_cast<int>(state))); 47 : } 48 : } 49 : 50 0 : bool Connection::operator<(const Connection &other) const { 51 0 : return id < other.id; 52 : } 53 : 54 0 : Time Connection::getMinExpectedStopTimeTL() const { 55 0 : if(!trafficLight.has_value()) 56 : return 0; 57 : 58 0 : TrafficLight &tl = trafficLight.value(); 59 : 60 0 : Time ret = 0; 61 0 : for(const auto &[_, phase]: tl.phases) { 62 0 : const TrafficLight::Phase::State &state = phase.state.at(tlLinkIndex.value()); 63 : 64 0 : Time wait = 0; 65 : 66 0 : switch(state) { 67 0 : case TrafficLight::Phase::State::RED: 68 0 : wait = phase.duration / 2.0; 69 0 : break; 70 : case TrafficLight::Phase::State::YELLOW: 71 : case TrafficLight::Phase::State::GREEN: 72 : default: 73 : break; 74 : } 75 : 76 0 : ret += wait * phase.duration; 77 : } 78 : 79 0 : ret /= tl.duration(); 80 : 81 0 : return ret; 82 : } 83 : 84 0 : bool Connection::yieldsTo(const Connection &other) const { 85 0 : const reference_wrapper<Connection> otherWrapper(const_cast<Connection &>(other)); 86 0 : return moreImportant.find(otherWrapper) != moreImportant.end(); 87 : } 88 : 89 0 : Time Connection::mustYieldUntil() const { 90 0 : Time t = -std::numeric_limits<Time>::infinity(); 91 0 : for(const Connection &connection: moreImportant) { 92 0 : t = max(t, connection.lastUsed); 93 : } 94 0 : return t + CRITICAL_GAP; 95 : } 96 : 97 : Connection Connection::STOP = {-1, Lane::INVALID, Lane::INVALID}; 98 : Connection Connection::LEAVE = {-2, Lane::INVALID, Lane::INVALID}; 99 : Connection Connection::INVALID = {-3, Lane::INVALID, Lane::INVALID}; 100 : 101 0 : size_t std::hash<Connection>::operator()(const Connection &connection) const { 102 0 : return hash<Connection::ID>()(connection.id); 103 : }