Skip to content

Commit

Permalink
optimize calculate_td_vdf() in Link
Browse files Browse the repository at this point in the history
- remove demand_periods from its arg list to have one unified implementation;
- store reduction_ratio in VDFPeriod and update run_bpr() accordingly;
- add set_reduction_ratio() in Link to set up reduction_ratio for each
  VDFPeriod;
- remove beg_iter and end_iter from SpecialEvent, and update its constructor;
- update read_settings() accordingly.
  • Loading branch information
jdlph committed Mar 24, 2023
1 parent fb3f6e4 commit bbad4b1
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 37 deletions.
2 changes: 1 addition & 1 deletion docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
author = 'Dr. Peiheng Li and Dr. Xuesong (Simon) Zhou'

# The full version, including alpha/beta/rc tags
release = 'v0.9.2'
release = 'v0.9.3'


# -- General configuration ---------------------------------------------------
Expand Down
37 changes: 12 additions & 25 deletions path4gmns/classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,27 +173,21 @@ def reset_period_flow_vol(self):
def increase_period_flow_vol(self, tau, fv):
self.flow_vol_by_period[tau] += fv

def calculate_td_vdf(self, demand_periods=None):
if demand_periods is None:
for tau in range(self.demand_period_size):
self.travel_time_by_period[tau] = (
self.vdfperiods[tau].run_bpr(self.flow_vol_by_period[tau])
)
else:
for dp in demand_periods:
tau = dp.get_id()
reduction_ratio = dp.get_reduction_ratio(self.id)
self.travel_time_by_period[tau] = (
self.vdfperiods[tau].run_bpr(self.flow_vol_by_period[tau],
reduction_ratio)
)
def calculate_td_vdf(self):
for tau in range(self.demand_period_size):
self.travel_time_by_period[tau] = (
self.vdfperiods[tau].run_bpr(self.flow_vol_by_period[tau])
)

def update_waiting_time(self, minute, wt):
try:
self.waiting_time[minute] += wt
except IndexError:
pass

def set_reduction_ratio(self, tau, rr):
self.vdfperiods[tau].reduction_ratio = rr


class Agent:
""" individual agent derived from aggregated demand between an OD pair
Expand Down Expand Up @@ -927,18 +921,10 @@ def get_legacy_name():

class SpecialEvent:

def __init__(self, name, beg_iteration, end_iteration) -> None:
def __init__(self, name) -> None:
self.name = name
self.beg_iter = beg_iteration
self.end_iter = end_iteration
self.affected_links = {}

def get_beg_iteration(self):
return self.beg_iter

def get_end_iteration(self):
return self.end_iter


class DemandPeriod:

Expand Down Expand Up @@ -1031,6 +1017,7 @@ def __init__(self, id, alpha=0.15, beta=4, mu=1000,
self.phf = phf
self.avg_travel_time = 0
self.voc = 0
self.reduction_ratio = 1

def get_avg_travel_time(self):
return self.avg_travel_time
Expand All @@ -1041,9 +1028,9 @@ def get_voc(self):
def get_fftt(self):
return self.fftt

def run_bpr(self, vol, reduction_ratio=1):
def run_bpr(self, vol):
vol = max(0, vol)
self.voc = vol / max(SMALL_DIVISOR, self.capacity * reduction_ratio)
self.voc = vol / max(SMALL_DIVISOR, self.capacity * self.reduction_ratio)
self.avg_travel_time = (
self.fftt
+ self.fftt
Expand Down
13 changes: 5 additions & 8 deletions path4gmns/colgen.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ def _update_link_cost_array(spnetworks):
)


def _update_link_travel_time(links, demand_periods=None):
def _update_link_travel_time(links):
for link in links:
if link.length == 0:
break

link.calculate_td_vdf(demand_periods)
link.calculate_td_vdf()


def _update_link_and_column_volume(column_pool, links, iter_num, reduce_path_vol = True):
Expand Down Expand Up @@ -277,7 +277,6 @@ def perform_column_generation(column_gen_num, column_update_num, ui):

links = A.get_links()
ats = A.get_agent_types()
dps = A.get_demand_periods()
column_pool = A.get_column_pool()

st = time()
Expand All @@ -286,7 +285,7 @@ def perform_column_generation(column_gen_num, column_update_num, ui):
print(f'current iteration number in column generation: {i}')

_update_link_and_column_volume(column_pool, links, i)
_update_link_travel_time(links, dps)
_update_link_travel_time(links)
# update generalized link cost before assignment
_update_link_cost_array(A.get_spnetworks())
# loop through all centroids on the base network
Expand All @@ -295,13 +294,11 @@ def perform_column_generation(column_gen_num, column_update_num, ui):
print(f'\nprocessing time of generating columns: {time()-st:.2f} s\n')

for i in range(column_update_num):
_update_link_and_column_volume(column_pool, links, i, False)
_update_link_travel_time(links)
_update_column_gradient_cost_and_flow(column_pool, links, ats, i)
_update_link_and_column_volume(column_pool, links, i + 1, False)
_update_link_travel_time(links)

# postprocessing
_update_link_and_column_volume(column_pool, links, column_gen_num, False)
_update_link_travel_time(links)
_update_column_attributes(column_pool, links)


Expand Down
7 changes: 4 additions & 3 deletions path4gmns/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -830,15 +830,16 @@ def read_settings(input_dir, assignment):
raise KeyError

name = s['name']
beg_iter = s['beg_iteration']
end_iter = s['end_iteration']
se = SpecialEvent(name, beg_iter, end_iter)
se = SpecialEvent(name)

links = s['affected_links']
for link in links:
link_id = str(link['link_id'])
ratio = link['reduction_ratio']
se.affected_links[link_id] = ratio
link_no = assignment.get_link_seq_no(link_id)
link_obj = assignment.get_link(link_no)
link_obj.set_reduction_ratio(i, ratio)

dp.special_event = se
except KeyError:
Expand Down

0 comments on commit bbad4b1

Please sign in to comment.