text
stringlengths
1
93.6k
next_state, reward, dones, info = env.step(action)
state = next_state
if env.step_count >= 150 and env.current_waypoint_index == 0:
dones = True
# Save route at the beginning of the episode
if not saved_route:
initial_heading = np.deg2rad(env.vehicle.get_transform().rotation.yaw)
initial_vehicle_location = vector(env.vehicle.get_location())
# Save the route to plot them later
for way in env.route_waypoints:
route_relative = get_displacement_vector(initial_vehicle_location,
vector(way[0].transform.location),
initial_heading)
new_row = pd.DataFrame([['route', env.episode_idx, route_relative[0], route_relative[1]]],
columns=["model_id", "episode", "route_x", "route_y"])
df = pd.concat([df, new_row], ignore_index=True)
saved_route = True
vehicle_relative = get_displacement_vector(initial_vehicle_location, vector(env.vehicle.get_location()),
initial_heading)
waypoint_relative = get_displacement_vector(initial_vehicle_location,
vector(env.current_waypoint.transform.location), initial_heading)
if env.collision_state:
collision_speed, collision_interval, cps, cpm = env.collision_speed, env.collision_interval, env.cps, env.cpm
else:
collision_speed, collision_interval, cps, cpm = 0, None, 0, 0
new_row = pd.DataFrame(
[[model_id, env.episode_idx, env.step_count, env.vehicle.control.throttle, env.vehicle.control.steer,
vehicle_relative[0], vehicle_relative[1], reward,
env.distance_traveled,
env.vehicle.get_speed(), env.distance_from_center,
np.rad2deg(env.vehicle.get_angle(env.current_waypoint)),
waypoint_relative[0], waypoint_relative[1], None, None,
env.routes_completed, collision_speed, collision_interval, cps, cpm
]], columns=columns)
df = pd.concat([df, new_row], ignore_index=True)
if record_video:
# Add frame
rendered_frame = env.render(mode="rgb_array")
video_recorder.add_frame(rendered_frame)
if dones:
state = env.reset()
episode_idx += 1
saved_route = False
print("Episode ", episode_idx)
# Release video
if record_video:
video_recorder.release()
df.to_csv(csv_path, index=False)
plot_eval([csv_path])
summary_eval(csv_path)
if __name__ == "__main__":
model_ckpt = args["model"]
algorithm_dict = {
"PPO": PPO,
"DDPG": DDPG,
"SAC": SAC,
"CLIP-SAC": CLIPRewardedSAC,
"CLIP-PPO": CLIPRewardedPPO,
}
if CONFIG.algorithm not in algorithm_dict:
raise ValueError("Invalid algorithm name")
AlgorithmRL = algorithm_dict[CONFIG.algorithm]
observation_space, encode_state_fn = create_encode_state_fn(CONFIG.state, CONFIG)
action_space_type = 'continuous' if CONFIG.action_space_type != 'discrete' else 'discrete'
eval_suffix = ''
if args['density'] == 'empty':
activate_traffic_flow = False
tf_num = 0
eval_suffix += 'empty'
else:
activate_traffic_flow = True
if args['density'] == 'regular':
tf_num = 20
else:
tf_num = 40
eval_suffix += 'dense'
if args['town'] != 'Town02':
eval_suffix += args['town']
env = CarlaRouteEnv(obs_res=CONFIG.obs_res, host=args["host"], port=args["port"],
reward_fn=reward_functions[CONFIG.reward_fn], observation_space=observation_space,
encode_state_fn=encode_state_fn, fps=args["fps"], action_smoothing=CONFIG.action_smoothing,
eval=True, action_space_type=action_space_type, activate_spectator=True, activate_render=True,
activate_bev=True, activate_seg_bev=CONFIG.use_seg_bev, start_carla=True,
activate_traffic_flow=activate_traffic_flow, tf_num=tf_num, town=args["town"])
for wrapper_class_str in CONFIG.wrappers: