Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
eclipse
GitHub Repository: eclipse/sumo
Path: blob/main/tools/assign/duaIterate_reroutingAnalysis.py
169673 views
1
#!/usr/bin/env python
2
# -*- coding: utf-8 -*-
3
# Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
4
# Copyright (C) 2012-2025 German Aerospace Center (DLR) and others.
5
# This program and the accompanying materials are made available under the
6
# terms of the Eclipse Public License 2.0 which is available at
7
# https://www.eclipse.org/legal/epl-2.0/
8
# This Source Code may also be made available under the following Secondary
9
# Licenses when the conditions for such availability set forth in the Eclipse
10
# Public License 2.0 are satisfied: GNU General Public License, version 2
11
# or later which is available at
12
# https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
13
# SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
14
15
# @file duaIterate_reroutingAnalysis.py
16
# @author Jakob Erdmann
17
# @date 2022-02-28
18
19
"""
20
Extract fraction of vehicles that changed their route in a duaIterate.py run by
21
parsing the rou.alt.xml files
22
"""
23
24
from __future__ import absolute_import
25
from __future__ import print_function
26
import os
27
import sys
28
import glob
29
sys.path.append(os.path.dirname(os.path.dirname(os.path.realpath(__file__))))
30
import sumolib # noqa
31
32
33
def parse_args(args=None):
34
USAGE = "Usage: " + sys.argv[0] + " [options]"
35
parser = sumolib.options.ArgumentParser(
36
usage=USAGE,
37
description="Extract fraction of vehicles that changed their route per iteration")
38
parser.add_argument("-d", "--base-directory", dest="baseDir", category="input", default=".", type=str,
39
help="Base directory of duaIterate run")
40
parser.add_argument("-v", "--verbose", action="store_true", default=False,
41
help="Give more details for every iteration")
42
options = parser.parse_args(args=args)
43
return options
44
45
46
def main():
47
options = parse_args()
48
lastRoutes = {} # vehID -> edges
49
files = glob.glob(os.path.join(options.baseDir, "**/*.rou.alt.xml"))
50
files = [(int(os.path.basename(os.path.dirname(f))), f) for f in files]
51
for index, (step, file) in enumerate(sorted(files)):
52
vehs = 0.0
53
changed = []
54
for veh in sumolib.xml.parse(file, 'vehicle'):
55
vehs += 1
56
last = int(veh.routeDistribution[0].last)
57
lastEdges = veh.routeDistribution[0].route[last].edges
58
if veh.id in lastRoutes and lastRoutes[veh.id] != lastEdges:
59
changed.append(veh.id)
60
lastRoutes[veh.id] = lastEdges
61
numChanged = len(changed)
62
if options.verbose:
63
print("file=%s vehs=%s changed=%s frac=%s ids=%s" % (
64
file, vehs, numChanged, numChanged / vehs, ' '.join(changed)))
65
66
if index > 0 and not options.verbose:
67
if vehs == 0:
68
print("no vehicles in file '%s'" % file)
69
else:
70
print(numChanged / vehs)
71
72
73
##################
74
if __name__ == "__main__":
75
main()
76
77