|
| 1 | +""" |
| 2 | +A very simple yet meaningful optimal control program consisting in a pendulum starting downward and ending upward |
| 3 | +while requiring the minimum of generalized forces. The solver is only allowed to move the pendulum sideways. |
| 4 | +
|
| 5 | +This simple example is a good place to start investigating bioptim as it describes the most common dynamics out there |
| 6 | +(the joint torque driven), it defines an objective function and some boundaries and initial guesses |
| 7 | +
|
| 8 | +During the optimization process, the graphs are updated real-time (even though it is a bit too fast and short to really |
| 9 | +appreciate it). Finally, once it finished optimizing, it animates the model using the optimal solution |
| 10 | +""" |
| 11 | + |
| 12 | +from bioptim import ( |
| 13 | + OptimalControlProgram, |
| 14 | + DynamicsOptions, |
| 15 | + BoundsList, |
| 16 | + InitialGuessList, |
| 17 | + ObjectiveFcn, |
| 18 | + Objective, |
| 19 | + OdeSolver, |
| 20 | + OdeSolverBase, |
| 21 | + Solver, |
| 22 | + TorquePinocchioModel, |
| 23 | + PhaseDynamics, |
| 24 | + OnlineOptim, |
| 25 | + OrderingStrategy, |
| 26 | + CostType, |
| 27 | +) |
| 28 | + |
| 29 | +from bioptim.examples.utils import ExampleUtils |
| 30 | + |
| 31 | + |
| 32 | +def prepare_ocp( |
| 33 | + model_path: str, |
| 34 | + final_time: float, |
| 35 | + n_shooting: int, |
| 36 | + ode_solver: OdeSolverBase = OdeSolver.RK4(), |
| 37 | + n_threads: int = 1, |
| 38 | + phase_dynamics: PhaseDynamics = PhaseDynamics.SHARED_DURING_THE_PHASE, |
| 39 | + expand_dynamics: bool = True, |
| 40 | + ordering_strategy: OrderingStrategy = OrderingStrategy.VARIABLE_MAJOR, |
| 41 | +) -> OptimalControlProgram: |
| 42 | + """ |
| 43 | + The initialization of an ocp |
| 44 | +
|
| 45 | + Parameters |
| 46 | + ---------- |
| 47 | + model_path: str |
| 48 | + The path to the Pinocchio model |
| 49 | + final_time: float |
| 50 | + The time in second required to perform the task |
| 51 | + n_shooting: int |
| 52 | + The number of shooting points to define int the direct multiple shooting program |
| 53 | + ode_solver: OdeSolverBase = OdeSolver.RK4() |
| 54 | + Which type of OdeSolver to use |
| 55 | + n_threads: int |
| 56 | + The number of threads to use in the paralleling (1 = no parallel computing) |
| 57 | + phase_dynamics: PhaseDynamics |
| 58 | + If the dynamics equation within a phase is unique or changes at each node. |
| 59 | + PhaseDynamics.SHARED_DURING_THE_PHASE is much faster, but lacks the capability to have changing dynamics within |
| 60 | + a phase. PhaseDynamics.ONE_PER_NODE should also be used when multi-node penalties with more than 3 nodes or with COLLOCATION (cx_intermediate_list) are added to the OCP. |
| 61 | + expand_dynamics: bool |
| 62 | + If the dynamics function should be expanded. Please note, this will solve the problem faster, but will slow down |
| 63 | + the declaration of the OCP, so it is a trade-off. Also depending on the solver, it may or may not work |
| 64 | + (for instance IRK is not compatible with expanded dynamics) |
| 65 | +
|
| 66 | + Returns |
| 67 | + ------- |
| 68 | + The OptimalControlProgram ready to be solved |
| 69 | + """ |
| 70 | + |
| 71 | + bio_model = TorquePinocchioModel(model_path) |
| 72 | + |
| 73 | + # Add objective functions |
| 74 | + objective_functions = Objective(ObjectiveFcn.Lagrange.MINIMIZE_CONTROL, key="tau") |
| 75 | + |
| 76 | + # DynamicsOptions |
| 77 | + dynamics = DynamicsOptions(ode_solver=ode_solver, expand_dynamics=expand_dynamics, phase_dynamics=phase_dynamics) |
| 78 | + |
| 79 | + # Path bounds |
| 80 | + x_bounds = BoundsList() |
| 81 | + x_bounds["q"] = bio_model.bounds_from_ranges("q") |
| 82 | + x_bounds["q"][:, [0, -1]] = 0 # Start and end at 0... |
| 83 | + x_bounds["q"][1, -1] = 3.14 # ...but end with pendulum 180 degrees rotated |
| 84 | + x_bounds["qdot"] = bio_model.bounds_from_ranges("qdot") |
| 85 | + x_bounds["qdot"][:, [0, -1]] = 0 # Start and end without any velocity |
| 86 | + |
| 87 | + # Initial guess (optional since it is 0, we show how to initialize anyway) |
| 88 | + x_init = InitialGuessList() |
| 89 | + x_init["q"] = [0] * bio_model.nb_q |
| 90 | + x_init["qdot"] = [0] * bio_model.nb_qdot |
| 91 | + |
| 92 | + # Define control path bounds |
| 93 | + n_tau = bio_model.nb_tau |
| 94 | + u_bounds = BoundsList() |
| 95 | + u_bounds["tau"] = [-100] * n_tau, [100] * n_tau # Limit the strength of the pendulum to (-100 to 100)... |
| 96 | + u_bounds["tau"][1, :] = 0 # ...but remove the capability to actively rotate |
| 97 | + |
| 98 | + # Initial guess (optional since it is 0, we show how to initialize anyway) |
| 99 | + u_init = InitialGuessList() |
| 100 | + u_init["tau"] = [0] * n_tau |
| 101 | + |
| 102 | + return OptimalControlProgram( |
| 103 | + bio_model, |
| 104 | + n_shooting, |
| 105 | + final_time, |
| 106 | + dynamics=dynamics, |
| 107 | + x_init=x_init, |
| 108 | + u_init=u_init, |
| 109 | + x_bounds=x_bounds, |
| 110 | + u_bounds=u_bounds, |
| 111 | + objective_functions=objective_functions, |
| 112 | + use_sx=True, |
| 113 | + n_threads=n_threads, |
| 114 | + ordering_strategy=ordering_strategy, |
| 115 | + ) |
| 116 | + |
| 117 | + |
| 118 | +def main(): |
| 119 | + """ |
| 120 | + If pendulum is run as a script, it will perform the optimization and animates it |
| 121 | + """ |
| 122 | + |
| 123 | + # --- Prepare the ocp --- # |
| 124 | + model_path = ExampleUtils.folder + "/models/pendulum.urdf" |
| 125 | + ocp = prepare_ocp(model_path=model_path, final_time=1, n_shooting=400, n_threads=2) |
| 126 | + |
| 127 | + # --- Solve the ocp --- # |
| 128 | + ocp.add_plot_penalty(CostType.ALL) # This will display the objectives and constraints at the current iteration |
| 129 | + solver = Solver.IPOPT(online_optim=OnlineOptim.DEFAULT) |
| 130 | + sol = ocp.solve(solver) |
| 131 | + |
| 132 | + # --- Show the results graph --- # |
| 133 | + sol.print_cost() |
| 134 | + # sol.graphs(show_bounds=True, save_name="results.png") |
| 135 | + |
| 136 | + |
| 137 | +if __name__ == "__main__": |
| 138 | + main() |
0 commit comments