glpmps.c 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402
  1. /* glpmps.c (MPS format 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. /***********************************************************************
  25. * NAME
  26. *
  27. * glp_init_mpscp - initialize MPS format control parameters
  28. *
  29. * SYNOPSIS
  30. *
  31. * void glp_init_mpscp(glp_mpscp *parm);
  32. *
  33. * DESCRIPTION
  34. *
  35. * The routine glp_init_mpscp initializes control parameters, which are
  36. * used by the MPS input/output routines glp_read_mps and glp_write_mps,
  37. * with default values.
  38. *
  39. * Default values of the control parameters are stored in the glp_mpscp
  40. * structure, which the parameter parm points to. */
  41. void glp_init_mpscp(glp_mpscp *parm)
  42. { parm->blank = '\0';
  43. parm->obj_name = NULL;
  44. parm->tol_mps = 1e-12;
  45. return;
  46. }
  47. static void check_parm(const char *func, const glp_mpscp *parm)
  48. { /* check control parameters */
  49. if (!(0x00 <= parm->blank && parm->blank <= 0xFF) ||
  50. !(parm->blank == '\0' || isprint(parm->blank)))
  51. xerror("%s: blank = 0x%02X; invalid parameter\n",
  52. func, parm->blank);
  53. if (!(parm->obj_name == NULL || strlen(parm->obj_name) <= 255))
  54. xerror("%s: obj_name = \"%.12s...\"; parameter too long\n",
  55. func, parm->obj_name);
  56. if (!(0.0 <= parm->tol_mps && parm->tol_mps < 1.0))
  57. xerror("%s: tol_mps = %g; invalid parameter\n",
  58. func, parm->tol_mps);
  59. return;
  60. }
  61. /***********************************************************************
  62. * NAME
  63. *
  64. * glp_read_mps - read problem data in MPS format
  65. *
  66. * SYNOPSIS
  67. *
  68. * int glp_read_mps(glp_prob *P, int fmt, const glp_mpscp *parm,
  69. * const char *fname);
  70. *
  71. * DESCRIPTION
  72. *
  73. * The routine glp_read_mps reads problem data in MPS format from a
  74. * text file.
  75. *
  76. * The parameter fmt specifies the version of MPS format:
  77. *
  78. * GLP_MPS_DECK - fixed (ancient) MPS format;
  79. * GLP_MPS_FILE - free (modern) MPS format.
  80. *
  81. * The parameter parm is a pointer to the structure glp_mpscp, which
  82. * specifies control parameters used by the routine. If parm is NULL,
  83. * the routine uses default settings.
  84. *
  85. * The character string fname specifies a name of the text file to be
  86. * read.
  87. *
  88. * Note that before reading data the current content of the problem
  89. * object is completely erased with the routine glp_erase_prob.
  90. *
  91. * RETURNS
  92. *
  93. * If the operation was successful, the routine glp_read_mps returns
  94. * zero. Otherwise, it prints an error message and returns non-zero. */
  95. struct csa
  96. { /* common storage area */
  97. glp_prob *P;
  98. /* pointer to problem object */
  99. int deck;
  100. /* MPS format (0 - free, 1 - fixed) */
  101. const glp_mpscp *parm;
  102. /* pointer to control parameters */
  103. const char *fname;
  104. /* name of input MPS file */
  105. XFILE *fp;
  106. /* stream assigned to input MPS file */
  107. jmp_buf jump;
  108. /* label for go to in case of error */
  109. int recno;
  110. /* current record (card) number */
  111. int recpos;
  112. /* current record (card) position */
  113. int c;
  114. /* current character */
  115. int fldno;
  116. /* current field number */
  117. char field[255+1];
  118. /* current field content */
  119. int w80;
  120. /* warning 'record must not be longer than 80 chars' issued */
  121. int wef;
  122. /* warning 'extra fields detected beyond field 6' issued */
  123. int obj_row;
  124. /* objective row number */
  125. void *work1, *work2, *work3;
  126. /* working arrays */
  127. };
  128. static void error(struct csa *csa, const char *fmt, ...)
  129. { /* print error message and terminate processing */
  130. va_list arg;
  131. xprintf("%s:%d: ", csa->fname, csa->recno);
  132. va_start(arg, fmt);
  133. xvprintf(fmt, arg);
  134. va_end(arg);
  135. longjmp(csa->jump, 1);
  136. /* no return */
  137. }
  138. static void warning(struct csa *csa, const char *fmt, ...)
  139. { /* print warning message and continue processing */
  140. va_list arg;
  141. xprintf("%s:%d: warning: ", csa->fname, csa->recno);
  142. va_start(arg, fmt);
  143. xvprintf(fmt, arg);
  144. va_end(arg);
  145. return;
  146. }
  147. static void read_char(struct csa *csa)
  148. { /* read next character */
  149. int c;
  150. if (csa->c == '\n')
  151. csa->recno++, csa->recpos = 0;
  152. csa->recpos++;
  153. read: c = xfgetc(csa->fp);
  154. if (c < 0)
  155. { if (xferror(csa->fp))
  156. error(csa, "read error - %s\n", xerrmsg());
  157. else if (csa->c == '\n')
  158. error(csa, "unexpected end of file\n");
  159. else
  160. { warning(csa, "missing final end of line\n");
  161. c = '\n';
  162. }
  163. }
  164. else if (c == '\n')
  165. ;
  166. else if (csa->c == '\r')
  167. { c = '\r';
  168. goto badc;
  169. }
  170. else if (csa->deck && c == '\r')
  171. { csa->c = '\r';
  172. goto read;
  173. }
  174. else if (c == ' ')
  175. ;
  176. else if (isspace(c))
  177. { if (csa->deck)
  178. badc: error(csa, "in fixed MPS format white-space character 0x%02"
  179. "X is not allowed\n", c);
  180. c = ' ';
  181. }
  182. else if (iscntrl(c))
  183. error(csa, "invalid control character 0x%02X\n", c);
  184. if (csa->deck && csa->recpos == 81 && c != '\n' && csa->w80 < 1)
  185. { warning(csa, "in fixed MPS format record must not be longer th"
  186. "an 80 characters\n");
  187. csa->w80++;
  188. }
  189. csa->c = c;
  190. return;
  191. }
  192. static int indicator(struct csa *csa, int name)
  193. { /* skip comment records and read possible indicator record */
  194. int ret;
  195. /* reset current field number */
  196. csa->fldno = 0;
  197. loop: /* read the very first character of the next record */
  198. xassert(csa->c == '\n');
  199. read_char(csa);
  200. if (csa->c == ' ' || csa->c == '\n')
  201. { /* data record */
  202. ret = 0;
  203. }
  204. else if (csa->c == '*')
  205. { /* comment record */
  206. while (csa->c != '\n')
  207. read_char(csa);
  208. goto loop;
  209. }
  210. else
  211. { /* indicator record */
  212. int len = 0;
  213. while (csa->c != ' ' && csa->c != '\n' && len < 12)
  214. { csa->field[len++] = (char)csa->c;
  215. read_char(csa);
  216. }
  217. csa->field[len] = '\0';
  218. if (!(strcmp(csa->field, "NAME") == 0 ||
  219. strcmp(csa->field, "ROWS") == 0 ||
  220. strcmp(csa->field, "COLUMNS") == 0 ||
  221. strcmp(csa->field, "RHS") == 0 ||
  222. strcmp(csa->field, "RANGES") == 0 ||
  223. strcmp(csa->field, "BOUNDS") == 0 ||
  224. strcmp(csa->field, "ENDATA") == 0))
  225. error(csa, "invalid indicator record\n");
  226. if (!name)
  227. { while (csa->c != '\n')
  228. read_char(csa);
  229. }
  230. ret = 1;
  231. }
  232. return ret;
  233. }
  234. static void read_field(struct csa *csa)
  235. { /* read next field of the current data record */
  236. csa->fldno++;
  237. if (csa->deck)
  238. { /* fixed MPS format */
  239. int beg, end, pos;
  240. /* determine predefined field positions */
  241. if (csa->fldno == 1)
  242. beg = 2, end = 3;
  243. else if (csa->fldno == 2)
  244. beg = 5, end = 12;
  245. else if (csa->fldno == 3)
  246. beg = 15, end = 22;
  247. else if (csa->fldno == 4)
  248. beg = 25, end = 36;
  249. else if (csa->fldno == 5)
  250. beg = 40, end = 47;
  251. else if (csa->fldno == 6)
  252. beg = 50, end = 61;
  253. else
  254. xassert(csa != csa);
  255. /* skip blanks preceding the current field */
  256. if (csa->c != '\n')
  257. { pos = csa->recpos;
  258. while (csa->recpos < beg)
  259. { if (csa->c == ' ')
  260. ;
  261. else if (csa->c == '\n')
  262. break;
  263. else
  264. error(csa, "in fixed MPS format positions %d-%d must "
  265. "be blank\n", pos, beg-1);
  266. read_char(csa);
  267. }
  268. }
  269. /* skip possible comment beginning in the field 3 or 5 */
  270. if ((csa->fldno == 3 || csa->fldno == 5) && csa->c == '$')
  271. { while (csa->c != '\n')
  272. read_char(csa);
  273. }
  274. /* read the current field */
  275. for (pos = beg; pos <= end; pos++)
  276. { if (csa->c == '\n') break;
  277. csa->field[pos-beg] = (char)csa->c;
  278. read_char(csa);
  279. }
  280. csa->field[pos-beg] = '\0';
  281. strtrim(csa->field);
  282. /* skip blanks following the last field */
  283. if (csa->fldno == 6 && csa->c != '\n')
  284. { while (csa->recpos <= 72)
  285. { if (csa->c == ' ')
  286. ;
  287. else if (csa->c == '\n')
  288. break;
  289. else
  290. error(csa, "in fixed MPS format positions 62-72 must "
  291. "be blank\n");
  292. read_char(csa);
  293. }
  294. while (csa->c != '\n')
  295. read_char(csa);
  296. }
  297. }
  298. else
  299. { /* free MPS format */
  300. int len;
  301. /* skip blanks preceding the current field */
  302. while (csa->c == ' ')
  303. read_char(csa);
  304. /* skip possible comment */
  305. if (csa->c == '$')
  306. { while (csa->c != '\n')
  307. read_char(csa);
  308. }
  309. /* read the current field */
  310. len = 0;
  311. while (!(csa->c == ' ' || csa->c == '\n'))
  312. { if (len == 255)
  313. error(csa, "length of field %d exceeds 255 characters\n",
  314. csa->fldno++);
  315. csa->field[len++] = (char)csa->c;
  316. read_char(csa);
  317. }
  318. csa->field[len] = '\0';
  319. /* skip anything following the last field (any extra fields
  320. are considered to be comments) */
  321. if (csa->fldno == 6)
  322. { while (csa->c == ' ')
  323. read_char(csa);
  324. if (csa->c != '$' && csa->c != '\n' && csa->wef < 1)
  325. { warning(csa, "some extra field(s) detected beyond field "
  326. "6; field(s) ignored\n");
  327. csa->wef++;
  328. }
  329. while (csa->c != '\n')
  330. read_char(csa);
  331. }
  332. }
  333. return;
  334. }
  335. static void patch_name(struct csa *csa, char *name)
  336. { /* process embedded blanks in symbolic name */
  337. int blank = csa->parm->blank;
  338. if (blank == '\0')
  339. { /* remove emedded blanks */
  340. strspx(name);
  341. }
  342. else
  343. { /* replace embedded blanks by specified character */
  344. for (; *name != '\0'; name++)
  345. if (*name == ' ') *name = (char)blank;
  346. }
  347. return;
  348. }
  349. static double read_number(struct csa *csa)
  350. { /* read next field and convert it to floating-point number */
  351. double x;
  352. char *s;
  353. /* read next field */
  354. read_field(csa);
  355. xassert(csa->fldno == 4 || csa->fldno == 6);
  356. if (csa->field[0] == '\0')
  357. error(csa, "missing numeric value in field %d\n", csa->fldno);
  358. /* skip initial spaces of the field */
  359. for (s = csa->field; *s == ' '; s++) ;
  360. /* perform conversion */
  361. if (str2num(s, &x) != 0)
  362. error(csa, "cannot convert `%s' to floating-point number\n",
  363. s);
  364. return x;
  365. }
  366. static void skip_field(struct csa *csa)
  367. { /* read and skip next field (assumed to be blank) */
  368. read_field(csa);
  369. if (csa->field[0] != '\0')
  370. error(csa, "field %d must be blank\n", csa->fldno);
  371. return;
  372. }
  373. static void read_name(struct csa *csa)
  374. { /* read NAME indicator record */
  375. if (!(indicator(csa, 1) && strcmp(csa->field, "NAME") == 0))
  376. error(csa, "missing NAME indicator record\n");
  377. /* this indicator record looks like a data record; simulate that
  378. fields 1 and 2 were read */
  379. csa->fldno = 2;
  380. /* field 3: model name */
  381. read_field(csa), patch_name(csa, csa->field);
  382. if (csa->field[0] == '\0')
  383. warning(csa, "missing model name in field 3\n");
  384. else
  385. glp_set_prob_name(csa->P, csa->field);
  386. /* skip anything following field 3 */
  387. while (csa->c != '\n')
  388. read_char(csa);
  389. return;
  390. }
  391. static void read_rows(struct csa *csa)
  392. { /* read ROWS section */
  393. int i, type;
  394. loop: if (indicator(csa, 0)) goto done;
  395. /* field 1: row type */
  396. read_field(csa), strspx(csa->field);
  397. if (strcmp(csa->field, "N") == 0)
  398. type = GLP_FR;
  399. else if (strcmp(csa->field, "G") == 0)
  400. type = GLP_LO;
  401. else if (strcmp(csa->field, "L") == 0)
  402. type = GLP_UP;
  403. else if (strcmp(csa->field, "E") == 0)
  404. type = GLP_FX;
  405. else if (csa->field[0] == '\0')
  406. error(csa, "missing row type in field 1\n");
  407. else
  408. error(csa, "invalid row type in field 1\n");
  409. /* field 2: row name */
  410. read_field(csa), patch_name(csa, csa->field);
  411. if (csa->field[0] == '\0')
  412. error(csa, "missing row name in field 2\n");
  413. if (glp_find_row(csa->P, csa->field) != 0)
  414. error(csa, "row `%s' multiply specified\n", csa->field);
  415. i = glp_add_rows(csa->P, 1);
  416. glp_set_row_name(csa->P, i, csa->field);
  417. glp_set_row_bnds(csa->P, i, type, 0.0, 0.0);
  418. /* fields 3, 4, 5, and 6 must be blank */
  419. skip_field(csa);
  420. skip_field(csa);
  421. skip_field(csa);
  422. skip_field(csa);
  423. goto loop;
  424. done: return;
  425. }
  426. static void read_columns(struct csa *csa)
  427. { /* read COLUMNS section */
  428. int i, j, f, len, kind = GLP_CV, *ind;
  429. double aij, *val;
  430. char name[255+1], *flag;
  431. /* allocate working arrays */
  432. csa->work1 = ind = xcalloc(1+csa->P->m, sizeof(int));
  433. csa->work2 = val = xcalloc(1+csa->P->m, sizeof(double));
  434. csa->work3 = flag = xcalloc(1+csa->P->m, sizeof(char));
  435. memset(&flag[1], 0, csa->P->m);
  436. /* no current column exists */
  437. j = 0, len = 0;
  438. loop: if (indicator(csa, 0)) goto done;
  439. /* field 1 must be blank */
  440. if (csa->deck)
  441. { read_field(csa);
  442. if (csa->field[0] != '\0')
  443. error(csa, "field 1 must be blank\n");
  444. }
  445. else
  446. csa->fldno++;
  447. /* field 2: column or kind name */
  448. read_field(csa), patch_name(csa, csa->field);
  449. strcpy(name, csa->field);
  450. /* field 3: row name or keyword 'MARKER' */
  451. read_field(csa), patch_name(csa, csa->field);
  452. if (strcmp(csa->field, "'MARKER'") == 0)
  453. { /* process kind data record */
  454. /* field 4 must be blank */
  455. if (csa->deck)
  456. { read_field(csa);
  457. if (csa->field[0] != '\0')
  458. error(csa, "field 4 must be blank\n");
  459. }
  460. else
  461. csa->fldno++;
  462. /* field 5: keyword 'INTORG' or 'INTEND' */
  463. read_field(csa), patch_name(csa, csa->field);
  464. if (strcmp(csa->field, "'INTORG'") == 0)
  465. kind = GLP_IV;
  466. else if (strcmp(csa->field, "'INTEND'") == 0)
  467. kind = GLP_CV;
  468. else if (csa->field[0] == '\0')
  469. error(csa, "missing keyword in field 5\n");
  470. else
  471. error(csa, "invalid keyword in field 5\n");
  472. /* field 6 must be blank */
  473. skip_field(csa);
  474. goto loop;
  475. }
  476. /* process column name specified in field 2 */
  477. if (name[0] == '\0')
  478. { /* the same column as in previous data record */
  479. if (j == 0)
  480. error(csa, "missing column name in field 2\n");
  481. }
  482. else if (j != 0 && strcmp(name, csa->P->col[j]->name) == 0)
  483. { /* the same column as in previous data record */
  484. xassert(j != 0);
  485. }
  486. else
  487. { /* store the current column */
  488. if (j != 0)
  489. { glp_set_mat_col(csa->P, j, len, ind, val);
  490. while (len > 0) flag[ind[len--]] = 0;
  491. }
  492. /* create new column */
  493. if (glp_find_col(csa->P, name) != 0)
  494. error(csa, "column `%s' multiply specified\n", name);
  495. j = glp_add_cols(csa->P, 1);
  496. glp_set_col_name(csa->P, j, name);
  497. glp_set_col_kind(csa->P, j, kind);
  498. if (kind == GLP_CV)
  499. glp_set_col_bnds(csa->P, j, GLP_LO, 0.0, 0.0);
  500. else if (kind == GLP_IV)
  501. glp_set_col_bnds(csa->P, j, GLP_DB, 0.0, 1.0);
  502. else
  503. xassert(kind != kind);
  504. }
  505. /* process fields 3-4 and 5-6 */
  506. for (f = 3; f <= 5; f += 2)
  507. { /* field 3 or 5: row name */
  508. if (f == 3)
  509. { if (csa->field[0] == '\0')
  510. error(csa, "missing row name in field 3\n");
  511. }
  512. else
  513. { read_field(csa), patch_name(csa, csa->field);
  514. if (csa->field[0] == '\0')
  515. { /* if field 5 is blank, field 6 also must be blank */
  516. skip_field(csa);
  517. continue;
  518. }
  519. }
  520. i = glp_find_row(csa->P, csa->field);
  521. if (i == 0)
  522. error(csa, "row `%s' not found\n", csa->field);
  523. if (flag[i])
  524. error(csa, "duplicate coefficient in row `%s'\n",
  525. csa->field);
  526. /* field 4 or 6: coefficient value */
  527. aij = read_number(csa);
  528. if (fabs(aij) < csa->parm->tol_mps) aij = 0.0;
  529. len++, ind[len] = i, val[len] = aij, flag[i] = 1;
  530. }
  531. goto loop;
  532. done: /* store the last column */
  533. if (j != 0)
  534. glp_set_mat_col(csa->P, j, len, ind, val);
  535. /* free working arrays */
  536. xfree(ind);
  537. xfree(val);
  538. xfree(flag);
  539. csa->work1 = csa->work2 = csa->work3 = NULL;
  540. return;
  541. }
  542. static void read_rhs(struct csa *csa)
  543. { /* read RHS section */
  544. int i, f, v, type;
  545. double rhs;
  546. char name[255+1], *flag;
  547. /* allocate working array */
  548. csa->work3 = flag = xcalloc(1+csa->P->m, sizeof(char));
  549. memset(&flag[1], 0, csa->P->m);
  550. /* no current RHS vector exists */
  551. v = 0;
  552. loop: if (indicator(csa, 0)) goto done;
  553. /* field 1 must be blank */
  554. if (csa->deck)
  555. { read_field(csa);
  556. if (csa->field[0] != '\0')
  557. error(csa, "field 1 must be blank\n");
  558. }
  559. else
  560. csa->fldno++;
  561. /* field 2: RHS vector name */
  562. read_field(csa), patch_name(csa, csa->field);
  563. if (csa->field[0] == '\0')
  564. { /* the same RHS vector as in previous data record */
  565. if (v == 0)
  566. { warning(csa, "missing RHS vector name in field 2\n");
  567. goto blnk;
  568. }
  569. }
  570. else if (v != 0 && strcmp(csa->field, name) == 0)
  571. { /* the same RHS vector as in previous data record */
  572. xassert(v != 0);
  573. }
  574. else
  575. blnk: { /* new RHS vector */
  576. if (v != 0)
  577. error(csa, "multiple RHS vectors not supported\n");
  578. v++;
  579. strcpy(name, csa->field);
  580. }
  581. /* process fields 3-4 and 5-6 */
  582. for (f = 3; f <= 5; f += 2)
  583. { /* field 3 or 5: row name */
  584. read_field(csa), patch_name(csa, csa->field);
  585. if (csa->field[0] == '\0')
  586. { if (f == 3)
  587. error(csa, "missing row name in field 3\n");
  588. else
  589. { /* if field 5 is blank, field 6 also must be blank */
  590. skip_field(csa);
  591. continue;
  592. }
  593. }
  594. i = glp_find_row(csa->P, csa->field);
  595. if (i == 0)
  596. error(csa, "row `%s' not found\n", csa->field);
  597. if (flag[i])
  598. error(csa, "duplicate right-hand side for row `%s'\n",
  599. csa->field);
  600. /* field 4 or 6: right-hand side value */
  601. rhs = read_number(csa);
  602. if (fabs(rhs) < csa->parm->tol_mps) rhs = 0.0;
  603. type = csa->P->row[i]->type;
  604. if (type == GLP_FR)
  605. { if (i == csa->obj_row)
  606. glp_set_obj_coef(csa->P, 0, rhs);
  607. else if (rhs != 0.0)
  608. warning(csa, "non-zero right-hand side for free row `%s'"
  609. " ignored\n", csa->P->row[i]->name);
  610. }
  611. else
  612. glp_set_row_bnds(csa->P, i, type, rhs, rhs);
  613. flag[i] = 1;
  614. }
  615. goto loop;
  616. done: /* free working array */
  617. xfree(flag);
  618. csa->work3 = NULL;
  619. return;
  620. }
  621. static void read_ranges(struct csa *csa)
  622. { /* read RANGES section */
  623. int i, f, v, type;
  624. double rhs, rng;
  625. char name[255+1], *flag;
  626. /* allocate working array */
  627. csa->work3 = flag = xcalloc(1+csa->P->m, sizeof(char));
  628. memset(&flag[1], 0, csa->P->m);
  629. /* no current RANGES vector exists */
  630. v = 0;
  631. loop: if (indicator(csa, 0)) goto done;
  632. /* field 1 must be blank */
  633. if (csa->deck)
  634. { read_field(csa);
  635. if (csa->field[0] != '\0')
  636. error(csa, "field 1 must be blank\n");
  637. }
  638. else
  639. csa->fldno++;
  640. /* field 2: RANGES vector name */
  641. read_field(csa), patch_name(csa, csa->field);
  642. if (csa->field[0] == '\0')
  643. { /* the same RANGES vector as in previous data record */
  644. if (v == 0)
  645. { warning(csa, "missing RANGES vector name in field 2\n");
  646. goto blnk;
  647. }
  648. }
  649. else if (v != 0 && strcmp(csa->field, name) == 0)
  650. { /* the same RANGES vector as in previous data record */
  651. xassert(v != 0);
  652. }
  653. else
  654. blnk: { /* new RANGES vector */
  655. if (v != 0)
  656. error(csa, "multiple RANGES vectors not supported\n");
  657. v++;
  658. strcpy(name, csa->field);
  659. }
  660. /* process fields 3-4 and 5-6 */
  661. for (f = 3; f <= 5; f += 2)
  662. { /* field 3 or 5: row name */
  663. read_field(csa), patch_name(csa, csa->field);
  664. if (csa->field[0] == '\0')
  665. { if (f == 3)
  666. error(csa, "missing row name in field 3\n");
  667. else
  668. { /* if field 5 is blank, field 6 also must be blank */
  669. skip_field(csa);
  670. continue;
  671. }
  672. }
  673. i = glp_find_row(csa->P, csa->field);
  674. if (i == 0)
  675. error(csa, "row `%s' not found\n", csa->field);
  676. if (flag[i])
  677. error(csa, "duplicate range for row `%s'\n", csa->field);
  678. /* field 4 or 6: range value */
  679. rng = read_number(csa);
  680. if (fabs(rng) < csa->parm->tol_mps) rng = 0.0;
  681. type = csa->P->row[i]->type;
  682. if (type == GLP_FR)
  683. warning(csa, "range for free row `%s' ignored\n",
  684. csa->P->row[i]->name);
  685. else if (type == GLP_LO)
  686. { rhs = csa->P->row[i]->lb;
  687. glp_set_row_bnds(csa->P, i, rhs == 0.0 ? GLP_FX : GLP_DB,
  688. rhs, rhs + fabs(rng));
  689. }
  690. else if (type == GLP_UP)
  691. { rhs = csa->P->row[i]->ub;
  692. glp_set_row_bnds(csa->P, i, rhs == 0.0 ? GLP_FX : GLP_DB,
  693. rhs - fabs(rng), rhs);
  694. }
  695. else if (type == GLP_FX)
  696. { rhs = csa->P->row[i]->lb;
  697. if (rng > 0.0)
  698. glp_set_row_bnds(csa->P, i, GLP_DB, rhs, rhs + rng);
  699. else if (rng < 0.0)
  700. glp_set_row_bnds(csa->P, i, GLP_DB, rhs + rng, rhs);
  701. }
  702. else
  703. xassert(type != type);
  704. flag[i] = 1;
  705. }
  706. goto loop;
  707. done: /* free working array */
  708. xfree(flag);
  709. csa->work3 = NULL;
  710. return;
  711. }
  712. static void read_bounds(struct csa *csa)
  713. { /* read BOUNDS section */
  714. GLPCOL *col;
  715. int j, v, mask, data;
  716. double bnd, lb, ub;
  717. char type[2+1], name[255+1], *flag;
  718. /* allocate working array */
  719. csa->work3 = flag = xcalloc(1+csa->P->n, sizeof(char));
  720. memset(&flag[1], 0, csa->P->n);
  721. /* no current BOUNDS vector exists */
  722. v = 0;
  723. loop: if (indicator(csa, 0)) goto done;
  724. /* field 1: bound type */
  725. read_field(csa);
  726. if (strcmp(csa->field, "LO") == 0)
  727. mask = 0x01, data = 1;
  728. else if (strcmp(csa->field, "UP") == 0)
  729. mask = 0x10, data = 1;
  730. else if (strcmp(csa->field, "FX") == 0)
  731. mask = 0x11, data = 1;
  732. else if (strcmp(csa->field, "FR") == 0)
  733. mask = 0x11, data = 0;
  734. else if (strcmp(csa->field, "MI") == 0)
  735. mask = 0x01, data = 0;
  736. else if (strcmp(csa->field, "PL") == 0)
  737. mask = 0x10, data = 0;
  738. else if (strcmp(csa->field, "LI") == 0)
  739. mask = 0x01, data = 1;
  740. else if (strcmp(csa->field, "UI") == 0)
  741. mask = 0x10, data = 1;
  742. else if (strcmp(csa->field, "BV") == 0)
  743. mask = 0x11, data = 0;
  744. else if (csa->field[0] == '\0')
  745. error(csa, "missing bound type in field 1\n");
  746. else
  747. error(csa, "invalid bound type in field 1\n");
  748. strcpy(type, csa->field);
  749. /* field 2: BOUNDS vector name */
  750. read_field(csa), patch_name(csa, csa->field);
  751. if (csa->field[0] == '\0')
  752. { /* the same BOUNDS vector as in previous data record */
  753. if (v == 0)
  754. { warning(csa, "missing BOUNDS vector name in field 2\n");
  755. goto blnk;
  756. }
  757. }
  758. else if (v != 0 && strcmp(csa->field, name) == 0)
  759. { /* the same BOUNDS vector as in previous data record */
  760. xassert(v != 0);
  761. }
  762. else
  763. blnk: { /* new BOUNDS vector */
  764. if (v != 0)
  765. error(csa, "multiple BOUNDS vectors not supported\n");
  766. v++;
  767. strcpy(name, csa->field);
  768. }
  769. /* field 3: column name */
  770. read_field(csa), patch_name(csa, csa->field);
  771. if (csa->field[0] == '\0')
  772. error(csa, "missing column name in field 3\n");
  773. j = glp_find_col(csa->P, csa->field);
  774. if (j == 0)
  775. error(csa, "column `%s' not found\n", csa->field);
  776. if ((flag[j] & mask) == 0x01)
  777. error(csa, "duplicate lower bound for column `%s'\n",
  778. csa->field);
  779. if ((flag[j] & mask) == 0x10)
  780. error(csa, "duplicate upper bound for column `%s'\n",
  781. csa->field);
  782. xassert((flag[j] & mask) == 0x00);
  783. /* field 4: bound value */
  784. if (data)
  785. { bnd = read_number(csa);
  786. if (fabs(bnd) < csa->parm->tol_mps) bnd = 0.0;
  787. }
  788. else
  789. read_field(csa), bnd = 0.0;
  790. /* get current column bounds */
  791. col = csa->P->col[j];
  792. if (col->type == GLP_FR)
  793. lb = -DBL_MAX, ub = +DBL_MAX;
  794. else if (col->type == GLP_LO)
  795. lb = col->lb, ub = +DBL_MAX;
  796. else if (col->type == GLP_UP)
  797. lb = -DBL_MAX, ub = col->ub;
  798. else if (col->type == GLP_DB)
  799. lb = col->lb, ub = col->ub;
  800. else if (col->type == GLP_FX)
  801. lb = ub = col->lb;
  802. else
  803. xassert(col != col);
  804. /* change column bounds */
  805. if (strcmp(type, "LO") == 0)
  806. lb = bnd;
  807. else if (strcmp(type, "UP") == 0)
  808. ub = bnd;
  809. else if (strcmp(type, "FX") == 0)
  810. lb = ub = bnd;
  811. else if (strcmp(type, "FR") == 0)
  812. lb = -DBL_MAX, ub = +DBL_MAX;
  813. else if (strcmp(type, "MI") == 0)
  814. lb = -DBL_MAX;
  815. else if (strcmp(type, "PL") == 0)
  816. ub = +DBL_MAX;
  817. else if (strcmp(type, "LI") == 0)
  818. { glp_set_col_kind(csa->P, j, GLP_IV);
  819. lb = ceil(bnd);
  820. }
  821. else if (strcmp(type, "UI") == 0)
  822. { glp_set_col_kind(csa->P, j, GLP_IV);
  823. ub = floor(bnd);
  824. }
  825. else if (strcmp(type, "BV") == 0)
  826. { glp_set_col_kind(csa->P, j, GLP_IV);
  827. lb = 0.0, ub = 1.0;
  828. }
  829. else
  830. xassert(type != type);
  831. /* set new column bounds */
  832. if (lb == -DBL_MAX && ub == +DBL_MAX)
  833. glp_set_col_bnds(csa->P, j, GLP_FR, lb, ub);
  834. else if (ub == +DBL_MAX)
  835. glp_set_col_bnds(csa->P, j, GLP_LO, lb, ub);
  836. else if (lb == -DBL_MAX)
  837. glp_set_col_bnds(csa->P, j, GLP_UP, lb, ub);
  838. else if (lb != ub)
  839. glp_set_col_bnds(csa->P, j, GLP_DB, lb, ub);
  840. else
  841. glp_set_col_bnds(csa->P, j, GLP_FX, lb, ub);
  842. flag[j] |= (char)mask;
  843. /* fields 5 and 6 must be blank */
  844. skip_field(csa);
  845. skip_field(csa);
  846. goto loop;
  847. done: /* free working array */
  848. xfree(flag);
  849. csa->work3 = NULL;
  850. return;
  851. }
  852. int glp_read_mps(glp_prob *P, int fmt, const glp_mpscp *parm,
  853. const char *fname)
  854. { /* read problem data in MPS format */
  855. glp_mpscp _parm;
  856. struct csa _csa, *csa = &_csa;
  857. int ret;
  858. xprintf("Reading problem data from `%s'...\n", fname);
  859. if (!(fmt == GLP_MPS_DECK || fmt == GLP_MPS_FILE))
  860. xerror("glp_read_mps: fmt = %d; invalid parameter\n", fmt);
  861. if (parm == NULL)
  862. glp_init_mpscp(&_parm), parm = &_parm;
  863. /* check control parameters */
  864. check_parm("glp_read_mps", parm);
  865. /* initialize common storage area */
  866. csa->P = P;
  867. csa->deck = (fmt == GLP_MPS_DECK);
  868. csa->parm = parm;
  869. csa->fname = fname;
  870. csa->fp = NULL;
  871. if (setjmp(csa->jump))
  872. { ret = 1;
  873. goto done;
  874. }
  875. csa->recno = csa->recpos = 0;
  876. csa->c = '\n';
  877. csa->fldno = 0;
  878. csa->field[0] = '\0';
  879. csa->w80 = csa->wef = 0;
  880. csa->obj_row = 0;
  881. csa->work1 = csa->work2 = csa->work3 = NULL;
  882. /* erase problem object */
  883. glp_erase_prob(P);
  884. glp_create_index(P);
  885. /* open input MPS file */
  886. csa->fp = xfopen(fname, "r");
  887. if (csa->fp == NULL)
  888. { xprintf("Unable to open `%s' - %s\n", fname, xerrmsg());
  889. ret = 1;
  890. goto done;
  891. }
  892. /* read NAME indicator record */
  893. read_name(csa);
  894. if (P->name != NULL)
  895. xprintf("Problem: %s\n", P->name);
  896. /* read ROWS section */
  897. if (!(indicator(csa, 0) && strcmp(csa->field, "ROWS") == 0))
  898. error(csa, "missing ROWS indicator record\n");
  899. read_rows(csa);
  900. /* determine objective row */
  901. if (parm->obj_name == NULL || parm->obj_name[0] == '\0')
  902. { /* use the first row of N type */
  903. int i;
  904. for (i = 1; i <= P->m; i++)
  905. { if (P->row[i]->type == GLP_FR)
  906. { csa->obj_row = i;
  907. break;
  908. }
  909. }
  910. if (csa->obj_row == 0)
  911. warning(csa, "unable to determine objective row\n");
  912. }
  913. else
  914. { /* use a row with specified name */
  915. int i;
  916. for (i = 1; i <= P->m; i++)
  917. { xassert(P->row[i]->name != NULL);
  918. if (strcmp(parm->obj_name, P->row[i]->name) == 0)
  919. { csa->obj_row = i;
  920. break;
  921. }
  922. }
  923. if (csa->obj_row == 0)
  924. error(csa, "objective row `%s' not found\n",
  925. parm->obj_name);
  926. }
  927. if (csa->obj_row != 0)
  928. { glp_set_obj_name(P, P->row[csa->obj_row]->name);
  929. xprintf("Objective: %s\n", P->obj);
  930. }
  931. /* read COLUMNS section */
  932. if (strcmp(csa->field, "COLUMNS") != 0)
  933. error(csa, "missing COLUMNS indicator record\n");
  934. read_columns(csa);
  935. /* set objective coefficients */
  936. if (csa->obj_row != 0)
  937. { GLPAIJ *aij;
  938. for (aij = P->row[csa->obj_row]->ptr; aij != NULL; aij =
  939. aij->r_next) glp_set_obj_coef(P, aij->col->j, aij->val);
  940. }
  941. /* read optional RHS section */
  942. if (strcmp(csa->field, "RHS") == 0)
  943. read_rhs(csa);
  944. /* read optional RANGES section */
  945. if (strcmp(csa->field, "RANGES") == 0)
  946. read_ranges(csa);
  947. /* read optional BOUNDS section */
  948. if (strcmp(csa->field, "BOUNDS") == 0)
  949. read_bounds(csa);
  950. /* read ENDATA indicator record */
  951. if (strcmp(csa->field, "ENDATA") != 0)
  952. error(csa, "invalid use of %s indicator record\n",
  953. csa->field);
  954. /* print some statistics */
  955. xprintf("%d row%s, %d column%s, %d non-zero%s\n",
  956. P->m, P->m == 1 ? "" : "s", P->n, P->n == 1 ? "" : "s",
  957. P->nnz, P->nnz == 1 ? "" : "s");
  958. if (glp_get_num_int(P) > 0)
  959. { int ni = glp_get_num_int(P);
  960. int nb = glp_get_num_bin(P);
  961. if (ni == 1)
  962. { if (nb == 0)
  963. xprintf("One variable is integer\n");
  964. else
  965. xprintf("One variable is binary\n");
  966. }
  967. else
  968. { xprintf("%d integer variables, ", ni);
  969. if (nb == 0)
  970. xprintf("none");
  971. else if (nb == 1)
  972. xprintf("one");
  973. else if (nb == ni)
  974. xprintf("all");
  975. else
  976. xprintf("%d", nb);
  977. xprintf(" of which %s binary\n", nb == 1 ? "is" : "are");
  978. }
  979. }
  980. xprintf("%d records were read\n", csa->recno);
  981. /* problem data has been successfully read */
  982. glp_delete_index(P);
  983. glp_sort_matrix(P);
  984. ret = 0;
  985. done: if (csa->fp != NULL) xfclose(csa->fp);
  986. if (csa->work1 != NULL) xfree(csa->work1);
  987. if (csa->work2 != NULL) xfree(csa->work2);
  988. if (csa->work3 != NULL) xfree(csa->work3);
  989. if (ret != 0) glp_erase_prob(P);
  990. return ret;
  991. }
  992. /***********************************************************************
  993. * NAME
  994. *
  995. * glp_write_mps - write problem data in MPS format
  996. *
  997. * SYNOPSIS
  998. *
  999. * int glp_write_mps(glp_prob *P, int fmt, const glp_mpscp *parm,
  1000. * const char *fname);
  1001. *
  1002. * DESCRIPTION
  1003. *
  1004. * The routine glp_write_mps writes problem data in MPS format to a
  1005. * text file.
  1006. *
  1007. * The parameter fmt specifies the version of MPS format:
  1008. *
  1009. * GLP_MPS_DECK - fixed (ancient) MPS format;
  1010. * GLP_MPS_FILE - free (modern) MPS format.
  1011. *
  1012. * The parameter parm is a pointer to the structure glp_mpscp, which
  1013. * specifies control parameters used by the routine. If parm is NULL,
  1014. * the routine uses default settings.
  1015. *
  1016. * The character string fname specifies a name of the text file to be
  1017. * written.
  1018. *
  1019. * RETURNS
  1020. *
  1021. * If the operation was successful, the routine glp_read_mps returns
  1022. * zero. Otherwise, it prints an error message and returns non-zero. */
  1023. #define csa csa1
  1024. struct csa
  1025. { /* common storage area */
  1026. glp_prob *P;
  1027. /* pointer to problem object */
  1028. int deck;
  1029. /* MPS format (0 - free, 1 - fixed) */
  1030. const glp_mpscp *parm;
  1031. /* pointer to control parameters */
  1032. char field[255+1];
  1033. /* field buffer */
  1034. };
  1035. static char *mps_name(struct csa *csa)
  1036. { /* make problem name */
  1037. char *f;
  1038. if (csa->P->name == NULL)
  1039. csa->field[0] = '\0';
  1040. else if (csa->deck)
  1041. { strncpy(csa->field, csa->P->name, 8);
  1042. csa->field[8] = '\0';
  1043. }
  1044. else
  1045. strcpy(csa->field, csa->P->name);
  1046. for (f = csa->field; *f != '\0'; f++)
  1047. if (*f == ' ') *f = '_';
  1048. return csa->field;
  1049. }
  1050. static char *row_name(struct csa *csa, int i)
  1051. { /* make i-th row name */
  1052. char *f;
  1053. xassert(0 <= i && i <= csa->P->m);
  1054. if (i == 0 || csa->P->row[i]->name == NULL ||
  1055. csa->deck && strlen(csa->P->row[i]->name) > 8)
  1056. sprintf(csa->field, "R%07d", i);
  1057. else
  1058. { strcpy(csa->field, csa->P->row[i]->name);
  1059. for (f = csa->field; *f != '\0'; f++)
  1060. if (*f == ' ') *f = '_';
  1061. }
  1062. return csa->field;
  1063. }
  1064. static char *col_name(struct csa *csa, int j)
  1065. { /* make j-th column name */
  1066. char *f;
  1067. xassert(1 <= j && j <= csa->P->n);
  1068. if (csa->P->col[j]->name == NULL ||
  1069. csa->deck && strlen(csa->P->col[j]->name) > 8)
  1070. sprintf(csa->field, "C%07d", j);
  1071. else
  1072. { strcpy(csa->field, csa->P->col[j]->name);
  1073. for (f = csa->field; *f != '\0'; f++)
  1074. if (*f == ' ') *f = '_';
  1075. }
  1076. return csa->field;
  1077. }
  1078. static char *mps_numb(struct csa *csa, double val)
  1079. { /* format floating-point number */
  1080. int dig;
  1081. char *exp;
  1082. for (dig = 12; dig >= 6; dig--)
  1083. { if (val != 0.0 && fabs(val) < 0.002)
  1084. sprintf(csa->field, "%.*E", dig-1, val);
  1085. else
  1086. sprintf(csa->field, "%.*G", dig, val);
  1087. exp = strchr(csa->field, 'E');
  1088. if (exp != NULL)
  1089. sprintf(exp+1, "%d", atoi(exp+1));
  1090. if (strlen(csa->field) <= 12) break;
  1091. }
  1092. xassert(strlen(csa->field) <= 12);
  1093. return csa->field;
  1094. }
  1095. int glp_write_mps(glp_prob *P, int fmt, const glp_mpscp *parm,
  1096. const char *fname)
  1097. { /* write problem data in MPS format */
  1098. glp_mpscp _parm;
  1099. struct csa _csa, *csa = &_csa;
  1100. XFILE *fp;
  1101. int out_obj, one_col = 0, empty = 0;
  1102. int i, j, recno, marker, count, gap, ret;
  1103. xprintf("Writing problem data to `%s'...\n", fname);
  1104. if (!(fmt == GLP_MPS_DECK || fmt == GLP_MPS_FILE))
  1105. xerror("glp_write_mps: fmt = %d; invalid parameter\n", fmt);
  1106. if (parm == NULL)
  1107. glp_init_mpscp(&_parm), parm = &_parm;
  1108. /* check control parameters */
  1109. check_parm("glp_write_mps", parm);
  1110. /* initialize common storage area */
  1111. csa->P = P;
  1112. csa->deck = (fmt == GLP_MPS_DECK);
  1113. csa->parm = parm;
  1114. /* create output MPS file */
  1115. fp = xfopen(fname, "w"), recno = 0;
  1116. if (fp == NULL)
  1117. { xprintf("Unable to create `%s' - %s\n", fname, xerrmsg());
  1118. ret = 1;
  1119. goto done;
  1120. }
  1121. /* write comment records */
  1122. xfprintf(fp, "* %-*s%s\n", P->name == NULL ? 1 : 12, "Problem:",
  1123. P->name == NULL ? "" : P->name), recno++;
  1124. xfprintf(fp, "* %-12s%s\n", "Class:", glp_get_num_int(P) == 0 ?
  1125. "LP" : "MIP"), recno++;
  1126. xfprintf(fp, "* %-12s%d\n", "Rows:", P->m), recno++;
  1127. if (glp_get_num_int(P) == 0)
  1128. xfprintf(fp, "* %-12s%d\n", "Columns:", P->n), recno++;
  1129. else
  1130. xfprintf(fp, "* %-12s%d (%d integer, %d binary)\n",
  1131. "Columns:", P->n, glp_get_num_int(P), glp_get_num_bin(P)),
  1132. recno++;
  1133. xfprintf(fp, "* %-12s%d\n", "Non-zeros:", P->nnz), recno++;
  1134. xfprintf(fp, "* %-12s%s\n", "Format:", csa->deck ? "Fixed MPS" :
  1135. "Free MPS"), recno++;
  1136. xfprintf(fp, "*\n", recno++);
  1137. /* write NAME indicator record */
  1138. xfprintf(fp, "NAME%*s%s\n",
  1139. P->name == NULL ? 0 : csa->deck ? 10 : 1, "", mps_name(csa)),
  1140. recno++;
  1141. #if 1
  1142. /* determine whether to write the objective row */
  1143. out_obj = 1;
  1144. for (i = 1; i <= P->m; i++)
  1145. { if (P->row[i]->type == GLP_FR)
  1146. { out_obj = 0;
  1147. break;
  1148. }
  1149. }
  1150. #endif
  1151. /* write ROWS section */
  1152. xfprintf(fp, "ROWS\n"), recno++;
  1153. for (i = (out_obj ? 0 : 1); i <= P->m; i++)
  1154. { int type;
  1155. type = (i == 0 ? GLP_FR : P->row[i]->type);
  1156. if (type == GLP_FR)
  1157. type = 'N';
  1158. else if (type == GLP_LO)
  1159. type = 'G';
  1160. else if (type == GLP_UP)
  1161. type = 'L';
  1162. else if (type == GLP_DB || type == GLP_FX)
  1163. type = 'E';
  1164. else
  1165. xassert(type != type);
  1166. xfprintf(fp, " %c%*s%s\n", type, csa->deck ? 2 : 1, "",
  1167. row_name(csa, i)), recno++;
  1168. }
  1169. /* write COLUMNS section */
  1170. xfprintf(fp, "COLUMNS\n"), recno++;
  1171. marker = 0;
  1172. for (j = 1; j <= P->n; j++)
  1173. { GLPAIJ cj, *aij;
  1174. int kind;
  1175. kind = P->col[j]->kind;
  1176. if (kind == GLP_CV)
  1177. { if (marker % 2 == 1)
  1178. { /* close current integer block */
  1179. marker++;
  1180. xfprintf(fp, "%*sM%07d%*s'MARKER'%*s'INTEND'\n",
  1181. csa->deck ? 4 : 1, "", marker, csa->deck ? 2 : 1, "",
  1182. csa->deck ? 17 : 1, ""), recno++;
  1183. }
  1184. }
  1185. else if (kind == GLP_IV)
  1186. { if (marker % 2 == 0)
  1187. { /* open new integer block */
  1188. marker++;
  1189. xfprintf(fp, "%*sM%07d%*s'MARKER'%*s'INTORG'\n",
  1190. csa->deck ? 4 : 1, "", marker, csa->deck ? 2 : 1, "",
  1191. csa->deck ? 17 : 1, ""), recno++;
  1192. }
  1193. }
  1194. else
  1195. xassert(kind != kind);
  1196. if (out_obj && P->col[j]->coef != 0.0)
  1197. { /* make fake objective coefficient */
  1198. aij = &cj;
  1199. aij->row = NULL;
  1200. aij->val = P->col[j]->coef;
  1201. aij->c_next = P->col[j]->ptr;
  1202. }
  1203. else
  1204. aij = P->col[j]->ptr;
  1205. #if 1 /* FIXME */
  1206. if (aij == NULL)
  1207. { /* empty column */
  1208. empty++;
  1209. xfprintf(fp, "%*s%-*s", csa->deck ? 4 : 1, "",
  1210. csa->deck ? 8 : 1, col_name(csa, j));
  1211. /* we need a row */
  1212. xassert(P->m > 0);
  1213. xfprintf(fp, "%*s%-*s",
  1214. csa->deck ? 2 : 1, "", csa->deck ? 8 : 1,
  1215. row_name(csa, 1));
  1216. xfprintf(fp, "%*s0%*s$ empty column\n",
  1217. csa->deck ? 13 : 1, "", csa->deck ? 3 : 1, ""), recno++;
  1218. }
  1219. #endif
  1220. count = 0;
  1221. for (aij = aij; aij != NULL; aij = aij->c_next)
  1222. { if (one_col || count % 2 == 0)
  1223. xfprintf(fp, "%*s%-*s", csa->deck ? 4 : 1, "",
  1224. csa->deck ? 8 : 1, col_name(csa, j));
  1225. gap = (one_col || count % 2 == 0 ? 2 : 3);
  1226. xfprintf(fp, "%*s%-*s",
  1227. csa->deck ? gap : 1, "", csa->deck ? 8 : 1,
  1228. row_name(csa, aij->row == NULL ? 0 : aij->row->i));
  1229. xfprintf(fp, "%*s%*s",
  1230. csa->deck ? 2 : 1, "", csa->deck ? 12 : 1,
  1231. mps_numb(csa, aij->val)), count++;
  1232. if (one_col || count % 2 == 0)
  1233. xfprintf(fp, "\n"), recno++;
  1234. }
  1235. if (!(one_col || count % 2 == 0))
  1236. xfprintf(fp, "\n"), recno++;
  1237. }
  1238. if (marker % 2 == 1)
  1239. { /* close last integer block */
  1240. marker++;
  1241. xfprintf(fp, "%*sM%07d%*s'MARKER'%*s'INTEND'\n",
  1242. csa->deck ? 4 : 1, "", marker, csa->deck ? 2 : 1, "",
  1243. csa->deck ? 17 : 1, ""), recno++;
  1244. }
  1245. #if 1
  1246. if (empty > 0)
  1247. xprintf("Warning: problem has %d empty column(s)\n", empty);
  1248. #endif
  1249. /* write RHS section */
  1250. xfprintf(fp, "RHS\n"), recno++;
  1251. count = 0;
  1252. for (i = (out_obj ? 0 : 1); i <= P->m; i++)
  1253. { int type;
  1254. double rhs;
  1255. if (i == 0)
  1256. rhs = P->c0;
  1257. else
  1258. { type = P->row[i]->type;
  1259. if (type == GLP_FR)
  1260. rhs = 0.0;
  1261. else if (type == GLP_LO)
  1262. rhs = P->row[i]->lb;
  1263. else if (type == GLP_UP)
  1264. rhs = P->row[i]->ub;
  1265. else if (type == GLP_DB || type == GLP_FX)
  1266. rhs = P->row[i]->lb;
  1267. else
  1268. xassert(type != type);
  1269. }
  1270. if (rhs != 0.0)
  1271. { if (one_col || count % 2 == 0)
  1272. xfprintf(fp, "%*s%-*s", csa->deck ? 4 : 1, "",
  1273. csa->deck ? 8 : 1, "RHS1");
  1274. gap = (one_col || count % 2 == 0 ? 2 : 3);
  1275. xfprintf(fp, "%*s%-*s",
  1276. csa->deck ? gap : 1, "", csa->deck ? 8 : 1,
  1277. row_name(csa, i));
  1278. xfprintf(fp, "%*s%*s",
  1279. csa->deck ? 2 : 1, "", csa->deck ? 12 : 1,
  1280. mps_numb(csa, rhs)), count++;
  1281. if (one_col || count % 2 == 0)
  1282. xfprintf(fp, "\n"), recno++;
  1283. }
  1284. }
  1285. if (!(one_col || count % 2 == 0))
  1286. xfprintf(fp, "\n"), recno++;
  1287. /* write RANGES section */
  1288. for (i = P->m; i >= 1; i--)
  1289. if (P->row[i]->type == GLP_DB) break;
  1290. if (i == 0) goto bnds;
  1291. xfprintf(fp, "RANGES\n"), recno++;
  1292. count = 0;
  1293. for (i = 1; i <= P->m; i++)
  1294. { if (P->row[i]->type == GLP_DB)
  1295. { if (one_col || count % 2 == 0)
  1296. xfprintf(fp, "%*s%-*s", csa->deck ? 4 : 1, "",
  1297. csa->deck ? 8 : 1, "RNG1");
  1298. gap = (one_col || count % 2 == 0 ? 2 : 3);
  1299. xfprintf(fp, "%*s%-*s",
  1300. csa->deck ? gap : 1, "", csa->deck ? 8 : 1,
  1301. row_name(csa, i));
  1302. xfprintf(fp, "%*s%*s",
  1303. csa->deck ? 2 : 1, "", csa->deck ? 12 : 1,
  1304. mps_numb(csa, P->row[i]->ub - P->row[i]->lb)), count++;
  1305. if (one_col || count % 2 == 0)
  1306. xfprintf(fp, "\n"), recno++;
  1307. }
  1308. }
  1309. if (!(one_col || count % 2 == 0))
  1310. xfprintf(fp, "\n"), recno++;
  1311. bnds: /* write BOUNDS section */
  1312. for (j = P->n; j >= 1; j--)
  1313. if (!(P->col[j]->type == GLP_LO && P->col[j]->lb == 0.0))
  1314. break;
  1315. if (j == 0) goto endt;
  1316. xfprintf(fp, "BOUNDS\n"), recno++;
  1317. for (j = 1; j <= P->n; j++)
  1318. { int type, data[2];
  1319. double bnd[2];
  1320. char *spec[2];
  1321. spec[0] = spec[1] = NULL;
  1322. type = P->col[j]->type;
  1323. if (type == GLP_FR)
  1324. spec[0] = "FR", data[0] = 0;
  1325. else if (type == GLP_LO)
  1326. { if (P->col[j]->lb != 0.0)
  1327. spec[0] = "LO", data[0] = 1, bnd[0] = P->col[j]->lb;
  1328. if (P->col[j]->kind == GLP_IV)
  1329. spec[1] = "PL", data[1] = 0;
  1330. }
  1331. else if (type == GLP_UP)
  1332. { spec[0] = "MI", data[0] = 0;
  1333. spec[1] = "UP", data[1] = 1, bnd[1] = P->col[j]->ub;
  1334. }
  1335. else if (type == GLP_DB)
  1336. { if (P->col[j]->lb != 0.0)
  1337. spec[0] = "LO", data[0] = 1, bnd[0] = P->col[j]->lb;
  1338. spec[1] = "UP", data[1] = 1, bnd[1] = P->col[j]->ub;
  1339. }
  1340. else if (type == GLP_FX)
  1341. spec[0] = "FX", data[0] = 1, bnd[0] = P->col[j]->lb;
  1342. else
  1343. xassert(type != type);
  1344. for (i = 0; i <= 1; i++)
  1345. { if (spec[i] != NULL)
  1346. { xfprintf(fp, " %s %-*s%*s%-*s", spec[i],
  1347. csa->deck ? 8 : 1, "BND1", csa->deck ? 2 : 1, "",
  1348. csa->deck ? 8 : 1, col_name(csa, j));
  1349. if (data[i])
  1350. xfprintf(fp, "%*s%*s", csa->deck ? 2 : 1, "",
  1351. csa->deck ? 12 : 1, mps_numb(csa, bnd[i]));
  1352. xfprintf(fp, "\n"), recno++;
  1353. }
  1354. }
  1355. }
  1356. endt: /* write ENDATA indicator record */
  1357. xfprintf(fp, "ENDATA\n"), recno++;
  1358. xfflush(fp);
  1359. if (xferror(fp))
  1360. { xprintf("Write error on `%s' - %s\n", fname, xerrmsg());
  1361. ret = 1;
  1362. goto done;
  1363. }
  1364. /* problem data has been successfully written */
  1365. xprintf("%d records were written\n", recno);
  1366. ret = 0;
  1367. done: if (fp != NULL) xfclose(fp);
  1368. return ret;
  1369. }
  1370. /* eof */