Path: blob/main/tools/assign/duaIterate_reroutingAnalysis.py
169673 views
#!/usr/bin/env python1# -*- coding: utf-8 -*-2# Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo3# Copyright (C) 2012-2025 German Aerospace Center (DLR) and others.4# This program and the accompanying materials are made available under the5# terms of the Eclipse Public License 2.0 which is available at6# https://www.eclipse.org/legal/epl-2.0/7# This Source Code may also be made available under the following Secondary8# Licenses when the conditions for such availability set forth in the Eclipse9# Public License 2.0 are satisfied: GNU General Public License, version 210# or later which is available at11# https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html12# SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later1314# @file duaIterate_reroutingAnalysis.py15# @author Jakob Erdmann16# @date 2022-02-281718"""19Extract fraction of vehicles that changed their route in a duaIterate.py run by20parsing the rou.alt.xml files21"""2223from __future__ import absolute_import24from __future__ import print_function25import os26import sys27import glob28sys.path.append(os.path.dirname(os.path.dirname(os.path.realpath(__file__))))29import sumolib # noqa303132def parse_args(args=None):33USAGE = "Usage: " + sys.argv[0] + " [options]"34parser = sumolib.options.ArgumentParser(35usage=USAGE,36description="Extract fraction of vehicles that changed their route per iteration")37parser.add_argument("-d", "--base-directory", dest="baseDir", category="input", default=".", type=str,38help="Base directory of duaIterate run")39parser.add_argument("-v", "--verbose", action="store_true", default=False,40help="Give more details for every iteration")41options = parser.parse_args(args=args)42return options434445def main():46options = parse_args()47lastRoutes = {} # vehID -> edges48files = glob.glob(os.path.join(options.baseDir, "**/*.rou.alt.xml"))49files = [(int(os.path.basename(os.path.dirname(f))), f) for f in files]50for index, (step, file) in enumerate(sorted(files)):51vehs = 0.052changed = []53for veh in sumolib.xml.parse(file, 'vehicle'):54vehs += 155last = int(veh.routeDistribution[0].last)56lastEdges = veh.routeDistribution[0].route[last].edges57if veh.id in lastRoutes and lastRoutes[veh.id] != lastEdges:58changed.append(veh.id)59lastRoutes[veh.id] = lastEdges60numChanged = len(changed)61if options.verbose:62print("file=%s vehs=%s changed=%s frac=%s ids=%s" % (63file, vehs, numChanged, numChanged / vehs, ' '.join(changed)))6465if index > 0 and not options.verbose:66if vehs == 0:67print("no vehicles in file '%s'" % file)68else:69print(numChanged / vehs)707172##################73if __name__ == "__main__":74main()757677