def test_goal_reached_evaluator(self):
        params = ParameterServer()
        bp = ContinuousHighwayBlueprint(params)
        env = SingleAgentRuntime(blueprint=bp, render=True)
        env.reset()
        world = env._world

        eval_id = env._scenario._eval_agent_ids[0]
        observed_world = world.Observe([eval_id])[0]
        evaluator = GoalReached(params)
        action = np.array([0., 0.], dtype=np.float32)
        start_time = time.time()
        print(evaluator.Evaluate(observed_world, action))
        end_time = time.time()
        print(f"It took {end_time-start_time} seconds.")
 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)
Exemple #3
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)
Exemple #4
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
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"))
Exemple #6
0
    def __init__(self,
                 params=None,
                 number_of_senarios=250,
                 random_seed=0,
                 ml_behavior=None,
                 viewer=True):
        params["BehaviorIDMClassic"]["BrakeForLaneEnd"] = True
        left_lane = MergingLaneCorridorConfig(params=params,
                                              road_ids=[0, 1],
                                              min_vel=10.,
                                              max_vel=15.,
                                              s_min=5.,
                                              s_max=25.,
                                              lane_corridor_id=0,
                                              controlled_ids=None)
        right_lane = MergingLaneCorridorConfig(
            params=params,
            road_ids=[0, 1],
            lane_corridor_id=1,
            s_min=5.,
            s_max=25.,
            min_vel=8.,
            max_vel=12.,
            behavior_model=BehaviorMobilRuleBased(params),
            controlled_ids=True)
        scenario_generation = \
          ConfigWithEase(
            num_scenarios=number_of_senarios,
            map_file_name=os.path.join(os.path.dirname(__file__), "../../../environments/blueprints/merging/DR_DEU_Merging_MT_v01_shifted.xodr"),  # NOLINT
            random_seed=random_seed,
            params=params,
            lane_corridor_configs=[left_lane, right_lane])
        if viewer:
            viewer = MPViewer(params=params,
                              x_range=[-35, 35],
                              y_range=[-35, 35],
                              follow_agent_id=True)
        dt = 0.2
        evaluator = GoalReached(params)
        observer = NearestObserver(params)
        ml_behavior = ml_behavior

        super().__init__(scenario_generation=scenario_generation,
                         viewer=viewer,
                         dt=dt,
                         evaluator=evaluator,
                         observer=observer,
                         ml_behavior=ml_behavior)
Exemple #7
0
    def __init__(self,
                 params=None,
                 number_of_senarios=250,
                 random_seed=0,
                 ml_behavior=None,
                 viewer=True):

        params["BehaviorIDMClassic"]["DesiredVelocity"] = 30.
        left_lane = HighwayLaneCorridorConfig(params=params,
                                              road_ids=[16],
                                              lane_corridor_id=0,
                                              min_vel=25.0,
                                              max_vel=30.0,
                                              controlled_ids=None)
        right_lane = HighwayLaneCorridorConfig(params=params,
                                               road_ids=[16],
                                               lane_corridor_id=1,
                                               min_vel=25.0,
                                               max_vel=30.0,
                                               controlled_ids=True)
        scenario_generation = \
          ConfigWithEase(
            num_scenarios=number_of_senarios,
            map_file_name=os.path.join(os.path.dirname(__file__), "../../../environments/blueprints/highway/city_highway_straight.xodr"),  # NOLINT
            random_seed=random_seed,
            params=params,
            lane_corridor_configs=[left_lane, right_lane])
        if viewer:
            viewer = MPViewer(params=params,
                              x_range=[-35, 35],
                              y_range=[-35, 35],
                              follow_agent_id=True)
        dt = 0.2
        evaluator = GoalReached(params)
        observer = NearestObserver(params)
        ml_behavior = ml_behavior

        super().__init__(scenario_generation=scenario_generation,
                         viewer=viewer,
                         dt=dt,
                         evaluator=evaluator,
                         observer=observer,
                         ml_behavior=ml_behavior)
Exemple #8
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
Exemple #9
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
Exemple #10
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)
Exemple #11
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
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
Exemple #13
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)
# 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
env = HyDiscreteHighway(params=params,
                        scenario_generation=scenario_generator,
                        behavior=behavior,
                        evaluator=evaluator,
                        observer=observer,
                        viewer=viewer,
Exemple #15
0
    def __init__(self,
                 params=None,
                 number_of_senarios=250,
                 random_seed=0,
                 ml_behavior=None,
                 viewer=True):
        lane_corridors = []
        lane_corridors.append(
            IntersectionLaneCorridorConfig(
                params=params,
                source_pos=[-40, -3],
                sink_pos=[40, -3],
                behavior_model=BehaviorIntersectionRuleBased(params),
                min_vel=10.,
                max_vel=15.,
                ds_min=10.,
                ds_max=15.,
                s_min=5.,
                s_max=50.,
                controlled_ids=None))
        lane_corridors.append(
            IntersectionLaneCorridorConfig(
                params=params,
                source_pos=[40, 3],
                sink_pos=[-40, 3],
                behavior_model=BehaviorIntersectionRuleBased(params),
                min_vel=10.,
                max_vel=15.,
                ds_min=15.,
                ds_max=15.,
                s_min=5.,
                s_max=50.,
                controlled_ids=None))
        lane_corridors.append(
            IntersectionLaneCorridorConfig(
                params=params,
                source_pos=[3, -30],
                sink_pos=[-40, 3],
                behavior_model=BehaviorIntersectionRuleBased(params),
                min_vel=5.,
                max_vel=10.,
                ds_min=10.,
                ds_max=15.,
                s_min=10.,
                s_max=25.,
                controlled_ids=True))

        scenario_generation = \
          ConfigWithEase(
            num_scenarios=number_of_senarios,
            map_file_name=os.path.join(os.path.dirname(__file__), "../../../environments/blueprints/intersection/4way_intersection.xodr"),  # NOLINT
            random_seed=random_seed,
            params=params,
            lane_corridor_configs=lane_corridors)
        if viewer:
            viewer = MPViewer(params=params,
                              x_range=[-35, 35],
                              y_range=[-35, 35],
                              follow_agent_id=True)
        dt = 0.2
        evaluator = GoalReached(params)
        observer = NearestObserver(params)
        ml_behavior = ml_behavior

        super().__init__(scenario_generation=scenario_generation,
                         viewer=viewer,
                         dt=dt,
                         evaluator=evaluator,
                         observer=observer,
                         ml_behavior=ml_behavior)
Exemple #16
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