alpaqa_problem.cpp
1 #include "alpaqa_problem.hpp"
2 #include "alpaqa_interface.hpp"
3 #include "casadi/core/exception.hpp"
4 
5 namespace casadi {
6 
8  : alpaqa::BoxConstrProblem<alpaqa::DefaultConfig>{
9  static_cast<alpaqa::DefaultConfig::length_t>(solver.nx_),
10  static_cast<alpaqa::DefaultConfig::length_t>(solver.ng_)},
11  solver_(solver), mem_(mem) {
12 
13 }
14 
15 //AlpaqaProblem::AlpaqaProblem(const AlpaqaProblem &) = default;
16 //AlpaqaProblem& AlpaqaProblem::operator=(const AlpaqaProblem &) = default;
17 //AlpaqaProblem::AlpaqaProblem(AlpaqaProblem &&) noexcept = default;
18 //AlpaqaProblem & AlpaqaProblem::operator=(AlpaqaProblem &&) noexcept = default;
20 
21 double AlpaqaProblem::eval_f(crvec x) const {
22  double obj_value;
23  mem_->arg[0] = x.data();
24  mem_->arg[1] = mem_->d_nlp.p;
25  mem_->res[0] = &obj_value;
26  try {
27  casadi_assert(solver_.calc_function(mem_, "nlp_f")==0, "Failing evaluating eval_f");
28  } catch(KeyboardInterruptException& ex) {
29  casadi_warning("KeyboardInterruptException");
31  } catch (std::exception& ex) {
32  if (solver_.show_eval_warnings_) {
33  casadi_warning("AlpaqaProblem::eval_f failed:" + std::string(ex.what()));
34  }
35  }
36 
37  return obj_value;
38 }
39 
40 void AlpaqaProblem::eval_grad_f(crvec x, rvec grad_fx) const {
41  eval_f_grad_f(x, grad_fx);
42 }
43 
44 double AlpaqaProblem::eval_f_grad_f(crvec x, rvec grad_fx) const {
45  double obj_value;
46  mem_->arg[0] = x.data();
47  mem_->arg[1] = mem_->d_nlp.p;
48  mem_->res[0] = &obj_value;
49  mem_->res[1] = grad_fx.data();
50  try {
51  casadi_assert(solver_.calc_function(mem_, "nlp_f_grad_f")==0, "Failing evaluating eval_f_grad_f");
52  } catch(KeyboardInterruptException& ex) {
53  casadi_warning("KeyboardInterruptException");
55  } catch (std::exception& ex) {
56  if (solver_.show_eval_warnings_) {
57  casadi_warning("AlpaqaProblem::eval_f_grad_f failed:" + std::string(ex.what()));
58  }
59  }
60  return obj_value;
61 }
62 
63 
64 template<typename T1, typename T2>
65 void copy(const T1* x, casadi_int n, T2* y) {
66  casadi_int i;
67  if (y) {
68  if (x) {
69  for (i=0; i<n; ++i) *y++ = *x++;
70  } else {
71  for (i=0; i<n; ++i) *y++ = 0.;
72  }
73  }
74 }
75 
76 void AlpaqaProblem::eval_g(crvec x, rvec g) const {
77  mem_->arg[0] = x.data();
78  mem_->arg[1] = mem_->d_nlp.p;
79  mem_->res[0] = g.data();
80  try {
81  casadi_assert(solver_.calc_function(mem_, "nlp_g")==0, "Failing evaluating eval_f_grad_f");
82  } catch(KeyboardInterruptException& ex) {
83  casadi_warning("KeyboardInterruptException");
85  } catch (std::exception& ex) {
86  if (solver_.show_eval_warnings_) {
87  casadi_warning("AlpaqaProblem::eval_g failed:" + std::string(ex.what()));
88  }
89  }
90 }
91 
92 void AlpaqaProblem::eval_jac_g(crvec x, rindexvec inner_idx,
93  rindexvec outer_ptr, rvec J_values) const {
94  if (J_values.size()>0) {
95  mem_->arg[0] = x.data();
96  mem_->arg[1] = mem_->d_nlp.p;
97  mem_->res[0] = J_values.data();
98  try {
99  casadi_assert(solver_.calc_function(mem_, "nlp_jac_g")==0, "Failing evaluating eval_f_grad_f");
100  } catch(KeyboardInterruptException& ex) {
101  casadi_warning("KeyboardInterruptException");
103  } catch (std::exception& ex) {
104  if (solver_.show_eval_warnings_) {
105  casadi_warning("AlpaqaProblem::eval_jac_g failed:" + std::string(ex.what()));
106  }
107  }
108  } else {
109  const Sparsity& sp = solver_.jacg_sp_;
110  if (!sp.is_dense()) {
111  copy(sp.row(), sp.nnz(), inner_idx.data());
112  copy(sp.colind(), get_n()+1, outer_ptr.data());
113  }
114  }
115 }
116 
117 void AlpaqaProblem::eval_hess_L(crvec x, crvec y, real_t scale,
118  rindexvec inner_idx, rindexvec outer_ptr,
119  rvec H_values) const {
120  if (H_values.size()>0) {
121  mem_->arg[0] = x.data();
122  mem_->arg[1] = mem_->d_nlp.p;
123  mem_->arg[2] = y.data();
124  mem_->arg[3] = &scale;
125  mem_->res[0] = H_values.data();
126  try {
127  casadi_assert(solver_.calc_function(mem_, "nlp_hess_L")==0, "Failing evaluating eval_f_grad_f");
128  } catch(KeyboardInterruptException& ex) {
129  casadi_warning("KeyboardInterruptException");
131  } catch (std::exception& ex) {
132  if (solver_.show_eval_warnings_) {
133  casadi_warning("AlpaqaProblem::eval_hess_L failed:" + std::string(ex.what()));
134  }
135  }
136  } else {
137  const Sparsity& sp = solver_.get_function("nlp_hess_L").sparsity_out(0);
138  if (!sp.is_dense()) {
139  copy(sp.row(), sp.nnz(), inner_idx.data());
140  copy(sp.colind(), get_n()+1, outer_ptr.data());
141  }
142  }
143 }
144 
145 void AlpaqaProblem::eval_hess_L_prod(crvec x, crvec y, real_t scale,
146  crvec v, rvec Hv) const {
147  mem_->arg[0] = x.data();
148  mem_->arg[1] = mem_->d_nlp.p;
149  mem_->arg[2] = y.data();
150  mem_->arg[3] = &scale;
151  mem_->arg[4] = v.data();
152  mem_->res[0] = Hv.data();
153  try {
154  casadi_assert(solver_.calc_function(mem_, "nlp_hess_L_prod")==0, "Failing evaluating eval_f_grad_f");
155  } catch(KeyboardInterruptException& ex) {
156  casadi_warning("KeyboardInterruptException");
158  } catch (std::exception& ex) {
159  if (solver_.show_eval_warnings_) {
160  casadi_warning("AlpaqaProblem::eval_hess_L_prod failed:" + std::string(ex.what()));
161  }
162  }
163 }
164 
165 void AlpaqaProblem::eval_grad_g_prod(crvec, crvec, rvec) const {
166  casadi_error("Not implemented");
167 }
168 
169 void AlpaqaProblem::eval_grad_gi(crvec, index_t, rvec) const {
170  casadi_error("Not implemented");
171 }
172 
173 double AlpaqaProblem::eval_ψ(crvec x, crvec y, crvec Σ, rvec ŷ) const {
174  double res;
175  mem_->arg[0] = x.data();
176  mem_->arg[1] = mem_->d_nlp.p;
177  mem_->arg[2] = y.data();
178  mem_->arg[3] = Σ.data();
179  mem_->arg[4] = this->D.lowerbound.data();
180  mem_->arg[5] = this->D.upperbound.data();
181  mem_->res[0] = &res;
182  mem_->res[1] = ŷ.data();
183  try {
184  casadi_assert(solver_.calc_function(mem_, "nlp_psi")==0, "Failing evaluating eval_f_grad_f");
185  } catch(KeyboardInterruptException& ex) {
186  casadi_warning("KeyboardInterruptException");
188  } catch (std::exception& ex) {
189  if (solver_.show_eval_warnings_) {
190  casadi_warning("AlpaqaProblem::eval_psi failed:" + std::string(ex.what()));
191  }
192  }
193  return res;
194 }
195 
196 double AlpaqaProblem::eval_ψ_grad_ψ(crvec x, crvec y, crvec Σ, rvec grad_ψ, rvec,
197  rvec) const {
198  double res;
199  mem_->arg[0] = x.data();
200  mem_->arg[1] = mem_->d_nlp.p;
201  mem_->arg[2] = y.data();
202  mem_->arg[3] = Σ.data();
203  mem_->arg[4] = this->D.lowerbound.data();
204  mem_->arg[5] = this->D.upperbound.data();
205  mem_->res[0] = &res;
206  mem_->res[1] = grad_ψ.data();
207  try {
208  casadi_assert(solver_.calc_function(mem_, "nlp_grad_psi")==0, "Failing evaluating eval_f_grad_f");
209  } catch(KeyboardInterruptException& ex) {
210  casadi_warning("KeyboardInterruptException");
212  } catch (std::exception& ex) {
213  if (solver_.show_eval_warnings_) {
214  casadi_warning("AlpaqaProblem::eval_grad_psi failed:" + std::string(ex.what()));
215  }
216  }
217  return res;
218 }
219 
220 void AlpaqaProblem::eval_grad_ψ(crvec x, crvec y, crvec Σ, rvec grad_ψ,
221  rvec a, rvec b) const {
222  eval_ψ_grad_ψ(x, y, Σ, grad_ψ, a, b);
223 }
224 
225 void AlpaqaProblem::eval_grad_L(crvec x, crvec y, rvec grad_L,
226  rvec) const {
227  mem_->arg[0] = x.data();
228  mem_->arg[1] = mem_->d_nlp.p;
229  mem_->arg[2] = y.data();
230  mem_->res[0] = grad_L.data();
231  try {
232  casadi_assert(solver_.calc_function(mem_, "nlp_grad_L")==0, "Failing evaluating eval_f_grad_f");
233  } catch(KeyboardInterruptException& ex) {
234  casadi_warning("KeyboardInterruptException");
236  } catch (std::exception& ex) {
237  if (solver_.show_eval_warnings_) {
238  casadi_warning("AlpaqaProblem::eval_grad_L failed:" + std::string(ex.what()));
239  }
240  }
241 }
242 
243 void AlpaqaProblem::eval_hess_ψ(crvec x, crvec y, crvec Σ, real_t scale,
244  rindexvec inner_idx, rindexvec outer_ptr,
245  rvec H_values) const {
246  if (H_values.size()>0) {
247  mem_->arg[0] = x.data();
248  mem_->arg[1] = mem_->d_nlp.p;
249  mem_->arg[2] = y.data();
250  mem_->arg[3] = Σ.data();
251  mem_->arg[4] = &scale;
252  mem_->arg[5] = this->D.lowerbound.data();
253  mem_->arg[6] = this->D.upperbound.data();
254  mem_->res[0] = H_values.data();
255  try {
256  casadi_assert(solver_.calc_function(mem_, "nlp_hess_psi")==0, "Failing evaluating eval_f_grad_f");
257  } catch(KeyboardInterruptException& ex) {
258  casadi_warning("KeyboardInterruptException");
260  } catch (std::exception& ex) {
261  if (solver_.show_eval_warnings_) {
262  casadi_warning("AlpaqaProblem::eval_hess_psi failed:" + std::string(ex.what()));
263  }
264  }
265  } else {
266  const Sparsity& sp = solver_.get_function("nlp_hess_psi").sparsity_out(0);
267  if (!sp.is_dense()) {
268  copy(sp.row(), sp.nnz(), inner_idx.data());
269  copy(sp.colind(), get_n()+1, outer_ptr.data());
270  }
271  }
272 }
273 
274 void AlpaqaProblem::eval_hess_ψ_prod(crvec x, crvec y, crvec Σ,
275  real_t scale, crvec v,
276  rvec Hv) const {
277  mem_->arg[0] = x.data();
278  mem_->arg[1] = mem_->d_nlp.p;
279  mem_->arg[2] = y.data();
280  mem_->arg[3] = Σ.data();
281  mem_->arg[4] = &scale;
282  mem_->arg[5] = this->D.lowerbound.data();
283  mem_->arg[6] = this->D.upperbound.data();
284  mem_->arg[7] = v.data();
285  mem_->res[0] = Hv.data();
286  try {
287  casadi_assert(solver_.calc_function(mem_, "nlp_hess_psi_prod")==0, "Failing evaluating eval_f_grad_f");
288  } catch(KeyboardInterruptException& ex) {
289  casadi_warning("KeyboardInterruptException");
291  } catch (std::exception& ex) {
292  if (solver_.show_eval_warnings_) {
293  casadi_warning("AlpaqaProblem::eval_hess_psi_prod failed:" + std::string(ex.what()));
294  }
295  }
296 }
297 
298 alpaqa::DefaultConfig::length_t AlpaqaProblem::get_hess_L_num_nonzeros() const {
299  const Sparsity& sp = solver_.get_function("nlp_hess_L").sparsity_out(0);
300  return sp.is_dense() ? 0 : sp.nnz();
301 }
302 
303 alpaqa::DefaultConfig::length_t AlpaqaProblem::get_hess_ψ_num_nonzeros() const {
304  const Sparsity& sp = solver_.get_function("nlp_hess_psi").sparsity_out(0);
305  return sp.is_dense() ? 0 : sp.nnz();
306 }
307 
308 
309 alpaqa::DefaultConfig::length_t AlpaqaProblem::get_jac_g_num_nonzeros() const {
310  const Sparsity& sp = solver_.jacg_sp_;
311  return sp.is_dense() ? 0 : sp.nnz();
312 }
313 
314 } // namespace casadi
length_t get_hess_L_num_nonzeros() const
length_t get_hess_ψ_num_nonzeros() const
void eval_hess_L_prod(crvec x, crvec y, real_t scale, crvec v, rvec Hv) const
real_t eval_ψ(crvec x, crvec y, crvec Σ, rvec ŷ) const
void eval_hess_ψ_prod(crvec x, crvec y, crvec Σ, real_t scale, crvec v, rvec Hv) const
void eval_g(crvec x, rvec g) const
void eval_grad_ψ(crvec x, crvec y, crvec Σ, rvec grad_ψ, rvec work_n, rvec work_m) const
void eval_jac_g(crvec x, rindexvec inner_idx, rindexvec outer_ptr, rvec J_values) const
length_t get_jac_g_num_nonzeros() const
void eval_grad_g_prod(crvec x, crvec y, rvec grad_gxy) const
void eval_grad_gi(crvec x, index_t i, rvec grad_i) const
void eval_grad_L(crvec x, crvec y, rvec grad_L, rvec work_n) const
real_t eval_f_grad_f(crvec x, rvec grad_fx) const
void eval_hess_L(crvec x, crvec y, real_t scale, rindexvec inner_idx, rindexvec outer_ptr, rvec H_values) const
real_t eval_f(crvec x) const
AlpaqaProblem(const AlpaqaInterface &solver, AlpaqaMemory *mem)
void eval_grad_f(crvec x, rvec grad_fx) const
void eval_hess_ψ(crvec x, crvec y, crvec Σ, real_t scale, rindexvec inner_idx, rindexvec outer_ptr, rvec H_values) const
real_t eval_ψ_grad_ψ(crvec x, crvec y, crvec Σ, rvec grad_ψ, rvec work_n, rvec work_m) const
const char * what() const override
Display error.
Definition: exception.hpp:90
casadi_int ng_
Number of constraints.
Definition: nlpsol_impl.hpp:69
casadi_int nx_
Number of variables.
Definition: nlpsol_impl.hpp:66
int calc_function(OracleMemory *m, const std::string &fcn, const double *const *arg=nullptr, int thread_id=0) const
std::vector< std::string > get_function() const override
Get list of dependency functions.
bool show_eval_warnings_
Show evaluation warnings.
General sparsity class.
Definition: sparsity.hpp:106
casadi_int nnz() const
Get the number of (structural) non-zeros.
Definition: sparsity.cpp:148
const casadi_int * row() const
Get a reference to row-vector,.
Definition: sparsity.cpp:164
const casadi_int * colind() const
Get a reference to the colindex of all column element (see class description)
Definition: sparsity.cpp:168
bool is_dense() const
Is dense?
Definition: sparsity.cpp:273
The casadi namespace.
Definition: archiver.cpp:28
void copy(const T1 *x, casadi_int n, T2 *y)
casadi_nlpsol_data< double > d_nlp
Definition: nlpsol_impl.hpp:42