Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
eclipse
GitHub Repository: eclipse/sumo
Path: blob/main/tools/route/splitRandom.py
169673 views
1
#!/usr/bin/env python
2
# Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3
# Copyright (C) 2010-2025 German Aerospace Center (DLR) and others.
4
# This program and the accompanying materials are made available under the
5
# terms of the Eclipse Public License 2.0 which is available at
6
# https://www.eclipse.org/legal/epl-2.0/
7
# This Source Code may also be made available under the following Secondary
8
# Licenses when the conditions for such availability set forth in the Eclipse
9
# Public License 2.0 are satisfied: GNU General Public License, version 2
10
# or later which is available at
11
# https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
12
# SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
13
14
# @file splitRandom.py
15
# @author Pablo Alvarez Lopez
16
# @date 2021-03-09
17
18
"""
19
splits a route file randomly in two parts
20
"""
21
22
from __future__ import print_function
23
from __future__ import absolute_import
24
import os
25
import sys
26
import random
27
28
# (seed)
29
30
if 'SUMO_HOME' in os.environ:
31
tools = os.path.join(os.environ['SUMO_HOME'], 'tools')
32
sys.path.append(tools)
33
else:
34
sys.exit("please declare environment variable 'SUMO_HOME'")
35
36
import sumolib # noqa
37
from sumolib.options import ArgumentParser # noqa
38
39
40
def get_options(args=None):
41
ap = ArgumentParser()
42
ap.add_argument("-r", "--route-file", dest="routefile", category="input", type=ap.route_file,
43
help="define the input route file with trips or vehicles")
44
ap.add_argument("-n", "--number", dest="number", category="input", type=int,
45
help="number of trips/vehicles to split (not together with percent)")
46
ap.add_argument("-p", "--percent", dest="percent", category="input", type=float,
47
help="percent of trips/vehicles to split (not together with number")
48
ap.add_argument("-a", "--output-file-a", dest="outputA", default="tripsA.rou.xml",
49
category="output", type=ap.route_file, help="define the first output route file")
50
ap.add_argument("-b", "--output-file-b", dest="outputB", default="tripsB.rou.xml",
51
category="output", type=ap.route_file, help="define the second output route file")
52
ap.add_argument("--random", action="store_true", default=False, category="random",
53
help="use a random seed to initialize the random number generator")
54
ap.add_argument("-s", "--seed", type=int, default=42, category="random",
55
help="random seed")
56
options = ap.parse_args(args=args)
57
if not options.routefile or not (options.number or options.percent) or (options.number and options.percent):
58
ap.print_help()
59
sys.exit()
60
return options
61
62
63
def main(options):
64
if not options.random:
65
random.seed(options.seed)
66
infile = options.routefile
67
# copy all trips into an array
68
tripsArray = list(sumolib.xml.parse(infile, ["trip", "vehicle", "flow"]))
69
# declare range [0, numTrips]
70
tripsRange = range(0, len(tripsArray))
71
# declare num of vehicles
72
numVehicles = 0
73
# set num vehicles depending of options or number
74
if (options.number):
75
numVehicles = int(options.number)
76
elif (options.percent):
77
numVehicles = int(len(tripsArray) * int(options.percent) / 100.0)
78
# randomSample
79
if (numVehicles < len(tripsArray)):
80
randomSample = random.sample(tripsRange, numVehicles)
81
else:
82
randomSample = tripsRange
83
# separate in two groups
84
tripsArrayA = []
85
tripsArrayB = []
86
# declare index
87
index = 0
88
# iterate over randomSample
89
for trip in tripsArray:
90
# if index is in randomSample, append in B, if not, append in A
91
if index in randomSample:
92
tripsArrayB.append(trip)
93
else:
94
tripsArrayA.append(trip)
95
# update index
96
index += 1
97
# write trips A
98
for fname, trips in [
99
(options.outputA, tripsArrayA),
100
(options.outputB, tripsArrayB)]:
101
with open(fname, 'w') as outf:
102
# write header
103
sumolib.writeXMLHeader(outf, "$Id$", "routes") # noqa
104
# iterate over trips
105
for trip in trips:
106
# write trip
107
outf.write(trip.toXML(initialIndent=" "))
108
# close route tag
109
outf.write("</routes>\n")
110
111
112
if __name__ == "__main__":
113
options = get_options(sys.argv[1:])
114
main(options)
115
116