ipopt_runtime.hpp
1 //
2 // MIT No Attribution
3 //
4 // Copyright (C) 2010-2023 Joel Andersson, Joris Gillis, Moritz Diehl, KU Leuven.
5 //
6 // Permission is hereby granted, free of charge, to any person obtaining a copy of this
7 // software and associated documentation files (the "Software"), to deal in the Software
8 // without restriction, including without limitation the rights to use, copy, modify,
9 // merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
10 // permit persons to whom the Software is furnished to do so.
11 //
12 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
13 // INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
14 // PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
15 // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
16 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
17 // SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
18 //
19 
20 // C-REPLACE "SOLVER_RET_SUCCESS" "0"
21 // C-REPLACE "SOLVER_RET_UNKNOWN" "1"
22 // C-REPLACE "SOLVER_RET_LIMITED" "2"
23 
24 // C-REPLACE "casadi_nlpsol_prob<T1>" "struct casadi_nlpsol_prob"
25 // C-REPLACE "casadi_nlpsol_data<T1>" "struct casadi_nlpsol_data"
26 
27 // C-REPLACE "reinterpret_cast<int**>" "(int**) "
28 // C-REPLACE "reinterpret_cast<int*>" "(int*) "
29 // C-REPLACE "const_cast<int*>" "(int*) "
30 
31 template<typename T1>
34  // Sparsity patterns
35  const casadi_int *sp_h, *sp_a;
36 
37  casadi_int nnz_h, nnz_a;
38 
39  Eval_F_CB eval_f;
40  Eval_G_CB eval_g;
41  Eval_Grad_F_CB eval_grad_f;
42  Eval_Jac_G_CB eval_jac_g;
43  Eval_H_CB eval_h;
44 };
45 // C-REPLACE "casadi_ipopt_prob<T1>" "struct casadi_ipopt_prob"
46 
47 // SYMBOL "ipopt_setup"
48 template<typename T1>
49 void casadi_ipopt_setup(casadi_ipopt_prob<T1>* p) {
50  if (p->sp_h) {
51  p->nnz_h = p->sp_h[2+p->sp_h[1]];
52  } else {
53  p->nnz_h = 0;
54  }
55  p->nnz_a = p->sp_a[2+p->sp_a[1]];
56 }
57 
58 
59 
60 // SYMBOL "ipopt_data"
61 template<typename T1>
63  // Problem structure
65  // Problem structure
67 
68  IpoptProblem ipopt;
69 
70  const casadi_real** arg;
71  casadi_real** res;
72  casadi_int* iw;
73  casadi_real* w;
74 
75  casadi_real *z_L, *z_U;
76 
77  enum ApplicationReturnStatus status;
78 
80  int success;
81 };
82 // C-REPLACE "casadi_ipopt_data<T1>" "struct casadi_ipopt_data"
83 
84 // SYMBOL "ipopt_init_mem"
85 template<typename T1>
86 int ipopt_init_mem(casadi_ipopt_data<T1>* d) {
87  return 0;
88 }
89 
90 // SYMBOL "ipopt_free_mem"
91 template<typename T1>
92 void ipopt_free_mem(casadi_ipopt_data<T1>* d) {
93  //Highs_destroy(d->ipopt);
94 
95 }
96 
97 // SYMBOL "ipopt_work"
98 template<typename T1>
99 void casadi_ipopt_work(const casadi_ipopt_prob<T1>* p, casadi_int* sz_arg, casadi_int* sz_res, casadi_int* sz_iw, casadi_int* sz_w) {
100  casadi_nlpsol_work(p->nlp, sz_arg, sz_res, sz_iw, sz_w);
101 
102  *sz_w += p->nlp->nx; // z_L
103  *sz_w += p->nlp->nx; // z_U
104 }
105 
106 // SYMBOL "ipopt_init"
107 template<typename T1>
108 void casadi_ipopt_init(casadi_ipopt_data<T1>* d, const T1*** arg, T1*** res, casadi_int** iw, T1** w) {
109  // Problem structure
110  const casadi_ipopt_prob<T1>* p = d->prob;
111  const casadi_nlpsol_prob<T1>* p_nlp = p->nlp;
112 
113  d->z_L = *w; *w += p_nlp->nx;
114  d->z_U = *w; *w += p_nlp->nx;
115 
116  d->arg = *arg;
117  d->res = *res;
118  d->iw = *iw;
119  d->w = *w;
120 }
121 
122 // SYMBOL "ipopt_presolve"
123 template<typename T1>
124 void casadi_ipopt_presolve(casadi_ipopt_data<T1>* d) {
125  // Problem structure
126  const casadi_ipopt_prob<T1>* p = d->prob;
127  const casadi_nlpsol_prob<T1>* p_nlp = p->nlp;
128  const casadi_nlpsol_data<T1>* d_nlp = d->nlp;
129 
130  d->ipopt = CreateIpoptProblem(
131  p_nlp->nx, (double *) d_nlp->lbz, (double *) d_nlp->ubz,
132  p_nlp->ng, (double *) d_nlp->lbz+p_nlp->nx,
133  (double *) d_nlp->ubz+p_nlp->nx,
134  p->nnz_a, p->nnz_h, 0,
135  p->eval_f, p->eval_g, p->eval_grad_f,
136  p->eval_jac_g, p->eval_h);
137 }
138 
139 
140 // SYMBOL "ipopt_solve"
141 template<typename T1>
142 void casadi_ipopt_solve(casadi_ipopt_data<T1>* d) {
143  // Problem structure
144  const casadi_ipopt_prob<T1>* p = d->prob;
145  const casadi_nlpsol_prob<T1>* p_nlp = p->nlp;
146  casadi_nlpsol_data<T1>* d_nlp = d->nlp;
147 
149 
150  // Initialize dual solution (simple bounds)
151  for (casadi_int i=0; i<p_nlp->nx; ++i) {
152  d->z_L[i] = casadi_fmax(0., -d_nlp->lam[i]);
153  d->z_U[i] = casadi_fmax(0., d_nlp->lam[i]);
154  }
155  d->status = IpoptSolve(d->ipopt, d_nlp->z, d_nlp->z + p_nlp->nx, &d_nlp->objective, d_nlp->lam+p_nlp->nx, d->z_L, d->z_U, d);
156 
157  // Get dual solution (simple bounds)
158  for (casadi_int i=0; i<p_nlp->nx; ++i) {
159  d_nlp->lam[i] = d->z_U[i]-d->z_L[i];
160  }
161 
162  FreeIpoptProblem(d->ipopt);
163 
164  if (d->status==Solve_Succeeded ||
165  d->status==Solved_To_Acceptable_Level ||
166  d->status==Feasible_Point_Found) {
168  } else if (d->status==Maximum_Iterations_Exceeded) {
170  }
171 
172  #if (IPOPT_VERSION_MAJOR > 3) || (IPOPT_VERSION_MAJOR == 3 && IPOPT_VERSION_MINOR >= 14)
173  if (d->status==Maximum_WallTime_Exceeded) d->unified_return_status = SOLVER_RET_LIMITED;
174  #endif
175 
177 }
178 
179 // SYMBOL "ipopt_sparsity"
180 template<typename T1>
181 void casadi_ipopt_sparsity(const casadi_int* sp, ipindex *iRow, ipindex *jCol) {
182  casadi_int ncol = sp[1];
183  const casadi_int* colind = sp+2;
184  const casadi_int* row = colind+ncol+1;
185 
186  for (casadi_int cc=0; cc<ncol; ++cc) {
187  for (casadi_int el=colind[cc]; el<colind[cc+1]; ++el) {
188  *iRow++ = row[el];
189  *jCol++ = cc;
190  }
191  }
192 }
193 
194 // SYMBOL "ipopt_hess_l_empty"
195 template<typename T1>
196 bool casadi_ipopt_hess_l_empty(ipindex n, ipnumber *x, bool new_x, ipnumber obj_factor, ipindex m, ipnumber *lambda, bool new_lambda, ipindex nele_hess, ipindex *iRow, ipindex *jCol, ipnumber *values, UserDataPtr user_data) {
197  return false;
198 }
@ SOLVER_RET_LIMITED
@ SOLVER_RET_SUCCESS
@ SOLVER_RET_UNKNOWN
const casadi_ipopt_prob< T1 > * prob
IpoptProblem ipopt
casadi_real ** res
casadi_real * z_L
casadi_real * w
const casadi_real ** arg
casadi_nlpsol_data< T1 > * nlp
enum ApplicationReturnStatus status
casadi_real * z_U
const casadi_int * sp_a
const casadi_int * sp_h
Eval_Jac_G_CB eval_jac_g
const casadi_nlpsol_prob< T1 > * nlp
Eval_Grad_F_CB eval_grad_f