-
Notifications
You must be signed in to change notification settings - Fork 378
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add MPG reward #931
Add MPG reward #931
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -330,3 +330,109 @@ def energy_consumption(env, gain=.001): | |
power += M * speed * accel + M * g * Cr * speed + 0.5 * rho * A * Ca * speed ** 3 | ||
|
||
return -gain * power | ||
|
||
|
||
def veh_energy_consumption(env, veh_id, gain=.001): | ||
"""Calculate power consumption of a vehicle. | ||
|
||
Assumes vehicle is an average sized vehicle. | ||
The power calculated here is the lower bound of the actual power consumed | ||
by a vehicle. | ||
""" | ||
power = 0 | ||
|
||
M = 1200 # mass of average sized vehicle (kg) | ||
g = 9.81 # gravitational acceleration (m/s^2) | ||
Cr = 0.005 # rolling resistance coefficient | ||
Ca = 0.3 # aerodynamic drag coefficient | ||
rho = 1.225 # air density (kg/m^3) | ||
A = 2.6 # vehicle cross sectional area (m^2) | ||
speed = env.k.vehicle.get_speed(veh_id) | ||
prev_speed = env.k.vehicle.get_previous_speed(veh_id) | ||
|
||
accel = abs(speed - prev_speed) / env.sim_step | ||
|
||
power += M * speed * accel + M * g * Cr * speed + 0.5 * rho * A * Ca * speed ** 3 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. do you have a reference especially for some of the coefficients (e.g., Cr)? if yes, you can add it to the docstring There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, they're from Joy and are custom values |
||
|
||
return -gain * power | ||
|
||
|
||
def miles_per_megajoule(env, veh_ids=None, gain=.001): | ||
"""Calculate miles per mega-joule of either a particular vehicle or the total average of all the vehicles. | ||
|
||
Assumes vehicle is an average sized vehicle. | ||
The power calculated here is the lower bound of the actual power consumed | ||
by a vehicle. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. params, returns? |
||
|
||
Parameters | ||
---------- | ||
env : flow.envs.Env | ||
the environment variable, which contains information on the current | ||
state of the system. | ||
veh_ids : [list] | ||
list of veh_ids to compute the reward over | ||
gain : float | ||
scaling factor for the reward | ||
""" | ||
mpj = 0 | ||
counter = 0 | ||
if veh_ids is None: | ||
veh_ids = env.k.vehicle.get_ids() | ||
elif not isinstance(veh_ids, list): | ||
veh_ids = [veh_ids] | ||
for veh_id in veh_ids: | ||
speed = env.k.vehicle.get_speed(veh_id) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what happens if veh_id is None? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. that'd be a higher up error. I can add an assert |
||
# convert to be positive since the function called is a penalty | ||
power = -veh_energy_consumption(env, veh_id, gain=1.0) | ||
if power > 0 and speed >= 0.0: | ||
counter += 1 | ||
# meters / joule is (v * \delta t) / (power * \delta t) | ||
mpj += speed / power | ||
if counter > 0: | ||
mpj /= counter | ||
|
||
# convert from meters per joule to miles per joule | ||
mpj /= 1609.0 | ||
# convert from miles per joule to miles per megajoule | ||
mpj *= 10**6 | ||
|
||
return mpj * gain | ||
|
||
|
||
def miles_per_gallon(env, veh_ids=None, gain=.001): | ||
"""Calculate mpg of either a particular vehicle or the total average of all the vehicles. | ||
|
||
Assumes vehicle is an average sized vehicle. | ||
The power calculated here is the lower bound of the actual power consumed | ||
by a vehicle. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. params and returns? |
||
|
||
Parameters | ||
---------- | ||
env : flow.envs.Env | ||
the environment variable, which contains information on the current | ||
state of the system. | ||
veh_ids : [list] | ||
list of veh_ids to compute the reward over | ||
gain : float | ||
scaling factor for the reward | ||
""" | ||
mpg = 0 | ||
counter = 0 | ||
if veh_ids is None: | ||
veh_ids = env.k.vehicle.get_ids() | ||
elif not isinstance(veh_ids, list): | ||
veh_ids = [veh_ids] | ||
for veh_id in veh_ids: | ||
speed = env.k.vehicle.get_speed(veh_id) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same, if veh_id is None? |
||
gallons_per_s = env.k.vehicle.get_fuel_consumption(veh_id) | ||
if gallons_per_s > 0 and speed >= 0.0: | ||
counter += 1 | ||
# meters / gallon is (v * \delta t) / (gallons_per_s * \delta t) | ||
mpg += speed / gallons_per_s | ||
if counter > 0: | ||
mpg /= counter | ||
|
||
# convert from meters per gallon to miles per gallon | ||
mpg /= 1609.0 | ||
|
||
return mpg * gain |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
add description of parameters and returns