glpipm.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144
  1. /* glpipm.c */
  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 "glpipm.h"
  24. #include "glpmat.h"
  25. #define ITER_MAX 100
  26. /* maximal number of iterations */
  27. struct csa
  28. { /* common storage area */
  29. /*--------------------------------------------------------------*/
  30. /* LP data */
  31. int m;
  32. /* number of rows (equality constraints) */
  33. int n;
  34. /* number of columns (structural variables) */
  35. int *A_ptr; /* int A_ptr[1+m+1]; */
  36. int *A_ind; /* int A_ind[A_ptr[m+1]]; */
  37. double *A_val; /* double A_val[A_ptr[m+1]]; */
  38. /* mxn-matrix A in storage-by-rows format */
  39. double *b; /* double b[1+m]; */
  40. /* m-vector b of right-hand sides */
  41. double *c; /* double c[1+n]; */
  42. /* n-vector c of objective coefficients; c[0] is constant term of
  43. the objective function */
  44. /*--------------------------------------------------------------*/
  45. /* LP solution */
  46. double *x; /* double x[1+n]; */
  47. double *y; /* double y[1+m]; */
  48. double *z; /* double z[1+n]; */
  49. /* current point in primal-dual space; the best point on exit */
  50. /*--------------------------------------------------------------*/
  51. /* control parameters */
  52. const glp_iptcp *parm;
  53. /*--------------------------------------------------------------*/
  54. /* working arrays and variables */
  55. double *D; /* double D[1+n]; */
  56. /* diagonal nxn-matrix D = X*inv(Z), where X = diag(x[j]) and
  57. Z = diag(z[j]) */
  58. int *P; /* int P[1+m+m]; */
  59. /* permutation mxm-matrix P used to minimize fill-in in Cholesky
  60. factorization */
  61. int *S_ptr; /* int S_ptr[1+m+1]; */
  62. int *S_ind; /* int S_ind[S_ptr[m+1]]; */
  63. double *S_val; /* double S_val[S_ptr[m+1]]; */
  64. double *S_diag; /* double S_diag[1+m]; */
  65. /* symmetric mxm-matrix S = P*A*D*A'*P' whose upper triangular
  66. part without diagonal elements is stored in S_ptr, S_ind, and
  67. S_val in storage-by-rows format, diagonal elements are stored
  68. in S_diag */
  69. int *U_ptr; /* int U_ptr[1+m+1]; */
  70. int *U_ind; /* int U_ind[U_ptr[m+1]]; */
  71. double *U_val; /* double U_val[U_ptr[m+1]]; */
  72. double *U_diag; /* double U_diag[1+m]; */
  73. /* upper triangular mxm-matrix U defining Cholesky factorization
  74. S = U'*U; its non-diagonal elements are stored in U_ptr, U_ind,
  75. U_val in storage-by-rows format, diagonal elements are stored
  76. in U_diag */
  77. int iter;
  78. /* iteration number (0, 1, 2, ...); iter = 0 corresponds to the
  79. initial point */
  80. double obj;
  81. /* current value of the objective function */
  82. double rpi;
  83. /* relative primal infeasibility rpi = ||A*x-b||/(1+||b||) */
  84. double rdi;
  85. /* relative dual infeasibility rdi = ||A'*y+z-c||/(1+||c||) */
  86. double gap;
  87. /* primal-dual gap = |c'*x-b'*y|/(1+|c'*x|) which is a relative
  88. difference between primal and dual objective functions */
  89. double phi;
  90. /* merit function phi = ||A*x-b||/max(1,||b||) +
  91. + ||A'*y+z-c||/max(1,||c||) +
  92. + |c'*x-b'*y|/max(1,||b||,||c||) */
  93. double mu;
  94. /* duality measure mu = x'*z/n (used as barrier parameter) */
  95. double rmu;
  96. /* rmu = max(||A*x-b||,||A'*y+z-c||)/mu */
  97. double rmu0;
  98. /* the initial value of rmu on iteration 0 */
  99. double *phi_min; /* double phi_min[1+ITER_MAX]; */
  100. /* phi_min[k] = min(phi[k]), where phi[k] is the value of phi on
  101. k-th iteration, 0 <= k <= iter */
  102. int best_iter;
  103. /* iteration number, on which the value of phi reached its best
  104. (minimal) value */
  105. double *best_x; /* double best_x[1+n]; */
  106. double *best_y; /* double best_y[1+m]; */
  107. double *best_z; /* double best_z[1+n]; */
  108. /* best point (in the sense of the merit function phi) which has
  109. been reached on iteration iter_best */
  110. double best_obj;
  111. /* objective value at the best point */
  112. double *dx_aff; /* double dx_aff[1+n]; */
  113. double *dy_aff; /* double dy_aff[1+m]; */
  114. double *dz_aff; /* double dz_aff[1+n]; */
  115. /* affine scaling direction */
  116. double alfa_aff_p, alfa_aff_d;
  117. /* maximal primal and dual stepsizes in affine scaling direction,
  118. on which x and z are still non-negative */
  119. double mu_aff;
  120. /* duality measure mu_aff = x_aff'*z_aff/n in the boundary point
  121. x_aff' = x+alfa_aff_p*dx_aff, z_aff' = z+alfa_aff_d*dz_aff */
  122. double sigma;
  123. /* Mehrotra's heuristic parameter (0 <= sigma <= 1) */
  124. double *dx_cc; /* double dx_cc[1+n]; */
  125. double *dy_cc; /* double dy_cc[1+m]; */
  126. double *dz_cc; /* double dz_cc[1+n]; */
  127. /* centering corrector direction */
  128. double *dx; /* double dx[1+n]; */
  129. double *dy; /* double dy[1+m]; */
  130. double *dz; /* double dz[1+n]; */
  131. /* final combined direction dx = dx_aff+dx_cc, dy = dy_aff+dy_cc,
  132. dz = dz_aff+dz_cc */
  133. double alfa_max_p;
  134. double alfa_max_d;
  135. /* maximal primal and dual stepsizes in combined direction, on
  136. which x and z are still non-negative */
  137. };
  138. /***********************************************************************
  139. * initialize - allocate and initialize common storage area
  140. *
  141. * This routine allocates and initializes the common storage area (CSA)
  142. * used by interior-point method routines. */
  143. static void initialize(struct csa *csa)
  144. { int m = csa->m;
  145. int n = csa->n;
  146. int i;
  147. if (csa->parm->msg_lev >= GLP_MSG_ALL)
  148. xprintf("Matrix A has %d non-zeros\n", csa->A_ptr[m+1]-1);
  149. csa->D = xcalloc(1+n, sizeof(double));
  150. /* P := I */
  151. csa->P = xcalloc(1+m+m, sizeof(int));
  152. for (i = 1; i <= m; i++) csa->P[i] = csa->P[m+i] = i;
  153. /* S := A*A', symbolically */
  154. csa->S_ptr = xcalloc(1+m+1, sizeof(int));
  155. csa->S_ind = adat_symbolic(m, n, csa->P, csa->A_ptr, csa->A_ind,
  156. csa->S_ptr);
  157. if (csa->parm->msg_lev >= GLP_MSG_ALL)
  158. xprintf("Matrix S = A*A' has %d non-zeros (upper triangle)\n",
  159. csa->S_ptr[m+1]-1 + m);
  160. /* determine P using specified ordering algorithm */
  161. if (csa->parm->ord_alg == GLP_ORD_NONE)
  162. { if (csa->parm->msg_lev >= GLP_MSG_ALL)
  163. xprintf("Original ordering is being used\n");
  164. for (i = 1; i <= m; i++)
  165. csa->P[i] = csa->P[m+i] = i;
  166. }
  167. else if (csa->parm->ord_alg == GLP_ORD_QMD)
  168. { if (csa->parm->msg_lev >= GLP_MSG_ALL)
  169. xprintf("Minimum degree ordering (QMD)...\n");
  170. min_degree(m, csa->S_ptr, csa->S_ind, csa->P);
  171. }
  172. else if (csa->parm->ord_alg == GLP_ORD_AMD)
  173. { if (csa->parm->msg_lev >= GLP_MSG_ALL)
  174. xprintf("Approximate minimum degree ordering (AMD)...\n");
  175. amd_order1(m, csa->S_ptr, csa->S_ind, csa->P);
  176. }
  177. else if (csa->parm->ord_alg == GLP_ORD_SYMAMD)
  178. { if (csa->parm->msg_lev >= GLP_MSG_ALL)
  179. xprintf("Approximate minimum degree ordering (SYMAMD)...\n")
  180. ;
  181. symamd_ord(m, csa->S_ptr, csa->S_ind, csa->P);
  182. }
  183. else
  184. xassert(csa != csa);
  185. /* S := P*A*A'*P', symbolically */
  186. xfree(csa->S_ind);
  187. csa->S_ind = adat_symbolic(m, n, csa->P, csa->A_ptr, csa->A_ind,
  188. csa->S_ptr);
  189. csa->S_val = xcalloc(csa->S_ptr[m+1], sizeof(double));
  190. csa->S_diag = xcalloc(1+m, sizeof(double));
  191. /* compute Cholesky factorization S = U'*U, symbolically */
  192. if (csa->parm->msg_lev >= GLP_MSG_ALL)
  193. xprintf("Computing Cholesky factorization S = L*L'...\n");
  194. csa->U_ptr = xcalloc(1+m+1, sizeof(int));
  195. csa->U_ind = chol_symbolic(m, csa->S_ptr, csa->S_ind, csa->U_ptr);
  196. if (csa->parm->msg_lev >= GLP_MSG_ALL)
  197. xprintf("Matrix L has %d non-zeros\n", csa->U_ptr[m+1]-1 + m);
  198. csa->U_val = xcalloc(csa->U_ptr[m+1], sizeof(double));
  199. csa->U_diag = xcalloc(1+m, sizeof(double));
  200. csa->iter = 0;
  201. csa->obj = 0.0;
  202. csa->rpi = 0.0;
  203. csa->rdi = 0.0;
  204. csa->gap = 0.0;
  205. csa->phi = 0.0;
  206. csa->mu = 0.0;
  207. csa->rmu = 0.0;
  208. csa->rmu0 = 0.0;
  209. csa->phi_min = xcalloc(1+ITER_MAX, sizeof(double));
  210. csa->best_iter = 0;
  211. csa->best_x = xcalloc(1+n, sizeof(double));
  212. csa->best_y = xcalloc(1+m, sizeof(double));
  213. csa->best_z = xcalloc(1+n, sizeof(double));
  214. csa->best_obj = 0.0;
  215. csa->dx_aff = xcalloc(1+n, sizeof(double));
  216. csa->dy_aff = xcalloc(1+m, sizeof(double));
  217. csa->dz_aff = xcalloc(1+n, sizeof(double));
  218. csa->alfa_aff_p = 0.0;
  219. csa->alfa_aff_d = 0.0;
  220. csa->mu_aff = 0.0;
  221. csa->sigma = 0.0;
  222. csa->dx_cc = xcalloc(1+n, sizeof(double));
  223. csa->dy_cc = xcalloc(1+m, sizeof(double));
  224. csa->dz_cc = xcalloc(1+n, sizeof(double));
  225. csa->dx = csa->dx_aff;
  226. csa->dy = csa->dy_aff;
  227. csa->dz = csa->dz_aff;
  228. csa->alfa_max_p = 0.0;
  229. csa->alfa_max_d = 0.0;
  230. return;
  231. }
  232. /***********************************************************************
  233. * A_by_vec - compute y = A*x
  234. *
  235. * This routine computes matrix-vector product y = A*x, where A is the
  236. * constraint matrix. */
  237. static void A_by_vec(struct csa *csa, double x[], double y[])
  238. { /* compute y = A*x */
  239. int m = csa->m;
  240. int *A_ptr = csa->A_ptr;
  241. int *A_ind = csa->A_ind;
  242. double *A_val = csa->A_val;
  243. int i, t, beg, end;
  244. double temp;
  245. for (i = 1; i <= m; i++)
  246. { temp = 0.0;
  247. beg = A_ptr[i], end = A_ptr[i+1];
  248. for (t = beg; t < end; t++) temp += A_val[t] * x[A_ind[t]];
  249. y[i] = temp;
  250. }
  251. return;
  252. }
  253. /***********************************************************************
  254. * AT_by_vec - compute y = A'*x
  255. *
  256. * This routine computes matrix-vector product y = A'*x, where A' is a
  257. * matrix transposed to the constraint matrix A. */
  258. static void AT_by_vec(struct csa *csa, double x[], double y[])
  259. { /* compute y = A'*x, where A' is transposed to A */
  260. int m = csa->m;
  261. int n = csa->n;
  262. int *A_ptr = csa->A_ptr;
  263. int *A_ind = csa->A_ind;
  264. double *A_val = csa->A_val;
  265. int i, j, t, beg, end;
  266. double temp;
  267. for (j = 1; j <= n; j++) y[j] = 0.0;
  268. for (i = 1; i <= m; i++)
  269. { temp = x[i];
  270. if (temp == 0.0) continue;
  271. beg = A_ptr[i], end = A_ptr[i+1];
  272. for (t = beg; t < end; t++) y[A_ind[t]] += A_val[t] * temp;
  273. }
  274. return;
  275. }
  276. /***********************************************************************
  277. * decomp_NE - numeric factorization of matrix S = P*A*D*A'*P'
  278. *
  279. * This routine implements numeric phase of Cholesky factorization of
  280. * the matrix S = P*A*D*A'*P', which is a permuted matrix of the normal
  281. * equation system. Matrix D is assumed to be already computed. */
  282. static void decomp_NE(struct csa *csa)
  283. { adat_numeric(csa->m, csa->n, csa->P, csa->A_ptr, csa->A_ind,
  284. csa->A_val, csa->D, csa->S_ptr, csa->S_ind, csa->S_val,
  285. csa->S_diag);
  286. chol_numeric(csa->m, csa->S_ptr, csa->S_ind, csa->S_val,
  287. csa->S_diag, csa->U_ptr, csa->U_ind, csa->U_val, csa->U_diag);
  288. return;
  289. }
  290. /***********************************************************************
  291. * solve_NE - solve normal equation system
  292. *
  293. * This routine solves the normal equation system:
  294. *
  295. * A*D*A'*y = h.
  296. *
  297. * It is assumed that the matrix A*D*A' has been previously factorized
  298. * by the routine decomp_NE.
  299. *
  300. * On entry the array y contains the vector of right-hand sides h. On
  301. * exit this array contains the computed vector of unknowns y.
  302. *
  303. * Once the vector y has been computed the routine checks for numeric
  304. * stability. If the residual vector:
  305. *
  306. * r = A*D*A'*y - h
  307. *
  308. * is relatively small, the routine returns zero, otherwise non-zero is
  309. * returned. */
  310. static int solve_NE(struct csa *csa, double y[])
  311. { int m = csa->m;
  312. int n = csa->n;
  313. int *P = csa->P;
  314. int i, j, ret = 0;
  315. double *h, *r, *w;
  316. /* save vector of right-hand sides h */
  317. h = xcalloc(1+m, sizeof(double));
  318. for (i = 1; i <= m; i++) h[i] = y[i];
  319. /* solve normal equation system (A*D*A')*y = h */
  320. /* since S = P*A*D*A'*P' = U'*U, then A*D*A' = P'*U'*U*P, so we
  321. have inv(A*D*A') = P'*inv(U)*inv(U')*P */
  322. /* w := P*h */
  323. w = xcalloc(1+m, sizeof(double));
  324. for (i = 1; i <= m; i++) w[i] = y[P[i]];
  325. /* w := inv(U')*w */
  326. ut_solve(m, csa->U_ptr, csa->U_ind, csa->U_val, csa->U_diag, w);
  327. /* w := inv(U)*w */
  328. u_solve(m, csa->U_ptr, csa->U_ind, csa->U_val, csa->U_diag, w);
  329. /* y := P'*w */
  330. for (i = 1; i <= m; i++) y[i] = w[P[m+i]];
  331. xfree(w);
  332. /* compute residual vector r = A*D*A'*y - h */
  333. r = xcalloc(1+m, sizeof(double));
  334. /* w := A'*y */
  335. w = xcalloc(1+n, sizeof(double));
  336. AT_by_vec(csa, y, w);
  337. /* w := D*w */
  338. for (j = 1; j <= n; j++) w[j] *= csa->D[j];
  339. /* r := A*w */
  340. A_by_vec(csa, w, r);
  341. xfree(w);
  342. /* r := r - h */
  343. for (i = 1; i <= m; i++) r[i] -= h[i];
  344. /* check for numeric stability */
  345. for (i = 1; i <= m; i++)
  346. { if (fabs(r[i]) / (1.0 + fabs(h[i])) > 1e-4)
  347. { ret = 1;
  348. break;
  349. }
  350. }
  351. xfree(h);
  352. xfree(r);
  353. return ret;
  354. }
  355. /***********************************************************************
  356. * solve_NS - solve Newtonian system
  357. *
  358. * This routine solves the Newtonian system:
  359. *
  360. * A*dx = p
  361. *
  362. * A'*dy + dz = q
  363. *
  364. * Z*dx + X*dz = r
  365. *
  366. * where X = diag(x[j]), Z = diag(z[j]), by reducing it to the normal
  367. * equation system:
  368. *
  369. * (A*inv(Z)*X*A')*dy = A*inv(Z)*(X*q-r)+p
  370. *
  371. * (it is assumed that the matrix A*inv(Z)*X*A' has been factorized by
  372. * the routine decomp_NE).
  373. *
  374. * Once vector dy has been computed the routine computes vectors dx and
  375. * dz as follows:
  376. *
  377. * dx = inv(Z)*(X*(A'*dy-q)+r)
  378. *
  379. * dz = inv(X)*(r-Z*dx)
  380. *
  381. * The routine solve_NS returns the same code which was reported by the
  382. * routine solve_NE (see above). */
  383. static int solve_NS(struct csa *csa, double p[], double q[], double r[],
  384. double dx[], double dy[], double dz[])
  385. { int m = csa->m;
  386. int n = csa->n;
  387. double *x = csa->x;
  388. double *z = csa->z;
  389. int i, j, ret;
  390. double *w = dx;
  391. /* compute the vector of right-hand sides A*inv(Z)*(X*q-r)+p for
  392. the normal equation system */
  393. for (j = 1; j <= n; j++)
  394. w[j] = (x[j] * q[j] - r[j]) / z[j];
  395. A_by_vec(csa, w, dy);
  396. for (i = 1; i <= m; i++) dy[i] += p[i];
  397. /* solve the normal equation system to compute vector dy */
  398. ret = solve_NE(csa, dy);
  399. /* compute vectors dx and dz */
  400. AT_by_vec(csa, dy, dx);
  401. for (j = 1; j <= n; j++)
  402. { dx[j] = (x[j] * (dx[j] - q[j]) + r[j]) / z[j];
  403. dz[j] = (r[j] - z[j] * dx[j]) / x[j];
  404. }
  405. return ret;
  406. }
  407. /***********************************************************************
  408. * initial_point - choose initial point using Mehrotra's heuristic
  409. *
  410. * This routine chooses a starting point using a heuristic proposed in
  411. * the paper:
  412. *
  413. * S. Mehrotra. On the implementation of a primal-dual interior point
  414. * method. SIAM J. on Optim., 2(4), pp. 575-601, 1992.
  415. *
  416. * The starting point x in the primal space is chosen as a solution of
  417. * the following least squares problem:
  418. *
  419. * minimize ||x||
  420. *
  421. * subject to A*x = b
  422. *
  423. * which can be computed explicitly as follows:
  424. *
  425. * x = A'*inv(A*A')*b
  426. *
  427. * Similarly, the starting point (y, z) in the dual space is chosen as
  428. * a solution of the following least squares problem:
  429. *
  430. * minimize ||z||
  431. *
  432. * subject to A'*y + z = c
  433. *
  434. * which can be computed explicitly as follows:
  435. *
  436. * y = inv(A*A')*A*c
  437. *
  438. * z = c - A'*y
  439. *
  440. * However, some components of the vectors x and z may be non-positive
  441. * or close to zero, so the routine uses a Mehrotra's heuristic to find
  442. * a more appropriate starting point. */
  443. static void initial_point(struct csa *csa)
  444. { int m = csa->m;
  445. int n = csa->n;
  446. double *b = csa->b;
  447. double *c = csa->c;
  448. double *x = csa->x;
  449. double *y = csa->y;
  450. double *z = csa->z;
  451. double *D = csa->D;
  452. int i, j;
  453. double dp, dd, ex, ez, xz;
  454. /* factorize A*A' */
  455. for (j = 1; j <= n; j++) D[j] = 1.0;
  456. decomp_NE(csa);
  457. /* x~ = A'*inv(A*A')*b */
  458. for (i = 1; i <= m; i++) y[i] = b[i];
  459. solve_NE(csa, y);
  460. AT_by_vec(csa, y, x);
  461. /* y~ = inv(A*A')*A*c */
  462. A_by_vec(csa, c, y);
  463. solve_NE(csa, y);
  464. /* z~ = c - A'*y~ */
  465. AT_by_vec(csa, y,z);
  466. for (j = 1; j <= n; j++) z[j] = c[j] - z[j];
  467. /* use Mehrotra's heuristic in order to choose more appropriate
  468. starting point with positive components of vectors x and z */
  469. dp = dd = 0.0;
  470. for (j = 1; j <= n; j++)
  471. { if (dp < -1.5 * x[j]) dp = -1.5 * x[j];
  472. if (dd < -1.5 * z[j]) dd = -1.5 * z[j];
  473. }
  474. /* note that b = 0 involves x = 0, and c = 0 involves y = 0 and
  475. z = 0, so we need to be careful */
  476. if (dp == 0.0) dp = 1.5;
  477. if (dd == 0.0) dd = 1.5;
  478. ex = ez = xz = 0.0;
  479. for (j = 1; j <= n; j++)
  480. { ex += (x[j] + dp);
  481. ez += (z[j] + dd);
  482. xz += (x[j] + dp) * (z[j] + dd);
  483. }
  484. dp += 0.5 * (xz / ez);
  485. dd += 0.5 * (xz / ex);
  486. for (j = 1; j <= n; j++)
  487. { x[j] += dp;
  488. z[j] += dd;
  489. xassert(x[j] > 0.0 && z[j] > 0.0);
  490. }
  491. return;
  492. }
  493. /***********************************************************************
  494. * basic_info - perform basic computations at the current point
  495. *
  496. * This routine computes the following quantities at the current point:
  497. *
  498. * 1) value of the objective function:
  499. *
  500. * F = c'*x + c[0]
  501. *
  502. * 2) relative primal infeasibility:
  503. *
  504. * rpi = ||A*x-b|| / (1+||b||)
  505. *
  506. * 3) relative dual infeasibility:
  507. *
  508. * rdi = ||A'*y+z-c|| / (1+||c||)
  509. *
  510. * 4) primal-dual gap (relative difference between the primal and the
  511. * dual objective function values):
  512. *
  513. * gap = |c'*x-b'*y| / (1+|c'*x|)
  514. *
  515. * 5) merit function:
  516. *
  517. * phi = ||A*x-b|| / max(1,||b||) + ||A'*y+z-c|| / max(1,||c||) +
  518. *
  519. * + |c'*x-b'*y| / max(1,||b||,||c||)
  520. *
  521. * 6) duality measure:
  522. *
  523. * mu = x'*z / n
  524. *
  525. * 7) the ratio of infeasibility to mu:
  526. *
  527. * rmu = max(||A*x-b||,||A'*y+z-c||) / mu
  528. *
  529. * where ||*|| denotes euclidian norm, *' denotes transposition. */
  530. static void basic_info(struct csa *csa)
  531. { int m = csa->m;
  532. int n = csa->n;
  533. double *b = csa->b;
  534. double *c = csa->c;
  535. double *x = csa->x;
  536. double *y = csa->y;
  537. double *z = csa->z;
  538. int i, j;
  539. double norm1, bnorm, norm2, cnorm, cx, by, *work, temp;
  540. /* compute value of the objective function */
  541. temp = c[0];
  542. for (j = 1; j <= n; j++) temp += c[j] * x[j];
  543. csa->obj = temp;
  544. /* norm1 = ||A*x-b|| */
  545. work = xcalloc(1+m, sizeof(double));
  546. A_by_vec(csa, x, work);
  547. norm1 = 0.0;
  548. for (i = 1; i <= m; i++)
  549. norm1 += (work[i] - b[i]) * (work[i] - b[i]);
  550. norm1 = sqrt(norm1);
  551. xfree(work);
  552. /* bnorm = ||b|| */
  553. bnorm = 0.0;
  554. for (i = 1; i <= m; i++) bnorm += b[i] * b[i];
  555. bnorm = sqrt(bnorm);
  556. /* compute relative primal infeasibility */
  557. csa->rpi = norm1 / (1.0 + bnorm);
  558. /* norm2 = ||A'*y+z-c|| */
  559. work = xcalloc(1+n, sizeof(double));
  560. AT_by_vec(csa, y, work);
  561. norm2 = 0.0;
  562. for (j = 1; j <= n; j++)
  563. norm2 += (work[j] + z[j] - c[j]) * (work[j] + z[j] - c[j]);
  564. norm2 = sqrt(norm2);
  565. xfree(work);
  566. /* cnorm = ||c|| */
  567. cnorm = 0.0;
  568. for (j = 1; j <= n; j++) cnorm += c[j] * c[j];
  569. cnorm = sqrt(cnorm);
  570. /* compute relative dual infeasibility */
  571. csa->rdi = norm2 / (1.0 + cnorm);
  572. /* by = b'*y */
  573. by = 0.0;
  574. for (i = 1; i <= m; i++) by += b[i] * y[i];
  575. /* cx = c'*x */
  576. cx = 0.0;
  577. for (j = 1; j <= n; j++) cx += c[j] * x[j];
  578. /* compute primal-dual gap */
  579. csa->gap = fabs(cx - by) / (1.0 + fabs(cx));
  580. /* compute merit function */
  581. csa->phi = 0.0;
  582. csa->phi += norm1 / (bnorm > 1.0 ? bnorm : 1.0);
  583. csa->phi += norm2 / (cnorm > 1.0 ? cnorm : 1.0);
  584. temp = 1.0;
  585. if (temp < bnorm) temp = bnorm;
  586. if (temp < cnorm) temp = cnorm;
  587. csa->phi += fabs(cx - by) / temp;
  588. /* compute duality measure */
  589. temp = 0.0;
  590. for (j = 1; j <= n; j++) temp += x[j] * z[j];
  591. csa->mu = temp / (double)n;
  592. /* compute the ratio of infeasibility to mu */
  593. csa->rmu = (norm1 > norm2 ? norm1 : norm2) / csa->mu;
  594. return;
  595. }
  596. /***********************************************************************
  597. * make_step - compute next point using Mehrotra's technique
  598. *
  599. * This routine computes the next point using the predictor-corrector
  600. * technique proposed in the paper:
  601. *
  602. * S. Mehrotra. On the implementation of a primal-dual interior point
  603. * method. SIAM J. on Optim., 2(4), pp. 575-601, 1992.
  604. *
  605. * At first, the routine computes so called affine scaling (predictor)
  606. * direction (dx_aff,dy_aff,dz_aff) which is a solution of the system:
  607. *
  608. * A*dx_aff = b - A*x
  609. *
  610. * A'*dy_aff + dz_aff = c - A'*y - z
  611. *
  612. * Z*dx_aff + X*dz_aff = - X*Z*e
  613. *
  614. * where (x,y,z) is the current point, X = diag(x[j]), Z = diag(z[j]),
  615. * e = (1,...,1)'.
  616. *
  617. * Then, the routine computes the centering parameter sigma, using the
  618. * following Mehrotra's heuristic:
  619. *
  620. * alfa_aff_p = inf{0 <= alfa <= 1 | x+alfa*dx_aff >= 0}
  621. *
  622. * alfa_aff_d = inf{0 <= alfa <= 1 | z+alfa*dz_aff >= 0}
  623. *
  624. * mu_aff = (x+alfa_aff_p*dx_aff)'*(z+alfa_aff_d*dz_aff)/n
  625. *
  626. * sigma = (mu_aff/mu)^3
  627. *
  628. * where alfa_aff_p is the maximal stepsize along the affine scaling
  629. * direction in the primal space, alfa_aff_d is the maximal stepsize
  630. * along the same direction in the dual space.
  631. *
  632. * After determining sigma the routine computes so called centering
  633. * (corrector) direction (dx_cc,dy_cc,dz_cc) which is the solution of
  634. * the system:
  635. *
  636. * A*dx_cc = 0
  637. *
  638. * A'*dy_cc + dz_cc = 0
  639. *
  640. * Z*dx_cc + X*dz_cc = sigma*mu*e - X*Z*e
  641. *
  642. * Finally, the routine computes the combined direction
  643. *
  644. * (dx,dy,dz) = (dx_aff,dy_aff,dz_aff) + (dx_cc,dy_cc,dz_cc)
  645. *
  646. * and determines maximal primal and dual stepsizes along the combined
  647. * direction:
  648. *
  649. * alfa_max_p = inf{0 <= alfa <= 1 | x+alfa*dx >= 0}
  650. *
  651. * alfa_max_d = inf{0 <= alfa <= 1 | z+alfa*dz >= 0}
  652. *
  653. * In order to prevent the next point to be too close to the boundary
  654. * of the positive ortant, the routine decreases maximal stepsizes:
  655. *
  656. * alfa_p = gamma_p * alfa_max_p
  657. *
  658. * alfa_d = gamma_d * alfa_max_d
  659. *
  660. * where gamma_p and gamma_d are scaling factors, and computes the next
  661. * point:
  662. *
  663. * x_new = x + alfa_p * dx
  664. *
  665. * y_new = y + alfa_d * dy
  666. *
  667. * z_new = z + alfa_d * dz
  668. *
  669. * which becomes the current point on the next iteration. */
  670. static int make_step(struct csa *csa)
  671. { int m = csa->m;
  672. int n = csa->n;
  673. double *b = csa->b;
  674. double *c = csa->c;
  675. double *x = csa->x;
  676. double *y = csa->y;
  677. double *z = csa->z;
  678. double *dx_aff = csa->dx_aff;
  679. double *dy_aff = csa->dy_aff;
  680. double *dz_aff = csa->dz_aff;
  681. double *dx_cc = csa->dx_cc;
  682. double *dy_cc = csa->dy_cc;
  683. double *dz_cc = csa->dz_cc;
  684. double *dx = csa->dx;
  685. double *dy = csa->dy;
  686. double *dz = csa->dz;
  687. int i, j, ret = 0;
  688. double temp, gamma_p, gamma_d, *p, *q, *r;
  689. /* allocate working arrays */
  690. p = xcalloc(1+m, sizeof(double));
  691. q = xcalloc(1+n, sizeof(double));
  692. r = xcalloc(1+n, sizeof(double));
  693. /* p = b - A*x */
  694. A_by_vec(csa, x, p);
  695. for (i = 1; i <= m; i++) p[i] = b[i] - p[i];
  696. /* q = c - A'*y - z */
  697. AT_by_vec(csa, y,q);
  698. for (j = 1; j <= n; j++) q[j] = c[j] - q[j] - z[j];
  699. /* r = - X * Z * e */
  700. for (j = 1; j <= n; j++) r[j] = - x[j] * z[j];
  701. /* solve the first Newtonian system */
  702. if (solve_NS(csa, p, q, r, dx_aff, dy_aff, dz_aff))
  703. { ret = 1;
  704. goto done;
  705. }
  706. /* alfa_aff_p = inf{0 <= alfa <= 1 | x + alfa*dx_aff >= 0} */
  707. /* alfa_aff_d = inf{0 <= alfa <= 1 | z + alfa*dz_aff >= 0} */
  708. csa->alfa_aff_p = csa->alfa_aff_d = 1.0;
  709. for (j = 1; j <= n; j++)
  710. { if (dx_aff[j] < 0.0)
  711. { temp = - x[j] / dx_aff[j];
  712. if (csa->alfa_aff_p > temp) csa->alfa_aff_p = temp;
  713. }
  714. if (dz_aff[j] < 0.0)
  715. { temp = - z[j] / dz_aff[j];
  716. if (csa->alfa_aff_d > temp) csa->alfa_aff_d = temp;
  717. }
  718. }
  719. /* mu_aff = (x+alfa_aff_p*dx_aff)' * (z+alfa_aff_d*dz_aff) / n */
  720. temp = 0.0;
  721. for (j = 1; j <= n; j++)
  722. temp += (x[j] + csa->alfa_aff_p * dx_aff[j]) *
  723. (z[j] + csa->alfa_aff_d * dz_aff[j]);
  724. csa->mu_aff = temp / (double)n;
  725. /* sigma = (mu_aff/mu)^3 */
  726. temp = csa->mu_aff / csa->mu;
  727. csa->sigma = temp * temp * temp;
  728. /* p = 0 */
  729. for (i = 1; i <= m; i++) p[i] = 0.0;
  730. /* q = 0 */
  731. for (j = 1; j <= n; j++) q[j] = 0.0;
  732. /* r = sigma * mu * e - X * Z * e */
  733. for (j = 1; j <= n; j++)
  734. r[j] = csa->sigma * csa->mu - dx_aff[j] * dz_aff[j];
  735. /* solve the second Newtonian system with the same coefficients
  736. but with altered right-hand sides */
  737. if (solve_NS(csa, p, q, r, dx_cc, dy_cc, dz_cc))
  738. { ret = 1;
  739. goto done;
  740. }
  741. /* (dx,dy,dz) = (dx_aff,dy_aff,dz_aff) + (dx_cc,dy_cc,dz_cc) */
  742. for (j = 1; j <= n; j++) dx[j] = dx_aff[j] + dx_cc[j];
  743. for (i = 1; i <= m; i++) dy[i] = dy_aff[i] + dy_cc[i];
  744. for (j = 1; j <= n; j++) dz[j] = dz_aff[j] + dz_cc[j];
  745. /* alfa_max_p = inf{0 <= alfa <= 1 | x + alfa*dx >= 0} */
  746. /* alfa_max_d = inf{0 <= alfa <= 1 | z + alfa*dz >= 0} */
  747. csa->alfa_max_p = csa->alfa_max_d = 1.0;
  748. for (j = 1; j <= n; j++)
  749. { if (dx[j] < 0.0)
  750. { temp = - x[j] / dx[j];
  751. if (csa->alfa_max_p > temp) csa->alfa_max_p = temp;
  752. }
  753. if (dz[j] < 0.0)
  754. { temp = - z[j] / dz[j];
  755. if (csa->alfa_max_d > temp) csa->alfa_max_d = temp;
  756. }
  757. }
  758. /* determine scale factors (not implemented yet) */
  759. gamma_p = 0.90;
  760. gamma_d = 0.90;
  761. /* compute the next point */
  762. for (j = 1; j <= n; j++)
  763. { x[j] += gamma_p * csa->alfa_max_p * dx[j];
  764. xassert(x[j] > 0.0);
  765. }
  766. for (i = 1; i <= m; i++)
  767. y[i] += gamma_d * csa->alfa_max_d * dy[i];
  768. for (j = 1; j <= n; j++)
  769. { z[j] += gamma_d * csa->alfa_max_d * dz[j];
  770. xassert(z[j] > 0.0);
  771. }
  772. done: /* free working arrays */
  773. xfree(p);
  774. xfree(q);
  775. xfree(r);
  776. return ret;
  777. }
  778. /***********************************************************************
  779. * terminate - deallocate common storage area
  780. *
  781. * This routine frees all memory allocated to the common storage area
  782. * used by interior-point method routines. */
  783. static void terminate(struct csa *csa)
  784. { xfree(csa->D);
  785. xfree(csa->P);
  786. xfree(csa->S_ptr);
  787. xfree(csa->S_ind);
  788. xfree(csa->S_val);
  789. xfree(csa->S_diag);
  790. xfree(csa->U_ptr);
  791. xfree(csa->U_ind);
  792. xfree(csa->U_val);
  793. xfree(csa->U_diag);
  794. xfree(csa->phi_min);
  795. xfree(csa->best_x);
  796. xfree(csa->best_y);
  797. xfree(csa->best_z);
  798. xfree(csa->dx_aff);
  799. xfree(csa->dy_aff);
  800. xfree(csa->dz_aff);
  801. xfree(csa->dx_cc);
  802. xfree(csa->dy_cc);
  803. xfree(csa->dz_cc);
  804. return;
  805. }
  806. /***********************************************************************
  807. * ipm_main - main interior-point method routine
  808. *
  809. * This is a main routine of the primal-dual interior-point method.
  810. *
  811. * The routine ipm_main returns one of the following codes:
  812. *
  813. * 0 - optimal solution found;
  814. * 1 - problem has no feasible (primal or dual) solution;
  815. * 2 - no convergence;
  816. * 3 - iteration limit exceeded;
  817. * 4 - numeric instability on solving Newtonian system.
  818. *
  819. * In case of non-zero return code the routine returns the best point,
  820. * which has been reached during optimization. */
  821. static int ipm_main(struct csa *csa)
  822. { int m = csa->m;
  823. int n = csa->n;
  824. int i, j, status;
  825. double temp;
  826. /* choose initial point using Mehrotra's heuristic */
  827. if (csa->parm->msg_lev >= GLP_MSG_ALL)
  828. xprintf("Guessing initial point...\n");
  829. initial_point(csa);
  830. /* main loop starts here */
  831. if (csa->parm->msg_lev >= GLP_MSG_ALL)
  832. xprintf("Optimization begins...\n");
  833. for (;;)
  834. { /* perform basic computations at the current point */
  835. basic_info(csa);
  836. /* save initial value of rmu */
  837. if (csa->iter == 0) csa->rmu0 = csa->rmu;
  838. /* accumulate values of min(phi[k]) and save the best point */
  839. xassert(csa->iter <= ITER_MAX);
  840. if (csa->iter == 0 || csa->phi_min[csa->iter-1] > csa->phi)
  841. { csa->phi_min[csa->iter] = csa->phi;
  842. csa->best_iter = csa->iter;
  843. for (j = 1; j <= n; j++) csa->best_x[j] = csa->x[j];
  844. for (i = 1; i <= m; i++) csa->best_y[i] = csa->y[i];
  845. for (j = 1; j <= n; j++) csa->best_z[j] = csa->z[j];
  846. csa->best_obj = csa->obj;
  847. }
  848. else
  849. csa->phi_min[csa->iter] = csa->phi_min[csa->iter-1];
  850. /* display information at the current point */
  851. if (csa->parm->msg_lev >= GLP_MSG_ON)
  852. xprintf("%3d: obj = %17.9e; rpi = %8.1e; rdi = %8.1e; gap ="
  853. " %8.1e\n", csa->iter, csa->obj, csa->rpi, csa->rdi,
  854. csa->gap);
  855. /* check if the current point is optimal */
  856. if (csa->rpi < 1e-8 && csa->rdi < 1e-8 && csa->gap < 1e-8)
  857. { if (csa->parm->msg_lev >= GLP_MSG_ALL)
  858. xprintf("OPTIMAL SOLUTION FOUND\n");
  859. status = 0;
  860. break;
  861. }
  862. /* check if the problem has no feasible solution */
  863. temp = 1e5 * csa->phi_min[csa->iter];
  864. if (temp < 1e-8) temp = 1e-8;
  865. if (csa->phi >= temp)
  866. { if (csa->parm->msg_lev >= GLP_MSG_ALL)
  867. xprintf("PROBLEM HAS NO FEASIBLE PRIMAL/DUAL SOLUTION\n")
  868. ;
  869. status = 1;
  870. break;
  871. }
  872. /* check for very slow convergence or divergence */
  873. if (((csa->rpi >= 1e-8 || csa->rdi >= 1e-8) && csa->rmu /
  874. csa->rmu0 >= 1e6) ||
  875. (csa->iter >= 30 && csa->phi_min[csa->iter] >= 0.5 *
  876. csa->phi_min[csa->iter - 30]))
  877. { if (csa->parm->msg_lev >= GLP_MSG_ALL)
  878. xprintf("NO CONVERGENCE; SEARCH TERMINATED\n");
  879. status = 2;
  880. break;
  881. }
  882. /* check for maximal number of iterations */
  883. if (csa->iter == ITER_MAX)
  884. { if (csa->parm->msg_lev >= GLP_MSG_ALL)
  885. xprintf("ITERATION LIMIT EXCEEDED; SEARCH TERMINATED\n");
  886. status = 3;
  887. break;
  888. }
  889. /* start the next iteration */
  890. csa->iter++;
  891. /* factorize normal equation system */
  892. for (j = 1; j <= n; j++) csa->D[j] = csa->x[j] / csa->z[j];
  893. decomp_NE(csa);
  894. /* compute the next point using Mehrotra's predictor-corrector
  895. technique */
  896. if (make_step(csa))
  897. { if (csa->parm->msg_lev >= GLP_MSG_ALL)
  898. xprintf("NUMERIC INSTABILITY; SEARCH TERMINATED\n");
  899. status = 4;
  900. break;
  901. }
  902. }
  903. /* restore the best point */
  904. if (status != 0)
  905. { for (j = 1; j <= n; j++) csa->x[j] = csa->best_x[j];
  906. for (i = 1; i <= m; i++) csa->y[i] = csa->best_y[i];
  907. for (j = 1; j <= n; j++) csa->z[j] = csa->best_z[j];
  908. if (csa->parm->msg_lev >= GLP_MSG_ALL)
  909. xprintf("Best point %17.9e was reached on iteration %d\n",
  910. csa->best_obj, csa->best_iter);
  911. }
  912. /* return to the calling program */
  913. return status;
  914. }
  915. /***********************************************************************
  916. * NAME
  917. *
  918. * ipm_solve - core LP solver based on the interior-point method
  919. *
  920. * SYNOPSIS
  921. *
  922. * #include "glpipm.h"
  923. * int ipm_solve(glp_prob *P, const glp_iptcp *parm);
  924. *
  925. * DESCRIPTION
  926. *
  927. * The routine ipm_solve is a core LP solver based on the primal-dual
  928. * interior-point method.
  929. *
  930. * The routine assumes the following standard formulation of LP problem
  931. * to be solved:
  932. *
  933. * minimize
  934. *
  935. * F = c[0] + c[1]*x[1] + c[2]*x[2] + ... + c[n]*x[n]
  936. *
  937. * subject to linear constraints
  938. *
  939. * a[1,1]*x[1] + a[1,2]*x[2] + ... + a[1,n]*x[n] = b[1]
  940. *
  941. * a[2,1]*x[1] + a[2,2]*x[2] + ... + a[2,n]*x[n] = b[2]
  942. *
  943. * . . . . . .
  944. *
  945. * a[m,1]*x[1] + a[m,2]*x[2] + ... + a[m,n]*x[n] = b[m]
  946. *
  947. * and non-negative variables
  948. *
  949. * x[1] >= 0, x[2] >= 0, ..., x[n] >= 0
  950. *
  951. * where:
  952. * F is the objective function;
  953. * x[1], ..., x[n] are (structural) variables;
  954. * c[0] is a constant term of the objective function;
  955. * c[1], ..., c[n] are objective coefficients;
  956. * a[1,1], ..., a[m,n] are constraint coefficients;
  957. * b[1], ..., b[n] are right-hand sides.
  958. *
  959. * The solution is three vectors x, y, and z, which are stored by the
  960. * routine in the arrays x, y, and z, respectively. These vectors
  961. * correspond to the best primal-dual point found during optimization.
  962. * They are approximate solution of the following system (which is the
  963. * Karush-Kuhn-Tucker optimality conditions):
  964. *
  965. * A*x = b (primal feasibility condition)
  966. *
  967. * A'*y + z = c (dual feasibility condition)
  968. *
  969. * x'*z = 0 (primal-dual complementarity condition)
  970. *
  971. * x >= 0, z >= 0 (non-negativity condition)
  972. *
  973. * where:
  974. * x[1], ..., x[n] are primal (structural) variables;
  975. * y[1], ..., y[m] are dual variables (Lagrange multipliers) for
  976. * equality constraints;
  977. * z[1], ..., z[n] are dual variables (Lagrange multipliers) for
  978. * non-negativity constraints.
  979. *
  980. * RETURNS
  981. *
  982. * 0 LP has been successfully solved.
  983. *
  984. * GLP_ENOCVG
  985. * No convergence.
  986. *
  987. * GLP_EITLIM
  988. * Iteration limit exceeded.
  989. *
  990. * GLP_EINSTAB
  991. * Numeric instability on solving Newtonian system.
  992. *
  993. * In case of non-zero return code the routine returns the best point,
  994. * which has been reached during optimization. */
  995. int ipm_solve(glp_prob *P, const glp_iptcp *parm)
  996. { struct csa _dsa, *csa = &_dsa;
  997. int m = P->m;
  998. int n = P->n;
  999. int nnz = P->nnz;
  1000. GLPROW *row;
  1001. GLPCOL *col;
  1002. GLPAIJ *aij;
  1003. int i, j, loc, ret, *A_ind, *A_ptr;
  1004. double dir, *A_val, *b, *c, *x, *y, *z;
  1005. xassert(m > 0);
  1006. xassert(n > 0);
  1007. /* allocate working arrays */
  1008. A_ptr = xcalloc(1+m+1, sizeof(int));
  1009. A_ind = xcalloc(1+nnz, sizeof(int));
  1010. A_val = xcalloc(1+nnz, sizeof(double));
  1011. b = xcalloc(1+m, sizeof(double));
  1012. c = xcalloc(1+n, sizeof(double));
  1013. x = xcalloc(1+n, sizeof(double));
  1014. y = xcalloc(1+m, sizeof(double));
  1015. z = xcalloc(1+n, sizeof(double));
  1016. /* prepare rows and constraint coefficients */
  1017. loc = 1;
  1018. for (i = 1; i <= m; i++)
  1019. { row = P->row[i];
  1020. xassert(row->type == GLP_FX);
  1021. b[i] = row->lb * row->rii;
  1022. A_ptr[i] = loc;
  1023. for (aij = row->ptr; aij != NULL; aij = aij->r_next)
  1024. { A_ind[loc] = aij->col->j;
  1025. A_val[loc] = row->rii * aij->val * aij->col->sjj;
  1026. loc++;
  1027. }
  1028. }
  1029. A_ptr[m+1] = loc;
  1030. xassert(loc-1 == nnz);
  1031. /* prepare columns and objective coefficients */
  1032. if (P->dir == GLP_MIN)
  1033. dir = +1.0;
  1034. else if (P->dir == GLP_MAX)
  1035. dir = -1.0;
  1036. else
  1037. xassert(P != P);
  1038. c[0] = dir * P->c0;
  1039. for (j = 1; j <= n; j++)
  1040. { col = P->col[j];
  1041. xassert(col->type == GLP_LO && col->lb == 0.0);
  1042. c[j] = dir * col->coef * col->sjj;
  1043. }
  1044. /* allocate and initialize the common storage area */
  1045. csa->m = m;
  1046. csa->n = n;
  1047. csa->A_ptr = A_ptr;
  1048. csa->A_ind = A_ind;
  1049. csa->A_val = A_val;
  1050. csa->b = b;
  1051. csa->c = c;
  1052. csa->x = x;
  1053. csa->y = y;
  1054. csa->z = z;
  1055. csa->parm = parm;
  1056. initialize(csa);
  1057. /* solve LP with the interior-point method */
  1058. ret = ipm_main(csa);
  1059. /* deallocate the common storage area */
  1060. terminate(csa);
  1061. /* determine solution status */
  1062. if (ret == 0)
  1063. { /* optimal solution found */
  1064. P->ipt_stat = GLP_OPT;
  1065. ret = 0;
  1066. }
  1067. else if (ret == 1)
  1068. { /* problem has no feasible (primal or dual) solution */
  1069. P->ipt_stat = GLP_NOFEAS;
  1070. ret = 0;
  1071. }
  1072. else if (ret == 2)
  1073. { /* no convergence */
  1074. P->ipt_stat = GLP_INFEAS;
  1075. ret = GLP_ENOCVG;
  1076. }
  1077. else if (ret == 3)
  1078. { /* iteration limit exceeded */
  1079. P->ipt_stat = GLP_INFEAS;
  1080. ret = GLP_EITLIM;
  1081. }
  1082. else if (ret == 4)
  1083. { /* numeric instability on solving Newtonian system */
  1084. P->ipt_stat = GLP_INFEAS;
  1085. ret = GLP_EINSTAB;
  1086. }
  1087. else
  1088. xassert(ret != ret);
  1089. /* store row solution components */
  1090. for (i = 1; i <= m; i++)
  1091. { row = P->row[i];
  1092. row->pval = row->lb;
  1093. row->dval = dir * y[i] * row->rii;
  1094. }
  1095. /* store column solution components */
  1096. P->ipt_obj = P->c0;
  1097. for (j = 1; j <= n; j++)
  1098. { col = P->col[j];
  1099. col->pval = x[j] * col->sjj;
  1100. col->dval = dir * z[j] / col->sjj;
  1101. P->ipt_obj += col->coef * col->pval;
  1102. }
  1103. /* free working arrays */
  1104. xfree(A_ptr);
  1105. xfree(A_ind);
  1106. xfree(A_val);
  1107. xfree(b);
  1108. xfree(c);
  1109. xfree(x);
  1110. xfree(y);
  1111. xfree(z);
  1112. return ret;
  1113. }
  1114. /* eof */