Skip to content

Commit 20b7da4

Browse files
committed
clean up legacy namings across files
- those namings were inherited from DTALite_s.py which has been deprecated; - convert possible integer node id to string for some API's including find_shortest_path(), get_accessible_nodes(), and get_accessible_links(); - update docstrings for the above API accordingly; - drop o_zone_name and d_zone_name from od_accessibility.csv, which are always empty; - remove str conversion on node id and link id from output_columns() and get_agent_node_path() as node id and link id are strings; - remove str conversion on zone id in add_centroids_connectors as zone is string; - change pre_source_node_id in AccessNetwork and from_node_id_prev in find_shortest_path() from -1 to '' as node id is string.
1 parent 63e6f22 commit 20b7da4

File tree

6 files changed

+84
-81
lines changed

6 files changed

+84
-81
lines changed

path4gmns/accessibility.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,7 @@ def _update_min_travel_time(an, at, min_travel_times, time_dependent, demand_per
5757
def _output_od_accessibility(min_travel_times, zones, mode, output_dir):
5858
""" output accessibility for each OD pair (i.e., travel time) """
5959
with open(output_dir+'/od_accessibility.csv', 'w', newline='') as f:
60-
headers = ['o_zone_id', 'o_zone_name',
61-
'd_zone_id', 'd_zone_name',
62-
'accessibility', 'distance',
63-
'geometry']
60+
headers = ['o_zone_id', 'd_zone_id', 'accessibility', 'distance', 'geometry']
6461

6562
writer = csv.writer(f)
6663
writer.writerow(headers)

path4gmns/classes.py

Lines changed: 36 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@
1515

1616
class Node:
1717

18-
def __init__(self, node_seq_no, node_id, zone_id, x='', y='', is_activity_node=False):
18+
def __init__(self, node_no, node_id, zone_id, x='', y='', is_activity_node=False):
1919
""" the attributes of node """
20-
# node_seq_no: internal node index used for calculation
21-
self.node_seq_no = node_seq_no
20+
# node_no: internal node index used for calculation
21+
self.node_no = node_no
2222
# node_id: user defined node id from input
2323
self.node_id = node_id
2424
# link objects
@@ -42,7 +42,7 @@ def get_node_id(self):
4242
return self.node_id
4343

4444
def get_node_no(self):
45-
return self.node_seq_no
45+
return self.node_no
4646

4747
def get_coordinate(self):
4848
return self.coord_x + ' ' + self.coord_y
@@ -65,7 +65,7 @@ class Link:
6565

6666
def __init__(self,
6767
id,
68-
link_seq_no,
68+
link_no,
6969
from_node_no,
7070
to_node_no,
7171
from_node_id,
@@ -80,9 +80,9 @@ def __init__(self,
8080
demand_period_size=1):
8181
""" the attributes of link """
8282
self.id = id
83-
self.link_seq_no = link_seq_no
84-
self.from_node_seq_no = from_node_no
85-
self.to_node_seq_no = to_node_no
83+
self.link_no = link_no
84+
self.from_node_no = from_node_no
85+
self.to_node_no = to_node_no
8686
self.from_node_id = from_node_id
8787
self.to_node_id = to_node_id
8888
# length is mile or km
@@ -117,7 +117,7 @@ def get_link_id(self):
117117
return self.id
118118

119119
def get_seq_no(self):
120-
return self.link_seq_no
120+
return self.link_no
121121

122122
def get_from_node_id(self):
123123
return self.from_node_id
@@ -195,11 +195,11 @@ class Agent:
195195
id: integer starts from 1
196196
seq_no: internal agent index starting from 0 used for calculation
197197
"""
198-
def __init__(self, agent_id, agent_seq_no, agent_type_id, demand_period_id,
198+
def __init__(self, agent_id, agent_no, agent_type_id, demand_period_id,
199199
o_zone_id, d_zone_id):
200200
""" the attribute of agent """
201201
self.id = agent_id
202-
self.seq_no = agent_seq_no
202+
self.seq_no = agent_no
203203
# vehicle
204204
self.at_id = agent_type_id
205205
self.dp_id = demand_period_id
@@ -416,8 +416,8 @@ def allocate_for_CAPI(self):
416416
node_label_cost = [MAX_LABEL_COST] * node_size
417417

418418
# initialize from_node_no_array, to_node_no_array, and link_cost_array
419-
from_node_no_array = [link.from_node_seq_no for link in self.links]
420-
to_node_no_array = [link.to_node_seq_no for link in self.links]
419+
from_node_no_array = [link.from_node_no for link in self.links]
420+
to_node_no_array = [link.to_node_no for link in self.links]
421421
link_cost_array = [link.fftt for link in self.links]
422422

423423
# initialize others
@@ -434,7 +434,7 @@ def allocate_for_CAPI(self):
434434
first_link_from[i] = j
435435
for link in node.outgoing_links:
436436
# set up the mapping from j to the true link seq no
437-
sorted_link_no_array[j] = link.link_seq_no
437+
sorted_link_no_array[j] = link.link_no
438438
j += 1
439439
last_link_from[i] = j
440440

@@ -475,7 +475,7 @@ def add_centroids_connectors(self):
475475
continue
476476

477477
# create a centroid
478-
node_id = 'c_' + str(z)
478+
node_id = 'c_' + z
479479
centroid = Node(node_no, node_id, z)
480480
# try coordinate of the centroid from each zone first. if the values
481481
# are invalid, then use that of the first node from each zone.
@@ -607,7 +607,7 @@ def get_agent_node_path(self, agent_id, path_only):
607607
path = ''
608608
if agent.node_path:
609609
path = ';'.join(
610-
str(self.map_no_to_id[x]) for x in reversed(agent.node_path)
610+
self.map_no_to_id[x] for x in reversed(agent.node_path)
611611
)
612612

613613
if path_only:
@@ -721,7 +721,7 @@ def get_agent_type_name(self):
721721
""" for allowed uses in single_source_shortest_path()"""
722722
return self.agent_type_name
723723

724-
def get_link_seq_no(self, id):
724+
def get_link_no(self, id):
725725
return self.link_ids[id]
726726

727727
def get_agents(self):
@@ -1171,7 +1171,7 @@ def __init__(self, base, add_cc=True):
11711171
self.link_size = base.get_link_size()
11721172
self.centroids_added = self.base.centroids_added
11731173
self.agent_type_name = 'all'
1174-
self.pre_source_node_id = -1
1174+
self.pre_source_node_id = ''
11751175
if add_cc:
11761176
self._add_centroids_connectors()
11771177
self.capi_allocated = False
@@ -1196,8 +1196,8 @@ def get_nodes_from_zone(self, zone_id):
11961196
def _get_zone_coord(self, zone_id):
11971197
""" coordinate of each zone is from its first node """
11981198
node_id = self.get_nodes_from_zone(zone_id)[0]
1199-
node_seq_no = self.base.get_node_no(node_id)
1200-
node = self.base.get_nodes()[node_seq_no]
1199+
node_no = self.base.get_node_no(node_id)
1200+
node = self.base.get_nodes()[node_no]
12011201
return node.coord_x, node.coord_y
12021202

12031203
def set_target_mode(self, mode):
@@ -1279,9 +1279,9 @@ def get_sp_distance(self, node_no):
12791279

12801280
dist = 0
12811281
while node_no >= 0:
1282-
link_seq_no = self.link_preds[node_no]
1283-
if link_seq_no >= 0:
1284-
dist += self.get_link(link_seq_no).get_length()
1282+
link_no = self.link_preds[node_no]
1283+
if link_no >= 0:
1284+
dist += self.get_link(link_no).get_length()
12851285

12861286
node_no = self.node_preds[node_no]
12871287

@@ -1497,6 +1497,10 @@ def find_shortest_path(self, from_node_id, to_node_id, mode, seq_type='node'):
14971497
at_name, _ = self._convert_mode(mode)
14981498
self.network.set_agent_type_name(at_name)
14991499

1500+
# add backward compatibility in case the user still use integer node id's
1501+
from_node_id = str(from_node_id)
1502+
to_node_id = str(to_node_id)
1503+
15001504
return find_shortest_path(self.network, from_node_id,
15011505
to_node_id, seq_type)
15021506

@@ -1542,9 +1546,9 @@ def get_link(self, seq_no):
15421546
""" return link object corresponding to link seq no """
15431547
return self.network.get_link(seq_no)
15441548

1545-
def get_link_seq_no(self, id):
1549+
def get_link_no(self, id):
15461550
""" id is string """
1547-
return self.network.get_link_seq_no(id)
1551+
return self.network.get_link_no(id)
15481552

15491553
def get_node_no(self, id):
15501554
""" id is integer """
@@ -1558,6 +1562,8 @@ def get_node_label_cost(self, node_no):
15581562

15591563
def get_accessible_nodes(self, source_node_id, time_budget,
15601564
mode, time_dependent, tau):
1565+
source_node_id = str(source_node_id)
1566+
15611567
if source_node_id not in self.network.map_id_to_no.keys():
15621568
raise Exception(f'Node ID: {source_node_id} not in the network')
15631569

@@ -1727,7 +1733,7 @@ def set_simu_start_time(self, st):
17271733

17281734
def set_capacity_ratio(self, tau, link_id, r):
17291735
try:
1730-
link_no = self.get_link_seq_no(link_id)
1736+
link_no = self.get_link_no(link_id)
17311737
except KeyError:
17321738
return
17331739

@@ -1793,10 +1799,10 @@ def find_shortest_path(self, from_node_id, to_node_id,
17931799
Parameters
17941800
----------
17951801
from_node_id
1796-
the starting node id
1802+
the starting node id, which shall be a string
17971803
17981804
to_node_id
1799-
the ending node id
1805+
the ending node id, which shall be a string
18001806
18011807
seq_type
18021808
'node' or 'link'. You will get the shortest path in sequence of
@@ -1837,7 +1843,7 @@ def get_accessible_nodes(self,
18371843
Parameters
18381844
----------
18391845
source_node_id
1840-
the starting node id for evaluation
1846+
the starting node id for evaluation, which shall be string
18411847
18421848
time_budget
18431849
the amount of time to travel in minutes
@@ -1894,7 +1900,7 @@ def get_accessible_links(self,
18941900
Parameters
18951901
----------
18961902
source_node_id
1897-
the starting node id for evaluation
1903+
the starting node id for evaluation, which shall be string
18981904
18991905
time_budget
19001906
the amount of time to travel in minutes

path4gmns/colgen.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -150,15 +150,15 @@ def _backtrace_shortest_path_tree(centroid,
150150
link_path = []
151151
dist = 0
152152

153-
curr_node_seq_no = c.get_node_no()
153+
curr_node_no = c.get_node_no()
154154
# retrieve the sequence backwards
155-
while curr_node_seq_no >= 0:
156-
curr_link_seq_no = link_preds[curr_node_seq_no]
157-
if curr_link_seq_no >= 0:
158-
link_path.append(curr_link_seq_no)
159-
dist += links[curr_link_seq_no].length
155+
while curr_node_no >= 0:
156+
curr_link_no = link_preds[curr_node_no]
157+
if curr_link_no >= 0:
158+
link_path.append(curr_link_no)
159+
dist += links[curr_link_no].length
160160

161-
curr_node_seq_no = node_preds[curr_node_seq_no]
161+
curr_node_no = node_preds[curr_node_no]
162162

163163
# make sure this is a valid path
164164
if not link_path:
@@ -199,12 +199,12 @@ def _update_column_attributes(column_pool, links):
199199

200200
for j in col.links:
201201
link = links[j]
202-
nodes.append(links[j].to_node_seq_no)
202+
nodes.append(links[j].to_node_no)
203203
travel_time += link.travel_time_by_period[dp]
204204
path_toll += links[j].get_toll()
205205

206206
# last node
207-
nodes.append(links[col.links[-1]].from_node_seq_no)
207+
nodes.append(links[col.links[-1]].from_node_no)
208208

209209
col.set_travel_time(travel_time)
210210
col.set_toll(path_toll)

path4gmns/path.py

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ def _single_source_shortest_path_fifo(G, origin_node_no):
111111
from_node = SEList.pop(0)
112112
status[from_node] = 0
113113
for link in G.nodes[from_node].outgoing_links:
114-
to_node = link.to_node_seq_no
114+
to_node = link.to_node_no
115115
new_to_node_cost = (G.node_label_cost[from_node]
116116
+ link.fftt)
117117
# we only compare cost at the downstream node ToID
@@ -124,7 +124,7 @@ def _single_source_shortest_path_fifo(G, origin_node_no):
124124
G.node_preds[to_node] = from_node
125125
# pointer to previous physical node index
126126
# from the current label at current node and time
127-
G.link_preds[to_node] = link.link_seq_no
127+
G.link_preds[to_node] = link.link_no
128128
if not status[to_node]:
129129
SEList.append(to_node)
130130
status[to_node] = 1
@@ -151,7 +151,7 @@ def _single_source_shortest_path_deque(G, origin_node_no):
151151
from_node = SEList.popleft()
152152
status[from_node] = 2
153153
for link in G.nodes[from_node].outgoing_links:
154-
to_node = link.to_node_seq_no
154+
to_node = link.to_node_no
155155
new_to_node_cost = (G.node_label_cost[from_node]
156156
+ link.fftt)
157157
# we only compare cost at the downstream node ToID
@@ -164,7 +164,7 @@ def _single_source_shortest_path_deque(G, origin_node_no):
164164
G.node_preds[to_node] = from_node
165165
# pointer to previous physical node index
166166
# from the current label at current node and time
167-
G.link_preds[to_node] = link.link_seq_no
167+
G.link_preds[to_node] = link.link_no
168168
if status[to_node] != 1:
169169
if status[to_node] == 2:
170170
SEList.appendleft(to_node)
@@ -198,7 +198,7 @@ def _single_source_shortest_path_dijkstra(G, origin_node_no):
198198
continue
199199
status[from_node] = 1
200200
for link in G.nodes[from_node].outgoing_links:
201-
to_node = link.to_node_seq_no
201+
to_node = link.to_node_no
202202
new_to_node_cost = label_cost + link.fftt
203203
# we only compare cost at the downstream node ToID
204204
# at the new arrival time t
@@ -210,7 +210,7 @@ def _single_source_shortest_path_dijkstra(G, origin_node_no):
210210
G.node_preds[to_node] = from_node
211211
# pointer to previous physical node index
212212
# from the current label at current node and time
213-
G.link_preds[to_node] = link.link_seq_no
213+
G.link_preds[to_node] = link.link_no
214214
heapq.heappush(SEList, (G.node_label_cost[to_node], to_node))
215215

216216

@@ -255,26 +255,26 @@ def output_path_sequence(G, to_node_id, type='node'):
255255
Note that this function returns GENERATOR rather than list.
256256
"""
257257
path = []
258-
current_node_seq_no = G.map_id_to_no[to_node_id]
258+
curr_node_no = G.map_id_to_no[to_node_id]
259259

260260
if type.startswith('node'):
261261
# retrieve the sequence backwards
262-
while current_node_seq_no >= 0:
263-
path.append(current_node_seq_no)
264-
current_node_seq_no = G.node_preds[current_node_seq_no]
262+
while curr_node_no >= 0:
263+
path.append(curr_node_no)
264+
curr_node_no = G.node_preds[curr_node_no]
265265
# reverse the sequence
266-
for node_seq_no in reversed(path):
267-
yield G.map_no_to_id[node_seq_no]
266+
for node_no in reversed(path):
267+
yield G.map_no_to_id[node_no]
268268
else:
269269
# retrieve the sequence backwards
270-
current_link_seq_no = G.link_preds[current_node_seq_no]
271-
while current_link_seq_no >= 0:
272-
path.append(current_link_seq_no)
273-
current_node_seq_no = G.node_preds[current_node_seq_no]
274-
current_link_seq_no = G.link_preds[current_node_seq_no]
270+
curr_link_no = G.link_preds[curr_node_no]
271+
while curr_link_no >= 0:
272+
path.append(curr_link_no)
273+
curr_node_no = G.node_preds[curr_node_no]
274+
curr_link_no = G.link_preds[curr_node_no]
275275
# reverse the sequence
276-
for link_seq_no in reversed(path):
277-
yield G.links[link_seq_no].get_link_id()
276+
for link_no in reversed(path):
277+
yield G.links[link_no].get_link_id()
278278

279279

280280
def _get_path_cost(G, to_node_id):
@@ -320,7 +320,7 @@ def find_path_for_agents(G, column_pool, engine_type='c'):
320320
print('setting up individual agents')
321321
G.setup_agents(column_pool)
322322

323-
from_node_id_prev = -1
323+
from_node_id_prev = ''
324324
for agent in G.agents:
325325
from_node_id = agent.o_node_id
326326
to_node_id = agent.d_node_id
@@ -344,17 +344,17 @@ def find_path_for_agents(G, column_pool, engine_type='c'):
344344
node_path = []
345345
link_path = []
346346

347-
current_node_seq_no = G.map_id_to_no[to_node_id]
347+
curr_node_no = G.map_id_to_no[to_node_id]
348348
# set up the cost
349-
agent.path_cost = G.node_label_cost[current_node_seq_no]
349+
agent.path_cost = G.node_label_cost[curr_node_no]
350350

351351
# retrieve the sequence backwards
352-
while current_node_seq_no >= 0:
353-
node_path.append(current_node_seq_no)
354-
current_link_seq_no = G.link_preds[current_node_seq_no]
355-
if current_link_seq_no >= 0:
356-
link_path.append(current_link_seq_no)
357-
current_node_seq_no = G.node_preds[current_node_seq_no]
352+
while curr_node_no >= 0:
353+
node_path.append(curr_node_no)
354+
curr_link_no = G.link_preds[curr_node_no]
355+
if curr_link_no >= 0:
356+
link_path.append(curr_link_no)
357+
curr_node_no = G.node_preds[curr_node_no]
358358

359359
# make sure it is a valid path
360360
if not link_path:

0 commit comments

Comments
 (0)