-
Notifications
You must be signed in to change notification settings - Fork 5
/
Graph.py
374 lines (302 loc) · 15.2 KB
/
Graph.py
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
'''
Created on Apr 18, 2014
@author: jeromethai
'''
from cvxopt import matrix
import numpy as np
import logging
class Graph:
"""Class Graph containing nodes, links, ODs, paths for traffic assignment"""
def __init__(self, description=None):
self.description = description
self.nodes = {}
self.links = {}
self.ODs = {}
self.paths = {}
self.numnodes = 0
self.numlinks = 0
self.numODs = 0
self.numpaths = 0
self.nodes_position = {}
self.indlinks = {} # indexation for matrix generations
self.indods = {} # indexation for matrix generations
self.indpaths = {} # indexation for matrix generations
def add_node(self, position=None):
"""Add a node with coordinates as a tuple"""
self.numnodes += 1
self.nodes_position[self.numnodes] = position
self.nodes[self.numnodes] = Node(position)
def add_nodes_from_list(self, list):
"""Add nodes from list of positions"""
for position in list: self.add_node(position)
def add_link(self, startnode, endnode, route=1, flow=0.0, delay=0.0, ffdelay=0.0, delayfunc=None):
"""Add a link"""
formatStr = 'ERROR: node {} doesn\'t exist, graph countains {} nodes.'
startnode, endnode, route = int(startnode), int(endnode), int(route)
if startnode == endnode: logging.error('self-loop not allowed.'); return
if startnode < 1 or startnode > self.numnodes: logging.info(formatStr.format(startnode, self.numnodes)); return
if endnode < 1 or endnode > self.numnodes: logging.info(formatStr.format(endnode, self.numnodes)); return
if (startnode, endnode, route) in self.links:
logging.error('link ({},{},{}) already exists.'.format(startnode, endnode, route)); return
else:
link = Link(startnode, endnode, route, float(flow), float(delay), float(ffdelay), delayfunc)
self.indlinks[(startnode, endnode, route)] = self.numlinks
self.numlinks += 1
self.links[(startnode, endnode, route)] = link
self.nodes[startnode].outlinks[(startnode, endnode, route)] = link
self.nodes[endnode].inlinks[(startnode, endnode, route)] = link
if delayfunc is not None:
link.ffdelay = delayfunc.ffdelay
link.delay = delayfunc.compute_delay(link.flow)
def add_links_from_list(self, list, delaytype):
"""Add links from list
the list is must contain starnode, endnode, ffdelay, and parameters of delay functions
"""
for startnode, endnode, route, ffdelay, parameters in list:
self.add_link(startnode, endnode, route, 0.0, ffdelay, ffdelay, delayfunc=create_delayfunc(delaytype, parameters))
def add_od(self, origin, destination, flow=0.0):
"""Add an OD pair"""
formatStr = 'ERROR: node {} doesn\'t exist, graph countains {} nodes.'
origin, destination = int(origin), int(destination)
if origin == destination: logging.error('self-loop not allowed.'); return
if origin < 1 or origin > self.numnodes: logging.info(formatStr.format(origin, self.numnodes)); return
if destination < 1 or destination > self.numnodes: logging.info(formatStr.format(destination, self.numnodes)); return
if (origin, destination) in self.ODs:
logging.error('OD ({},{}) already exists'.format(origin, destination)); return
else:
self.indods[(origin, destination)] = self.numODs
self.numODs += 1
od = OD(origin, destination, float(flow))
self.ODs[(origin, destination)] = od
self.nodes[origin].startODs[(origin, destination)] = od
self.nodes[destination].endODs[(origin, destination)] = od
def add_ods_from_list(self, list):
"""Add OD's from list
the list is must contain origin, destimation, ffdelay, flow
"""
for origin, destination, flow in list: self.add_od(origin, destination, flow)
def add_path(self, link_ids):
"""Add a path with link_ids a list of link ids"""
origin = link_ids[0][0]
destination = link_ids[len(link_ids)-1][1]
if not (origin, destination) in self.ODs: logging.error('OD ({},{}) doesn\'t exist.'.format(origin, destination)); return
for i in range(len(link_ids)-1):
if link_ids[i][1] != link_ids[i+1][0]: logging.error('path not valid.'); return
for path in self.ODs[(origin, destination)].paths.values():
if [(link.startnode,link.endnode,link.route) for link in path.links] == link_ids: logging.error('path already exists.'); return
links = []; delay = 0.0; ffdelay = 0.0
for id in link_ids:
link = self.links[(id[0], id[1], id[2])]; links.append(link); delay += link.delay; ffdelay += link.ffdelay
self.ODs[(origin, destination)].numpaths += 1
route = self.ODs[(origin, destination)].numpaths
path = Path(origin, destination, route, links, 0.0, delay, ffdelay)
self.indpaths[(origin, destination, route)] = self.numpaths
self.numpaths += 1
self.paths[(origin, destination, route)] = path
self.ODs[(origin, destination)].paths[(origin, destination, route)] = path
for link in links:
self.links[(link.startnode, link.endnode, link.route)].numpaths += 1
self.links[(link.startnode, link.endnode, link.route)].paths[(origin, destination, route)] = path
def add_path_from_nodes(self, node_ids):
"""Add a path from a list of nodes"""
link_ids = [(node_ids[k], node_ids[k+1], 1) for k in range(len(node_ids)-1)]
self.add_path(link_ids)
def visualize(self, general=True, nodes=False, links=False, ODs=False, paths=False, only_pos_flows=False, tol=1e-3):
"""Visualize graph"""
if general:
logging.info('Description: ', self.description)
#print 'Nodes: ', self.nodes
logging.info('Number of nodes: ', self.numnodes)
#print 'Links: ', self.links
logging.info('Number of links: ', self.numlinks)
#print 'OD pairs: ', self.ODs
logging.info('Number of OD pairs: ', self.numODs)
#print 'Paths: ', self.paths
logging.info('Number of paths: ', self.numpaths)
#print 'Nodes\' position: ', self.nodes_position
#print 'Link indexation', self.indlinks
#print 'OD indexation', self.indods
#print 'Path indexation', self.indpaths
if nodes:
for id, node in self.nodes.items():
logging.info('Node id: ', id)
logging.info('Position', node.position)
logging.info('In-links: ', node.inlinks)
logging.info('Out-links: ', node.outlinks)
logging.info('Start ODs: ', node.startODs)
logging.info('End ODs: ', node.endODs)
logging.info()
if links:
for id, link in self.links.items():
if link.flow > tol or not only_pos_flows:
logging.info('Link id: ', id)
logging.info('Flow: ', link.flow)
logging.info('Number of paths: ', link.numpaths)
logging.info('Paths: ', link.paths)
logging.info('Delay: ', link.delay)
logging.info('Free flow delay: ', link.ffdelay)
if link.delayfunc is not None: logging.info('Type of delay function: ', link.delayfunc.type)
logging.info()
if ODs:
for id, od in self.ODs.items():
logging.info('OD pair id: ', id)
logging.info('Flow: ', od.flow)
logging.info('Number of paths: ', od.numpaths)
logging.info('Paths: ', od.paths)
logging.info()
if paths:
for id, path in self.paths.items():
logging.info('Path id: ', id)
logging.info('Links: ', [(link.startnode, link.endnode, link.route) for link in path.links])
logging.info('Flow: ', path.flow)
logging.info('Delay: ', path.delay)
logging.info('Free flow delay: ', path.ffdelay)
logging.info()
def get_linkflows(self):
"""Get link flows in a column cvxopt matrix"""
linkflows = matrix(0.0, (self.numlinks, 1))
for id,link in self.links.items(): linkflows[self.indlinks[id]] = link.flow
return linkflows
def get_ffdelays(self):
"""Get ffdelays in a column cvxopt matrix"""
ffdelays = matrix(0.0, (self.numlinks, 1))
for id,link in self.links.items(): ffdelays[self.indlinks[id]] = link.delayfunc.ffdelay
return ffdelays
def get_slopes(self):
"""Get slopes in a column cvxopt matrix"""
slopes = matrix(0.0, (self.numlinks, 1))
for id,link in self.links.items(): slopes[self.indlinks[id]] = link.delayfunc.slope
return slopes
def get_coefs(self):
"""Get coefficients of the polynomial delay functions in cvxopt.matrix
Return value
------------
coefs[i,j] = coef[j] for link i
"""
type = self.links.values()[0].delayfunc.type
if type != 'Polynomial': logging.error('Delay functions must be polynomial'); return
n, degree = self.numlinks, self.links.values()[0].delayfunc.degree
coefs = matrix(0.0, (n, degree))
for id,link in self.links.items():
for j in range(degree): coefs[self.indlinks[id],j] = link.delayfunc.coef[j]
return coefs
def get_ks(self):
"""Get parameters k1, k2 of the hyperbolic delay function in cvxopt.matrix
Return value
------------
k[i,j] = kj for link i with j=1,2
"""
type = self.links.values()[0].delayfunc.type
if type != 'Hyperbolic': logging.error('Delay functions must be hyperbolic'); return
ks = matrix(0.0, (self.numlinks,2))
for id,link in self.links.items():
i = self.indlinks[id]
ks[i,0], ks[i,1] = link.delayfunc.k1, link.delayfunc.k2
return ks
def get_parameters(self):
"""Get parameters of the graph"""
type = self.links.values()[0].delayfunc.type
if type == 'Polynomial': return self.get_coefs(), type
if type == 'Hyperbolic': return self.get_ks(), type
def update_linkflows_linkdelays(self, linkflows):
"""Update link flows and link delays in Graph object"""
for id,link in self.links.items():
flow = linkflows[self.indlinks[id]]
link.flow, link.delay = flow, link.delayfunc.compute_delay(flow)
def update_pathdelays(self):
"""Update path delays in Graph object"""
for path in self.paths.values(): path.delay = sum([link.delay for link in path.links])
def update_pathflows(self, pathflows):
"""Update path flows in Graph object"""
for id,path in self.paths.items(): path.flow = pathflows[self.indpaths[id]]
def get_interior_links(self):
"""Get the ids of links in the interior of the graph"""
intnode_ids, intlk_ids = [], []
for id,node in self.nodes.items():
if len(node.inlinks.items()) == 1: intnode_ids.append(id)
for id,link in self.links.items():
if link.endnode not in intnode_ids and link.startnode not in intnode_ids:
intlk_ids.append(id)
return intlk_ids
class Link:
"""A link in the graph"""
def __init__(self, startnode, endnode, route, flow=0.0, delay=0.0, ffdelay=0.0, delayfunc=None):
self.startnode = startnode
self.endnode = endnode
self.route = route #if multiple edges
self.flow = flow #flow on the link
self.delay = delay #delay on the link
self.ffdelay = ffdelay #free flow delay
self.delayfunc = delayfunc
self.paths = {} #set of paths passing through
self.numpaths = 0
def repr(self):
return (self.startnode,self.endnode,self.route)
class Node:
"""A node in the graph"""
def __init__(self, position):
self.position = position
self.inlinks = {}
self.outlinks = {}
self.startODs = {} #set of OD pairs with origin at node
self.endODs = {} #set of OD pairs with destination at node
class Path:
"""A path in the graph"""
def __init__(self, origin, destination, route, links, flow=0.0, delay=0.0, ffdelay=0.0):
self.o = origin #origin node
self.d = destination #destination node
self.route = route #index of path associated to this od pair
self.links = links #set of all links on the path
self.flow = flow
self.delay = delay
self.ffdelay = ffdelay
class OD:
"""OD pairs"""
def __init__(self, origin, destination, flow=0.0):
self.o = origin
self.d = destination
self.flow = flow
self.paths = {} # set of all paths for the OD pair
self.numpaths = 0
class PolyDelay:
"""Polynomial Delay function
delay(x) = ffdelay + sum_{k>=1} coef[k]*(x)^k
example: coef[k]=ffdelay*theta[k]*slope^k w/ theta type = [0.0, 0.0, 0.0, 0.15]"""
def __init__(self, ffdelay, slope, coef):
self.ffdelay = ffdelay
self.slope = slope # this can be seen as inverse capacities
self.coef = coef
self.degree = len(coef)
self.type = 'Polynomial'
def compute_delay(self, flow):
"""Compute delay"""
return self.ffdelay + np.dot(self.coef, np.power(flow, range(1,self.degree+1)))
class HyperDelay:
"""Hyperbolic Delay function
delay(x) = ffdelay - k1/k2 + k1/(k2-x)
k2 is the max capacity on link (1000/veh/lane)
example: k1=a*ffdelay/slope, k2=b/slope w/ (a,b) type = (3.5, 3)"""
def __init__(self, ffdelay, slope, k1, k2):
self.ffdelay = ffdelay
self.slope = slope
self.k1 = k1
self.k2 = k2
self.type = 'Hyperbolic'
def compute_delay(self, flow):
"""Compute delay"""
k1, k2, ffdelay = self.k1, self.k2, self.ffdelay
return ffdelay - k1/k2 + k1/(k2-flow)
def create_delayfunc(type, parameters=None):
"""Create a Delay function of a specific type"""
if type == 'None': return None
if type == 'Polynomial': return PolyDelay(parameters[0], parameters[1], parameters[2])
if type == 'Hyperbolic': return HyperDelay(parameters[0], parameters[1], parameters[2], parameters[3])
if type == 'Other': return Other(parameters[0], parameters[1], parameters[2])
def create_graph_from_list(list_nodes, list_links, delaytype, list_ods=None, description=None):
"""Create a graph from a list of of nodes and links
"""
graph = Graph(description)
graph.add_nodes_from_list(list_nodes)
graph.add_links_from_list(list_links, delaytype)
if list_ods is not None: graph.add_ods_from_list(list_ods)
return graph