You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

565 lines
18 KiB

2 months ago
  1. /* lpx.h (old GLPK API) */
  2. /* Written by Andrew Makhorin <mao@gnu.org>, August 2013. */
  3. #ifndef LPX_H
  4. #define LPX_H
  5. #include <glpk.h>
  6. #ifdef __cplusplus
  7. extern "C" {
  8. #endif
  9. #define LPX glp_prob
  10. /* problem class: */
  11. #define LPX_LP 100 /* linear programming (LP) */
  12. #define LPX_MIP 101 /* mixed integer programming (MIP) */
  13. /* type of auxiliary/structural variable: */
  14. #define LPX_FR 110 /* free variable */
  15. #define LPX_LO 111 /* variable with lower bound */
  16. #define LPX_UP 112 /* variable with upper bound */
  17. #define LPX_DB 113 /* double-bounded variable */
  18. #define LPX_FX 114 /* fixed variable */
  19. /* optimization direction flag: */
  20. #define LPX_MIN 120 /* minimization */
  21. #define LPX_MAX 121 /* maximization */
  22. /* status of primal basic solution: */
  23. #define LPX_P_UNDEF 132 /* primal solution is undefined */
  24. #define LPX_P_FEAS 133 /* solution is primal feasible */
  25. #define LPX_P_INFEAS 134 /* solution is primal infeasible */
  26. #define LPX_P_NOFEAS 135 /* no primal feasible solution exists */
  27. /* status of dual basic solution: */
  28. #define LPX_D_UNDEF 136 /* dual solution is undefined */
  29. #define LPX_D_FEAS 137 /* solution is dual feasible */
  30. #define LPX_D_INFEAS 138 /* solution is dual infeasible */
  31. #define LPX_D_NOFEAS 139 /* no dual feasible solution exists */
  32. /* status of auxiliary/structural variable: */
  33. #define LPX_BS 140 /* basic variable */
  34. #define LPX_NL 141 /* non-basic variable on lower bound */
  35. #define LPX_NU 142 /* non-basic variable on upper bound */
  36. #define LPX_NF 143 /* non-basic free variable */
  37. #define LPX_NS 144 /* non-basic fixed variable */
  38. /* status of interior-point solution: */
  39. #define LPX_T_UNDEF 150 /* interior solution is undefined */
  40. #define LPX_T_OPT 151 /* interior solution is optimal */
  41. /* kind of structural variable: */
  42. #define LPX_CV 160 /* continuous variable */
  43. #define LPX_IV 161 /* integer variable */
  44. /* status of integer solution: */
  45. #define LPX_I_UNDEF 170 /* integer solution is undefined */
  46. #define LPX_I_OPT 171 /* integer solution is optimal */
  47. #define LPX_I_FEAS 172 /* integer solution is feasible */
  48. #define LPX_I_NOFEAS 173 /* no integer solution exists */
  49. /* status codes reported by the routine lpx_get_status: */
  50. #define LPX_OPT 180 /* optimal */
  51. #define LPX_FEAS 181 /* feasible */
  52. #define LPX_INFEAS 182 /* infeasible */
  53. #define LPX_NOFEAS 183 /* no feasible */
  54. #define LPX_UNBND 184 /* unbounded */
  55. #define LPX_UNDEF 185 /* undefined */
  56. /* exit codes returned by solver routines: */
  57. #define LPX_E_OK 200 /* success */
  58. #define LPX_E_EMPTY 201 /* empty problem */
  59. #define LPX_E_BADB 202 /* invalid initial basis */
  60. #define LPX_E_INFEAS 203 /* infeasible initial solution */
  61. #define LPX_E_FAULT 204 /* unable to start the search */
  62. #define LPX_E_OBJLL 205 /* objective lower limit reached */
  63. #define LPX_E_OBJUL 206 /* objective upper limit reached */
  64. #define LPX_E_ITLIM 207 /* iterations limit exhausted */
  65. #define LPX_E_TMLIM 208 /* time limit exhausted */
  66. #define LPX_E_NOFEAS 209 /* no feasible solution */
  67. #define LPX_E_INSTAB 210 /* numerical instability */
  68. #define LPX_E_SING 211 /* problems with basis matrix */
  69. #define LPX_E_NOCONV 212 /* no convergence (interior) */
  70. #define LPX_E_NOPFS 213 /* no primal feas. sol. (LP presolver) */
  71. #define LPX_E_NODFS 214 /* no dual feas. sol. (LP presolver) */
  72. #define LPX_E_MIPGAP 215 /* relative mip gap tolerance reached */
  73. /* control parameter identifiers: */
  74. #define LPX_K_MSGLEV 300 /* lp->msg_lev */
  75. #define LPX_K_SCALE 301 /* lp->scale */
  76. #define LPX_K_DUAL 302 /* lp->dual */
  77. #define LPX_K_PRICE 303 /* lp->price */
  78. #define LPX_K_RELAX 304 /* lp->relax */
  79. #define LPX_K_TOLBND 305 /* lp->tol_bnd */
  80. #define LPX_K_TOLDJ 306 /* lp->tol_dj */
  81. #define LPX_K_TOLPIV 307 /* lp->tol_piv */
  82. #define LPX_K_ROUND 308 /* lp->round */
  83. #define LPX_K_OBJLL 309 /* lp->obj_ll */
  84. #define LPX_K_OBJUL 310 /* lp->obj_ul */
  85. #define LPX_K_ITLIM 311 /* lp->it_lim */
  86. #define LPX_K_ITCNT 312 /* lp->it_cnt */
  87. #define LPX_K_TMLIM 313 /* lp->tm_lim */
  88. #define LPX_K_OUTFRQ 314 /* lp->out_frq */
  89. #define LPX_K_OUTDLY 315 /* lp->out_dly */
  90. #define LPX_K_BRANCH 316 /* lp->branch */
  91. #define LPX_K_BTRACK 317 /* lp->btrack */
  92. #define LPX_K_TOLINT 318 /* lp->tol_int */
  93. #define LPX_K_TOLOBJ 319 /* lp->tol_obj */
  94. #define LPX_K_MPSINFO 320 /* lp->mps_info */
  95. #define LPX_K_MPSOBJ 321 /* lp->mps_obj */
  96. #define LPX_K_MPSORIG 322 /* lp->mps_orig */
  97. #define LPX_K_MPSWIDE 323 /* lp->mps_wide */
  98. #define LPX_K_MPSFREE 324 /* lp->mps_free */
  99. #define LPX_K_MPSSKIP 325 /* lp->mps_skip */
  100. #define LPX_K_LPTORIG 326 /* lp->lpt_orig */
  101. #define LPX_K_PRESOL 327 /* lp->presol */
  102. #define LPX_K_BINARIZE 328 /* lp->binarize */
  103. #define LPX_K_USECUTS 329 /* lp->use_cuts */
  104. #define LPX_K_BFTYPE 330 /* lp->bfcp->type */
  105. #define LPX_K_MIPGAP 331 /* lp->mip_gap */
  106. #define LPX_C_COVER 0x01 /* mixed cover cuts */
  107. #define LPX_C_CLIQUE 0x02 /* clique cuts */
  108. #define LPX_C_GOMORY 0x04 /* Gomory's mixed integer cuts */
  109. #define LPX_C_MIR 0x08 /* mixed integer rounding cuts */
  110. #define LPX_C_ALL 0xFF /* all cuts */
  111. typedef struct
  112. { /* this structure contains results reported by the routines which
  113. checks Karush-Kuhn-Tucker conditions (for details see comments
  114. to those routines) */
  115. /*--------------------------------------------------------------*/
  116. /* xR - A * xS = 0 (KKT.PE) */
  117. double pe_ae_max;
  118. /* largest absolute error */
  119. int pe_ae_row;
  120. /* number of row with largest absolute error */
  121. double pe_re_max;
  122. /* largest relative error */
  123. int pe_re_row;
  124. /* number of row with largest relative error */
  125. int pe_quality;
  126. /* quality of primal solution:
  127. 'H' - high
  128. 'M' - medium
  129. 'L' - low
  130. '?' - primal solution is wrong */
  131. /*--------------------------------------------------------------*/
  132. /* l[k] <= x[k] <= u[k] (KKT.PB) */
  133. double pb_ae_max;
  134. /* largest absolute error */
  135. int pb_ae_ind;
  136. /* number of variable with largest absolute error */
  137. double pb_re_max;
  138. /* largest relative error */
  139. int pb_re_ind;
  140. /* number of variable with largest relative error */
  141. int pb_quality;
  142. /* quality of primal feasibility:
  143. 'H' - high
  144. 'M' - medium
  145. 'L' - low
  146. '?' - primal solution is infeasible */
  147. /*--------------------------------------------------------------*/
  148. /* A' * (dR - cR) + (dS - cS) = 0 (KKT.DE) */
  149. double de_ae_max;
  150. /* largest absolute error */
  151. int de_ae_col;
  152. /* number of column with largest absolute error */
  153. double de_re_max;
  154. /* largest relative error */
  155. int de_re_col;
  156. /* number of column with largest relative error */
  157. int de_quality;
  158. /* quality of dual solution:
  159. 'H' - high
  160. 'M' - medium
  161. 'L' - low
  162. '?' - dual solution is wrong */
  163. /*--------------------------------------------------------------*/
  164. /* d[k] >= 0 or d[k] <= 0 (KKT.DB) */
  165. double db_ae_max;
  166. /* largest absolute error */
  167. int db_ae_ind;
  168. /* number of variable with largest absolute error */
  169. double db_re_max;
  170. /* largest relative error */
  171. int db_re_ind;
  172. /* number of variable with largest relative error */
  173. int db_quality;
  174. /* quality of dual feasibility:
  175. 'H' - high
  176. 'M' - medium
  177. 'L' - low
  178. '?' - dual solution is infeasible */
  179. /*--------------------------------------------------------------*/
  180. /* (x[k] - bound of x[k]) * d[k] = 0 (KKT.CS) */
  181. double cs_ae_max;
  182. /* largest absolute error */
  183. int cs_ae_ind;
  184. /* number of variable with largest absolute error */
  185. double cs_re_max;
  186. /* largest relative error */
  187. int cs_re_ind;
  188. /* number of variable with largest relative error */
  189. int cs_quality;
  190. /* quality of complementary slackness:
  191. 'H' - high
  192. 'M' - medium
  193. 'L' - low
  194. '?' - primal and dual solutions are not complementary */
  195. } LPXKKT;
  196. LPX *lpx_create_prob(void);
  197. /* create problem object */
  198. void lpx_set_prob_name(LPX *lp, const char *name);
  199. /* assign (change) problem name */
  200. void lpx_set_obj_name(LPX *lp, const char *name);
  201. /* assign (change) objective function name */
  202. void lpx_set_obj_dir(LPX *lp, int dir);
  203. /* set (change) optimization direction flag */
  204. int lpx_add_rows(LPX *lp, int nrs);
  205. /* add new rows to problem object */
  206. int lpx_add_cols(LPX *lp, int ncs);
  207. /* add new columns to problem object */
  208. void lpx_set_row_name(LPX *lp, int i, const char *name);
  209. /* assign (change) row name */
  210. void lpx_set_col_name(LPX *lp, int j, const char *name);
  211. /* assign (change) column name */
  212. void lpx_set_row_bnds(LPX *lp, int i, int type, double lb, double ub);
  213. /* set (change) row bounds */
  214. void lpx_set_col_bnds(LPX *lp, int j, int type, double lb, double ub);
  215. /* set (change) column bounds */
  216. void lpx_set_obj_coef(glp_prob *lp, int j, double coef);
  217. /* set (change) obj. coefficient or constant term */
  218. void lpx_set_mat_row(LPX *lp, int i, int len, const int ind[],
  219. const double val[]);
  220. /* set (replace) row of the constraint matrix */
  221. void lpx_set_mat_col(LPX *lp, int j, int len, const int ind[],
  222. const double val[]);
  223. /* set (replace) column of the constraint matrix */
  224. void lpx_load_matrix(LPX *lp, int ne, const int ia[], const int ja[],
  225. const double ar[]);
  226. /* load (replace) the whole constraint matrix */
  227. void lpx_del_rows(LPX *lp, int nrs, const int num[]);
  228. /* delete specified rows from problem object */
  229. void lpx_del_cols(LPX *lp, int ncs, const int num[]);
  230. /* delete specified columns from problem object */
  231. void lpx_delete_prob(LPX *lp);
  232. /* delete problem object */
  233. const char *lpx_get_prob_name(LPX *lp);
  234. /* retrieve problem name */
  235. const char *lpx_get_obj_name(LPX *lp);
  236. /* retrieve objective function name */
  237. int lpx_get_obj_dir(LPX *lp);
  238. /* retrieve optimization direction flag */
  239. int lpx_get_num_rows(LPX *lp);
  240. /* retrieve number of rows */
  241. int lpx_get_num_cols(LPX *lp);
  242. /* retrieve number of columns */
  243. const char *lpx_get_row_name(LPX *lp, int i);
  244. /* retrieve row name */
  245. const char *lpx_get_col_name(LPX *lp, int j);
  246. /* retrieve column name */
  247. int lpx_get_row_type(LPX *lp, int i);
  248. /* retrieve row type */
  249. double lpx_get_row_lb(LPX *lp, int i);
  250. /* retrieve row lower bound */
  251. double lpx_get_row_ub(LPX *lp, int i);
  252. /* retrieve row upper bound */
  253. void lpx_get_row_bnds(LPX *lp, int i, int *typx, double *lb,
  254. double *ub);
  255. /* retrieve row bounds */
  256. int lpx_get_col_type(LPX *lp, int j);
  257. /* retrieve column type */
  258. double lpx_get_col_lb(LPX *lp, int j);
  259. /* retrieve column lower bound */
  260. double lpx_get_col_ub(LPX *lp, int j);
  261. /* retrieve column upper bound */
  262. void lpx_get_col_bnds(LPX *lp, int j, int *typx, double *lb,
  263. double *ub);
  264. /* retrieve column bounds */
  265. double lpx_get_obj_coef(LPX *lp, int j);
  266. /* retrieve obj. coefficient or constant term */
  267. int lpx_get_num_nz(LPX *lp);
  268. /* retrieve number of constraint coefficients */
  269. int lpx_get_mat_row(LPX *lp, int i, int ind[], double val[]);
  270. /* retrieve row of the constraint matrix */
  271. int lpx_get_mat_col(LPX *lp, int j, int ind[], double val[]);
  272. /* retrieve column of the constraint matrix */
  273. void lpx_create_index(LPX *lp);
  274. /* create the name index */
  275. int lpx_find_row(LPX *lp, const char *name);
  276. /* find row by its name */
  277. int lpx_find_col(LPX *lp, const char *name);
  278. /* find column by its name */
  279. void lpx_delete_index(LPX *lp);
  280. /* delete the name index */
  281. void lpx_scale_prob(LPX *lp);
  282. /* scale problem data */
  283. void lpx_unscale_prob(LPX *lp);
  284. /* unscale problem data */
  285. void lpx_set_row_stat(LPX *lp, int i, int stat);
  286. /* set (change) row status */
  287. void lpx_set_col_stat(LPX *lp, int j, int stat);
  288. /* set (change) column status */
  289. void lpx_std_basis(LPX *lp);
  290. /* construct standard initial LP basis */
  291. void lpx_adv_basis(LPX *lp);
  292. /* construct advanced initial LP basis */
  293. void lpx_cpx_basis(LPX *lp);
  294. /* construct Bixby's initial LP basis */
  295. int lpx_simplex(LPX *lp);
  296. /* easy-to-use driver to the simplex method */
  297. int lpx_exact(LPX *lp);
  298. /* easy-to-use driver to the exact simplex method */
  299. int lpx_get_status(LPX *lp);
  300. /* retrieve generic status of basic solution */
  301. int lpx_get_prim_stat(LPX *lp);
  302. /* retrieve primal status of basic solution */
  303. int lpx_get_dual_stat(LPX *lp);
  304. /* retrieve dual status of basic solution */
  305. double lpx_get_obj_val(LPX *lp);
  306. /* retrieve objective value (basic solution) */
  307. int lpx_get_row_stat(LPX *lp, int i);
  308. /* retrieve row status (basic solution) */
  309. double lpx_get_row_prim(LPX *lp, int i);
  310. /* retrieve row primal value (basic solution) */
  311. double lpx_get_row_dual(LPX *lp, int i);
  312. /* retrieve row dual value (basic solution) */
  313. void lpx_get_row_info(LPX *lp, int i, int *tagx, double *vx,
  314. double *dx);
  315. /* obtain row solution information */
  316. int lpx_get_col_stat(LPX *lp, int j);
  317. /* retrieve column status (basic solution) */
  318. double lpx_get_col_prim(LPX *lp, int j);
  319. /* retrieve column primal value (basic solution) */
  320. double lpx_get_col_dual(glp_prob *lp, int j);
  321. /* retrieve column dual value (basic solution) */
  322. void lpx_get_col_info(LPX *lp, int j, int *tagx, double *vx,
  323. double *dx);
  324. /* obtain column solution information (obsolete) */
  325. int lpx_get_ray_info(LPX *lp);
  326. /* determine what causes primal unboundness */
  327. void lpx_check_kkt(LPX *lp, int scaled, LPXKKT *kkt);
  328. /* check Karush-Kuhn-Tucker conditions */
  329. int lpx_warm_up(LPX *lp);
  330. /* "warm up" LP basis */
  331. int lpx_eval_tab_row(LPX *lp, int k, int ind[], double val[]);
  332. /* compute row of the simplex table */
  333. int lpx_eval_tab_col(LPX *lp, int k, int ind[], double val[]);
  334. /* compute column of the simplex table */
  335. int lpx_transform_row(LPX *lp, int len, int ind[], double val[]);
  336. /* transform explicitly specified row */
  337. int lpx_transform_col(LPX *lp, int len, int ind[], double val[]);
  338. /* transform explicitly specified column */
  339. int lpx_prim_ratio_test(LPX *lp, int len, const int ind[],
  340. const double val[], int how, double tol);
  341. /* perform primal ratio test */
  342. int lpx_dual_ratio_test(LPX *lp, int len, const int ind[],
  343. const double val[], int how, double tol);
  344. /* perform dual ratio test */
  345. int lpx_interior(LPX *lp);
  346. /* easy-to-use driver to the interior point method */
  347. int lpx_ipt_status(LPX *lp);
  348. /* retrieve status of interior-point solution */
  349. double lpx_ipt_obj_val(LPX *lp);
  350. /* retrieve objective value (interior point) */
  351. double lpx_ipt_row_prim(LPX *lp, int i);
  352. /* retrieve row primal value (interior point) */
  353. double lpx_ipt_row_dual(LPX *lp, int i);
  354. /* retrieve row dual value (interior point) */
  355. double lpx_ipt_col_prim(LPX *lp, int j);
  356. /* retrieve column primal value (interior point) */
  357. double lpx_ipt_col_dual(LPX *lp, int j);
  358. /* retrieve column dual value (interior point) */
  359. void lpx_set_class(LPX *lp, int klass);
  360. /* set problem class */
  361. int lpx_get_class(LPX *lp);
  362. /* determine problem klass */
  363. void lpx_set_col_kind(LPX *lp, int j, int kind);
  364. /* set (change) column kind */
  365. int lpx_get_col_kind(LPX *lp, int j);
  366. /* retrieve column kind */
  367. int lpx_get_num_int(LPX *lp);
  368. /* retrieve number of integer columns */
  369. int lpx_get_num_bin(LPX *lp);
  370. /* retrieve number of binary columns */
  371. int lpx_integer(LPX *lp);
  372. /* easy-to-use driver to the branch-and-bound method */
  373. int lpx_intopt(LPX *lp);
  374. /* easy-to-use driver to the branch-and-bound method */
  375. int lpx_mip_status(LPX *lp);
  376. /* retrieve status of MIP solution */
  377. double lpx_mip_obj_val(LPX *lp);
  378. /* retrieve objective value (MIP solution) */
  379. double lpx_mip_row_val(LPX *lp, int i);
  380. /* retrieve row value (MIP solution) */
  381. double lpx_mip_col_val(LPX *lp, int j);
  382. /* retrieve column value (MIP solution) */
  383. void lpx_check_int(LPX *lp, LPXKKT *kkt);
  384. /* check integer feasibility conditions */
  385. void lpx_reset_parms(LPX *lp);
  386. /* reset control parameters to default values */
  387. void lpx_set_int_parm(LPX *lp, int parm, int val);
  388. /* set (change) integer control parameter */
  389. int lpx_get_int_parm(LPX *lp, int parm);
  390. /* query integer control parameter */
  391. void lpx_set_real_parm(LPX *lp, int parm, double val);
  392. /* set (change) real control parameter */
  393. double lpx_get_real_parm(LPX *lp, int parm);
  394. /* query real control parameter */
  395. LPX *lpx_read_mps(const char *fname);
  396. /* read problem data in fixed MPS format */
  397. int lpx_write_mps(LPX *lp, const char *fname);
  398. /* write problem data in fixed MPS format */
  399. int lpx_read_bas(LPX *lp, const char *fname);
  400. /* read LP basis in fixed MPS format */
  401. int lpx_write_bas(LPX *lp, const char *fname);
  402. /* write LP basis in fixed MPS format */
  403. LPX *lpx_read_freemps(const char *fname);
  404. /* read problem data in free MPS format */
  405. int lpx_write_freemps(LPX *lp, const char *fname);
  406. /* write problem data in free MPS format */
  407. LPX *lpx_read_cpxlp(const char *fname);
  408. /* read problem data in CPLEX LP format */
  409. int lpx_write_cpxlp(LPX *lp, const char *fname);
  410. /* write problem data in CPLEX LP format */
  411. LPX *lpx_read_model(const char *model, const char *data,
  412. const char *output);
  413. /* read LP/MIP model written in GNU MathProg language */
  414. int lpx_print_prob(LPX *lp, const char *fname);
  415. /* write problem data in plain text format */
  416. int lpx_print_sol(LPX *lp, const char *fname);
  417. /* write LP problem solution in printable format */
  418. int lpx_print_sens_bnds(LPX *lp, const char *fname);
  419. /* write bounds sensitivity information */
  420. int lpx_print_ips(LPX *lp, const char *fname);
  421. /* write interior point solution in printable format */
  422. int lpx_print_mip(LPX *lp, const char *fname);
  423. /* write MIP problem solution in printable format */
  424. int lpx_is_b_avail(LPX *lp);
  425. /* check if LP basis is available */
  426. int lpx_main(int argc, const char *argv[]);
  427. /* stand-alone LP/MIP solver */
  428. #ifdef __cplusplus
  429. }
  430. #endif
  431. #endif
  432. /* eof */