Beispiel #1
0
def test_plot_graphs_multi_phases():
    # Load graphs_one_phase
    PROJECT_FOLDER = Path(__file__).parent / ".."
    spec = importlib.util.spec_from_file_location(
        "align_markers",
        str(PROJECT_FOLDER) +
        "/examples/torque_driven_ocp/multiphase_align_markers.py")
    graphs_multi_phases = importlib.util.module_from_spec(spec)
    spec.loader.exec_module(graphs_multi_phases)

    ocp = graphs_multi_phases.prepare_ocp(
        biorbd_model_path=str(PROJECT_FOLDER) +
        "/examples/torque_driven_ocp/cube.bioMod")
    sol = ocp.solve()

    plt = ShowResult(ocp, sol)
    plt.graphs(automatically_organize=False)
Beispiel #2
0
def test_plot_graphs_one_phase():
    # Load graphs_one_phase
    PROJECT_FOLDER = Path(__file__).parent / ".."
    spec = importlib.util.spec_from_file_location(
        "align_markers",
        str(PROJECT_FOLDER) +
        "/examples/torque_driven_ocp/align_markers_with_torque_actuators.py")
    graphs_one_phase = importlib.util.module_from_spec(spec)
    spec.loader.exec_module(graphs_one_phase)

    ocp = graphs_one_phase.prepare_ocp(
        biorbd_model_path=str(PROJECT_FOLDER) +
        "/examples/torque_driven_ocp/cube.bioMod",
        number_shooting_points=30,
        final_time=2,
    )
    sol = ocp.solve()

    plt = ShowResult(ocp, sol)
    plt.graphs(automatically_organize=False)
Beispiel #3
0
def test_plot_merged_graphs():
    # Load graphs_one_phase
    PROJECT_FOLDER = Path(__file__).parent / ".."
    spec = importlib.util.spec_from_file_location(
        "align_markers",
        str(PROJECT_FOLDER) +
        "/examples/muscle_driven_ocp/muscle_excitations_tracker.py")
    merged_graphs = importlib.util.module_from_spec(spec)
    spec.loader.exec_module(merged_graphs)

    # Define the problem
    model_path = str(
        PROJECT_FOLDER) + "/examples/muscle_driven_ocp/arm26.bioMod"
    biorbd_model = biorbd.Model(model_path)
    final_time = 0.5
    nb_shooting = 9

    # Generate random data to fit
    np.random.seed(42)
    t, markers_ref, x_ref, muscle_excitations_ref = merged_graphs.generate_data(
        biorbd_model, final_time, nb_shooting)

    biorbd_model = biorbd.Model(
        model_path
    )  # To prevent from non free variable, the model must be reloaded
    ocp = merged_graphs.prepare_ocp(
        biorbd_model,
        final_time,
        nb_shooting,
        markers_ref,
        muscle_excitations_ref,
        x_ref[:biorbd_model.nbQ(), :].T,
        use_residual_torque=True,
        kin_data_to_track="markers",
    )
    sol = ocp.solve()

    plt = ShowResult(ocp, sol)
    plt.graphs(automatically_organize=False)
pendulum = importlib.util.module_from_spec(spec)
spec.loader.exec_module(pendulum)

ocp = pendulum.prepare_ocp(
    biorbd_model_path="pendulum.bioMod",
    final_time=2,
    number_shooting_points=10,
    nb_threads=2,
)

X = InitialGuess([0, 0, 0, 0])
U = InitialGuess([-1, 1])

# --- Single shooting --- #
sol_simulate_single_shooting = Simulate.from_controls_and_initial_states(
    ocp, X, U, single_shoot=True)
result_single = ShowResult(ocp, sol_simulate_single_shooting)
result_single.graphs()

# --- Multiple shooting --- #
sol_simulate_multiple_shooting = Simulate.from_controls_and_initial_states(
    ocp, X, U, single_shoot=False)
result_multiple = ShowResult(ocp, sol_simulate_multiple_shooting)
result_multiple.graphs()

# --- Single shooting --- #
result_single.animate()

# --- Multiple shooting --- #
result_multiple.animate()
Beispiel #5
0
    )


def plot_callback(x, q_to_plot):
    return x[q_to_plot, :]


if __name__ == "__main__":
    # Prepare the Optimal Control Program
    ocp = prepare_ocp(biorbd_model_path="pendulum.bioMod", final_time=2, number_shooting_points=50)

    # Add my lovely new plot
    ocp.add_plot("My New Extra Plot", lambda x, u, p: plot_callback(x, [0, 1, 3]), PlotType.PLOT)
    ocp.add_plot(
        "My New Extra Plot", lambda x, u, p: plot_callback(x, [1, 3]), plot_type=PlotType.STEP, axes_idx=[1, 2]
    )
    ocp.add_plot(
        "My Second New Extra Plot",
        lambda x, u, p: plot_callback(x, [1, 3]),
        plot_type=PlotType.INTEGRATED,
        axes_idx=[1, 2],
    )

    # --- Solve the program --- #
    sol = ocp.solve(show_online_optim=False)

    # --- Show results --- #
    result = ShowResult(ocp, sol)
    result.graphs()
    result.animate()
    InterpolationType.CONSTANT,
)

muscle_states_init = InitialConditions(
    [muscle_activated_init] * ocp.nlp[0]["model"].nbMuscles() +
    [muscle_fatigued_init] * ocp.nlp[0]["model"].nbMuscles() +
    [muscle_resting_init] * ocp.nlp[0]["model"].nbMuscles(),
    InterpolationType.CONSTANT,
)
X.concatenate(muscle_states_init)

# --- Simulate --- #
sol_simulate = Simulate.from_controls_and_initial_states(ocp,
                                                         X,
                                                         U,
                                                         single_shoot=True)

# --- Save for biorbdviz --- #
OptimalControlProgram.save_get_data(ocp,
                                    sol_simulate,
                                    f"results/simulate.bob",
                                    interpolate_nb_frames=100)

# --- Graph --- #
result_single = ShowResult(ocp, sol_simulate)
result_single.graphs()
# todo multiphase
print("ok")

# result_single.animate()
Beispiel #7
0
    )


if __name__ == "__main__":
    print(f"Show the bounds")
    for interpolation_type in InterpolationType:
        print(f"Solving problem using {interpolation_type} bounds")
        ocp = prepare_ocp("cube.bioMod",
                          number_shooting_points=30,
                          final_time=2,
                          interpolation_type=interpolation_type)
        sol = ocp.solve()
        print("\n")

        # Print the last solution
        result_plot = ShowResult(ocp, sol)
        result_plot.graphs(adapt_graph_size_to_bounds=True)

    for interpolation_type in InterpolationType:
        print(f"Solving problem using {interpolation_type} bounds")
        ocp = prepare_ocp("cube.bioMod",
                          number_shooting_points=30,
                          final_time=2,
                          interpolation_type=interpolation_type)
        sol = ocp.solve()
        print("\n")

        # Print the last solution
        result_plot = ShowResult(ocp, sol)
        result_plot.graphs(adapt_graph_size_to_bounds=False)
Beispiel #8
0
    # ------------- #

    return OptimalControlProgram(
        biorbd_model,
        dynamics,
        number_shooting_points,
        final_time,
        x_init,
        u_init,
        x_bounds,
        u_bounds,
        objective_functions,
    )


if __name__ == "__main__":

    # changer le path quand ce sera pret
    ocp = prepare_ocp(
        "TruncAnd2Arm_Quaternion.bioMod",
        number_shooting_points=5,
        final_time=0.25,
    )
    sol = ocp.solve()
    print("\n")

    # Print the last solution
    result_plot = ShowResult(ocp, sol)
    result_plot.graphs()