Exemple #1
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)
Exemple #2
0
def generate_demonstrations(params, env, eval_criteria, demo_behavior=None, use_mp_runner=True, db=None):
    demo_collector = DemonstrationCollector()
    save_dir = os.path.join(params["Experiment"]["dir"], "demonstrations")
    demo_generator = DemonstrationGenerator(env, params, demo_behavior, demo_collector, save_dir)
    if not os.path.exists(save_dir):
      os.makedirs(save_dir)
    demo_generator.generate_demonstrations(num_demo_episodes, eval_criteria, save_dir, use_mp_runner=use_mp_runner, db=db)
    demo_generator.dump_demonstrations(save_dir)
    return demo_generator.demonstrations
Exemple #3
0
def dump():
    ckpt_dir = [
        os.path.join('/home/ekumar/master_thesis/code/hythe-src/checkpoints/',
                     ck) for ck in
        os.listdir("/home/ekumar/master_thesis/code/hythe-src/checkpoints/")
    ]
    eval_criteria = {"goal_reached": lambda x: x}
    print(ckpt_dir)
    demos_dir = os.path.join('/home/ekumar/demos/')
    list_of_demos = []
    for cdir in ckpt_dir:
        print(f"Extracting result {cdir}")
        result = BenchmarkResult.load_results(cdir)
        democ = DemonstrationCollector()
        democ._collection_result = result
        democ._directory = demos_dir
        demos = democ.ProcessCollectionResult(eval_criteria)
        list_of_demos.extend(demos)
    # make the demonstrations dir in the exp root
    os.makedirs(
        "/home/ekumar/output/experiments/exp_c76fc949-e95f-4774-91ba-6bec575ada37/demonstrations/generated_demonstrations"
    )
    to_pickle(
        list_of_demos,
        "/home/ekumar/output/experiments/exp_c76fc949-e95f-4774-91ba-6bec575ada37/demonstrations/generated_demonstrations",
        "demonstrations")
    collector = DemonstrationCollector.load(
        "/home/ekumar/output/experiments/exp_c76fc949-e95f-4774-91ba-6bec575ada37/demonstrations/generated_demonstrations"
    )
    print("Total demonstations found:",
          len(collector.GetDemonstrationExperiences()))
    return
Exemple #4
0
def unpack_load_demonstrations(demo_root):
    demo_dir = os.path.join(demo_root,
                            "demonstrations/generated_demonstrations")
    collector = DemonstrationCollector.load(demo_dir)
    return collector, collector.GetDemonstrationExperiences()