Skip to content

Commit

Permalink
update DEBUG_PRINTING in SBA and rebalancer
Browse files Browse the repository at this point in the history
  • Loading branch information
Leot6 committed Apr 24, 2021
1 parent 0127625 commit 3fe3a56
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 55 deletions.
2 changes: 1 addition & 1 deletion lib/dispatcher/gi/greedy_insertion.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []
Expand Down
6 changes: 5 additions & 1 deletion lib/dispatcher/osp/osp_assign.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 = []
Expand All @@ -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

57 changes: 26 additions & 31 deletions lib/dispatcher/sba/single_req_batch_assign.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 = []
Expand All @@ -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]
Expand All @@ -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
10 changes: 7 additions & 3 deletions lib/rebalancer/naive_rebalancer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []
Expand All @@ -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)')

18 changes: 9 additions & 9 deletions lib/simulator/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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'

Expand All @@ -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
Expand Down
14 changes: 8 additions & 6 deletions lib/simulator/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -167,15 +167,19 @@ 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:
nop += len(veh.new_picked_rids)
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):
Expand All @@ -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]};'
Expand Down
14 changes: 10 additions & 4 deletions run.py
Original file line number Diff line number Diff line change
Expand Up @@ -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':
Expand Down

0 comments on commit 3fe3a56

Please sign in to comment.