Skip to content

Commit

Permalink
Exception handling (#654)
Browse files Browse the repository at this point in the history
* exception handling

* Update flow/core/kernel/vehicle/traci.py

Co-Authored-By: Aboudy Kreidieh <akreidieh@gmail.com>
  • Loading branch information
cathywu and AboudyKreidieh committed Aug 2, 2019
1 parent 681d200 commit 5b30957
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 9 deletions.
17 changes: 11 additions & 6 deletions flow/core/kernel/vehicle/traci.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Script containing the TraCI vehicle kernel class."""
import traceback

from flow.core.kernel.vehicle import KernelVehicle
import traci.constants as tc
Expand Down Expand Up @@ -207,7 +208,7 @@ def update(self, reset):
self.__vehicles[veh_id]["timestep"] = _time_step
self.__vehicles[veh_id]["timedelta"] = _time_delta
except TypeError:
pass
print(traceback.format_exc())
headway = vehicle_obs.get(veh_id, {}).get(tc.VAR_LEADER, None)
# check for a collided vehicle or a vehicle with no leader
if headway is None:
Expand All @@ -221,7 +222,7 @@ def update(self, reset):
try:
self.__vehicles[headway[0]]["follower"] = veh_id
except KeyError:
pass
print(traceback.format_exc())

# update the sumo observations variable
self.__sumo_obs = vehicle_obs.copy()
Expand Down Expand Up @@ -821,6 +822,7 @@ def _next_edge_leaders(self, veh_id, edge_dict, lane, num_edges):
- self.get_length(leader)
except KeyError:
# current edge has no vehicles, so move on
# print(traceback.format_exc())
continue

# stop if a lane follower is found
Expand Down Expand Up @@ -865,6 +867,7 @@ def _prev_edge_followers(self, veh_id, edge_dict, lane, num_edges):
follower = edge_dict[edge][lane][-1][0]
except KeyError:
# current edge has no vehicles, so move on
# print(traceback.format_exc())
continue

# stop if a lane follower is found
Expand Down Expand Up @@ -952,16 +955,18 @@ def update_vehicle_colors(self):
try:
# color rl vehicles red
self.set_color(veh_id=veh_id, color=RED)
except (FatalTraCIError, TraCIException):
pass
except (FatalTraCIError, TraCIException) as e:
print('Error when updating rl vehicle colors:', e)
print(traceback.format_exc())

# color vehicles white if not observed and cyan if observed
for veh_id in self.get_human_ids():
try:
color = CYAN if veh_id in self.get_observed_ids() else WHITE
self.set_color(veh_id=veh_id, color=color)
except (FatalTraCIError, TraCIException):
pass
except (FatalTraCIError, TraCIException) as e:
print('Error when updating human vehicle colors:', e)
print(traceback.format_exc())

# clear the list of observed vehicles
for veh_id in self.get_observed_ids():
Expand Down
4 changes: 2 additions & 2 deletions flow/envs/base_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,7 @@ def reset(self):
try:
self.k.vehicle.remove(veh_id)
except (FatalTraCIError, TraCIException):
pass
print(traceback.format_exc())

# clear all vehicles from the network and the vehicles class
# FIXME (ev, ak) this is weird and shouldn't be necessary
Expand Down Expand Up @@ -671,7 +671,7 @@ def terminate(self):
self.renderer.close()
except FileNotFoundError:
# Skip automatic termination. Connection is probably already closed
pass
print(traceback.format_exc())

def render(self, reset=False, buffer_length=5):
"""Render a frame.
Expand Down
2 changes: 1 addition & 1 deletion flow/multiagent_envs/multiagent_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ def reset(self, new_inflow_rate=None):
try:
self.k.vehicle.remove(veh_id)
except (FatalTraCIError, TraCIException):
pass
print(traceback.format_exc())

# clear all vehicles from the network and the vehicles class
# FIXME (ev, ak) this is weird and shouldn't be necessary
Expand Down

0 comments on commit 5b30957

Please sign in to comment.