Exemplo n.º 1
0
def main():
    args = configure_args()
    exp_dir = args.exp_dir or "results/training/toy_evaluation"
    params_filename = glob.glob(os.path.join(exp_dir, "params_*"))
    params = ParameterServer(filename=params_filename[0])
    behavior_params_filename = glob.glob(
        os.path.join(exp_dir, "behavior_params*"))
    if behavior_params_filename:
        splits = 8
        behavior_params = ParameterServer(filename=behavior_params_filename[0])
        behavior_space = BehaviorSpace(behavior_params)
        hypothesis_set, _ = behavior_space.create_hypothesis_set_fixed_split(
            split=splits)
        observer = BeliefObserver(params, hypothesis_set, splits=splits)
        behavior = BehaviorDiscreteMacroActionsML(behavior_params)
    else:
        behavior = BehaviorDiscreteMacroActionsML(params)
        observer = NearestAgentsObserver(params)

    evaluator = GoalReached(params)

    scenario_params = ParameterServer(
        filename=
        "configuration/database/scenario_sets/interaction_merging_light_dense_1D.json"
    )
    scenario_generator = ConfigurableScenarioGeneration(params=scenario_params,
                                                        num_scenarios=5)
    scenario_file = glob.glob(os.path.join(exp_dir, "scenarios_list*"))
    scenario_generator.load_scenario_list(scenario_file[0])
    viewer = MPViewer(params=params,
                      x_range=[-35, 35],
                      y_range=[-35, 35],
                      follow_agent_id=True)
    env = HyDiscreteHighway(behavior=behavior,
                            observer=observer,
                            evaluator=evaluator,
                            viewer=viewer,
                            scenario_generation=scenario_generator,
                            render=True)

    env.reset()
    actions = [0, 1, 2, 3, 4, 5, 6]
    for action in actions:
        print(action)
        env.step(action)
    agent = FQFAgent(env=env, test_env=env, params=params)

    agent.load_models(os.path.join(exp_dir, "agent/checkpoints/final"))
Exemplo n.º 2
0
    def test_iqn_agent(self):
        params = ParameterServer()
        params["ML"]["BaseAgent"]["NumSteps"] = 2
        params["ML"]["BaseAgent"]["MaxEpisodeSteps"] = 2

        bp = DiscreteHighwayBlueprint(params,
                                      number_of_senarios=10,
                                      random_seed=0)
        env = SingleAgentRuntime(blueprint=bp, render=False)
        env._observer = NearestAgentsObserver(params)
        env._action_wrapper = BehaviorDiscreteMacroActionsML(params)

        iqn_agent = IQNAgent(agent_save_dir="./save_dir",
                             env=env,
                             params=params)
        iqn_agent.train_episode()

        iqn_agent.save(checkpoint_type="best")
        iqn_agent.save(checkpoint_type="last")

        loaded_agent = IQNAgent(agent_save_dir="./save_dir",
                                checkpoint_load="best")
        loaded_agent2 = IQNAgent(agent_save_dir="./save_dir",
                                 checkpoint_load="last")

        loaded_agent_with_env = IQNAgent(env=env,
                                         agent_save_dir="./save_dir",
                                         checkpoint_load="last")
        loaded_agent_with_env.train_episode()

        self.assertEqual(loaded_agent.ml_behavior.action_space.n,
                         iqn_agent.ml_behavior.action_space.n)
        return
Exemplo n.º 3
0
 def test_obs_traj(self):
     map_filename = "external/bark_ml_project/bark_ml/environments/blueprints/highway/city_highway_straight.xodr"
     params = ParameterServer()
     behavior = BehaviorDiscreteMacroActionsML(params)
     evaluator = GoalReached(params)
     observer = NearestAgentsObserver(params)
     viewer = MPViewer(params=params,
                       x_range=[-35, 35],
                       y_range=[-35, 35],
                       follow_agent_id=True)
     env = HyDiscreteHighway(params=params,
                             map_filename=map_filename,
                             behavior=behavior,
                             evaluator=evaluator,
                             observer=observer,
                             viewer=viewer,
                             render=True)
     env.reset()
     actions = np.random.randint(0, 7, 100)
     for action in actions:
         concatenated_state, _, _, _ = env.step(action)
         nn_ip_state = concatenated_state
         ego_nn_input_state = deepcopy(
             concatenated_state[0:observer._len_ego_state])
         reverted_observed_state = observer.rev_observe_for_ego_vehicle(
             nn_ip_state)
         ext_reverted_observed_state = np.zeros(
             (reverted_observed_state.shape[0] + 1))
         ext_reverted_observed_state[1:] = reverted_observed_state
         renormed_ego_state = observer._select_state_by_index(
             observer._norm(ext_reverted_observed_state))
         time.sleep(0.2)
         np.testing.assert_array_almost_equal(ego_nn_input_state,
                                              renormed_ego_state)
Exemplo n.º 4
0
    def test_collect_demonstrations(self):
        params = ParameterServer()
        bp = DiscreteHighwayBlueprint(params,
                                      number_of_senarios=10,
                                      random_seed=0)
        env = SingleAgentRuntime(blueprint=bp, render=False)
        env._observer = NearestAgentsObserver(params)
        env._action_wrapper = BehaviorDiscreteMacroActionsML(params)
        env._evaluator = TestEvaluator()

        demo_behavior = bark_ml.library_wrappers.lib_fqf_iqn_qrdqn.\
                tests.test_demo_behavior.TestDemoBehavior(params)
        collector = DemonstrationCollector()
        collection_result = collector.CollectDemonstrations(env, demo_behavior, 4, "./test_demo_collected", \
               use_mp_runner=False, runner_init_params={"deepcopy" : False})
        self.assertTrue(
            os.path.exists("./test_demo_collected/collection_result"))
        print(collection_result.get_data_frame().to_string())

        experiences = collector.ProcessCollectionResult(
            eval_criteria={"goal_r1": lambda x: x})
        # expected length = 2 scenarios (only every second reaches goal) x 3 steps (4 executed, but first not counted)
        self.assertEqual(len(experiences), 2 * 3)

        collector.dump("./final_collections")

        loaded_collector = DemonstrationCollector.load("./final_collections")
        experiences_loaded = loaded_collector.GetDemonstrationExperiences()
        print(experiences_loaded)
        self.assertEqual(len(experiences_loaded), 2 * 3)
Exemplo n.º 5
0
 def __init__(self, params=None, number_of_senarios=25, random_seed=0):
     ml_behavior = BehaviorDiscreteMacroActionsML(params)
     MergingBlueprint.__init__(self,
                               params=params,
                               number_of_senarios=number_of_senarios,
                               random_seed=random_seed,
                               ml_behavior=ml_behavior,
                               viewer=True)
Exemplo n.º 6
0
def main():
    args = configure_args()
    if is_local:
        dir_prefix = ""
    else:
        dir_prefix="hy-iqn-lfd-full-exp.runfiles/hythe/"
    logging.info(f"Executing job: {args.jobname}")
    logging.info(f"Experiment server at: {os.getcwd()}")
    params = ParameterServer(filename=os.path.join(dir_prefix, params_file),
                             log_if_default=True)
    params = configure_params(params, seed=args.jobname)
    experiment_id = params["Experiment"]["random_seed"]
    params_filename = os.path.join(params["Experiment"]["dir"], "params_{}.json".format(experiment_id))

    behavior = BehaviorDiscreteMacroActionsML(params)
    evaluator = GoalReached(params)
    observer = NearestAgentsObserver(params)
    viewer = MPViewer(params=params,
                      x_range=[-35, 35],
                      y_range=[-35, 35],
                      follow_agent_id=True)

    # extract params and save experiment parameters
    params["ML"]["BaseAgent"]["SummaryPath"] = os.path.join(params["Experiment"]["dir"], "agent/summaries")
    params["ML"]["BaseAgent"]["CheckpointPath"] = os.path.join(params["Experiment"]["dir"], "agent/checkpoints")

    params.Save(filename=params_filename)
    logging.info('-' * 60)
    logging.info("Writing params to :{}".format(params_filename))
    logging.info('-' * 60)

    # database creation
    dbs1 = DatabaseSerializer(test_scenarios=1, test_world_steps=2,
                             num_serialize_scenarios=num_demo_scenarios)
    dbs1.process(os.path.join(dir_prefix, "configuration/database"),
      filter_sets="**/**/interaction_merging_light_dense_1D.json")
    local_release_filename = dbs1.release(version="lfd_offline")
    db1 = BenchmarkDatabase(database_root=local_release_filename)
    scenario_generator1, _, _ = db1.get_scenario_generator(0)

    env = HyDiscreteHighway(params=params,
                            scenario_generation=scenario_generator1,
                            behavior=behavior,
                            evaluator=evaluator,
                            observer=observer,
                            viewer=viewer,
                            render=False)

    scenario, _ = scenario_generator1.get_next_scenario()
    world = scenario.GetWorldState()
    observer.Reset(world)

    assert env.action_space._n == 8, "Action Space is incorrect!"
    run(args, params, env, db=db1)
    params.Save(params_filename)
    logging.info('-' * 60)
    logging.info("Writing params to :{}".format(params_filename))
    logging.info('-' * 60)
Exemplo n.º 7
0
def main():
    map_filename = "external/bark_ml_project/bark_ml/environments/blueprints/highway/city_highway_straight.xodr"
    params_filename = glob.glob(os.path.join(demo_root, "params_[!behavior]*"))
    params = ParameterServer(filename=params_filename[0], log_if_default=True)
    behavior = BehaviorDiscreteMacroActionsML(params)
    evaluator = GoalReached(params)
    if is_belief_observer:
        splits = 2
        bparams_filename = glob.glob(os.path.join(demo_root, "behavior_*"))
        params_behavior = ParameterServer(filename=bparams_filename[0])
        behavior_space = BehaviorSpace(params_behavior)

        hypothesis_set, hypothesis_params = behavior_space.create_hypothesis_set_fixed_split(
            split=splits)
        observer = BeliefObserver(params, hypothesis_set, splits=splits)
    else:
        observer = NearestAgentsObserver(params)
    viewer = MPViewer(params=params,
                      x_range=[-35, 35],
                      y_range=[-35, 35],
                      follow_agent_id=True)
    env = HyDiscreteHighway(params=params,
                            map_filename=map_filename,
                            behavior=behavior,
                            evaluator=evaluator,
                            observer=observer,
                            viewer=viewer,
                            render=False)

    env.reset()

    ego_world_states = []
    _, demonstrations = unpack_load_demonstrations(demo_root)
    for demo in demonstrations:
        ego_state = np.zeros((observer._len_ego_state + 1))
        (nn_ip_state, action, reward, next_state, done, is_demo) = demo
        ego_nn_input_state = deepcopy(nn_ip_state[0:observer._len_ego_state])
        ego_state[1:] = ego_nn_input_state
        reverted_observed_state = observer.rev_observe_for_ego_vehicle(
            ego_state)
        ego_world_states.append(
            (reverted_observed_state[int(StateDefinition.X_POSITION)],
             reverted_observed_state[int(StateDefinition.Y_POSITION)],
             reverted_observed_state[int(StateDefinition.THETA_POSITION)],
             reverted_observed_state[int(StateDefinition.VEL_POSITION)],
             action, int(is_demo)))
    df = pd.DataFrame(ego_world_states,
                      columns=[
                          'pos_x', 'pos_y', 'orientation', 'velocity',
                          'action', 'is_demo'
                      ])
    print(df.head(10))
    df.to_pickle(os.path.join(demo_root, "demonstrations/demo_dataframe"))
    return
Exemplo n.º 8
0
 def __init__(self,
              params=None,
              num_scenarios=25,
              random_seed=0,
              mode="dense"):
     ml_behavior = BehaviorDiscreteMacroActionsML(params)
     HighwayBlueprint.__init__(self,
                               params=params,
                               num_scenarios=num_scenarios,
                               random_seed=random_seed,
                               ml_behavior=ml_behavior,
                               viewer=True,
                               mode=mode)
Exemplo n.º 9
0
 def test_env(self):
     map_filename = "external/bark_ml_project/bark_ml/environments/blueprints/highway/city_highway_straight.xodr"
     params = ParameterServer()
     behavior = BehaviorDiscreteMacroActionsML(params)
     evaluator = GoalReached(params)
     observer = NearestAgentsObserver(params)
     viewer = MPViewer(params=params,
                       x_range=[-35, 35],
                       y_range=[-35, 35],
                       follow_agent_id=True)
     env = HyDiscreteHighway(params=params,
                             map_filename=map_filename,
                             behavior=behavior,
                             evaluator=evaluator,
                             observer=observer,
                             viewer=viewer,
                             render=True)
     env.reset()
     actions = [5] * 100
     print(actions)
     for action in actions:
         env.step(action)
         time.sleep(0.2)
     return
Exemplo n.º 10
0
def main():
    print("Experiment server at:", os.getcwd())
    params = ParameterServer(
        filename=
        "hy-exp-run.runfiles/hythe/configuration/params/default_exp_runne_params.json"
    )
    params = configure_params(params)
    num_scenarios = 5
    random_seed = 0
    behavior = BehaviorDiscreteMacroActionsML(params)
    # evaluator = GoalReachedGuiding(params)
    evaluator = GoalReached(params)
    observer = NearestAgentsObserver(params)
    viewer = MPViewer(params=params,
                      x_range=[-35, 35],
                      y_range=[-35, 35],
                      follow_agent_id=True)
    experiment_id = params["Experiment"]["random_seed"]
    params_filename = "./{}_default_exp_runner_params.json".format(
        experiment_id)
    print(params_filename)
    params.Save(filename=params_filename)
    # database creation
    dbs = DatabaseSerializer(
        test_scenarios=2, test_world_steps=2, num_serialize_scenarios=20
    )  # increase the number of serialize scenarios to 100
    dbs.process("hy-exp-run.runfiles/hythe/configuration/database")
    local_release_filename = dbs.release(version="test",
                                         sub_dir="hy_bark_packaged_databases")
    db = BenchmarkDatabase(database_root=local_release_filename)

    # switch this to other generator to get other index
    # scenario_generator, _, _ = db.get_scenario_generator(0)
    scenario_generator, _, _ = db.get_scenario_generator(1)
    #
    # env = GymSingleAgentRuntime(ml_behavior = behavior,
    #                             observer = observer,
    #                             evaluator = evaluator,
    #                             step_time=0.2,
    #                             viewer=viewer,
    #                             scenario_generator=scenario_generator,
    #                             render=False)

    env = HyDiscreteHighway(params=params,
                            scenario_generation=scenario_generator,
                            behavior=behavior,
                            evaluator=evaluator,
                            observer=observer,
                            viewer=viewer,
                            render=False)

    run(params, env)
    # video_renderer.drawWorld(env._world)
    env._viewer.export_video("./test_video")
    # from gym.envs.registration import register
    # register(
    #     id='highway-v1',
    #     entry_point='bark_ml.environments.gym:DiscreteHighwayGym'
    # )
    # import gym
    # env = gym.make("highway-v1")
    # env.reset()
    # actions = [5]*100
    # print(actions)
    # for action in actions:
    #     env.step(action)
    #     time.sleep(0.2)
    return
Exemplo n.º 11
0
def main():
    args = configure_args()
    if is_local:
        dir_prefix = ""
    else:
        dir_prefix = "hy-x-iqn-beliefs.runfiles/hythe/"
    print("Experiment server at :", os.getcwd())
    params = ParameterServer(filename=os.path.join(
        dir_prefix, "configuration/params/iqn_params.json"))
    params = configure_params(params, seed=args.jobname)
    experiment_id = params["Experiment"]["random_seed"]
    params_filename = os.path.join(params["Experiment"]["dir"],
                                   "params_{}.json".format(experiment_id))
    params_behavior_filename = os.path.join(
        params["Experiment"]["dir"],
        "behavior_params_{}.json".format(experiment_id))

    # check if exp exists and handle preemption
    exp_exists = check_if_exp_exists(params)
    if exp_exists:
        print("Loading existing experiment from: {}".format(
            args.jobname, (params["Experiment"]["dir"])))
        if os.path.isfile(params_filename):
            params = ParameterServer(filename=params_filename,
                                     log_if_default=True)
        if os.path.isfile(params_behavior_filename):
            params_behavior = ParameterServer(
                filename=params_behavior_filename, log_if_default=True)
    else:
        Path(params["Experiment"]["dir"]).mkdir(parents=True, exist_ok=True)
    params["ML"]["BaseAgent"]["SummaryPath"] = os.path.join(
        params["Experiment"]["dir"], "agent/summaries")
    params["ML"]["BaseAgent"]["CheckpointPath"] = os.path.join(
        params["Experiment"]["dir"], "agent/checkpoints")

    params_behavior = ParameterServer(filename=os.path.join(
        dir_prefix, "configuration/params/1D_desired_gap_no_prior.json"),
                                      log_if_default=True)
    params.Save(filename=params_filename)
    params_behavior.Save(filename=params_behavior_filename)

    # configure belief observer
    splits = 2
    behavior_space = configure_behavior_space(params_behavior)

    hypothesis_set, hypothesis_params = behavior_space.create_hypothesis_set_fixed_split(
        split=splits)
    observer = BeliefObserver(params, hypothesis_set, splits=splits)
    behavior = BehaviorDiscreteMacroActionsML(params)
    evaluator = GoalReached(params)

    viewer = MPViewer(params=params,
                      x_range=[-35, 35],
                      y_range=[-35, 35],
                      follow_agent_id=True)

    # database creation
    dbs = DatabaseSerializer(test_scenarios=2,
                             test_world_steps=2,
                             num_serialize_scenarios=num_scenarios)
    dbs.process(os.path.join(dir_prefix, "configuration/database"),
                filter_sets="**/**/interaction_merging_mid_dense_1D_new.json")
    local_release_filename = dbs.release(version="test")
    db = BenchmarkDatabase(database_root=local_release_filename)
    scenario_generator, _, _ = db.get_scenario_generator(scenario_set_id=0)
    env = HyDiscreteHighway(params=params,
                            scenario_generation=scenario_generator,
                            behavior=behavior,
                            evaluator=evaluator,
                            observer=observer,
                            viewer=viewer,
                            render=is_local)
    print('Observation/state space size', env.observation_space)

    run(params, env, exp_exists)
    params.Save(filename=params_filename)
    logging.info('-' * 60)
    logging.info("Writing params to :{}".format(params_filename))
    logging.info('-' * 60)
    params_behavior.Save(filename=params_behavior_filename)
    logging.info('-' * 60)
    logging.info(
        "Writing behavior params to :{}".format(params_behavior_filename))
    logging.info('-' * 60)

    return
Exemplo n.º 12
0
def main():
    args = configure_args()
    if is_local:
        dir_prefix = ""
    else:
        dir_prefix = "hy-iqnfd-beliefs-exp.runfiles/hythe/"
    print("Executing job :", args.jobname)
    print("Experiment server at :", os.getcwd())
    params = ParameterServer(filename=os.path.join(
        dir_prefix, "configuration/params/iqn_params_demo_full_local.json"),
                             log_if_default=True)
    params = configure_params(params, seed=args.jobname)
    experiment_id = params["Experiment"]["random_seed"]
    params_filename = os.path.join(params["Experiment"]["dir"],
                                   "params_{}.json".format(experiment_id))

    behavior = BehaviorDiscreteMacroActionsML(params)
    evaluator = GoalReached(params)

    params_behavior_filename = os.path.join(
        params["Experiment"]["dir"],
        "behavior_params_{}.json".format(experiment_id))
    params_behavior = ParameterServer(filename=os.path.join(
        dir_prefix, "configuration/params/1D_desired_gap_no_prior.json"),
                                      log_if_default=True)
    params_behavior.Save(filename=params_behavior_filename)

    # configure belief observer
    splits = 2
    behavior_space = BehaviorSpace(params_behavior)

    hypothesis_set, hypothesis_params = behavior_space.create_hypothesis_set_fixed_split(
        split=splits)
    observer = BeliefObserver(params, hypothesis_set, splits=splits)

    viewer = MPViewer(params=params,
                      x_range=[-35, 35],
                      y_range=[-35, 35],
                      follow_agent_id=True)

    # extract params and save experiment parameters
    params["ML"]["BaseAgent"]["SummaryPath"] = os.path.join(
        params["Experiment"]["dir"], "agent/summaries")
    params["ML"]["BaseAgent"]["CheckpointPath"] = os.path.join(
        params["Experiment"]["dir"], "agent/checkpoints")

    params.Save(filename=params_filename)
    logging.info('-' * 60)
    logging.info("Writing params to :{}".format(params_filename))
    logging.info('-' * 60)

    # database creation
    dbs = DatabaseSerializer(test_scenarios=1,
                             test_world_steps=2,
                             num_serialize_scenarios=num_scenarios)
    dbs.process(os.path.join(dir_prefix, "configuration/database"),
                filter_sets="**/**/interaction_merging_light_dense_1D.json")
    local_release_filename = dbs.release(version="test")
    db = BenchmarkDatabase(database_root=local_release_filename)
    scenario_generator, _, _ = db.get_scenario_generator(0)

    env = HyDiscreteHighway(params=params,
                            scenario_generation=scenario_generator,
                            behavior=behavior,
                            evaluator=evaluator,
                            observer=observer,
                            viewer=viewer,
                            render=False)
    scenario, _ = scenario_generator.get_next_scenario()
    world = scenario.GetWorldState()
    observer.Reset(world)

    assert env.action_space._n == 8, "Action Space is incorrect!"
    run(args, params, env, db=db)
    params.Save(params_filename)
    logging.info('-' * 60)
    logging.info("Writing params to :{}".format(params_filename))
    logging.info('-' * 60)
    params_behavior.Save(filename=params_behavior_filename)
    logging.info('-' * 60)
    logging.info(
        "Writing behavior params to :{}".format(params_behavior_filename))
    logging.info('-' * 60)
Exemplo n.º 13
0
def main():
    map_filename = "external/bark_ml_project/bark_ml/environments/blueprints/highway/city_highway_straight.xodr"
    params = ParameterServer(filename=os.path.join(
        exp_root, "params_iqn_pre_exp_2sdi64.json"),
                             log_if_default=True)
    params_behavior = ParameterServer(filename=os.path.join(
        exp_root, "behavior_params_iqn_pre_exp_2sdi64.json"))
    behavior = BehaviorDiscreteMacroActionsML(params)
    evaluator = GoalReached(params)
    if is_belief_observer:
        splits = 2
        behavior_space = BehaviorSpace(params_behavior)

        hypothesis_set, hypothesis_params = behavior_space.create_hypothesis_set_fixed_split(
            split=splits)
        observer = BeliefObserver(params, hypothesis_set, splits=splits)
    else:
        observer = NearestAgentsObserver(params)
    viewer = MPViewer(params=params,
                      x_range=[-35, 35],
                      y_range=[-35, 35],
                      follow_agent_id=True)

    # database creation
    dbs1 = DatabaseSerializer(test_scenarios=1,
                              test_world_steps=2,
                              num_serialize_scenarios=1)
    dbs1.process(os.path.join("", "configuration/database"),
                 filter_sets="**/**/interaction_merging_light_dense_1D.json")
    local_release_filename = dbs1.release(version="lfd_offline")
    db1 = BenchmarkDatabase(database_root=local_release_filename)
    scenario_generator1, _, _ = db1.get_scenario_generator(0)

    env = HyDiscreteHighway(params=params,
                            scenario_generation=scenario_generator1,
                            map_filename=map_filename,
                            behavior=behavior,
                            evaluator=evaluator,
                            observer=observer,
                            viewer=viewer,
                            render=False)

    scenario, _ = scenario_generator1.get_next_scenario()
    world = scenario.GetWorldState()
    observer.Reset(world)
    env.reset()

    agent = pick_agent(exp_root, env, params)
    ego_world_states = []
    memory = agent.memory
    learned_states = memory["state"]
    actions = memory['action']
    is_demos = memory['is_demo']
    for state, action, is_demo in zip(learned_states, actions, is_demos):
        ego_state = np.zeros((observer._len_ego_state + 1))
        ego_nn_input_state = deepcopy(state[0:observer._len_ego_state])
        ego_state[1:] = ego_nn_input_state
        reverted_observed_state = observer.rev_observe_for_ego_vehicle(
            ego_state)
        ego_world_states.append(
            (reverted_observed_state[int(StateDefinition.X_POSITION)],
             reverted_observed_state[int(StateDefinition.Y_POSITION)],
             reverted_observed_state[int(StateDefinition.THETA_POSITION)],
             reverted_observed_state[int(StateDefinition.VEL_POSITION)],
             action[0], is_demo[0]))
    df = pd.DataFrame(ego_world_states,
                      columns=[
                          'pos_x', 'pos_y', 'orientation', 'velocity',
                          'action', 'is_demo'
                      ])
    print(df.head(10))
    if not os.path.exists(os.path.join(exp_root, "demonstrations")):
        os.makedirs(os.path.join(exp_root, "demonstrations"))
    df.to_pickle(os.path.join(exp_root, "demonstrations", "learned_dataframe"))
    return
Exemplo n.º 14
0
def main():
    print("Experiment server at :", os.getcwd())

    args = configure_args()
    #load exp params
    exp_dir = args.output_dir
    params_filename = glob.glob(os.path.join(exp_dir, "params_[!behavior]*"))
    params = ParameterServer(filename=params_filename[0])
    params.load(fn=params_filename[0])
    params["ML"]["BaseAgent"]["SummaryPath"] = os.path.join(exp_dir, "agent/summaries")
    params["ML"]["BaseAgent"]["CheckpointPath"] = os.path.join(exp_dir, "agent/checkpoints")
    splits = 8
    behavior_params_filename = glob.glob(os.path.join(exp_dir, "behavior_params*"))
    if behavior_params_filename:
      params_behavior = ParameterServer(filename=behavior_params_filename[0])
    else:
      params_behavior = ParameterServer(filename="configuration/params/1D_desired_gap_no_prior.json")
    behavior_space = configure_behavior_space(params_behavior)

    hypothesis_set, hypothesis_params = behavior_space.create_hypothesis_set_fixed_split(split=splits)
    observer = BeliefObserver(params, hypothesis_set, splits=splits)
    behavior = BehaviorDiscreteMacroActionsML(params_behavior)
    evaluator = GoalReached(params)

    viewer = MPViewer(params=params,
                      x_range=[-35, 35],
                      y_range=[-35, 35],
                      follow_agent_id=True)


    # database creation
    dir_prefix = ""
    dbs = DatabaseSerializer(test_scenarios=2, test_world_steps=2,
                             num_serialize_scenarios=10)
    dbs.process(os.path.join(dir_prefix, "configuration/database"), filter_sets="**/**/interaction_merging_light_dense_1D.json")
    local_release_filename = dbs.release(version="test")
    db = BenchmarkDatabase(database_root=local_release_filename)
    scenario_generator, _, _ = db.get_scenario_generator(0)

    video_renderer = VideoRenderer(renderer=viewer, world_step_time=0.2)
    env = HyDiscreteHighway(params=params,
                            scenario_generation=scenario_generator,
                            behavior=behavior,
                            evaluator=evaluator,
                            observer=observer,
                            viewer=video_renderer,
                            render=is_local)

    # non-agent evaluation mode
    num_steps = 100
    num_samples = params_behavior["BehaviorSpace"]["Hypothesis"]["BehaviorHypothesisIDM"]["NumSamples"]
    print("Steps, samples, splits", num_steps, num_samples, splits)
    step = 1
    env.reset()

    threshold = observer.is_enabled_threshold
    discretize = observer.is_discretize
    
    beliefs_df = pd.DataFrame(columns=["Step", "Action", "Agent", "Beliefs", "HyNum"])
    beliefs_orig_df = pd.DataFrame(columns=["Step", "Action", "Agent", "Beliefs", "HyNum"])
    while step <= num_steps:
        action = 5 #np.random.randint(0, behavior.action_space.n)
        next_state, reward, done, info = env.step(action)
        for agent, beliefs in observer.beliefs.items():
            beliefs = np.asarray(beliefs)
            oring = deepcopy(beliefs)
            for i, belief in enumerate(oring):
                beliefs_orig_df = beliefs_orig_df.append({"Step": step, "Action": action, "Agent": agent, "Beliefs": belief, "HyNum": i}, ignore_index=True)
            if discretize:
              beliefs = observer.discretize_beliefs(beliefs)
            if threshold:
              beliefs = observer.threshold_beliefs(beliefs)
            for i, belief in enumerate(beliefs):
                beliefs_df = beliefs_df.append({"Step": step, "Action": action, "Agent": agent, "Beliefs": belief, "HyNum": i}, ignore_index=True)
        step += 1

    suffix = "switch"
    if threshold:
      suffix += "_threshold"
    if discretize:
      suffix += "_discretize"
    beliefs_data_filename = "beliefs_{}_{}_{}".format(splits, num_samples, num_steps)
    beliefs_data_filename += suffix
    print(beliefs_data_filename)
    beliefs_df.to_pickle(os.path.join(str(Path.home()), "master_thesis/code/hythe-src/beliefs_data/", beliefs_data_filename))

    beliefs_data_filename = "orig_beliefs_{}_{}_{}".format(splits, num_samples, num_steps)
    beliefs_data_filename += suffix
    print(beliefs_data_filename)
    beliefs_orig_df.to_pickle(os.path.join(str(Path.home()), "master_thesis/code/hythe-src/beliefs_data/", beliefs_data_filename))

    video_filename = os.path.join(str(Path.home()), "master_thesis/code/hythe-src/beliefs_data/", "video_{}".format(num_samples))
    print(video_filename)
    video_filename += suffix
    print(video_filename)
    video_renderer.export_video(filename=video_filename)
    return
Exemplo n.º 15
0
def main():
    args = configure_args()
    if is_local:
        dir_prefix = ""
    else:
        dir_prefix = "hy-iqn-exp.runfiles/hythe/"
    print("Executing job :", args.jobname)
    print("Experiment server at :", os.getcwd())
    params = ParameterServer(filename=os.path.join(
        dir_prefix, "configuration/params/iqn_params.json"),
                             log_if_default=True)
    params = configure_params(params, seed=args.jobname)
    experiment_id = params["Experiment"]["random_seed"]
    params_filename = os.path.join(params["Experiment"]["dir"],
                                   "params_{}.json".format(experiment_id))

    # check if exp exists and handle preemption
    exp_exists = check_if_exp_exists(params)
    if exp_exists:
        print("Loading existing experiment from: {}".format(
            args.jobname, (params["Experiment"]["dir"])))
        if os.path.isfile(params_filename):
            params = ParameterServer(filename=params_filename,
                                     log_if_default=True)
    else:
        Path(params["Experiment"]["dir"]).mkdir(parents=True, exist_ok=True)

    behavior = BehaviorDiscreteMacroActionsML(params)
    evaluator = GoalReached(params)
    observer = NearestAgentsObserver(params)
    viewer = MPViewer(params=params,
                      x_range=[-35, 35],
                      y_range=[-35, 35],
                      follow_agent_id=True)

    # extract params and save experiment parameters
    params["ML"]["BaseAgent"]["SummaryPath"] = os.path.join(
        params["Experiment"]["dir"], "agent/summaries")
    params["ML"]["BaseAgent"]["CheckpointPath"] = os.path.join(
        params["Experiment"]["dir"], "agent/checkpoints")

    params.Save(filename=params_filename)
    logging.info('-' * 60)
    logging.info("Writing params to :{}".format(params_filename))
    logging.info('-' * 60)

    # database creation
    dbs = DatabaseSerializer(test_scenarios=2,
                             test_world_steps=2,
                             num_serialize_scenarios=num_scenarios)
    dbs.process(os.path.join(dir_prefix, "configuration/database"),
                filter_sets="**/**/interaction_merging_light_dense_1D.json")
    local_release_filename = dbs.release(version="test")
    db = BenchmarkDatabase(database_root=local_release_filename)
    scenario_generator, _, _ = db.get_scenario_generator(0)

    env = HyDiscreteHighway(params=params,
                            scenario_generation=scenario_generator,
                            behavior=behavior,
                            evaluator=evaluator,
                            observer=observer,
                            viewer=viewer,
                            render=False)
    assert env.action_space._n == 8, "Action Space is incorrect!"
    run(params, env, exp_exists)
    params.Save(params_filename)
    logging.info('-' * 60)
    logging.info("Writing params to :{}".format(params_filename))
    logging.info('-' * 60)
Exemplo n.º 16
0
    def __init__(self, env, test_env, params, bark_behavior=None):
        BehaviorModel.__init__(self, params)
        self._params = params
        self.env = env
        self.test_env = test_env
        self._bark_behavior_model = bark_behavior or BehaviorDiscreteMacroActionsML(
            params)

        self.device = torch.device("cuda" if self._params["ML"]["BaseAgent"][
            "Cuda", "", True] and torch.cuda.is_available() else "cpu")

        self.online_net = None
        self.target_net = None

        self.steps = 0
        self.learning_steps = 0
        self.episodes = 0
        self.best_eval_score = -np.inf
        self.num_actions = self.env.action_space.n
        self.num_steps = self._params["ML"]["BaseAgent"]["NumSteps", "",
                                                         5000000]
        self.batch_size = self._params["ML"]["BaseAgent"]["BatchSize", "", 32]

        self.double_q_learning = self._params["ML"]["BaseAgent"][
            "Double_q_learning", "", False]
        self.dueling_net = self._params["ML"]["BaseAgent"]["DuelingNet", "",
                                                           False]
        self.noisy_net = self._params["ML"]["BaseAgent"]["NoisyNet", "", False]
        self.use_per = self._params["ML"]["BaseAgent"]["Use_per", "", False]

        self.reward_log_interval = self._params["ML"]["BaseAgent"][
            "RewardLogInterval", "", 5]
        self.summary_log_interval = self._params["ML"]["BaseAgent"][
            "SummaryLogInterval", "", 100]
        self.eval_interval = self._params["ML"]["BaseAgent"]["EvalInterval",
                                                             "", 25000]
        self.num_eval_steps = self._params["ML"]["BaseAgent"]["NumEvalSteps",
                                                              "", 12500]
        self.gamma_n = \
         self._params["ML"]["BaseAgent"]["Gamma", "", 0.99] ** \
         self._params["ML"]["BaseAgent"]["Multi_step", "", 1]

        self.start_steps = self._params["ML"]["BaseAgent"]["StartSteps", "",
                                                           5000]
        self.epsilon_train = LinearAnneaer(
            1.0, self._params["ML"]["BaseAgent"]["EpsilonTrain", "", 0.01],
            self._params["ML"]["BaseAgent"]["EpsilonDecaySteps", "", 25000])
        self.epsilon_eval = self._params["ML"]["BaseAgent"]["EpsilonEval", "",
                                                            0.001]
        self.update_interval = \
         self._params["ML"]["BaseAgent"]["Update_interval", "", 4]
        self.target_update_interval = self._params["ML"]["BaseAgent"][
            "TargetUpdateInterval", "", 5000]
        self.max_episode_steps = \
         self._params["ML"]["BaseAgent"]["MaxEpisodeSteps",  "", 10000]
        self.grad_cliping = self._params["ML"]["BaseAgent"]["GradCliping", "",
                                                            5.0]

        self.summary_dir = \
         self._params["ML"]["BaseAgent"]["SummaryPath", "", ""]
        self.model_dir = \
         self._params["ML"]["BaseAgent"]["CheckpointPath", "", ""]

        if not os.path.exists(self.model_dir) and self.model_dir:
            os.makedirs(self.model_dir)
        if not os.path.exists(self.summary_dir) and self.summary_dir:
            os.makedirs(self.summary_dir)

        self.writer = SummaryWriter(log_dir=self.summary_dir)
        self.train_return = RunningMeanStats(self.summary_log_interval)

        # NOTE: by default we do not want the action to be set externally
        #       as this enables the agents to be plug and played in BARK.
        self._set_action_externally = False

        # Replay memory which is memory-efficient to store stacked frames.
        if self.use_per:
            beta_steps = (self.num_steps - self.start_steps) / \
                   self.update_interval
            self.memory = LazyPrioritizedMultiStepMemory(
                self._params["ML"]["BaseAgent"]["MemorySize", "", 10**6],
                self.env.observation_space.shape,
                self.device,
                self._params["ML"]["BaseAgent"]["Gamma", "", 0.99],
                self._params["ML"]["BaseAgent"]["Multi_step", "", 1],
                beta_steps=beta_steps)
        else:
            self.memory = LazyMultiStepMemory(
                self._params["ML"]["BaseAgent"]["MemorySize", "", 10**6],
                self.env.observation_space.shape, self.device,
                self._params["ML"]["BaseAgent"]["Gamma", "", 0.99],
                self._params["ML"]["BaseAgent"]["Multi_step", "", 1])
Exemplo n.º 17
0
 def test_discrete_behavior(self):
     params = ParameterServer()
     discrete_behavior = BehaviorDiscreteMacroActionsML(params)
     # sets 0-th motion primitive active
     discrete_behavior.ActionToBehavior(0)
     print(discrete_behavior.action_space)
Exemplo n.º 18
0
dbs.process("configuration/database", filter_sets="**/**/interaction_merging_light_dense_1D.json")
local_release_filename = dbs.release(version="resume")

db = BenchmarkDatabase(database_root=local_release_filename)
scenario_generator, _, _ = db.get_scenario_generator(0)

# load belief observer specifics
if is_belief_observer:
  splits = 2
  behavior_params_filename = glob.glob(os.path.join(exp_dir, "behavior_params*"))[0]
  params_behavior = ParameterServer(filename=behavior_params_filename, log_if_default=True)
  behavior_space = BehaviorSpace(params_behavior)

  hypothesis_set, hypothesis_params = behavior_space.create_hypothesis_set_fixed_split(split=splits)
  observer = BeliefObserver(params, hypothesis_set, splits=splits)
  behavior = BehaviorDiscreteMacroActionsML(params_behavior)
# if not, load default observer
else:
  behavior = BehaviorDiscreteMacroActionsML(params)
  observer = NearestAgentsObserver(params)

evaluator = GoalReached(params)

viewer = MPViewer(
  params=params,
  center= [960, 1000.8],
  enforce_x_length=True,
  x_length = 100.0,
  use_world_bounds=False)

# load env