Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
eclipse
GitHub Repository: eclipse/sumo
Path: blob/main/tools/purgatory/outputs.py
169674 views
1
# Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
2
# Copyright (C) 2007-2025 German Aerospace Center (DLR) and others.
3
# This program and the accompanying materials are made available under the
4
# terms of the Eclipse Public License 2.0 which is available at
5
# https://www.eclipse.org/legal/epl-2.0/
6
# This Source Code may also be made available under the following Secondary
7
# Licenses when the conditions for such availability set forth in the Eclipse
8
# Public License 2.0 are satisfied: GNU General Public License, version 2
9
# or later which is available at
10
# https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
11
# SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
12
13
# @file outputs.py
14
# @author Yun-Pang Floetteroed
15
# @author Daniel Krajzewicz
16
# @author Michael Behrisch
17
# @date 2007-12-25
18
19
from __future__ import absolute_import
20
from __future__ import print_function
21
22
import os
23
import random
24
import datetime
25
import operator
26
27
# calculate the time for reading the input data (matrix data are excluded.)
28
29
30
def timeForInput(inputreaderstart):
31
fouttime = open('timeforinput.txt', 'w')
32
inputreadtime = datetime.datetime.now() - inputreaderstart
33
fouttime.write('Time for reading input files:%s\n' % inputreadtime)
34
fouttime.close()
35
36
# output the input matrices, origins, destinations and the number of OD
37
# pairsdemand > 0)
38
39
40
def outputODZone(startVertices, endVertices, Pshort_EffCells, MatrixCounter):
41
foutmatrixstart = open('origins.txt', 'a')
42
foutmatrixend = open('destinations.txt', 'a')
43
foutmatrixstart.write('Interval =%s\n' % (MatrixCounter))
44
foutmatrixstart.write('number of origins=%s\n' % len(startVertices))
45
foutmatrixstart.write(
46
'number of effective OD cells for Passenger vehicles=%s\n' % (Pshort_EffCells))
47
# foutmatrixstart.write('number of effective OD cells for Passenger vehicles(long distances)=%s\n' %(Plong_EffCells))
48
# foutmatrixstart.write('number of effective OD cells for trucks=%s\n' %(Truck_EffCells))
49
for i in range(0, len(startVertices)):
50
foutmatrixstart.write('%s\n' % startVertices[i])
51
52
foutmatrixend.write('number of destinations=%s\n' % len(endVertices))
53
for j in range(0, len(endVertices)):
54
foutmatrixend.write('%s\n' % endVertices[j])
55
56
foutmatrixstart.close()
57
foutmatrixend.close()
58
59
# output the network data which is based on the SUMO-network
60
61
62
def outputNetwork(net):
63
foutnet = open('network.txt', 'w')
64
net.printNet(foutnet)
65
foutnet.close()
66
67
# ouput the required CPU time for the assignment and the assignment
68
# results (e.g. link flows, link travel times)
69
70
71
def outputStatistics(net, starttime, periods):
72
totaltime = 0.0
73
totalflow = 0.0
74
assigntime = datetime.datetime.now() - starttime
75
foutMOE = open('MOE.txt', 'w')
76
foutMOE.write('Number of analyzed periods(hr):%s' % periods)
77
# generate the output of the link travel times
78
for edge in net._edges:
79
if edge.estcapacity > 0.:
80
totaltime += edge.flow * edge.actualtime
81
totalflow += edge.flow
82
foutMOE.write(('\nedge:%s \t from:%s \t to:%s \t freeflowtime(s):%2.2f \t traveltime(s):%2.2f \t ' +
83
'traffic flow(veh):%2.2f \t v/c:%2.2f') % (
84
edge._id, edge._from, edge._to, edge.freeflowtime, edge.actualtime,
85
edge.flow, (edge.flow / edge.estcapacity)))
86
if edge.flow > edge.estcapacity and edge.connection == 0:
87
foutMOE.write('****overflow!')
88
89
avetime = totaltime / totalflow
90
foutMOE.write('\nTotal flow(veh):%2.2f \t average travel time(s):%2.2f\n' % (
91
totalflow, avetime))
92
foutMOE.write(
93
'\nTime for the traffic assignment and reading matrices:%s' % assigntime)
94
foutMOE.close()
95
return assigntime
96
97
# output the releasing time and the route for each vehicle
98
99
100
def sortedVehOutput(vehicles, departtime, options, foutroute):
101
random.seed(42)
102
for veh in vehicles:
103
if veh.depart == 0:
104
veh.depart = random.randint(
105
departtime, departtime + 3600 * options.hours)
106
# sorting by departure times
107
vehicles.sort(key=operator.attrgetter('depart'))
108
# output the generated routes
109
for veh in vehicles:
110
foutroute.write(' <vehicle id="%s" depart="%d" departLane="free">\n' % (
111
veh.label, veh.depart))
112
foutroute.write(' <route>')
113
# for generating vehicle routes used in SUMO
114
for edge in veh.route[1:-1]:
115
foutroute.write('%s ' % edge._id)
116
foutroute.write('</route>\n')
117
foutroute.write(' </vehicle>\n')
118
119
# output the result of the matrix estimation with the traffic counts
120
121
122
def outputMatrix(startVertices, endVertices, estMatrix, daytimeindex):
123
filename = 'estimatedMatri-' + daytimeindex + '.fma'
124
foutmtx = open(filename, 'w')
125
126
foutmtx.write(
127
'$VMR;D2;estimated with the generalized least squares model\n')
128
foutmtx.write('* Verkehrsmittelkennung\n')
129
foutmtx.write(' 1\n')
130
foutmtx.write('* Von Bis\n\n')
131
foutmtx.write('* Faktor\n')
132
foutmtx.write('1.00\n')
133
foutmtx.write('*\n')
134
foutmtx.write('* Deutsches Zentrum fuer Luft- und Raumfahrt e.V.\n')
135
foutmtx.write('* %s\n' % datetime.datetime.now())
136
foutmtx.write('* Anzahl Bezirke\n')
137
foutmtx.write('%s\n' % len(startVertices))
138
foutmtx.write('*\n')
139
for startVertex in startVertices:
140
foutmtx.write('%s ' % startVertex.label)
141
foutmtx.write('\n*')
142
for start, startVertex in enumerate(startVertices):
143
count = -1
144
foutmtx.write('\n* from: %s\n' % startVertex.label)
145
for end, endVertex in enumerate(endVertices):
146
count += 1
147
if operator.mod(count, 12) != 0:
148
foutmtx.write('%s ' % estMatrix[start][end])
149
elif count > 12:
150
foutmtx.write('\n%s ' % estMatrix[start][end])
151
foutmtx.close()
152
153
154
def linkChoicesOutput(net, startVertices, endVertices, matrixPshort, linkChoiceMap, odPairsMap, outputdir, starttime):
155
foutchoice = open(os.path.join(outputdir, "linkchoices.xml"), 'w')
156
print("""<?xml version="1.0"?>
157
<!-- generated on %s by $Id$ -->
158
<edgechoices>""" % starttime, file=foutchoice)
159
for e in net._detectedEdges:
160
if len(linkChoiceMap[e.detected]) > 0:
161
foutchoice.write(
162
' <edge id="%s" flows="%s">\n' % (e.label, e.flow))
163
foutchoice.write(' <choiceprobs>\n')
164
for start, startVertex in enumerate(startVertices):
165
for end, endVertex in enumerate(endVertices):
166
if startVertex.label != endVertex.label and matrixPshort[start][end] > 0.:
167
odIndex = odPairsMap[
168
startVertex.label][endVertex.label]
169
foutchoice.write(' <choice origin="%s" destination="%s" choiceprob="%.5f"/>\n'
170
% (startVertex.label, endVertex.label,
171
linkChoiceMap[e.detected][odIndex] / matrixPshort[start][end]))
172
foutchoice.write(' </choiceprobs>\n')
173
foutchoice.write(' </edge>\n')
174
foutchoice.write('</edgechoices>\n')
175
foutchoice.close()
176
177