Line data Source code
1 : #include "Dynamic/Env/Loader.hpp" 2 : 3 : #include "Dynamic/Env/Env.hpp" 4 : #include "Dynamic/Env/Lane.hpp" 5 : #include "Dynamic/Env/TrafficLight.hpp" 6 : #include "data/SUMO/Network.hpp" 7 : #include "data/SUMO/TAZ.hpp" 8 : 9 : using namespace std; 10 : using namespace Dynamic::Env; 11 : 12 : // clang-format off 13 0 : shared_ptr<Env> Loader< 14 : const SUMO::NetworkTAZs &, 15 : Dynamic::RewardFunction & 16 : >::load( 17 : const SUMO::NetworkTAZs &sumo, 18 : RewardFunction &rewardFunction 19 : ) { 20 : // clang-format on 21 : 22 0 : shared_ptr<Env> ret = make_shared<Env>(rewardFunction); 23 : 24 0 : env = ret.get(); 25 : 26 0 : addTrafficLights(sumo); 27 : 28 0 : addEdges(sumo); 29 : 30 0 : addConnections(sumo); 31 : 32 0 : addTAZs(sumo); 33 : 34 0 : return ret; 35 : } 36 : 37 : // clang-format off 38 0 : void Loader< 39 : const SUMO::NetworkTAZs &, 40 : Dynamic::RewardFunction & 41 : >::addTrafficLights( 42 : const SUMO::NetworkTAZs &sumo 43 : ) { 44 0 : for(const auto &[sumoTLID, sumoTL]: sumo.network.getTrafficLights()){ 45 0 : TrafficLight &tl = env->addTrafficLight( 46 : adapter.addSumoTL(sumoTLID), 47 0 : sumoTL.offset 48 0 : ); 49 0 : for(const auto &[pTime, p]: sumoTL.phases){ 50 0 : vector<TrafficLight::Phase::State> state; 51 0 : for(const SUMO::Network::TrafficLightLogic::Phase::State &s: p.state){ 52 0 : switch(s){ 53 0 : case SUMO::Network::TrafficLightLogic::Phase::State::RED: 54 0 : state.push_back(TrafficLight::Phase::State::RED); 55 0 : break; 56 0 : case SUMO::Network::TrafficLightLogic::Phase::State::YELLOW_STOP: 57 0 : state.push_back(TrafficLight::Phase::State::YELLOW); 58 0 : break; 59 0 : default: 60 0 : state.push_back(TrafficLight::Phase::State::GREEN); 61 0 : break; 62 : } 63 : } 64 : 65 0 : tl.addPhase( 66 : pTime, 67 0 : p.duration, 68 : state 69 0 : ); 70 : } 71 : } 72 0 : } 73 : 74 : // clang-format off 75 0 : void Loader< 76 : const SUMO::NetworkTAZs &, 77 : Dynamic::RewardFunction & 78 : >::addEdges( 79 : const SUMO::NetworkTAZs &sumo 80 : ) { 81 : // clang-format on 82 : 83 0 : const vector<SUMO::Network::Edge> &edges = sumo.network.getEdges(); 84 0 : for(const SUMO::Network::Edge &edge: edges) { 85 0 : if(edge.function == SUMO::Network::Edge::Function::INTERNAL) continue; 86 : 87 0 : const auto &p = adapter.addSumoEdge(edge.id); 88 0 : const Edge::ID &eid = p.first; 89 0 : Node u = p.second.first, v = p.second.second; 90 : 91 0 : env->addEdge( 92 : eid, 93 : u, 94 : v, 95 : edge.length(), 96 : edge.speed(), 97 0 : edge.priority, 98 : edge.lanes.size() 99 0 : ); 100 : } 101 0 : } 102 : 103 : // clang-format off 104 0 : void Loader< 105 : const SUMO::NetworkTAZs &, 106 : Dynamic::RewardFunction & 107 : >::addConnections( 108 : const SUMO::NetworkTAZs &sumo 109 : ) { 110 : // clang-format on 111 : 112 0 : auto connections = sumo.network.getConnections(); 113 0 : for(const auto &[fromID, connectionsFrom]: connections) { 114 0 : for(const auto &[toID, connectionsFromTo]: connectionsFrom) { 115 0 : for(const SUMO::Network::Connection &connection: connectionsFromTo) { 116 0 : addConnection(sumo, connection); 117 : } 118 : } 119 : } 120 : 121 0 : for(const auto &[fromID, connectionsFrom]: connections) { 122 0 : for(const auto &[toID, connectionsFromTo]: connectionsFrom) { 123 0 : for(const SUMO::Network::Connection &connection: connectionsFromTo) { 124 0 : addConflicts(sumo, connection); 125 : } 126 : } 127 : } 128 0 : } 129 : 130 : // clang-format off 131 0 : void Loader< 132 : const SUMO::NetworkTAZs &, 133 : Dynamic::RewardFunction & 134 : >::addConnection( 135 : const SUMO::NetworkTAZs &, 136 : const SUMO::Network::Connection &connection 137 : ) { 138 : // clang-format on 139 0 : Connection::ID connectionID = nextConnectionID++; 140 : 141 : // clang-format off 142 0 : if( 143 0 : connection.from.function == SUMO::Network::Edge::Function::INTERNAL || 144 0 : connection.to.function == SUMO::Network::Edge::Function::INTERNAL 145 0 : ) return; 146 : // clang-format on 147 : 148 0 : Edge::ID fromID = adapter.toEdge(connection.from.id); 149 0 : Edge::ID toID = adapter.toEdge(connection.to.id); 150 : 151 0 : Edge &from = env->getEdge(fromID); 152 0 : Edge &to = env->getEdge(toID); 153 : 154 0 : Lane &fromLane = from.lanes.at(connection.fromLaneIndex); 155 0 : Lane &toLane = to.lanes.at(connection.toLaneIndex); 156 : 157 0 : Connection &conn = env->addConnection( 158 : connectionID, 159 : fromLane, 160 : toLane 161 0 : ); 162 : 163 0 : if(connection.tl) { 164 0 : conn.trafficLight = env->getTrafficLight(adapter.toTL(connection.tl.value().get().id)); 165 0 : conn.tlLinkIndex = connection.linkIndex; 166 : 167 0 : conn.trafficLight.value().get().connections.emplace_back(conn); 168 : } 169 : } 170 : 171 : // clang-format off 172 0 : void Loader< 173 : const SUMO::NetworkTAZs &, 174 : Dynamic::RewardFunction & 175 : >::addConflicts( 176 : const SUMO::NetworkTAZs &, 177 : const SUMO::Network::Connection &connection 178 : ) { 179 : // clang-format on 180 : 181 : // clang-format off 182 0 : if( 183 0 : connection.from.function == SUMO::Network::Edge::Function::INTERNAL || 184 0 : connection.to.function == SUMO::Network::Edge::Function::INTERNAL 185 0 : ) return; 186 : // clang-format on 187 : 188 0 : Edge::ID fromID = adapter.toEdge(connection.from.id); 189 0 : Lane::Index fromLaneIndex = connection.fromLaneIndex; 190 : 191 0 : Edge::ID toID = adapter.toEdge(connection.to.id); 192 0 : Lane::Index toLaneIndex = connection.toLaneIndex; 193 : 194 0 : Lane &fromLane = env->getEdge(fromID).lanes.at(fromLaneIndex); 195 0 : Lane &toLane = env->getEdge(toID).lanes.at(toLaneIndex); 196 : 197 0 : Connection &envConnection = fromLane.getOutgoingConnection(toLane); 198 : 199 0 : for(const SUMO::Network::Connection &otherConnection: connection.getRequest().getResponse()) { 200 : // clang-format off 201 0 : if( 202 0 : otherConnection.from.function == SUMO::Network::Edge::Function::INTERNAL || 203 0 : otherConnection.to.function == SUMO::Network::Edge::Function::INTERNAL 204 0 : ) continue; 205 : // clang-format on 206 : 207 0 : Edge::ID otherFromID = adapter.toEdge(otherConnection.from.id); 208 0 : Lane::Index otherFromLaneIndex = otherConnection.fromLaneIndex; 209 : 210 0 : Edge::ID otherToID = adapter.toEdge(otherConnection.to.id); 211 0 : Lane::Index otherToLaneIndex = otherConnection.toLaneIndex; 212 : 213 0 : Lane &otherFromLane = env->getEdge(otherFromID).lanes.at(otherFromLaneIndex); 214 0 : Lane &otherToLane = env->getEdge(otherToID).lanes.at(otherToLaneIndex); 215 : 216 0 : Connection &otherEnvConnection = otherFromLane.getOutgoingConnection(otherToLane); 217 : 218 0 : envConnection.addMoreImportant(otherEnvConnection); 219 : } 220 : } 221 : 222 : // clang-format off 223 0 : void Loader< 224 : const SUMO::NetworkTAZs &, 225 : Dynamic::RewardFunction & 226 : >::addTAZs( 227 : const SUMO::NetworkTAZs &sumo 228 : ) { 229 : // clang-format on 230 : 231 0 : for(const auto &[id, taz]: sumo.tazs) { 232 0 : TAZ::ID envTAZID = adapter.addSumoTAZ( 233 0 : taz.id, 234 0 : taz.sources, 235 0 : taz.sinks 236 : ); 237 : 238 0 : TAZ &envTAZ = env->addTAZ(envTAZID); 239 : 240 0 : for(const SUMO::TAZ::Source &source: taz.sources) { 241 0 : envTAZ.sources.insert(env->getEdge(adapter.toEdge(source.id))); 242 : } 243 : 244 0 : for(const SUMO::TAZ::Sink &sink: taz.sinks) { 245 0 : envTAZ.sinks.insert(env->getEdge(adapter.toEdge(sink.id))); 246 : } 247 : } 248 0 : }