glpcpx.c 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240
  1. /* glpcpx.c (CPLEX LP 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_cpxcp - initialize CPLEX LP format control parameters
  28. *
  29. * SYNOPSIS
  30. *
  31. * void glp_init_cpxcp(glp_cpxcp *parm):
  32. *
  33. * The routine glp_init_cpxcp initializes control parameters used by
  34. * the CPLEX LP input/output routines glp_read_lp and glp_write_lp with
  35. * default values.
  36. *
  37. * Default values of the control parameters are stored in the glp_cpxcp
  38. * structure, which the parameter parm points to. */
  39. void glp_init_cpxcp(glp_cpxcp *parm)
  40. { xassert(parm != NULL);
  41. return;
  42. }
  43. static void check_parm(const char *func, const glp_cpxcp *parm)
  44. { /* check control parameters */
  45. xassert(func != NULL);
  46. xassert(parm != NULL);
  47. return;
  48. }
  49. /***********************************************************************
  50. * NAME
  51. *
  52. * glp_read_lp - read problem data in CPLEX LP format
  53. *
  54. * SYNOPSIS
  55. *
  56. * int glp_read_lp(glp_prob *P, const glp_cpxcp *parm, const char
  57. * *fname);
  58. *
  59. * DESCRIPTION
  60. *
  61. * The routine glp_read_lp reads problem data in CPLEX LP format from
  62. * a text file.
  63. *
  64. * The parameter parm is a pointer to the structure glp_cpxcp, which
  65. * specifies control parameters used by the routine. If parm is NULL,
  66. * the routine uses default settings.
  67. *
  68. * The character string fname specifies a name of the text file to be
  69. * read.
  70. *
  71. * Note that before reading data the current content of the problem
  72. * object is completely erased with the routine glp_erase_prob.
  73. *
  74. * RETURNS
  75. *
  76. * If the operation was successful, the routine glp_read_lp returns
  77. * zero. Otherwise, it prints an error message and returns non-zero. */
  78. struct csa
  79. { /* common storage area */
  80. glp_prob *P;
  81. /* LP/MIP problem object */
  82. const glp_cpxcp *parm;
  83. /* pointer to control parameters */
  84. const char *fname;
  85. /* name of input CPLEX LP file */
  86. XFILE *fp;
  87. /* stream assigned to input CPLEX LP file */
  88. jmp_buf jump;
  89. /* label for go to in case of error */
  90. int count;
  91. /* line count */
  92. int c;
  93. /* current character or XEOF */
  94. int token;
  95. /* current token: */
  96. #define T_EOF 0x00 /* end of file */
  97. #define T_MINIMIZE 0x01 /* keyword 'minimize' */
  98. #define T_MAXIMIZE 0x02 /* keyword 'maximize' */
  99. #define T_SUBJECT_TO 0x03 /* keyword 'subject to' */
  100. #define T_BOUNDS 0x04 /* keyword 'bounds' */
  101. #define T_GENERAL 0x05 /* keyword 'general' */
  102. #define T_INTEGER 0x06 /* keyword 'integer' */
  103. #define T_BINARY 0x07 /* keyword 'binary' */
  104. #define T_END 0x08 /* keyword 'end' */
  105. #define T_NAME 0x09 /* symbolic name */
  106. #define T_NUMBER 0x0A /* numeric constant */
  107. #define T_PLUS 0x0B /* delimiter '+' */
  108. #define T_MINUS 0x0C /* delimiter '-' */
  109. #define T_COLON 0x0D /* delimiter ':' */
  110. #define T_LE 0x0E /* delimiter '<=' */
  111. #define T_GE 0x0F /* delimiter '>=' */
  112. #define T_EQ 0x10 /* delimiter '=' */
  113. char image[255+1];
  114. /* image of current token */
  115. int imlen;
  116. /* length of token image */
  117. double value;
  118. /* value of numeric constant */
  119. int n_max;
  120. /* length of the following five arrays (enlarged automatically,
  121. if necessary) */
  122. int *ind; /* int ind[1+n_max]; */
  123. double *val; /* double val[1+n_max]; */
  124. char *flag; /* char flag[1+n_max]; */
  125. /* working arrays used to construct linear forms */
  126. double *lb; /* double lb[1+n_max]; */
  127. double *ub; /* double ub[1+n_max]; */
  128. /* lower and upper bounds of variables (columns) */
  129. };
  130. #define CHAR_SET "!\"#$%&()/,.;?@_`'{}|~"
  131. /* characters, which may appear in symbolic names */
  132. static void error(struct csa *csa, const char *fmt, ...)
  133. { /* print error message and terminate processing */
  134. va_list arg;
  135. xprintf("%s:%d: ", csa->fname, csa->count);
  136. va_start(arg, fmt);
  137. xvprintf(fmt, arg);
  138. va_end(arg);
  139. longjmp(csa->jump, 1);
  140. /* no return */
  141. }
  142. static void warning(struct csa *csa, const char *fmt, ...)
  143. { /* print warning message and continue processing */
  144. va_list arg;
  145. xprintf("%s:%d: warning: ", csa->fname, csa->count);
  146. va_start(arg, fmt);
  147. xvprintf(fmt, arg);
  148. va_end(arg);
  149. return;
  150. }
  151. static void read_char(struct csa *csa)
  152. { /* read next character from input file */
  153. int c;
  154. xassert(csa->c != XEOF);
  155. if (csa->c == '\n') csa->count++;
  156. c = xfgetc(csa->fp);
  157. if (c < 0)
  158. { if (xferror(csa->fp))
  159. error(csa, "read error - %s\n", xerrmsg());
  160. else if (csa->c == '\n')
  161. { csa->count--;
  162. c = XEOF;
  163. }
  164. else
  165. { warning(csa, "missing final end of line\n");
  166. c = '\n';
  167. }
  168. }
  169. else if (c == '\n')
  170. ;
  171. else if (isspace(c))
  172. c = ' ';
  173. else if (iscntrl(c))
  174. error(csa, "invalid control character 0x%02X\n", c);
  175. csa->c = c;
  176. return;
  177. }
  178. static void add_char(struct csa *csa)
  179. { /* append current character to current token */
  180. if (csa->imlen == sizeof(csa->image)-1)
  181. error(csa, "token `%.15s...' too long\n", csa->image);
  182. csa->image[csa->imlen++] = (char)csa->c;
  183. csa->image[csa->imlen] = '\0';
  184. read_char(csa);
  185. return;
  186. }
  187. static int the_same(char *s1, char *s2)
  188. { /* compare two character strings ignoring case sensitivity */
  189. for (; *s1 != '\0'; s1++, s2++)
  190. { if (tolower((unsigned char)*s1) != tolower((unsigned char)*s2))
  191. return 0;
  192. }
  193. return 1;
  194. }
  195. static void scan_token(struct csa *csa)
  196. { /* scan next token */
  197. int flag;
  198. csa->token = -1;
  199. csa->image[0] = '\0';
  200. csa->imlen = 0;
  201. csa->value = 0.0;
  202. loop: flag = 0;
  203. /* skip non-significant characters */
  204. while (csa->c == ' ') read_char(csa);
  205. /* recognize and scan current token */
  206. if (csa->c == XEOF)
  207. csa->token = T_EOF;
  208. else if (csa->c == '\n')
  209. { read_char(csa);
  210. /* if the next character is letter, it may begin a keyword */
  211. if (isalpha(csa->c))
  212. { flag = 1;
  213. goto name;
  214. }
  215. goto loop;
  216. }
  217. else if (csa->c == '\\')
  218. { /* comment; ignore everything until end-of-line */
  219. while (csa->c != '\n') read_char(csa);
  220. goto loop;
  221. }
  222. else if (isalpha(csa->c) || csa->c != '.' && strchr(CHAR_SET,
  223. csa->c) != NULL)
  224. name: { /* symbolic name */
  225. csa->token = T_NAME;
  226. while (isalnum(csa->c) || strchr(CHAR_SET, csa->c) != NULL)
  227. add_char(csa);
  228. if (flag)
  229. { /* check for keyword */
  230. if (the_same(csa->image, "minimize"))
  231. csa->token = T_MINIMIZE;
  232. else if (the_same(csa->image, "minimum"))
  233. csa->token = T_MINIMIZE;
  234. else if (the_same(csa->image, "min"))
  235. csa->token = T_MINIMIZE;
  236. else if (the_same(csa->image, "maximize"))
  237. csa->token = T_MAXIMIZE;
  238. else if (the_same(csa->image, "maximum"))
  239. csa->token = T_MAXIMIZE;
  240. else if (the_same(csa->image, "max"))
  241. csa->token = T_MAXIMIZE;
  242. else if (the_same(csa->image, "subject"))
  243. { if (csa->c == ' ')
  244. { read_char(csa);
  245. if (tolower(csa->c) == 't')
  246. { csa->token = T_SUBJECT_TO;
  247. csa->image[csa->imlen++] = ' ';
  248. csa->image[csa->imlen] = '\0';
  249. add_char(csa);
  250. if (tolower(csa->c) != 'o')
  251. error(csa, "keyword `subject to' incomplete\n");
  252. add_char(csa);
  253. if (isalpha(csa->c))
  254. error(csa, "keyword `%s%c...' not recognized\n",
  255. csa->image, csa->c);
  256. }
  257. }
  258. }
  259. else if (the_same(csa->image, "such"))
  260. { if (csa->c == ' ')
  261. { read_char(csa);
  262. if (tolower(csa->c) == 't')
  263. { csa->token = T_SUBJECT_TO;
  264. csa->image[csa->imlen++] = ' ';
  265. csa->image[csa->imlen] = '\0';
  266. add_char(csa);
  267. if (tolower(csa->c) != 'h')
  268. err: error(csa, "keyword `such that' incomplete\n");
  269. add_char(csa);
  270. if (tolower(csa->c) != 'a') goto err;
  271. add_char(csa);
  272. if (tolower(csa->c) != 't') goto err;
  273. add_char(csa);
  274. if (isalpha(csa->c))
  275. error(csa, "keyword `%s%c...' not recognized\n",
  276. csa->image, csa->c);
  277. }
  278. }
  279. }
  280. else if (the_same(csa->image, "st"))
  281. csa->token = T_SUBJECT_TO;
  282. else if (the_same(csa->image, "s.t."))
  283. csa->token = T_SUBJECT_TO;
  284. else if (the_same(csa->image, "st."))
  285. csa->token = T_SUBJECT_TO;
  286. else if (the_same(csa->image, "bounds"))
  287. csa->token = T_BOUNDS;
  288. else if (the_same(csa->image, "bound"))
  289. csa->token = T_BOUNDS;
  290. else if (the_same(csa->image, "general"))
  291. csa->token = T_GENERAL;
  292. else if (the_same(csa->image, "generals"))
  293. csa->token = T_GENERAL;
  294. else if (the_same(csa->image, "gen"))
  295. csa->token = T_GENERAL;
  296. else if (the_same(csa->image, "integer"))
  297. csa->token = T_INTEGER;
  298. else if (the_same(csa->image, "integers"))
  299. csa->token = T_INTEGER;
  300. else if (the_same(csa->image, "int"))
  301. csa->token = T_INTEGER;
  302. else if (the_same(csa->image, "binary"))
  303. csa->token = T_BINARY;
  304. else if (the_same(csa->image, "binaries"))
  305. csa->token = T_BINARY;
  306. else if (the_same(csa->image, "bin"))
  307. csa->token = T_BINARY;
  308. else if (the_same(csa->image, "end"))
  309. csa->token = T_END;
  310. }
  311. }
  312. else if (isdigit(csa->c) || csa->c == '.')
  313. { /* numeric constant */
  314. csa->token = T_NUMBER;
  315. /* scan integer part */
  316. while (isdigit(csa->c)) add_char(csa);
  317. /* scan optional fractional part (it is mandatory, if there is
  318. no integer part) */
  319. if (csa->c == '.')
  320. { add_char(csa);
  321. if (csa->imlen == 1 && !isdigit(csa->c))
  322. error(csa, "invalid use of decimal point\n");
  323. while (isdigit(csa->c)) add_char(csa);
  324. }
  325. /* scan optional decimal exponent */
  326. if (csa->c == 'e' || csa->c == 'E')
  327. { add_char(csa);
  328. if (csa->c == '+' || csa->c == '-') add_char(csa);
  329. if (!isdigit(csa->c))
  330. error(csa, "numeric constant `%s' incomplete\n",
  331. csa->image);
  332. while (isdigit(csa->c)) add_char(csa);
  333. }
  334. /* convert the numeric constant to floating-point */
  335. if (str2num(csa->image, &csa->value))
  336. error(csa, "numeric constant `%s' out of range\n",
  337. csa->image);
  338. }
  339. else if (csa->c == '+')
  340. csa->token = T_PLUS, add_char(csa);
  341. else if (csa->c == '-')
  342. csa->token = T_MINUS, add_char(csa);
  343. else if (csa->c == ':')
  344. csa->token = T_COLON, add_char(csa);
  345. else if (csa->c == '<')
  346. { csa->token = T_LE, add_char(csa);
  347. if (csa->c == '=') add_char(csa);
  348. }
  349. else if (csa->c == '>')
  350. { csa->token = T_GE, add_char(csa);
  351. if (csa->c == '=') add_char(csa);
  352. }
  353. else if (csa->c == '=')
  354. { csa->token = T_EQ, add_char(csa);
  355. if (csa->c == '<')
  356. csa->token = T_LE, add_char(csa);
  357. else if (csa->c == '>')
  358. csa->token = T_GE, add_char(csa);
  359. }
  360. else
  361. error(csa, "character `%c' not recognized\n", csa->c);
  362. /* skip non-significant characters */
  363. while (csa->c == ' ') read_char(csa);
  364. return;
  365. }
  366. static int find_col(struct csa *csa, char *name)
  367. { /* find column by its symbolic name */
  368. int j;
  369. j = glp_find_col(csa->P, name);
  370. if (j == 0)
  371. { /* not found; create new column */
  372. j = glp_add_cols(csa->P, 1);
  373. glp_set_col_name(csa->P, j, name);
  374. /* enlarge working arrays, if necessary */
  375. if (csa->n_max < j)
  376. { int n_max = csa->n_max;
  377. int *ind = csa->ind;
  378. double *val = csa->val;
  379. char *flag = csa->flag;
  380. double *lb = csa->lb;
  381. double *ub = csa->ub;
  382. csa->n_max += csa->n_max;
  383. csa->ind = xcalloc(1+csa->n_max, sizeof(int));
  384. memcpy(&csa->ind[1], &ind[1], n_max * sizeof(int));
  385. xfree(ind);
  386. csa->val = xcalloc(1+csa->n_max, sizeof(double));
  387. memcpy(&csa->val[1], &val[1], n_max * sizeof(double));
  388. xfree(val);
  389. csa->flag = xcalloc(1+csa->n_max, sizeof(char));
  390. memset(&csa->flag[1], 0, csa->n_max * sizeof(char));
  391. memcpy(&csa->flag[1], &flag[1], n_max * sizeof(char));
  392. xfree(flag);
  393. csa->lb = xcalloc(1+csa->n_max, sizeof(double));
  394. memcpy(&csa->lb[1], &lb[1], n_max * sizeof(double));
  395. xfree(lb);
  396. csa->ub = xcalloc(1+csa->n_max, sizeof(double));
  397. memcpy(&csa->ub[1], &ub[1], n_max * sizeof(double));
  398. xfree(ub);
  399. }
  400. csa->lb[j] = +DBL_MAX, csa->ub[j] = -DBL_MAX;
  401. }
  402. return j;
  403. }
  404. /***********************************************************************
  405. * parse_linear_form - parse linear form
  406. *
  407. * This routine parses the linear form using the following syntax:
  408. *
  409. * <variable> ::= <symbolic name>
  410. * <coefficient> ::= <numeric constant>
  411. * <term> ::= <variable> | <numeric constant> <variable>
  412. * <linear form> ::= <term> | + <term> | - <term> |
  413. * <linear form> + <term> | <linear form> - <term>
  414. *
  415. * The routine returns the number of terms in the linear form. */
  416. static int parse_linear_form(struct csa *csa)
  417. { int j, k, len = 0, newlen;
  418. double s, coef;
  419. loop: /* parse an optional sign */
  420. if (csa->token == T_PLUS)
  421. s = +1.0, scan_token(csa);
  422. else if (csa->token == T_MINUS)
  423. s = -1.0, scan_token(csa);
  424. else
  425. s = +1.0;
  426. /* parse an optional coefficient */
  427. if (csa->token == T_NUMBER)
  428. coef = csa->value, scan_token(csa);
  429. else
  430. coef = 1.0;
  431. /* parse a variable name */
  432. if (csa->token != T_NAME)
  433. error(csa, "missing variable name\n");
  434. /* find the corresponding column */
  435. j = find_col(csa, csa->image);
  436. /* check if the variable is already used in the linear form */
  437. if (csa->flag[j])
  438. error(csa, "multiple use of variable `%s' not allowed\n",
  439. csa->image);
  440. /* add new term to the linear form */
  441. len++, csa->ind[len] = j, csa->val[len] = s * coef;
  442. /* and mark that the variable is used in the linear form */
  443. csa->flag[j] = 1;
  444. scan_token(csa);
  445. /* if the next token is a sign, there is another term */
  446. if (csa->token == T_PLUS || csa->token == T_MINUS) goto loop;
  447. /* clear marks of the variables used in the linear form */
  448. for (k = 1; k <= len; k++) csa->flag[csa->ind[k]] = 0;
  449. /* remove zero coefficients */
  450. newlen = 0;
  451. for (k = 1; k <= len; k++)
  452. { if (csa->val[k] != 0.0)
  453. { newlen++;
  454. csa->ind[newlen] = csa->ind[k];
  455. csa->val[newlen] = csa->val[k];
  456. }
  457. }
  458. return newlen;
  459. }
  460. /***********************************************************************
  461. * parse_objective - parse objective function
  462. *
  463. * This routine parses definition of the objective function using the
  464. * following syntax:
  465. *
  466. * <obj sense> ::= minimize | minimum | min | maximize | maximum | max
  467. * <obj name> ::= <empty> | <symbolic name> :
  468. * <obj function> ::= <obj sense> <obj name> <linear form> */
  469. static void parse_objective(struct csa *csa)
  470. { /* parse objective sense */
  471. int k, len;
  472. /* parse the keyword 'minimize' or 'maximize' */
  473. if (csa->token == T_MINIMIZE)
  474. glp_set_obj_dir(csa->P, GLP_MIN);
  475. else if (csa->token == T_MAXIMIZE)
  476. glp_set_obj_dir(csa->P, GLP_MAX);
  477. else
  478. xassert(csa != csa);
  479. scan_token(csa);
  480. /* parse objective name */
  481. if (csa->token == T_NAME && csa->c == ':')
  482. { /* objective name is followed by a colon */
  483. glp_set_obj_name(csa->P, csa->image);
  484. scan_token(csa);
  485. xassert(csa->token == T_COLON);
  486. scan_token(csa);
  487. }
  488. else
  489. { /* objective name is not specified; use default */
  490. glp_set_obj_name(csa->P, "obj");
  491. }
  492. /* parse linear form */
  493. len = parse_linear_form(csa);
  494. for (k = 1; k <= len; k++)
  495. glp_set_obj_coef(csa->P, csa->ind[k], csa->val[k]);
  496. return;
  497. }
  498. /***********************************************************************
  499. * parse_constraints - parse constraints section
  500. *
  501. * This routine parses the constraints section using the following
  502. * syntax:
  503. *
  504. * <row name> ::= <empty> | <symbolic name> :
  505. * <row sense> ::= < | <= | =< | > | >= | => | =
  506. * <right-hand side> ::= <numeric constant> | + <numeric constant> |
  507. * - <numeric constant>
  508. * <constraint> ::= <row name> <linear form> <row sense>
  509. * <right-hand side>
  510. * <subject to> ::= subject to | such that | st | s.t. | st.
  511. * <constraints section> ::= <subject to> <constraint> |
  512. * <constraints section> <constraint> */
  513. static void parse_constraints(struct csa *csa)
  514. { int i, len, type;
  515. double s;
  516. /* parse the keyword 'subject to' */
  517. xassert(csa->token == T_SUBJECT_TO);
  518. scan_token(csa);
  519. loop: /* create new row (constraint) */
  520. i = glp_add_rows(csa->P, 1);
  521. /* parse row name */
  522. if (csa->token == T_NAME && csa->c == ':')
  523. { /* row name is followed by a colon */
  524. if (glp_find_row(csa->P, csa->image) != 0)
  525. error(csa, "constraint `%s' multiply defined\n",
  526. csa->image);
  527. glp_set_row_name(csa->P, i, csa->image);
  528. scan_token(csa);
  529. xassert(csa->token == T_COLON);
  530. scan_token(csa);
  531. }
  532. else
  533. { /* row name is not specified; use default */
  534. char name[50];
  535. sprintf(name, "r.%d", csa->count);
  536. glp_set_row_name(csa->P, i, name);
  537. }
  538. /* parse linear form */
  539. len = parse_linear_form(csa);
  540. glp_set_mat_row(csa->P, i, len, csa->ind, csa->val);
  541. /* parse constraint sense */
  542. if (csa->token == T_LE)
  543. type = GLP_UP, scan_token(csa);
  544. else if (csa->token == T_GE)
  545. type = GLP_LO, scan_token(csa);
  546. else if (csa->token == T_EQ)
  547. type = GLP_FX, scan_token(csa);
  548. else
  549. error(csa, "missing constraint sense\n");
  550. /* parse right-hand side */
  551. if (csa->token == T_PLUS)
  552. s = +1.0, scan_token(csa);
  553. else if (csa->token == T_MINUS)
  554. s = -1.0, scan_token(csa);
  555. else
  556. s = +1.0;
  557. if (csa->token != T_NUMBER)
  558. error(csa, "missing right-hand side\n");
  559. glp_set_row_bnds(csa->P, i, type, s * csa->value, s * csa->value);
  560. /* the rest of the current line must be empty */
  561. if (!(csa->c == '\n' || csa->c == XEOF))
  562. error(csa, "invalid symbol(s) beyond right-hand side\n");
  563. scan_token(csa);
  564. /* if the next token is a sign, numeric constant, or a symbolic
  565. name, here is another constraint */
  566. if (csa->token == T_PLUS || csa->token == T_MINUS ||
  567. csa->token == T_NUMBER || csa->token == T_NAME) goto loop;
  568. return;
  569. }
  570. static void set_lower_bound(struct csa *csa, int j, double lb)
  571. { /* set lower bound of j-th variable */
  572. if (csa->lb[j] != +DBL_MAX)
  573. { warning(csa, "lower bound of variable `%s' redefined\n",
  574. glp_get_col_name(csa->P, j));
  575. }
  576. csa->lb[j] = lb;
  577. return;
  578. }
  579. static void set_upper_bound(struct csa *csa, int j, double ub)
  580. { /* set upper bound of j-th variable */
  581. if (csa->ub[j] != -DBL_MAX)
  582. { warning(csa, "upper bound of variable `%s' redefined\n",
  583. glp_get_col_name(csa->P, j));
  584. }
  585. csa->ub[j] = ub;
  586. return;
  587. }
  588. /***********************************************************************
  589. * parse_bounds - parse bounds section
  590. *
  591. * This routine parses the bounds section using the following syntax:
  592. *
  593. * <variable> ::= <symbolic name>
  594. * <infinity> ::= infinity | inf
  595. * <bound> ::= <numeric constant> | + <numeric constant> |
  596. * - <numeric constant> | + <infinity> | - <infinity>
  597. * <lt> ::= < | <= | =<
  598. * <gt> ::= > | >= | =>
  599. * <bound definition> ::= <bound> <lt> <variable> <lt> <bound> |
  600. * <bound> <lt> <variable> | <variable> <lt> <bound> |
  601. * <variable> <gt> <bound> | <variable> = <bound> | <variable> free
  602. * <bounds> ::= bounds | bound
  603. * <bounds section> ::= <bounds> |
  604. * <bounds section> <bound definition> */
  605. static void parse_bounds(struct csa *csa)
  606. { int j, lb_flag;
  607. double lb, s;
  608. /* parse the keyword 'bounds' */
  609. xassert(csa->token == T_BOUNDS);
  610. scan_token(csa);
  611. loop: /* bound definition can start with a sign, numeric constant, or
  612. a symbolic name */
  613. if (!(csa->token == T_PLUS || csa->token == T_MINUS ||
  614. csa->token == T_NUMBER || csa->token == T_NAME)) goto done;
  615. /* parse bound definition */
  616. if (csa->token == T_PLUS || csa->token == T_MINUS)
  617. { /* parse signed lower bound */
  618. lb_flag = 1;
  619. s = (csa->token == T_PLUS ? +1.0 : -1.0);
  620. scan_token(csa);
  621. if (csa->token == T_NUMBER)
  622. lb = s * csa->value, scan_token(csa);
  623. else if (the_same(csa->image, "infinity") ||
  624. the_same(csa->image, "inf"))
  625. { if (s > 0.0)
  626. error(csa, "invalid use of `+inf' as lower bound\n");
  627. lb = -DBL_MAX, scan_token(csa);
  628. }
  629. else
  630. error(csa, "missing lower bound\n");
  631. }
  632. else if (csa->token == T_NUMBER)
  633. { /* parse unsigned lower bound */
  634. lb_flag = 1;
  635. lb = csa->value, scan_token(csa);
  636. }
  637. else
  638. { /* lower bound is not specified */
  639. lb_flag = 0;
  640. }
  641. /* parse the token that should follow the lower bound */
  642. if (lb_flag)
  643. { if (csa->token != T_LE)
  644. error(csa, "missing `<', `<=', or `=<' after lower bound\n")
  645. ;
  646. scan_token(csa);
  647. }
  648. /* parse variable name */
  649. if (csa->token != T_NAME)
  650. error(csa, "missing variable name\n");
  651. j = find_col(csa, csa->image);
  652. /* set lower bound */
  653. if (lb_flag) set_lower_bound(csa, j, lb);
  654. scan_token(csa);
  655. /* parse the context that follows the variable name */
  656. if (csa->token == T_LE)
  657. { /* parse upper bound */
  658. scan_token(csa);
  659. if (csa->token == T_PLUS || csa->token == T_MINUS)
  660. { /* parse signed upper bound */
  661. s = (csa->token == T_PLUS ? +1.0 : -1.0);
  662. scan_token(csa);
  663. if (csa->token == T_NUMBER)
  664. { set_upper_bound(csa, j, s * csa->value);
  665. scan_token(csa);
  666. }
  667. else if (the_same(csa->image, "infinity") ||
  668. the_same(csa->image, "inf"))
  669. { if (s < 0.0)
  670. error(csa, "invalid use of `-inf' as upper bound\n");
  671. set_upper_bound(csa, j, +DBL_MAX);
  672. scan_token(csa);
  673. }
  674. else
  675. error(csa, "missing upper bound\n");
  676. }
  677. else if (csa->token == T_NUMBER)
  678. { /* parse unsigned upper bound */
  679. set_upper_bound(csa, j, csa->value);
  680. scan_token(csa);
  681. }
  682. else
  683. error(csa, "missing upper bound\n");
  684. }
  685. else if (csa->token == T_GE)
  686. { /* parse lower bound */
  687. if (lb_flag)
  688. { /* the context '... <= x >= ...' is invalid */
  689. error(csa, "invalid bound definition\n");
  690. }
  691. scan_token(csa);
  692. if (csa->token == T_PLUS || csa->token == T_MINUS)
  693. { /* parse signed lower bound */
  694. s = (csa->token == T_PLUS ? +1.0 : -1.0);
  695. scan_token(csa);
  696. if (csa->token == T_NUMBER)
  697. { set_lower_bound(csa, j, s * csa->value);
  698. scan_token(csa);
  699. }
  700. else if (the_same(csa->image, "infinity") ||
  701. the_same(csa->image, "inf") == 0)
  702. { if (s > 0.0)
  703. error(csa, "invalid use of `+inf' as lower bound\n");
  704. set_lower_bound(csa, j, -DBL_MAX);
  705. scan_token(csa);
  706. }
  707. else
  708. error(csa, "missing lower bound\n");
  709. }
  710. else if (csa->token == T_NUMBER)
  711. { /* parse unsigned lower bound */
  712. set_lower_bound(csa, j, csa->value);
  713. scan_token(csa);
  714. }
  715. else
  716. error(csa, "missing lower bound\n");
  717. }
  718. else if (csa->token == T_EQ)
  719. { /* parse fixed value */
  720. if (lb_flag)
  721. { /* the context '... <= x = ...' is invalid */
  722. error(csa, "invalid bound definition\n");
  723. }
  724. scan_token(csa);
  725. if (csa->token == T_PLUS || csa->token == T_MINUS)
  726. { /* parse signed fixed value */
  727. s = (csa->token == T_PLUS ? +1.0 : -1.0);
  728. scan_token(csa);
  729. if (csa->token == T_NUMBER)
  730. { set_lower_bound(csa, j, s * csa->value);
  731. set_upper_bound(csa, j, s * csa->value);
  732. scan_token(csa);
  733. }
  734. else
  735. error(csa, "missing fixed value\n");
  736. }
  737. else if (csa->token == T_NUMBER)
  738. { /* parse unsigned fixed value */
  739. set_lower_bound(csa, j, csa->value);
  740. set_upper_bound(csa, j, csa->value);
  741. scan_token(csa);
  742. }
  743. else
  744. error(csa, "missing fixed value\n");
  745. }
  746. else if (the_same(csa->image, "free"))
  747. { /* parse the keyword 'free' */
  748. if (lb_flag)
  749. { /* the context '... <= x free ...' is invalid */
  750. error(csa, "invalid bound definition\n");
  751. }
  752. set_lower_bound(csa, j, -DBL_MAX);
  753. set_upper_bound(csa, j, +DBL_MAX);
  754. scan_token(csa);
  755. }
  756. else if (!lb_flag)
  757. { /* neither lower nor upper bounds are specified */
  758. error(csa, "invalid bound definition\n");
  759. }
  760. goto loop;
  761. done: return;
  762. }
  763. /***********************************************************************
  764. * parse_integer - parse general, integer, or binary section
  765. *
  766. * <variable> ::= <symbolic name>
  767. * <general> ::= general | generals | gen
  768. * <integer> ::= integer | integers | int
  769. * <binary> ::= binary | binaries | bin
  770. * <section head> ::= <general> <integer> <binary>
  771. * <additional section> ::= <section head> |
  772. * <additional section> <variable> */
  773. static void parse_integer(struct csa *csa)
  774. { int j, binary;
  775. /* parse the keyword 'general', 'integer', or 'binary' */
  776. if (csa->token == T_GENERAL)
  777. binary = 0, scan_token(csa);
  778. else if (csa->token == T_INTEGER)
  779. binary = 0, scan_token(csa);
  780. else if (csa->token == T_BINARY)
  781. binary = 1, scan_token(csa);
  782. else
  783. xassert(csa != csa);
  784. /* parse list of variables (may be empty) */
  785. while (csa->token == T_NAME)
  786. { /* find the corresponding column */
  787. j = find_col(csa, csa->image);
  788. /* change kind of the variable */
  789. glp_set_col_kind(csa->P, j, GLP_IV);
  790. /* set 0-1 bounds for the binary variable */
  791. if (binary)
  792. { set_lower_bound(csa, j, 0.0);
  793. set_upper_bound(csa, j, 1.0);
  794. }
  795. scan_token(csa);
  796. }
  797. return;
  798. }
  799. int glp_read_lp(glp_prob *P, const glp_cpxcp *parm, const char *fname)
  800. { /* read problem data in CPLEX LP format */
  801. glp_cpxcp _parm;
  802. struct csa _csa, *csa = &_csa;
  803. int ret;
  804. xprintf("Reading problem data from `%s'...\n", fname);
  805. if (parm == NULL)
  806. glp_init_cpxcp(&_parm), parm = &_parm;
  807. /* check control parameters */
  808. check_parm("glp_read_lp", parm);
  809. /* initialize common storage area */
  810. csa->P = P;
  811. csa->parm = parm;
  812. csa->fname = fname;
  813. csa->fp = NULL;
  814. if (setjmp(csa->jump))
  815. { ret = 1;
  816. goto done;
  817. }
  818. csa->count = 0;
  819. csa->c = '\n';
  820. csa->token = T_EOF;
  821. csa->image[0] = '\0';
  822. csa->imlen = 0;
  823. csa->value = 0.0;
  824. csa->n_max = 100;
  825. csa->ind = xcalloc(1+csa->n_max, sizeof(int));
  826. csa->val = xcalloc(1+csa->n_max, sizeof(double));
  827. csa->flag = xcalloc(1+csa->n_max, sizeof(char));
  828. memset(&csa->flag[1], 0, csa->n_max * sizeof(char));
  829. csa->lb = xcalloc(1+csa->n_max, sizeof(double));
  830. csa->ub = xcalloc(1+csa->n_max, sizeof(double));
  831. /* erase problem object */
  832. glp_erase_prob(P);
  833. glp_create_index(P);
  834. /* open input CPLEX LP file */
  835. csa->fp = xfopen(fname, "r");
  836. if (csa->fp == NULL)
  837. { xprintf("Unable to open `%s' - %s\n", fname, xerrmsg());
  838. ret = 1;
  839. goto done;
  840. }
  841. /* scan very first token */
  842. scan_token(csa);
  843. /* parse definition of the objective function */
  844. if (!(csa->token == T_MINIMIZE || csa->token == T_MAXIMIZE))
  845. error(csa, "`minimize' or `maximize' keyword missing\n");
  846. parse_objective(csa);
  847. /* parse constraints section */
  848. if (csa->token != T_SUBJECT_TO)
  849. error(csa, "constraints section missing\n");
  850. parse_constraints(csa);
  851. /* parse optional bounds section */
  852. if (csa->token == T_BOUNDS) parse_bounds(csa);
  853. /* parse optional general, integer, and binary sections */
  854. while (csa->token == T_GENERAL ||
  855. csa->token == T_INTEGER ||
  856. csa->token == T_BINARY) parse_integer(csa);
  857. /* check for the keyword 'end' */
  858. if (csa->token == T_END)
  859. scan_token(csa);
  860. else if (csa->token == T_EOF)
  861. warning(csa, "keyword `end' missing\n");
  862. else
  863. error(csa, "symbol `%s' in wrong position\n", csa->image);
  864. /* nothing must follow the keyword 'end' (except comments) */
  865. if (csa->token != T_EOF)
  866. error(csa, "extra symbol(s) detected beyond `end'\n");
  867. /* set bounds of variables */
  868. { int j, type;
  869. double lb, ub;
  870. for (j = 1; j <= P->n; j++)
  871. { lb = csa->lb[j];
  872. ub = csa->ub[j];
  873. if (lb == +DBL_MAX) lb = 0.0; /* default lb */
  874. if (ub == -DBL_MAX) ub = +DBL_MAX; /* default ub */
  875. if (lb == -DBL_MAX && ub == +DBL_MAX)
  876. type = GLP_FR;
  877. else if (ub == +DBL_MAX)
  878. type = GLP_LO;
  879. else if (lb == -DBL_MAX)
  880. type = GLP_UP;
  881. else if (lb != ub)
  882. type = GLP_DB;
  883. else
  884. type = GLP_FX;
  885. glp_set_col_bnds(csa->P, j, type, lb, ub);
  886. }
  887. }
  888. /* print some statistics */
  889. xprintf("%d row%s, %d column%s, %d non-zero%s\n",
  890. P->m, P->m == 1 ? "" : "s", P->n, P->n == 1 ? "" : "s",
  891. P->nnz, P->nnz == 1 ? "" : "s");
  892. if (glp_get_num_int(P) > 0)
  893. { int ni = glp_get_num_int(P);
  894. int nb = glp_get_num_bin(P);
  895. if (ni == 1)
  896. { if (nb == 0)
  897. xprintf("One variable is integer\n");
  898. else
  899. xprintf("One variable is binary\n");
  900. }
  901. else
  902. { xprintf("%d integer variables, ", ni);
  903. if (nb == 0)
  904. xprintf("none");
  905. else if (nb == 1)
  906. xprintf("one");
  907. else if (nb == ni)
  908. xprintf("all");
  909. else
  910. xprintf("%d", nb);
  911. xprintf(" of which %s binary\n", nb == 1 ? "is" : "are");
  912. }
  913. }
  914. xprintf("%d lines were read\n", csa->count);
  915. /* problem data has been successfully read */
  916. glp_delete_index(P);
  917. glp_sort_matrix(P);
  918. ret = 0;
  919. done: if (csa->fp != NULL) xfclose(csa->fp);
  920. xfree(csa->ind);
  921. xfree(csa->val);
  922. xfree(csa->flag);
  923. xfree(csa->lb);
  924. xfree(csa->ub);
  925. if (ret != 0) glp_erase_prob(P);
  926. return ret;
  927. }
  928. /***********************************************************************
  929. * NAME
  930. *
  931. * glp_write_lp - write problem data in CPLEX LP format
  932. *
  933. * SYNOPSIS
  934. *
  935. * int glp_write_lp(glp_prob *P, const glp_cpxcp *parm, const char
  936. * *fname);
  937. *
  938. * DESCRIPTION
  939. *
  940. * The routine glp_write_lp writes problem data in CPLEX LP format to
  941. * a text file.
  942. *
  943. * The parameter parm is a pointer to the structure glp_cpxcp, which
  944. * specifies control parameters used by the routine. If parm is NULL,
  945. * the routine uses default settings.
  946. *
  947. * The character string fname specifies a name of the text file to be
  948. * written.
  949. *
  950. * RETURNS
  951. *
  952. * If the operation was successful, the routine glp_write_lp returns
  953. * zero. Otherwise, it prints an error message and returns non-zero. */
  954. #define csa csa1
  955. struct csa
  956. { /* common storage area */
  957. glp_prob *P;
  958. /* pointer to problem object */
  959. const glp_cpxcp *parm;
  960. /* pointer to control parameters */
  961. };
  962. static int check_name(char *name)
  963. { /* check if specified name is valid for CPLEX LP format */
  964. if (*name == '.') return 1;
  965. if (isdigit((unsigned char)*name)) return 1;
  966. for (; *name; name++)
  967. { if (!isalnum((unsigned char)*name) &&
  968. strchr(CHAR_SET, (unsigned char)*name) == NULL) return 1;
  969. }
  970. return 0; /* name is ok */
  971. }
  972. static void adjust_name(char *name)
  973. { /* attempt to adjust specified name to make it valid for CPLEX LP
  974. format */
  975. for (; *name; name++)
  976. { if (*name == ' ')
  977. *name = '_';
  978. else if (*name == '-')
  979. *name = '~';
  980. else if (*name == '[')
  981. *name = '(';
  982. else if (*name == ']')
  983. *name = ')';
  984. }
  985. return;
  986. }
  987. static char *row_name(struct csa *csa, int i, char rname[255+1])
  988. { /* construct symbolic name of i-th row (constraint) */
  989. const char *name;
  990. if (i == 0)
  991. name = glp_get_obj_name(csa->P);
  992. else
  993. name = glp_get_row_name(csa->P, i);
  994. if (name == NULL) goto fake;
  995. strcpy(rname, name);
  996. adjust_name(rname);
  997. if (check_name(rname)) goto fake;
  998. return rname;
  999. fake: if (i == 0)
  1000. strcpy(rname, "obj");
  1001. else
  1002. sprintf(rname, "r_%d", i);
  1003. return rname;
  1004. }
  1005. static char *col_name(struct csa *csa, int j, char cname[255+1])
  1006. { /* construct symbolic name of j-th column (variable) */
  1007. const char *name;
  1008. name = glp_get_col_name(csa->P, j);
  1009. if (name == NULL) goto fake;
  1010. strcpy(cname, name);
  1011. adjust_name(cname);
  1012. if (check_name(cname)) goto fake;
  1013. return cname;
  1014. fake: sprintf(cname, "x_%d", j);
  1015. return cname;
  1016. }
  1017. int glp_write_lp(glp_prob *P, const glp_cpxcp *parm, const char *fname)
  1018. { /* write problem data in CPLEX LP format */
  1019. glp_cpxcp _parm;
  1020. struct csa _csa, *csa = &_csa;
  1021. XFILE *fp;
  1022. GLPROW *row;
  1023. GLPCOL *col;
  1024. GLPAIJ *aij;
  1025. int i, j, len, flag, count, ret;
  1026. char line[1000+1], term[500+1], name[255+1];
  1027. xprintf("Writing problem data to `%s'...\n", fname);
  1028. if (parm == NULL)
  1029. glp_init_cpxcp(&_parm), parm = &_parm;
  1030. /* check control parameters */
  1031. check_parm("glp_write_lp", parm);
  1032. /* initialize common storage area */
  1033. csa->P = P;
  1034. csa->parm = parm;
  1035. /* create output CPLEX LP file */
  1036. fp = xfopen(fname, "w"), count = 0;
  1037. if (fp == NULL)
  1038. { xprintf("Unable to create `%s' - %s\n", fname, xerrmsg());
  1039. ret = 1;
  1040. goto done;
  1041. }
  1042. /* write problem name */
  1043. xfprintf(fp, "\\* Problem: %s *\\\n",
  1044. P->name == NULL ? "Unknown" : P->name), count++;
  1045. xfprintf(fp, "\n"), count++;
  1046. /* the problem should contain at least one row and one column */
  1047. if (!(P->m > 0 && P->n > 0))
  1048. { xprintf("Warning: problem has no rows/columns\n");
  1049. xfprintf(fp, "\\* WARNING: PROBLEM HAS NO ROWS/COLUMNS *\\\n"),
  1050. count++;
  1051. xfprintf(fp, "\n"), count++;
  1052. goto skip;
  1053. }
  1054. /* write the objective function definition */
  1055. if (P->dir == GLP_MIN)
  1056. xfprintf(fp, "Minimize\n"), count++;
  1057. else if (P->dir == GLP_MAX)
  1058. xfprintf(fp, "Maximize\n"), count++;
  1059. else
  1060. xassert(P != P);
  1061. row_name(csa, 0, name);
  1062. sprintf(line, " %s:", name);
  1063. len = 0;
  1064. for (j = 1; j <= P->n; j++)
  1065. { col = P->col[j];
  1066. if (col->coef != 0.0 || col->ptr == NULL)
  1067. { len++;
  1068. col_name(csa, j, name);
  1069. if (col->coef == 0.0)
  1070. sprintf(term, " + 0 %s", name); /* empty column */
  1071. else if (col->coef == +1.0)
  1072. sprintf(term, " + %s", name);
  1073. else if (col->coef == -1.0)
  1074. sprintf(term, " - %s", name);
  1075. else if (col->coef > 0.0)
  1076. sprintf(term, " + %.*g %s", DBL_DIG, +col->coef, name);
  1077. else
  1078. sprintf(term, " - %.*g %s", DBL_DIG, -col->coef, name);
  1079. if (strlen(line) + strlen(term) > 72)
  1080. xfprintf(fp, "%s\n", line), line[0] = '\0', count++;
  1081. strcat(line, term);
  1082. }
  1083. }
  1084. if (len == 0)
  1085. { /* empty objective */
  1086. sprintf(term, " 0 %s", col_name(csa, 1, name));
  1087. strcat(line, term);
  1088. }
  1089. xfprintf(fp, "%s\n", line), count++;
  1090. if (P->c0 != 0.0)
  1091. xfprintf(fp, "\\* constant term = %.*g *\\\n", DBL_DIG, P->c0),
  1092. count++;
  1093. xfprintf(fp, "\n"), count++;
  1094. /* write the constraints section */
  1095. xfprintf(fp, "Subject To\n"), count++;
  1096. for (i = 1; i <= P->m; i++)
  1097. { row = P->row[i];
  1098. if (row->type == GLP_FR) continue; /* skip free row */
  1099. row_name(csa, i, name);
  1100. sprintf(line, " %s:", name);
  1101. /* linear form */
  1102. for (aij = row->ptr; aij != NULL; aij = aij->r_next)
  1103. { col_name(csa, aij->col->j, name);
  1104. if (aij->val == +1.0)
  1105. sprintf(term, " + %s", name);
  1106. else if (aij->val == -1.0)
  1107. sprintf(term, " - %s", name);
  1108. else if (aij->val > 0.0)
  1109. sprintf(term, " + %.*g %s", DBL_DIG, +aij->val, name);
  1110. else
  1111. sprintf(term, " - %.*g %s", DBL_DIG, -aij->val, name);
  1112. if (strlen(line) + strlen(term) > 72)
  1113. xfprintf(fp, "%s\n", line), line[0] = '\0', count++;
  1114. strcat(line, term);
  1115. }
  1116. if (row->type == GLP_DB)
  1117. { /* double-bounded (ranged) constraint */
  1118. sprintf(term, " - ~r_%d", i);
  1119. if (strlen(line) + strlen(term) > 72)
  1120. xfprintf(fp, "%s\n", line), line[0] = '\0', count++;
  1121. strcat(line, term);
  1122. }
  1123. else if (row->ptr == NULL)
  1124. { /* empty constraint */
  1125. sprintf(term, " 0 %s", col_name(csa, 1, name));
  1126. strcat(line, term);
  1127. }
  1128. /* right hand-side */
  1129. if (row->type == GLP_LO)
  1130. sprintf(term, " >= %.*g", DBL_DIG, row->lb);
  1131. else if (row->type == GLP_UP)
  1132. sprintf(term, " <= %.*g", DBL_DIG, row->ub);
  1133. else if (row->type == GLP_DB || row->type == GLP_FX)
  1134. sprintf(term, " = %.*g", DBL_DIG, row->lb);
  1135. else
  1136. xassert(row != row);
  1137. if (strlen(line) + strlen(term) > 72)
  1138. xfprintf(fp, "%s\n", line), line[0] = '\0', count++;
  1139. strcat(line, term);
  1140. xfprintf(fp, "%s\n", line), count++;
  1141. }
  1142. xfprintf(fp, "\n"), count++;
  1143. /* write the bounds section */
  1144. flag = 0;
  1145. for (i = 1; i <= P->m; i++)
  1146. { row = P->row[i];
  1147. if (row->type != GLP_DB) continue;
  1148. if (!flag)
  1149. xfprintf(fp, "Bounds\n"), flag = 1, count++;
  1150. xfprintf(fp, " 0 <= ~r_%d <= %.*g\n",
  1151. i, DBL_DIG, row->ub - row->lb), count++;
  1152. }
  1153. for (j = 1; j <= P->n; j++)
  1154. { col = P->col[j];
  1155. if (col->type == GLP_LO && col->lb == 0.0) continue;
  1156. if (!flag)
  1157. xfprintf(fp, "Bounds\n"), flag = 1, count++;
  1158. col_name(csa, j, name);
  1159. if (col->type == GLP_FR)
  1160. xfprintf(fp, " %s free\n", name), count++;
  1161. else if (col->type == GLP_LO)
  1162. xfprintf(fp, " %s >= %.*g\n",
  1163. name, DBL_DIG, col->lb), count++;
  1164. else if (col->type == GLP_UP)
  1165. xfprintf(fp, " -Inf <= %s <= %.*g\n",
  1166. name, DBL_DIG, col->ub), count++;
  1167. else if (col->type == GLP_DB)
  1168. xfprintf(fp, " %.*g <= %s <= %.*g\n",
  1169. DBL_DIG, col->lb, name, DBL_DIG, col->ub), count++;
  1170. else if (col->type == GLP_FX)
  1171. xfprintf(fp, " %s = %.*g\n",
  1172. name, DBL_DIG, col->lb), count++;
  1173. else
  1174. xassert(col != col);
  1175. }
  1176. if (flag) xfprintf(fp, "\n"), count++;
  1177. /* write the integer section */
  1178. flag = 0;
  1179. for (j = 1; j <= P->n; j++)
  1180. { col = P->col[j];
  1181. if (col->kind == GLP_CV) continue;
  1182. xassert(col->kind == GLP_IV);
  1183. if (!flag)
  1184. xfprintf(fp, "Generals\n"), flag = 1, count++;
  1185. xfprintf(fp, " %s\n", col_name(csa, j, name)), count++;
  1186. }
  1187. if (flag) xfprintf(fp, "\n"), count++;
  1188. skip: /* write the end keyword */
  1189. xfprintf(fp, "End\n"), count++;
  1190. xfflush(fp);
  1191. if (xferror(fp))
  1192. { xprintf("Write error on `%s' - %s\n", fname, xerrmsg());
  1193. ret = 1;
  1194. goto done;
  1195. }
  1196. /* problem data has been successfully written */
  1197. xprintf("%d lines were written\n", count);
  1198. ret = 0;
  1199. done: if (fp != NULL) xfclose(fp);
  1200. return ret;
  1201. }
  1202. /* eof */