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.

1505 lines
47 KiB

3 months ago
  1. /* lpx.c (old GLPK API) */
  2. /* Written by Andrew Makhorin <mao@gnu.org>, August 2013. */
  3. /* This file contains routines that implement the old GLPK API as it
  4. * was defined in GLPK 4.48.
  5. *
  6. * To compile an existing project using these routines you need to add
  7. * to the project this file and the header lpx.h.
  8. *
  9. * Please note that you may mix calls to old and new GLPK API routines
  10. * (except calls to glp_create_prob and glp_delete_prob). */
  11. #include <float.h>
  12. #include <limits.h>
  13. #include "lpx.h"
  14. #define xassert glp_assert
  15. #define xerror glp_error
  16. struct CPS
  17. { /* control parameters */
  18. LPX *lp;
  19. /* pointer to corresponding problem object */
  20. int msg_lev;
  21. /* level of messages output by the solver:
  22. 0 - no output
  23. 1 - error messages only
  24. 2 - normal output
  25. 3 - full output (includes informational messages) */
  26. int scale;
  27. /* scaling option:
  28. 0 - no scaling
  29. 1 - equilibration scaling
  30. 2 - geometric mean scaling
  31. 3 - geometric mean scaling, then equilibration scaling */
  32. int dual;
  33. /* dual simplex option:
  34. 0 - use primal simplex
  35. 1 - use dual simplex */
  36. int price;
  37. /* pricing option (for both primal and dual simplex):
  38. 0 - textbook pricing
  39. 1 - steepest edge pricing */
  40. double relax;
  41. /* relaxation parameter used in the ratio test; if it is zero,
  42. the textbook ratio test is used; if it is non-zero (should be
  43. positive), Harris' two-pass ratio test is used; in the latter
  44. case on the first pass basic variables (in the case of primal
  45. simplex) or reduced costs of non-basic variables (in the case
  46. of dual simplex) are allowed to slightly violate their bounds,
  47. but not more than (relax * tol_bnd) or (relax * tol_dj) (thus,
  48. relax is a percentage of tol_bnd or tol_dj) */
  49. double tol_bnd;
  50. /* relative tolerance used to check if the current basic solution
  51. is primal feasible */
  52. double tol_dj;
  53. /* absolute tolerance used to check if the current basic solution
  54. is dual feasible */
  55. double tol_piv;
  56. /* relative tolerance used to choose eligible pivotal elements of
  57. the simplex table in the ratio test */
  58. int round;
  59. /* solution rounding option:
  60. 0 - report all computed values and reduced costs "as is"
  61. 1 - if possible (allowed by the tolerances), replace computed
  62. values and reduced costs which are close to zero by exact
  63. zeros */
  64. double obj_ll;
  65. /* lower limit of the objective function; if on the phase II the
  66. objective function reaches this limit and continues decreasing,
  67. the solver stops the search */
  68. double obj_ul;
  69. /* upper limit of the objective function; if on the phase II the
  70. objective function reaches this limit and continues increasing,
  71. the solver stops the search */
  72. int it_lim;
  73. /* simplex iterations limit; if this value is positive, it is
  74. decreased by one each time when one simplex iteration has been
  75. performed, and reaching zero value signals the solver to stop
  76. the search; negative value means no iterations limit */
  77. double tm_lim;
  78. /* searching time limit, in seconds; if this value is positive,
  79. it is decreased each time when one simplex iteration has been
  80. performed by the amount of time spent for the iteration, and
  81. reaching zero value signals the solver to stop the search;
  82. negative value means no time limit */
  83. int out_frq;
  84. /* output frequency, in iterations; this parameter specifies how
  85. frequently the solver sends information about the solution to
  86. the standard output */
  87. double out_dly;
  88. /* output delay, in seconds; this parameter specifies how long
  89. the solver should delay sending information about the solution
  90. to the standard output; zero value means no delay */
  91. int branch; /* MIP */
  92. /* branching heuristic:
  93. 0 - branch on first variable
  94. 1 - branch on last variable
  95. 2 - branch using heuristic by Driebeck and Tomlin
  96. 3 - branch on most fractional variable */
  97. int btrack; /* MIP */
  98. /* backtracking heuristic:
  99. 0 - select most recent node (depth first search)
  100. 1 - select earliest node (breadth first search)
  101. 2 - select node using the best projection heuristic
  102. 3 - select node with best local bound */
  103. double tol_int; /* MIP */
  104. /* absolute tolerance used to check if the current basic solution
  105. is integer feasible */
  106. double tol_obj; /* MIP */
  107. /* relative tolerance used to check if the value of the objective
  108. function is not better than in the best known integer feasible
  109. solution */
  110. int mps_info; /* lpx_write_mps */
  111. /* if this flag is set, the routine lpx_write_mps outputs several
  112. comment cards that contains some information about the problem;
  113. otherwise the routine outputs no comment cards */
  114. int mps_obj; /* lpx_write_mps */
  115. /* this parameter tells the routine lpx_write_mps how to output
  116. the objective function row:
  117. 0 - never output objective function row
  118. 1 - always output objective function row
  119. 2 - output objective function row if and only if the problem
  120. has no free rows */
  121. int mps_orig; /* lpx_write_mps */
  122. /* if this flag is set, the routine lpx_write_mps uses original
  123. row and column symbolic names; otherwise the routine generates
  124. plain names using ordinal numbers of rows and columns */
  125. int mps_wide; /* lpx_write_mps */
  126. /* if this flag is set, the routine lpx_write_mps uses all data
  127. fields; otherwise the routine keeps fields 5 and 6 empty */
  128. int mps_free; /* lpx_write_mps */
  129. /* if this flag is set, the routine lpx_write_mps omits column
  130. and vector names everytime if possible (free style); otherwise
  131. the routine never omits these names (pedantic style) */
  132. int mps_skip; /* lpx_write_mps */
  133. /* if this flag is set, the routine lpx_write_mps skips empty
  134. columns (i.e. which has no constraint coefficients); otherwise
  135. the routine outputs all columns */
  136. int lpt_orig; /* lpx_write_lpt */
  137. /* if this flag is set, the routine lpx_write_lpt uses original
  138. row and column symbolic names; otherwise the routine generates
  139. plain names using ordinal numbers of rows and columns */
  140. int presol; /* lpx_simplex */
  141. /* LP presolver option:
  142. 0 - do not use LP presolver
  143. 1 - use LP presolver */
  144. int binarize; /* lpx_intopt */
  145. /* if this flag is set, the routine lpx_intopt replaces integer
  146. columns by binary ones */
  147. int use_cuts; /* lpx_intopt */
  148. /* if this flag is set, the routine lpx_intopt tries generating
  149. cutting planes:
  150. LPX_C_COVER - mixed cover cuts
  151. LPX_C_CLIQUE - clique cuts
  152. LPX_C_GOMORY - Gomory's mixed integer cuts
  153. LPX_C_ALL - all cuts */
  154. double mip_gap; /* MIP */
  155. /* relative MIP gap tolerance */
  156. struct CPS *link;
  157. /* pointer to CPS for another problem object */
  158. };
  159. static struct CPS *cps_ptr = NULL;
  160. /* initial pointer to CPS linked list */
  161. static struct CPS *find_cps(LPX *lp)
  162. { /* find CPS for specified problem object */
  163. struct CPS *cps;
  164. for (cps = cps_ptr; cps != NULL; cps = cps->link)
  165. if (cps->lp == lp) break;
  166. /* if cps is NULL (not found), the problem object was created
  167. with glp_create_prob rather than with lpx_create_prob */
  168. xassert(cps != NULL);
  169. return cps;
  170. }
  171. static void reset_cps(struct CPS *cps)
  172. { /* reset control parameters to default values */
  173. cps->msg_lev = 3;
  174. cps->scale = 1;
  175. cps->dual = 0;
  176. cps->price = 1;
  177. cps->relax = 0.07;
  178. cps->tol_bnd = 1e-7;
  179. cps->tol_dj = 1e-7;
  180. cps->tol_piv = 1e-9;
  181. cps->round = 0;
  182. cps->obj_ll = -DBL_MAX;
  183. cps->obj_ul = +DBL_MAX;
  184. cps->it_lim = -1;
  185. cps->tm_lim = -1.0;
  186. cps->out_frq = 200;
  187. cps->out_dly = 0.0;
  188. cps->branch = 2;
  189. cps->btrack = 3;
  190. cps->tol_int = 1e-5;
  191. cps->tol_obj = 1e-7;
  192. cps->mps_info = 1;
  193. cps->mps_obj = 2;
  194. cps->mps_orig = 0;
  195. cps->mps_wide = 1;
  196. cps->mps_free = 0;
  197. cps->mps_skip = 0;
  198. cps->lpt_orig = 0;
  199. cps->presol = 0;
  200. cps->binarize = 0;
  201. cps->use_cuts = 0;
  202. cps->mip_gap = 0.0;
  203. return;
  204. }
  205. LPX *lpx_create_prob(void)
  206. { /* create problem object */
  207. LPX *lp;
  208. struct CPS *cps;
  209. lp = glp_create_prob();
  210. cps = glp_alloc(1, sizeof(struct CPS));
  211. cps->lp = lp;
  212. reset_cps(cps);
  213. cps->link = cps_ptr;
  214. cps_ptr = cps;
  215. return lp;
  216. }
  217. void lpx_set_prob_name(LPX *lp, const char *name)
  218. { /* assign (change) problem name */
  219. glp_set_prob_name(lp, name);
  220. return;
  221. }
  222. void lpx_set_obj_name(LPX *lp, const char *name)
  223. { /* assign (change) objective function name */
  224. glp_set_obj_name(lp, name);
  225. return;
  226. }
  227. void lpx_set_obj_dir(LPX *lp, int dir)
  228. { /* set (change) optimization direction flag */
  229. glp_set_obj_dir(lp, dir - LPX_MIN + GLP_MIN);
  230. return;
  231. }
  232. int lpx_add_rows(LPX *lp, int nrs)
  233. { /* add new rows to problem object */
  234. return glp_add_rows(lp, nrs);
  235. }
  236. int lpx_add_cols(LPX *lp, int ncs)
  237. { /* add new columns to problem object */
  238. return glp_add_cols(lp, ncs);
  239. }
  240. void lpx_set_row_name(LPX *lp, int i, const char *name)
  241. { /* assign (change) row name */
  242. glp_set_row_name(lp, i, name);
  243. return;
  244. }
  245. void lpx_set_col_name(LPX *lp, int j, const char *name)
  246. { /* assign (change) column name */
  247. glp_set_col_name(lp, j, name);
  248. return;
  249. }
  250. void lpx_set_row_bnds(LPX *lp, int i, int type, double lb, double ub)
  251. { /* set (change) row bounds */
  252. glp_set_row_bnds(lp, i, type - LPX_FR + GLP_FR, lb, ub);
  253. return;
  254. }
  255. void lpx_set_col_bnds(LPX *lp, int j, int type, double lb, double ub)
  256. { /* set (change) column bounds */
  257. glp_set_col_bnds(lp, j, type - LPX_FR + GLP_FR, lb, ub);
  258. return;
  259. }
  260. void lpx_set_obj_coef(glp_prob *lp, int j, double coef)
  261. { /* set (change) obj. coefficient or constant term */
  262. glp_set_obj_coef(lp, j, coef);
  263. return;
  264. }
  265. void lpx_set_mat_row(LPX *lp, int i, int len, const int ind[],
  266. const double val[])
  267. { /* set (replace) row of the constraint matrix */
  268. glp_set_mat_row(lp, i, len, ind, val);
  269. return;
  270. }
  271. void lpx_set_mat_col(LPX *lp, int j, int len, const int ind[],
  272. const double val[])
  273. { /* set (replace) column of the constraint matrix */
  274. glp_set_mat_col(lp, j, len, ind, val);
  275. return;
  276. }
  277. void lpx_load_matrix(LPX *lp, int ne, const int ia[], const int ja[],
  278. const double ar[])
  279. { /* load (replace) the whole constraint matrix */
  280. glp_load_matrix(lp, ne, ia, ja, ar);
  281. return;
  282. }
  283. void lpx_del_rows(LPX *lp, int nrs, const int num[])
  284. { /* delete specified rows from problem object */
  285. glp_del_rows(lp, nrs, num);
  286. return;
  287. }
  288. void lpx_del_cols(LPX *lp, int ncs, const int num[])
  289. { /* delete specified columns from problem object */
  290. glp_del_cols(lp, ncs, num);
  291. return;
  292. }
  293. void lpx_delete_prob(LPX *lp)
  294. { /* delete problem object */
  295. struct CPS *cps = find_cps(lp);
  296. if (cps_ptr == cps)
  297. cps_ptr = cps->link;
  298. else
  299. { struct CPS *prev;
  300. for (prev = cps_ptr; prev != NULL; prev = prev->link)
  301. if (prev->link == cps) break;
  302. xassert(prev != NULL);
  303. prev->link = cps->link;
  304. }
  305. glp_free(cps);
  306. glp_delete_prob(lp);
  307. return;
  308. }
  309. const char *lpx_get_prob_name(LPX *lp)
  310. { /* retrieve problem name */
  311. return glp_get_prob_name(lp);
  312. }
  313. const char *lpx_get_obj_name(LPX *lp)
  314. { /* retrieve objective function name */
  315. return glp_get_obj_name(lp);
  316. }
  317. int lpx_get_obj_dir(LPX *lp)
  318. { /* retrieve optimization direction flag */
  319. return glp_get_obj_dir(lp) - GLP_MIN + LPX_MIN;
  320. }
  321. int lpx_get_num_rows(LPX *lp)
  322. { /* retrieve number of rows */
  323. return glp_get_num_rows(lp);
  324. }
  325. int lpx_get_num_cols(LPX *lp)
  326. { /* retrieve number of columns */
  327. return glp_get_num_cols(lp);
  328. }
  329. const char *lpx_get_row_name(LPX *lp, int i)
  330. { /* retrieve row name */
  331. return glp_get_row_name(lp, i);
  332. }
  333. const char *lpx_get_col_name(LPX *lp, int j)
  334. { /* retrieve column name */
  335. return glp_get_col_name(lp, j);
  336. }
  337. int lpx_get_row_type(LPX *lp, int i)
  338. { /* retrieve row type */
  339. return glp_get_row_type(lp, i) - GLP_FR + LPX_FR;
  340. }
  341. double lpx_get_row_lb(glp_prob *lp, int i)
  342. { /* retrieve row lower bound */
  343. double lb;
  344. lb = glp_get_row_lb(lp, i);
  345. if (lb == -DBL_MAX) lb = 0.0;
  346. return lb;
  347. }
  348. double lpx_get_row_ub(glp_prob *lp, int i)
  349. { /* retrieve row upper bound */
  350. double ub;
  351. ub = glp_get_row_ub(lp, i);
  352. if (ub == +DBL_MAX) ub = 0.0;
  353. return ub;
  354. }
  355. void lpx_get_row_bnds(glp_prob *lp, int i, int *typx, double *lb,
  356. double *ub)
  357. { /* retrieve row bounds */
  358. if (typx != NULL) *typx = lpx_get_row_type(lp, i);
  359. if (lb != NULL) *lb = lpx_get_row_lb(lp, i);
  360. if (ub != NULL) *ub = lpx_get_row_ub(lp, i);
  361. return;
  362. }
  363. int lpx_get_col_type(LPX *lp, int j)
  364. { /* retrieve column type */
  365. return glp_get_col_type(lp, j) - GLP_FR + LPX_FR;
  366. }
  367. double lpx_get_col_lb(glp_prob *lp, int j)
  368. { /* retrieve column lower bound */
  369. double lb;
  370. lb = glp_get_col_lb(lp, j);
  371. if (lb == -DBL_MAX) lb = 0.0;
  372. return lb;
  373. }
  374. double lpx_get_col_ub(glp_prob *lp, int j)
  375. { /* retrieve column upper bound */
  376. double ub;
  377. ub = glp_get_col_ub(lp, j);
  378. if (ub == +DBL_MAX) ub = 0.0;
  379. return ub;
  380. }
  381. void lpx_get_col_bnds(glp_prob *lp, int j, int *typx, double *lb,
  382. double *ub)
  383. { /* retrieve column bounds */
  384. if (typx != NULL) *typx = lpx_get_col_type(lp, j);
  385. if (lb != NULL) *lb = lpx_get_col_lb(lp, j);
  386. if (ub != NULL) *ub = lpx_get_col_ub(lp, j);
  387. return;
  388. }
  389. double lpx_get_obj_coef(LPX *lp, int j)
  390. { /* retrieve obj. coefficient or constant term */
  391. return glp_get_obj_coef(lp, j);
  392. }
  393. int lpx_get_num_nz(LPX *lp)
  394. { /* retrieve number of constraint coefficients */
  395. return glp_get_num_nz(lp);
  396. }
  397. int lpx_get_mat_row(LPX *lp, int i, int ind[], double val[])
  398. { /* retrieve row of the constraint matrix */
  399. return glp_get_mat_row(lp, i, ind, val);
  400. }
  401. int lpx_get_mat_col(LPX *lp, int j, int ind[], double val[])
  402. { /* retrieve column of the constraint matrix */
  403. return glp_get_mat_col(lp, j, ind, val);
  404. }
  405. void lpx_create_index(LPX *lp)
  406. { /* create the name index */
  407. glp_create_index(lp);
  408. return;
  409. }
  410. int lpx_find_row(LPX *lp, const char *name)
  411. { /* find row by its name */
  412. return glp_find_row(lp, name);
  413. }
  414. int lpx_find_col(LPX *lp, const char *name)
  415. { /* find column by its name */
  416. return glp_find_col(lp, name);
  417. }
  418. void lpx_delete_index(LPX *lp)
  419. { /* delete the name index */
  420. glp_delete_index(lp);
  421. return;
  422. }
  423. void lpx_scale_prob(LPX *lp)
  424. { /* scale problem data */
  425. switch (lpx_get_int_parm(lp, LPX_K_SCALE))
  426. { case 0:
  427. /* no scaling */
  428. glp_unscale_prob(lp);
  429. break;
  430. case 1:
  431. /* equilibration scaling */
  432. glp_scale_prob(lp, GLP_SF_EQ);
  433. break;
  434. case 2:
  435. /* geometric mean scaling */
  436. glp_scale_prob(lp, GLP_SF_GM);
  437. break;
  438. case 3:
  439. /* geometric mean scaling, then equilibration scaling */
  440. glp_scale_prob(lp, GLP_SF_GM | GLP_SF_EQ);
  441. break;
  442. default:
  443. xassert(lp != lp);
  444. }
  445. return;
  446. }
  447. void lpx_unscale_prob(LPX *lp)
  448. { /* unscale problem data */
  449. glp_unscale_prob(lp);
  450. return;
  451. }
  452. void lpx_set_row_stat(LPX *lp, int i, int stat)
  453. { /* set (change) row status */
  454. glp_set_row_stat(lp, i, stat - LPX_BS + GLP_BS);
  455. return;
  456. }
  457. void lpx_set_col_stat(LPX *lp, int j, int stat)
  458. { /* set (change) column status */
  459. glp_set_col_stat(lp, j, stat - LPX_BS + GLP_BS);
  460. return;
  461. }
  462. void lpx_std_basis(LPX *lp)
  463. { /* construct standard initial LP basis */
  464. glp_std_basis(lp);
  465. return;
  466. }
  467. void lpx_adv_basis(LPX *lp)
  468. { /* construct advanced initial LP basis */
  469. glp_adv_basis(lp, 0);
  470. return;
  471. }
  472. void lpx_cpx_basis(LPX *lp)
  473. { /* construct Bixby's initial LP basis */
  474. glp_cpx_basis(lp);
  475. return;
  476. }
  477. static void fill_smcp(LPX *lp, glp_smcp *parm)
  478. { glp_init_smcp(parm);
  479. switch (lpx_get_int_parm(lp, LPX_K_MSGLEV))
  480. { case 0: parm->msg_lev = GLP_MSG_OFF; break;
  481. case 1: parm->msg_lev = GLP_MSG_ERR; break;
  482. case 2: parm->msg_lev = GLP_MSG_ON; break;
  483. case 3: parm->msg_lev = GLP_MSG_ALL; break;
  484. default: xassert(lp != lp);
  485. }
  486. switch (lpx_get_int_parm(lp, LPX_K_DUAL))
  487. { case 0: parm->meth = GLP_PRIMAL; break;
  488. case 1: parm->meth = GLP_DUAL; break;
  489. default: xassert(lp != lp);
  490. }
  491. switch (lpx_get_int_parm(lp, LPX_K_PRICE))
  492. { case 0: parm->pricing = GLP_PT_STD; break;
  493. case 1: parm->pricing = GLP_PT_PSE; break;
  494. default: xassert(lp != lp);
  495. }
  496. if (lpx_get_real_parm(lp, LPX_K_RELAX) == 0.0)
  497. parm->r_test = GLP_RT_STD;
  498. else
  499. parm->r_test = GLP_RT_HAR;
  500. parm->tol_bnd = lpx_get_real_parm(lp, LPX_K_TOLBND);
  501. parm->tol_dj = lpx_get_real_parm(lp, LPX_K_TOLDJ);
  502. parm->tol_piv = lpx_get_real_parm(lp, LPX_K_TOLPIV);
  503. parm->obj_ll = lpx_get_real_parm(lp, LPX_K_OBJLL);
  504. parm->obj_ul = lpx_get_real_parm(lp, LPX_K_OBJUL);
  505. if (lpx_get_int_parm(lp, LPX_K_ITLIM) < 0)
  506. parm->it_lim = INT_MAX;
  507. else
  508. parm->it_lim = lpx_get_int_parm(lp, LPX_K_ITLIM);
  509. if (lpx_get_real_parm(lp, LPX_K_TMLIM) < 0.0)
  510. parm->tm_lim = INT_MAX;
  511. else
  512. parm->tm_lim =
  513. (int)(1000.0 * lpx_get_real_parm(lp, LPX_K_TMLIM));
  514. parm->out_frq = lpx_get_int_parm(lp, LPX_K_OUTFRQ);
  515. parm->out_dly =
  516. (int)(1000.0 * lpx_get_real_parm(lp, LPX_K_OUTDLY));
  517. switch (lpx_get_int_parm(lp, LPX_K_PRESOL))
  518. { case 0: parm->presolve = GLP_OFF; break;
  519. case 1: parm->presolve = GLP_ON; break;
  520. default: xassert(lp != lp);
  521. }
  522. return;
  523. }
  524. int lpx_simplex(LPX *lp)
  525. { /* easy-to-use driver to the simplex method */
  526. glp_smcp parm;
  527. int ret;
  528. fill_smcp(lp, &parm);
  529. ret = glp_simplex(lp, &parm);
  530. switch (ret)
  531. { case 0: ret = LPX_E_OK; break;
  532. case GLP_EBADB:
  533. case GLP_ESING:
  534. case GLP_ECOND:
  535. case GLP_EBOUND: ret = LPX_E_FAULT; break;
  536. case GLP_EFAIL: ret = LPX_E_SING; break;
  537. case GLP_EOBJLL: ret = LPX_E_OBJLL; break;
  538. case GLP_EOBJUL: ret = LPX_E_OBJUL; break;
  539. case GLP_EITLIM: ret = LPX_E_ITLIM; break;
  540. case GLP_ETMLIM: ret = LPX_E_TMLIM; break;
  541. case GLP_ENOPFS: ret = LPX_E_NOPFS; break;
  542. case GLP_ENODFS: ret = LPX_E_NODFS; break;
  543. default: xassert(ret != ret);
  544. }
  545. return ret;
  546. }
  547. int lpx_exact(LPX *lp)
  548. { /* easy-to-use driver to the exact simplex method */
  549. glp_smcp parm;
  550. int ret;
  551. fill_smcp(lp, &parm);
  552. ret = glp_exact(lp, &parm);
  553. switch (ret)
  554. { case 0: ret = LPX_E_OK; break;
  555. case GLP_EBADB:
  556. case GLP_ESING:
  557. case GLP_EBOUND:
  558. case GLP_EFAIL: ret = LPX_E_FAULT; break;
  559. case GLP_EITLIM: ret = LPX_E_ITLIM; break;
  560. case GLP_ETMLIM: ret = LPX_E_TMLIM; break;
  561. default: xassert(ret != ret);
  562. }
  563. return ret;
  564. }
  565. int lpx_get_status(glp_prob *lp)
  566. { /* retrieve generic status of basic solution */
  567. int status;
  568. switch (glp_get_status(lp))
  569. { case GLP_OPT: status = LPX_OPT; break;
  570. case GLP_FEAS: status = LPX_FEAS; break;
  571. case GLP_INFEAS: status = LPX_INFEAS; break;
  572. case GLP_NOFEAS: status = LPX_NOFEAS; break;
  573. case GLP_UNBND: status = LPX_UNBND; break;
  574. case GLP_UNDEF: status = LPX_UNDEF; break;
  575. default: xassert(lp != lp);
  576. }
  577. return status;
  578. }
  579. int lpx_get_prim_stat(glp_prob *lp)
  580. { /* retrieve status of primal basic solution */
  581. return glp_get_prim_stat(lp) - GLP_UNDEF + LPX_P_UNDEF;
  582. }
  583. int lpx_get_dual_stat(glp_prob *lp)
  584. { /* retrieve status of dual basic solution */
  585. return glp_get_dual_stat(lp) - GLP_UNDEF + LPX_D_UNDEF;
  586. }
  587. double lpx_get_obj_val(LPX *lp)
  588. { /* retrieve objective value (basic solution) */
  589. return glp_get_obj_val(lp);
  590. }
  591. int lpx_get_row_stat(LPX *lp, int i)
  592. { /* retrieve row status (basic solution) */
  593. return glp_get_row_stat(lp, i) - GLP_BS + LPX_BS;
  594. }
  595. double lpx_get_row_prim(LPX *lp, int i)
  596. { /* retrieve row primal value (basic solution) */
  597. return glp_get_row_prim(lp, i);
  598. }
  599. double lpx_get_row_dual(LPX *lp, int i)
  600. { /* retrieve row dual value (basic solution) */
  601. return glp_get_row_dual(lp, i);
  602. }
  603. void lpx_get_row_info(glp_prob *lp, int i, int *tagx, double *vx,
  604. double *dx)
  605. { /* obtain row solution information */
  606. if (tagx != NULL) *tagx = lpx_get_row_stat(lp, i);
  607. if (vx != NULL) *vx = lpx_get_row_prim(lp, i);
  608. if (dx != NULL) *dx = lpx_get_row_dual(lp, i);
  609. return;
  610. }
  611. int lpx_get_col_stat(LPX *lp, int j)
  612. { /* retrieve column status (basic solution) */
  613. return glp_get_col_stat(lp, j) - GLP_BS + LPX_BS;
  614. }
  615. double lpx_get_col_prim(LPX *lp, int j)
  616. { /* retrieve column primal value (basic solution) */
  617. return glp_get_col_prim(lp, j);
  618. }
  619. double lpx_get_col_dual(glp_prob *lp, int j)
  620. { /* retrieve column dual value (basic solution) */
  621. return glp_get_col_dual(lp, j);
  622. }
  623. void lpx_get_col_info(glp_prob *lp, int j, int *tagx, double *vx,
  624. double *dx)
  625. { /* obtain column solution information */
  626. if (tagx != NULL) *tagx = lpx_get_col_stat(lp, j);
  627. if (vx != NULL) *vx = lpx_get_col_prim(lp, j);
  628. if (dx != NULL) *dx = lpx_get_col_dual(lp, j);
  629. return;
  630. }
  631. int lpx_get_ray_info(LPX *lp)
  632. { /* determine what causes primal unboundness */
  633. return glp_get_unbnd_ray(lp);
  634. }
  635. void lpx_check_kkt(LPX *lp, int scaled, LPXKKT *kkt)
  636. { /* check Karush-Kuhn-Tucker conditions */
  637. int m = glp_get_num_rows(lp);
  638. int ae_ind, re_ind;
  639. double ae_max, re_max;
  640. xassert(scaled == scaled);
  641. glp_check_kkt(lp, GLP_SOL, GLP_KKT_PE, &ae_max, &ae_ind, &re_max,
  642. &re_ind);
  643. kkt->pe_ae_max = ae_max;
  644. kkt->pe_ae_row = ae_ind;
  645. kkt->pe_re_max = re_max;
  646. kkt->pe_re_row = re_ind;
  647. if (re_max <= 1e-9)
  648. kkt->pe_quality = 'H';
  649. else if (re_max <= 1e-6)
  650. kkt->pe_quality = 'M';
  651. else if (re_max <= 1e-3)
  652. kkt->pe_quality = 'L';
  653. else
  654. kkt->pe_quality = '?';
  655. glp_check_kkt(lp, GLP_SOL, GLP_KKT_PB, &ae_max, &ae_ind, &re_max,
  656. &re_ind);
  657. kkt->pb_ae_max = ae_max;
  658. kkt->pb_ae_ind = ae_ind;
  659. kkt->pb_re_max = re_max;
  660. kkt->pb_re_ind = re_ind;
  661. if (re_max <= 1e-9)
  662. kkt->pb_quality = 'H';
  663. else if (re_max <= 1e-6)
  664. kkt->pb_quality = 'M';
  665. else if (re_max <= 1e-3)
  666. kkt->pb_quality = 'L';
  667. else
  668. kkt->pb_quality = '?';
  669. glp_check_kkt(lp, GLP_SOL, GLP_KKT_DE, &ae_max, &ae_ind, &re_max,
  670. &re_ind);
  671. kkt->de_ae_max = ae_max;
  672. if (ae_ind == 0)
  673. kkt->de_ae_col = 0;
  674. else
  675. kkt->de_ae_col = ae_ind - m;
  676. kkt->de_re_max = re_max;
  677. if (re_ind == 0)
  678. kkt->de_re_col = 0;
  679. else
  680. kkt->de_re_col = ae_ind - m;
  681. if (re_max <= 1e-9)
  682. kkt->de_quality = 'H';
  683. else if (re_max <= 1e-6)
  684. kkt->de_quality = 'M';
  685. else if (re_max <= 1e-3)
  686. kkt->de_quality = 'L';
  687. else
  688. kkt->de_quality = '?';
  689. glp_check_kkt(lp, GLP_SOL, GLP_KKT_DB, &ae_max, &ae_ind, &re_max,
  690. &re_ind);
  691. kkt->db_ae_max = ae_max;
  692. kkt->db_ae_ind = ae_ind;
  693. kkt->db_re_max = re_max;
  694. kkt->db_re_ind = re_ind;
  695. if (re_max <= 1e-9)
  696. kkt->db_quality = 'H';
  697. else if (re_max <= 1e-6)
  698. kkt->db_quality = 'M';
  699. else if (re_max <= 1e-3)
  700. kkt->db_quality = 'L';
  701. else
  702. kkt->db_quality = '?';
  703. kkt->cs_ae_max = 0.0, kkt->cs_ae_ind = 0;
  704. kkt->cs_re_max = 0.0, kkt->cs_re_ind = 0;
  705. kkt->cs_quality = 'H';
  706. return;
  707. }
  708. int lpx_warm_up(LPX *lp)
  709. { /* "warm up" LP basis */
  710. int ret;
  711. ret = glp_warm_up(lp);
  712. if (ret == 0)
  713. ret = LPX_E_OK;
  714. else if (ret == GLP_EBADB)
  715. ret = LPX_E_BADB;
  716. else if (ret == GLP_ESING)
  717. ret = LPX_E_SING;
  718. else if (ret == GLP_ECOND)
  719. ret = LPX_E_SING;
  720. else
  721. xassert(ret != ret);
  722. return ret;
  723. }
  724. int lpx_eval_tab_row(LPX *lp, int k, int ind[], double val[])
  725. { /* compute row of the simplex tableau */
  726. return glp_eval_tab_row(lp, k, ind, val);
  727. }
  728. int lpx_eval_tab_col(LPX *lp, int k, int ind[], double val[])
  729. { /* compute column of the simplex tableau */
  730. return glp_eval_tab_col(lp, k, ind, val);
  731. }
  732. int lpx_transform_row(LPX *lp, int len, int ind[], double val[])
  733. { /* transform explicitly specified row */
  734. return glp_transform_row(lp, len, ind, val);
  735. }
  736. int lpx_transform_col(LPX *lp, int len, int ind[], double val[])
  737. { /* transform explicitly specified column */
  738. return glp_transform_col(lp, len, ind, val);
  739. }
  740. int lpx_prim_ratio_test(LPX *lp, int len, const int ind[],
  741. const double val[], int how, double tol)
  742. { /* perform primal ratio test */
  743. int piv;
  744. piv = glp_prim_rtest(lp, len, ind, val, how, tol);
  745. xassert(0 <= piv && piv <= len);
  746. return piv == 0 ? 0 : ind[piv];
  747. }
  748. int lpx_dual_ratio_test(LPX *lp, int len, const int ind[],
  749. const double val[], int how, double tol)
  750. { /* perform dual ratio test */
  751. int piv;
  752. piv = glp_dual_rtest(lp, len, ind, val, how, tol);
  753. xassert(0 <= piv && piv <= len);
  754. return piv == 0 ? 0 : ind[piv];
  755. }
  756. int lpx_interior(LPX *lp)
  757. { /* easy-to-use driver to the interior-point method */
  758. int ret;
  759. ret = glp_interior(lp, NULL);
  760. switch (ret)
  761. { case 0: ret = LPX_E_OK; break;
  762. case GLP_EFAIL: ret = LPX_E_FAULT; break;
  763. case GLP_ENOFEAS: ret = LPX_E_NOFEAS; break;
  764. case GLP_ENOCVG: ret = LPX_E_NOCONV; break;
  765. case GLP_EITLIM: ret = LPX_E_ITLIM; break;
  766. case GLP_EINSTAB: ret = LPX_E_INSTAB; break;
  767. default: xassert(ret != ret);
  768. }
  769. return ret;
  770. }
  771. int lpx_ipt_status(glp_prob *lp)
  772. { /* retrieve status of interior-point solution */
  773. int status;
  774. switch (glp_ipt_status(lp))
  775. { case GLP_UNDEF: status = LPX_T_UNDEF; break;
  776. case GLP_OPT: status = LPX_T_OPT; break;
  777. default: xassert(lp != lp);
  778. }
  779. return status;
  780. }
  781. double lpx_ipt_obj_val(LPX *lp)
  782. { /* retrieve objective value (interior point) */
  783. return glp_ipt_obj_val(lp);
  784. }
  785. double lpx_ipt_row_prim(LPX *lp, int i)
  786. { /* retrieve row primal value (interior point) */
  787. return glp_ipt_row_prim(lp, i);
  788. }
  789. double lpx_ipt_row_dual(LPX *lp, int i)
  790. { /* retrieve row dual value (interior point) */
  791. return glp_ipt_row_dual(lp, i);
  792. }
  793. double lpx_ipt_col_prim(LPX *lp, int j)
  794. { /* retrieve column primal value (interior point) */
  795. return glp_ipt_col_prim(lp, j);
  796. }
  797. double lpx_ipt_col_dual(LPX *lp, int j)
  798. { /* retrieve column dual value (interior point) */
  799. return glp_ipt_col_dual(lp, j);
  800. }
  801. void lpx_set_class(LPX *lp, int klass)
  802. { /* set problem class */
  803. xassert(lp == lp);
  804. if (!(klass == LPX_LP || klass == LPX_MIP))
  805. xerror("lpx_set_class: invalid problem class\n");
  806. return;
  807. }
  808. int lpx_get_class(LPX *lp)
  809. { /* determine problem klass */
  810. return glp_get_num_int(lp) == 0 ? LPX_LP : LPX_MIP;
  811. }
  812. void lpx_set_col_kind(LPX *lp, int j, int kind)
  813. { /* set (change) column kind */
  814. glp_set_col_kind(lp, j, kind - LPX_CV + GLP_CV);
  815. return;
  816. }
  817. int lpx_get_col_kind(LPX *lp, int j)
  818. { /* retrieve column kind */
  819. return glp_get_col_kind(lp, j) == GLP_CV ? LPX_CV : LPX_IV;
  820. }
  821. int lpx_get_num_int(LPX *lp)
  822. { /* retrieve number of integer columns */
  823. return glp_get_num_int(lp);
  824. }
  825. int lpx_get_num_bin(LPX *lp)
  826. { /* retrieve number of binary columns */
  827. return glp_get_num_bin(lp);
  828. }
  829. static int solve_mip(LPX *lp, int presolve)
  830. { glp_iocp parm;
  831. int ret;
  832. glp_init_iocp(&parm);
  833. switch (lpx_get_int_parm(lp, LPX_K_MSGLEV))
  834. { case 0: parm.msg_lev = GLP_MSG_OFF; break;
  835. case 1: parm.msg_lev = GLP_MSG_ERR; break;
  836. case 2: parm.msg_lev = GLP_MSG_ON; break;
  837. case 3: parm.msg_lev = GLP_MSG_ALL; break;
  838. default: xassert(lp != lp);
  839. }
  840. switch (lpx_get_int_parm(lp, LPX_K_BRANCH))
  841. { case 0: parm.br_tech = GLP_BR_FFV; break;
  842. case 1: parm.br_tech = GLP_BR_LFV; break;
  843. case 2: parm.br_tech = GLP_BR_DTH; break;
  844. case 3: parm.br_tech = GLP_BR_MFV; break;
  845. default: xassert(lp != lp);
  846. }
  847. switch (lpx_get_int_parm(lp, LPX_K_BTRACK))
  848. { case 0: parm.bt_tech = GLP_BT_DFS; break;
  849. case 1: parm.bt_tech = GLP_BT_BFS; break;
  850. case 2: parm.bt_tech = GLP_BT_BPH; break;
  851. case 3: parm.bt_tech = GLP_BT_BLB; break;
  852. default: xassert(lp != lp);
  853. }
  854. parm.tol_int = lpx_get_real_parm(lp, LPX_K_TOLINT);
  855. parm.tol_obj = lpx_get_real_parm(lp, LPX_K_TOLOBJ);
  856. if (lpx_get_real_parm(lp, LPX_K_TMLIM) < 0.0 ||
  857. lpx_get_real_parm(lp, LPX_K_TMLIM) > 1e6)
  858. parm.tm_lim = INT_MAX;
  859. else
  860. parm.tm_lim =
  861. (int)(1000.0 * lpx_get_real_parm(lp, LPX_K_TMLIM));
  862. parm.mip_gap = lpx_get_real_parm(lp, LPX_K_MIPGAP);
  863. if (lpx_get_int_parm(lp, LPX_K_USECUTS) & LPX_C_GOMORY)
  864. parm.gmi_cuts = GLP_ON;
  865. else
  866. parm.gmi_cuts = GLP_OFF;
  867. if (lpx_get_int_parm(lp, LPX_K_USECUTS) & LPX_C_MIR)
  868. parm.mir_cuts = GLP_ON;
  869. else
  870. parm.mir_cuts = GLP_OFF;
  871. if (lpx_get_int_parm(lp, LPX_K_USECUTS) & LPX_C_COVER)
  872. parm.cov_cuts = GLP_ON;
  873. else
  874. parm.cov_cuts = GLP_OFF;
  875. if (lpx_get_int_parm(lp, LPX_K_USECUTS) & LPX_C_CLIQUE)
  876. parm.clq_cuts = GLP_ON;
  877. else
  878. parm.clq_cuts = GLP_OFF;
  879. parm.presolve = presolve;
  880. if (lpx_get_int_parm(lp, LPX_K_BINARIZE))
  881. parm.binarize = GLP_ON;
  882. ret = glp_intopt(lp, &parm);
  883. switch (ret)
  884. { case 0: ret = LPX_E_OK; break;
  885. case GLP_ENOPFS: ret = LPX_E_NOPFS; break;
  886. case GLP_ENODFS: ret = LPX_E_NODFS; break;
  887. case GLP_EBOUND:
  888. case GLP_EROOT: ret = LPX_E_FAULT; break;
  889. case GLP_EFAIL: ret = LPX_E_SING; break;
  890. case GLP_EMIPGAP: ret = LPX_E_MIPGAP; break;
  891. case GLP_ETMLIM: ret = LPX_E_TMLIM; break;
  892. default: xassert(ret != ret);
  893. }
  894. return ret;
  895. }
  896. int lpx_integer(LPX *lp)
  897. { /* easy-to-use driver to the branch-and-bound method */
  898. return solve_mip(lp, GLP_OFF);
  899. }
  900. int lpx_intopt(LPX *lp)
  901. { /* easy-to-use driver to the branch-and-bound method */
  902. return solve_mip(lp, GLP_ON);
  903. }
  904. int lpx_mip_status(glp_prob *lp)
  905. { /* retrieve status of MIP solution */
  906. int status;
  907. switch (glp_mip_status(lp))
  908. { case GLP_UNDEF: status = LPX_I_UNDEF; break;
  909. case GLP_OPT: status = LPX_I_OPT; break;
  910. case GLP_FEAS: status = LPX_I_FEAS; break;
  911. case GLP_NOFEAS: status = LPX_I_NOFEAS; break;
  912. default: xassert(lp != lp);
  913. }
  914. return status;
  915. }
  916. double lpx_mip_obj_val(LPX *lp)
  917. { /* retrieve objective value (MIP solution) */
  918. return glp_mip_obj_val(lp);
  919. }
  920. double lpx_mip_row_val(LPX *lp, int i)
  921. { /* retrieve row value (MIP solution) */
  922. return glp_mip_row_val(lp, i);
  923. }
  924. double lpx_mip_col_val(LPX *lp, int j)
  925. { /* retrieve column value (MIP solution) */
  926. return glp_mip_col_val(lp, j);
  927. }
  928. void lpx_check_int(LPX *lp, LPXKKT *kkt)
  929. { /* check integer feasibility conditions */
  930. int ae_ind, re_ind;
  931. double ae_max, re_max;
  932. glp_check_kkt(lp, GLP_MIP, GLP_KKT_PE, &ae_max, &ae_ind, &re_max,
  933. &re_ind);
  934. kkt->pe_ae_max = ae_max;
  935. kkt->pe_ae_row = ae_ind;
  936. kkt->pe_re_max = re_max;
  937. kkt->pe_re_row = re_ind;
  938. if (re_max <= 1e-9)
  939. kkt->pe_quality = 'H';
  940. else if (re_max <= 1e-6)
  941. kkt->pe_quality = 'M';
  942. else if (re_max <= 1e-3)
  943. kkt->pe_quality = 'L';
  944. else
  945. kkt->pe_quality = '?';
  946. glp_check_kkt(lp, GLP_MIP, GLP_KKT_PB, &ae_max, &ae_ind, &re_max,
  947. &re_ind);
  948. kkt->pb_ae_max = ae_max;
  949. kkt->pb_ae_ind = ae_ind;
  950. kkt->pb_re_max = re_max;
  951. kkt->pb_re_ind = re_ind;
  952. if (re_max <= 1e-9)
  953. kkt->pb_quality = 'H';
  954. else if (re_max <= 1e-6)
  955. kkt->pb_quality = 'M';
  956. else if (re_max <= 1e-3)
  957. kkt->pb_quality = 'L';
  958. else
  959. kkt->pb_quality = '?';
  960. return;
  961. }
  962. void lpx_reset_parms(LPX *lp)
  963. { /* reset control parameters to default values */
  964. struct CPS *cps = find_cps(lp);
  965. reset_cps(cps);
  966. return;
  967. }
  968. void lpx_set_int_parm(LPX *lp, int parm, int val)
  969. { /* set (change) integer control parameter */
  970. struct CPS *cps = find_cps(lp);
  971. switch (parm)
  972. { case LPX_K_MSGLEV:
  973. if (!(0 <= val && val <= 3))
  974. xerror("lpx_set_int_parm: MSGLEV = %d; invalid value\n",
  975. val);
  976. cps->msg_lev = val;
  977. break;
  978. case LPX_K_SCALE:
  979. if (!(0 <= val && val <= 3))
  980. xerror("lpx_set_int_parm: SCALE = %d; invalid value\n",
  981. val);
  982. cps->scale = val;
  983. break;
  984. case LPX_K_DUAL:
  985. if (!(val == 0 || val == 1))
  986. xerror("lpx_set_int_parm: DUAL = %d; invalid value\n",
  987. val);
  988. cps->dual = val;
  989. break;
  990. case LPX_K_PRICE:
  991. if (!(val == 0 || val == 1))
  992. xerror("lpx_set_int_parm: PRICE = %d; invalid value\n",
  993. val);
  994. cps->price = val;
  995. break;
  996. case LPX_K_ROUND:
  997. if (!(val == 0 || val == 1))
  998. xerror("lpx_set_int_parm: ROUND = %d; invalid value\n",
  999. val);
  1000. cps->round = val;
  1001. break;
  1002. case LPX_K_ITLIM:
  1003. cps->it_lim = val;
  1004. break;
  1005. case LPX_K_ITCNT:
  1006. glp_set_it_cnt(lp, val);
  1007. break;
  1008. case LPX_K_OUTFRQ:
  1009. if (!(val > 0))
  1010. xerror("lpx_set_int_parm: OUTFRQ = %d; invalid value\n",
  1011. val);
  1012. cps->out_frq = val;
  1013. break;
  1014. case LPX_K_BRANCH:
  1015. if (!(val == 0 || val == 1 || val == 2 || val == 3))
  1016. xerror("lpx_set_int_parm: BRANCH = %d; invalid value\n",
  1017. val);
  1018. cps->branch = val;
  1019. break;
  1020. case LPX_K_BTRACK:
  1021. if (!(val == 0 || val == 1 || val == 2 || val == 3))
  1022. xerror("lpx_set_int_parm: BTRACK = %d; invalid value\n",
  1023. val);
  1024. cps->btrack = val;
  1025. break;
  1026. case LPX_K_MPSINFO:
  1027. if (!(val == 0 || val == 1))
  1028. xerror("lpx_set_int_parm: MPSINFO = %d; invalid value\n",
  1029. val);
  1030. cps->mps_info = val;
  1031. break;
  1032. case LPX_K_MPSOBJ:
  1033. if (!(val == 0 || val == 1 || val == 2))
  1034. xerror("lpx_set_int_parm: MPSOBJ = %d; invalid value\n",
  1035. val);
  1036. cps->mps_obj = val;
  1037. break;
  1038. case LPX_K_MPSORIG:
  1039. if (!(val == 0 || val == 1))
  1040. xerror("lpx_set_int_parm: MPSORIG = %d; invalid value\n",
  1041. val);
  1042. cps->mps_orig = val;
  1043. break;
  1044. case LPX_K_MPSWIDE:
  1045. if (!(val == 0 || val == 1))
  1046. xerror("lpx_set_int_parm: MPSWIDE = %d; invalid value\n",
  1047. val);
  1048. cps->mps_wide = val;
  1049. break;
  1050. case LPX_K_MPSFREE:
  1051. if (!(val == 0 || val == 1))
  1052. xerror("lpx_set_int_parm: MPSFREE = %d; invalid value\n",
  1053. val);
  1054. cps->mps_free = val;
  1055. break;
  1056. case LPX_K_MPSSKIP:
  1057. if (!(val == 0 || val == 1))
  1058. xerror("lpx_set_int_parm: MPSSKIP = %d; invalid value\n",
  1059. val);
  1060. cps->mps_skip = val;
  1061. break;
  1062. case LPX_K_LPTORIG:
  1063. if (!(val == 0 || val == 1))
  1064. xerror("lpx_set_int_parm: LPTORIG = %d; invalid value\n",
  1065. val);
  1066. cps->lpt_orig = val;
  1067. break;
  1068. case LPX_K_PRESOL:
  1069. if (!(val == 0 || val == 1))
  1070. xerror("lpx_set_int_parm: PRESOL = %d; invalid value\n",
  1071. val);
  1072. cps->presol = val;
  1073. break;
  1074. case LPX_K_BINARIZE:
  1075. if (!(val == 0 || val == 1))
  1076. xerror("lpx_set_int_parm: BINARIZE = %d; invalid value\n"
  1077. , val);
  1078. cps->binarize = val;
  1079. break;
  1080. case LPX_K_USECUTS:
  1081. if (val & ~LPX_C_ALL)
  1082. xerror("lpx_set_int_parm: USECUTS = 0x%X; invalid value\n",
  1083. val);
  1084. cps->use_cuts = val;
  1085. break;
  1086. case LPX_K_BFTYPE:
  1087. { glp_bfcp parm;
  1088. glp_get_bfcp(lp, &parm);
  1089. switch (val)
  1090. { case 1:
  1091. parm.type = GLP_BF_FT; break;
  1092. case 2:
  1093. parm.type = GLP_BF_BG; break;
  1094. case 3:
  1095. parm.type = GLP_BF_GR; break;
  1096. default:
  1097. xerror("lpx_set_int_parm: BFTYPE = %d; invalid val"
  1098. "ue\n", val);
  1099. }
  1100. glp_set_bfcp(lp, &parm);
  1101. }
  1102. break;
  1103. default:
  1104. xerror("lpx_set_int_parm: parm = %d; invalid parameter\n",
  1105. parm);
  1106. }
  1107. return;
  1108. }
  1109. int lpx_get_int_parm(LPX *lp, int parm)
  1110. { /* query integer control parameter */
  1111. struct CPS *cps = find_cps(lp);
  1112. int val = 0;
  1113. switch (parm)
  1114. { case LPX_K_MSGLEV:
  1115. val = cps->msg_lev; break;
  1116. case LPX_K_SCALE:
  1117. val = cps->scale; break;
  1118. case LPX_K_DUAL:
  1119. val = cps->dual; break;
  1120. case LPX_K_PRICE:
  1121. val = cps->price; break;
  1122. case LPX_K_ROUND:
  1123. val = cps->round; break;
  1124. case LPX_K_ITLIM:
  1125. val = cps->it_lim; break;
  1126. case LPX_K_ITCNT:
  1127. val = glp_get_it_cnt(lp); break;
  1128. case LPX_K_OUTFRQ:
  1129. val = cps->out_frq; break;
  1130. case LPX_K_BRANCH:
  1131. val = cps->branch; break;
  1132. case LPX_K_BTRACK:
  1133. val = cps->btrack; break;
  1134. case LPX_K_MPSINFO:
  1135. val = cps->mps_info; break;
  1136. case LPX_K_MPSOBJ:
  1137. val = cps->mps_obj; break;
  1138. case LPX_K_MPSORIG:
  1139. val = cps->mps_orig; break;
  1140. case LPX_K_MPSWIDE:
  1141. val = cps->mps_wide; break;
  1142. case LPX_K_MPSFREE:
  1143. val = cps->mps_free; break;
  1144. case LPX_K_MPSSKIP:
  1145. val = cps->mps_skip; break;
  1146. case LPX_K_LPTORIG:
  1147. val = cps->lpt_orig; break;
  1148. case LPX_K_PRESOL:
  1149. val = cps->presol; break;
  1150. case LPX_K_BINARIZE:
  1151. val = cps->binarize; break;
  1152. case LPX_K_USECUTS:
  1153. val = cps->use_cuts; break;
  1154. case LPX_K_BFTYPE:
  1155. { glp_bfcp parm;
  1156. glp_get_bfcp(lp, &parm);
  1157. switch (parm.type)
  1158. { case GLP_BF_FT:
  1159. val = 1; break;
  1160. case GLP_BF_BG:
  1161. val = 2; break;
  1162. case GLP_BF_GR:
  1163. val = 3; break;
  1164. default:
  1165. xassert(lp != lp);
  1166. }
  1167. }
  1168. break;
  1169. default:
  1170. xerror("lpx_get_int_parm: parm = %d; invalid parameter\n",
  1171. parm);
  1172. }
  1173. return val;
  1174. }
  1175. void lpx_set_real_parm(LPX *lp, int parm, double val)
  1176. { /* set (change) real control parameter */
  1177. struct CPS *cps = find_cps(lp);
  1178. switch (parm)
  1179. { case LPX_K_RELAX:
  1180. if (!(0.0 <= val && val <= 1.0))
  1181. xerror("lpx_set_real_parm: RELAX = %g; invalid value\n",
  1182. val);
  1183. cps->relax = val;
  1184. break;
  1185. case LPX_K_TOLBND:
  1186. if (!(DBL_EPSILON <= val && val <= 0.001))
  1187. xerror("lpx_set_real_parm: TOLBND = %g; invalid value\n",
  1188. val);
  1189. cps->tol_bnd = val;
  1190. break;
  1191. case LPX_K_TOLDJ:
  1192. if (!(DBL_EPSILON <= val && val <= 0.001))
  1193. xerror("lpx_set_real_parm: TOLDJ = %g; invalid value\n",
  1194. val);
  1195. cps->tol_dj = val;
  1196. break;
  1197. case LPX_K_TOLPIV:
  1198. if (!(DBL_EPSILON <= val && val <= 0.001))
  1199. xerror("lpx_set_real_parm: TOLPIV = %g; invalid value\n",
  1200. val);
  1201. cps->tol_piv = val;
  1202. break;
  1203. case LPX_K_OBJLL:
  1204. cps->obj_ll = val;
  1205. break;
  1206. case LPX_K_OBJUL:
  1207. cps->obj_ul = val;
  1208. break;
  1209. case LPX_K_TMLIM:
  1210. cps->tm_lim = val;
  1211. break;
  1212. case LPX_K_OUTDLY:
  1213. cps->out_dly = val;
  1214. break;
  1215. case LPX_K_TOLINT:
  1216. if (!(DBL_EPSILON <= val && val <= 0.001))
  1217. xerror("lpx_set_real_parm: TOLINT = %g; invalid value\n",
  1218. val);
  1219. cps->tol_int = val;
  1220. break;
  1221. case LPX_K_TOLOBJ:
  1222. if (!(DBL_EPSILON <= val && val <= 0.001))
  1223. xerror("lpx_set_real_parm: TOLOBJ = %g; invalid value\n",
  1224. val);
  1225. cps->tol_obj = val;
  1226. break;
  1227. case LPX_K_MIPGAP:
  1228. if (val < 0.0)
  1229. xerror("lpx_set_real_parm: MIPGAP = %g; invalid value\n",
  1230. val);
  1231. cps->mip_gap = val;
  1232. break;
  1233. default:
  1234. xerror("lpx_set_real_parm: parm = %d; invalid parameter\n",
  1235. parm);
  1236. }
  1237. return;
  1238. }
  1239. double lpx_get_real_parm(LPX *lp, int parm)
  1240. { /* query real control parameter */
  1241. struct CPS *cps = find_cps(lp);
  1242. double val = 0.0;
  1243. switch (parm)
  1244. { case LPX_K_RELAX:
  1245. val = cps->relax;
  1246. break;
  1247. case LPX_K_TOLBND:
  1248. val = cps->tol_bnd;
  1249. break;
  1250. case LPX_K_TOLDJ:
  1251. val = cps->tol_dj;
  1252. break;
  1253. case LPX_K_TOLPIV:
  1254. val = cps->tol_piv;
  1255. break;
  1256. case LPX_K_OBJLL:
  1257. val = cps->obj_ll;
  1258. break;
  1259. case LPX_K_OBJUL:
  1260. val = cps->obj_ul;
  1261. break;
  1262. case LPX_K_TMLIM:
  1263. val = cps->tm_lim;
  1264. break;
  1265. case LPX_K_OUTDLY:
  1266. val = cps->out_dly;
  1267. break;
  1268. case LPX_K_TOLINT:
  1269. val = cps->tol_int;
  1270. break;
  1271. case LPX_K_TOLOBJ:
  1272. val = cps->tol_obj;
  1273. break;
  1274. case LPX_K_MIPGAP:
  1275. val = cps->mip_gap;
  1276. break;
  1277. default:
  1278. xerror("lpx_get_real_parm: parm = %d; invalid parameter\n",
  1279. parm);
  1280. }
  1281. return val;
  1282. }
  1283. LPX *lpx_read_mps(const char *fname)
  1284. { /* read problem data in fixed MPS format */
  1285. LPX *lp = lpx_create_prob();
  1286. if (glp_read_mps(lp, GLP_MPS_DECK, NULL, fname))
  1287. lpx_delete_prob(lp), lp = NULL;
  1288. return lp;
  1289. }
  1290. int lpx_write_mps(LPX *lp, const char *fname)
  1291. { /* write problem data in fixed MPS format */
  1292. return glp_write_mps(lp, GLP_MPS_DECK, NULL, fname);
  1293. }
  1294. int lpx_read_bas(LPX *lp, const char *fname)
  1295. { /* read LP basis in fixed MPS format */
  1296. xassert(lp == lp);
  1297. xassert(fname == fname);
  1298. xerror("lpx_read_bas: operation not supported\n");
  1299. return 0;
  1300. }
  1301. int lpx_write_bas(LPX *lp, const char *fname)
  1302. { /* write LP basis in fixed MPS format */
  1303. xassert(lp == lp);
  1304. xassert(fname == fname);
  1305. xerror("lpx_write_bas: operation not supported\n");
  1306. return 0;
  1307. }
  1308. LPX *lpx_read_freemps(const char *fname)
  1309. { /* read problem data in free MPS format */
  1310. LPX *lp = lpx_create_prob();
  1311. if (glp_read_mps(lp, GLP_MPS_FILE, NULL, fname))
  1312. lpx_delete_prob(lp), lp = NULL;
  1313. return lp;
  1314. }
  1315. int lpx_write_freemps(LPX *lp, const char *fname)
  1316. { /* write problem data in free MPS format */
  1317. return glp_write_mps(lp, GLP_MPS_FILE, NULL, fname);
  1318. }
  1319. LPX *lpx_read_cpxlp(const char *fname)
  1320. { /* read problem data in CPLEX LP format */
  1321. LPX *lp;
  1322. lp = lpx_create_prob();
  1323. if (glp_read_lp(lp, NULL, fname))
  1324. lpx_delete_prob(lp), lp = NULL;
  1325. return lp;
  1326. }
  1327. int lpx_write_cpxlp(LPX *lp, const char *fname)
  1328. { /* write problem data in CPLEX LP format */
  1329. return glp_write_lp(lp, NULL, fname);
  1330. }
  1331. LPX *lpx_read_model(const char *model, const char *data, const char
  1332. *output)
  1333. { /* read LP/MIP model written in GNU MathProg language */
  1334. LPX *lp = NULL;
  1335. glp_tran *tran;
  1336. /* allocate the translator workspace */
  1337. tran = glp_mpl_alloc_wksp();
  1338. /* read model section and optional data section */
  1339. if (glp_mpl_read_model(tran, model, data != NULL)) goto done;
  1340. /* read separate data section, if required */
  1341. if (data != NULL)
  1342. if (glp_mpl_read_data(tran, data)) goto done;
  1343. /* generate the model */
  1344. if (glp_mpl_generate(tran, output)) goto done;
  1345. /* build the problem instance from the model */
  1346. lp = lpx_create_prob();
  1347. glp_mpl_build_prob(tran, lp);
  1348. done: /* free the translator workspace */
  1349. glp_mpl_free_wksp(tran);
  1350. /* bring the problem object to the calling program */
  1351. return lp;
  1352. }
  1353. int lpx_print_prob(LPX *lp, const char *fname)
  1354. { /* write problem data in plain text format */
  1355. return glp_write_lp(lp, NULL, fname);
  1356. }
  1357. int lpx_print_sol(LPX *lp, const char *fname)
  1358. { /* write LP problem solution in printable format */
  1359. return glp_print_sol(lp, fname);
  1360. }
  1361. int lpx_print_sens_bnds(LPX *lp, const char *fname)
  1362. { /* write bounds sensitivity information */
  1363. if (glp_get_status(lp) == GLP_OPT && !glp_bf_exists(lp))
  1364. glp_factorize(lp);
  1365. return glp_print_ranges(lp, 0, NULL, 0, fname);
  1366. }
  1367. int lpx_print_ips(LPX *lp, const char *fname)
  1368. { /* write interior point solution in printable format */
  1369. return glp_print_ipt(lp, fname);
  1370. }
  1371. int lpx_print_mip(LPX *lp, const char *fname)
  1372. { /* write MIP problem solution in printable format */
  1373. return glp_print_mip(lp, fname);
  1374. }
  1375. int lpx_is_b_avail(glp_prob *lp)
  1376. { /* check if LP basis is available */
  1377. return glp_bf_exists(lp);
  1378. }
  1379. int lpx_main(int argc, const char *argv[])
  1380. { /* stand-alone LP/MIP solver */
  1381. return glp_main(argc, argv);
  1382. }
  1383. /* eof */