Path: blob/main/tests/complex/tutorial/city_mobil/data/statistics.py
169708 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 statistics.py14# @author Michael Behrisch15# @author Daniel Krajzewicz16# @date 2008-10-171718from __future__ import absolute_import19from __future__ import print_function20import sys21persons = {}22personsRunning = 0232425class Person:2627def __init__(self, id, source, target, step):28self.id = id29self.source = source30self.target = target31self.waitStart = step32self.depart = None33self.arrive = None343536def personArrived(personID, edge, target, step):37global personsRunning38persons[personID] = Person(personID, edge, target, step)39personsRunning += 1404142def personLoaded(personID, step):43persons[personID].depart = step444546def personUnloaded(personID, step):47global personsRunning48persons[personID].arrive = step49personsRunning -= 1505152def evaluate(forTest=False):53try:54import numpy55import math56except ImportError:57print("No numpy available, skipping statistics")58return59waitTimes = []60routeTimes = {}61for person in persons.values():62waitTimes.append(person.depart - person.waitStart)63route = (person.source, person.target)64if route not in routeTimes:65routeTimes[route] = []66routeTimes[route].append(person.arrive - person.depart)67waitArray = numpy.array(waitTimes)68if forTest:69print("waiting time (max, mean, dev):", waitArray.max() < 1000,70waitArray.mean() < 1000, math.sqrt(waitArray.var()) < 200)71else:72print("waiting time (max, mean, dev):", waitArray.max(),73waitArray.mean(), math.sqrt(waitArray.var()))7475for route, times in sorted(routeTimes.items()):76timeArray = numpy.array(times)77if forTest:78print(route, timeArray.max() < 1000, timeArray.mean() < 1000, math.sqrt(timeArray.var()) < 200)79else:80print(route, timeArray.max(), timeArray.mean(), math.sqrt(timeArray.var()))8182co2 = 0.83for line in open("aggregated.xml"):84if "cyber" in line:85pos = line.find('CO2_abs="') + 986if pos >= 9:87endpos = line.find('"', pos)88co2 += float(line[pos:endpos])8990if forTest:91print("CO2:", co2 < 10000000)92else:93print("CO2:", co2)949596if __name__ == "__main__":97from pylab import figure, errorbar, legend, savefig, show, title, xlabel, xlim, ylabel, ylim98stats = open(sys.argv[1])99demand = []100simpleWaitMean = []101agentWaitMean = []102simpleWaitDev = []103agentWaitDev = []104simpleRouteMean = []105agentRouteMean = []106simpleRouteDev = []107agentRouteDev = []108for line in stats:109if "simple" in line:110mean = simpleWaitMean111dev = simpleWaitDev112rmean = simpleRouteMean113rdev = simpleRouteDev114demand.append(int(line.split()[-1]))115if "agent" in line:116mean = agentWaitMean117dev = agentWaitDev118rmean = agentRouteMean119rdev = agentRouteDev120if "waiting" in line:121mean.append(float(line.split()[-2]))122dev.append(float(line.split()[-1]))123if line.startswith("('footmain0to1'"):124rmean.append(float(line.split()[-2]))125rdev.append(float(line.split()[-1]))126stats.close()127figure()128errorbar(demand, simpleWaitMean, simpleWaitDev, lw=2,129ms=10, fmt='o', label='standard bus scenario')130errorbar(demand, agentWaitMean, agentWaitDev, lw=2, ms=10,131color="red", fmt='o', label='agent controlled cyber cars')132xlim(0, 50)133ylim(0, 3300)134xlabel('Repeater interval (s)')135ylabel('Waiting time (s)')136title('Mean and standard deviation of waiting time')137legend(numpoints=1)138savefig("waitingtime.png")139figure()140errorbar(demand, simpleRouteMean, simpleRouteDev, lw=2,141ms=10, fmt='o', label='standard bus scenario')142errorbar(demand, agentRouteMean, agentRouteDev, lw=2, ms=10,143color="red", fmt='o', label='agent controlled cyber cars')144xlim(0, 50)145ylim(0, 300)146xlabel('Repeater interval (s)')147ylabel('Travel time (s)')148title('Mean and standard deviation of travel time on the longest route')149legend(numpoints=1)150savefig("traveltime.png")151show()152153154