Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix on custom_model.py + test #822

Merged
merged 3 commits into from
Jan 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions bioptim/examples/custom_model/custom_package/my_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@
This script implements a custom model to work with bioptim. Bioptim has a deep connection with biorbd,
but it is possible to use bioptim without biorbd. This is an example of how to use bioptim with a custom model.
"""
from typing import Callable

import numpy as np
from casadi import sin, MX
from typing import Callable


class MyModel:
Expand All @@ -26,6 +25,11 @@ def serialize(self) -> tuple[Callable, dict]:
return MyModel, dict(com=self.com, inertia=self.inertia)

# ---- Needed for the example ---- #

@property
def name(self) -> str:
return "MyModel"

@property
def nb_tau(self):
return 1
Expand Down
17 changes: 11 additions & 6 deletions bioptim/examples/custom_model/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@
"""
import numpy as np

# import the custom model
from bioptim.examples.custom_model.custom_package import MyModel

from bioptim import (
OptimalControlProgram,
BoundsList,
Expand All @@ -21,6 +18,9 @@
PhaseDynamics,
)

# import the custom model
from bioptim.examples.custom_model.custom_package import MyModel


def prepare_ocp(
model: MyModel,
Expand Down Expand Up @@ -70,7 +70,10 @@ def prepare_ocp(
# Dynamics
dynamics = DynamicsList()
dynamics.add(
configure_dynamics, dynamic_function=dynamics, expand_dynamics=expand_dynamics, phase_dynamics=phase_dynamics
configure_dynamics,
dynamic_function=dynamics,
expand_dynamics=expand_dynamics,
phase_dynamics=phase_dynamics,
)

# Path constraint
Expand Down Expand Up @@ -119,7 +122,9 @@ def main():
"""

# import the custom dynamics and configuration
from bioptim.examples.custom_model.custom_package.custom_dynamics import custom_configure_my_dynamics
from bioptim.examples.custom_model.custom_package.custom_dynamics import (
custom_configure_my_dynamics,
)

# --- Prepare the ocp --- #
ocp = prepare_ocp(
Expand All @@ -141,7 +146,7 @@ def main():
sol = ocp.solve(solver=solver)

# --- Show results --- #
sol.graphs(show_bounds=True)
# sol.graphs(show_bounds=True)
sol.print_cost()

# --- Animation --- #
Expand Down
98 changes: 71 additions & 27 deletions tests/shard1/test_all_examples.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import numpy as np
import os
import pytest
import numpy as np

from bioptim import InterpolationType, PhaseDynamics


Expand All @@ -24,7 +25,10 @@ def test__acados__pendulum():
bioptim_folder = os.path.dirname(ocp_module.__file__)

ocp_module.prepare_ocp(
biorbd_model_path=bioptim_folder + "/models/pendulum.bioMod", n_shooting=41, final_time=3, expand_dynamics=True
biorbd_model_path=bioptim_folder + "/models/pendulum.bioMod",
n_shooting=41,
final_time=3,
expand_dynamics=True,
)


Expand Down Expand Up @@ -194,7 +198,9 @@ def test__getting_started__example_external_forces():


def test__getting_started__example_inequality_constraint():
from bioptim.examples.getting_started import example_inequality_constraint as ocp_module
from bioptim.examples.getting_started import (
example_inequality_constraint as ocp_module,
)

bioptim_folder = os.path.dirname(ocp_module.__file__)

Expand All @@ -214,11 +220,13 @@ def test__getting_started__example_inequality_constraint():


def test__getting_started__example_mapping():
from bioptim.examples.getting_started import example_mapping as ocp_module
pass


def test__getting_started__example_multinode_constraints():
from bioptim.examples.getting_started import example_multinode_constraints as ocp_module
from bioptim.examples.getting_started import (
example_multinode_constraints as ocp_module,
)

bioptim_folder = os.path.dirname(ocp_module.__file__)

Expand All @@ -231,7 +239,9 @@ def test__getting_started__example_multinode_constraints():


def test__getting_started__example_multinode_objective():
from bioptim.examples.getting_started import example_multinode_objective as ocp_module
from bioptim.examples.getting_started import (
example_multinode_objective as ocp_module,
)

bioptim_folder = os.path.dirname(ocp_module.__file__)

Expand Down Expand Up @@ -274,11 +284,11 @@ def test__getting_started__example_multiphase():


def test__getting_started__example_optimal_time():
from bioptim.examples.getting_started import example_optimal_time as ocp_module
pass


def test__getting_started__example_simulation():
from bioptim.examples.getting_started import example_optimal_time as ocp_module
pass


# todo: Add example_variable_scaling.py?
Expand All @@ -299,7 +309,9 @@ def test__getting_started__pendulum():


def test__getting_started__pendulum_constrained_states_controls():
from bioptim.examples.getting_started import pendulum_constrained_states_controls as ocp_module
from bioptim.examples.getting_started import (
pendulum_constrained_states_controls as ocp_module,
)

bioptim_folder = os.path.dirname(ocp_module.__file__)

Expand All @@ -314,15 +326,15 @@ def test__getting_started__pendulum_constrained_states_controls():

## examples/moving_horizon_estimation
def test__moving_horizon_estimation__mhe():
from bioptim.examples.moving_horizon_estimation import mhe as ocp_module
pass


def test__muscle_driven_ocp__muscle_activations_tracker():
from bioptim.examples.muscle_driven_ocp import muscle_activations_tracker as ocp_module
pass


def test__muscle_driven_ocp__muscle_excitations_tracker():
from bioptim.examples.muscle_driven_ocp import muscle_excitations_tracker as ocp_module
pass


def test__muscle_driven_ocp__static_arm():
Expand All @@ -341,11 +353,13 @@ def test__muscle_driven_ocp__static_arm():


def test__muscle_driven_with_contact__muscle_activations_contacts_tracker():
from bioptim.examples.muscle_driven_with_contact import muscle_activations_contacts_tracker as ocp_module
pass


def test__optimal_time_ocp__multiphase_time_constraint():
from bioptim.examples.optimal_time_ocp import multiphase_time_constraint as ocp_module
from bioptim.examples.optimal_time_ocp import (
multiphase_time_constraint as ocp_module,
)

bioptim_folder = os.path.dirname(ocp_module.__file__)

Expand All @@ -365,7 +379,9 @@ def test__optimal_time_ocp__multiphase_time_constraint():


def test__optimal_time_ocp__pendulum_min_time_Lagrange():
from bioptim.examples.optimal_time_ocp import pendulum_min_time_Lagrange as ocp_module
from bioptim.examples.optimal_time_ocp import (
pendulum_min_time_Lagrange as ocp_module,
)

bioptim_folder = os.path.dirname(ocp_module.__file__)

Expand Down Expand Up @@ -408,7 +424,9 @@ def test__optimal_time_ocp__time_constraint():


def test__symmetrical_torque_driven_ocp__symmetry_by_constraint():
from bioptim.examples.symmetrical_torque_driven_ocp import symmetry_by_constraint as ocp_module
from bioptim.examples.symmetrical_torque_driven_ocp import (
symmetry_by_constraint as ocp_module,
)

bioptim_folder = os.path.dirname(ocp_module.__file__)

Expand All @@ -420,7 +438,9 @@ def test__symmetrical_torque_driven_ocp__symmetry_by_constraint():


def test__symmetrical_torque_driven_ocp__symmetry_by_mapping():
from bioptim.examples.symmetrical_torque_driven_ocp import symmetry_by_mapping as ocp_module
from bioptim.examples.symmetrical_torque_driven_ocp import (
symmetry_by_mapping as ocp_module,
)

bioptim_folder = os.path.dirname(ocp_module.__file__)

Expand All @@ -432,7 +452,9 @@ def test__symmetrical_torque_driven_ocp__symmetry_by_mapping():


def test__torque_driven_ocp__maximize_predicted_height_CoM():
from bioptim.examples.torque_driven_ocp import maximize_predicted_height_CoM as ocp_module
from bioptim.examples.torque_driven_ocp import (
maximize_predicted_height_CoM as ocp_module,
)

bioptim_folder = os.path.dirname(ocp_module.__file__)

Expand All @@ -449,7 +471,9 @@ def test__torque_driven_ocp__maximize_predicted_height_CoM():


def test__torque_driven_ocp__multi_biorbd_model():
from bioptim.examples.torque_driven_ocp import example_multi_biorbd_model as ocp_module
from bioptim.examples.torque_driven_ocp import (
example_multi_biorbd_model as ocp_module,
)

bioptim_folder = os.path.dirname(ocp_module.__file__)

Expand All @@ -463,7 +487,9 @@ def test__torque_driven_ocp__multi_biorbd_model():


def test__torque_driven_ocp__phase_transition_uneven_variable_number_by_mapping():
from bioptim.examples.torque_driven_ocp import phase_transition_uneven_variable_number_by_mapping as ocp_module
from bioptim.examples.torque_driven_ocp import (
phase_transition_uneven_variable_number_by_mapping as ocp_module,
)

bioptim_folder = os.path.dirname(ocp_module.__file__)

Expand All @@ -477,7 +503,9 @@ def test__torque_driven_ocp__phase_transition_uneven_variable_number_by_mapping(


def test__torque_driven_ocp__phase_transition_uneven_variable_number_by_bounds():
from bioptim.examples.torque_driven_ocp import phase_transition_uneven_variable_number_by_bounds as ocp_module
from bioptim.examples.torque_driven_ocp import (
phase_transition_uneven_variable_number_by_bounds as ocp_module,
)

bioptim_folder = os.path.dirname(ocp_module.__file__)

Expand Down Expand Up @@ -1317,11 +1345,13 @@ def test__track__optimal_estimation():


def test__torque_driven_ocp__track_markers_2D_pendulum():
from bioptim.examples.torque_driven_ocp import track_markers_2D_pendulum as ocp_module
pass


def test__torque_driven_ocp__track_markers_with_torque_actuators():
from bioptim.examples.torque_driven_ocp import track_markers_with_torque_actuators as ocp_module
from bioptim.examples.torque_driven_ocp import (
track_markers_with_torque_actuators as ocp_module,
)

bioptim_folder = os.path.dirname(ocp_module.__file__)

Expand Down Expand Up @@ -1349,7 +1379,9 @@ def test__torque_driven_ocp__trampo_quaternions():


def test__torque_driven_ocp__minimize_segment_velocity():
from bioptim.examples.torque_driven_ocp import example_minimize_segment_velocity as ocp_module
from bioptim.examples.torque_driven_ocp import (
example_minimize_segment_velocity as ocp_module,
)

bioptim_folder = os.path.dirname(ocp_module.__file__)

Expand Down Expand Up @@ -1418,7 +1450,9 @@ def test__getting_started__example_variable_scaling():


def test__torque_driven_ocp__torque_activation_driven():
from bioptim.examples.torque_driven_ocp import torque_activation_driven as ocp_module
from bioptim.examples.torque_driven_ocp import (
torque_activation_driven as ocp_module,
)

bioptim_folder = os.path.dirname(ocp_module.__file__)

Expand All @@ -1432,7 +1466,9 @@ def test__torque_driven_ocp__torque_activation_driven():


def test__inverse_optimal_control__double_pendulum_torque_driven_IOCP():
from bioptim.examples.inverse_optimal_control import double_pendulum_torque_driven_IOCP as ocp_module
from bioptim.examples.inverse_optimal_control import (
double_pendulum_torque_driven_IOCP as ocp_module,
)

bioptim_folder = os.path.dirname(ocp_module.__file__)

Expand All @@ -1445,7 +1481,9 @@ def test__inverse_optimal_control__double_pendulum_torque_driven_IOCP():


def test__contact_and_muscle_forces_example():
from bioptim.examples.muscle_driven_with_contact import contact_forces_inequality_constraint_muscle as ocp_module
from bioptim.examples.muscle_driven_with_contact import (
contact_forces_inequality_constraint_muscle as ocp_module,
)

bioptim_folder = os.path.dirname(ocp_module.__file__)

Expand Down Expand Up @@ -1485,3 +1523,9 @@ def test_min_max_example():
ocp_module.prepare_ocp(
bio_model_path=bioptim_folder + "/models/double_pendulum.bioMod",
)


def test_custom_model():
from bioptim.examples.custom_model.main import main as ocp_module

ocp_module()
Loading