def __init__(self, action_wrapper, observer, evaluator, step_time, viewer, scenario_generator=None, render=False): Runtime.__init__(self, step_time=step_time, viewer=viewer, scenario_generator=scenario_generator, render=render) self._action_wrapper = action_wrapper self._observer = observer self._evaluator = evaluator
def test_python_model(self): param_server = ParameterServer( filename="modules/runtime/tests/data/deterministic_scenario.json") scenario_generation = DeterministicScenarioGeneration(num_scenarios=3, random_seed=0, params=param_server) viewer = MPViewer(params=param_server, follow_agent_id=False, use_world_bounds=True) env = Runtime(0.2, viewer, scenario_generation, render=True) behavior_model = DummyBehaviorModel(param_server) env.reset() env._world.get_agent(0).behavior_model = behavior_model env._world.get_agent(0).behavior_model.clone() np.testing.assert_array_equal( env._world.get_agent(0).behavior_model.plan(0.2, env._world)[1], np.array([0.2, 5111.626, 5106.8305 + 0.2, 1.5, 10])) env.reset() env._world.get_agent(0).behavior_model = behavior_model env._world.step(0.2) np.testing.assert_array_equal( env._world.get_agent(0).state, np.array([0.2, 5111.626, 5106.8305 + 0.2, 1.5, 10], dtype=np.float32)) env._world.step(0.2) np.testing.assert_array_equal( env._world.get_agent(0).state, np.array([0.4, 5111.626, 5106.8305 + 0.4, 1.5, 10], dtype=np.float32)) env._world.step(0.2) np.testing.assert_array_equal( env._world.get_agent(0).state, np.array([0.6, 5111.626, 5106.8305 + 0.6, 1.5, 10], dtype=np.float32)) print("History:", env._world.get_agent(0).history) # environment loop env.reset() for i in range(0, 7): env.step()
def test_runtime(self): """Asserts the runtime to make sure the basic functionality is given by the current state of BARK. """ param_server = ParameterServer( filename="tests/data/deterministic_scenario_test.json") scenario_generation = DeterministicScenarioGeneration(num_scenarios=3, random_seed=0, params=param_server) param_server["Visualization"]["Agents"]["DrawRoute", "Draw the local map of each agent", True] viewer = MPViewer(params=param_server, use_world_bounds=True) env = Runtime(0.2, viewer, scenario_generation, render=True) env.reset() agent_ids = [] agent_states = [] centers_of_driving_corridor= [] for key, agent in env._world.agents.items(): agent_ids.append(agent.id) agent_states.append(agent.state) for i in range(0, 5): print("Scenario {}:".format( str(env._scenario_generator._current_scenario_idx))) # assert scenario ids self.assertEqual(env._scenario_generator._current_scenario_idx, (i % 3) + 1) for _ in range(0, 35): # assert ids states_before = [] for i, (key, agent) in enumerate(env._world.agents.items()): self.assertEqual(key, agent.id) self.assertEqual(agent_ids[i], agent.id) states_before.append(agent.state) # TODO(@hart): why does this not work print(key, agent.goal_definition.goal_shape) env.step() # assert state has been changed by the step() function for i, (key, agent) in enumerate(env._world.agents.items()): np.testing.assert_equal(np.any(np.not_equal(states_before[i], agent.state)), True) # check whether the reset works env.reset() for i, (key, agent) in enumerate(env._world.agents.items()): self.assertEqual(key, agent.id) self.assertEqual(agent_ids[i], agent.id) np.testing.assert_array_equal(agent_states[i], agent.state)
def test_python_model(self): param_server = ParameterServer( filename="modules/runtime/tests/data/deterministic_scenario.json") scenario_generation = DeterministicScenarioGeneration(num_scenarios=3, random_seed=0, params=param_server) viewer = MPViewer(params=param_server, follow_agent_id=False, use_world_bounds=True) env = Runtime(0.2, viewer, scenario_generation, render=True) single_track_model = SingleTrackModel(param_server) behavior_model = DynamicBehaviorModel(single_track_model, param_server) env.reset() env._world.get_agent(0).behavior_model = behavior_model env._world.get_agent(0).behavior_model.clone() env.reset() env._world.get_agent(0).behavior_model = behavior_model env._world.get_agent(0).behavior_model.set_last_action(np.array([1., 2.])) print(env._world.get_agent(0).behavior_model.get_last_action()) env._world.step(0.2) print(env._world.get_agent(0).behavior_model.get_last_action())
def test_bark_agent_in_runtime(self): # check whether the bark agent really does work params = ParameterServer( filename="configurations/sac_highway/config.json") configuration = SACHighwayConfiguration(params) scenario_generator = configuration._scenario_generator viewer = MPViewer(params=params, x_range=[-50, 50], y_range=[-50, 50], follow_agent_id=100, screen_dims=[500, 500], use_world_bounds=False) env = Runtime(0.2, viewer, scenario_generator, render=True) env.reset() dynamic_model = env._world.agents[ env._scenario._eval_agent_ids[0]].dynamic_model bark_agent = BARKMLBehaviorModel(configuration, dynamic_model, env._scenario._eval_agent_ids) bark_agent.set_last_action(np.array([1., 0.])) env._world.get_agent( env._scenario._eval_agent_ids[0]).behavior_model = bark_agent for _ in range(0, 35): env.step()
def test_visualization(self): param_server = ParameterServer( filename="modules/runtime/tests/data/deterministic_scenario.json") scenario_generation = DeterministicScenarioGeneration( num_scenarios=3, random_seed=0, params=param_server) viewer = MPViewer(params=param_server, follow_agent_id=False, use_world_bounds=True) env = Runtime(0.2, viewer, scenario_generation, render=True) env.reset() for _ in range(0, 5): print("Scenario {}:".format( str(env._scenario_generator._current_scenario_idx))) for _ in range(0, 5): env.step() env.reset()
params = ParameterServer(filename=base_dir + "/configurations/highway/config.json") scenario_generation = params["Scenario"]["Generation"][ "ConfigurableScenarioGeneration"] map_filename = scenario_generation["MapFilename"] scenario_generation["MapFilename"] = base_dir + "/" + map_filename params["BaseDir"] = base_dir # actual model ml_config = HighwayConfiguration(params) ml_behavior = BARKMLBehaviorModel(configuration=ml_config) scenario_generator = ml_config._scenario_generator viewer = MPViewer(params=params, use_world_bounds=True) env = Runtime(0.2, viewer, scenario_generator, render=True) env.reset() env._world.agents[ env._scenario._eval_agent_ids[0]].behavior_model = ml_behavior print(ml_behavior) for _ in range(0, 50): env.step() # to find database files os.chdir("../benchmark_database/") dbs = DatabaseSerializer(test_scenarios=4, test_world_steps=5, num_serialize_scenarios=10) dbs.process("database")
# viewer viewer = MPViewer(params=param_server, use_world_bounds=True) # viewer = Panda3dViewer(params=param_server, # x_range=[-40, 40], # y_range=[-40, 40], # follow_agent_id=agent3.id) # World Simulation sim_step_time = param_server["simulation"]["step_time", "Step-time used in simulation", 0.2] sim_real_time_factor = param_server["simulation"][ "real_time_factor", "execution in real-time or faster", 1.] # viewer = VideoRenderer(renderer=viewer, # world_step_time=sim_step_time, # fig_path="/Users/hart/2019/bark/video") env = Runtime(step_time=0.2, viewer=viewer, scenario_generator=scenarios, render=True) # run 3 scenarios for _ in range(0, 3): env.reset() # step each scenario 20 times for step in range(0, 80): env.step() time.sleep(sim_step_time / sim_real_time_factor) # viewer.export_video(filename="/Users/hart/2019/bark/video/intersection", remove_image_dir=True)
scenario_generation = UniformVehicleDistribution(num_scenarios=10, random_seed=0, params=param_server) viewer = MPViewer(params=param_server, x_range=[5060, 5160], y_range=[5070, 5150]) sim_step_time = param_server["simulation"]["step_time", "Step-time used in simulation", 0.2] sim_real_time_factor = param_server["simulation"][ "real_time_factor", "execution in real-time or faster", 1] scenario, idx = scenario_generation.get_next_scenario() # Rendering WITHOUT intermediate steps video_renderer = VideoRenderer(renderer=viewer, world_step_time=sim_step_time) env = Runtime(0.2, video_renderer, scenario_generation, render=True) env.reset() for _ in range(0, 5): env.step() video_renderer.export_video(filename="examples/scenarios/test_video_step") # Rendering WITH intermediate steps video_renderer = VideoRenderer(renderer=viewer, world_step_time=sim_step_time, render_intermediate_steps=10) env = Runtime(0.2, video_renderer, scenario_generation, render=True) env.reset() for _ in range(0, 5): env.step() video_renderer.export_video(