highs_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 "casadi_qp_prob<T1>" "struct casadi_qp_prob"
21 // C-REPLACE "casadi_qp_data<T1>" "struct casadi_qp_data"
22 
23 // C-REPLACE "reinterpret_cast<int**>" "(int**) "
24 // C-REPLACE "reinterpret_cast<int*>" "(int*) "
25 // C-REPLACE "const_cast<int*>" "(int*) "
26 
27 template<typename T1>
30 
31  const int *colinda, *rowa;
32  const int *colindh, *rowh;
33  const int *integrality;
34 };
35 // C-REPLACE "casadi_highs_prob<T1>" "struct casadi_highs_prob"
36 
37 // SYMBOL "highs_setup"
38 template<typename T1>
39 void casadi_highs_setup(casadi_highs_prob<T1>* p) {
40 
41 }
42 
43 
44 
45 // SYMBOL "highs_data"
46 template<typename T1>
48  // Problem structure
50  // Problem structure
52 
54 
63  T1 mip_gap;
70 
71  void* highs;
72 };
73 // C-REPLACE "casadi_highs_data<T1>" "struct casadi_highs_data"
74 
75 // SYMBOL "highs_init_mem"
76 template<typename T1>
77 int highs_init_mem(casadi_highs_data<T1>* d) {
78  d->highs = Highs_create();
79  return 0;
80 }
81 
82 // SYMBOL "highs_free_mem"
83 template<typename T1>
84 void highs_free_mem(casadi_highs_data<T1>* d) {
85  Highs_destroy(d->highs);
86 }
87 
88 // SYMBOL "highs_work"
89 template<typename T1>
90 void casadi_highs_work(const casadi_highs_prob<T1>* p, casadi_int* sz_arg, casadi_int* sz_res, casadi_int* sz_iw, casadi_int* sz_w) {
91  casadi_qp_work(p->qp, sz_arg, sz_res, sz_iw, sz_w);
92 }
93 
94 // SYMBOL "highs_init"
95 template<typename T1>
96 void casadi_highs_init(casadi_highs_data<T1>* d, const T1*** arg, T1*** res, casadi_int** iw, T1** w) {
97 
98 
99 }
100 
101 
102 // C-REPLACE "SOLVER_RET_SUCCESS" "0"
103 // C-REPLACE "SOLVER_RET_LIMITED" "2"
104 
105 // SYMBOL "highs_solve"
106 template<typename T1>
107 int casadi_highs_solve(casadi_highs_data<T1>* d, const double** arg, double** res, casadi_int* iw, double* w) {
108 
109  const casadi_highs_prob<T1>* p = d->prob;
110  const casadi_qp_prob<T1>* p_qp = p->qp;
111  casadi_qp_data<T1>* d_qp = d->qp;
112 
113 
114  // Create HiGHS instance and pass problem
115  int status;
116  const int matrix_format = 1;
117  const int sense = 1;
118  const double offset = 0.0;
119 
120  status = Highs_passModel(d->highs, p_qp->nx, p_qp->na, p_qp->nnz_a, p_qp->nnz_h,
121  matrix_format, matrix_format, sense, offset,
122  d_qp->g, d_qp->lbx, d_qp->ubx, d_qp->lba, d_qp->uba,
123  p->colinda, p->rowa, d_qp->a,
124  p->colindh, p->rowh, d_qp->h,
125  p->integrality);
126 
127  if (!(status==kHighsStatusOk || status==kHighsStatusWarning)) return 1;
128 
129  // solve incumbent model
130  status = Highs_run(d->highs);
131 
132  if (!(status==kHighsStatusOk || status==kHighsStatusWarning)) return 1;
133 
134  // get primal and dual solution
135  Highs_getSolution(d->highs, d_qp->x, d_qp->lam_x, 0, d_qp->lam_a);
136 
137  if (d_qp->lam_x) {
138  casadi_scal(p_qp->nx, -1., d_qp->lam_x);
139  }
140  if (d_qp->lam_a) {
141  casadi_scal(p_qp->na, -1., d_qp->lam_a);
142  }
143 
144  if (d_qp->f) {
145  *d_qp->f = Highs_getObjectiveValue(d->highs);
146  }
147 
148  status = Highs_getModelStatus(d->highs);
149  d->return_status = status;
150  d_qp->success = status==kHighsModelStatusOptimal;
151 
152  if (status==kHighsModelStatusOptimal)
154 
155  if (status==kHighsModelStatusTimeLimit
156  || status==kHighsModelStatusIterationLimit)
158 
159  if (Highs_getIntInfoValue(d->highs, "simplex_iteration_count", &d->simplex_iteration_count)!=kHighsStatusOk) return 1;
160  if (Highs_getIntInfoValue(d->highs, "ipm_iteration_count", &d->ipm_iteration_count)!=kHighsStatusOk) return 1;
161  if (Highs_getIntInfoValue(d->highs, "qp_iteration_count", &d->qp_iteration_count)!=kHighsStatusOk) return 1;
162  if (Highs_getIntInfoValue(d->highs, "crossover_iteration_count", &d->crossover_iteration_count)!=kHighsStatusOk) return 1;
163  if (Highs_getIntInfoValue(d->highs, "primal_solution_status", &d->primal_solution_status)!=kHighsStatusOk) return 1;
164  if (Highs_getIntInfoValue(d->highs, "dual_solution_status", &d->dual_solution_status)!=kHighsStatusOk) return 1;
165  if (Highs_getIntInfoValue(d->highs, "basis_validity", &d->basis_validity)!=kHighsStatusOk) return 1;
166  if (Highs_getDoubleInfoValue(d->highs, "mip_dual_bound", &d->mip_dual_bound)!=kHighsStatusOk) return 1;
167  if (Highs_getDoubleInfoValue(d->highs, "mip_gap", &d->mip_gap)!=kHighsStatusOk) return 1;
168  if (Highs_getIntInfoValue(d->highs, "num_primal_infeasibilities", &d->num_primal_infeasibilities)!=kHighsStatusOk) return 1;
169  if (Highs_getDoubleInfoValue(d->highs, "max_primal_infeasibility", &d->max_primal_infeasibility)!=kHighsStatusOk) return 1;
170  if (Highs_getDoubleInfoValue(d->highs, "sum_primal_infeasibilities", &d->sum_primal_infeasibilities)!=kHighsStatusOk) return 1;
171  if (Highs_getIntInfoValue(d->highs, "num_dual_infeasibilities", &d->num_dual_infeasibilities)!=kHighsStatusOk) return 1;
172  if (Highs_getDoubleInfoValue(d->highs, "max_dual_infeasibility", &d->max_dual_infeasibility)!=kHighsStatusOk) return 1;
173  if (Highs_getDoubleInfoValue(d->highs, "sum_dual_infeasibilities", &d->sum_dual_infeasibilities)!=kHighsStatusOk) return 1;
174 
175 
176  return 0;
177 }
@ SOLVER_RET_LIMITED
@ SOLVER_RET_SUCCESS
casadi_qp_data< T1 > * qp
const casadi_highs_prob< T1 > * prob
const int * colindh
const int * colinda
const casadi_qp_prob< T1 > * qp
const int * integrality
const T1 * h
Definition: casadi_qp.hpp:64
const T1 * lba
Definition: casadi_qp.hpp:64
const T1 * lbx
Definition: casadi_qp.hpp:64
const T1 * uba
Definition: casadi_qp.hpp:64
const T1 * ubx
Definition: casadi_qp.hpp:64
UnifiedReturnStatus unified_return_status
Definition: casadi_qp.hpp:57
const T1 * g
Definition: casadi_qp.hpp:64
const T1 * a
Definition: casadi_qp.hpp:64
casadi_int nx
Definition: casadi_qp.hpp:33
casadi_int na
Definition: casadi_qp.hpp:33
casadi_int nnz_a
Definition: casadi_qp.hpp:34
casadi_int nnz_h
Definition: casadi_qp.hpp:34