Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
FlexPool/hybrid_experiment.py
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
336 lines (303 sloc)
11.6 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pandas as pd | |
import time | |
import sys | |
from random import shuffle | |
import pdb | |
import os | |
def run(experiment_id, env, agent, num_steps, no_op_steps=2, average_cycle=1, cheat=False, cheat_cycle=15, down_sample = 1): | |
score = pd.DataFrame( | |
columns=[ | |
"dayofweek", | |
"minofday", | |
"requests", | |
"wait_time", | |
"passenger_requests", | |
"package_requests", | |
"passenger_reject", | |
"package_reject", | |
"total_service_time", | |
"total_idle_time", | |
"idle", | |
"gas", | |
"resource", | |
"dispatch", | |
"reward", | |
"effective_dist", | |
"actual_dist", | |
"agent_time", | |
"original_requests", | |
"curr_passengers", | |
"curr_packages", | |
] | |
) | |
vehicles, requests, __, __, __, __, __, __, __ = env.step() | |
for _ in range(no_op_steps - 2): | |
_, requests_, __, __, __, __, __, __, __ = env.step() | |
requests = requests.append(requests_) | |
# pdb.set_trace() | |
if agent: | |
agent.reset(requests, env.dayofweek, env.minofday) | |
vehicles, requests, __, __, __, __, __, __, __ = env.step() | |
start = time.time() | |
prev_reward = 0 | |
prev_eff_dist = 0 | |
prev_act_dist = 0 | |
N = len(vehicles) | |
for t in range(num_steps): | |
if cheat and t % cheat_cycle == 0: | |
if t > num_steps - 30: | |
break | |
future_requests = env.get_requests(num_steps=30) | |
agent.update_future_demand(future_requests) | |
agent_start = time.time() | |
if agent: | |
actions = agent.get_actions(vehicles, requests) | |
else: | |
actions = [] | |
agent_time = time.time() - agent_start | |
dispatch = len(actions) | |
dayofweek = env.dayofweek | |
minofday = env.minofday | |
vehicles, requests, wait, r_reject, g_reject, gas, idle, original_requests, vehicle_scores = env.step(actions) | |
####### EDITED HERE ######## | |
if t % 100 == 0: | |
fpath = "./experiments/results/vehicle_logs/" + str(experiment_id) | |
fname = "/status_" + str(t) + ".csv" | |
if not os.path.exists(fpath): | |
os.makedirs(fpath) | |
df = vehicles.set_index("id").join(vehicle_scores.set_index("id")) | |
save_path = fpath + fname | |
df.to_csv(save_path, index=False) | |
####### EDITED HERE ######## | |
avg_reward = vehicles.reward.mean() | |
eff_dist_total = vehicles.eff_dist.sum() | |
act_dist_total = vehicles.act_dist.sum() | |
total_service_time = vehicle_scores.service_time.sum() | |
total_idle_time = vehicle_scores.idle_time.sum() | |
curr_passengers = vehicles.curr_passengers.sum() | |
try: | |
curr_packages = vehicles.curr_packages.sum() | |
except: | |
curr_packages = 0 | |
score.loc[t] = ( | |
dayofweek, | |
minofday, | |
len(requests)*down_sample, | |
wait, | |
len(requests[requests.g_type == "r"])*down_sample, | |
len(requests[requests.g_type != "r"])*down_sample, | |
r_reject, | |
g_reject, | |
total_service_time, | |
total_idle_time, | |
idle, | |
gas, | |
sum(vehicles.available), | |
dispatch, | |
avg_reward - prev_reward, | |
eff_dist_total - prev_eff_dist, | |
act_dist_total - prev_act_dist, | |
agent_time, | |
original_requests, | |
curr_passengers, | |
curr_packages, | |
) | |
# pdb.set_trace() | |
prev_reward = avg_reward | |
prev_eff_dist = eff_dist_total | |
prev_act_dist = act_dist_total | |
if t > 0 and t % average_cycle == 0: | |
elapsed = time.time() - start | |
W, wait, r_reject, g_reject, dispatch, reward, effective_dist, actual_dist, psg, pkg = score.loc[ | |
t - average_cycle : t - 1, | |
[ | |
"requests", | |
"wait_time", | |
"passenger_reject", | |
"package_reject", | |
"dispatch", | |
"reward", | |
"effective_dist", | |
"actual_dist", | |
"passenger_requests", | |
"package_requests", | |
], | |
].sum() | |
print( | |
"t = {:d} ({:.0f} elapsed) // REQ: {:.0f} / PsG: {:.0f} / PkG: {:.0f} / PsRej: {:.0f} / PkRej: {:.0f}/ ACC: {:.1f} / WAIT: {:.0f} / DSP: {:.2f} / RWD: {:.1f} / ED: {:.1f} / AD: {:.1f}".format( | |
int(t * env.cycle), | |
elapsed, | |
W, | |
psg, | |
pkg, | |
r_reject, | |
g_reject, | |
(W - (r_reject + g_reject)), | |
wait, | |
dispatch / N, | |
reward, | |
effective_dist, | |
actual_dist, | |
) | |
) | |
sys.stdout.flush() | |
# pdb.set_trace() | |
return score, env.get_vehicles_score() | |
def load_trips(scenario, trip_path, sample_size, skiprows=0): | |
trip_cols = pd.read_csv(trip_path, nrows=1).columns | |
trips = pd.read_csv(trip_path, names=trip_cols, nrows=sample_size, skiprows=skiprows + 1) | |
trips["second"] -= trips.loc[0, "second"] | |
duration = int(trips.second.values[-1] / 60) | |
# pdb.set_trace() | |
dayofweek = trips.loc[0, "dayofweek"] | |
minofday = trips.loc[0, "hour"] * 60 + trips.loc[0, "minute"] | |
features = ["trip_time", "phash", "plat", "plon", "dhash", "dlat", "dlon", "second", "trip_distance", "hop_flag"] | |
if scenario == "DeepPool": | |
features = ["trip_time", "phash", "plat", "plon", "dhash", "dlat", "dlon", "second", "trip_distance"] | |
elif scenario == "Hybrid": | |
features = [ | |
"trip_time", | |
"phash", | |
"plat", | |
"plon", | |
"dhash", | |
"dlat", | |
"dlon", | |
"g_type", | |
"second", | |
"trip_distance", | |
"hop_flag", | |
] | |
elif scenario == "Hybrid_nohop": | |
features = [ | |
"trip_time", | |
"phash", | |
"plat", | |
"plon", | |
"dhash", | |
"dlat", | |
"dlon", | |
"g_type", | |
"second", | |
"trip_distance", | |
] | |
trips = trips[features] | |
return trips, dayofweek, minofday, duration | |
def load_trip_chunks(scenario, trip_path, num_trips, duration, offset=0, randomize=True): | |
trips, dayofweek, minofday, minutes = load_trips(scenario, trip_path, num_trips) | |
num_chunks = int(minutes / duration) | |
# pdb.set_trace() | |
chunks = [] | |
date = 1 | |
for _ in range(num_chunks): | |
trips["second"] -= trips.second.values[0] | |
chunk = trips[trips.second < (duration + offset) * 60.0] | |
chunks.append((chunk, date, dayofweek, minofday)) | |
trips = trips[trips.second >= (duration + offset) * 60.0] | |
minofday += duration | |
if minofday >= 1440: # 24 hour * 60 minute | |
minofday -= 1440 | |
dayofweek = (dayofweek + 1) % 7 | |
date += 1 | |
if randomize: | |
shuffle(chunks) | |
return chunks | |
def load_trip_eval(trip_path, num_trips, day_start=4, no_op_steps=30): | |
trips, dayofweek, minofday, minutes = load_trips(trip_path, num_trips) | |
chunks = [] | |
day_shift = (7 - dayofweek) % 7 | |
# Start at 6 am on Monday | |
trips = trips[trips.second >= ((day_shift * 24 + day_start) * 60 - no_op_steps) * 60] | |
dayofweek = 0 | |
minofday = day_start * 60 - no_op_steps | |
date = 1 + day_shift | |
while len(trips): | |
trips["second"] -= trips.second.values[0] | |
day_chunk = trips[trips.second < (24 * 60 + no_op_steps) * 60.0] | |
chunks.append((day_chunk, date, dayofweek, minofday)) | |
trips = trips[trips.second >= 24 * 60 * 60.0] | |
dayofweek = (dayofweek + 1) % 7 | |
date += 1 | |
if dayofweek == 0: | |
break | |
return chunks | |
def describe(score, episode, fpath): | |
passenger_requests = score.passenger_requests.sum() | |
package_requests = score.package_requests.sum() | |
total_requests = int(passenger_requests + package_requests) | |
total_wait = score.wait_time.sum() | |
total_passenger_reject = int(score.passenger_reject.sum()) | |
total_package_reject = int(score.package_reject.sum()) | |
passenger_reject_rate = float(total_passenger_reject / passenger_requests) | |
package_reject_rate = float(total_package_reject / package_requests) | |
total_reject = total_passenger_reject + total_package_reject | |
reject_rate = float(total_reject / total_requests) | |
total_idle = int(score.total_idle_time.sum()) | |
total_service = int(score.total_service_time.sum()) | |
total_reward = score.reward.sum() | |
avg_wait = total_wait / (total_requests - total_reject) | |
effort = float(total_idle) / (total_requests * 0.2 - total_reject) | |
avg_time = score.agent_time.mean() | |
avg_num_transitions = (total_requests - total_reject) / (score.original_requests.sum()) | |
distance_ratio = (score.effective_dist.sum()) / (score.actual_dist.sum()) | |
total_time = (score.actual_dist.sum()) / (score.original_requests.sum() * 15) | |
average_utilization_1 = score.total_service_time.sum() / ( | |
score.total_service_time.sum() + score.total_idle_time.sum() | |
) | |
avg_utl_2 = score.total_service_time.sum() / (score.total_service_time.sum() + score.idle.sum()) | |
print("----------------------------------- EPISODE %d SUMMARY -----------------------------------" % episode) | |
print( | |
"PASSENGER_REQUESTS: {0:d} / PASSENGER_REJECTS: {1:d} / PACKAGE_REQUESTS: {2:d} / GOODS_REJECTS: {3:d} / IDLE: {4:d} / SERVICE: {5:d} / REWARD: {6:.0f}/ RATIO_EFF_DIST: {7:.2f} / TRANS: {8:.4f}".format( | |
int(passenger_requests), | |
total_passenger_reject, | |
int(package_requests), | |
total_package_reject, | |
total_idle, | |
total_service, | |
total_reward, | |
distance_ratio, | |
avg_num_transitions, | |
) | |
) | |
print( | |
"WAIT TIME: {0:.2f} / RIDER REJECT RATE: {1:.3f} / GOODS REJECT RATE: {2:.3f} / OVERALL REJECT RATE: {3:.3f} / EFFORT: {4:.2f} / TIME: {5:.2f} / TRIP_TIME: {6:.4f} / AVG_UTL1: {7:.2f} / AVG_UTL2: {8:.2f}".format( | |
avg_wait, | |
passenger_reject_rate, | |
package_reject_rate, | |
reject_rate, | |
effort, | |
avg_time, | |
total_time, | |
average_utilization_1, | |
avg_utl_2, | |
) | |
) | |
f = open(fpath, "a") | |
f.write("\n----------------------------------- EPISODE %d SUMMARY -----------------------------------" % episode) | |
f.write( | |
"\nPASSENGER_REQUESTS: {0:d} / PASSENGER_REJECTS: {1:d} / PACKAGE_REQUESTS: {2:d} / GOODS_REJECTS: {3:d} / IDLE: {4:d} / SERVICE: {5:d} / REWARD: {6:.0f}/ RATIO_EFF_DIST: {7:.2f} / TRANS: {8:.4f}".format( | |
int(passenger_requests), | |
total_passenger_reject, | |
int(package_requests), | |
total_package_reject, | |
total_idle, | |
total_service, | |
total_reward, | |
distance_ratio, | |
avg_num_transitions, | |
) | |
) | |
f.write( | |
"\nWAIT TIME: {0:.2f} / RIDER REJECT RATE: {1:.3f} / GOODS REJECT RATE: {2:.3f} / OVERALL REJECT RATE: {3:.3f} / EFFORT: {4:.2f} / TIME: {5:.2f} / TRIP_TIME: {6:.4f} / AVG_UTL1: {7:.2f} / AVG_UTL2: {8:.2f}".format( | |
avg_wait, | |
passenger_reject_rate, | |
package_reject_rate, | |
reject_rate, | |
effort, | |
avg_time, | |
total_time, | |
average_utilization_1, | |
avg_utl_2, | |
) | |
) | |
f.close() |