glpnet06.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. /* glpnet06.c (out-of-kilter algorithm) */
  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 "glpenv.h"
  24. #include "glpnet.h"
  25. /***********************************************************************
  26. * NAME
  27. *
  28. * okalg - out-of-kilter algorithm
  29. *
  30. * SYNOPSIS
  31. *
  32. * #include "glpnet.h"
  33. * int okalg(int nv, int na, const int tail[], const int head[],
  34. * const int low[], const int cap[], const int cost[], int x[],
  35. * int pi[]);
  36. *
  37. * DESCRIPTION
  38. *
  39. * The routine okalg implements the out-of-kilter algorithm to find a
  40. * minimal-cost circulation in the specified flow network.
  41. *
  42. * INPUT PARAMETERS
  43. *
  44. * nv is the number of nodes, nv >= 0.
  45. *
  46. * na is the number of arcs, na >= 0.
  47. *
  48. * tail[a], a = 1,...,na, is the index of tail node of arc a.
  49. *
  50. * head[a], a = 1,...,na, is the index of head node of arc a.
  51. *
  52. * low[a], a = 1,...,na, is an lower bound to the flow through arc a.
  53. *
  54. * cap[a], a = 1,...,na, is an upper bound to the flow through arc a,
  55. * which is the capacity of the arc.
  56. *
  57. * cost[a], a = 1,...,na, is a per-unit cost of the flow through arc a.
  58. *
  59. * NOTES
  60. *
  61. * 1. Multiple arcs are allowed, but self-loops are not allowed.
  62. *
  63. * 2. It is required that 0 <= low[a] <= cap[a] for all arcs.
  64. *
  65. * 3. Arc costs may have any sign.
  66. *
  67. * OUTPUT PARAMETERS
  68. *
  69. * x[a], a = 1,...,na, is optimal value of the flow through arc a.
  70. *
  71. * pi[i], i = 1,...,nv, is Lagrange multiplier for flow conservation
  72. * equality constraint corresponding to node i (the node potential).
  73. *
  74. * RETURNS
  75. *
  76. * 0 optimal circulation found;
  77. *
  78. * 1 there is no feasible circulation;
  79. *
  80. * 2 integer overflow occured;
  81. *
  82. * 3 optimality test failed (logic error).
  83. *
  84. * REFERENCES
  85. *
  86. * L.R.Ford, Jr., and D.R.Fulkerson, "Flows in Networks," The RAND
  87. * Corp., Report R-375-PR (August 1962), Chap. III "Minimal Cost Flow
  88. * Problems," pp.113-26. */
  89. static int overflow(int u, int v)
  90. { /* check for integer overflow on computing u + v */
  91. if (u > 0 && v > 0 && u + v < 0) return 1;
  92. if (u < 0 && v < 0 && u + v > 0) return 1;
  93. return 0;
  94. }
  95. int okalg(int nv, int na, const int tail[], const int head[],
  96. const int low[], const int cap[], const int cost[], int x[],
  97. int pi[])
  98. { int a, aok, delta, i, j, k, lambda, pos1, pos2, s, t, temp, ret,
  99. *ptr, *arc, *link, *list;
  100. /* sanity checks */
  101. xassert(nv >= 0);
  102. xassert(na >= 0);
  103. for (a = 1; a <= na; a++)
  104. { i = tail[a], j = head[a];
  105. xassert(1 <= i && i <= nv);
  106. xassert(1 <= j && j <= nv);
  107. xassert(i != j);
  108. xassert(0 <= low[a] && low[a] <= cap[a]);
  109. }
  110. /* allocate working arrays */
  111. ptr = xcalloc(1+nv+1, sizeof(int));
  112. arc = xcalloc(1+na+na, sizeof(int));
  113. link = xcalloc(1+nv, sizeof(int));
  114. list = xcalloc(1+nv, sizeof(int));
  115. /* ptr[i] := (degree of node i) */
  116. for (i = 1; i <= nv; i++)
  117. ptr[i] = 0;
  118. for (a = 1; a <= na; a++)
  119. { ptr[tail[a]]++;
  120. ptr[head[a]]++;
  121. }
  122. /* initialize arc pointers */
  123. ptr[1]++;
  124. for (i = 1; i < nv; i++)
  125. ptr[i+1] += ptr[i];
  126. ptr[nv+1] = ptr[nv];
  127. /* build arc lists */
  128. for (a = 1; a <= na; a++)
  129. { arc[--ptr[tail[a]]] = a;
  130. arc[--ptr[head[a]]] = a;
  131. }
  132. xassert(ptr[1] == 1);
  133. xassert(ptr[nv+1] == na+na+1);
  134. /* now the indices of arcs incident to node i are stored in
  135. locations arc[ptr[i]], arc[ptr[i]+1], ..., arc[ptr[i+1]-1] */
  136. /* initialize arc flows and node potentials */
  137. for (a = 1; a <= na; a++)
  138. x[a] = 0;
  139. for (i = 1; i <= nv; i++)
  140. pi[i] = 0;
  141. loop: /* main loop starts here */
  142. /* find out-of-kilter arc */
  143. aok = 0;
  144. for (a = 1; a <= na; a++)
  145. { i = tail[a], j = head[a];
  146. if (overflow(cost[a], pi[i] - pi[j]))
  147. { ret = 2;
  148. goto done;
  149. }
  150. lambda = cost[a] + (pi[i] - pi[j]);
  151. if (x[a] < low[a] || lambda < 0 && x[a] < cap[a])
  152. { /* arc a = i->j is out of kilter, and we need to increase
  153. the flow through this arc */
  154. aok = a, s = j, t = i;
  155. break;
  156. }
  157. if (x[a] > cap[a] || lambda > 0 && x[a] > low[a])
  158. { /* arc a = i->j is out of kilter, and we need to decrease
  159. the flow through this arc */
  160. aok = a, s = i, t = j;
  161. break;
  162. }
  163. }
  164. if (aok == 0)
  165. { /* all arcs are in kilter */
  166. /* check for feasibility */
  167. for (a = 1; a <= na; a++)
  168. { if (!(low[a] <= x[a] && x[a] <= cap[a]))
  169. { ret = 3;
  170. goto done;
  171. }
  172. }
  173. for (i = 1; i <= nv; i++)
  174. { temp = 0;
  175. for (k = ptr[i]; k < ptr[i+1]; k++)
  176. { a = arc[k];
  177. if (tail[a] == i)
  178. { /* a is outgoing arc */
  179. temp += x[a];
  180. }
  181. else if (head[a] == i)
  182. { /* a is incoming arc */
  183. temp -= x[a];
  184. }
  185. else
  186. xassert(a != a);
  187. }
  188. if (temp != 0)
  189. { ret = 3;
  190. goto done;
  191. }
  192. }
  193. /* check for optimality */
  194. for (a = 1; a <= na; a++)
  195. { i = tail[a], j = head[a];
  196. lambda = cost[a] + (pi[i] - pi[j]);
  197. if (lambda > 0 && x[a] != low[a] ||
  198. lambda < 0 && x[a] != cap[a])
  199. { ret = 3;
  200. goto done;
  201. }
  202. }
  203. /* current circulation is optimal */
  204. ret = 0;
  205. goto done;
  206. }
  207. /* now we need to find a cycle (t, a, s, ..., t), which allows
  208. increasing the flow along it, where a is the out-of-kilter arc
  209. just found */
  210. /* link[i] = 0 means that node i is not labelled yet;
  211. link[i] = a means that arc a immediately precedes node i */
  212. /* initially only node s is labelled */
  213. for (i = 1; i <= nv; i++)
  214. link[i] = 0;
  215. link[s] = aok, list[1] = s, pos1 = pos2 = 1;
  216. /* breadth first search */
  217. while (pos1 <= pos2)
  218. { /* dequeue node i */
  219. i = list[pos1++];
  220. /* consider all arcs incident to node i */
  221. for (k = ptr[i]; k < ptr[i+1]; k++)
  222. { a = arc[k];
  223. if (tail[a] == i)
  224. { /* a = i->j is a forward arc from s to t */
  225. j = head[a];
  226. /* if node j has been labelled, skip the arc */
  227. if (link[j] != 0) continue;
  228. /* if the arc does not allow increasing the flow through
  229. it, skip the arc */
  230. if (x[a] >= cap[a]) continue;
  231. if (overflow(cost[a], pi[i] - pi[j]))
  232. { ret = 2;
  233. goto done;
  234. }
  235. lambda = cost[a] + (pi[i] - pi[j]);
  236. if (lambda > 0 && x[a] >= low[a]) continue;
  237. }
  238. else if (head[a] == i)
  239. { /* a = i<-j is a backward arc from s to t */
  240. j = tail[a];
  241. /* if node j has been labelled, skip the arc */
  242. if (link[j] != 0) continue;
  243. /* if the arc does not allow decreasing the flow through
  244. it, skip the arc */
  245. if (x[a] <= low[a]) continue;
  246. if (overflow(cost[a], pi[j] - pi[i]))
  247. { ret = 2;
  248. goto done;
  249. }
  250. lambda = cost[a] + (pi[j] - pi[i]);
  251. if (lambda < 0 && x[a] <= cap[a]) continue;
  252. }
  253. else
  254. xassert(a != a);
  255. /* label node j and enqueue it */
  256. link[j] = a, list[++pos2] = j;
  257. /* check for breakthrough */
  258. if (j == t) goto brkt;
  259. }
  260. }
  261. /* NONBREAKTHROUGH */
  262. /* consider all arcs, whose one endpoint is labelled and other is
  263. not, and determine maximal change of node potentials */
  264. delta = 0;
  265. for (a = 1; a <= na; a++)
  266. { i = tail[a], j = head[a];
  267. if (link[i] != 0 && link[j] == 0)
  268. { /* a = i->j, where node i is labelled, node j is not */
  269. if (overflow(cost[a], pi[i] - pi[j]))
  270. { ret = 2;
  271. goto done;
  272. }
  273. lambda = cost[a] + (pi[i] - pi[j]);
  274. if (x[a] <= cap[a] && lambda > 0)
  275. if (delta == 0 || delta > + lambda) delta = + lambda;
  276. }
  277. else if (link[i] == 0 && link[j] != 0)
  278. { /* a = j<-i, where node j is labelled, node i is not */
  279. if (overflow(cost[a], pi[i] - pi[j]))
  280. { ret = 2;
  281. goto done;
  282. }
  283. lambda = cost[a] + (pi[i] - pi[j]);
  284. if (x[a] >= low[a] && lambda < 0)
  285. if (delta == 0 || delta > - lambda) delta = - lambda;
  286. }
  287. }
  288. if (delta == 0)
  289. { /* there is no feasible circulation */
  290. ret = 1;
  291. goto done;
  292. }
  293. /* increase potentials of all unlabelled nodes */
  294. for (i = 1; i <= nv; i++)
  295. { if (link[i] == 0)
  296. { if (overflow(pi[i], delta))
  297. { ret = 2;
  298. goto done;
  299. }
  300. pi[i] += delta;
  301. }
  302. }
  303. goto loop;
  304. brkt: /* BREAKTHROUGH */
  305. /* walk through arcs of the cycle (t, a, s, ..., t) found in the
  306. reverse order and determine maximal change of the flow */
  307. delta = 0;
  308. for (j = t;; j = i)
  309. { /* arc a immediately precedes node j in the cycle */
  310. a = link[j];
  311. if (head[a] == j)
  312. { /* a = i->j is a forward arc of the cycle */
  313. i = tail[a];
  314. lambda = cost[a] + (pi[i] - pi[j]);
  315. if (lambda > 0 && x[a] < low[a])
  316. { /* x[a] may be increased until its lower bound */
  317. temp = low[a] - x[a];
  318. }
  319. else if (lambda <= 0 && x[a] < cap[a])
  320. { /* x[a] may be increased until its upper bound */
  321. temp = cap[a] - x[a];
  322. }
  323. else
  324. xassert(a != a);
  325. }
  326. else if (tail[a] == j)
  327. { /* a = i<-j is a backward arc of the cycle */
  328. i = head[a];
  329. lambda = cost[a] + (pi[j] - pi[i]);
  330. if (lambda < 0 && x[a] > cap[a])
  331. { /* x[a] may be decreased until its upper bound */
  332. temp = x[a] - cap[a];
  333. }
  334. else if (lambda >= 0 && x[a] > low[a])
  335. { /* x[a] may be decreased until its lower bound */
  336. temp = x[a] - low[a];
  337. }
  338. else
  339. xassert(a != a);
  340. }
  341. else
  342. xassert(a != a);
  343. if (delta == 0 || delta > temp) delta = temp;
  344. /* check for end of the cycle */
  345. if (i == t) break;
  346. }
  347. xassert(delta > 0);
  348. /* increase the flow along the cycle */
  349. for (j = t;; j = i)
  350. { /* arc a immediately precedes node j in the cycle */
  351. a = link[j];
  352. if (head[a] == j)
  353. { /* a = i->j is a forward arc of the cycle */
  354. i = tail[a];
  355. /* overflow cannot occur */
  356. x[a] += delta;
  357. }
  358. else if (tail[a] == j)
  359. { /* a = i<-j is a backward arc of the cycle */
  360. i = head[a];
  361. /* overflow cannot occur */
  362. x[a] -= delta;
  363. }
  364. else
  365. xassert(a != a);
  366. /* check for end of the cycle */
  367. if (i == t) break;
  368. }
  369. goto loop;
  370. done: /* free working arrays */
  371. xfree(ptr);
  372. xfree(arc);
  373. xfree(link);
  374. xfree(list);
  375. return ret;
  376. }
  377. /* eof */