Skip to content

Commit

Permalink
Merge pull request #630 from cathywu/visualizer
Browse files Browse the repository at this point in the history
Minor changes to RLlib visualizer
  • Loading branch information
AboudyKreidieh committed Jul 13, 2019
2 parents 4f4bd42 + 1a83e37 commit 5c5a124
Showing 1 changed file with 33 additions and 9 deletions.
42 changes: 33 additions & 9 deletions flow/visualize/visualizer_rllib.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ def visualizer_rllib(args):
'python ./visualizer_rllib.py /tmp/ray/result_dir 1 --run PPO')
sys.exit(1)

sim_params.restart_instance = False
sim_params.restart_instance = True
dir_path = os.path.dirname(os.path.realpath(__file__))
emission_path = '{0}/test_time_rollout/'.format(dir_path)
sim_params.emission_path = emission_path if args.gen_emission else None
Expand All @@ -115,6 +115,9 @@ def visualizer_rllib(args):
sim_params.pxpm = 4
elif args.render_mode == 'sumo_gui':
sim_params.render = True
print('NOTE: With render mode {}, an extra instance of the SUMO GUI '
'will display before the GUI for visualizing the result. Click '
'the green Play arrow to continue.'.format(args.render_mode))
elif args.render_mode == 'no_render':
sim_params.render = False
if args.save_render:
Expand All @@ -123,8 +126,7 @@ def visualizer_rllib(args):
sim_params.save_render = True

# Create and register a gym+rllib env
create_env, env_name = make_create_env(
params=flow_params, version=0)
create_env, env_name = make_create_env(params=flow_params, version=0)
register_env(env_name, create_env)

# check if the environment is a single or multiagent environment, and
Expand Down Expand Up @@ -192,8 +194,10 @@ def visualizer_rllib(args):
env.restart_simulation(
sim_params=sim_params, render=sim_params.render)

# Simulate and collect metrics
final_outflows = []
mean_speed = []
std_speed = []
for i in range(args.num_rollouts):
vel = []
state = env.reset()
Expand Down Expand Up @@ -236,23 +240,42 @@ def visualizer_rllib(args):
outflow = vehicles.get_outflow_rate(500)
final_outflows.append(outflow)
mean_speed.append(np.mean(vel))
std_speed.append(np.std(vel))
if multiagent:
for agent_id, rew in rets.items():
print('Round {}, Return: {} for agent {}'.format(
i, ret, agent_id))
else:
print('Round {}, Return: {}'.format(i, ret))

print('==== Summary of results ====')
print("Return:")
print(mean_speed)
if multiagent:
for agent_id, rew in rets.items():
print('For agent', agent_id)
print(rew)
print('Average, std return: {}, {} for agent {}'.format(
np.mean(rew), np.std(rew), agent_id))
else:
print('Average, std return: {}, {}'.format(
print(rets)
print('Average, std: {}, {}'.format(
np.mean(rets), np.std(rets)))
print('Average, std speed: {}, {}'.format(
np.mean(mean_speed), np.std(mean_speed)))
print('Average, std outflow: {}, {}'.format(
np.mean(final_outflows), np.std(final_outflows)))

print("\nSpeed, mean (m/s):")
print(mean_speed)
print('Average, std: {}, {}'.format(np.mean(mean_speed), np.std(
mean_speed)))
print("\nSpeed, std (m/s):")
print(std_speed)
print('Average, std: {}, {}'.format(np.mean(std_speed), np.std(
std_speed)))

# Compute arrival rate of vehicles in the last 500 sec of the run
print("\nOutflows (veh/hr):")
print(final_outflows)
print('Average, std: {}, {}'.format(np.mean(final_outflows),
np.std(final_outflows)))

# terminate the environment
env.unwrapped.terminate()
Expand Down Expand Up @@ -333,7 +356,8 @@ def create_parser():
parser.add_argument(
'--save_render',
action='store_true',
help='saves the render to a file')
help='Saves a rendered video to a file. NOTE: Overrides render_mode '
'with pyglet rendering.')
parser.add_argument(
'--horizon',
type=int,
Expand Down

0 comments on commit 5c5a124

Please sign in to comment.