@@ -20,13 +20,34 @@ class PyTinySolver {
2020 void set_x0 (Eigen::Ref<tinyVector>);
2121 void set_x_ref (Eigen::Ref<tinyMatrix>);
2222 void set_u_ref (Eigen::Ref<tinyMatrix>);
23+ void set_sensitivity_matrices (
24+ Eigen::Ref<tinyMatrix> dK,
25+ Eigen::Ref<tinyMatrix> dP,
26+ Eigen::Ref<tinyMatrix> dC1,
27+ Eigen::Ref<tinyMatrix> dC2,
28+ float rho = 0.0 ,
29+ int verbose = 0 );
30+ void set_cache_terms (
31+ Eigen::Ref<tinyMatrix> Kinf,
32+ Eigen::Ref<tinyMatrix> Pinf,
33+ Eigen::Ref<tinyMatrix> Quu_inv,
34+ Eigen::Ref<tinyMatrix> AmBKt,
35+ int verbose = 0 );
36+ void update_settings (TinySettings* settings);
2337
2438 int solve ();
2539 TinySolution* get_solution ();
2640
2741 void print_problem_data ();
2842
2943 int codegen (const char *, int ); // output_dir, verbosity
44+
45+ int codegen_with_sensitivity (const char *output_dir,
46+ Eigen::Ref<tinyMatrix> dK,
47+ Eigen::Ref<tinyMatrix> dP,
48+ Eigen::Ref<tinyMatrix> dC1,
49+ Eigen::Ref<tinyMatrix> dC2,
50+ int verbose);
3051 private:
3152 TinySolver *_solver;
3253};
@@ -67,6 +88,37 @@ void PyTinySolver::set_u_ref(Eigen::Ref<tinyMatrix> u_ref) {
6788 tiny_set_u_ref (this ->_solver , u_ref);
6889}
6990
91+ void PyTinySolver::set_sensitivity_matrices (
92+ Eigen::Ref<tinyMatrix> dK,
93+ Eigen::Ref<tinyMatrix> dP,
94+ Eigen::Ref<tinyMatrix> dC1,
95+ Eigen::Ref<tinyMatrix> dC2,
96+ float rho,
97+ int verbose) {
98+ // Create copies of the matrices to ensure they remain valid
99+ tinyMatrix dK_copy = dK;
100+ tinyMatrix dP_copy = dP;
101+ tinyMatrix dC1_copy = dC1;
102+ tinyMatrix dC2_copy = dC2;
103+
104+ // Store the sensitivity matrices in the solver's cache
105+ if (this ->_solver ->cache != nullptr ) {
106+
107+ // For now, we'll just store them for code generation
108+ if (verbose) {
109+ std::cout << " Sensitivity matrices set for code generation" << std::endl;
110+ std::cout << " dK norm: " << dK_copy.norm () << std::endl;
111+ std::cout << " dP norm: " << dP_copy.norm () << std::endl;
112+ std::cout << " dC1 norm: " << dC1_copy.norm () << std::endl;
113+ std::cout << " dC2 norm: " << dC2_copy.norm () << std::endl;
114+ }
115+ } else {
116+ if (verbose) {
117+ std::cout << " Warning: Cache not initialized, sensitivity matrices will only be used for code generation" << std::endl;
118+ }
119+ }
120+ }
121+
70122int PyTinySolver::solve () {
71123 py::gil_scoped_release release;
72124 int status = tiny_solve (this ->_solver );
@@ -134,6 +186,69 @@ int PyTinySolver::codegen(const char *output_dir, int verbose) {
134186 return tiny_codegen (this ->_solver , output_dir, verbose);
135187}
136188
189+ int PyTinySolver::codegen_with_sensitivity (const char *output_dir,
190+ Eigen::Ref<tinyMatrix> dK_ref,
191+ Eigen::Ref<tinyMatrix> dP_ref,
192+ Eigen::Ref<tinyMatrix> dC1_ref,
193+ Eigen::Ref<tinyMatrix> dC2_ref,
194+ int verbose) {
195+ tinyMatrix dK_copy = dK_ref;
196+ tinyMatrix dP_copy = dP_ref;
197+ tinyMatrix dC1_copy = dC1_ref;
198+ tinyMatrix dC2_copy = dC2_ref;
199+
200+ return tiny_codegen_with_sensitivity (this ->_solver , output_dir,
201+ &dK_copy, &dP_copy, &dC1_copy, &dC2_copy, verbose);
202+ }
203+
204+ void PyTinySolver::set_cache_terms (
205+ Eigen::Ref<tinyMatrix> Kinf,
206+ Eigen::Ref<tinyMatrix> Pinf,
207+ Eigen::Ref<tinyMatrix> Quu_inv,
208+ Eigen::Ref<tinyMatrix> AmBKt,
209+ int verbose) {
210+ if (!this ->_solver ) {
211+ throw py::value_error (" Solver not initialized" );
212+ }
213+ if (!this ->_solver ->cache ) {
214+ throw py::value_error (" Solver cache not initialized" );
215+ }
216+
217+ // Create copies of the matrices to ensure they remain valid
218+ this ->_solver ->cache ->Kinf = Kinf;
219+ this ->_solver ->cache ->Pinf = Pinf;
220+ this ->_solver ->cache ->Quu_inv = Quu_inv;
221+ this ->_solver ->cache ->AmBKt = AmBKt;
222+ this ->_solver ->cache ->C1 = Quu_inv; // Cache terms
223+ this ->_solver ->cache ->C2 = AmBKt; // Cache terms
224+
225+ if (verbose) {
226+ std::cout << " Cache terms set with norms:" << std::endl;
227+ std::cout << " Kinf norm: " << Kinf.norm () << std::endl;
228+ std::cout << " Pinf norm: " << Pinf.norm () << std::endl;
229+ std::cout << " Quu_inv norm: " << Quu_inv.norm () << std::endl;
230+ std::cout << " AmBKt norm: " << AmBKt.norm () << std::endl;
231+ }
232+ }
233+
234+ void PyTinySolver::update_settings (TinySettings* settings) {
235+ if (this ->_solver && this ->_solver ->settings && settings) {
236+ // Copy settings to the solver's settings
237+ this ->_solver ->settings ->abs_pri_tol = settings->abs_pri_tol ;
238+ this ->_solver ->settings ->abs_dua_tol = settings->abs_dua_tol ;
239+ this ->_solver ->settings ->max_iter = settings->max_iter ;
240+ this ->_solver ->settings ->check_termination = settings->check_termination ;
241+ this ->_solver ->settings ->en_state_bound = settings->en_state_bound ;
242+ this ->_solver ->settings ->en_input_bound = settings->en_input_bound ;
243+
244+ // Copy adaptive rho settings
245+ this ->_solver ->settings ->adaptive_rho = settings->adaptive_rho ;
246+ this ->_solver ->settings ->adaptive_rho_min = settings->adaptive_rho_min ;
247+ this ->_solver ->settings ->adaptive_rho_max = settings->adaptive_rho_max ;
248+ this ->_solver ->settings ->adaptive_rho_enable_clipping = settings->adaptive_rho_enable_clipping ;
249+ }
250+ }
251+
137252PYBIND11_MODULE (ext_tinympc, m) {
138253// // PYBIND11_MODULE(@TINYMPC_EXT_MODULE_NAME@, m) {
139254
@@ -168,7 +283,11 @@ PYBIND11_MODULE(ext_tinympc, m) {
168283 .def_readwrite (" max_iter" , &TinySettings::max_iter)
169284 .def_readwrite (" check_termination" , &TinySettings::check_termination)
170285 .def_readwrite (" en_state_bound" , &TinySettings::en_state_bound)
171- .def_readwrite (" en_input_bound" , &TinySettings::en_input_bound);
286+ .def_readwrite (" en_input_bound" , &TinySettings::en_input_bound)
287+ .def_readwrite (" adaptive_rho" , &TinySettings::adaptive_rho)
288+ .def_readwrite (" adaptive_rho_min" , &TinySettings::adaptive_rho_min)
289+ .def_readwrite (" adaptive_rho_max" , &TinySettings::adaptive_rho_max)
290+ .def_readwrite (" adaptive_rho_enable_clipping" , &TinySettings::adaptive_rho_enable_clipping);
172291
173292 m.def (" tiny_set_default_settings" , &tiny_set_default_settings);
174293
@@ -180,8 +299,19 @@ PYBIND11_MODULE(ext_tinympc, m) {
180299 .def (" set_x0" , &PyTinySolver::set_x0)
181300 .def (" set_x_ref" , &PyTinySolver::set_x_ref)
182301 .def (" set_u_ref" , &PyTinySolver::set_u_ref)
302+ .def (" update_settings" , &PyTinySolver::update_settings)
303+ .def (" set_sensitivity_matrices" , &PyTinySolver::set_sensitivity_matrices,
304+ " dK" _a.noconvert (), " dP" _a.noconvert (),
305+ " dC1" _a.noconvert (), " dC2" _a.noconvert (),
306+ " rho" _a=0.0 , " verbose" _a=0 )
307+ .def (" set_cache_terms" , &PyTinySolver::set_cache_terms,
308+ py::arg (" Kinf" ), py::arg (" Pinf" ), py::arg (" Quu_inv" ), py::arg (" AmBKt" ),
309+ py::arg (" verbose" ) = 0 )
183310 .def (" solve" , &PyTinySolver::solve)
184311 .def (" print_problem_data" , &PyTinySolver::print_problem_data)
185- .def (" codegen" , &PyTinySolver::codegen);
312+ .def (" codegen" , &PyTinySolver::codegen, " output_dir" _a, " verbose" _a=0 )
313+ .def (" codegen_with_sensitivity" , &PyTinySolver::codegen_with_sensitivity,
314+ " output_dir" _a, " dK" _a.noconvert (), " dP" _a.noconvert (),
315+ " dC1" _a.noconvert (), " dC2" _a.noconvert (), " verbose" _a);
186316
187- }
317+ }
0 commit comments