-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathcustom_plotting.py
133 lines (110 loc) · 3.78 KB
/
custom_plotting.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
"""
This example is a trivial example using the pendulum without any objective. It is designed to show how to create new
plots and how to expand pre-existing one with new information
"""
from casadi import MX
from bioptim import (
BiorbdModel,
OptimalControlProgram,
Dynamics,
DynamicsFcn,
BoundsList,
PlotType,
Solver,
PhaseDynamics,
)
def custom_plot_callback(x: MX, q_to_plot: list) -> MX:
"""
Create a used defined plot function with extra_parameters
Parameters
----------
x: MX
The current states of the optimization
q_to_plot: list
The slice indices to plot
Returns
-------
The value to plot
"""
return x[q_to_plot, :]
def prepare_ocp(
biorbd_model_path: str,
final_time: float,
n_shooting: int,
phase_dynamics: PhaseDynamics = PhaseDynamics.SHARED_DURING_THE_PHASE,
expand_dynamics: bool = True,
) -> OptimalControlProgram:
"""
Prepare the program
Parameters
----------
biorbd_model_path: str
The path of the biorbd model
final_time: float
The time at the final node
n_shooting: int
The number of shooting points
phase_dynamics: PhaseDynamics
If the dynamics equation within a phase is unique or changes at each node.
PhaseDynamics.SHARED_DURING_THE_PHASE is much faster, but lacks the capability to have changing dynamics within
a phase. A good example of when PhaseDynamics.ONE_PER_NODE should be used is when different external forces
are applied at each node
expand_dynamics: bool
If the dynamics function should be expanded. Please note, this will solve the problem faster, but will slow down
the declaration of the OCP, so it is a trade-off. Also depending on the solver, it may or may not work
(for instance IRK is not compatible with expanded dynamics)
"""
bio_model = BiorbdModel(biorbd_model_path)
# Dynamics
dynamics = Dynamics(DynamicsFcn.TORQUE_DRIVEN, expand_dynamics=expand_dynamics, phase_dynamics=phase_dynamics)
# Path constraint
x_bounds = BoundsList()
x_bounds["q"] = bio_model.bounds_from_ranges("q")
x_bounds["q"][:, [0, -1]] = 0
x_bounds["q"][1, -1] = 3.14
x_bounds["qdot"] = bio_model.bounds_from_ranges("qdot")
x_bounds["qdot"][:, [0, -1]] = 0
# Define control path constraint
torque_min, torque_max = -100, 100
n_tau = bio_model.nb_tau
u_bounds = BoundsList()
u_bounds["tau"] = [torque_min] * n_tau, [torque_max] * n_tau
ocp = OptimalControlProgram(
bio_model,
dynamics,
n_shooting,
final_time,
x_bounds=x_bounds,
u_bounds=u_bounds,
)
# Add my lovely new plot
ocp.add_plot(
"My New Extra Plot",
lambda t0, phases_dt, node_idx, x, u, p, a, d: custom_plot_callback(x, [0, 1, 3]),
plot_type=PlotType.PLOT,
)
ocp.add_plot( # This one combines to the previous one as they have the same name
"My New Extra Plot",
lambda t0, phases_dt, node_idx, x, u, p, a, d: custom_plot_callback(x, [1, 3]),
plot_type=PlotType.STEP,
axes_idx=[1, 2],
)
ocp.add_plot(
"My Second New Extra Plot",
lambda t0, phases_dt, node_idx, x, u, p, a, d: custom_plot_callback(x, [2, 1]),
plot_type=PlotType.INTEGRATED,
axes_idx=[0, 2],
)
return ocp
def main():
"""
Create multiple new plot and add new stuff to them using a custom defined function
"""
# Prepare the Optimal Control Program
ocp = prepare_ocp(biorbd_model_path="models/pendulum.bioMod", final_time=2, n_shooting=50)
# --- Solve the program --- #
sol = ocp.solve(Solver.IPOPT(show_online_optim=False))
# --- Show results --- #
sol.graphs()
if __name__ == "__main__":
main()