sumolib.scenario.scenarios
1# Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo 2# Copyright (C) 2012-2026 German Aerospace Center (DLR) and others. 3# This program and the accompanying materials are made available under the 4# terms of the Eclipse Public License 2.0 which is available at 5# https://www.eclipse.org/legal/epl-2.0/ 6# This Source Code may also be made available under the following Secondary 7# Licenses when the conditions for such availability set forth in the Eclipse 8# Public License 2.0 are satisfied: GNU General Public License, version 2 9# or later which is available at 10# https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html 11# SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later 12 13# @file __init__.py 14# @author Daniel Krajzewicz 15# @date 2014-07-01 16 17 18from __future__ import absolute_import 19from __future__ import print_function 20import os 21import sumolib.net.generator.cross as netGenerator # noqa 22import sumolib.net.generator.demand as demandGenerator 23import sumolib 24 25 26SANDBOX_PATH = os.path.join(os.path.dirname(__file__), "..", "sandbox") 27REBUILD = False 28 29 30def maxIndexValue_unset(l1, l2): 31 i = 0 32 max_val = None 33 max_idx = -1 34 while i < len(l1): 35 if l2[i] != 0: 36 i = i + 1 37 continue 38 if max_val is None or max_val < l1[i]: 39 max_idx = i 40 max_val = l1[i] 41 i = i + 1 42 return max_idx, max_val 43 44 45def minIndexValue_unset(l1, l2): 46 i = 0 47 min_val = None 48 min_idx = -1 49 while i < len(l1): 50 if l2[i] != 0: 51 i = i + 1 52 continue 53 if min_val is None or min_val > l1[i]: 54 min_idx = i 55 min_val = l1[i] 56 i = i + 1 57 return min_idx, min_val 58 59 60def fileNeedsRebuild(filePath, app): 61 print("fileNeedsRebuild> %s" % filePath) 62 if REBUILD: 63 return True 64 if not os.path.exists(filePath): 65 return True 66 genAppPath = sumolib.checkBinary(app) 67 tf = os.path.getmtime(filePath) 68 ta = os.path.getmtime(genAppPath) 69 return tf < ta 70 71 72def split_by_proportions(total, proportions, mininum_values): 73 """splits the given total by the given proportions but ensures that each value in 74 the result has at least the given minimum value""" 75 assert len(proportions) == len(mininum_values) 76 assert total >= sum(mininum_values) 77 assert min(proportions) > 0 78 num = len(proportions) 79 sumProportions = float(sum(proportions)) 80 fractions = [p / sumProportions for p in proportions] 81 result = [max(m, int(round(total * f))) 82 for f, m in zip(fractions, mininum_values)] 83 delta = sum(result) - total 84 correct = -1 if delta > 0 else 1 85 i = 0 86 while delta != 0: 87 if result[i] + correct >= mininum_values[i]: 88 result[i] += correct 89 delta += correct 90 i = (i + 1) % num 91 92 assert sum(result) == total 93 return result 94 95 96def extrapolateDemand(stream, freq, probs, pivot=demandGenerator.PIVOT__PEAK, tBeg=0): 97 ret = demandGenerator.Demand() 98 if pivot == demandGenerator.PIVOT__PEAK: 99 mmax = 0 100 mpos = [] 101 for i, p in enumerate(probs): 102 if p > mmax: 103 mpos = i 104 mmax = p 105 # !!! should be done 106 # if count(probs, p)>1: 107 # raise "more than one maximum value" 108 # else: 109 # pivot = mpos 110 pivot = mpos 111 t = tBeg 112 for i, p in enumerate(probs): 113 # ok, this works just if _numberModel is a number 114 num = float(stream._numberModel) * p / probs[pivot] 115 ret.addStream(demandGenerator.Stream(stream.sid + "_" + str(i), t, t + freq, 116 num, stream._departEdgeModel, stream._arrivalEdgeModel, 117 stream._vTypeModel)) 118 t = t + freq 119 return ret 120 121 122class Scenario: 123 124 def __init__(self, dataPath): 125 self.dataPath = dataPath 126 self.net = None 127 self.netName = None 128 self.demand = None 129 self.demandName = None 130 self.additional = {} 131 self.conn = None 132 self.addAdditionalFile("vtypes") 133 try: 134 os.makedirs(os.path.join(SANDBOX_PATH, self.name)) 135 except OSError: 136 pass 137 138 def addAdditionalFile(self, name): 139 self.additional[name] = [] 140 141 def addAdditional(self, name, add): 142 self.additional[name].append(add) 143 144 def writeSUMOConfig(self, cfgName, addOptions={}): 145 cfg = {} 146 for a in addOptions: 147 cfg[a] = addOptions[a] 148 cfg["net-file"] = self.netName 149 cfg["route-files"] = self.demandName 150 if "vtypes" in self.additional: 151 cfg["additional-files"] = "vtypes.add.xml" 152 for a in self.additional: 153 if a == "vtypes": 154 continue 155 fileName = a + ".add.xml" 156 if len(self.additional[a]) > 0: 157 sumolib.files.additional.write(fileName, self.additional[a]) 158 if "additional-files" not in cfg: 159 cfg["additional-files"] = fileName 160 else: 161 cfg["additional-files"] = cfg["additional-files"] + \ 162 "," + fileName 163 fdo = open(cfgName, "w") 164 fdo.write("<c>\n") 165 for v in cfg: 166 fdo.write(' <%s value="%s"/>\n' % (v, cfg[v])) 167 fdo.write("</c>\n") 168 fdo.close() 169 170 def getNet(self): 171 if self.net is not None: 172 return self.net 173 if self.netName is not None: 174 self.net = sumolib.net.readNet(self.netName) 175 return self.net 176 raise RuntimeError("network is unknown") 177 178 def fullPath(self, fileName): 179 print("full >" + os.path.join(self.dataPath, fileName)) 180 return os.path.join(self.dataPath, fileName) 181 182 def sandboxPath(self, fileName): 183 print("sandbox >" + os.path.join(SANDBOX_PATH, fileName)) 184 return os.path.join(SANDBOX_PATH, fileName) 185 186 def getOppositeFlows(self): 187 fNS = [0] * 24 188 fWE = [0] * 24 189 for s in self.demand.streams: 190 if s._departEdgeModel.startswith("em") or s._departEdgeModel.startswith("wm"): 191 fWE[int(s._validFrom / 3600) 192 ] = fWE[int(s._validFrom / 3600)] + s._numberModel 193 elif s._departEdgeModel.startswith("sm") or s._departEdgeModel.startswith("nm"): 194 fNS[int(s._validFrom / 3600) 195 ] = fNS[int(s._validFrom / 3600)] + s._numberModel 196 return (fNS, fWE) 197 198 def getOppositeFlows2(self, ew, sn): 199 fNS = [0] * 24 200 fWE = [0] * 24 201 for s in self.demand.streams: 202 if s._departEdgeModel in ew: 203 fWE[int(s._validFrom / 3600) 204 ] = fWE[int(s._validFrom / 3600)] + s._numberModel 205 else: 206 fNS[int(s._validFrom / 3600) 207 ] = fNS[int(s._validFrom / 3600)] + s._numberModel 208 return (fNS, fWE) 209 210 def buildWAUT(self, streamsNS, streamsWE): 211 # 212 rel = [] 213 ovr = [] 214 program = [0] * len(streamsNS) 215 for i, x in enumerate(streamsNS): 216 s = streamsNS[i] + streamsWE[i] 217 if s != 0: 218 rel.append(x / s) 219 else: 220 rel.append(0) 221 ovr.append(s) 222 # 223 NIGHT = 1 224 DAY = 2 225 MORNING = 3 226 AFTERNOON = 4 227 NIGHT_MAX = 500 228 229 MORNING_MIN = 500 230 AFTERNOON_MIN = 500 231 REL_THRESHOLD = .45 232 IREL_THRESHOLD = 1. - REL_THRESHOLD 233 234 i = 0 235 # streamsNS[i]<NIGHT_MAX and streamsWE[i]<NIGHT_MAX: 236 while i < len(streamsNS) and streamsNS[i] + streamsWE[i] < NIGHT_MAX: 237 program[i] = NIGHT 238 i = i + 1 239 i = len(streamsNS) - 1 240 # and streamsNS[i]<NIGHT_MAX and streamsWE[i]<NIGHT_MAX: 241 while i > 0 and streamsNS[i] + streamsWE[i] < NIGHT_MAX: 242 program[i] = NIGHT 243 i = i - 1 244 i = 0 245 maxIdx, maxVal = maxIndexValue_unset(rel, program) 246 minIdx, minVal = minIndexValue_unset(rel, program) 247 print(program) 248 print("morning max %s %s" % (maxIdx, maxVal)) 249 print("morning min %s %s" % (minIdx, minVal)) 250 print("%s %s" % (maxVal, 1. - minVal)) 251 if maxVal > 1. - minVal: 252 i = maxIdx 253 while i > 0 and rel[i] > IREL_THRESHOLD and program[i] == 0 and streamsNS[i] + streamsWE[i] > MORNING_MIN: 254 program[i] = MORNING 255 i = i - 1 256 i = maxIdx + 1 257 while i < len(rel) and rel[i] > IREL_THRESHOLD and program[ 258 i] == 0 and streamsNS[i] + streamsWE[i] > MORNING_MIN: 259 program[i] = MORNING 260 i = i + 1 261 else: 262 i = minIdx 263 print("!") 264 print(" %s %s %s %s" % 265 (i, rel[i], program[i], streamsNS[i] + streamsWE[i])) 266 while i > 0 and rel[i] < REL_THRESHOLD and program[i] == 0 and streamsNS[i] + streamsWE[i] > MORNING_MIN: 267 program[i] = MORNING 268 i = i - 1 269 print(" %s %s %s %s" % 270 (i, rel[i], program[i], streamsNS[i] + streamsWE[i])) 271 i = minIdx + 1 272 print(" %s %s %s %s" % 273 (i, rel[i], program[i], streamsNS[i] + streamsWE[i])) 274 while i < len(rel) and rel[i] < REL_THRESHOLD and program[ 275 i] == 0 and streamsNS[i] + streamsWE[i] > MORNING_MIN: 276 program[i] = MORNING 277 i = i + 1 278 print(" %s %s %s %s" % 279 (i, rel[i], program[i], streamsNS[i] + streamsWE[i])) 280 281 print(rel) 282 print(program) 283 maxIdx, maxVal = maxIndexValue_unset(rel, program) 284 minIdx, minVal = minIndexValue_unset(rel, program) 285 if maxIdx != -1 and minIdx != -1: 286 print("%s %s" % (maxIdx, maxVal)) 287 print("%s %s" % (minIdx, minVal)) 288 print("%s %s" % (maxVal, 1 - minVal)) 289 if maxVal > 1 - minVal: 290 i = maxIdx 291 # and rel[i]>.6 292 while i > 0 and rel[i] > IREL_THRESHOLD and program[ 293 i] == 0 and streamsNS[i] + streamsWE[i] > AFTERNOON_MIN: 294 program[i] = AFTERNOON 295 i = i - 1 296 i = maxIdx + 1 297 while i < len(rel) and rel[i] > IREL_THRESHOLD and program[ 298 i] == 0 and streamsNS[i] + streamsWE[i] > AFTERNOON_MIN: 299 program[i] = AFTERNOON 300 i = i + 1 301 else: 302 i = minIdx 303 while i > 0 and rel[i] < REL_THRESHOLD and program[ 304 i] == 0 and streamsNS[i] + streamsWE[i] > AFTERNOON_MIN: 305 program[i] = AFTERNOON 306 i = i - 1 307 i = minIdx + 1 308 while i < len(rel) and rel[i] < REL_THRESHOLD and program[ 309 i] == 0 and streamsNS[i] + streamsWE[i] > AFTERNOON_MIN: 310 program[i] = AFTERNOON 311 i = i + 1 312 for i, x in enumerate(program): 313 if program[i] == 0: 314 program[i] = DAY 315 fdo = open("waut.txt", "w") 316 for i, x in enumerate(program): 317 fdo.write("%s %s %s %s %s\n" % 318 (streamsNS[i], streamsWE[i], ovr[i], rel[i], program[i])) 319 fdo.close() 320 # 321 ret1 = {} 322 for i in range(1, 5): 323 maxV = None 324 maxI = 0 325 for j, v in enumerate(rel): 326 if program[j] != i: 327 continue 328 if maxV is None: 329 maxV = rel[j] 330 maxI = j 331 elif maxV < rel[j]: 332 maxV = rel[j] 333 maxI = j 334 elif maxV < 1. - rel[j]: 335 maxV = 1. - rel[j] 336 maxI = j 337 print("%s %s %s" % (i, maxV, maxI)) 338 greens = split_by_proportions( 339 72, (rel[maxI], 1. - rel[maxI]), (10, 10)) 340 ret1[i] = greens 341 print(greens) 342 # build programs 343 ret2 = [] 344 last = -1 345 for j, v in enumerate(program): 346 if last != v: 347 ret2.append((j, v)) 348 last = v 349 return (ret1, ret2) 350 351 352def getScenario(which, useName, params, withDefaultDemand=True): 353 if which == "RiLSA1": 354 from . import rilsa1 355 return rilsa1.Scenario_RiLSA1(useName, withDefaultDemand) 356 elif which == "RiLSA1OutTLS": 357 from . import rilsa1_out_tls 358 return rilsa1_out_tls.Scenario_RiLSA1OutTLS(params, withDefaultDemand) 359 elif which == "RiLSA1BothTLS": 360 from . import rilsa1_both_tls 361 return rilsa1_both_tls.Scenario_RiLSA1BothTLS(params, withDefaultDemand) 362 elif which == "RiLSA1OutTLS24": 363 from . import rilsa1_out_tls24 364 return rilsa1_out_tls24.Scenario_RiLSA1OutTLS24(params, withDefaultDemand) 365 elif which == "RiLSA1BothTLS24": 366 from . import rilsa1_both_tls24 367 return rilsa1_both_tls24.Scenario_RiLSA1BothTLS24(params, withDefaultDemand) 368 elif which == "BasicCross": 369 from . import basic_cross 370 return basic_cross.Scenario_BasicCross(useName, withDefaultDemand) 371 elif which == "BasicCrossL": 372 from . import basic_crossl 373 return basic_crossl.Scenario_BasicCrossL(useName, withDefaultDemand) 374 elif which == "BasicCorridor": 375 from . import basic_corridor 376 return basic_corridor.Scenario_BasicCorridor(useName, params["xoff"], withDefaultDemand) 377 elif which == "BasicNet": 378 from . import basic_net 379 return basic_net.Scenario_BasicNet(useName, params["rot"], withDefaultDemand) 380 elif which == "RealWorld": 381 from . import real_world 382 return real_world.Scenario_RealWorld(useName, params["which"], withDefaultDemand) 383 elif which == "BasicRiLSANet": 384 from . import basic_rilsanet 385 return basic_rilsanet.Scenario_BasicRiLSANet(useName, params, withDefaultDemand) 386 elif which == "BasicRiLSANet2x2": 387 from . import basic_rilsanet2x2 388 return basic_rilsanet2x2.Scenario_BasicRiLSANet2x2(useName, params, withDefaultDemand) 389 elif which == "BasicRiLSACorridor3": 390 from . import basic_rilsacorridor3 391 return basic_rilsacorridor3.Scenario_BasicRiLSACorridor3(useName, params, withDefaultDemand) 392 raise RuntimeError("unknown scenario '%s'" % which)
SANDBOX_PATH =
'/home/delphi/gcc/sumo/tools/sumolib/scenario/scenarios/../sandbox'
REBUILD =
False
def
maxIndexValue_unset(l1, l2):
def
minIndexValue_unset(l1, l2):
def
fileNeedsRebuild(filePath, app):
def
split_by_proportions(total, proportions, mininum_values):
73def split_by_proportions(total, proportions, mininum_values): 74 """splits the given total by the given proportions but ensures that each value in 75 the result has at least the given minimum value""" 76 assert len(proportions) == len(mininum_values) 77 assert total >= sum(mininum_values) 78 assert min(proportions) > 0 79 num = len(proportions) 80 sumProportions = float(sum(proportions)) 81 fractions = [p / sumProportions for p in proportions] 82 result = [max(m, int(round(total * f))) 83 for f, m in zip(fractions, mininum_values)] 84 delta = sum(result) - total 85 correct = -1 if delta > 0 else 1 86 i = 0 87 while delta != 0: 88 if result[i] + correct >= mininum_values[i]: 89 result[i] += correct 90 delta += correct 91 i = (i + 1) % num 92 93 assert sum(result) == total 94 return result
splits the given total by the given proportions but ensures that each value in the result has at least the given minimum value
def
extrapolateDemand(stream, freq, probs, pivot=10000, tBeg=0):
97def extrapolateDemand(stream, freq, probs, pivot=demandGenerator.PIVOT__PEAK, tBeg=0): 98 ret = demandGenerator.Demand() 99 if pivot == demandGenerator.PIVOT__PEAK: 100 mmax = 0 101 mpos = [] 102 for i, p in enumerate(probs): 103 if p > mmax: 104 mpos = i 105 mmax = p 106 # !!! should be done 107 # if count(probs, p)>1: 108 # raise "more than one maximum value" 109 # else: 110 # pivot = mpos 111 pivot = mpos 112 t = tBeg 113 for i, p in enumerate(probs): 114 # ok, this works just if _numberModel is a number 115 num = float(stream._numberModel) * p / probs[pivot] 116 ret.addStream(demandGenerator.Stream(stream.sid + "_" + str(i), t, t + freq, 117 num, stream._departEdgeModel, stream._arrivalEdgeModel, 118 stream._vTypeModel)) 119 t = t + freq 120 return ret
class
Scenario:
123class Scenario: 124 125 def __init__(self, dataPath): 126 self.dataPath = dataPath 127 self.net = None 128 self.netName = None 129 self.demand = None 130 self.demandName = None 131 self.additional = {} 132 self.conn = None 133 self.addAdditionalFile("vtypes") 134 try: 135 os.makedirs(os.path.join(SANDBOX_PATH, self.name)) 136 except OSError: 137 pass 138 139 def addAdditionalFile(self, name): 140 self.additional[name] = [] 141 142 def addAdditional(self, name, add): 143 self.additional[name].append(add) 144 145 def writeSUMOConfig(self, cfgName, addOptions={}): 146 cfg = {} 147 for a in addOptions: 148 cfg[a] = addOptions[a] 149 cfg["net-file"] = self.netName 150 cfg["route-files"] = self.demandName 151 if "vtypes" in self.additional: 152 cfg["additional-files"] = "vtypes.add.xml" 153 for a in self.additional: 154 if a == "vtypes": 155 continue 156 fileName = a + ".add.xml" 157 if len(self.additional[a]) > 0: 158 sumolib.files.additional.write(fileName, self.additional[a]) 159 if "additional-files" not in cfg: 160 cfg["additional-files"] = fileName 161 else: 162 cfg["additional-files"] = cfg["additional-files"] + \ 163 "," + fileName 164 fdo = open(cfgName, "w") 165 fdo.write("<c>\n") 166 for v in cfg: 167 fdo.write(' <%s value="%s"/>\n' % (v, cfg[v])) 168 fdo.write("</c>\n") 169 fdo.close() 170 171 def getNet(self): 172 if self.net is not None: 173 return self.net 174 if self.netName is not None: 175 self.net = sumolib.net.readNet(self.netName) 176 return self.net 177 raise RuntimeError("network is unknown") 178 179 def fullPath(self, fileName): 180 print("full >" + os.path.join(self.dataPath, fileName)) 181 return os.path.join(self.dataPath, fileName) 182 183 def sandboxPath(self, fileName): 184 print("sandbox >" + os.path.join(SANDBOX_PATH, fileName)) 185 return os.path.join(SANDBOX_PATH, fileName) 186 187 def getOppositeFlows(self): 188 fNS = [0] * 24 189 fWE = [0] * 24 190 for s in self.demand.streams: 191 if s._departEdgeModel.startswith("em") or s._departEdgeModel.startswith("wm"): 192 fWE[int(s._validFrom / 3600) 193 ] = fWE[int(s._validFrom / 3600)] + s._numberModel 194 elif s._departEdgeModel.startswith("sm") or s._departEdgeModel.startswith("nm"): 195 fNS[int(s._validFrom / 3600) 196 ] = fNS[int(s._validFrom / 3600)] + s._numberModel 197 return (fNS, fWE) 198 199 def getOppositeFlows2(self, ew, sn): 200 fNS = [0] * 24 201 fWE = [0] * 24 202 for s in self.demand.streams: 203 if s._departEdgeModel in ew: 204 fWE[int(s._validFrom / 3600) 205 ] = fWE[int(s._validFrom / 3600)] + s._numberModel 206 else: 207 fNS[int(s._validFrom / 3600) 208 ] = fNS[int(s._validFrom / 3600)] + s._numberModel 209 return (fNS, fWE) 210 211 def buildWAUT(self, streamsNS, streamsWE): 212 # 213 rel = [] 214 ovr = [] 215 program = [0] * len(streamsNS) 216 for i, x in enumerate(streamsNS): 217 s = streamsNS[i] + streamsWE[i] 218 if s != 0: 219 rel.append(x / s) 220 else: 221 rel.append(0) 222 ovr.append(s) 223 # 224 NIGHT = 1 225 DAY = 2 226 MORNING = 3 227 AFTERNOON = 4 228 NIGHT_MAX = 500 229 230 MORNING_MIN = 500 231 AFTERNOON_MIN = 500 232 REL_THRESHOLD = .45 233 IREL_THRESHOLD = 1. - REL_THRESHOLD 234 235 i = 0 236 # streamsNS[i]<NIGHT_MAX and streamsWE[i]<NIGHT_MAX: 237 while i < len(streamsNS) and streamsNS[i] + streamsWE[i] < NIGHT_MAX: 238 program[i] = NIGHT 239 i = i + 1 240 i = len(streamsNS) - 1 241 # and streamsNS[i]<NIGHT_MAX and streamsWE[i]<NIGHT_MAX: 242 while i > 0 and streamsNS[i] + streamsWE[i] < NIGHT_MAX: 243 program[i] = NIGHT 244 i = i - 1 245 i = 0 246 maxIdx, maxVal = maxIndexValue_unset(rel, program) 247 minIdx, minVal = minIndexValue_unset(rel, program) 248 print(program) 249 print("morning max %s %s" % (maxIdx, maxVal)) 250 print("morning min %s %s" % (minIdx, minVal)) 251 print("%s %s" % (maxVal, 1. - minVal)) 252 if maxVal > 1. - minVal: 253 i = maxIdx 254 while i > 0 and rel[i] > IREL_THRESHOLD and program[i] == 0 and streamsNS[i] + streamsWE[i] > MORNING_MIN: 255 program[i] = MORNING 256 i = i - 1 257 i = maxIdx + 1 258 while i < len(rel) and rel[i] > IREL_THRESHOLD and program[ 259 i] == 0 and streamsNS[i] + streamsWE[i] > MORNING_MIN: 260 program[i] = MORNING 261 i = i + 1 262 else: 263 i = minIdx 264 print("!") 265 print(" %s %s %s %s" % 266 (i, rel[i], program[i], streamsNS[i] + streamsWE[i])) 267 while i > 0 and rel[i] < REL_THRESHOLD and program[i] == 0 and streamsNS[i] + streamsWE[i] > MORNING_MIN: 268 program[i] = MORNING 269 i = i - 1 270 print(" %s %s %s %s" % 271 (i, rel[i], program[i], streamsNS[i] + streamsWE[i])) 272 i = minIdx + 1 273 print(" %s %s %s %s" % 274 (i, rel[i], program[i], streamsNS[i] + streamsWE[i])) 275 while i < len(rel) and rel[i] < REL_THRESHOLD and program[ 276 i] == 0 and streamsNS[i] + streamsWE[i] > MORNING_MIN: 277 program[i] = MORNING 278 i = i + 1 279 print(" %s %s %s %s" % 280 (i, rel[i], program[i], streamsNS[i] + streamsWE[i])) 281 282 print(rel) 283 print(program) 284 maxIdx, maxVal = maxIndexValue_unset(rel, program) 285 minIdx, minVal = minIndexValue_unset(rel, program) 286 if maxIdx != -1 and minIdx != -1: 287 print("%s %s" % (maxIdx, maxVal)) 288 print("%s %s" % (minIdx, minVal)) 289 print("%s %s" % (maxVal, 1 - minVal)) 290 if maxVal > 1 - minVal: 291 i = maxIdx 292 # and rel[i]>.6 293 while i > 0 and rel[i] > IREL_THRESHOLD and program[ 294 i] == 0 and streamsNS[i] + streamsWE[i] > AFTERNOON_MIN: 295 program[i] = AFTERNOON 296 i = i - 1 297 i = maxIdx + 1 298 while i < len(rel) and rel[i] > IREL_THRESHOLD and program[ 299 i] == 0 and streamsNS[i] + streamsWE[i] > AFTERNOON_MIN: 300 program[i] = AFTERNOON 301 i = i + 1 302 else: 303 i = minIdx 304 while i > 0 and rel[i] < REL_THRESHOLD and program[ 305 i] == 0 and streamsNS[i] + streamsWE[i] > AFTERNOON_MIN: 306 program[i] = AFTERNOON 307 i = i - 1 308 i = minIdx + 1 309 while i < len(rel) and rel[i] < REL_THRESHOLD and program[ 310 i] == 0 and streamsNS[i] + streamsWE[i] > AFTERNOON_MIN: 311 program[i] = AFTERNOON 312 i = i + 1 313 for i, x in enumerate(program): 314 if program[i] == 0: 315 program[i] = DAY 316 fdo = open("waut.txt", "w") 317 for i, x in enumerate(program): 318 fdo.write("%s %s %s %s %s\n" % 319 (streamsNS[i], streamsWE[i], ovr[i], rel[i], program[i])) 320 fdo.close() 321 # 322 ret1 = {} 323 for i in range(1, 5): 324 maxV = None 325 maxI = 0 326 for j, v in enumerate(rel): 327 if program[j] != i: 328 continue 329 if maxV is None: 330 maxV = rel[j] 331 maxI = j 332 elif maxV < rel[j]: 333 maxV = rel[j] 334 maxI = j 335 elif maxV < 1. - rel[j]: 336 maxV = 1. - rel[j] 337 maxI = j 338 print("%s %s %s" % (i, maxV, maxI)) 339 greens = split_by_proportions( 340 72, (rel[maxI], 1. - rel[maxI]), (10, 10)) 341 ret1[i] = greens 342 print(greens) 343 # build programs 344 ret2 = [] 345 last = -1 346 for j, v in enumerate(program): 347 if last != v: 348 ret2.append((j, v)) 349 last = v 350 return (ret1, ret2)
Scenario(dataPath)
125 def __init__(self, dataPath): 126 self.dataPath = dataPath 127 self.net = None 128 self.netName = None 129 self.demand = None 130 self.demandName = None 131 self.additional = {} 132 self.conn = None 133 self.addAdditionalFile("vtypes") 134 try: 135 os.makedirs(os.path.join(SANDBOX_PATH, self.name)) 136 except OSError: 137 pass
def
writeSUMOConfig(self, cfgName, addOptions={}):
145 def writeSUMOConfig(self, cfgName, addOptions={}): 146 cfg = {} 147 for a in addOptions: 148 cfg[a] = addOptions[a] 149 cfg["net-file"] = self.netName 150 cfg["route-files"] = self.demandName 151 if "vtypes" in self.additional: 152 cfg["additional-files"] = "vtypes.add.xml" 153 for a in self.additional: 154 if a == "vtypes": 155 continue 156 fileName = a + ".add.xml" 157 if len(self.additional[a]) > 0: 158 sumolib.files.additional.write(fileName, self.additional[a]) 159 if "additional-files" not in cfg: 160 cfg["additional-files"] = fileName 161 else: 162 cfg["additional-files"] = cfg["additional-files"] + \ 163 "," + fileName 164 fdo = open(cfgName, "w") 165 fdo.write("<c>\n") 166 for v in cfg: 167 fdo.write(' <%s value="%s"/>\n' % (v, cfg[v])) 168 fdo.write("</c>\n") 169 fdo.close()
def
getOppositeFlows(self):
187 def getOppositeFlows(self): 188 fNS = [0] * 24 189 fWE = [0] * 24 190 for s in self.demand.streams: 191 if s._departEdgeModel.startswith("em") or s._departEdgeModel.startswith("wm"): 192 fWE[int(s._validFrom / 3600) 193 ] = fWE[int(s._validFrom / 3600)] + s._numberModel 194 elif s._departEdgeModel.startswith("sm") or s._departEdgeModel.startswith("nm"): 195 fNS[int(s._validFrom / 3600) 196 ] = fNS[int(s._validFrom / 3600)] + s._numberModel 197 return (fNS, fWE)
def
getOppositeFlows2(self, ew, sn):
199 def getOppositeFlows2(self, ew, sn): 200 fNS = [0] * 24 201 fWE = [0] * 24 202 for s in self.demand.streams: 203 if s._departEdgeModel in ew: 204 fWE[int(s._validFrom / 3600) 205 ] = fWE[int(s._validFrom / 3600)] + s._numberModel 206 else: 207 fNS[int(s._validFrom / 3600) 208 ] = fNS[int(s._validFrom / 3600)] + s._numberModel 209 return (fNS, fWE)
def
buildWAUT(self, streamsNS, streamsWE):
211 def buildWAUT(self, streamsNS, streamsWE): 212 # 213 rel = [] 214 ovr = [] 215 program = [0] * len(streamsNS) 216 for i, x in enumerate(streamsNS): 217 s = streamsNS[i] + streamsWE[i] 218 if s != 0: 219 rel.append(x / s) 220 else: 221 rel.append(0) 222 ovr.append(s) 223 # 224 NIGHT = 1 225 DAY = 2 226 MORNING = 3 227 AFTERNOON = 4 228 NIGHT_MAX = 500 229 230 MORNING_MIN = 500 231 AFTERNOON_MIN = 500 232 REL_THRESHOLD = .45 233 IREL_THRESHOLD = 1. - REL_THRESHOLD 234 235 i = 0 236 # streamsNS[i]<NIGHT_MAX and streamsWE[i]<NIGHT_MAX: 237 while i < len(streamsNS) and streamsNS[i] + streamsWE[i] < NIGHT_MAX: 238 program[i] = NIGHT 239 i = i + 1 240 i = len(streamsNS) - 1 241 # and streamsNS[i]<NIGHT_MAX and streamsWE[i]<NIGHT_MAX: 242 while i > 0 and streamsNS[i] + streamsWE[i] < NIGHT_MAX: 243 program[i] = NIGHT 244 i = i - 1 245 i = 0 246 maxIdx, maxVal = maxIndexValue_unset(rel, program) 247 minIdx, minVal = minIndexValue_unset(rel, program) 248 print(program) 249 print("morning max %s %s" % (maxIdx, maxVal)) 250 print("morning min %s %s" % (minIdx, minVal)) 251 print("%s %s" % (maxVal, 1. - minVal)) 252 if maxVal > 1. - minVal: 253 i = maxIdx 254 while i > 0 and rel[i] > IREL_THRESHOLD and program[i] == 0 and streamsNS[i] + streamsWE[i] > MORNING_MIN: 255 program[i] = MORNING 256 i = i - 1 257 i = maxIdx + 1 258 while i < len(rel) and rel[i] > IREL_THRESHOLD and program[ 259 i] == 0 and streamsNS[i] + streamsWE[i] > MORNING_MIN: 260 program[i] = MORNING 261 i = i + 1 262 else: 263 i = minIdx 264 print("!") 265 print(" %s %s %s %s" % 266 (i, rel[i], program[i], streamsNS[i] + streamsWE[i])) 267 while i > 0 and rel[i] < REL_THRESHOLD and program[i] == 0 and streamsNS[i] + streamsWE[i] > MORNING_MIN: 268 program[i] = MORNING 269 i = i - 1 270 print(" %s %s %s %s" % 271 (i, rel[i], program[i], streamsNS[i] + streamsWE[i])) 272 i = minIdx + 1 273 print(" %s %s %s %s" % 274 (i, rel[i], program[i], streamsNS[i] + streamsWE[i])) 275 while i < len(rel) and rel[i] < REL_THRESHOLD and program[ 276 i] == 0 and streamsNS[i] + streamsWE[i] > MORNING_MIN: 277 program[i] = MORNING 278 i = i + 1 279 print(" %s %s %s %s" % 280 (i, rel[i], program[i], streamsNS[i] + streamsWE[i])) 281 282 print(rel) 283 print(program) 284 maxIdx, maxVal = maxIndexValue_unset(rel, program) 285 minIdx, minVal = minIndexValue_unset(rel, program) 286 if maxIdx != -1 and minIdx != -1: 287 print("%s %s" % (maxIdx, maxVal)) 288 print("%s %s" % (minIdx, minVal)) 289 print("%s %s" % (maxVal, 1 - minVal)) 290 if maxVal > 1 - minVal: 291 i = maxIdx 292 # and rel[i]>.6 293 while i > 0 and rel[i] > IREL_THRESHOLD and program[ 294 i] == 0 and streamsNS[i] + streamsWE[i] > AFTERNOON_MIN: 295 program[i] = AFTERNOON 296 i = i - 1 297 i = maxIdx + 1 298 while i < len(rel) and rel[i] > IREL_THRESHOLD and program[ 299 i] == 0 and streamsNS[i] + streamsWE[i] > AFTERNOON_MIN: 300 program[i] = AFTERNOON 301 i = i + 1 302 else: 303 i = minIdx 304 while i > 0 and rel[i] < REL_THRESHOLD and program[ 305 i] == 0 and streamsNS[i] + streamsWE[i] > AFTERNOON_MIN: 306 program[i] = AFTERNOON 307 i = i - 1 308 i = minIdx + 1 309 while i < len(rel) and rel[i] < REL_THRESHOLD and program[ 310 i] == 0 and streamsNS[i] + streamsWE[i] > AFTERNOON_MIN: 311 program[i] = AFTERNOON 312 i = i + 1 313 for i, x in enumerate(program): 314 if program[i] == 0: 315 program[i] = DAY 316 fdo = open("waut.txt", "w") 317 for i, x in enumerate(program): 318 fdo.write("%s %s %s %s %s\n" % 319 (streamsNS[i], streamsWE[i], ovr[i], rel[i], program[i])) 320 fdo.close() 321 # 322 ret1 = {} 323 for i in range(1, 5): 324 maxV = None 325 maxI = 0 326 for j, v in enumerate(rel): 327 if program[j] != i: 328 continue 329 if maxV is None: 330 maxV = rel[j] 331 maxI = j 332 elif maxV < rel[j]: 333 maxV = rel[j] 334 maxI = j 335 elif maxV < 1. - rel[j]: 336 maxV = 1. - rel[j] 337 maxI = j 338 print("%s %s %s" % (i, maxV, maxI)) 339 greens = split_by_proportions( 340 72, (rel[maxI], 1. - rel[maxI]), (10, 10)) 341 ret1[i] = greens 342 print(greens) 343 # build programs 344 ret2 = [] 345 last = -1 346 for j, v in enumerate(program): 347 if last != v: 348 ret2.append((j, v)) 349 last = v 350 return (ret1, ret2)
def
getScenario(which, useName, params, withDefaultDemand=True):
353def getScenario(which, useName, params, withDefaultDemand=True): 354 if which == "RiLSA1": 355 from . import rilsa1 356 return rilsa1.Scenario_RiLSA1(useName, withDefaultDemand) 357 elif which == "RiLSA1OutTLS": 358 from . import rilsa1_out_tls 359 return rilsa1_out_tls.Scenario_RiLSA1OutTLS(params, withDefaultDemand) 360 elif which == "RiLSA1BothTLS": 361 from . import rilsa1_both_tls 362 return rilsa1_both_tls.Scenario_RiLSA1BothTLS(params, withDefaultDemand) 363 elif which == "RiLSA1OutTLS24": 364 from . import rilsa1_out_tls24 365 return rilsa1_out_tls24.Scenario_RiLSA1OutTLS24(params, withDefaultDemand) 366 elif which == "RiLSA1BothTLS24": 367 from . import rilsa1_both_tls24 368 return rilsa1_both_tls24.Scenario_RiLSA1BothTLS24(params, withDefaultDemand) 369 elif which == "BasicCross": 370 from . import basic_cross 371 return basic_cross.Scenario_BasicCross(useName, withDefaultDemand) 372 elif which == "BasicCrossL": 373 from . import basic_crossl 374 return basic_crossl.Scenario_BasicCrossL(useName, withDefaultDemand) 375 elif which == "BasicCorridor": 376 from . import basic_corridor 377 return basic_corridor.Scenario_BasicCorridor(useName, params["xoff"], withDefaultDemand) 378 elif which == "BasicNet": 379 from . import basic_net 380 return basic_net.Scenario_BasicNet(useName, params["rot"], withDefaultDemand) 381 elif which == "RealWorld": 382 from . import real_world 383 return real_world.Scenario_RealWorld(useName, params["which"], withDefaultDemand) 384 elif which == "BasicRiLSANet": 385 from . import basic_rilsanet 386 return basic_rilsanet.Scenario_BasicRiLSANet(useName, params, withDefaultDemand) 387 elif which == "BasicRiLSANet2x2": 388 from . import basic_rilsanet2x2 389 return basic_rilsanet2x2.Scenario_BasicRiLSANet2x2(useName, params, withDefaultDemand) 390 elif which == "BasicRiLSACorridor3": 391 from . import basic_rilsacorridor3 392 return basic_rilsacorridor3.Scenario_BasicRiLSACorridor3(useName, params, withDefaultDemand) 393 raise RuntimeError("unknown scenario '%s'" % which)