Path: blob/main/tests/complex/tutorial/san_pablo_dam/runner.py
169685 views
#!/usr/bin/env python1# Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo2# Copyright (C) 2008-2025 German Aerospace Center (DLR) and others.3# This program and the accompanying materials are made available under the4# terms of the Eclipse Public License 2.0 which is available at5# https://www.eclipse.org/legal/epl-2.0/6# This Source Code may also be made available under the following Secondary7# Licenses when the conditions for such availability set forth in the Eclipse8# Public License 2.0 are satisfied: GNU General Public License, version 29# or later which is available at10# https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html11# SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later1213# @file runner.py14# @author Daniel Krajzewicz15# @author Michael Behrisch16# @date 2007-10-251718from __future__ import absolute_import19from __future__ import print_function2021import os22import subprocess23import sys24import shutil25from scipy.optimize import fmin_cobyla2627if 'SUMO_HOME' in os.environ:28tools = os.path.join(os.environ['SUMO_HOME'], 'tools')29sys.path.append(tools)30else:31sys.exit("please declare environment variable 'SUMO_HOME'")32from sumolib import checkBinary # noqa33import validate # noqa343536def buildVSS(obs7file, obs8file, vss):37t7Times = validate.readTimes(obs7file)38t8Times = validate.readTimes(obs8file)39print('data read: ', len(t7Times), len(t8Times))4041fp = open(vss, 'w')42lObs8 = 337.543print('<vss>', file=fp)44for i, t7 in enumerate(t7Times):45v = lObs8 / (t8Times[i] - t7)46if i != len(t7Times) - 1 and t7 != t7Times[i + 1]:47print(' <step time ="%s" speed="%s"/>' % (t7, v), file=fp)48print('</vss>', file=fp)49fp.close()505152def genDemand(inputFile, outputFile):53t1Times = validate.readTimes(inputFile)54fRou = open(outputFile, 'w')55fRou.write('<routes>\n')56fRou.write(' <route id="route01" edges="1to7 7to8"/>\n')57for vehID, t in enumerate(t1Times):58print(' <vehicle depart="%s" arrivalPos="-1" id="%s" route="route01" type="pass" departSpeed="max" />' % (59t, vehID), file=fRou)60print('</routes>', file=fRou)61fRou.close()6263# definition of gof() function to be given to fmin_cobyla() or fmin()646566def gof(p):67para = [('vMax', p[0]),68('aMax', p[1]),69('bMax', p[2]),70('lCar', p[3]),71('sigA', max(0, min(1, p[4]))),72('tTau', p[5])]73print('# simulation with:', *["%s:%.3f" % i for i in para])74fType = open('data/input_types.add.xml', 'w')75fType.write(('<routes>\n <vType accel="%(aMax)s" decel="%(bMax)s" id="pass"' +76' length="%(lCar)s" minGap="2.5" maxSpeed="%(vMax)s"' +77' sigma="%(sigA)s" tau="%(tTau)s" />\n</routes>') % dict(para))78fType.close()79result = validate.validate(checkBinary('sumo'))80print('#### yields rmse: %.4f' % result)81print("%s %s" % (" ".join(["%.3f" % pe for pe in p]), result), file=fpLog)82fpLog.flush()83return result8485# defining all the constraints868788def conVmax(params): # vMax < 2589return 25.0 - params[0]909192def conTtau(params): # tTau > 1.193return params[5] - 1.1949596def conSigA(params): # sigA > 0.197return params[4] - 0.19899100def conSigA2(params): # sigA < 1.0101return 1.0 - params[4]102103104def conAmax(params): # aMax > 0.1105return params[1] - 0.1106107108netconvertBinary = checkBinary('netconvert')109# build/check network110retcode = subprocess.call([netconvertBinary, "-n", "data/spd-road.nod.xml", "-e",111"data/spd-road.edg.xml", "-o", "data/spd-road.net.xml", "-v"],112stdout=sys.stdout, stderr=sys.stderr)113try:114shutil.copy("data/spd-road.net.xml", "net.net.xml")115except IOError:116print("Missing 'spd-road.net.xml'")117print(">> Netbuilding closed with status %s" % retcode)118sys.stdout.flush()119# build/check vss120buildVSS('data/obstimes_1_7.txt',121'data/obstimes_1_8.txt', 'data/spd-road.vss.xml')122shutil.copy("data/spd-road.vss.xml", "vss.xml")123sys.stdout.flush()124genDemand('data/obstimes_1_1.txt', 'data/spd-road.rou.xml')125validate.parseObsTimes()126# perform calibration127fpLog = open('results.csv', 'w')128params = [22.0, 2.0, 2.0, 5.0, 0.5, 1.5]129# call to (unconstrained) Nelder Mead; does not work correctly, because130# method very often stumples over unrealistic input parameters (like tTau<1),131# which causes SUMO to behave strangely.132# fmin(gof, params)133fmin_cobyla(134gof, params, [conVmax, conAmax, conTtau, conSigA, conSigA2], rhoend=1.0e-4)135fpLog.close()136137138