def __init__(self, configuration=None, dynamic_model=None, agents_to_observe=None): DynamicBehaviorModel.__init__(self, dynamic_model, configuration._params) self._configuration = configuration self._dynamic_model = dynamic_model self._agents_to_observe = agents_to_observe
class BARKMLBehaviorModel(BehaviorModel): """This class makes a trained agent available as BehaviorModel in BARK. """ def __init__(self, configuration=None): BehaviorModel.__init__(self, configuration._params) self._configuration = configuration self._dynamic_behavior_model = DynamicBehaviorModel( self._configuration._behavior_model._dynamic_model, configuration._params) def Plan(self, delta_time, observed_world): self._configuration._observer.reset(observed_world, [0]) observed_state = self._configuration._observer.observe(observed_world) action = self._configuration._agent.act(observed_state) self._dynamic_behavior_model.SetLastAction(action) trajectory = self._dynamic_behavior_model.Plan(delta_time, observed_world) super(BARKMLBehaviorModel, self).SetLastTrajectory(trajectory) return trajectory def Clone(self): return self def __getstate__(self): try: del self.__dict__['_configuration'] except: pass try: del self.__dict__['_dynamic_behavior_model'] except: pass odict = self.__dict__.copy() return odict def __setstate__(self, sdict): # HACK base_dir = "/home/hart/Dokumente/2020/bark-ml" 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 sdict['_configuration'] = HighwayConfiguration(params) sdict['_dynamic_behavior_model'] = DynamicBehaviorModel( sdict['_configuration']._behavior_model._dynamic_model, sdict['_configuration']._params) BehaviorModel.__init__(self, sdict['_configuration']._params) self.__dict__.update(sdict)
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 __setstate__(self, sdict): # HACK base_dir = "/home/hart/Dokumente/2020/bark-ml" 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 sdict['_configuration'] = HighwayConfiguration(params) sdict['_dynamic_behavior_model'] = DynamicBehaviorModel( sdict['_configuration']._behavior_model._dynamic_model, sdict['_configuration']._params) BehaviorModel.__init__(self, sdict['_configuration']._params) self.__dict__.update(sdict)
def reset(self, world, agents_to_act): """see base class """ super(DynamicModel, self).reset(world=world, agents_to_act=agents_to_act) self._input_count = 0 self._behavior_models = [] for agent_id in agents_to_act: self._behavior_models.append( DynamicBehaviorModel(self._dynamic_model, self._params)) if agent_id in world.agents: world.agents[agent_id].behavior_model = self._behavior_models[ -1] world.agents[agent_id].set_move_automatically(False) else: raise ValueError("AgentID does not exist in world.") return world
def reset(self, world, agents_to_act): """see base class """ super(DynamicModel, self).reset(world=world, agents_to_act=agents_to_act) self._behavior_models = [] self._controlled_agents = agents_to_act for agent_id in agents_to_act: self._behavior_models.append( DynamicBehaviorModel(self._dynamic_model, self._params)) if agent_id in world.agents: actions = np.zeros(shape=(self._control_inputs), dtype=np.float32) self._behavior_models[-1].SetLastAction(actions) world.agents[agent_id].behavior_model = self._behavior_models[ -1] else: raise ValueError("AgentID does not exist in world.") return world
def __init__(self, configuration=None): BehaviorModel.__init__(self, configuration._params) self._configuration = configuration self._dynamic_behavior_model = DynamicBehaviorModel( self._configuration._behavior_model._dynamic_model, configuration._params)