File: GenerateTaxiRoutes.py

package info (click to toggle)
sumo 0.28.0+dfsg1-1~bpo8+1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-backports
  • size: 103,616 kB
  • sloc: xml: 534,021; cpp: 183,697; python: 66,271; java: 43,017; ansic: 36,466; sh: 11,391; makefile: 1,411; perl: 450
file content (240 lines) | stat: -rw-r--r-- 8,135 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
#!/usr/bin/env python
# -*- coding: Latin-1 -*-
"""
@file    GenerateTaxiRoutes.py
@author  Sascha Krieg
@author  Daniel Krajzewicz
@author  Michael Behrisch
@date    2008-04-07
@version $Id: GenerateTaxiRoutes.py 20433 2016-04-13 08:00:14Z behrisch $

Creates an Route-File, which contains for each Taxi the route, from an FCD-File.

SUMO, Simulation of Urban MObility; see http://sumo.dlr.de/
Copyright (C) 2008-2016 DLR (http://www.dlr.de/) and contributors

This file is part of SUMO.
SUMO is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
"""
from __future__ import absolute_import
from __future__ import print_function

from util.CalcTime import getTimeInSecs
from util.CalcTime import getNiceTimeLabel
import util.Path as path
import util.Reader as reader
from cPickle import load

#global vars
taxis = []
routes = []
vlsEdges = []
# contains for each Taxi the actual "TaxiId" based on the number of single
# routes which are created for them
taxiIdDict = {}
fcdDict = {}
vehIdListKeys = []
vehIdListValues = []


def getTaxiId(taxi):

    return "%s_%s" % (taxi, taxiIdDict.setdefault(taxi, 0))


def getSimTaxiId(taxi):
    global vehIdListKeys, vehIdListValues

    if len(vehIdListKeys) < 1:
        vehIdList = load(open(path.rawFcdVehIdList, 'r'))
        vehIdListKeys = vehIdList.keys()
        vehIdListValues = vehIdList.values()

    return "%s" % (vehIdListKeys[vehIdListValues.index(int(taxi))])


def readFCD():
    """Reads the FCD and creates a list of Taxis and for each a list of routes"""
    vlsEdges = reader.readVLS_Edges()

    inputFile = open(path.fcd, 'r')
    for line in inputFile:
        words = line.split("\t")
        # add route
        taxiId = getTaxiId(words[4])
        actTime = getTimeInSecs(words[0])
        if taxiId in taxis:
            prevTime = routes[taxis.index(taxiId)][-1][0]
            # check if time lies not to far away from each other
            if words[1] in vlsEdges and (actTime - prevTime) < 180:
                routes[taxis.index(taxiId)].append((actTime, words[1]))
            # if time diff >3min add a new taxiId and start a new route
            elif words[1] in vlsEdges:
                taxiIdDict[words[4]] += 1  # create new taxiId
                taxis.append(getTaxiId(words[4]))  # append new created id
                # append new list (list will be filled with edges)
                routes.append([(actTime, words[1])])
            else:
                taxiIdDict[words[4]] += 1
        # if the edge is in the VLS-Area a new route is created
        elif words[1] in vlsEdges:
            taxis.append(taxiId)
            #                 departTime
            routes.append([(actTime, words[1])])

    inputFile.close()
    print(len(taxis))


def readFCDOLD():
    """Reads the FCD and creates a list of Taxis and for each a list of routes"""
    vlsEdges = reader.readVLS_Edges()

    inputFile = open(path.fcd, 'r')
    for line in inputFile:
        words = line.split("\t")
        # add route
        taxiId = getTaxiId(words[4])
        if taxiId in taxis:
            if words[1] in vlsEdges:
                routes[taxis.index(taxiId)].append(words[1])
            else:
                taxiIdDict[words[4]] += 1
        # if the edge is in the VLS-Area a new route is created
        elif words[1] in vlsEdges:
            taxis.append(taxiId)
            #                 departTime
            routes.append([getTimeInSecs(words[0]), words[1]])

    inputFile.close()
    print(len(taxis))


def readFCDCompleteOLD(fcdPath):
    """Reads the FCD-File and creates a list of Id's with a belonging List of Data tuples."""
    # reset all
    global taxis, routes, vlsEdges, taxiIdDict, fcdDict
    taxis = []
    routes = []
    vlsEdges = []
    taxiIdDict = {}
    fcdDict = {}

    vlsEdges = reader.readVLS_Edges()

    inputFile = open(fcdPath, 'r')
    for line in inputFile:
        words = line.split("\t")
        # add route
        taxiId = getTaxiId(words[4])
        if taxiId in taxis:
            if words[1] in vlsEdges:
                # routes[taxis.index(taxiId)].append(words[1])
                fcdDict[taxiId].append(
                    (getTimeInSecs(words[0]), words[1], words[2]))
            else:
                taxiIdDict[words[4]] += 1
        # if the edge is in the VLS-Area a new route is created
        elif words[1] in vlsEdges:
            taxis.append(taxiId)
            #                 departTime
            # routes.append([(int)(mktime(strptime(words[0],format))-simDate),words[1]])
            fcdDict[taxiId] = [(getTimeInSecs(words[0]), words[1], words[2])]

    inputFile.close()
    return fcdDict


def readFCDComplete(fcdPath):
    """Reads the FCD and creates a list of Taxis and for each a list of routes"""
    # reset all
    global taxis, routes, vlsEdges, taxiIdDict, fcdDict
    taxis = []
    routes = []
    vlsEdges = []
    taxiIdDict = {}
    fcdDict = {}

    vlsEdges = reader.readVLS_Edges()

    inputFile = open(path.fcd, 'r')
    for line in inputFile:
        words = line.split("\t")
        # add route
        taxiId = getTaxiId(words[4])
        actTime = getTimeInSecs(words[0])

        if taxiId in taxis:
            # prevTime=routes[taxis.index(taxiId)][-1][0]
            prevTime = fcdDict[taxiId][-1][0]
            # check if time lies not to far away from each other
            if words[1] in vlsEdges and (actTime - prevTime) < 180:
                #routes[taxis.index(taxiId)].append((actTime, words[1]))
                fcdDict[taxiId].append((actTime, words[1], words[2]))
            # if time diff >3min add a new taxiId and start a new route
            elif words[1] in vlsEdges:
                taxiIdDict[words[4]] += 1  # create new taxiId
                taxis.append(getTaxiId(words[4]))  # append new created id
                # append new list (list will be filled with edges)
                fcdDict[getTaxiId(words[4])] = [(actTime, words[1], words[2])]
            else:
                taxiIdDict[words[4]] += 1
        # if the edge is in the VLS-Area a new route is created
        elif words[1] in vlsEdges:
            taxis.append(taxiId)
            #                 departTime
            # routes.append([(actTime,words[1])])
            fcdDict[taxiId] = [(actTime, words[1], words[2])]
    inputFile.close()
    return fcdDict


def readSimFCDComplete(fcdPath):
    """Reads the FCD-File and creates a list of Id's with a belonging List of Data tuples. Uses the given taxiIds."""
    # reset all
    global taxis, routes, vlsEdges, taxiIdDict, fcdDict
    taxis = []
    routes = []
    vlsEdges = []
    taxiIdDict = {}
    fcdDict = {}

    inputFile = open(fcdPath, 'r')
    for line in inputFile:
        words = line.split("\t")
        # add route
        taxiId = getSimTaxiId(words[4])
        if taxiId in taxis:
            fcdDict[taxiId].append(
                (getTimeInSecs(getNiceTimeLabel(words[0])), words[1], words[2]))
        else:
            taxis.append(taxiId)
            fcdDict[taxiId] = [
                (getTimeInSecs(getNiceTimeLabel(words[0])), words[1], words[2])]
    inputFile.close()
    return fcdDict


def writeRoutes():
    """Writes the collected values in a Sumo-Routes-File"""
    outputFile = open(path.taxiRoutes, 'w')

    outputFile.write("<routes>\n")
    # known for like used in java
    for i in xrange(len(taxis)):
        if len(routes[i]) > 3:
            outputFile.write("\t<vehicle id=\"" + taxis[i] + "\" type=\"taxi\" depart=\"" + str(
                routes[i][0][0]) + "\" color=\"1,0,0\">\n")
            outputFile.write("\t\t<route>")
            for time, edge in routes[i]:
                outputFile.write(edge + " ")
            # delete the space between the last edge and </route>
            outputFile.seek(-1, 1)
            outputFile.write("</route>\n")
            outputFile.write("\t</vehicle>\n")

    outputFile.write("</routes>")
    outputFile.close()