Skip to content

Commit

Permalink
Merge pull request #715 from EveCharbie/StochasticOCP_merge
Browse files Browse the repository at this point in the history
StochastiOCP merge
  • Loading branch information
pariterre authored Jul 18, 2023
2 parents 9237b31 + 377de7b commit 1112d7d
Show file tree
Hide file tree
Showing 59 changed files with 19,857 additions and 321 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -114,3 +114,6 @@ c_generated_code
# Bioptim files
bioptim/sandbox/
*.pkl

# Mac dev
*.DS_store
Binary file added OCP_equation.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ As a tour guide that uses this binder, you can watch the `bioptim` workshop that
- [From the sources](#installing-from-the-sources-for-linux-mac-and-windows)
- [Installation complete](#installation-complete)

[Defining our optimal control problems](#defining-our-optimal-control-problems)

[A first practical example](#a-first-practical-example)
- [The import](#the-import)
- [Building the ocp](#building-the-ocp)
Expand Down Expand Up @@ -236,6 +238,19 @@ python setup.py install
Assuming everything went well, that is it!
You can already enjoy bioptimizing!

# Defining our optimal control problems
Here we will detail our implementation of optimal control problems and some definitions.
The mathematical transcription of the OCP is as follows:
![](OCP_equation.jpg)
The optimization variables are the states (x = variables that represent the state of the system at each node and that
are subject to continuity constraints), controls (u = decision variables defined at each node that have an effect on the system),
algebraic states (s = optimization variables that are defined at each node, but that are not subject to the
built-in continuity constraints), and parameters (p = optimization variables that are defined once per phase).
The state continuity constraints implementation may vary depending on the transcription of the problem (implicit vs explicit, direct multiple shooting vs direct collocations).

The cost function can include Mayer terms (function evaluated at one node) and Lagrange terms (functions integrated over the duration of the phase).
The optimization variables can be subject to equality and/or inequality constraints.

# A first practical example
The easiest way to learn `bioptim` is to dive into it.
So let's do that and build our first optimal control program together.
Expand Down
2 changes: 2 additions & 0 deletions bioptim/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,4 +220,6 @@
from .optimization.variable_scaling import VariableScalingList, VariableScaling
from .optimization.variational_optimal_control_program import VariationalOptimalControlProgram

from .optimization.stochastic_optimal_control_program import StochasticOptimalControlProgram
from .optimization.problem_type import SocpType
from .misc.casadi_expand import lt, le, gt, ge, if_else, if_else_zero
303 changes: 279 additions & 24 deletions bioptim/dynamics/configure_problem.py

Large diffs are not rendered by default.

19 changes: 17 additions & 2 deletions bioptim/dynamics/dynamics_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ class DynamicsFunctions:
"""

@staticmethod
def custom(states: MX.sym, controls: MX.sym, parameters: MX.sym, nlp) -> DynamicsEvaluation:
def custom(
states: MX.sym, controls: MX.sym, parameters: MX.sym, stochastic_variables: MX.sym, nlp
) -> DynamicsEvaluation:
"""
Interface to custom dynamic function provided by the user.
Expand All @@ -65,13 +67,14 @@ def custom(states: MX.sym, controls: MX.sym, parameters: MX.sym, nlp) -> Dynamic
The defects of the implicit dynamics
"""

return nlp.dynamics_type.dynamic_function(states, controls, parameters, nlp)
return nlp.dynamics_type.dynamic_function(states, controls, parameters, stochastic_variables, nlp)

@staticmethod
def torque_driven(
states: MX.sym,
controls: MX.sym,
parameters: MX.sym,
stochastic_variables: MX.sym,
nlp,
with_contact: bool,
with_passive_torque: bool,
Expand Down Expand Up @@ -227,6 +230,7 @@ def torque_activations_driven(
states: MX.sym,
controls: MX.sym,
parameters: MX.sym,
stochastic_variables: MX.sym,
nlp,
with_contact: bool,
with_passive_torque: bool,
Expand Down Expand Up @@ -285,6 +289,7 @@ def torque_derivative_driven(
states: MX.sym,
controls: MX.sym,
parameters: MX.sym,
stochastic_variables: MX.sym,
nlp,
rigidbody_dynamics: RigidBodyDynamics,
with_contact: bool,
Expand Down Expand Up @@ -355,6 +360,7 @@ def forces_from_torque_driven(
states: MX.sym,
controls: MX.sym,
parameters: MX.sym,
stochastic_variables: MX.sym,
nlp,
with_passive_torque: bool = False,
with_ligament: bool = False,
Expand All @@ -370,6 +376,8 @@ def forces_from_torque_driven(
The controls of the system
parameters: MX.sym
The parameters of the system
stochastic_variables: MX.sym
The stochastic variables of the system
nlp: NonLinearProgram
The definition of the system
with_passive_torque: bool
Expand Down Expand Up @@ -400,6 +408,7 @@ def forces_from_torque_activation_driven(
states: MX.sym,
controls: MX.sym,
parameters: MX.sym,
stochastic_variables: MX.sym,
nlp,
with_passive_torque: bool = False,
with_ligament: bool = False,
Expand Down Expand Up @@ -445,6 +454,7 @@ def muscles_driven(
states: MX.sym,
controls: MX.sym,
parameters: MX.sym,
stochastic_variables: MX.sym,
nlp,
with_contact: bool,
with_passive_torque: bool = False,
Expand Down Expand Up @@ -580,6 +590,7 @@ def forces_from_muscle_driven(
states: MX.sym,
controls: MX.sym,
parameters: MX.sym,
stochastic_variables: MX.sym,
nlp,
with_passive_torque: bool = False,
with_ligament: bool = False,
Expand Down Expand Up @@ -626,6 +637,7 @@ def joints_acceleration_driven(
states: MX.sym,
controls: MX.sym,
parameters: MX.sym,
stochastic_variables: MX.sym,
nlp,
rigidbody_dynamics: RigidBodyDynamics = RigidBodyDynamics.ODE,
) -> DynamicsEvaluation:
Expand Down Expand Up @@ -892,6 +904,7 @@ def holonomic_torque_driven(
states: MX | SX,
controls: MX | SX,
parameters: MX | SX,
stochastic_variables: MX | SX,
nlp: NonLinearProgram,
) -> DynamicsEvaluation:
"""
Expand All @@ -905,6 +918,8 @@ def holonomic_torque_driven(
The controls of the system
parameters: MX | SX
The parameters acting on the system
stochastic_variables: MX | SX
The stochastic variables of the system
nlp: NonLinearProgram
A reference to the phase
Expand Down
Loading

0 comments on commit 1112d7d

Please sign in to comment.