From 3fe3a569ea0fb2f4ae944a3003d6dbc25588347d Mon Sep 17 00:00:00 2001 From: Cheng LI <26736986+Leot6@users.noreply.github.com> Date: Sat, 24 Apr 2021 17:55:55 +0800 Subject: [PATCH] update DEBUG_PRINTING in SBA and rebalancer --- lib/dispatcher/gi/greedy_insertion.py | 2 +- lib/dispatcher/osp/osp_assign.py | 6 +- lib/dispatcher/sba/single_req_batch_assign.py | 57 +++++++++---------- lib/rebalancer/naive_rebalancer.py | 10 +++- lib/simulator/config.py | 18 +++--- lib/simulator/model.py | 14 +++-- run.py | 14 +++-- 7 files changed, 66 insertions(+), 55 deletions(-) diff --git a/lib/dispatcher/gi/greedy_insertion.py b/lib/dispatcher/gi/greedy_insertion.py index 93bbd3c..3482750 100755 --- a/lib/dispatcher/gi/greedy_insertion.py +++ b/lib/dispatcher/gi/greedy_insertion.py @@ -31,8 +31,8 @@ def __init__(self, amod): def dispatch(self, T): if IS_DEBUG: - print(' -assigning reqs to vehs through GI...') t = time.time() + print(f' -assigning {len(self.queue)} reqs to vehs through GI...') num_dispatched_req = 0 vids_assigned = [] diff --git a/lib/dispatcher/osp/osp_assign.py b/lib/dispatcher/osp/osp_assign.py index f3b77a6..fefd248 100755 --- a/lib/dispatcher/osp/osp_assign.py +++ b/lib/dispatcher/osp/osp_assign.py @@ -5,7 +5,7 @@ import time import mosek import numpy as np -from lib.simulator.config import OBJECTIVE, Reliability_Shreshold +from lib.simulator.config import IS_DEBUG, OBJECTIVE, Reliability_Shreshold from lib.dispatcher.osp.osp_schedule import compute_sche_delay, compute_sche_reward, compute_sche_reliability CUTOFF_ILP = 20 @@ -200,6 +200,7 @@ def ILP_assign(veh_trip_edges, reqs_pool, reqs_all, reqs_picking=[], prev_assign def greedy_assign(veh_trip_edges): + t = time.time() rids_assigned = [] vids_assigned = [] sches_assigned = [] @@ -218,5 +219,8 @@ def greedy_assign(veh_trip_edges): # print(' *trip %s is assigned to veh %d with cost %.2f' % ([req.id for req in trip], veh_id, cost)) assert len(rids_assigned) == len(set(rids_assigned)) + if IS_DEBUG: + print(f' +greedy assignment... ({round((time.time() - t), 2)}s)') + return rids_assigned, vids_assigned, sches_assigned diff --git a/lib/dispatcher/sba/single_req_batch_assign.py b/lib/dispatcher/sba/single_req_batch_assign.py index 79a40bb..cd74d06 100755 --- a/lib/dispatcher/sba/single_req_batch_assign.py +++ b/lib/dispatcher/sba/single_req_batch_assign.py @@ -7,7 +7,7 @@ from lib.simulator.config import IS_DEBUG from lib.routing.routing_server import get_duration_from_origin_to_dest from lib.dispatcher.osp.osp_pool import build_vt_table -from lib.dispatcher.osp.osp_assign import ILP_assign +from lib.dispatcher.osp.osp_assign import ILP_assign, greedy_assign from lib.dispatcher.osp.osp_schedule import compute_schedule # MULTI_ASSIGN = True @@ -42,8 +42,8 @@ def __str__(self): def ridesharing_match_sba(vehs, reqs_all, reqs_pool, T): if IS_DEBUG: - print(' -T = %d, find veh req pairs ...' % T) - a1 = time.time() + t = time.time() + print(f' -assigning {len(reqs_pool)} reqs to vehs through SBA...') if MULTI_ASSIGN: reqs_prev = [] @@ -53,42 +53,19 @@ def ridesharing_match_sba(vehs, reqs_all, reqs_pool, T): # veh_req_edges = search_feasible_veh_req_edges(vehs, reqs_pool, T) num_edges = len(veh_req_edges) - if IS_DEBUG: - print(' a1 running time:', round((time.time() - a1), 2)) - # ILP assign - if IS_DEBUG: - print(' -T = %d, start ILP assign with %d edges...' % (T, len(veh_req_edges))) - a2 = time.time() - rids_assigned, vids_assigned, sches_assigned = ILP_assign(veh_req_edges, reqs_pool, reqs_all) - # rids_assigned, vids_assigned, sches_assigned = greedy_assign(veh_req_edges) + # rids_assigned, vids_assigned, sches_assigned = ILP_assign(veh_req_edges, reqs_pool, reqs_all) + rids_assigned, vids_assigned, sches_assigned = greedy_assign(veh_req_edges) if IS_DEBUG: - print(' a2 running time:', round((time.time() - a2), 2)) + print(f' +assigned reqs {len(rids_assigned)} ({round((time.time() - t), 2)}s)') return rids_assigned, vids_assigned, sches_assigned, num_edges -# def search_feasible_veh_req_edges(vehs, reqs_pool, T): -# veh_req_edges = [] -# for veh in tqdm(vehs, desc=f'veh search ({len(vehs)} vehs)', leave=False): -# veh_params = [veh.nid, veh.t_to_nid, veh.n] -# sub_sche = veh.sche -# for req in tqdm(reqs_pool, desc=f'candidate req ({len(reqs_pool)} reqs)', leave=False): -# # filter out the req which can not be served even when the veh is idle -# if get_duration_from_origin_to_dest(veh.nid, req.onid) + veh.t_to_nid + T > req.Clp: -# continue -# trip = tuple([req]) -# req_params = [req.id, req.onid, req.dnid, req.Clp, req.Cld] -# best_sche, min_cost, feasible_sches, num_of_sche_searched \ -# = compute_schedule(veh_params, [sub_sche], req_params, T) -# if best_sche: -# veh_req_edges.append((veh, trip, best_sche, min_cost)) -# return veh_req_edges - - -# when numreqs << numvehs, this one runs faster than the above one +# when numreqs << numvehs, this one runs faster than the following one def build_rv_graph(vehs, reqs_pool, T): + t = time.time() veh_req_edges = [] for req in tqdm(reqs_pool, desc=f'req search ({len(reqs_pool)} reqs)', leave=False): req_params = [req.id, req.onid, req.dnid, req.Clp, req.Cld] @@ -101,5 +78,23 @@ def build_rv_graph(vehs, reqs_pool, T): best_sche, cost, feasible_sches, n_s_c = compute_schedule(veh_params, [sub_sche], req_params, T) if best_sche: veh_req_edges.append((veh, trip, best_sche, cost)) + if IS_DEBUG: + print(f' +computing feasible veh req pairs... ({round((time.time() - t), 2)}s)') return veh_req_edges +# def search_feasible_veh_req_edges(vehs, reqs_pool, T): +# veh_req_edges = [] +# for veh in tqdm(vehs, desc=f'veh search ({len(vehs)} vehs)', leave=False): +# veh_params = [veh.nid, veh.t_to_nid, veh.n] +# sub_sche = veh.sche +# for req in tqdm(reqs_pool, desc=f'candidate req ({len(reqs_pool)} reqs)', leave=False): +# # filter out the req which can not be served even when the veh is idle +# if get_duration_from_origin_to_dest(veh.nid, req.onid) + veh.t_to_nid + T > req.Clp: +# continue +# trip = tuple([req]) +# req_params = [req.id, req.onid, req.dnid, req.Clp, req.Cld] +# best_sche, min_cost, feasible_sches, num_of_sche_searched \ +# = compute_schedule(veh_params, [sub_sche], req_params, T) +# if best_sche: +# veh_req_edges.append((veh, trip, best_sche, min_cost)) +# return veh_req_edges \ No newline at end of file diff --git a/lib/rebalancer/naive_rebalancer.py b/lib/rebalancer/naive_rebalancer.py index c01265d..afb89b0 100755 --- a/lib/rebalancer/naive_rebalancer.py +++ b/lib/rebalancer/naive_rebalancer.py @@ -24,8 +24,12 @@ def __init__(self, amod): def rebelancing(self, T): if IS_DEBUG: - print(' -T = %d, start rebalancing...' % T) - a4 = time.time() + t = time.time() + noi = 0 # number of idle vehicles + for veh in self.vehs: + if veh.idle: + noi += 1 + print(f' -repositioning {noi} idle vehs to {len(self.reqs_unassigned)} positions through NR...') reqs_unassigned = sorted(self.reqs_unassigned, key=lambda r: r.id) veh_rebl_pairs = [] @@ -42,5 +46,5 @@ def rebelancing(self, T): self.vehs[vid].build_route(sche, self.reqs, T) if IS_DEBUG: - print(' a4 running time:', round((time.time() - a4), 2)) + print(f' +rebalancing vehs {len(vids_rebl)} ({round((time.time() - t), 2)}s)') diff --git a/lib/simulator/config.py b/lib/simulator/config.py index 9574649..07e2cdd 100755 --- a/lib/simulator/config.py +++ b/lib/simulator/config.py @@ -22,15 +22,15 @@ # demand volume (percentage of total), simulation start time and its nickname DMD_VOL = 1 # <= 1 -DMD_SST = parse(DATE + ' 00:00:00') -# DMD_SST = parse(DATE + ' 18:30:00') +# DMD_SST = parse(DATE + ' 00:00:00') +DMD_SST = parse(DATE + ' 18:30:00') # peak hour REQ_INIT_IDX = 0 DMD_STR = 'Manhattan' # warm-up time, study time and cool-down time of the simulation (in seconds), 24 hour = 1440 min -T_WARM_UP = 60 * 0 # = 60 * 30 +T_WARM_UP = 60 * 30 # = 60 * 30 T_STUDY = 60 * 60 # < 60 * 1371 -T_COOL_DOWN = 60 * 0 # = 60 * 39 +T_COOL_DOWN = 60 * 39 # = 60 * 39 # T_TOTAL = (T_WARM_UP + T_STUDY + T_COOL_DOWN) # fleet size, vehicle capacity and ridesharing size @@ -50,8 +50,8 @@ INT_REBL = INT_ASSIGN * 1 # dispatching and rebalancing methods -DISPATCHER = 'GI' -# DISPATCHER = 'SBA' +# DISPATCHER = 'GI' +DISPATCHER = 'SBA' # DISPATCHER = 'OSP' # DISPATCHER = 'RTV' @@ -65,11 +65,11 @@ Reliability_Shreshold = 0 # if true, activate the animation / analysis +IS_ANALYSIS = True IS_ANIMATION = False # IS_ANIMATION = True -IS_ANALYSIS = True -# IS_DEBUG = False -IS_DEBUG = True +IS_DEBUG = False +# IS_DEBUG = True # travel time mode # IS_STOCHASTIC_TRAFFIC = True diff --git a/lib/simulator/model.py b/lib/simulator/model.py index d565312..27cb5e9 100755 --- a/lib/simulator/model.py +++ b/lib/simulator/model.py @@ -120,7 +120,7 @@ def dispatch_at_time(self, T): f'Total reqs received = {self.N}, of which {len(self.reqs_served)} complete ' f'+ {len(self.reqs_onboard)} onboard + {len(self.reqs_picking)} picking ' f'+ {len(self.reqs_unassigned)} pending + {len(self.rejs)} walkaway ' - f'({round((time.time() - stime), 2)})') + f'({round((time.time() - stime), 2)}s)') print() def reject_long_wait_reqs(self): @@ -167,6 +167,7 @@ def upd_vehs_and_reqs_stat_to_time(self): if IS_DEBUG: noi = 0 # number of idle vehicles + nor = 0 # number of rebalancing vehicles nop = 0 # number of picked requests nod = 0 # number of dropped requests for veh in self.vehs: @@ -174,8 +175,11 @@ def upd_vehs_and_reqs_stat_to_time(self): nod += len(veh.new_dropped_rids) if veh.idle: noi += 1 - print(f' +picked reqs: {nop}, dropped reqs: {nod}, ' - f'idle vehicles: {noi}/{self.V} ({round((time.time() - s1), 2)}s)') + if veh.rebl: + nor += 1 + print(f' +picked reqs: {nop}, dropped reqs: {nod}') + print(f' +idle vehs: {noi}/{self.V}, rebl vehs: {nor}/{self.V} ' + f'({round((time.time() - s1), 2)}s)') # generate requests up to time T, loading from reqs data file def gen_reqs_to_time(self): @@ -201,9 +205,7 @@ def gen_reqs_to_time(self): assert self.N == len(self.reqs) if IS_DEBUG: - print(f' +reqs in queue: {len(self.queue)}, ' - f'reqs in pool: {len(self.queue) + len(self.reqs_picking) + len(self.reqs_unassigned)} ' - f'({round((time.time() - s3), 2)}s)') + print(f' +new received reqs: {len(self.queue)} ({round((time.time() - s3), 2)}s)') # debug # print(f'reqs in queue: {[r.id for r in self.queue]}; reqs picking:{[r.id for r in self.reqs_picking]};' diff --git a/run.py b/run.py index 1ac942f..769fbff 100755 --- a/run.py +++ b/run.py @@ -25,10 +25,16 @@ # start time of simulation stime = time.time() # dispatch the system for T_TOTAL seconds, at the interval of INT_ASSIGN - for T in tqdm(range(INT_ASSIGN, T_TOTAL+INT_ASSIGN, INT_ASSIGN), desc=f'AMoD simulation (Δt={INT_ASSIGN}s)'): - model.dispatch_at_time(T) - if IS_ANIMATION and T_WARM_UP < T <= T_WARM_UP + T_STUDY: - frames_vehs.append(copy.deepcopy(model.vehs)) + if IS_DEBUG: + for T in range(INT_ASSIGN, T_TOTAL + INT_ASSIGN, INT_ASSIGN): + model.dispatch_at_time(T) + if IS_ANIMATION and T_WARM_UP < T <= T_WARM_UP + T_STUDY: + frames_vehs.append(copy.deepcopy(model.vehs)) + else: + for T in tqdm(range(INT_ASSIGN, T_TOTAL + INT_ASSIGN, INT_ASSIGN), desc=f'AMoD simulation (Δt={INT_ASSIGN}s)'): + model.dispatch_at_time(T) + if IS_ANIMATION and T_WARM_UP < T <= T_WARM_UP + T_STUDY: + frames_vehs.append(copy.deepcopy(model.vehs)) if IS_DEBUG: if DISPATCHER == 'OSP':