/farmR/src/glpnet06.c

https://code.google.com/p/javawfm/ · C · 382 lines · 249 code · 6 blank · 127 comment · 132 complexity · e42fc3424bd95383c54bb2728c46f1d3 MD5 · raw file

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