Path: blob/main/tests/complex/tutorial/san_pablo_dam/validate.py
169685 views
# Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo1# Copyright (C) 2008-2025 German Aerospace Center (DLR) and others.2# This program and the accompanying materials are made available under the3# terms of the Eclipse Public License 2.0 which is available at4# https://www.eclipse.org/legal/epl-2.0/5# This Source Code may also be made available under the following Secondary6# Licenses when the conditions for such availability set forth in the Eclipse7# Public License 2.0 are satisfied: GNU General Public License, version 28# or later which is available at9# https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html10# SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later1112# @file validate.py13# @author Michael Behrisch14# @date 2012-01-211516from __future__ import absolute_import17from __future__ import print_function1819import sys20import math21import subprocess2223dDay = 124obsTimes = {}25verbose = False262728def readTimes(obsfile):29times = []30with open(obsfile) as ifile:31for line in ifile:32ll = line.split(':')33if ll:34times.append(353600 * int(ll[0]) + 60 * int(ll[1]) + int(float(ll[2])))36return times373839def parseObsTimes():40for i in range(0, 9):41obsTimes[i] = []42for i in range(1, 8):43if dDay == 1 and i == 5:44continue45if dDay == 2 and i == 6:46continue47obsTimes[i] = readTimes('data/obstimes_%s_%s.txt' % (dDay, i))4849# convert obsTimes[][] into travel-times:50for i in range(1, 8):51ni = len(obsTimes[i])52if ni == len(obsTimes[i + 1]) and ni > 100:53for j in range(ni):54obsTimes[i][j] = obsTimes[i + 1][j] - obsTimes[i][j]555657def validate(sumoBinary):58subprocess.call(59[sumoBinary, "-c", "data/spd-road.sumocfg"], stdout=sys.stdout, stderr=sys.stderr)60sys.stdout.flush()61sys.stderr.flush()6263# analyzing the results...64# read the empirical times65simTimes = {}66for i in range(0, 9):67simTimes[i] = []6869# read the simulated times70obs2Nr = {'obs1': 1, 'obs2': 2, 'obs3': 3,71'obs4': 4, 'obs5': 5, 'obs6': 6, 'obs7': 7}7273with open('data/detector.xml') as ifile:74for line in ifile:75if line.find('<interval') != -1:76ll = line.split('"')77iObs = obs2Nr[ll[5]]78if int(ll[7]) > 0:79simTimes[iObs].append(float(ll[1]))8081# convert simTimes[][] into travel-times:82for i in range(1, 8):83ni = len(simTimes[i])84if ni == len(simTimes[i + 1]) and ni > 100:85for j in range(ni):86simTimes[i][j] = simTimes[i + 1][j] - simTimes[i][j]8788# compute final statistics89err = [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]90errAll = 0.091cntAll = 092if verbose:93f = open('data/sumo-obs-error.txt', 'w')94for i in range(1, 7):95if len(obsTimes[i]) <= 100 or len(obsTimes[i + 1]) <= 100:96continue97if len(obsTimes[i]) == len(simTimes[i]):98tmp = 0.099for o, s in zip(obsTimes[i], simTimes[i]):100d = o - s101tmp += d * d102err[i] = math.sqrt(tmp / len(obsTimes[i]))103if verbose:104print("%s %s" % (i, err[i]), file=f)105errAll += err[i]106cntAll += 1107if verbose:108f.close()109110# finally, write the individual travel times into a csv-file111# this is not really needed when validate is called from calibrate as an intermediate112# step, but it makes analyzing of the result more simple.113# first the header114if verbose:115c = open('data/compare-tt.csv', 'w')116c.write('# indx;')117for i in range(1, 7):118if len(obsTimes[i]) > 100 and len(obsTimes[i + 1]) > 100:119c.write('obs%s;sim%s;' % (i, i))120c.write('\n')121122# then the data, of course on the ones which are useable123for line in range(len(simTimes[1])):124c.write(repr(line) + ';')125for i in range(1, 7):126if len(obsTimes[i]) > 100 and len(obsTimes[i + 1]) > 100:127ttObs = int(obsTimes[i][line])128ttSim = int(simTimes[i][line])129c.write(repr(ttObs) + ';' + repr(ttSim) + ';')130c.write('\n')131c.close()132return errAll / cntAll133134135