-
Notifications
You must be signed in to change notification settings - Fork 46
/
custom_dynamics.py
225 lines (190 loc) · 7.09 KB
/
custom_dynamics.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
"""
This example is a trivial box that must superimpose one of its corner to a marker at the beginning of the movement
and superimpose the same corner to a different marker at the end.
It is designed to show how one can define its own custom dynamics function if the provided ones are not
sufficient.
More specifically this example reproduces the behavior of the DynamicsFcn.TORQUE_DRIVEN using a custom dynamics
"""
import platform
from casadi import MX, SX, vertcat
from bioptim import (
BiorbdModel,
Node,
OptimalControlProgram,
DynamicsList,
ConfigureProblem,
DynamicsFcn,
DynamicsFunctions,
Objective,
ObjectiveFcn,
ConstraintList,
ConstraintFcn,
BoundsList,
OdeSolver,
OdeSolverBase,
NonLinearProgram,
Solver,
DynamicsEvaluation,
PhaseDynamics,
)
def custom_dynamics(
time: MX | SX,
states: MX | SX,
controls: MX | SX,
parameters: MX | SX,
algebraic_states: MX | SX,
numerical_timeseries: MX | SX,
nlp: NonLinearProgram,
my_additional_factor=1,
) -> DynamicsEvaluation:
"""
The custom dynamics function that provides the derivative of the states: dxdt = f(x, u, p)
Parameters
----------
time: MX | SX
The time of the system
states: MX | SX
The state of the system
controls: MX | SX
The controls of the system
parameters: MX | SX
The parameters acting on the system
algebraic_states: MX | SX
The algebraic states of the system
nlp: NonLinearProgram
A reference to the phase
my_additional_factor: int
An example of an extra parameter sent by the user
Returns
-------
The derivative of the states in the tuple[MX | SX] format
"""
q = DynamicsFunctions.get(nlp.states["q"], states)
qdot = DynamicsFunctions.get(nlp.states["qdot"], states)
tau = DynamicsFunctions.get(nlp.controls["tau"], controls)
# You can directly call biorbd function (as for ddq) or call bioptim accessor (as for dq)
dq = DynamicsFunctions.compute_qdot(nlp, q, qdot) * my_additional_factor
ddq = nlp.model.forward_dynamics(q, qdot, tau)
# the user has to choose if want to return the explicit dynamics dx/dt = f(x,u,p)
# as the first argument of DynamicsEvaluation or
# the implicit dynamics f(x,u,p,xdot)=0 as the second argument
# which may be useful for IRK or COLLOCATION integrators
return DynamicsEvaluation(dxdt=vertcat(dq, ddq), defects=None)
def custom_configure(
ocp: OptimalControlProgram, nlp: NonLinearProgram, my_additional_factor=1, numerical_data_timeseries=None
):
"""
Tell the program which variables are states and controls.
The user is expected to use the ConfigureProblem.configure_xxx functions.
Parameters
----------
ocp: OptimalControlProgram
A reference to the ocp
nlp: NonLinearProgram
A reference to the phase
my_additional_factor: int
An example of an extra parameter sent by the user
"""
ConfigureProblem.configure_q(ocp, nlp, as_states=True, as_controls=False)
ConfigureProblem.configure_qdot(ocp, nlp, as_states=True, as_controls=False)
ConfigureProblem.configure_tau(ocp, nlp, as_states=False, as_controls=True)
ConfigureProblem.configure_dynamics_function(ocp, nlp, custom_dynamics, my_additional_factor=my_additional_factor)
def prepare_ocp(
biorbd_model_path: str,
problem_type_custom: bool = True,
ode_solver: OdeSolverBase = OdeSolver.RK4(),
use_sx: bool = False,
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
problem_type_custom: bool
If the preparation should be done using the user-defined dynamic function or the normal TORQUE_DRIVEN.
They should return the same results
ode_solver: OdeSolverBase
The type of ode solver used
use_sx: bool
If the program should be constructed using SX instead of MX (longer to create the CasADi graph, faster to solve)
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)
Returns
-------
The ocp ready to be solved
"""
# --- Options --- #
# BioModel path
bio_model = BiorbdModel(biorbd_model_path)
# Problem parameters
n_shooting = 30
final_time = 2
# Add objective functions
objective_functions = Objective(ObjectiveFcn.Lagrange.MINIMIZE_CONTROL, key="tau", weight=100, multi_thread=False)
# Dynamics
dynamics = DynamicsList()
if problem_type_custom:
dynamics.add(
custom_configure,
dynamic_function=custom_dynamics,
my_additional_factor=1,
expand_dynamics=expand_dynamics,
phase_dynamics=phase_dynamics,
)
else:
dynamics.add(
DynamicsFcn.TORQUE_DRIVEN,
dynamic_function=custom_dynamics,
expand_dynamics=expand_dynamics,
phase_dynamics=phase_dynamics,
)
# Constraints
constraints = ConstraintList()
constraints.add(ConstraintFcn.SUPERIMPOSE_MARKERS, node=Node.START, first_marker="m0", second_marker="m1")
constraints.add(ConstraintFcn.SUPERIMPOSE_MARKERS, node=Node.END, first_marker="m0", second_marker="m2")
# Path constraint
x_bounds = BoundsList()
x_bounds.add("q", bio_model.bounds_from_ranges("q"))
x_bounds.add("qdot", bio_model.bounds_from_ranges("qdot"))
x_bounds["q"][1:, [0, -1]] = 0
x_bounds["q"][2, -1] = 1.57
x_bounds["qdot"][:, [0, -1]] = 0
# Define control path constraint
tau_min, tau_max = -100, 100
u_bounds = BoundsList()
u_bounds.add("tau", min_bound=[tau_min] * bio_model.nb_tau, max_bound=[tau_max] * bio_model.nb_tau)
# ------------- #
return OptimalControlProgram(
bio_model,
dynamics,
n_shooting,
final_time,
x_bounds=x_bounds,
u_bounds=u_bounds,
objective_functions=objective_functions,
constraints=constraints,
ode_solver=ode_solver,
use_sx=use_sx,
)
def main():
"""
Runs the optimization and animates it
"""
model_path = "models/cube.bioMod"
ocp = prepare_ocp(biorbd_model_path=model_path)
# --- Solve the program --- #
sol = ocp.solve(Solver.IPOPT(show_online_optim=platform.system() == "Linux"))
# --- Show results --- #
sol.animate()
if __name__ == "__main__":
main()