def test_json(self): """ Integration test for json export and import workflow """ horizon = 500 additional_env_params = { "target_velocity": 8, "max-deacc": -1, "max-acc": 1, "num_steps": horizon } additional_net_params = { "length": 260, "lanes": 1, "speed_limit": 30, "resolution": 40 } vehicle_params = [ dict(veh_id="rl", num_vehicles=1, acceleration_controller=(RLController, {}), routing_controller=(ContinuousRouter, {})), dict(veh_id="idm", num_vehicles=21, acceleration_controller=(IDMController, {}), routing_controller=(ContinuousRouter, {})) ] flow_params = dict(sumo=dict(sim_step=0.1), env=dict(additional_params=additional_env_params), net=dict(no_internal_links=False, additional_params=additional_net_params), veh=vehicle_params, initial=dict(spacing="uniform", bunching=30, min_gap=0)) flow_env_name = "WaveAttenuationPOEnv" exp_tag = "stabilizing_the_ring_example" # experiment prefix flow_params['flowenv'] = flow_env_name flow_params['exp_tag'] = exp_tag flow_params['module'] = "stabilizing_the_ring" # an example # Just to make sure an env can be created successfully # using these current flow_params _, _ = make_create_env(flow_env_name, flow_params, version=0, exp_tag=exp_tag) config = ppo.DEFAULT_CONFIG.copy() # save the flow params for replay flow_json = json.dumps(flow_params, cls=NameEncoder, sort_keys=True, indent=4) config['env_config']['flow_params'] = flow_json # dump the config so we can fetch it json_out_file = '~/params.json' with open(os.path.expanduser(json_out_file), 'w+') as outfile: json.dump(config, outfile, cls=NameEncoder, sort_keys=True, indent=4) config = get_rllib_config(os.path.expanduser('~')) # Fetching values using utility function `get_flow_params` imported_flow_params, mce = \ get_flow_params(config) # Making sure the imported flow_params match the originals self.assertTrue(imported_flow_params == flow_params) # delete the created file os.remove(os.path.expanduser('~/params.json'))
parser.add_argument('--emission_to_csv', action='store_true', help='Specifies whether to convert the emission file ' 'created by sumo into a csv file') parser.add_argument( '--evaluate', action='store_true', help='Specifies whether to use the "evaluate" reward for the environment.') if __name__ == "__main__": args = parser.parse_args() result_dir = args.result_dir if args.result_dir[-1] != '/' \ else args.result_dir[:-1] config = get_rllib_config(result_dir) # Run on only one cpu for rendering purposes ray.init(num_cpus=1) config["num_workers"] = 1 flow_params = get_flow_params(config) # Create and register a gym+rllib env create_env, env_name = make_create_env(params=flow_params, version=0, render=False) register_env(env_name, create_env) agent_cls = get_agent_class(args.run) agent = agent_cls(env=env_name, config=config)
def main(args): global current_scenario, scenarios, SCENARIOS_PATH, agent, env ray.init(num_cpus=5) result_dir = None # parse the flow args if args.result_dir: result_dir = args.result_dir if args.result_dir[-1] != '/' \ else args.result_dir[:-1] config = get_rllib_config(result_dir) flow_params = get_flow_params(config) # Create and register a gym+rllib env create_env, env_name = make_create_env(params=flow_params, version=0, sumo_binary="sumo") register_env(env_name, create_env) agent_cls = get_agent_class("PPO") config["num_workers"] = 1 agent = agent_cls(env=env_name, config=config) checkpoint = result_dir + '/checkpoint-' + args.checkpoint_num agent.restore(checkpoint) # Recreate the scenario from the pickled parameters exp_tag = flow_params["exp_tag"] net_params = flow_params['net'] vehicles = flow_params['veh'] initial_config = flow_params['initial'] module = __import__("flow.scenarios", fromlist=[flow_params["scenario"]]) scenario_class = getattr(module, flow_params["scenario"]) module = __import__("flow.scenarios", fromlist=[flow_params["generator"]]) generator_class = getattr(module, flow_params["generator"]) scenario = scenario_class(name=exp_tag, generator_class=generator_class, vehicles=vehicles, net_params=net_params, initial_config=initial_config) # Start the environment with the gui turned on and a path for the # emission file module = __import__("flow.envs", fromlist=[flow_params["env_name"]]) env_class = getattr(module, flow_params["env_name"]) env_params = flow_params['env'] sumo_params = flow_params['sumo'] sumo_params.sumo_binary = "sumo" sumo_params.emission_path = "./test_time_rollout/" # FIXME this will actually start sumo, since base_env in flow # comes with a call to start sumo # make it start on the same port! # got to set up sumo with two ports env = env_class(env_params=env_params, sumo_params=sumo_params, scenario=scenario) env.sumo_params.port = 5500 env.restart_sumo(env.sumo_params) # now that you have the env, you need to pass it to next_simulation_step # so you can use it to feed actions task = None sumo_start_fn = functools.partial(start_sumo_executable, args.gui, args.sumo_args) if args.configuration_file: # Replace the built-in scenarios with a single, user-specified one. # We don't merge the lists to avoid clashes with two scenarios having is_default set. SCENARIOS_PATH = None name = os.path.basename(args.configuration_file) scenarios = { to_kebab_case(name): Scenario.from_config_json({ 'name': name, 'description': 'User-specified scenario', 'config_file': args.configuration_file, 'is_default': True }) } else: scenarios = load_scenarios_file({}, SCENARIOS_PATH) def setup_websockets_server(): return functools.partial( websocket_simulation_control, lambda: sumo_start_fn(getattr(current_scenario, 'config_file')), task) loop = asyncio.get_event_loop() # websockets ws_handler = setup_websockets_server() ws_server = websockets.serve(ws_handler, '0.0.0.0', 5678) # http app = setup_http_server(task, SCENARIOS_PATH, scenarios) http_server = loop.create_server(app.make_handler(), '0.0.0.0', 5000) loop.run_until_complete(http_server) loop.run_until_complete(ws_server) print("""Listening on: 127.0.0.1:5000 (HTTP) 127.0.0.1:5678 (WebSockets) """) loop.run_forever()