glplpx01.c 48 KB

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