except ControllerOutOfLaneException: logger.warning(f"{veh_id} out of lane, skipped attaching sensors") vehicles_off_road.add(veh_id) valid_vehicles = {v for v in current_vehicles if v not in vehicles_off_road} obs, _, _, dones = smarts.observe_from(valid_vehicles) _record_data(smarts.elapsed_sim_time, obs, collected_data) # an example of how we might save the data per car observation_folder = "collected_observations" if not os.path.exists(observation_folder): os.makedirs(observation_folder) for car, data in collected_data.items(): outfile = f"{observation_folder}/{scenario.name}_{scenario.traffic_history.name}_{car}.pkl" with open(outfile, "wb") as of: pickle.dump(data, of) smarts.destroy() if __name__ == "__main__": parser = default_argument_parser("observation-collection-example") args = parser.parse_args() main( script=parser.prog, scenarios=args.scenarios, headless=args.headless, seed=args.seed, )
agent_id, smarts.elapsed_sim_time)) logger.warning(" ... with {}".format( observations[agent_id].events)) else: logger.info( "agent_id={} reached goal @ sim_time={}". format(agent_id, smarts.elapsed_sim_time)) logger.debug(" ... with {}".format( observations[agent_id].events)) del observations[agent_id] smarts.destroy() if __name__ == "__main__": parser = default_argument_parser("history-vehicles-replacement-example") parser.add_argument( "--replacements-per-episode", "-k", help="The number vehicles to randomly replace with agents per episode.", type=int, default=1, ) args = parser.parse_args() main( script=parser.prog, scenarios=args.scenarios, headless=args.headless, seed=args.seed, vehicles_to_replace=args.replacements_per_episode,
for episode in episodes(n=num_episodes): agent = open_agent_spec.build_agent() observations = env.reset() episode.record_scenario(env.scenario_log) dones = {"__all__": False} while not dones["__all__"]: agent_obs = observations[AGENT_ID] agent_action = agent.act(agent_obs) observations, rewards, dones, infos = env.step({AGENT_ID: agent_action}) episode.record_step(observations, rewards, dones, infos) del agent env.close() if __name__ == "__main__": parser = default_argument_parser("OpEn-trajectory-optimizer-example") args = parser.parse_args() main( scenarios=args.scenarios, sim_name=args.sim_name, headless=args.headless, num_episodes=args.episodes, seed=args.seed, )
or (psutil.cpu_count() / 2)) ray.init(num_cpus=max(num_cpus, 1)) ray.wait([ train.remote( training_scenarios, evaluation_scenarios, sim_name, headless, num_episodes, seed, ) ]) if __name__ == "__main__": parser = default_argument_parser("pytorch-example") parser.add_argument( "--evaluation-scenario", default="scenarios/loop", help="The scenario to use for evaluation.", type=str, ) args = parser.parse_args() main( training_scenarios=args.scenarios, evaluation_scenarios=[args.evaluation_scenario], sim_name=args.sim_name, headless=args.headless, num_episodes=args.episodes, seed=args.seed,
# envision_record_data_replay_path="./data_replay", ) for episode in episodes(n=num_episodes): agent = agent_spec.build_agent() observations = env.reset() episode.record_scenario(env.scenario_log) dones = {"__all__": False} while not dones["__all__"]: agent_obs = observations[AGENT_ID] agent_action = agent.act(agent_obs) observations, rewards, dones, infos = env.step( {AGENT_ID: agent_action}) episode.record_step(observations, rewards, dones, infos) env.close() if __name__ == "__main__": parser = default_argument_parser("trajectory-tracking-agent-example") args = parser.parse_args() main( scenarios=args.scenarios, sim_name=args.sim_name, headless=args.headless, num_episodes=args.episodes, seed=args.seed, )
agent_id: agent_spec.build_agent() for agent_id, agent_spec in agent_specs.items() } observations = env.reset() episode.record_scenario(env.scenario_log) dones = {"__all__": False} while not dones["__all__"]: actions = { agent_id: agents[agent_id].act(agent_obs) for agent_id, agent_obs in observations.items() } observations, rewards, dones, infos = env.step(actions) episode.record_step(observations, rewards, dones, infos) env.close() if __name__ == "__main__": parser = default_argument_parser("multi-agent-example") args = parser.parse_args() main( scenarios=args.scenarios, sim_name=args.sim_name, headless=args.headless, num_episodes=args.episodes, seed=args.seed, )
} batched_actions.append(actions) # Step all environments in parallel ( batched_observations, batched_rewards, batched_dones, batched_infos, ) = env.step(batched_actions) env.close() if __name__ == "__main__": parser = default_argument_parser("parallel-environment-example") parser.add_argument( "--num-agents", default=2, type=int, help="Number of ego agents to simulate in each environment.", ) parser.add_argument( "--num-stack", default=2, type=int, help= "Number of consecutive frames to stack in each environment's observation.", ) parser.add_argument( "--num-env",
visdom=False, seed=seed, fixed_timestep_sec=0.1, ) if max_episode_steps is None: max_episode_steps = 1000 for episode in episodes(n=num_episodes): env.reset() episode.record_scenario(env.scenario_log) for _ in range(max_episode_steps): env.step({}) episode.record_step({}, {}, {}, {}) env.close() if __name__ == "__main__": parser = default_argument_parser("egoless-example") args = parser.parse_args() main( scenarios=args.scenarios, sim_name=args.sim_name, headless=args.headless, num_episodes=args.episodes, seed=args.seed, )
# Wrap a single-agent env with SingleAgent wrapper to make `step` and `reset` # output compliant with gym spaces. env = SingleAgent(env) for episode in episodes(n=num_episodes): agent = agent_spec.build_agent() observation = env.reset() episode.record_scenario(env.scenario_log) done = False while not done: agent_action = agent.act(observation) observation, reward, done, info = env.step(agent_action) episode.record_step(observation, reward, done, info) env.close() if __name__ == "__main__": parser = default_argument_parser("single-agent-example") args = parser.parse_args() main( scenarios=args.scenarios, sim_name=args.sim_name, headless=args.headless, num_episodes=args.episodes, seed=args.seed, )