Exemple #1
0
def run_caterpillar(actor,
                    save_dir: str,
                    steps: int,
                    disable_list=None,
                    broken_value=0):
    if disable_list is None:
        disable_list = []

    caterpillar = Caterpillar(config.somites, mode="default")

    # Record during sampling
    sim_distance_file = DataCSVSaver(os.path.join(save_dir, "distance.txt"),
                                     ("step", "distance"))
    sim_phase_diffs_file = DataCSVSaver(
        os.path.join(save_dir, "phase_diffs.txt"),
        ["step"] + ["phi_{}".format(i) for i in range(config.oscillators)])
    sim_phases_file = DataCSVSaver(
        os.path.join(save_dir, "phases.txt"),
        ["step"] + ["phi_{}".format(i) for i in range(config.oscillators)])
    sim_actions_file = DataCSVSaver(
        os.path.join(save_dir, "actions.txt"),
        ["step"] + ["action_{}".format(i) for i in range(config.oscillators)])
    sim_frictions_file = DataCSVSaver(
        os.path.join(save_dir, "frictions.txt"),
        ["step"] + ["friction_{}".format(i) for i in range(config.somites)])
    sim_tension_file = DataCSVSaver(
        os.path.join(save_dir, "tensions.txt"),
        ["step"] + ["tension_{}".format(i) for i in range(config.oscillators)])

    for step in range(steps):
        obv, action = observe_and_act(actor,
                                      caterpillar,
                                      disable_list=disable_list,
                                      broken_value=broken_value)
        _, frictions, tensions = obv
        caterpillar.step(step, int(1 / config.params["time_dalte"] / 10))

        # Save data
        sim_distance_file.append_data(step, caterpillar.moved_distance())
        sim_phase_diffs_file.append_data(step, *caterpillar.phases_from_base())
        sim_phases_file.append_data(step, *caterpillar.phis)
        sim_actions_file.append_data(step, *action)
        sim_frictions_file.append_data(step, *frictions)
        sim_tension_file.append_data(step, *tensions)

    caterpillar.save_simulation("{}/render.sim".format(save_dir))

    return caterpillar.moved_distance()
def test_current_params(actor: base_actor, log_dir: str, episode: int):
    steps = input("How many steps for this sample?: ")
    if steps == "":
        utils.notice("default steps {}".format(config.params["default_sample_steps"]))
        steps = config.params["default_sample_steps"]

    # Record during sampling
    sim_distance_file = DataCSVSaver(
        "{}/train_result_ep{}_distance.txt".format(log_dir, episode),
        ("step", "distance")
    )
    sim_phase_diffs_file = DataCSVSaver(
        "{}/train_result_ep{}_phase_diffs.txt".format(log_dir, episode),
        ["step"] + ["phi_{}".format(i) for i in range(config.oscillators)]
    )
    sim_actions_file = DataCSVSaver(
        "{}/train_result_ep{}_actions.txt".format(log_dir, episode),
        ["step"] + ["action_{}".format(i) for i in range(config.oscillators)]
    )
    sim_frictions_file = DataCSVSaver(
        "{}/train_result_ep{}_frictions.txt".format(log_dir, episode),
        ["step"] + ["friction_{}".format(i) for i in range(config.somites)]
    )

    caterpillar = Caterpillar(config.somites, mode="default")
    for step in range(int(steps)):
        try:
            _, action = caterpillar_runner.observe_and_act(actor, caterpillar, [])
            caterpillar.step(step, int(1 / config.params["time_dalte"] / 10))
        except Exception:
            continue
        else:
            # Save data
            sim_distance_file.append_data(step, caterpillar.moved_distance())
            sim_phase_diffs_file.append_data(step, *caterpillar.phases_from_base())
            sim_actions_file.append_data(step, *action)
            frictions = caterpillar.frictions()
            sim_frictions_file.append_data(step, *frictions)

    print("Moved distance:", caterpillar.moved_distance())
    caterpillar.save_simulation("{}/train_result_ep{}.sim".format(log_dir, episode))