Skip to content
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ As a tour guide that uses this binder, you can watch the `bioptim` workshop that

A GUI is available to run all the current examples. To run it you can use the following command, from the root folder of the project:
```bash
conda install -c conda-forge pyqt pyqtgraph
python -m bioptim.examples
```
Please refer to section [Examples](#examples) for more information on how to run the examples.
Expand Down
42 changes: 26 additions & 16 deletions bioptim/examples/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,12 @@
# Avoid clash with module name
examples_ = OrderedDict(
[
("acados", OrderedDict([("Static arm", "static_arm.py"), ("Cube", "cube.py"), ("Pendulum", "pendulum.py")])),
(
"fatigue",
"toy_examples/acados",
OrderedDict([("Static arm", "static_arm.py"), ("Cube", "cube.py"), ("Pendulum", "pendulum.py")]),
),
(
"toy_examples/fatigue",
OrderedDict(
[
("Pendulum with fatigue", "pendulum_with_fatigue.py"),
Expand All @@ -37,32 +40,39 @@
"getting_started",
OrderedDict(
[
("Custom Bounds", "custom_bounds.py"),
("Custom constraint", "custom_constraint.py"),
("Custom initial guess", "custom_initial_guess.py"),
("Custom objectives", "custom_objectives.py"),
("Custom parameters", "custom_parameters.py"),
("Custom phase transitions", "custom_phase_transitions.py"),
("Custom plotting", "custom_plotting.py"),
("Example cyclic movement", "example_cyclic_movement.py"),
("Example external forces", "example_external_forces.py"),
("Example inequality constraint", "example_inequality_constraint.py"),
("Example mapping", "example_mapping.py"),
("Example multiphase", "example_multiphase.py"),
("Example optimal time", "example_optimal_time.py"),
("Example simulation", "example_simulation.py"),
("Pendulum", "pendulum.py"),
("Example cyclic movement", "example_cyclic_movement.py"),
("Basic OCP", "basic_ocp.py"),
("Example constraint weight", "custom_constraint_weights.py"),
("How to plot", "how_to_plot.py"),
]
),
),
(
"toy_examples/feature_examples",
OrderedDict(
[
("Custom Bounds", "custom_bounds.py"),
("Custom constraint", "custom_constraint.py"),
("Custom initial guess", "custom_initial_guess.py"),
]
),
),
(
"moving_horizon_estimation",
"toy_examples/moving_horizon_estimation",
OrderedDict(
[("Cyclic nmpc", "cyclic_nmpc.py"), ("Mhe", "mhe.py"), ("Multi cyclic nmpc", "multi_cyclic_nmpc.py")]
),
),
(
"muscle_driven_ocp",
"toy_examples/muscle_driven_ocp",
OrderedDict(
[
("Muscle activations tracker", "muscle_activations_tracker.py"),
Expand All @@ -73,7 +83,7 @@
),
),
(
"muscle_driven_with_contact",
"toy_examples/muscle_driven_with_contact",
OrderedDict(
[
("Contact forces inequality constraint muscle", "contact_forces_inequality_constraint_muscle.py"),
Expand All @@ -86,7 +96,7 @@
),
),
(
"optimal_time_ocp",
"toy_examples/optimal_time_ocp",
OrderedDict(
[
("Multiphase time constraint", "multiphase_time_constraint.py"),
Expand All @@ -96,7 +106,7 @@
),
),
(
"symmetrical_torque_driven_ocp",
"toy_examples/symmetrical_torque_driven_ocp",
OrderedDict(
[
("Symmetry by constraint", "symmetry_by_constraint.py"),
Expand All @@ -105,7 +115,7 @@
),
),
(
"torque_driven_ocp",
"toy_examples/torque_driven_ocp",
OrderedDict(
[
("Maximize predicted height center_of_mass", "maximize_predicted_height_CoM.py"),
Expand All @@ -126,7 +136,7 @@
),
),
(
"track",
"toy_examples/tracking",
OrderedDict(
[
("Track marker on segment", "track_marker_on_segment.py"),
Expand Down
6 changes: 4 additions & 2 deletions bioptim/examples/getting_started/example_multistart.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
PhaseDynamics,
SolutionMerge,
)
from bioptim.examples.utils import ExampleUtils


def prepare_ocp(
Expand Down Expand Up @@ -196,7 +197,8 @@ def prepare_multi_start(
def main():
# --- Prepare the multi-start and run it --- #

bio_model_path = ["models/pendulum.bioMod"]
example_path = ExampleUtils.folder
bio_model_path = [example_path + "/models/pendulum.bioMod"]
final_time = [1]
n_shooting = [30, 40, 50]
seed = [0, 1, 2, 3]
Expand All @@ -208,7 +210,7 @@ def main():
"seed": seed,
}

save_folder = "./temporary_results"
save_folder = example_path + "/./temporary_results"
multi_start = prepare_multi_start(
combinatorial_parameters=combinatorial_parameters,
save_folder=save_folder,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
VariationalTorqueBiorbdModel,
VariationalOptimalControlProgram,
)
from bioptim.examples.utils import ExampleUtils
import numpy as np


Expand Down Expand Up @@ -92,7 +93,8 @@ def main():
n_shooting = 100

# --- Prepare the ocp --- #
ocp = prepare_ocp(bio_model_path="models/pendulum.bioMod", final_time=1, n_shooting=n_shooting)
biorbd_model_path = ExampleUtils.folder + "/models/pendulum.bioMod"
ocp = prepare_ocp(bio_model_path=biorbd_model_path, final_time=1, n_shooting=n_shooting)

# --- Print ocp structure --- #
ocp.print(to_console=False, to_graph=False)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
VariationalTorqueBiorbdModel,
VariationalOptimalControlProgram,
)
from bioptim.examples.utils import ExampleUtils
import numpy as np


Expand Down Expand Up @@ -111,7 +112,8 @@ def main():
n_shooting = 100

# --- Prepare the ocp --- #
ocp = prepare_ocp(bio_model_path="models/pendulum_holonomic.bioMod", final_time=1, n_shooting=n_shooting)
biorbd_model_path = ExampleUtils.folder + "/models/pendulum_holonomic.bioMod"
ocp = prepare_ocp(bio_model_path=biorbd_model_path, final_time=1, n_shooting=n_shooting)

# --- Print ocp structure --- #
ocp.print(to_console=False, to_graph=False)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
PhaseDynamics,
ConstraintWeight,
)
from bioptim.examples.utils import ExampleUtils


def custom_weight(node: int, n_nodes: int) -> float:
Expand Down Expand Up @@ -172,6 +173,7 @@ def main():
"""

nodes_to_test = [Node.START, Node.INTERMEDIATES, Node.ALL_SHOOTING]
biorbd_model_path = ExampleUtils.folder + "/models/cube.bioMod"

for interpolation_type in InterpolationType:
for node in nodes_to_test:
Expand All @@ -183,7 +185,11 @@ def main():

print(f"Solving problem using {interpolation_type} weight applied at {node} nodes.")
ocp = prepare_ocp(
"models/cube.bioMod", n_shooting=30, final_time=2, interpolation_type=interpolation_type, node=node
biorbd_model_path=biorbd_model_path,
n_shooting=30,
final_time=2,
interpolation_type=interpolation_type,
node=node,
)
sol = ocp.solve()
print("\n")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
MultinodeObjectiveList,
CostType,
)
from bioptim.examples.utils import ExampleUtils


def multinode_min_controls(controllers: list[PenaltyController]):
Expand Down Expand Up @@ -125,7 +126,8 @@ def main():

# --- Prepare the ocp --- #
n_shooting = 30
ocp = prepare_ocp(biorbd_model_path="models/pendulum.bioMod", final_time=1, n_shooting=n_shooting)
biorbd_model_path = ExampleUtils.folder + "/models/pendulum.bioMod"
ocp = prepare_ocp(biorbd_model_path=biorbd_model_path, final_time=1, n_shooting=n_shooting)
ocp.add_plot_penalty(CostType.ALL)

# --- Solve the ocp --- #
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
SolutionMerge,
OdeSolver,
)
from bioptim.examples.utils import ExampleUtils


def compute_all_states(sol, bio_model: HolonomicTorqueBiorbdModel):
Expand Down Expand Up @@ -197,8 +198,8 @@ def main():
Runs the optimization and animates it
"""

model_path = "models/two_pendulums.bioMod"
ocp, bio_model = prepare_ocp(biorbd_model_path=model_path)
biorbd_model_path = ExampleUtils.folder + "/models/two_pendulums.bioMod"
ocp, bio_model = prepare_ocp(biorbd_model_path=biorbd_model_path)

# --- Solve the program --- #
sol = ocp.solve(Solver.IPOPT())
Expand All @@ -211,15 +212,15 @@ def main():
if viewer == "bioviz":
import bioviz

viz = bioviz.Viz(model_path)
viz = bioviz.Viz(biorbd_model_path)
viz.load_movement(q)
viz.exec()

if viewer == "pyorerun":
import pyorerun

viz = pyorerun.PhaseRerun(t_span=np.concatenate(sol.decision_time()).squeeze())
viz.add_animated_model(pyorerun.BiorbdModel(model_path), q=q)
viz.add_animated_model(pyorerun.BiorbdModel(biorbd_model_path), q=q)

viz.rerun("double_pendulum")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
CostType,
OdeSolver,
)
from bioptim.examples.utils import ExampleUtils
from .custom_dynamics import (
ModifiedHolonomicTorqueBiorbdModel,
constraint_holonomic,
Expand Down Expand Up @@ -232,8 +233,8 @@ def main():
Runs the optimization and animates it
"""

model_path = "models/two_pendulums.bioMod"
ocp, bio_model = prepare_ocp(biorbd_model_path=model_path)
biorbd_model_path = ExampleUtils.folder + "/models/two_pendulums.bioMod"
ocp, bio_model = prepare_ocp(biorbd_model_path=biorbd_model_path)
ocp.add_plot_penalty(CostType.ALL)

# --- Solve the program --- #
Expand All @@ -248,14 +249,14 @@ def main():
if viewer == "bioviz":
import bioviz

viz = bioviz.Viz(model_path)
viz = bioviz.Viz(biorbd_model_path)
viz.load_movement(q)
viz.exec()

if viewer == "pyorerun":
from pyorerun import BiorbdModel as PyorerunBiorbdModel, PhaseRerun

pyomodel = PyorerunBiorbdModel(model_path)
pyomodel = PyorerunBiorbdModel(biorbd_model_path)
viz = PhaseRerun(t_span=np.concatenate(sol.decision_time()).squeeze())
viz.add_animated_model(pyomodel, q=q)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,13 @@
PhaseDynamics,
SolutionMerge,
)
from bioptim.examples.utils import ExampleUtils


def prepare_ocp(
weights,
coefficients,
biorbd_model_path="models/double_pendulum.bioMod",
biorbd_model_path,
phase_dynamics: PhaseDynamics = PhaseDynamics.SHARED_DURING_THE_PHASE,
n_threads: int = 4,
expand_dynamics: bool = True,
Expand Down Expand Up @@ -119,7 +120,8 @@ def fitness(self, weights):
"""
global i_inverse
i_inverse += 1
ocp = prepare_ocp(weights, self.coefficients)
biorbd_model_path = ExampleUtils.folder + "/models/double_pendulums.bioMod"
ocp = prepare_ocp(weights, self.coefficients, biorbd_model_path=biorbd_model_path)
sol = ocp.solve(self.solver)
print(
f"+++++++++++++++++++++++++++ Optimized the {i_inverse}th ocp in the inverse algo +++++++++++++++++++++++++++"
Expand Down Expand Up @@ -148,7 +150,8 @@ def main():

# Generate data using OCP
weights_to_track = [0.4, 0.3, 0.3]
ocp_to_track = prepare_ocp(weights=weights_to_track, coefficients=[1, 1, 1])
biorbd_model_path = ExampleUtils.folder + "/models/double_pendulums.bioMod"
ocp_to_track = prepare_ocp(weights=weights_to_track, coefficients=[1, 1, 1], biorbd_model_path=biorbd_model_path)
ocp_to_track.add_plot_penalty(CostType.ALL)
solver = Solver.IPOPT()
# solver.set_linear_solver("ma57") # Much faster, but necessite libhsl installed
Expand All @@ -163,7 +166,7 @@ def main():
for i in range(len(weights_to_track)):
weights_pareto = [0, 0, 0]
weights_pareto[i] = 1
ocp_pareto = prepare_ocp(weights=weights_pareto, coefficients=[1, 1, 1])
ocp_pareto = prepare_ocp(weights=weights_pareto, coefficients=[1, 1, 1], biorbd_model_path=biorbd_model_path)
sol_pareto = ocp_pareto.solve(solver)
# sol_pareto.animate()
coefficients.append(sol_pareto.cost)
Expand Down Expand Up @@ -215,7 +218,7 @@ def main():
# Compare the kinematics
import biorbd

ocp_final = prepare_ocp(weights=pop_weights, coefficients=coefficients)
ocp_final = prepare_ocp(weights=pop_weights, coefficients=coefficients, biorbd_model_path=biorbd_model_path)
sol_final = ocp_final.solve(solver)
states = sol_final.decision_states(to_merge=SolutionMerge.NODES)
controls = sol_final.decision_controls(to_merge=SolutionMerge.NODES)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
Solution,
PhaseDynamics,
)
from bioptim.examples.utils import ExampleUtils


class MyCyclicNMPC(CyclicNonlinearModelPredictiveControl):
Expand Down Expand Up @@ -76,14 +77,14 @@ def prepare_nmpc(


def main():
model_path = "models/arm2.bioMod"
biorbd_model_path = ExampleUtils.folder + "/models/arm2.bioMod"
torque_max = 50

cycle_duration = 1
cycle_len = 20
n_cycles = 3

nmpc = prepare_nmpc(model_path, cycle_len=cycle_len, cycle_duration=cycle_duration, max_torque=torque_max)
nmpc = prepare_nmpc(biorbd_model_path, cycle_len=cycle_len, cycle_duration=cycle_duration, max_torque=torque_max)

def update_functions(_nmpc: CyclicNonlinearModelPredictiveControl, cycle_idx: int, _sol: Solution):
return cycle_idx < n_cycles # True if there are still some cycle to perform
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
PhaseDynamics,
SolutionMerge,
)
from bioptim.examples.utils import ExampleUtils


def states_to_markers(bio_model, states):
Expand Down Expand Up @@ -182,7 +183,7 @@ def get_solver_options(solver):


def main():
biorbd_model_path = "models/cart_pendulum.bioMod"
biorbd_model_path = ExampleUtils.folder + "/models/cart_pendulum.bioMod"
bio_model = TorqueBiorbdModel(biorbd_model_path)

solver = Solver.IPOPT() # or Solver.ACADOS() # If ACADOS is used, it must be manually installed
Expand Down
Loading
Loading