11import numpy as np
2+ from copy import deepcopy
23from casadi import SX
34from bioptim import (
45 BiorbdModel ,
1112 Solver ,
1213 MultiCyclicCycleSolutions ,
1314 ExternalForceSetTimeSeries ,
14- OdeSolver ,
15+ ControlType ,
1516)
1617from .fes_nmpc import FesNmpc
1718from ..models .dynamical_model import FesMskModel
@@ -99,7 +100,9 @@ def _initialize_solution(self, dt: float, states: list, controls: list, paramete
99100 dynamics = self .nlp [0 ].dynamics_type ,
100101 n_shooting = self .total_optimization_run * self .cycle_len ,
101102 phase_time = self .total_optimization_run * self .cycle_len * dt ,
103+ x_bounds = self .nlp [0 ].x_bounds ,
102104 x_init = x_init ,
105+ u_bounds = self .nlp [0 ].u_bounds ,
103106 u_init = u_init ,
104107 use_sx = self .cx == SX ,
105108 parameters = parameters ,
@@ -108,6 +111,65 @@ def _initialize_solution(self, dt: float, states: list, controls: list, paramete
108111 a_init = InitialGuessList ()
109112 return Solution .from_initial_guess (solution_ocp , [np .array ([dt ]), x_init , u_init , p_init , a_init ])
110113
114+ def _initialize_one_cycle (self , dt : float , states : np .ndarray , controls : np .ndarray , parameters : np .ndarray ):
115+ """return a solution for a single window kept of the MHE"""
116+ x_init = InitialGuessList ()
117+ for key in self .nlp [0 ].states .keys ():
118+ x_init .add (
119+ key ,
120+ states [key ],
121+ interpolation = self .nlp [0 ].x_init .type ,
122+ phase = 0 ,
123+ )
124+
125+ u_init = InitialGuessList ()
126+ u_init_for_solution = InitialGuessList ()
127+ for key in self .nlp [0 ].controls .keys ():
128+ controls_tp = controls [key ]
129+ u_init_for_solution .add (key , controls_tp , interpolation = InterpolationType .EACH_FRAME , phase = 0 )
130+ if self .nlp [0 ].control_type == ControlType .CONSTANT :
131+ controls_tp = controls_tp [:, :- 1 ]
132+ u_init .add (key , controls_tp , interpolation = InterpolationType .EACH_FRAME , phase = 0 )
133+
134+ model_serialized = self .nlp [0 ].model .serialize ()
135+ model_class = model_serialized [0 ]
136+ model_initializer = model_serialized [1 ]
137+
138+ param_list = ParameterList (use_sx = self .cx == SX )
139+ p_init = InitialGuessList ()
140+ for key in self .nlp [0 ].parameters .keys ():
141+ parameters_tp = parameters [key ]
142+ param_list .add (
143+ name = key ,
144+ function = self .nlp [0 ].parameters [key ].function ,
145+ size = self .nlp [0 ].parameters [key ].shape ,
146+ scaling = self .nlp [0 ].parameters [key ].scaling ,
147+ )
148+ p_init .add (
149+ key ,
150+ parameters_tp ,
151+ interpolation = InterpolationType .EACH_FRAME ,
152+ phase = 0 ,
153+ )
154+
155+ solution_ocp = OptimalControlProgram (
156+ bio_model = model_class (** model_initializer ),
157+ dynamics = self .nlp [0 ].dynamics_type ,
158+ objective_functions = deepcopy (self .common_objective_functions ),
159+ n_shooting = self .cycle_len ,
160+ phase_time = self .cycle_len * dt ,
161+ x_bounds = self .nlp [0 ].x_bounds ,
162+ x_init = x_init ,
163+ u_bounds = self .nlp [0 ].u_bounds ,
164+ u_init = u_init ,
165+ use_sx = self .cx == SX ,
166+ parameters = param_list ,
167+ parameter_init = p_init ,
168+ parameter_bounds = self .parameter_bounds ,
169+ )
170+ a_init = InitialGuessList ()
171+ return Solution .from_initial_guess (solution_ocp , [np .array ([dt ]), x_init , u_init_for_solution , p_init , a_init ])
172+
111173 def create_model_from_list (self , models : list ):
112174 if isinstance (models [0 ], BiorbdModel ):
113175 return models [0 ]
0 commit comments