glpnpp04.c 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415
  1. /* glpnpp04.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 "glpnpp.h"
  24. /***********************************************************************
  25. * NAME
  26. *
  27. * npp_binarize_prob - binarize MIP problem
  28. *
  29. * SYNOPSIS
  30. *
  31. * #include "glpnpp.h"
  32. * int npp_binarize_prob(NPP *npp);
  33. *
  34. * DESCRIPTION
  35. *
  36. * The routine npp_binarize_prob replaces in the original MIP problem
  37. * every integer variable:
  38. *
  39. * l[q] <= x[q] <= u[q], (1)
  40. *
  41. * where l[q] < u[q], by an equivalent sum of binary variables.
  42. *
  43. * RETURNS
  44. *
  45. * The routine returns the number of integer variables for which the
  46. * transformation failed, because u[q] - l[q] > d_max.
  47. *
  48. * PROBLEM TRANSFORMATION
  49. *
  50. * If variable x[q] has non-zero lower bound, it is first processed
  51. * with the routine npp_lbnd_col. Thus, we can assume that:
  52. *
  53. * 0 <= x[q] <= u[q]. (2)
  54. *
  55. * If u[q] = 1, variable x[q] is already binary, so further processing
  56. * is not needed. Let, therefore, that 2 <= u[q] <= d_max, and n be a
  57. * smallest integer such that u[q] <= 2^n - 1 (n >= 2, since u[q] >= 2).
  58. * Then variable x[q] can be replaced by the following sum:
  59. *
  60. * n-1
  61. * x[q] = sum 2^k x[k], (3)
  62. * k=0
  63. *
  64. * where x[k] are binary columns (variables). If u[q] < 2^n - 1, the
  65. * following additional inequality constraint must be also included in
  66. * the transformed problem:
  67. *
  68. * n-1
  69. * sum 2^k x[k] <= u[q]. (4)
  70. * k=0
  71. *
  72. * Note: Assuming that in the transformed problem x[q] becomes binary
  73. * variable x[0], this transformation causes new n-1 binary variables
  74. * to appear.
  75. *
  76. * Substituting x[q] from (3) to the objective row gives:
  77. *
  78. * z = sum c[j] x[j] + c[0] =
  79. * j
  80. *
  81. * = sum c[j] x[j] + c[q] x[q] + c[0] =
  82. * j!=q
  83. * n-1
  84. * = sum c[j] x[j] + c[q] sum 2^k x[k] + c[0] =
  85. * j!=q k=0
  86. * n-1
  87. * = sum c[j] x[j] + sum c[k] x[k] + c[0],
  88. * j!=q k=0
  89. *
  90. * where:
  91. *
  92. * c[k] = 2^k c[q], k = 0, ..., n-1. (5)
  93. *
  94. * And substituting x[q] from (3) to i-th constraint row i gives:
  95. *
  96. * L[i] <= sum a[i,j] x[j] <= U[i] ==>
  97. * j
  98. *
  99. * L[i] <= sum a[i,j] x[j] + a[i,q] x[q] <= U[i] ==>
  100. * j!=q
  101. * n-1
  102. * L[i] <= sum a[i,j] x[j] + a[i,q] sum 2^k x[k] <= U[i] ==>
  103. * j!=q k=0
  104. * n-1
  105. * L[i] <= sum a[i,j] x[j] + sum a[i,k] x[k] <= U[i],
  106. * j!=q k=0
  107. *
  108. * where:
  109. *
  110. * a[i,k] = 2^k a[i,q], k = 0, ..., n-1. (6)
  111. *
  112. * RECOVERING SOLUTION
  113. *
  114. * Value of variable x[q] is computed with formula (3). */
  115. struct binarize
  116. { int q;
  117. /* column reference number for x[q] = x[0] */
  118. int j;
  119. /* column reference number for x[1]; x[2] has reference number
  120. j+1, x[3] - j+2, etc. */
  121. int n;
  122. /* total number of binary variables, n >= 2 */
  123. };
  124. static int rcv_binarize_prob(NPP *npp, void *info);
  125. int npp_binarize_prob(NPP *npp)
  126. { /* binarize MIP problem */
  127. struct binarize *info;
  128. NPPROW *row;
  129. NPPCOL *col, *bin;
  130. NPPAIJ *aij;
  131. int u, n, k, temp, nfails, nvars, nbins, nrows;
  132. /* new variables will be added to the end of the column list, so
  133. we go from the end to beginning of the column list */
  134. nfails = nvars = nbins = nrows = 0;
  135. for (col = npp->c_tail; col != NULL; col = col->prev)
  136. { /* skip continuous variable */
  137. if (!col->is_int) continue;
  138. /* skip fixed variable */
  139. if (col->lb == col->ub) continue;
  140. /* skip binary variable */
  141. if (col->lb == 0.0 && col->ub == 1.0) continue;
  142. /* check if the transformation is applicable */
  143. if (col->lb < -1e6 || col->ub > +1e6 ||
  144. col->ub - col->lb > 4095.0)
  145. { /* unfortunately, not */
  146. nfails++;
  147. continue;
  148. }
  149. /* process integer non-binary variable x[q] */
  150. nvars++;
  151. /* make x[q] non-negative, if its lower bound is non-zero */
  152. if (col->lb != 0.0)
  153. npp_lbnd_col(npp, col);
  154. /* now 0 <= x[q] <= u[q] */
  155. xassert(col->lb == 0.0);
  156. u = (int)col->ub;
  157. xassert(col->ub == (double)u);
  158. /* if x[q] is binary, further processing is not needed */
  159. if (u == 1) continue;
  160. /* determine smallest n such that u <= 2^n - 1 (thus, n is the
  161. number of binary variables needed) */
  162. n = 2, temp = 4;
  163. while (u >= temp)
  164. n++, temp += temp;
  165. nbins += n;
  166. /* create transformation stack entry */
  167. info = npp_push_tse(npp,
  168. rcv_binarize_prob, sizeof(struct binarize));
  169. info->q = col->j;
  170. info->j = 0; /* will be set below */
  171. info->n = n;
  172. /* if u < 2^n - 1, we need one additional row for (4) */
  173. if (u < temp - 1)
  174. { row = npp_add_row(npp), nrows++;
  175. row->lb = -DBL_MAX, row->ub = u;
  176. }
  177. else
  178. row = NULL;
  179. /* in the transformed problem variable x[q] becomes binary
  180. variable x[0], so its objective and constraint coefficients
  181. are not changed */
  182. col->ub = 1.0;
  183. /* include x[0] into constraint (4) */
  184. if (row != NULL)
  185. npp_add_aij(npp, row, col, 1.0);
  186. /* add other binary variables x[1], ..., x[n-1] */
  187. for (k = 1, temp = 2; k < n; k++, temp += temp)
  188. { /* add new binary variable x[k] */
  189. bin = npp_add_col(npp);
  190. bin->is_int = 1;
  191. bin->lb = 0.0, bin->ub = 1.0;
  192. bin->coef = (double)temp * col->coef;
  193. /* store column reference number for x[1] */
  194. if (info->j == 0)
  195. info->j = bin->j;
  196. else
  197. xassert(info->j + (k-1) == bin->j);
  198. /* duplicate constraint coefficients for x[k]; this also
  199. automatically includes x[k] into constraint (4) */
  200. for (aij = col->ptr; aij != NULL; aij = aij->c_next)
  201. npp_add_aij(npp, aij->row, bin, (double)temp * aij->val);
  202. }
  203. }
  204. if (nvars > 0)
  205. xprintf("%d integer variable(s) were replaced by %d binary one"
  206. "s\n", nvars, nbins);
  207. if (nrows > 0)
  208. xprintf("%d row(s) were added due to binarization\n", nrows);
  209. if (nfails > 0)
  210. xprintf("Binarization failed for %d integer variable(s)\n",
  211. nfails);
  212. return nfails;
  213. }
  214. static int rcv_binarize_prob(NPP *npp, void *_info)
  215. { /* recovery binarized variable */
  216. struct binarize *info = _info;
  217. int k, temp;
  218. double sum;
  219. /* compute value of x[q]; see formula (3) */
  220. sum = npp->c_value[info->q];
  221. for (k = 1, temp = 2; k < info->n; k++, temp += temp)
  222. sum += (double)temp * npp->c_value[info->j + (k-1)];
  223. npp->c_value[info->q] = sum;
  224. return 0;
  225. }
  226. /**********************************************************************/
  227. struct elem
  228. { /* linear form element a[j] x[j] */
  229. double aj;
  230. /* non-zero coefficient value */
  231. NPPCOL *xj;
  232. /* pointer to variable (column) */
  233. struct elem *next;
  234. /* pointer to another term */
  235. };
  236. static struct elem *copy_form(NPP *npp, NPPROW *row, double s)
  237. { /* copy linear form */
  238. NPPAIJ *aij;
  239. struct elem *ptr, *e;
  240. ptr = NULL;
  241. for (aij = row->ptr; aij != NULL; aij = aij->r_next)
  242. { e = dmp_get_atom(npp->pool, sizeof(struct elem));
  243. e->aj = s * aij->val;
  244. e->xj = aij->col;
  245. e->next = ptr;
  246. ptr = e;
  247. }
  248. return ptr;
  249. }
  250. static void drop_form(NPP *npp, struct elem *ptr)
  251. { /* drop linear form */
  252. struct elem *e;
  253. while (ptr != NULL)
  254. { e = ptr;
  255. ptr = e->next;
  256. dmp_free_atom(npp->pool, e, sizeof(struct elem));
  257. }
  258. return;
  259. }
  260. /***********************************************************************
  261. * NAME
  262. *
  263. * npp_is_packing - test if constraint is packing inequality
  264. *
  265. * SYNOPSIS
  266. *
  267. * #include "glpnpp.h"
  268. * int npp_is_packing(NPP *npp, NPPROW *row);
  269. *
  270. * RETURNS
  271. *
  272. * If the specified row (constraint) is packing inequality (see below),
  273. * the routine npp_is_packing returns non-zero. Otherwise, it returns
  274. * zero.
  275. *
  276. * PACKING INEQUALITIES
  277. *
  278. * In canonical format the packing inequality is the following:
  279. *
  280. * sum x[j] <= 1, (1)
  281. * j in J
  282. *
  283. * where all variables x[j] are binary. This inequality expresses the
  284. * condition that in any integer feasible solution at most one variable
  285. * from set J can take non-zero (unity) value while other variables
  286. * must be equal to zero. W.l.o.g. it is assumed that |J| >= 2, because
  287. * if J is empty or |J| = 1, the inequality (1) is redundant.
  288. *
  289. * In general case the packing inequality may include original variables
  290. * x[j] as well as their complements x~[j]:
  291. *
  292. * sum x[j] + sum x~[j] <= 1, (2)
  293. * j in Jp j in Jn
  294. *
  295. * where Jp and Jn are not intersected. Therefore, using substitution
  296. * x~[j] = 1 - x[j] gives the packing inequality in generalized format:
  297. *
  298. * sum x[j] - sum x[j] <= 1 - |Jn|. (3)
  299. * j in Jp j in Jn */
  300. int npp_is_packing(NPP *npp, NPPROW *row)
  301. { /* test if constraint is packing inequality */
  302. NPPCOL *col;
  303. NPPAIJ *aij;
  304. int b;
  305. xassert(npp == npp);
  306. if (!(row->lb == -DBL_MAX && row->ub != +DBL_MAX))
  307. return 0;
  308. b = 1;
  309. for (aij = row->ptr; aij != NULL; aij = aij->r_next)
  310. { col = aij->col;
  311. if (!(col->is_int && col->lb == 0.0 && col->ub == 1.0))
  312. return 0;
  313. if (aij->val == +1.0)
  314. ;
  315. else if (aij->val == -1.0)
  316. b--;
  317. else
  318. return 0;
  319. }
  320. if (row->ub != (double)b) return 0;
  321. return 1;
  322. }
  323. /***********************************************************************
  324. * NAME
  325. *
  326. * npp_hidden_packing - identify hidden packing inequality
  327. *
  328. * SYNOPSIS
  329. *
  330. * #include "glpnpp.h"
  331. * int npp_hidden_packing(NPP *npp, NPPROW *row);
  332. *
  333. * DESCRIPTION
  334. *
  335. * The routine npp_hidden_packing processes specified inequality
  336. * constraint, which includes only binary variables, and the number of
  337. * the variables is not less than two. If the original inequality is
  338. * equivalent to a packing inequality, the routine replaces it by this
  339. * equivalent inequality. If the original constraint is double-sided
  340. * inequality, it is replaced by a pair of single-sided inequalities,
  341. * if necessary.
  342. *
  343. * RETURNS
  344. *
  345. * If the original inequality constraint was replaced by equivalent
  346. * packing inequality, the routine npp_hidden_packing returns non-zero.
  347. * Otherwise, it returns zero.
  348. *
  349. * PROBLEM TRANSFORMATION
  350. *
  351. * Consider an inequality constraint:
  352. *
  353. * sum a[j] x[j] <= b, (1)
  354. * j in J
  355. *
  356. * where all variables x[j] are binary, and |J| >= 2. (In case of '>='
  357. * inequality it can be transformed to '<=' format by multiplying both
  358. * its sides by -1.)
  359. *
  360. * Let Jp = {j: a[j] > 0}, Jn = {j: a[j] < 0}. Performing substitution
  361. * x[j] = 1 - x~[j] for all j in Jn, we have:
  362. *
  363. * sum a[j] x[j] <= b ==>
  364. * j in J
  365. *
  366. * sum a[j] x[j] + sum a[j] x[j] <= b ==>
  367. * j in Jp j in Jn
  368. *
  369. * sum a[j] x[j] + sum a[j] (1 - x~[j]) <= b ==>
  370. * j in Jp j in Jn
  371. *
  372. * sum a[j] x[j] - sum a[j] x~[j] <= b - sum a[j].
  373. * j in Jp j in Jn j in Jn
  374. *
  375. * Thus, meaning the transformation above, we can assume that in
  376. * inequality (1) all coefficients a[j] are positive. Moreover, we can
  377. * assume that a[j] <= b. In fact, let a[j] > b; then the following
  378. * three cases are possible:
  379. *
  380. * 1) b < 0. In this case inequality (1) is infeasible, so the problem
  381. * has no feasible solution (see the routine npp_analyze_row);
  382. *
  383. * 2) b = 0. In this case inequality (1) is a forcing inequality on its
  384. * upper bound (see the routine npp_forcing row), from which it
  385. * follows that all variables x[j] should be fixed at zero;
  386. *
  387. * 3) b > 0. In this case inequality (1) defines an implied zero upper
  388. * bound for variable x[j] (see the routine npp_implied_bounds), from
  389. * which it follows that x[j] should be fixed at zero.
  390. *
  391. * It is assumed that all three cases listed above have been recognized
  392. * by the routine npp_process_prob, which performs basic MIP processing
  393. * prior to a call the routine npp_hidden_packing. So, if one of these
  394. * cases occurs, we should just skip processing such constraint.
  395. *
  396. * Thus, let 0 < a[j] <= b. Then it is obvious that constraint (1) is
  397. * equivalent to packing inquality only if:
  398. *
  399. * a[j] + a[k] > b + eps (2)
  400. *
  401. * for all j, k in J, j != k, where eps is an absolute tolerance for
  402. * row (linear form) value. Checking the condition (2) for all j and k,
  403. * j != k, requires time O(|J|^2). However, this time can be reduced to
  404. * O(|J|), if use minimal a[j] and a[k], in which case it is sufficient
  405. * to check the condition (2) only once.
  406. *
  407. * Once the original inequality (1) is replaced by equivalent packing
  408. * inequality, we need to perform back substitution x~[j] = 1 - x[j] for
  409. * all j in Jn (see above).
  410. *
  411. * RECOVERING SOLUTION
  412. *
  413. * None needed. */
  414. static int hidden_packing(NPP *npp, struct elem *ptr, double *_b)
  415. { /* process inequality constraint: sum a[j] x[j] <= b;
  416. 0 - specified row is NOT hidden packing inequality;
  417. 1 - specified row is packing inequality;
  418. 2 - specified row is hidden packing inequality. */
  419. struct elem *e, *ej, *ek;
  420. int neg;
  421. double b = *_b, eps;
  422. xassert(npp == npp);
  423. /* a[j] must be non-zero, x[j] must be binary, for all j in J */
  424. for (e = ptr; e != NULL; e = e->next)
  425. { xassert(e->aj != 0.0);
  426. xassert(e->xj->is_int);
  427. xassert(e->xj->lb == 0.0 && e->xj->ub == 1.0);
  428. }
  429. /* check if the specified inequality constraint already has the
  430. form of packing inequality */
  431. neg = 0; /* neg is |Jn| */
  432. for (e = ptr; e != NULL; e = e->next)
  433. { if (e->aj == +1.0)
  434. ;
  435. else if (e->aj == -1.0)
  436. neg++;
  437. else
  438. break;
  439. }
  440. if (e == NULL)
  441. { /* all coefficients a[j] are +1 or -1; check rhs b */
  442. if (b == (double)(1 - neg))
  443. { /* it is packing inequality; no processing is needed */
  444. return 1;
  445. }
  446. }
  447. /* substitute x[j] = 1 - x~[j] for all j in Jn to make all a[j]
  448. positive; the result is a~[j] = |a[j]| and new rhs b */
  449. for (e = ptr; e != NULL; e = e->next)
  450. if (e->aj < 0) b -= e->aj;
  451. /* now a[j] > 0 for all j in J (actually |a[j]| are used) */
  452. /* if a[j] > b, skip processing--this case must not appear */
  453. for (e = ptr; e != NULL; e = e->next)
  454. if (fabs(e->aj) > b) return 0;
  455. /* now 0 < a[j] <= b for all j in J */
  456. /* find two minimal coefficients a[j] and a[k], j != k */
  457. ej = NULL;
  458. for (e = ptr; e != NULL; e = e->next)
  459. if (ej == NULL || fabs(ej->aj) > fabs(e->aj)) ej = e;
  460. xassert(ej != NULL);
  461. ek = NULL;
  462. for (e = ptr; e != NULL; e = e->next)
  463. if (e != ej)
  464. if (ek == NULL || fabs(ek->aj) > fabs(e->aj)) ek = e;
  465. xassert(ek != NULL);
  466. /* the specified constraint is equivalent to packing inequality
  467. iff a[j] + a[k] > b + eps */
  468. eps = 1e-3 + 1e-6 * fabs(b);
  469. if (fabs(ej->aj) + fabs(ek->aj) <= b + eps) return 0;
  470. /* perform back substitution x~[j] = 1 - x[j] and construct the
  471. final equivalent packing inequality in generalized format */
  472. b = 1.0;
  473. for (e = ptr; e != NULL; e = e->next)
  474. { if (e->aj > 0.0)
  475. e->aj = +1.0;
  476. else /* e->aj < 0.0 */
  477. e->aj = -1.0, b -= 1.0;
  478. }
  479. *_b = b;
  480. return 2;
  481. }
  482. int npp_hidden_packing(NPP *npp, NPPROW *row)
  483. { /* identify hidden packing inequality */
  484. NPPROW *copy;
  485. NPPAIJ *aij;
  486. struct elem *ptr, *e;
  487. int kase, ret, count = 0;
  488. double b;
  489. /* the row must be inequality constraint */
  490. xassert(row->lb < row->ub);
  491. for (kase = 0; kase <= 1; kase++)
  492. { if (kase == 0)
  493. { /* process row upper bound */
  494. if (row->ub == +DBL_MAX) continue;
  495. ptr = copy_form(npp, row, +1.0);
  496. b = + row->ub;
  497. }
  498. else
  499. { /* process row lower bound */
  500. if (row->lb == -DBL_MAX) continue;
  501. ptr = copy_form(npp, row, -1.0);
  502. b = - row->lb;
  503. }
  504. /* now the inequality has the form "sum a[j] x[j] <= b" */
  505. ret = hidden_packing(npp, ptr, &b);
  506. xassert(0 <= ret && ret <= 2);
  507. if (kase == 1 && ret == 1 || ret == 2)
  508. { /* the original inequality has been identified as hidden
  509. packing inequality */
  510. count++;
  511. #ifdef GLP_DEBUG
  512. xprintf("Original constraint:\n");
  513. for (aij = row->ptr; aij != NULL; aij = aij->r_next)
  514. xprintf(" %+g x%d", aij->val, aij->col->j);
  515. if (row->lb != -DBL_MAX) xprintf(", >= %g", row->lb);
  516. if (row->ub != +DBL_MAX) xprintf(", <= %g", row->ub);
  517. xprintf("\n");
  518. xprintf("Equivalent packing inequality:\n");
  519. for (e = ptr; e != NULL; e = e->next)
  520. xprintf(" %sx%d", e->aj > 0.0 ? "+" : "-", e->xj->j);
  521. xprintf(", <= %g\n", b);
  522. #endif
  523. if (row->lb == -DBL_MAX || row->ub == +DBL_MAX)
  524. { /* the original row is single-sided inequality; no copy
  525. is needed */
  526. copy = NULL;
  527. }
  528. else
  529. { /* the original row is double-sided inequality; we need
  530. to create its copy for other bound before replacing it
  531. with the equivalent inequality */
  532. copy = npp_add_row(npp);
  533. if (kase == 0)
  534. { /* the copy is for lower bound */
  535. copy->lb = row->lb, copy->ub = +DBL_MAX;
  536. }
  537. else
  538. { /* the copy is for upper bound */
  539. copy->lb = -DBL_MAX, copy->ub = row->ub;
  540. }
  541. /* copy original row coefficients */
  542. for (aij = row->ptr; aij != NULL; aij = aij->r_next)
  543. npp_add_aij(npp, copy, aij->col, aij->val);
  544. }
  545. /* replace the original inequality by equivalent one */
  546. npp_erase_row(npp, row);
  547. row->lb = -DBL_MAX, row->ub = b;
  548. for (e = ptr; e != NULL; e = e->next)
  549. npp_add_aij(npp, row, e->xj, e->aj);
  550. /* continue processing lower bound for the copy */
  551. if (copy != NULL) row = copy;
  552. }
  553. drop_form(npp, ptr);
  554. }
  555. return count;
  556. }
  557. /***********************************************************************
  558. * NAME
  559. *
  560. * npp_implied_packing - identify implied packing inequality
  561. *
  562. * SYNOPSIS
  563. *
  564. * #include "glpnpp.h"
  565. * int npp_implied_packing(NPP *npp, NPPROW *row, int which,
  566. * NPPCOL *var[], char set[]);
  567. *
  568. * DESCRIPTION
  569. *
  570. * The routine npp_implied_packing processes specified row (constraint)
  571. * of general format:
  572. *
  573. * L <= sum a[j] x[j] <= U. (1)
  574. * j
  575. *
  576. * If which = 0, only lower bound L, which must exist, is considered,
  577. * while upper bound U is ignored. Similarly, if which = 1, only upper
  578. * bound U, which must exist, is considered, while lower bound L is
  579. * ignored. Thus, if the specified row is a double-sided inequality or
  580. * equality constraint, this routine should be called twice for both
  581. * lower and upper bounds.
  582. *
  583. * The routine npp_implied_packing attempts to find a non-trivial (i.e.
  584. * having not less than two binary variables) packing inequality:
  585. *
  586. * sum x[j] - sum x[j] <= 1 - |Jn|, (2)
  587. * j in Jp j in Jn
  588. *
  589. * which is relaxation of the constraint (1) in the sense that any
  590. * solution satisfying to that constraint also satisfies to the packing
  591. * inequality (2). If such relaxation exists, the routine stores
  592. * pointers to descriptors of corresponding binary variables and their
  593. * flags, resp., to locations var[1], var[2], ..., var[len] and set[1],
  594. * set[2], ..., set[len], where set[j] = 0 means that j in Jp and
  595. * set[j] = 1 means that j in Jn.
  596. *
  597. * RETURNS
  598. *
  599. * The routine npp_implied_packing returns len, which is the total
  600. * number of binary variables in the packing inequality found, len >= 2.
  601. * However, if the relaxation does not exist, the routine returns zero.
  602. *
  603. * ALGORITHM
  604. *
  605. * If which = 0, the constraint coefficients (1) are multiplied by -1
  606. * and b is assigned -L; if which = 1, the constraint coefficients (1)
  607. * are not changed and b is assigned +U. In both cases the specified
  608. * constraint gets the following format:
  609. *
  610. * sum a[j] x[j] <= b. (3)
  611. * j
  612. *
  613. * (Note that (3) is a relaxation of (1), because one of bounds L or U
  614. * is ignored.)
  615. *
  616. * Let J be set of binary variables, Kp be set of non-binary (integer
  617. * or continuous) variables with a[j] > 0, and Kn be set of non-binary
  618. * variables with a[j] < 0. Then the inequality (3) can be written as
  619. * follows:
  620. *
  621. * sum a[j] x[j] <= b - sum a[j] x[j] - sum a[j] x[j]. (4)
  622. * j in J j in Kp j in Kn
  623. *
  624. * To get rid of non-binary variables we can replace the inequality (4)
  625. * by the following relaxed inequality:
  626. *
  627. * sum a[j] x[j] <= b~, (5)
  628. * j in J
  629. *
  630. * where:
  631. *
  632. * b~ = sup(b - sum a[j] x[j] - sum a[j] x[j]) =
  633. * j in Kp j in Kn
  634. *
  635. * = b - inf sum a[j] x[j] - inf sum a[j] x[j] = (6)
  636. * j in Kp j in Kn
  637. *
  638. * = b - sum a[j] l[j] - sum a[j] u[j].
  639. * j in Kp j in Kn
  640. *
  641. * Note that if lower bound l[j] (if j in Kp) or upper bound u[j]
  642. * (if j in Kn) of some non-binary variable x[j] does not exist, then
  643. * formally b = +oo, in which case further analysis is not performed.
  644. *
  645. * Let Bp = {j in J: a[j] > 0}, Bn = {j in J: a[j] < 0}. To make all
  646. * the inequality coefficients in (5) positive, we replace all x[j] in
  647. * Bn by their complementaries, substituting x[j] = 1 - x~[j] for all
  648. * j in Bn, that gives:
  649. *
  650. * sum a[j] x[j] - sum a[j] x~[j] <= b~ - sum a[j]. (7)
  651. * j in Bp j in Bn j in Bn
  652. *
  653. * This inequality is a relaxation of the original constraint (1), and
  654. * it is a binary knapsack inequality. Writing it in the standard format
  655. * we have:
  656. *
  657. * sum alfa[j] z[j] <= beta, (8)
  658. * j in J
  659. *
  660. * where:
  661. * ( + a[j], if j in Bp,
  662. * alfa[j] = < (9)
  663. * ( - a[j], if j in Bn,
  664. *
  665. * ( x[j], if j in Bp,
  666. * z[j] = < (10)
  667. * ( 1 - x[j], if j in Bn,
  668. *
  669. * beta = b~ - sum a[j]. (11)
  670. * j in Bn
  671. *
  672. * In the inequality (8) all coefficients are positive, therefore, the
  673. * packing relaxation to be found for this inequality is the following:
  674. *
  675. * sum z[j] <= 1. (12)
  676. * j in P
  677. *
  678. * It is obvious that set P within J, which we would like to find, must
  679. * satisfy to the following condition:
  680. *
  681. * alfa[j] + alfa[k] > beta + eps for all j, k in P, j != k, (13)
  682. *
  683. * where eps is an absolute tolerance for value of the linear form.
  684. * Thus, it is natural to take P = {j: alpha[j] > (beta + eps) / 2}.
  685. * Moreover, if in the equality (8) there exist coefficients alfa[k],
  686. * for which alfa[k] <= (beta + eps) / 2, but which, nevertheless,
  687. * satisfies to the condition (13) for all j in P, *one* corresponding
  688. * variable z[k] (having, for example, maximal coefficient alfa[k]) can
  689. * be included in set P, that allows increasing the number of binary
  690. * variables in (12) by one.
  691. *
  692. * Once the set P has been built, for the inequality (12) we need to
  693. * perform back substitution according to (10) in order to express it
  694. * through the original binary variables. As the result of such back
  695. * substitution the relaxed packing inequality get its final format (2),
  696. * where Jp = J intersect Bp, and Jn = J intersect Bn. */
  697. int npp_implied_packing(NPP *npp, NPPROW *row, int which,
  698. NPPCOL *var[], char set[])
  699. { struct elem *ptr, *e, *i, *k;
  700. int len = 0;
  701. double b, eps;
  702. /* build inequality (3) */
  703. if (which == 0)
  704. { ptr = copy_form(npp, row, -1.0);
  705. xassert(row->lb != -DBL_MAX);
  706. b = - row->lb;
  707. }
  708. else if (which == 1)
  709. { ptr = copy_form(npp, row, +1.0);
  710. xassert(row->ub != +DBL_MAX);
  711. b = + row->ub;
  712. }
  713. /* remove non-binary variables to build relaxed inequality (5);
  714. compute its right-hand side b~ with formula (6) */
  715. for (e = ptr; e != NULL; e = e->next)
  716. { if (!(e->xj->is_int && e->xj->lb == 0.0 && e->xj->ub == 1.0))
  717. { /* x[j] is non-binary variable */
  718. if (e->aj > 0.0)
  719. { if (e->xj->lb == -DBL_MAX) goto done;
  720. b -= e->aj * e->xj->lb;
  721. }
  722. else /* e->aj < 0.0 */
  723. { if (e->xj->ub == +DBL_MAX) goto done;
  724. b -= e->aj * e->xj->ub;
  725. }
  726. /* a[j] = 0 means that variable x[j] is removed */
  727. e->aj = 0.0;
  728. }
  729. }
  730. /* substitute x[j] = 1 - x~[j] to build knapsack inequality (8);
  731. compute its right-hand side beta with formula (11) */
  732. for (e = ptr; e != NULL; e = e->next)
  733. if (e->aj < 0.0) b -= e->aj;
  734. /* if beta is close to zero, the knapsack inequality is either
  735. infeasible or forcing inequality; this must never happen, so
  736. we skip further analysis */
  737. if (b < 1e-3) goto done;
  738. /* build set P as well as sets Jp and Jn, and determine x[k] as
  739. explained above in comments to the routine */
  740. eps = 1e-3 + 1e-6 * b;
  741. i = k = NULL;
  742. for (e = ptr; e != NULL; e = e->next)
  743. { /* note that alfa[j] = |a[j]| */
  744. if (fabs(e->aj) > 0.5 * (b + eps))
  745. { /* alfa[j] > (b + eps) / 2; include x[j] in set P, i.e. in
  746. set Jp or Jn */
  747. var[++len] = e->xj;
  748. set[len] = (char)(e->aj > 0.0 ? 0 : 1);
  749. /* alfa[i] = min alfa[j] over all j included in set P */
  750. if (i == NULL || fabs(i->aj) > fabs(e->aj)) i = e;
  751. }
  752. else if (fabs(e->aj) >= 1e-3)
  753. { /* alfa[k] = max alfa[j] over all j not included in set P;
  754. we skip coefficient a[j] if it is close to zero to avoid
  755. numerically unreliable results */
  756. if (k == NULL || fabs(k->aj) < fabs(e->aj)) k = e;
  757. }
  758. }
  759. /* if alfa[k] satisfies to condition (13) for all j in P, include
  760. x[k] in P */
  761. if (i != NULL && k != NULL && fabs(i->aj) + fabs(k->aj) > b + eps)
  762. { var[++len] = k->xj;
  763. set[len] = (char)(k->aj > 0.0 ? 0 : 1);
  764. }
  765. /* trivial packing inequality being redundant must never appear,
  766. so we just ignore it */
  767. if (len < 2) len = 0;
  768. done: drop_form(npp, ptr);
  769. return len;
  770. }
  771. /***********************************************************************
  772. * NAME
  773. *
  774. * npp_is_covering - test if constraint is covering inequality
  775. *
  776. * SYNOPSIS
  777. *
  778. * #include "glpnpp.h"
  779. * int npp_is_covering(NPP *npp, NPPROW *row);
  780. *
  781. * RETURNS
  782. *
  783. * If the specified row (constraint) is covering inequality (see below),
  784. * the routine npp_is_covering returns non-zero. Otherwise, it returns
  785. * zero.
  786. *
  787. * COVERING INEQUALITIES
  788. *
  789. * In canonical format the covering inequality is the following:
  790. *
  791. * sum x[j] >= 1, (1)
  792. * j in J
  793. *
  794. * where all variables x[j] are binary. This inequality expresses the
  795. * condition that in any integer feasible solution variables in set J
  796. * cannot be all equal to zero at the same time, i.e. at least one
  797. * variable must take non-zero (unity) value. W.l.o.g. it is assumed
  798. * that |J| >= 2, because if J is empty, the inequality (1) is
  799. * infeasible, and if |J| = 1, the inequality (1) is a forcing row.
  800. *
  801. * In general case the covering inequality may include original
  802. * variables x[j] as well as their complements x~[j]:
  803. *
  804. * sum x[j] + sum x~[j] >= 1, (2)
  805. * j in Jp j in Jn
  806. *
  807. * where Jp and Jn are not intersected. Therefore, using substitution
  808. * x~[j] = 1 - x[j] gives the packing inequality in generalized format:
  809. *
  810. * sum x[j] - sum x[j] >= 1 - |Jn|. (3)
  811. * j in Jp j in Jn
  812. *
  813. * (May note that the inequality (3) cuts off infeasible solutions,
  814. * where x[j] = 0 for all j in Jp and x[j] = 1 for all j in Jn.)
  815. *
  816. * NOTE: If |J| = 2, the inequality (3) is equivalent to packing
  817. * inequality (see the routine npp_is_packing). */
  818. int npp_is_covering(NPP *npp, NPPROW *row)
  819. { /* test if constraint is covering inequality */
  820. NPPCOL *col;
  821. NPPAIJ *aij;
  822. int b;
  823. xassert(npp == npp);
  824. if (!(row->lb != -DBL_MAX && row->ub == +DBL_MAX))
  825. return 0;
  826. b = 1;
  827. for (aij = row->ptr; aij != NULL; aij = aij->r_next)
  828. { col = aij->col;
  829. if (!(col->is_int && col->lb == 0.0 && col->ub == 1.0))
  830. return 0;
  831. if (aij->val == +1.0)
  832. ;
  833. else if (aij->val == -1.0)
  834. b--;
  835. else
  836. return 0;
  837. }
  838. if (row->lb != (double)b) return 0;
  839. return 1;
  840. }
  841. /***********************************************************************
  842. * NAME
  843. *
  844. * npp_hidden_covering - identify hidden covering inequality
  845. *
  846. * SYNOPSIS
  847. *
  848. * #include "glpnpp.h"
  849. * int npp_hidden_covering(NPP *npp, NPPROW *row);
  850. *
  851. * DESCRIPTION
  852. *
  853. * The routine npp_hidden_covering processes specified inequality
  854. * constraint, which includes only binary variables, and the number of
  855. * the variables is not less than three. If the original inequality is
  856. * equivalent to a covering inequality (see below), the routine
  857. * replaces it by the equivalent inequality. If the original constraint
  858. * is double-sided inequality, it is replaced by a pair of single-sided
  859. * inequalities, if necessary.
  860. *
  861. * RETURNS
  862. *
  863. * If the original inequality constraint was replaced by equivalent
  864. * covering inequality, the routine npp_hidden_covering returns
  865. * non-zero. Otherwise, it returns zero.
  866. *
  867. * PROBLEM TRANSFORMATION
  868. *
  869. * Consider an inequality constraint:
  870. *
  871. * sum a[j] x[j] >= b, (1)
  872. * j in J
  873. *
  874. * where all variables x[j] are binary, and |J| >= 3. (In case of '<='
  875. * inequality it can be transformed to '>=' format by multiplying both
  876. * its sides by -1.)
  877. *
  878. * Let Jp = {j: a[j] > 0}, Jn = {j: a[j] < 0}. Performing substitution
  879. * x[j] = 1 - x~[j] for all j in Jn, we have:
  880. *
  881. * sum a[j] x[j] >= b ==>
  882. * j in J
  883. *
  884. * sum a[j] x[j] + sum a[j] x[j] >= b ==>
  885. * j in Jp j in Jn
  886. *
  887. * sum a[j] x[j] + sum a[j] (1 - x~[j]) >= b ==>
  888. * j in Jp j in Jn
  889. *
  890. * sum m a[j] x[j] - sum a[j] x~[j] >= b - sum a[j].
  891. * j in Jp j in Jn j in Jn
  892. *
  893. * Thus, meaning the transformation above, we can assume that in
  894. * inequality (1) all coefficients a[j] are positive. Moreover, we can
  895. * assume that b > 0, because otherwise the inequality (1) would be
  896. * redundant (see the routine npp_analyze_row). It is then obvious that
  897. * constraint (1) is equivalent to covering inequality only if:
  898. *
  899. * a[j] >= b, (2)
  900. *
  901. * for all j in J.
  902. *
  903. * Once the original inequality (1) is replaced by equivalent covering
  904. * inequality, we need to perform back substitution x~[j] = 1 - x[j] for
  905. * all j in Jn (see above).
  906. *
  907. * RECOVERING SOLUTION
  908. *
  909. * None needed. */
  910. static int hidden_covering(NPP *npp, struct elem *ptr, double *_b)
  911. { /* process inequality constraint: sum a[j] x[j] >= b;
  912. 0 - specified row is NOT hidden covering inequality;
  913. 1 - specified row is covering inequality;
  914. 2 - specified row is hidden covering inequality. */
  915. struct elem *e;
  916. int neg;
  917. double b = *_b, eps;
  918. xassert(npp == npp);
  919. /* a[j] must be non-zero, x[j] must be binary, for all j in J */
  920. for (e = ptr; e != NULL; e = e->next)
  921. { xassert(e->aj != 0.0);
  922. xassert(e->xj->is_int);
  923. xassert(e->xj->lb == 0.0 && e->xj->ub == 1.0);
  924. }
  925. /* check if the specified inequality constraint already has the
  926. form of covering inequality */
  927. neg = 0; /* neg is |Jn| */
  928. for (e = ptr; e != NULL; e = e->next)
  929. { if (e->aj == +1.0)
  930. ;
  931. else if (e->aj == -1.0)
  932. neg++;
  933. else
  934. break;
  935. }
  936. if (e == NULL)
  937. { /* all coefficients a[j] are +1 or -1; check rhs b */
  938. if (b == (double)(1 - neg))
  939. { /* it is covering inequality; no processing is needed */
  940. return 1;
  941. }
  942. }
  943. /* substitute x[j] = 1 - x~[j] for all j in Jn to make all a[j]
  944. positive; the result is a~[j] = |a[j]| and new rhs b */
  945. for (e = ptr; e != NULL; e = e->next)
  946. if (e->aj < 0) b -= e->aj;
  947. /* now a[j] > 0 for all j in J (actually |a[j]| are used) */
  948. /* if b <= 0, skip processing--this case must not appear */
  949. if (b < 1e-3) return 0;
  950. /* now a[j] > 0 for all j in J, and b > 0 */
  951. /* the specified constraint is equivalent to covering inequality
  952. iff a[j] >= b for all j in J */
  953. eps = 1e-9 + 1e-12 * fabs(b);
  954. for (e = ptr; e != NULL; e = e->next)
  955. if (fabs(e->aj) < b - eps) return 0;
  956. /* perform back substitution x~[j] = 1 - x[j] and construct the
  957. final equivalent covering inequality in generalized format */
  958. b = 1.0;
  959. for (e = ptr; e != NULL; e = e->next)
  960. { if (e->aj > 0.0)
  961. e->aj = +1.0;
  962. else /* e->aj < 0.0 */
  963. e->aj = -1.0, b -= 1.0;
  964. }
  965. *_b = b;
  966. return 2;
  967. }
  968. int npp_hidden_covering(NPP *npp, NPPROW *row)
  969. { /* identify hidden covering inequality */
  970. NPPROW *copy;
  971. NPPAIJ *aij;
  972. struct elem *ptr, *e;
  973. int kase, ret, count = 0;
  974. double b;
  975. /* the row must be inequality constraint */
  976. xassert(row->lb < row->ub);
  977. for (kase = 0; kase <= 1; kase++)
  978. { if (kase == 0)
  979. { /* process row lower bound */
  980. if (row->lb == -DBL_MAX) continue;
  981. ptr = copy_form(npp, row, +1.0);
  982. b = + row->lb;
  983. }
  984. else
  985. { /* process row upper bound */
  986. if (row->ub == +DBL_MAX) continue;
  987. ptr = copy_form(npp, row, -1.0);
  988. b = - row->ub;
  989. }
  990. /* now the inequality has the form "sum a[j] x[j] >= b" */
  991. ret = hidden_covering(npp, ptr, &b);
  992. xassert(0 <= ret && ret <= 2);
  993. if (kase == 1 && ret == 1 || ret == 2)
  994. { /* the original inequality has been identified as hidden
  995. covering inequality */
  996. count++;
  997. #ifdef GLP_DEBUG
  998. xprintf("Original constraint:\n");
  999. for (aij = row->ptr; aij != NULL; aij = aij->r_next)
  1000. xprintf(" %+g x%d", aij->val, aij->col->j);
  1001. if (row->lb != -DBL_MAX) xprintf(", >= %g", row->lb);
  1002. if (row->ub != +DBL_MAX) xprintf(", <= %g", row->ub);
  1003. xprintf("\n");
  1004. xprintf("Equivalent covering inequality:\n");
  1005. for (e = ptr; e != NULL; e = e->next)
  1006. xprintf(" %sx%d", e->aj > 0.0 ? "+" : "-", e->xj->j);
  1007. xprintf(", >= %g\n", b);
  1008. #endif
  1009. if (row->lb == -DBL_MAX || row->ub == +DBL_MAX)
  1010. { /* the original row is single-sided inequality; no copy
  1011. is needed */
  1012. copy = NULL;
  1013. }
  1014. else
  1015. { /* the original row is double-sided inequality; we need
  1016. to create its copy for other bound before replacing it
  1017. with the equivalent inequality */
  1018. copy = npp_add_row(npp);
  1019. if (kase == 0)
  1020. { /* the copy is for upper bound */
  1021. copy->lb = -DBL_MAX, copy->ub = row->ub;
  1022. }
  1023. else
  1024. { /* the copy is for lower bound */
  1025. copy->lb = row->lb, copy->ub = +DBL_MAX;
  1026. }
  1027. /* copy original row coefficients */
  1028. for (aij = row->ptr; aij != NULL; aij = aij->r_next)
  1029. npp_add_aij(npp, copy, aij->col, aij->val);
  1030. }
  1031. /* replace the original inequality by equivalent one */
  1032. npp_erase_row(npp, row);
  1033. row->lb = b, row->ub = +DBL_MAX;
  1034. for (e = ptr; e != NULL; e = e->next)
  1035. npp_add_aij(npp, row, e->xj, e->aj);
  1036. /* continue processing upper bound for the copy */
  1037. if (copy != NULL) row = copy;
  1038. }
  1039. drop_form(npp, ptr);
  1040. }
  1041. return count;
  1042. }
  1043. /***********************************************************************
  1044. * NAME
  1045. *
  1046. * npp_is_partitioning - test if constraint is partitioning equality
  1047. *
  1048. * SYNOPSIS
  1049. *
  1050. * #include "glpnpp.h"
  1051. * int npp_is_partitioning(NPP *npp, NPPROW *row);
  1052. *
  1053. * RETURNS
  1054. *
  1055. * If the specified row (constraint) is partitioning equality (see
  1056. * below), the routine npp_is_partitioning returns non-zero. Otherwise,
  1057. * it returns zero.
  1058. *
  1059. * PARTITIONING EQUALITIES
  1060. *
  1061. * In canonical format the partitioning equality is the following:
  1062. *
  1063. * sum x[j] = 1, (1)
  1064. * j in J
  1065. *
  1066. * where all variables x[j] are binary. This equality expresses the
  1067. * condition that in any integer feasible solution exactly one variable
  1068. * in set J must take non-zero (unity) value while other variables must
  1069. * be equal to zero. W.l.o.g. it is assumed that |J| >= 2, because if
  1070. * J is empty, the inequality (1) is infeasible, and if |J| = 1, the
  1071. * inequality (1) is a fixing row.
  1072. *
  1073. * In general case the partitioning equality may include original
  1074. * variables x[j] as well as their complements x~[j]:
  1075. *
  1076. * sum x[j] + sum x~[j] = 1, (2)
  1077. * j in Jp j in Jn
  1078. *
  1079. * where Jp and Jn are not intersected. Therefore, using substitution
  1080. * x~[j] = 1 - x[j] leads to the partitioning equality in generalized
  1081. * format:
  1082. *
  1083. * sum x[j] - sum x[j] = 1 - |Jn|. (3)
  1084. * j in Jp j in Jn */
  1085. int npp_is_partitioning(NPP *npp, NPPROW *row)
  1086. { /* test if constraint is partitioning equality */
  1087. NPPCOL *col;
  1088. NPPAIJ *aij;
  1089. int b;
  1090. xassert(npp == npp);
  1091. if (row->lb != row->ub) return 0;
  1092. b = 1;
  1093. for (aij = row->ptr; aij != NULL; aij = aij->r_next)
  1094. { col = aij->col;
  1095. if (!(col->is_int && col->lb == 0.0 && col->ub == 1.0))
  1096. return 0;
  1097. if (aij->val == +1.0)
  1098. ;
  1099. else if (aij->val == -1.0)
  1100. b--;
  1101. else
  1102. return 0;
  1103. }
  1104. if (row->lb != (double)b) return 0;
  1105. return 1;
  1106. }
  1107. /***********************************************************************
  1108. * NAME
  1109. *
  1110. * npp_reduce_ineq_coef - reduce inequality constraint coefficients
  1111. *
  1112. * SYNOPSIS
  1113. *
  1114. * #include "glpnpp.h"
  1115. * int npp_reduce_ineq_coef(NPP *npp, NPPROW *row);
  1116. *
  1117. * DESCRIPTION
  1118. *
  1119. * The routine npp_reduce_ineq_coef processes specified inequality
  1120. * constraint attempting to replace it by an equivalent constraint,
  1121. * where magnitude of coefficients at binary variables is smaller than
  1122. * in the original constraint. If the inequality is double-sided, it is
  1123. * replaced by a pair of single-sided inequalities, if necessary.
  1124. *
  1125. * RETURNS
  1126. *
  1127. * The routine npp_reduce_ineq_coef returns the number of coefficients
  1128. * reduced.
  1129. *
  1130. * BACKGROUND
  1131. *
  1132. * Consider an inequality constraint:
  1133. *
  1134. * sum a[j] x[j] >= b. (1)
  1135. * j in J
  1136. *
  1137. * (In case of '<=' inequality it can be transformed to '>=' format by
  1138. * multiplying both its sides by -1.) Let x[k] be a binary variable;
  1139. * other variables can be integer as well as continuous. We can write
  1140. * constraint (1) as follows:
  1141. *
  1142. * a[k] x[k] + t[k] >= b, (2)
  1143. *
  1144. * where:
  1145. *
  1146. * t[k] = sum a[j] x[j]. (3)
  1147. * j in J\{k}
  1148. *
  1149. * Since x[k] is binary, constraint (2) is equivalent to disjunction of
  1150. * the following two constraints:
  1151. *
  1152. * x[k] = 0, t[k] >= b (4)
  1153. *
  1154. * OR
  1155. *
  1156. * x[k] = 1, t[k] >= b - a[k]. (5)
  1157. *
  1158. * Let also that for the partial sum t[k] be known some its implied
  1159. * lower bound inf t[k].
  1160. *
  1161. * Case a[k] > 0. Let inf t[k] < b, since otherwise both constraints
  1162. * (4) and (5) and therefore constraint (2) are redundant.
  1163. * If inf t[k] > b - a[k], only constraint (5) is redundant, in which
  1164. * case it can be replaced with the following redundant and therefore
  1165. * equivalent constraint:
  1166. *
  1167. * t[k] >= b - a'[k] = inf t[k], (6)
  1168. *
  1169. * where:
  1170. *
  1171. * a'[k] = b - inf t[k]. (7)
  1172. *
  1173. * Thus, the original constraint (2) is equivalent to the following
  1174. * constraint with coefficient at variable x[k] changed:
  1175. *
  1176. * a'[k] x[k] + t[k] >= b. (8)
  1177. *
  1178. * From inf t[k] < b it follows that a'[k] > 0, i.e. the coefficient
  1179. * at x[k] keeps its sign. And from inf t[k] > b - a[k] it follows that
  1180. * a'[k] < a[k], i.e. the coefficient reduces in magnitude.
  1181. *
  1182. * Case a[k] < 0. Let inf t[k] < b - a[k], since otherwise both
  1183. * constraints (4) and (5) and therefore constraint (2) are redundant.
  1184. * If inf t[k] > b, only constraint (4) is redundant, in which case it
  1185. * can be replaced with the following redundant and therefore equivalent
  1186. * constraint:
  1187. *
  1188. * t[k] >= b' = inf t[k]. (9)
  1189. *
  1190. * Rewriting constraint (5) as follows:
  1191. *
  1192. * t[k] >= b - a[k] = b' - a'[k], (10)
  1193. *
  1194. * where:
  1195. *
  1196. * a'[k] = a[k] + b' - b = a[k] + inf t[k] - b, (11)
  1197. *
  1198. * we can see that disjunction of constraint (9) and (10) is equivalent
  1199. * to disjunction of constraint (4) and (5), from which it follows that
  1200. * the original constraint (2) is equivalent to the following constraint
  1201. * with both coefficient at variable x[k] and right-hand side changed:
  1202. *
  1203. * a'[k] x[k] + t[k] >= b'. (12)
  1204. *
  1205. * From inf t[k] < b - a[k] it follows that a'[k] < 0, i.e. the
  1206. * coefficient at x[k] keeps its sign. And from inf t[k] > b it follows
  1207. * that a'[k] > a[k], i.e. the coefficient reduces in magnitude.
  1208. *
  1209. * PROBLEM TRANSFORMATION
  1210. *
  1211. * In the routine npp_reduce_ineq_coef the following implied lower
  1212. * bound of the partial sum (3) is used:
  1213. *
  1214. * inf t[k] = sum a[j] l[j] + sum a[j] u[j], (13)
  1215. * j in Jp\{k} k in Jn\{k}
  1216. *
  1217. * where Jp = {j : a[j] > 0}, Jn = {j : a[j] < 0}, l[j] and u[j] are
  1218. * lower and upper bounds, resp., of variable x[j].
  1219. *
  1220. * In order to compute inf t[k] more efficiently, the following formula,
  1221. * which is equivalent to (13), is actually used:
  1222. *
  1223. * ( h - a[k] l[k] = h, if a[k] > 0,
  1224. * inf t[k] = < (14)
  1225. * ( h - a[k] u[k] = h - a[k], if a[k] < 0,
  1226. *
  1227. * where:
  1228. *
  1229. * h = sum a[j] l[j] + sum a[j] u[j] (15)
  1230. * j in Jp j in Jn
  1231. *
  1232. * is the implied lower bound of row (1).
  1233. *
  1234. * Reduction of positive coefficient (a[k] > 0) does not change value
  1235. * of h, since l[k] = 0. In case of reduction of negative coefficient
  1236. * (a[k] < 0) from (11) it follows that:
  1237. *
  1238. * delta a[k] = a'[k] - a[k] = inf t[k] - b (> 0), (16)
  1239. *
  1240. * so new value of h (accounting that u[k] = 1) can be computed as
  1241. * follows:
  1242. *
  1243. * h := h + delta a[k] = h + (inf t[k] - b). (17)
  1244. *
  1245. * RECOVERING SOLUTION
  1246. *
  1247. * None needed. */
  1248. static int reduce_ineq_coef(NPP *npp, struct elem *ptr, double *_b)
  1249. { /* process inequality constraint: sum a[j] x[j] >= b */
  1250. /* returns: the number of coefficients reduced */
  1251. struct elem *e;
  1252. int count = 0;
  1253. double h, inf_t, new_a, b = *_b;
  1254. xassert(npp == npp);
  1255. /* compute h; see (15) */
  1256. h = 0.0;
  1257. for (e = ptr; e != NULL; e = e->next)
  1258. { if (e->aj > 0.0)
  1259. { if (e->xj->lb == -DBL_MAX) goto done;
  1260. h += e->aj * e->xj->lb;
  1261. }
  1262. else /* e->aj < 0.0 */
  1263. { if (e->xj->ub == +DBL_MAX) goto done;
  1264. h += e->aj * e->xj->ub;
  1265. }
  1266. }
  1267. /* perform reduction of coefficients at binary variables */
  1268. for (e = ptr; e != NULL; e = e->next)
  1269. { /* skip non-binary variable */
  1270. if (!(e->xj->is_int && e->xj->lb == 0.0 && e->xj->ub == 1.0))
  1271. continue;
  1272. if (e->aj > 0.0)
  1273. { /* compute inf t[k]; see (14) */
  1274. inf_t = h;
  1275. if (b - e->aj < inf_t && inf_t < b)
  1276. { /* compute reduced coefficient a'[k]; see (7) */
  1277. new_a = b - inf_t;
  1278. if (new_a >= +1e-3 &&
  1279. e->aj - new_a >= 0.01 * (1.0 + e->aj))
  1280. { /* accept a'[k] */
  1281. #ifdef GLP_DEBUG
  1282. xprintf("+");
  1283. #endif
  1284. e->aj = new_a;
  1285. count++;
  1286. }
  1287. }
  1288. }
  1289. else /* e->aj < 0.0 */
  1290. { /* compute inf t[k]; see (14) */
  1291. inf_t = h - e->aj;
  1292. if (b < inf_t && inf_t < b - e->aj)
  1293. { /* compute reduced coefficient a'[k]; see (11) */
  1294. new_a = e->aj + (inf_t - b);
  1295. if (new_a <= -1e-3 &&
  1296. new_a - e->aj >= 0.01 * (1.0 - e->aj))
  1297. { /* accept a'[k] */
  1298. #ifdef GLP_DEBUG
  1299. xprintf("-");
  1300. #endif
  1301. e->aj = new_a;
  1302. /* update h; see (17) */
  1303. h += (inf_t - b);
  1304. /* compute b'; see (9) */
  1305. b = inf_t;
  1306. count++;
  1307. }
  1308. }
  1309. }
  1310. }
  1311. *_b = b;
  1312. done: return count;
  1313. }
  1314. int npp_reduce_ineq_coef(NPP *npp, NPPROW *row)
  1315. { /* reduce inequality constraint coefficients */
  1316. NPPROW *copy;
  1317. NPPAIJ *aij;
  1318. struct elem *ptr, *e;
  1319. int kase, count[2];
  1320. double b;
  1321. /* the row must be inequality constraint */
  1322. xassert(row->lb < row->ub);
  1323. count[0] = count[1] = 0;
  1324. for (kase = 0; kase <= 1; kase++)
  1325. { if (kase == 0)
  1326. { /* process row lower bound */
  1327. if (row->lb == -DBL_MAX) continue;
  1328. #ifdef GLP_DEBUG
  1329. xprintf("L");
  1330. #endif
  1331. ptr = copy_form(npp, row, +1.0);
  1332. b = + row->lb;
  1333. }
  1334. else
  1335. { /* process row upper bound */
  1336. if (row->ub == +DBL_MAX) continue;
  1337. #ifdef GLP_DEBUG
  1338. xprintf("U");
  1339. #endif
  1340. ptr = copy_form(npp, row, -1.0);
  1341. b = - row->ub;
  1342. }
  1343. /* now the inequality has the form "sum a[j] x[j] >= b" */
  1344. count[kase] = reduce_ineq_coef(npp, ptr, &b);
  1345. if (count[kase] > 0)
  1346. { /* the original inequality has been replaced by equivalent
  1347. one with coefficients reduced */
  1348. if (row->lb == -DBL_MAX || row->ub == +DBL_MAX)
  1349. { /* the original row is single-sided inequality; no copy
  1350. is needed */
  1351. copy = NULL;
  1352. }
  1353. else
  1354. { /* the original row is double-sided inequality; we need
  1355. to create its copy for other bound before replacing it
  1356. with the equivalent inequality */
  1357. #ifdef GLP_DEBUG
  1358. xprintf("*");
  1359. #endif
  1360. copy = npp_add_row(npp);
  1361. if (kase == 0)
  1362. { /* the copy is for upper bound */
  1363. copy->lb = -DBL_MAX, copy->ub = row->ub;
  1364. }
  1365. else
  1366. { /* the copy is for lower bound */
  1367. copy->lb = row->lb, copy->ub = +DBL_MAX;
  1368. }
  1369. /* copy original row coefficients */
  1370. for (aij = row->ptr; aij != NULL; aij = aij->r_next)
  1371. npp_add_aij(npp, copy, aij->col, aij->val);
  1372. }
  1373. /* replace the original inequality by equivalent one */
  1374. npp_erase_row(npp, row);
  1375. row->lb = b, row->ub = +DBL_MAX;
  1376. for (e = ptr; e != NULL; e = e->next)
  1377. npp_add_aij(npp, row, e->xj, e->aj);
  1378. /* continue processing upper bound for the copy */
  1379. if (copy != NULL) row = copy;
  1380. }
  1381. drop_form(npp, ptr);
  1382. }
  1383. return count[0] + count[1];
  1384. }
  1385. /* eof */