glpspx01.c 97 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955
  1. /* glpspx01.c (primal simplex method) */
  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 "glpspx.h"
  24. struct csa
  25. { /* common storage area */
  26. /*--------------------------------------------------------------*/
  27. /* LP data */
  28. int m;
  29. /* number of rows (auxiliary variables), m > 0 */
  30. int n;
  31. /* number of columns (structural variables), n > 0 */
  32. char *type; /* char type[1+m+n]; */
  33. /* type[0] is not used;
  34. type[k], 1 <= k <= m+n, is the type of variable x[k]:
  35. GLP_FR - free variable
  36. GLP_LO - variable with lower bound
  37. GLP_UP - variable with upper bound
  38. GLP_DB - double-bounded variable
  39. GLP_FX - fixed variable */
  40. double *lb; /* double lb[1+m+n]; */
  41. /* lb[0] is not used;
  42. lb[k], 1 <= k <= m+n, is an lower bound of variable x[k];
  43. if x[k] has no lower bound, lb[k] is zero */
  44. double *ub; /* double ub[1+m+n]; */
  45. /* ub[0] is not used;
  46. ub[k], 1 <= k <= m+n, is an upper bound of variable x[k];
  47. if x[k] has no upper bound, ub[k] is zero;
  48. if x[k] is of fixed type, ub[k] is the same as lb[k] */
  49. double *coef; /* double coef[1+m+n]; */
  50. /* coef[0] is not used;
  51. coef[k], 1 <= k <= m+n, is an objective coefficient at
  52. variable x[k] (note that on phase I auxiliary variables also
  53. may have non-zero objective coefficients) */
  54. /*--------------------------------------------------------------*/
  55. /* original objective function */
  56. double *obj; /* double obj[1+n]; */
  57. /* obj[0] is a constant term of the original objective function;
  58. obj[j], 1 <= j <= n, is an original objective coefficient at
  59. structural variable x[m+j] */
  60. double zeta;
  61. /* factor used to scale original objective coefficients; its
  62. sign defines original optimization direction: zeta > 0 means
  63. minimization, zeta < 0 means maximization */
  64. /*--------------------------------------------------------------*/
  65. /* constraint matrix A; it has m rows and n columns and is stored
  66. by columns */
  67. int *A_ptr; /* int A_ptr[1+n+1]; */
  68. /* A_ptr[0] is not used;
  69. A_ptr[j], 1 <= j <= n, is starting position of j-th column in
  70. arrays A_ind and A_val; note that A_ptr[1] is always 1;
  71. A_ptr[n+1] indicates the position after the last element in
  72. arrays A_ind and A_val */
  73. int *A_ind; /* int A_ind[A_ptr[n+1]]; */
  74. /* row indices */
  75. double *A_val; /* double A_val[A_ptr[n+1]]; */
  76. /* non-zero element values */
  77. /*--------------------------------------------------------------*/
  78. /* basis header */
  79. int *head; /* int head[1+m+n]; */
  80. /* head[0] is not used;
  81. head[i], 1 <= i <= m, is the ordinal number of basic variable
  82. xB[i]; head[i] = k means that xB[i] = x[k] and i-th column of
  83. matrix B is k-th column of matrix (I|-A);
  84. head[m+j], 1 <= j <= n, is the ordinal number of non-basic
  85. variable xN[j]; head[m+j] = k means that xN[j] = x[k] and j-th
  86. column of matrix N is k-th column of matrix (I|-A) */
  87. char *stat; /* char stat[1+n]; */
  88. /* stat[0] is not used;
  89. stat[j], 1 <= j <= n, is the status of non-basic variable
  90. xN[j], which defines its active bound:
  91. GLP_NL - lower bound is active
  92. GLP_NU - upper bound is active
  93. GLP_NF - free variable
  94. GLP_NS - fixed variable */
  95. /*--------------------------------------------------------------*/
  96. /* matrix B is the basis matrix; it is composed from columns of
  97. the augmented constraint matrix (I|-A) corresponding to basic
  98. variables and stored in a factorized (invertable) form */
  99. int valid;
  100. /* factorization is valid only if this flag is set */
  101. BFD *bfd; /* BFD bfd[1:m,1:m]; */
  102. /* factorized (invertable) form of the basis matrix */
  103. /*--------------------------------------------------------------*/
  104. /* matrix N is a matrix composed from columns of the augmented
  105. constraint matrix (I|-A) corresponding to non-basic variables
  106. except fixed ones; it is stored by rows and changes every time
  107. the basis changes */
  108. int *N_ptr; /* int N_ptr[1+m+1]; */
  109. /* N_ptr[0] is not used;
  110. N_ptr[i], 1 <= i <= m, is starting position of i-th row in
  111. arrays N_ind and N_val; note that N_ptr[1] is always 1;
  112. N_ptr[m+1] indicates the position after the last element in
  113. arrays N_ind and N_val */
  114. int *N_len; /* int N_len[1+m]; */
  115. /* N_len[0] is not used;
  116. N_len[i], 1 <= i <= m, is length of i-th row (0 to n) */
  117. int *N_ind; /* int N_ind[N_ptr[m+1]]; */
  118. /* column indices */
  119. double *N_val; /* double N_val[N_ptr[m+1]]; */
  120. /* non-zero element values */
  121. /*--------------------------------------------------------------*/
  122. /* working parameters */
  123. int phase;
  124. /* search phase:
  125. 0 - not determined yet
  126. 1 - search for primal feasible solution
  127. 2 - search for optimal solution */
  128. glp_long tm_beg;
  129. /* time value at the beginning of the search */
  130. int it_beg;
  131. /* simplex iteration count at the beginning of the search */
  132. int it_cnt;
  133. /* simplex iteration count; it increases by one every time the
  134. basis changes (including the case when a non-basic variable
  135. jumps to its opposite bound) */
  136. int it_dpy;
  137. /* simplex iteration count at the most recent display output */
  138. /*--------------------------------------------------------------*/
  139. /* basic solution components */
  140. double *bbar; /* double bbar[1+m]; */
  141. /* bbar[0] is not used;
  142. bbar[i], 1 <= i <= m, is primal value of basic variable xB[i]
  143. (if xB[i] is free, its primal value is not updated) */
  144. double *cbar; /* double cbar[1+n]; */
  145. /* cbar[0] is not used;
  146. cbar[j], 1 <= j <= n, is reduced cost of non-basic variable
  147. xN[j] (if xN[j] is fixed, its reduced cost is not updated) */
  148. /*--------------------------------------------------------------*/
  149. /* the following pricing technique options may be used:
  150. GLP_PT_STD - standard ("textbook") pricing;
  151. GLP_PT_PSE - projected steepest edge;
  152. GLP_PT_DVX - Devex pricing (not implemented yet);
  153. in case of GLP_PT_STD the reference space is not used, and all
  154. steepest edge coefficients are set to 1 */
  155. int refct;
  156. /* this count is set to an initial value when the reference space
  157. is defined and decreases by one every time the basis changes;
  158. once this count reaches zero, the reference space is redefined
  159. again */
  160. char *refsp; /* char refsp[1+m+n]; */
  161. /* refsp[0] is not used;
  162. refsp[k], 1 <= k <= m+n, is the flag which means that variable
  163. x[k] belongs to the current reference space */
  164. double *gamma; /* double gamma[1+n]; */
  165. /* gamma[0] is not used;
  166. gamma[j], 1 <= j <= n, is the steepest edge coefficient for
  167. non-basic variable xN[j]; if xN[j] is fixed, gamma[j] is not
  168. used and just set to 1 */
  169. /*--------------------------------------------------------------*/
  170. /* non-basic variable xN[q] chosen to enter the basis */
  171. int q;
  172. /* index of the non-basic variable xN[q] chosen, 1 <= q <= n;
  173. if the set of eligible non-basic variables is empty and thus
  174. no variable has been chosen, q is set to 0 */
  175. /*--------------------------------------------------------------*/
  176. /* pivot column of the simplex table corresponding to non-basic
  177. variable xN[q] chosen is the following vector:
  178. T * e[q] = - inv(B) * N * e[q] = - inv(B) * N[q],
  179. where B is the current basis matrix, N[q] is a column of the
  180. matrix (I|-A) corresponding to xN[q] */
  181. int tcol_nnz;
  182. /* number of non-zero components, 0 <= nnz <= m */
  183. int *tcol_ind; /* int tcol_ind[1+m]; */
  184. /* tcol_ind[0] is not used;
  185. tcol_ind[t], 1 <= t <= nnz, is an index of non-zero component,
  186. i.e. tcol_ind[t] = i means that tcol_vec[i] != 0 */
  187. double *tcol_vec; /* double tcol_vec[1+m]; */
  188. /* tcol_vec[0] is not used;
  189. tcol_vec[i], 1 <= i <= m, is a numeric value of i-th component
  190. of the column */
  191. double tcol_max;
  192. /* infinity (maximum) norm of the column (max |tcol_vec[i]|) */
  193. int tcol_num;
  194. /* number of significant non-zero components, which means that:
  195. |tcol_vec[i]| >= eps for i in tcol_ind[1,...,num],
  196. |tcol_vec[i]| < eps for i in tcol_ind[num+1,...,nnz],
  197. where eps is a pivot tolerance */
  198. /*--------------------------------------------------------------*/
  199. /* basic variable xB[p] chosen to leave the basis */
  200. int p;
  201. /* index of the basic variable xB[p] chosen, 1 <= p <= m;
  202. p = 0 means that no basic variable reaches its bound;
  203. p < 0 means that non-basic variable xN[q] reaches its opposite
  204. bound before any basic variable */
  205. int p_stat;
  206. /* new status (GLP_NL, GLP_NU, or GLP_NS) to be assigned to xB[p]
  207. once it has left the basis */
  208. double teta;
  209. /* change of non-basic variable xN[q] (see above), on which xB[p]
  210. (or, if p < 0, xN[q] itself) reaches its bound */
  211. /*--------------------------------------------------------------*/
  212. /* pivot row of the simplex table corresponding to basic variable
  213. xB[p] chosen is the following vector:
  214. T' * e[p] = - N' * inv(B') * e[p] = - N' * rho,
  215. where B' is a matrix transposed to the current basis matrix,
  216. N' is a matrix, whose rows are columns of the matrix (I|-A)
  217. corresponding to non-basic non-fixed variables */
  218. int trow_nnz;
  219. /* number of non-zero components, 0 <= nnz <= n */
  220. int *trow_ind; /* int trow_ind[1+n]; */
  221. /* trow_ind[0] is not used;
  222. trow_ind[t], 1 <= t <= nnz, is an index of non-zero component,
  223. i.e. trow_ind[t] = j means that trow_vec[j] != 0 */
  224. double *trow_vec; /* int trow_vec[1+n]; */
  225. /* trow_vec[0] is not used;
  226. trow_vec[j], 1 <= j <= n, is a numeric value of j-th component
  227. of the row */
  228. /*--------------------------------------------------------------*/
  229. /* working arrays */
  230. double *work1; /* double work1[1+m]; */
  231. double *work2; /* double work2[1+m]; */
  232. double *work3; /* double work3[1+m]; */
  233. double *work4; /* double work4[1+m]; */
  234. };
  235. static const double kappa = 0.10;
  236. /***********************************************************************
  237. * alloc_csa - allocate common storage area
  238. *
  239. * This routine allocates all arrays in the common storage area (CSA)
  240. * and returns a pointer to the CSA. */
  241. static struct csa *alloc_csa(glp_prob *lp)
  242. { struct csa *csa;
  243. int m = lp->m;
  244. int n = lp->n;
  245. int nnz = lp->nnz;
  246. csa = xmalloc(sizeof(struct csa));
  247. xassert(m > 0 && n > 0);
  248. csa->m = m;
  249. csa->n = n;
  250. csa->type = xcalloc(1+m+n, sizeof(char));
  251. csa->lb = xcalloc(1+m+n, sizeof(double));
  252. csa->ub = xcalloc(1+m+n, sizeof(double));
  253. csa->coef = xcalloc(1+m+n, sizeof(double));
  254. csa->obj = xcalloc(1+n, sizeof(double));
  255. csa->A_ptr = xcalloc(1+n+1, sizeof(int));
  256. csa->A_ind = xcalloc(1+nnz, sizeof(int));
  257. csa->A_val = xcalloc(1+nnz, sizeof(double));
  258. csa->head = xcalloc(1+m+n, sizeof(int));
  259. csa->stat = xcalloc(1+n, sizeof(char));
  260. csa->N_ptr = xcalloc(1+m+1, sizeof(int));
  261. csa->N_len = xcalloc(1+m, sizeof(int));
  262. csa->N_ind = NULL; /* will be allocated later */
  263. csa->N_val = NULL; /* will be allocated later */
  264. csa->bbar = xcalloc(1+m, sizeof(double));
  265. csa->cbar = xcalloc(1+n, sizeof(double));
  266. csa->refsp = xcalloc(1+m+n, sizeof(char));
  267. csa->gamma = xcalloc(1+n, sizeof(double));
  268. csa->tcol_ind = xcalloc(1+m, sizeof(int));
  269. csa->tcol_vec = xcalloc(1+m, sizeof(double));
  270. csa->trow_ind = xcalloc(1+n, sizeof(int));
  271. csa->trow_vec = xcalloc(1+n, sizeof(double));
  272. csa->work1 = xcalloc(1+m, sizeof(double));
  273. csa->work2 = xcalloc(1+m, sizeof(double));
  274. csa->work3 = xcalloc(1+m, sizeof(double));
  275. csa->work4 = xcalloc(1+m, sizeof(double));
  276. return csa;
  277. }
  278. /***********************************************************************
  279. * init_csa - initialize common storage area
  280. *
  281. * This routine initializes all data structures in the common storage
  282. * area (CSA). */
  283. static void alloc_N(struct csa *csa);
  284. static void build_N(struct csa *csa);
  285. static void init_csa(struct csa *csa, glp_prob *lp)
  286. { int m = csa->m;
  287. int n = csa->n;
  288. char *type = csa->type;
  289. double *lb = csa->lb;
  290. double *ub = csa->ub;
  291. double *coef = csa->coef;
  292. double *obj = csa->obj;
  293. int *A_ptr = csa->A_ptr;
  294. int *A_ind = csa->A_ind;
  295. double *A_val = csa->A_val;
  296. int *head = csa->head;
  297. char *stat = csa->stat;
  298. char *refsp = csa->refsp;
  299. double *gamma = csa->gamma;
  300. int i, j, k, loc;
  301. double cmax;
  302. /* auxiliary variables */
  303. for (i = 1; i <= m; i++)
  304. { GLPROW *row = lp->row[i];
  305. type[i] = (char)row->type;
  306. lb[i] = row->lb * row->rii;
  307. ub[i] = row->ub * row->rii;
  308. coef[i] = 0.0;
  309. }
  310. /* structural variables */
  311. for (j = 1; j <= n; j++)
  312. { GLPCOL *col = lp->col[j];
  313. type[m+j] = (char)col->type;
  314. lb[m+j] = col->lb / col->sjj;
  315. ub[m+j] = col->ub / col->sjj;
  316. coef[m+j] = col->coef * col->sjj;
  317. }
  318. /* original objective function */
  319. obj[0] = lp->c0;
  320. memcpy(&obj[1], &coef[m+1], n * sizeof(double));
  321. /* factor used to scale original objective coefficients */
  322. cmax = 0.0;
  323. for (j = 1; j <= n; j++)
  324. if (cmax < fabs(obj[j])) cmax = fabs(obj[j]);
  325. if (cmax == 0.0) cmax = 1.0;
  326. switch (lp->dir)
  327. { case GLP_MIN:
  328. csa->zeta = + 1.0 / cmax;
  329. break;
  330. case GLP_MAX:
  331. csa->zeta = - 1.0 / cmax;
  332. break;
  333. default:
  334. xassert(lp != lp);
  335. }
  336. #if 1
  337. if (fabs(csa->zeta) < 1.0) csa->zeta *= 1000.0;
  338. #endif
  339. /* matrix A (by columns) */
  340. loc = 1;
  341. for (j = 1; j <= n; j++)
  342. { GLPAIJ *aij;
  343. A_ptr[j] = loc;
  344. for (aij = lp->col[j]->ptr; aij != NULL; aij = aij->c_next)
  345. { A_ind[loc] = aij->row->i;
  346. A_val[loc] = aij->row->rii * aij->val * aij->col->sjj;
  347. loc++;
  348. }
  349. }
  350. A_ptr[n+1] = loc;
  351. xassert(loc == lp->nnz+1);
  352. /* basis header */
  353. xassert(lp->valid);
  354. memcpy(&head[1], &lp->head[1], m * sizeof(int));
  355. k = 0;
  356. for (i = 1; i <= m; i++)
  357. { GLPROW *row = lp->row[i];
  358. if (row->stat != GLP_BS)
  359. { k++;
  360. xassert(k <= n);
  361. head[m+k] = i;
  362. stat[k] = (char)row->stat;
  363. }
  364. }
  365. for (j = 1; j <= n; j++)
  366. { GLPCOL *col = lp->col[j];
  367. if (col->stat != GLP_BS)
  368. { k++;
  369. xassert(k <= n);
  370. head[m+k] = m + j;
  371. stat[k] = (char)col->stat;
  372. }
  373. }
  374. xassert(k == n);
  375. /* factorization of matrix B */
  376. csa->valid = 1, lp->valid = 0;
  377. csa->bfd = lp->bfd, lp->bfd = NULL;
  378. /* matrix N (by rows) */
  379. alloc_N(csa);
  380. build_N(csa);
  381. /* working parameters */
  382. csa->phase = 0;
  383. csa->tm_beg = xtime();
  384. csa->it_beg = csa->it_cnt = lp->it_cnt;
  385. csa->it_dpy = -1;
  386. /* reference space and steepest edge coefficients */
  387. csa->refct = 0;
  388. memset(&refsp[1], 0, (m+n) * sizeof(char));
  389. for (j = 1; j <= n; j++) gamma[j] = 1.0;
  390. return;
  391. }
  392. /***********************************************************************
  393. * invert_B - compute factorization of the basis matrix
  394. *
  395. * This routine computes factorization of the current basis matrix B.
  396. *
  397. * If the operation is successful, the routine returns zero, otherwise
  398. * non-zero. */
  399. static int inv_col(void *info, int i, int ind[], double val[])
  400. { /* this auxiliary routine returns row indices and numeric values
  401. of non-zero elements of i-th column of the basis matrix */
  402. struct csa *csa = info;
  403. int m = csa->m;
  404. #ifdef GLP_DEBUG
  405. int n = csa->n;
  406. #endif
  407. int *A_ptr = csa->A_ptr;
  408. int *A_ind = csa->A_ind;
  409. double *A_val = csa->A_val;
  410. int *head = csa->head;
  411. int k, len, ptr, t;
  412. #ifdef GLP_DEBUG
  413. xassert(1 <= i && i <= m);
  414. #endif
  415. k = head[i]; /* B[i] is k-th column of (I|-A) */
  416. #ifdef GLP_DEBUG
  417. xassert(1 <= k && k <= m+n);
  418. #endif
  419. if (k <= m)
  420. { /* B[i] is k-th column of submatrix I */
  421. len = 1;
  422. ind[1] = k;
  423. val[1] = 1.0;
  424. }
  425. else
  426. { /* B[i] is (k-m)-th column of submatrix (-A) */
  427. ptr = A_ptr[k-m];
  428. len = A_ptr[k-m+1] - ptr;
  429. memcpy(&ind[1], &A_ind[ptr], len * sizeof(int));
  430. memcpy(&val[1], &A_val[ptr], len * sizeof(double));
  431. for (t = 1; t <= len; t++) val[t] = - val[t];
  432. }
  433. return len;
  434. }
  435. static int invert_B(struct csa *csa)
  436. { int ret;
  437. ret = bfd_factorize(csa->bfd, csa->m, NULL, inv_col, csa);
  438. csa->valid = (ret == 0);
  439. return ret;
  440. }
  441. /***********************************************************************
  442. * update_B - update factorization of the basis matrix
  443. *
  444. * This routine replaces i-th column of the basis matrix B by k-th
  445. * column of the augmented constraint matrix (I|-A) and then updates
  446. * the factorization of B.
  447. *
  448. * If the factorization has been successfully updated, the routine
  449. * returns zero, otherwise non-zero. */
  450. static int update_B(struct csa *csa, int i, int k)
  451. { int m = csa->m;
  452. #ifdef GLP_DEBUG
  453. int n = csa->n;
  454. #endif
  455. int ret;
  456. #ifdef GLP_DEBUG
  457. xassert(1 <= i && i <= m);
  458. xassert(1 <= k && k <= m+n);
  459. #endif
  460. if (k <= m)
  461. { /* new i-th column of B is k-th column of I */
  462. int ind[1+1];
  463. double val[1+1];
  464. ind[1] = k;
  465. val[1] = 1.0;
  466. xassert(csa->valid);
  467. ret = bfd_update_it(csa->bfd, i, 0, 1, ind, val);
  468. }
  469. else
  470. { /* new i-th column of B is (k-m)-th column of (-A) */
  471. int *A_ptr = csa->A_ptr;
  472. int *A_ind = csa->A_ind;
  473. double *A_val = csa->A_val;
  474. double *val = csa->work1;
  475. int beg, end, ptr, len;
  476. beg = A_ptr[k-m];
  477. end = A_ptr[k-m+1];
  478. len = 0;
  479. for (ptr = beg; ptr < end; ptr++)
  480. val[++len] = - A_val[ptr];
  481. xassert(csa->valid);
  482. ret = bfd_update_it(csa->bfd, i, 0, len, &A_ind[beg-1], val);
  483. }
  484. csa->valid = (ret == 0);
  485. return ret;
  486. }
  487. /***********************************************************************
  488. * error_ftran - compute residual vector r = h - B * x
  489. *
  490. * This routine computes the residual vector r = h - B * x, where B is
  491. * the current basis matrix, h is the vector of right-hand sides, x is
  492. * the solution vector. */
  493. static void error_ftran(struct csa *csa, double h[], double x[],
  494. double r[])
  495. { int m = csa->m;
  496. #ifdef GLP_DEBUG
  497. int n = csa->n;
  498. #endif
  499. int *A_ptr = csa->A_ptr;
  500. int *A_ind = csa->A_ind;
  501. double *A_val = csa->A_val;
  502. int *head = csa->head;
  503. int i, k, beg, end, ptr;
  504. double temp;
  505. /* compute the residual vector:
  506. r = h - B * x = h - B[1] * x[1] - ... - B[m] * x[m],
  507. where B[1], ..., B[m] are columns of matrix B */
  508. memcpy(&r[1], &h[1], m * sizeof(double));
  509. for (i = 1; i <= m; i++)
  510. { temp = x[i];
  511. if (temp == 0.0) continue;
  512. k = head[i]; /* B[i] is k-th column of (I|-A) */
  513. #ifdef GLP_DEBUG
  514. xassert(1 <= k && k <= m+n);
  515. #endif
  516. if (k <= m)
  517. { /* B[i] is k-th column of submatrix I */
  518. r[k] -= temp;
  519. }
  520. else
  521. { /* B[i] is (k-m)-th column of submatrix (-A) */
  522. beg = A_ptr[k-m];
  523. end = A_ptr[k-m+1];
  524. for (ptr = beg; ptr < end; ptr++)
  525. r[A_ind[ptr]] += A_val[ptr] * temp;
  526. }
  527. }
  528. return;
  529. }
  530. /***********************************************************************
  531. * refine_ftran - refine solution of B * x = h
  532. *
  533. * This routine performs one iteration to refine the solution of
  534. * the system B * x = h, where B is the current basis matrix, h is the
  535. * vector of right-hand sides, x is the solution vector. */
  536. static void refine_ftran(struct csa *csa, double h[], double x[])
  537. { int m = csa->m;
  538. double *r = csa->work1;
  539. double *d = csa->work1;
  540. int i;
  541. /* compute the residual vector r = h - B * x */
  542. error_ftran(csa, h, x, r);
  543. /* compute the correction vector d = inv(B) * r */
  544. xassert(csa->valid);
  545. bfd_ftran(csa->bfd, d);
  546. /* refine the solution vector (new x) = (old x) + d */
  547. for (i = 1; i <= m; i++) x[i] += d[i];
  548. return;
  549. }
  550. /***********************************************************************
  551. * error_btran - compute residual vector r = h - B'* x
  552. *
  553. * This routine computes the residual vector r = h - B'* x, where B'
  554. * is a matrix transposed to the current basis matrix, h is the vector
  555. * of right-hand sides, x is the solution vector. */
  556. static void error_btran(struct csa *csa, double h[], double x[],
  557. double r[])
  558. { int m = csa->m;
  559. #ifdef GLP_DEBUG
  560. int n = csa->n;
  561. #endif
  562. int *A_ptr = csa->A_ptr;
  563. int *A_ind = csa->A_ind;
  564. double *A_val = csa->A_val;
  565. int *head = csa->head;
  566. int i, k, beg, end, ptr;
  567. double temp;
  568. /* compute the residual vector r = b - B'* x */
  569. for (i = 1; i <= m; i++)
  570. { /* r[i] := b[i] - (i-th column of B)'* x */
  571. k = head[i]; /* B[i] is k-th column of (I|-A) */
  572. #ifdef GLP_DEBUG
  573. xassert(1 <= k && k <= m+n);
  574. #endif
  575. temp = h[i];
  576. if (k <= m)
  577. { /* B[i] is k-th column of submatrix I */
  578. temp -= x[k];
  579. }
  580. else
  581. { /* B[i] is (k-m)-th column of submatrix (-A) */
  582. beg = A_ptr[k-m];
  583. end = A_ptr[k-m+1];
  584. for (ptr = beg; ptr < end; ptr++)
  585. temp += A_val[ptr] * x[A_ind[ptr]];
  586. }
  587. r[i] = temp;
  588. }
  589. return;
  590. }
  591. /***********************************************************************
  592. * refine_btran - refine solution of B'* x = h
  593. *
  594. * This routine performs one iteration to refine the solution of the
  595. * system B'* x = h, where B' is a matrix transposed to the current
  596. * basis matrix, h is the vector of right-hand sides, x is the solution
  597. * vector. */
  598. static void refine_btran(struct csa *csa, double h[], double x[])
  599. { int m = csa->m;
  600. double *r = csa->work1;
  601. double *d = csa->work1;
  602. int i;
  603. /* compute the residual vector r = h - B'* x */
  604. error_btran(csa, h, x, r);
  605. /* compute the correction vector d = inv(B') * r */
  606. xassert(csa->valid);
  607. bfd_btran(csa->bfd, d);
  608. /* refine the solution vector (new x) = (old x) + d */
  609. for (i = 1; i <= m; i++) x[i] += d[i];
  610. return;
  611. }
  612. /***********************************************************************
  613. * alloc_N - allocate matrix N
  614. *
  615. * This routine determines maximal row lengths of matrix N, sets its
  616. * row pointers, and then allocates arrays N_ind and N_val.
  617. *
  618. * Note that some fixed structural variables may temporarily become
  619. * double-bounded, so corresponding columns of matrix A should not be
  620. * ignored on calculating maximal row lengths of matrix N. */
  621. static void alloc_N(struct csa *csa)
  622. { int m = csa->m;
  623. int n = csa->n;
  624. int *A_ptr = csa->A_ptr;
  625. int *A_ind = csa->A_ind;
  626. int *N_ptr = csa->N_ptr;
  627. int *N_len = csa->N_len;
  628. int i, j, beg, end, ptr;
  629. /* determine number of non-zeros in each row of the augmented
  630. constraint matrix (I|-A) */
  631. for (i = 1; i <= m; i++)
  632. N_len[i] = 1;
  633. for (j = 1; j <= n; j++)
  634. { beg = A_ptr[j];
  635. end = A_ptr[j+1];
  636. for (ptr = beg; ptr < end; ptr++)
  637. N_len[A_ind[ptr]]++;
  638. }
  639. /* determine maximal row lengths of matrix N and set its row
  640. pointers */
  641. N_ptr[1] = 1;
  642. for (i = 1; i <= m; i++)
  643. { /* row of matrix N cannot have more than n non-zeros */
  644. if (N_len[i] > n) N_len[i] = n;
  645. N_ptr[i+1] = N_ptr[i] + N_len[i];
  646. }
  647. /* now maximal number of non-zeros in matrix N is known */
  648. csa->N_ind = xcalloc(N_ptr[m+1], sizeof(int));
  649. csa->N_val = xcalloc(N_ptr[m+1], sizeof(double));
  650. return;
  651. }
  652. /***********************************************************************
  653. * add_N_col - add column of matrix (I|-A) to matrix N
  654. *
  655. * This routine adds j-th column to matrix N which is k-th column of
  656. * the augmented constraint matrix (I|-A). (It is assumed that old j-th
  657. * column was previously removed from matrix N.) */
  658. static void add_N_col(struct csa *csa, int j, int k)
  659. { int m = csa->m;
  660. #ifdef GLP_DEBUG
  661. int n = csa->n;
  662. #endif
  663. int *N_ptr = csa->N_ptr;
  664. int *N_len = csa->N_len;
  665. int *N_ind = csa->N_ind;
  666. double *N_val = csa->N_val;
  667. int pos;
  668. #ifdef GLP_DEBUG
  669. xassert(1 <= j && j <= n);
  670. xassert(1 <= k && k <= m+n);
  671. #endif
  672. if (k <= m)
  673. { /* N[j] is k-th column of submatrix I */
  674. pos = N_ptr[k] + (N_len[k]++);
  675. #ifdef GLP_DEBUG
  676. xassert(pos < N_ptr[k+1]);
  677. #endif
  678. N_ind[pos] = j;
  679. N_val[pos] = 1.0;
  680. }
  681. else
  682. { /* N[j] is (k-m)-th column of submatrix (-A) */
  683. int *A_ptr = csa->A_ptr;
  684. int *A_ind = csa->A_ind;
  685. double *A_val = csa->A_val;
  686. int i, beg, end, ptr;
  687. beg = A_ptr[k-m];
  688. end = A_ptr[k-m+1];
  689. for (ptr = beg; ptr < end; ptr++)
  690. { i = A_ind[ptr]; /* row number */
  691. pos = N_ptr[i] + (N_len[i]++);
  692. #ifdef GLP_DEBUG
  693. xassert(pos < N_ptr[i+1]);
  694. #endif
  695. N_ind[pos] = j;
  696. N_val[pos] = - A_val[ptr];
  697. }
  698. }
  699. return;
  700. }
  701. /***********************************************************************
  702. * del_N_col - remove column of matrix (I|-A) from matrix N
  703. *
  704. * This routine removes j-th column from matrix N which is k-th column
  705. * of the augmented constraint matrix (I|-A). */
  706. static void del_N_col(struct csa *csa, int j, int k)
  707. { int m = csa->m;
  708. #ifdef GLP_DEBUG
  709. int n = csa->n;
  710. #endif
  711. int *N_ptr = csa->N_ptr;
  712. int *N_len = csa->N_len;
  713. int *N_ind = csa->N_ind;
  714. double *N_val = csa->N_val;
  715. int pos, head, tail;
  716. #ifdef GLP_DEBUG
  717. xassert(1 <= j && j <= n);
  718. xassert(1 <= k && k <= m+n);
  719. #endif
  720. if (k <= m)
  721. { /* N[j] is k-th column of submatrix I */
  722. /* find element in k-th row of N */
  723. head = N_ptr[k];
  724. for (pos = head; N_ind[pos] != j; pos++) /* nop */;
  725. /* and remove it from the row list */
  726. tail = head + (--N_len[k]);
  727. #ifdef GLP_DEBUG
  728. xassert(pos <= tail);
  729. #endif
  730. N_ind[pos] = N_ind[tail];
  731. N_val[pos] = N_val[tail];
  732. }
  733. else
  734. { /* N[j] is (k-m)-th column of submatrix (-A) */
  735. int *A_ptr = csa->A_ptr;
  736. int *A_ind = csa->A_ind;
  737. int i, beg, end, ptr;
  738. beg = A_ptr[k-m];
  739. end = A_ptr[k-m+1];
  740. for (ptr = beg; ptr < end; ptr++)
  741. { i = A_ind[ptr]; /* row number */
  742. /* find element in i-th row of N */
  743. head = N_ptr[i];
  744. for (pos = head; N_ind[pos] != j; pos++) /* nop */;
  745. /* and remove it from the row list */
  746. tail = head + (--N_len[i]);
  747. #ifdef GLP_DEBUG
  748. xassert(pos <= tail);
  749. #endif
  750. N_ind[pos] = N_ind[tail];
  751. N_val[pos] = N_val[tail];
  752. }
  753. }
  754. return;
  755. }
  756. /***********************************************************************
  757. * build_N - build matrix N for current basis
  758. *
  759. * This routine builds matrix N for the current basis from columns
  760. * of the augmented constraint matrix (I|-A) corresponding to non-basic
  761. * non-fixed variables. */
  762. static void build_N(struct csa *csa)
  763. { int m = csa->m;
  764. int n = csa->n;
  765. int *head = csa->head;
  766. char *stat = csa->stat;
  767. int *N_len = csa->N_len;
  768. int j, k;
  769. /* N := empty matrix */
  770. memset(&N_len[1], 0, m * sizeof(int));
  771. /* go through non-basic columns of matrix (I|-A) */
  772. for (j = 1; j <= n; j++)
  773. { if (stat[j] != GLP_NS)
  774. { /* xN[j] is non-fixed; add j-th column to matrix N which is
  775. k-th column of matrix (I|-A) */
  776. k = head[m+j]; /* x[k] = xN[j] */
  777. #ifdef GLP_DEBUG
  778. xassert(1 <= k && k <= m+n);
  779. #endif
  780. add_N_col(csa, j, k);
  781. }
  782. }
  783. return;
  784. }
  785. /***********************************************************************
  786. * get_xN - determine current value of non-basic variable xN[j]
  787. *
  788. * This routine returns the current value of non-basic variable xN[j],
  789. * which is a value of its active bound. */
  790. static double get_xN(struct csa *csa, int j)
  791. { int m = csa->m;
  792. #ifdef GLP_DEBUG
  793. int n = csa->n;
  794. #endif
  795. double *lb = csa->lb;
  796. double *ub = csa->ub;
  797. int *head = csa->head;
  798. char *stat = csa->stat;
  799. int k;
  800. double xN;
  801. #ifdef GLP_DEBUG
  802. xassert(1 <= j && j <= n);
  803. #endif
  804. k = head[m+j]; /* x[k] = xN[j] */
  805. #ifdef GLP_DEBUG
  806. xassert(1 <= k && k <= m+n);
  807. #endif
  808. switch (stat[j])
  809. { case GLP_NL:
  810. /* x[k] is on its lower bound */
  811. xN = lb[k]; break;
  812. case GLP_NU:
  813. /* x[k] is on its upper bound */
  814. xN = ub[k]; break;
  815. case GLP_NF:
  816. /* x[k] is free non-basic variable */
  817. xN = 0.0; break;
  818. case GLP_NS:
  819. /* x[k] is fixed non-basic variable */
  820. xN = lb[k]; break;
  821. default:
  822. xassert(stat != stat);
  823. }
  824. return xN;
  825. }
  826. /***********************************************************************
  827. * eval_beta - compute primal values of basic variables
  828. *
  829. * This routine computes current primal values of all basic variables:
  830. *
  831. * beta = - inv(B) * N * xN,
  832. *
  833. * where B is the current basis matrix, N is a matrix built of columns
  834. * of matrix (I|-A) corresponding to non-basic variables, and xN is the
  835. * vector of current values of non-basic variables. */
  836. static void eval_beta(struct csa *csa, double beta[])
  837. { int m = csa->m;
  838. int n = csa->n;
  839. int *A_ptr = csa->A_ptr;
  840. int *A_ind = csa->A_ind;
  841. double *A_val = csa->A_val;
  842. int *head = csa->head;
  843. double *h = csa->work2;
  844. int i, j, k, beg, end, ptr;
  845. double xN;
  846. /* compute the right-hand side vector:
  847. h := - N * xN = - N[1] * xN[1] - ... - N[n] * xN[n],
  848. where N[1], ..., N[n] are columns of matrix N */
  849. for (i = 1; i <= m; i++)
  850. h[i] = 0.0;
  851. for (j = 1; j <= n; j++)
  852. { k = head[m+j]; /* x[k] = xN[j] */
  853. #ifdef GLP_DEBUG
  854. xassert(1 <= k && k <= m+n);
  855. #endif
  856. /* determine current value of xN[j] */
  857. xN = get_xN(csa, j);
  858. if (xN == 0.0) continue;
  859. if (k <= m)
  860. { /* N[j] is k-th column of submatrix I */
  861. h[k] -= xN;
  862. }
  863. else
  864. { /* N[j] is (k-m)-th column of submatrix (-A) */
  865. beg = A_ptr[k-m];
  866. end = A_ptr[k-m+1];
  867. for (ptr = beg; ptr < end; ptr++)
  868. h[A_ind[ptr]] += xN * A_val[ptr];
  869. }
  870. }
  871. /* solve system B * beta = h */
  872. memcpy(&beta[1], &h[1], m * sizeof(double));
  873. xassert(csa->valid);
  874. bfd_ftran(csa->bfd, beta);
  875. /* and refine the solution */
  876. refine_ftran(csa, h, beta);
  877. return;
  878. }
  879. /***********************************************************************
  880. * eval_pi - compute vector of simplex multipliers
  881. *
  882. * This routine computes the vector of current simplex multipliers:
  883. *
  884. * pi = inv(B') * cB,
  885. *
  886. * where B' is a matrix transposed to the current basis matrix, cB is
  887. * a subvector of objective coefficients at basic variables. */
  888. static void eval_pi(struct csa *csa, double pi[])
  889. { int m = csa->m;
  890. double *c = csa->coef;
  891. int *head = csa->head;
  892. double *cB = csa->work2;
  893. int i;
  894. /* construct the right-hand side vector cB */
  895. for (i = 1; i <= m; i++)
  896. cB[i] = c[head[i]];
  897. /* solve system B'* pi = cB */
  898. memcpy(&pi[1], &cB[1], m * sizeof(double));
  899. xassert(csa->valid);
  900. bfd_btran(csa->bfd, pi);
  901. /* and refine the solution */
  902. refine_btran(csa, cB, pi);
  903. return;
  904. }
  905. /***********************************************************************
  906. * eval_cost - compute reduced cost of non-basic variable xN[j]
  907. *
  908. * This routine computes the current reduced cost of non-basic variable
  909. * xN[j]:
  910. *
  911. * d[j] = cN[j] - N'[j] * pi,
  912. *
  913. * where cN[j] is the objective coefficient at variable xN[j], N[j] is
  914. * a column of the augmented constraint matrix (I|-A) corresponding to
  915. * xN[j], pi is the vector of simplex multipliers. */
  916. static double eval_cost(struct csa *csa, double pi[], int j)
  917. { int m = csa->m;
  918. #ifdef GLP_DEBUG
  919. int n = csa->n;
  920. #endif
  921. double *coef = csa->coef;
  922. int *head = csa->head;
  923. int k;
  924. double dj;
  925. #ifdef GLP_DEBUG
  926. xassert(1 <= j && j <= n);
  927. #endif
  928. k = head[m+j]; /* x[k] = xN[j] */
  929. #ifdef GLP_DEBUG
  930. xassert(1 <= k && k <= m+n);
  931. #endif
  932. dj = coef[k];
  933. if (k <= m)
  934. { /* N[j] is k-th column of submatrix I */
  935. dj -= pi[k];
  936. }
  937. else
  938. { /* N[j] is (k-m)-th column of submatrix (-A) */
  939. int *A_ptr = csa->A_ptr;
  940. int *A_ind = csa->A_ind;
  941. double *A_val = csa->A_val;
  942. int beg, end, ptr;
  943. beg = A_ptr[k-m];
  944. end = A_ptr[k-m+1];
  945. for (ptr = beg; ptr < end; ptr++)
  946. dj += A_val[ptr] * pi[A_ind[ptr]];
  947. }
  948. return dj;
  949. }
  950. /***********************************************************************
  951. * eval_bbar - compute and store primal values of basic variables
  952. *
  953. * This routine computes primal values of all basic variables and then
  954. * stores them in the solution array. */
  955. static void eval_bbar(struct csa *csa)
  956. { eval_beta(csa, csa->bbar);
  957. return;
  958. }
  959. /***********************************************************************
  960. * eval_cbar - compute and store reduced costs of non-basic variables
  961. *
  962. * This routine computes reduced costs of all non-basic variables and
  963. * then stores them in the solution array. */
  964. static void eval_cbar(struct csa *csa)
  965. {
  966. #ifdef GLP_DEBUG
  967. int m = csa->m;
  968. #endif
  969. int n = csa->n;
  970. #ifdef GLP_DEBUG
  971. int *head = csa->head;
  972. #endif
  973. double *cbar = csa->cbar;
  974. double *pi = csa->work3;
  975. int j;
  976. #ifdef GLP_DEBUG
  977. int k;
  978. #endif
  979. /* compute simplex multipliers */
  980. eval_pi(csa, pi);
  981. /* compute and store reduced costs */
  982. for (j = 1; j <= n; j++)
  983. {
  984. #ifdef GLP_DEBUG
  985. k = head[m+j]; /* x[k] = xN[j] */
  986. xassert(1 <= k && k <= m+n);
  987. #endif
  988. cbar[j] = eval_cost(csa, pi, j);
  989. }
  990. return;
  991. }
  992. /***********************************************************************
  993. * reset_refsp - reset the reference space
  994. *
  995. * This routine resets (redefines) the reference space used in the
  996. * projected steepest edge pricing algorithm. */
  997. static void reset_refsp(struct csa *csa)
  998. { int m = csa->m;
  999. int n = csa->n;
  1000. int *head = csa->head;
  1001. char *refsp = csa->refsp;
  1002. double *gamma = csa->gamma;
  1003. int j, k;
  1004. xassert(csa->refct == 0);
  1005. csa->refct = 1000;
  1006. memset(&refsp[1], 0, (m+n) * sizeof(char));
  1007. for (j = 1; j <= n; j++)
  1008. { k = head[m+j]; /* x[k] = xN[j] */
  1009. refsp[k] = 1;
  1010. gamma[j] = 1.0;
  1011. }
  1012. return;
  1013. }
  1014. /***********************************************************************
  1015. * eval_gamma - compute steepest edge coefficient
  1016. *
  1017. * This routine computes the steepest edge coefficient for non-basic
  1018. * variable xN[j] using its direct definition:
  1019. *
  1020. * gamma[j] = delta[j] + sum alfa[i,j]^2,
  1021. * i in R
  1022. *
  1023. * where delta[j] = 1, if xN[j] is in the current reference space,
  1024. * and 0 otherwise; R is a set of basic variables xB[i], which are in
  1025. * the current reference space; alfa[i,j] are elements of the current
  1026. * simplex table.
  1027. *
  1028. * NOTE: The routine is intended only for debugginig purposes. */
  1029. static double eval_gamma(struct csa *csa, int j)
  1030. { int m = csa->m;
  1031. #ifdef GLP_DEBUG
  1032. int n = csa->n;
  1033. #endif
  1034. int *head = csa->head;
  1035. char *refsp = csa->refsp;
  1036. double *alfa = csa->work3;
  1037. double *h = csa->work3;
  1038. int i, k;
  1039. double gamma;
  1040. #ifdef GLP_DEBUG
  1041. xassert(1 <= j && j <= n);
  1042. #endif
  1043. k = head[m+j]; /* x[k] = xN[j] */
  1044. #ifdef GLP_DEBUG
  1045. xassert(1 <= k && k <= m+n);
  1046. #endif
  1047. /* construct the right-hand side vector h = - N[j] */
  1048. for (i = 1; i <= m; i++)
  1049. h[i] = 0.0;
  1050. if (k <= m)
  1051. { /* N[j] is k-th column of submatrix I */
  1052. h[k] = -1.0;
  1053. }
  1054. else
  1055. { /* N[j] is (k-m)-th column of submatrix (-A) */
  1056. int *A_ptr = csa->A_ptr;
  1057. int *A_ind = csa->A_ind;
  1058. double *A_val = csa->A_val;
  1059. int beg, end, ptr;
  1060. beg = A_ptr[k-m];
  1061. end = A_ptr[k-m+1];
  1062. for (ptr = beg; ptr < end; ptr++)
  1063. h[A_ind[ptr]] = A_val[ptr];
  1064. }
  1065. /* solve system B * alfa = h */
  1066. xassert(csa->valid);
  1067. bfd_ftran(csa->bfd, alfa);
  1068. /* compute gamma */
  1069. gamma = (refsp[k] ? 1.0 : 0.0);
  1070. for (i = 1; i <= m; i++)
  1071. { k = head[i];
  1072. #ifdef GLP_DEBUG
  1073. xassert(1 <= k && k <= m+n);
  1074. #endif
  1075. if (refsp[k]) gamma += alfa[i] * alfa[i];
  1076. }
  1077. return gamma;
  1078. }
  1079. /***********************************************************************
  1080. * chuzc - choose non-basic variable (column of the simplex table)
  1081. *
  1082. * This routine chooses non-basic variable xN[q], which has largest
  1083. * weighted reduced cost:
  1084. *
  1085. * |d[q]| / sqrt(gamma[q]) = max |d[j]| / sqrt(gamma[j]),
  1086. * j in J
  1087. *
  1088. * where J is a subset of eligible non-basic variables xN[j], d[j] is
  1089. * reduced cost of xN[j], gamma[j] is the steepest edge coefficient.
  1090. *
  1091. * The working objective function is always minimized, so the sign of
  1092. * d[q] determines direction, in which xN[q] has to change:
  1093. *
  1094. * if d[q] < 0, xN[q] has to increase;
  1095. *
  1096. * if d[q] > 0, xN[q] has to decrease.
  1097. *
  1098. * If |d[j]| <= tol_dj, where tol_dj is a specified tolerance, xN[j]
  1099. * is not included in J and therefore ignored. (It is assumed that the
  1100. * working objective row is appropriately scaled, i.e. max|c[k]| = 1.)
  1101. *
  1102. * If J is empty and no variable has been chosen, q is set to 0. */
  1103. static void chuzc(struct csa *csa, double tol_dj)
  1104. { int n = csa->n;
  1105. char *stat = csa->stat;
  1106. double *cbar = csa->cbar;
  1107. double *gamma = csa->gamma;
  1108. int j, q;
  1109. double dj, best, temp;
  1110. /* nothing is chosen so far */
  1111. q = 0, best = 0.0;
  1112. /* look through the list of non-basic variables */
  1113. for (j = 1; j <= n; j++)
  1114. { dj = cbar[j];
  1115. switch (stat[j])
  1116. { case GLP_NL:
  1117. /* xN[j] can increase */
  1118. if (dj >= - tol_dj) continue;
  1119. break;
  1120. case GLP_NU:
  1121. /* xN[j] can decrease */
  1122. if (dj <= + tol_dj) continue;
  1123. break;
  1124. case GLP_NF:
  1125. /* xN[j] can change in any direction */
  1126. if (- tol_dj <= dj && dj <= + tol_dj) continue;
  1127. break;
  1128. case GLP_NS:
  1129. /* xN[j] cannot change at all */
  1130. continue;
  1131. default:
  1132. xassert(stat != stat);
  1133. }
  1134. /* xN[j] is eligible non-basic variable; choose one which has
  1135. largest weighted reduced cost */
  1136. #ifdef GLP_DEBUG
  1137. xassert(gamma[j] > 0.0);
  1138. #endif
  1139. temp = (dj * dj) / gamma[j];
  1140. if (best < temp)
  1141. q = j, best = temp;
  1142. }
  1143. /* store the index of non-basic variable xN[q] chosen */
  1144. csa->q = q;
  1145. return;
  1146. }
  1147. /***********************************************************************
  1148. * eval_tcol - compute pivot column of the simplex table
  1149. *
  1150. * This routine computes the pivot column of the simplex table, which
  1151. * corresponds to non-basic variable xN[q] chosen.
  1152. *
  1153. * The pivot column is the following vector:
  1154. *
  1155. * tcol = T * e[q] = - inv(B) * N * e[q] = - inv(B) * N[q],
  1156. *
  1157. * where B is the current basis matrix, N[q] is a column of the matrix
  1158. * (I|-A) corresponding to variable xN[q]. */
  1159. static void eval_tcol(struct csa *csa)
  1160. { int m = csa->m;
  1161. #ifdef GLP_DEBUG
  1162. int n = csa->n;
  1163. #endif
  1164. int *head = csa->head;
  1165. int q = csa->q;
  1166. int *tcol_ind = csa->tcol_ind;
  1167. double *tcol_vec = csa->tcol_vec;
  1168. double *h = csa->tcol_vec;
  1169. int i, k, nnz;
  1170. #ifdef GLP_DEBUG
  1171. xassert(1 <= q && q <= n);
  1172. #endif
  1173. k = head[m+q]; /* x[k] = xN[q] */
  1174. #ifdef GLP_DEBUG
  1175. xassert(1 <= k && k <= m+n);
  1176. #endif
  1177. /* construct the right-hand side vector h = - N[q] */
  1178. for (i = 1; i <= m; i++)
  1179. h[i] = 0.0;
  1180. if (k <= m)
  1181. { /* N[q] is k-th column of submatrix I */
  1182. h[k] = -1.0;
  1183. }
  1184. else
  1185. { /* N[q] is (k-m)-th column of submatrix (-A) */
  1186. int *A_ptr = csa->A_ptr;
  1187. int *A_ind = csa->A_ind;
  1188. double *A_val = csa->A_val;
  1189. int beg, end, ptr;
  1190. beg = A_ptr[k-m];
  1191. end = A_ptr[k-m+1];
  1192. for (ptr = beg; ptr < end; ptr++)
  1193. h[A_ind[ptr]] = A_val[ptr];
  1194. }
  1195. /* solve system B * tcol = h */
  1196. xassert(csa->valid);
  1197. bfd_ftran(csa->bfd, tcol_vec);
  1198. /* construct sparse pattern of the pivot column */
  1199. nnz = 0;
  1200. for (i = 1; i <= m; i++)
  1201. { if (tcol_vec[i] != 0.0)
  1202. tcol_ind[++nnz] = i;
  1203. }
  1204. csa->tcol_nnz = nnz;
  1205. return;
  1206. }
  1207. /***********************************************************************
  1208. * refine_tcol - refine pivot column of the simplex table
  1209. *
  1210. * This routine refines the pivot column of the simplex table assuming
  1211. * that it was previously computed by the routine eval_tcol. */
  1212. static void refine_tcol(struct csa *csa)
  1213. { int m = csa->m;
  1214. #ifdef GLP_DEBUG
  1215. int n = csa->n;
  1216. #endif
  1217. int *head = csa->head;
  1218. int q = csa->q;
  1219. int *tcol_ind = csa->tcol_ind;
  1220. double *tcol_vec = csa->tcol_vec;
  1221. double *h = csa->work3;
  1222. int i, k, nnz;
  1223. #ifdef GLP_DEBUG
  1224. xassert(1 <= q && q <= n);
  1225. #endif
  1226. k = head[m+q]; /* x[k] = xN[q] */
  1227. #ifdef GLP_DEBUG
  1228. xassert(1 <= k && k <= m+n);
  1229. #endif
  1230. /* construct the right-hand side vector h = - N[q] */
  1231. for (i = 1; i <= m; i++)
  1232. h[i] = 0.0;
  1233. if (k <= m)
  1234. { /* N[q] is k-th column of submatrix I */
  1235. h[k] = -1.0;
  1236. }
  1237. else
  1238. { /* N[q] is (k-m)-th column of submatrix (-A) */
  1239. int *A_ptr = csa->A_ptr;
  1240. int *A_ind = csa->A_ind;
  1241. double *A_val = csa->A_val;
  1242. int beg, end, ptr;
  1243. beg = A_ptr[k-m];
  1244. end = A_ptr[k-m+1];
  1245. for (ptr = beg; ptr < end; ptr++)
  1246. h[A_ind[ptr]] = A_val[ptr];
  1247. }
  1248. /* refine solution of B * tcol = h */
  1249. refine_ftran(csa, h, tcol_vec);
  1250. /* construct sparse pattern of the pivot column */
  1251. nnz = 0;
  1252. for (i = 1; i <= m; i++)
  1253. { if (tcol_vec[i] != 0.0)
  1254. tcol_ind[++nnz] = i;
  1255. }
  1256. csa->tcol_nnz = nnz;
  1257. return;
  1258. }
  1259. /***********************************************************************
  1260. * sort_tcol - sort pivot column of the simplex table
  1261. *
  1262. * This routine reorders the list of non-zero elements of the pivot
  1263. * column to put significant elements, whose magnitude is not less than
  1264. * a specified tolerance, in front of the list, and stores the number
  1265. * of significant elements in tcol_num. */
  1266. static void sort_tcol(struct csa *csa, double tol_piv)
  1267. {
  1268. #ifdef GLP_DEBUG
  1269. int m = csa->m;
  1270. #endif
  1271. int nnz = csa->tcol_nnz;
  1272. int *tcol_ind = csa->tcol_ind;
  1273. double *tcol_vec = csa->tcol_vec;
  1274. int i, num, pos;
  1275. double big, eps, temp;
  1276. /* compute infinity (maximum) norm of the column */
  1277. big = 0.0;
  1278. for (pos = 1; pos <= nnz; pos++)
  1279. {
  1280. #ifdef GLP_DEBUG
  1281. i = tcol_ind[pos];
  1282. xassert(1 <= i && i <= m);
  1283. #endif
  1284. temp = fabs(tcol_vec[tcol_ind[pos]]);
  1285. if (big < temp) big = temp;
  1286. }
  1287. csa->tcol_max = big;
  1288. /* determine absolute pivot tolerance */
  1289. eps = tol_piv * (1.0 + 0.01 * big);
  1290. /* move significant column components to front of the list */
  1291. for (num = 0; num < nnz; )
  1292. { i = tcol_ind[nnz];
  1293. if (fabs(tcol_vec[i]) < eps)
  1294. nnz--;
  1295. else
  1296. { num++;
  1297. tcol_ind[nnz] = tcol_ind[num];
  1298. tcol_ind[num] = i;
  1299. }
  1300. }
  1301. csa->tcol_num = num;
  1302. return;
  1303. }
  1304. /***********************************************************************
  1305. * chuzr - choose basic variable (row of the simplex table)
  1306. *
  1307. * This routine chooses basic variable xB[p], which reaches its bound
  1308. * first on changing non-basic variable xN[q] in valid direction.
  1309. *
  1310. * The parameter rtol is a relative tolerance used to relax bounds of
  1311. * basic variables. If rtol = 0, the routine implements the standard
  1312. * ratio test. Otherwise, if rtol > 0, the routine implements Harris'
  1313. * two-pass ratio test. In the latter case rtol should be about three
  1314. * times less than a tolerance used to check primal feasibility. */
  1315. static void chuzr(struct csa *csa, double rtol)
  1316. { int m = csa->m;
  1317. #ifdef GLP_DEBUG
  1318. int n = csa->n;
  1319. #endif
  1320. char *type = csa->type;
  1321. double *lb = csa->lb;
  1322. double *ub = csa->ub;
  1323. double *coef = csa->coef;
  1324. int *head = csa->head;
  1325. int phase = csa->phase;
  1326. double *bbar = csa->bbar;
  1327. double *cbar = csa->cbar;
  1328. int q = csa->q;
  1329. int *tcol_ind = csa->tcol_ind;
  1330. double *tcol_vec = csa->tcol_vec;
  1331. int tcol_num = csa->tcol_num;
  1332. int i, i_stat, k, p, p_stat, pos;
  1333. double alfa, big, delta, s, t, teta, tmax;
  1334. #ifdef GLP_DEBUG
  1335. xassert(1 <= q && q <= n);
  1336. #endif
  1337. /* s := - sign(d[q]), where d[q] is reduced cost of xN[q] */
  1338. #ifdef GLP_DEBUG
  1339. xassert(cbar[q] != 0.0);
  1340. #endif
  1341. s = (cbar[q] > 0.0 ? -1.0 : +1.0);
  1342. /*** FIRST PASS ***/
  1343. k = head[m+q]; /* x[k] = xN[q] */
  1344. #ifdef GLP_DEBUG
  1345. xassert(1 <= k && k <= m+n);
  1346. #endif
  1347. if (type[k] == GLP_DB)
  1348. { /* xN[q] has both lower and upper bounds */
  1349. p = -1, p_stat = 0, teta = ub[k] - lb[k], big = 1.0;
  1350. }
  1351. else
  1352. { /* xN[q] has no opposite bound */
  1353. p = 0, p_stat = 0, teta = DBL_MAX, big = 0.0;
  1354. }
  1355. /* walk through significant elements of the pivot column */
  1356. for (pos = 1; pos <= tcol_num; pos++)
  1357. { i = tcol_ind[pos];
  1358. #ifdef GLP_DEBUG
  1359. xassert(1 <= i && i <= m);
  1360. #endif
  1361. k = head[i]; /* x[k] = xB[i] */
  1362. #ifdef GLP_DEBUG
  1363. xassert(1 <= k && k <= m+n);
  1364. #endif
  1365. alfa = s * tcol_vec[i];
  1366. #ifdef GLP_DEBUG
  1367. xassert(alfa != 0.0);
  1368. #endif
  1369. /* xB[i] = ... + alfa * xN[q] + ..., and due to s we need to
  1370. consider the only case when xN[q] is increasing */
  1371. if (alfa > 0.0)
  1372. { /* xB[i] is increasing */
  1373. if (phase == 1 && coef[k] < 0.0)
  1374. { /* xB[i] violates its lower bound, which plays the role
  1375. of an upper bound on phase I */
  1376. delta = rtol * (1.0 + kappa * fabs(lb[k]));
  1377. t = ((lb[k] + delta) - bbar[i]) / alfa;
  1378. i_stat = GLP_NL;
  1379. }
  1380. else if (phase == 1 && coef[k] > 0.0)
  1381. { /* xB[i] violates its upper bound, which plays the role
  1382. of an lower bound on phase I */
  1383. continue;
  1384. }
  1385. else if (type[k] == GLP_UP || type[k] == GLP_DB ||
  1386. type[k] == GLP_FX)
  1387. { /* xB[i] is within its bounds and has an upper bound */
  1388. delta = rtol * (1.0 + kappa * fabs(ub[k]));
  1389. t = ((ub[k] + delta) - bbar[i]) / alfa;
  1390. i_stat = GLP_NU;
  1391. }
  1392. else
  1393. { /* xB[i] is within its bounds and has no upper bound */
  1394. continue;
  1395. }
  1396. }
  1397. else
  1398. { /* xB[i] is decreasing */
  1399. if (phase == 1 && coef[k] > 0.0)
  1400. { /* xB[i] violates its upper bound, which plays the role
  1401. of an lower bound on phase I */
  1402. delta = rtol * (1.0 + kappa * fabs(ub[k]));
  1403. t = ((ub[k] - delta) - bbar[i]) / alfa;
  1404. i_stat = GLP_NU;
  1405. }
  1406. else if (phase == 1 && coef[k] < 0.0)
  1407. { /* xB[i] violates its lower bound, which plays the role
  1408. of an upper bound on phase I */
  1409. continue;
  1410. }
  1411. else if (type[k] == GLP_LO || type[k] == GLP_DB ||
  1412. type[k] == GLP_FX)
  1413. { /* xB[i] is within its bounds and has an lower bound */
  1414. delta = rtol * (1.0 + kappa * fabs(lb[k]));
  1415. t = ((lb[k] - delta) - bbar[i]) / alfa;
  1416. i_stat = GLP_NL;
  1417. }
  1418. else
  1419. { /* xB[i] is within its bounds and has no lower bound */
  1420. continue;
  1421. }
  1422. }
  1423. /* t is a change of xN[q], on which xB[i] reaches its bound
  1424. (possibly relaxed); since the basic solution is assumed to
  1425. be primal feasible (or pseudo feasible on phase I), t has
  1426. to be non-negative by definition; however, it may happen
  1427. that xB[i] slightly (i.e. within a tolerance) violates its
  1428. bound, that leads to negative t; in the latter case, if
  1429. xB[i] is chosen, negative t means that xN[q] changes in
  1430. wrong direction; if pivot alfa[i,q] is close to zero, even
  1431. small bound violation of xB[i] may lead to a large change
  1432. of xN[q] in wrong direction; let, for example, xB[i] >= 0
  1433. and in the current basis its value be -5e-9; let also xN[q]
  1434. be on its zero bound and should increase; from the ratio
  1435. test rule it follows that the pivot alfa[i,q] < 0; however,
  1436. if alfa[i,q] is, say, -1e-9, the change of xN[q] in wrong
  1437. direction is 5e-9 / (-1e-9) = -5, and using it for updating
  1438. values of other basic variables will give absolutely wrong
  1439. results; therefore, if t is negative, we should replace it
  1440. by exact zero assuming that xB[i] is exactly on its bound,
  1441. and the violation appears due to round-off errors */
  1442. if (t < 0.0) t = 0.0;
  1443. /* apply minimal ratio test */
  1444. if (teta > t || teta == t && big < fabs(alfa))
  1445. p = i, p_stat = i_stat, teta = t, big = fabs(alfa);
  1446. }
  1447. /* the second pass is skipped in the following cases: */
  1448. /* if the standard ratio test is used */
  1449. if (rtol == 0.0) goto done;
  1450. /* if xN[q] reaches its opposite bound or if no basic variable
  1451. has been chosen on the first pass */
  1452. if (p <= 0) goto done;
  1453. /* if xB[p] is a blocking variable, i.e. if it prevents xN[q]
  1454. from any change */
  1455. if (teta == 0.0) goto done;
  1456. /*** SECOND PASS ***/
  1457. /* here tmax is a maximal change of xN[q], on which the solution
  1458. remains primal feasible (or pseudo feasible on phase I) within
  1459. a tolerance */
  1460. #if 0
  1461. tmax = (1.0 + 10.0 * DBL_EPSILON) * teta;
  1462. #else
  1463. tmax = teta;
  1464. #endif
  1465. /* nothing is chosen so far */
  1466. p = 0, p_stat = 0, teta = DBL_MAX, big = 0.0;
  1467. /* walk through significant elements of the pivot column */
  1468. for (pos = 1; pos <= tcol_num; pos++)
  1469. { i = tcol_ind[pos];
  1470. #ifdef GLP_DEBUG
  1471. xassert(1 <= i && i <= m);
  1472. #endif
  1473. k = head[i]; /* x[k] = xB[i] */
  1474. #ifdef GLP_DEBUG
  1475. xassert(1 <= k && k <= m+n);
  1476. #endif
  1477. alfa = s * tcol_vec[i];
  1478. #ifdef GLP_DEBUG
  1479. xassert(alfa != 0.0);
  1480. #endif
  1481. /* xB[i] = ... + alfa * xN[q] + ..., and due to s we need to
  1482. consider the only case when xN[q] is increasing */
  1483. if (alfa > 0.0)
  1484. { /* xB[i] is increasing */
  1485. if (phase == 1 && coef[k] < 0.0)
  1486. { /* xB[i] violates its lower bound, which plays the role
  1487. of an upper bound on phase I */
  1488. t = (lb[k] - bbar[i]) / alfa;
  1489. i_stat = GLP_NL;
  1490. }
  1491. else if (phase == 1 && coef[k] > 0.0)
  1492. { /* xB[i] violates its upper bound, which plays the role
  1493. of an lower bound on phase I */
  1494. continue;
  1495. }
  1496. else if (type[k] == GLP_UP || type[k] == GLP_DB ||
  1497. type[k] == GLP_FX)
  1498. { /* xB[i] is within its bounds and has an upper bound */
  1499. t = (ub[k] - bbar[i]) / alfa;
  1500. i_stat = GLP_NU;
  1501. }
  1502. else
  1503. { /* xB[i] is within its bounds and has no upper bound */
  1504. continue;
  1505. }
  1506. }
  1507. else
  1508. { /* xB[i] is decreasing */
  1509. if (phase == 1 && coef[k] > 0.0)
  1510. { /* xB[i] violates its upper bound, which plays the role
  1511. of an lower bound on phase I */
  1512. t = (ub[k] - bbar[i]) / alfa;
  1513. i_stat = GLP_NU;
  1514. }
  1515. else if (phase == 1 && coef[k] < 0.0)
  1516. { /* xB[i] violates its lower bound, which plays the role
  1517. of an upper bound on phase I */
  1518. continue;
  1519. }
  1520. else if (type[k] == GLP_LO || type[k] == GLP_DB ||
  1521. type[k] == GLP_FX)
  1522. { /* xB[i] is within its bounds and has an lower bound */
  1523. t = (lb[k] - bbar[i]) / alfa;
  1524. i_stat = GLP_NL;
  1525. }
  1526. else
  1527. { /* xB[i] is within its bounds and has no lower bound */
  1528. continue;
  1529. }
  1530. }
  1531. /* (see comments for the first pass) */
  1532. if (t < 0.0) t = 0.0;
  1533. /* t is a change of xN[q], on which xB[i] reaches its bound;
  1534. if t <= tmax, all basic variables can violate their bounds
  1535. only within relaxation tolerance delta; we can use this
  1536. freedom and choose basic variable having largest influence
  1537. coefficient to avoid possible numeric instability */
  1538. if (t <= tmax && big < fabs(alfa))
  1539. p = i, p_stat = i_stat, teta = t, big = fabs(alfa);
  1540. }
  1541. /* something must be chosen on the second pass */
  1542. xassert(p != 0);
  1543. done: /* store the index and status of basic variable xB[p] chosen */
  1544. csa->p = p;
  1545. if (p > 0 && type[head[p]] == GLP_FX)
  1546. csa->p_stat = GLP_NS;
  1547. else
  1548. csa->p_stat = p_stat;
  1549. /* store corresponding change of non-basic variable xN[q] */
  1550. #ifdef GLP_DEBUG
  1551. xassert(teta >= 0.0);
  1552. #endif
  1553. csa->teta = s * teta;
  1554. return;
  1555. }
  1556. /***********************************************************************
  1557. * eval_rho - compute pivot row of the inverse
  1558. *
  1559. * This routine computes the pivot (p-th) row of the inverse inv(B),
  1560. * which corresponds to basic variable xB[p] chosen:
  1561. *
  1562. * rho = inv(B') * e[p],
  1563. *
  1564. * where B' is a matrix transposed to the current basis matrix, e[p]
  1565. * is unity vector. */
  1566. static void eval_rho(struct csa *csa, double rho[])
  1567. { int m = csa->m;
  1568. int p = csa->p;
  1569. double *e = rho;
  1570. int i;
  1571. #ifdef GLP_DEBUG
  1572. xassert(1 <= p && p <= m);
  1573. #endif
  1574. /* construct the right-hand side vector e[p] */
  1575. for (i = 1; i <= m; i++)
  1576. e[i] = 0.0;
  1577. e[p] = 1.0;
  1578. /* solve system B'* rho = e[p] */
  1579. xassert(csa->valid);
  1580. bfd_btran(csa->bfd, rho);
  1581. return;
  1582. }
  1583. /***********************************************************************
  1584. * refine_rho - refine pivot row of the inverse
  1585. *
  1586. * This routine refines the pivot row of the inverse inv(B) assuming
  1587. * that it was previously computed by the routine eval_rho. */
  1588. static void refine_rho(struct csa *csa, double rho[])
  1589. { int m = csa->m;
  1590. int p = csa->p;
  1591. double *e = csa->work3;
  1592. int i;
  1593. #ifdef GLP_DEBUG
  1594. xassert(1 <= p && p <= m);
  1595. #endif
  1596. /* construct the right-hand side vector e[p] */
  1597. for (i = 1; i <= m; i++)
  1598. e[i] = 0.0;
  1599. e[p] = 1.0;
  1600. /* refine solution of B'* rho = e[p] */
  1601. refine_btran(csa, e, rho);
  1602. return;
  1603. }
  1604. /***********************************************************************
  1605. * eval_trow - compute pivot row of the simplex table
  1606. *
  1607. * This routine computes the pivot row of the simplex table, which
  1608. * corresponds to basic variable xB[p] chosen.
  1609. *
  1610. * The pivot row is the following vector:
  1611. *
  1612. * trow = T'* e[p] = - N'* inv(B') * e[p] = - N' * rho,
  1613. *
  1614. * where rho is the pivot row of the inverse inv(B) previously computed
  1615. * by the routine eval_rho.
  1616. *
  1617. * Note that elements of the pivot row corresponding to fixed non-basic
  1618. * variables are not computed. */
  1619. static void eval_trow(struct csa *csa, double rho[])
  1620. { int m = csa->m;
  1621. int n = csa->n;
  1622. #ifdef GLP_DEBUG
  1623. char *stat = csa->stat;
  1624. #endif
  1625. int *N_ptr = csa->N_ptr;
  1626. int *N_len = csa->N_len;
  1627. int *N_ind = csa->N_ind;
  1628. double *N_val = csa->N_val;
  1629. int *trow_ind = csa->trow_ind;
  1630. double *trow_vec = csa->trow_vec;
  1631. int i, j, beg, end, ptr, nnz;
  1632. double temp;
  1633. /* clear the pivot row */
  1634. for (j = 1; j <= n; j++)
  1635. trow_vec[j] = 0.0;
  1636. /* compute the pivot row as a linear combination of rows of the
  1637. matrix N: trow = - rho[1] * N'[1] - ... - rho[m] * N'[m] */
  1638. for (i = 1; i <= m; i++)
  1639. { temp = rho[i];
  1640. if (temp == 0.0) continue;
  1641. /* trow := trow - rho[i] * N'[i] */
  1642. beg = N_ptr[i];
  1643. end = beg + N_len[i];
  1644. for (ptr = beg; ptr < end; ptr++)
  1645. {
  1646. #ifdef GLP_DEBUG
  1647. j = N_ind[ptr];
  1648. xassert(1 <= j && j <= n);
  1649. xassert(stat[j] != GLP_NS);
  1650. #endif
  1651. trow_vec[N_ind[ptr]] -= temp * N_val[ptr];
  1652. }
  1653. }
  1654. /* construct sparse pattern of the pivot row */
  1655. nnz = 0;
  1656. for (j = 1; j <= n; j++)
  1657. { if (trow_vec[j] != 0.0)
  1658. trow_ind[++nnz] = j;
  1659. }
  1660. csa->trow_nnz = nnz;
  1661. return;
  1662. }
  1663. /***********************************************************************
  1664. * update_bbar - update values of basic variables
  1665. *
  1666. * This routine updates values of all basic variables for the adjacent
  1667. * basis. */
  1668. static void update_bbar(struct csa *csa)
  1669. {
  1670. #ifdef GLP_DEBUG
  1671. int m = csa->m;
  1672. int n = csa->n;
  1673. #endif
  1674. double *bbar = csa->bbar;
  1675. int q = csa->q;
  1676. int tcol_nnz = csa->tcol_nnz;
  1677. int *tcol_ind = csa->tcol_ind;
  1678. double *tcol_vec = csa->tcol_vec;
  1679. int p = csa->p;
  1680. double teta = csa->teta;
  1681. int i, pos;
  1682. #ifdef GLP_DEBUG
  1683. xassert(1 <= q && q <= n);
  1684. xassert(p < 0 || 1 <= p && p <= m);
  1685. #endif
  1686. /* if xN[q] leaves the basis, compute its value in the adjacent
  1687. basis, where it will replace xB[p] */
  1688. if (p > 0)
  1689. bbar[p] = get_xN(csa, q) + teta;
  1690. /* update values of other basic variables (except xB[p], because
  1691. it will be replaced by xN[q]) */
  1692. if (teta == 0.0) goto done;
  1693. for (pos = 1; pos <= tcol_nnz; pos++)
  1694. { i = tcol_ind[pos];
  1695. /* skip xB[p] */
  1696. if (i == p) continue;
  1697. /* (change of xB[i]) = alfa[i,q] * (change of xN[q]) */
  1698. bbar[i] += tcol_vec[i] * teta;
  1699. }
  1700. done: return;
  1701. }
  1702. /***********************************************************************
  1703. * reeval_cost - recompute reduced cost of non-basic variable xN[q]
  1704. *
  1705. * This routine recomputes reduced cost of non-basic variable xN[q] for
  1706. * the current basis more accurately using its direct definition:
  1707. *
  1708. * d[q] = cN[q] - N'[q] * pi =
  1709. *
  1710. * = cN[q] - N'[q] * (inv(B') * cB) =
  1711. *
  1712. * = cN[q] - (cB' * inv(B) * N[q]) =
  1713. *
  1714. * = cN[q] + cB' * (pivot column).
  1715. *
  1716. * It is assumed that the pivot column of the simplex table is already
  1717. * computed. */
  1718. static double reeval_cost(struct csa *csa)
  1719. { int m = csa->m;
  1720. #ifdef GLP_DEBUG
  1721. int n = csa->n;
  1722. #endif
  1723. double *coef = csa->coef;
  1724. int *head = csa->head;
  1725. int q = csa->q;
  1726. int tcol_nnz = csa->tcol_nnz;
  1727. int *tcol_ind = csa->tcol_ind;
  1728. double *tcol_vec = csa->tcol_vec;
  1729. int i, pos;
  1730. double dq;
  1731. #ifdef GLP_DEBUG
  1732. xassert(1 <= q && q <= n);
  1733. #endif
  1734. dq = coef[head[m+q]];
  1735. for (pos = 1; pos <= tcol_nnz; pos++)
  1736. { i = tcol_ind[pos];
  1737. #ifdef GLP_DEBUG
  1738. xassert(1 <= i && i <= m);
  1739. #endif
  1740. dq += coef[head[i]] * tcol_vec[i];
  1741. }
  1742. return dq;
  1743. }
  1744. /***********************************************************************
  1745. * update_cbar - update reduced costs of non-basic variables
  1746. *
  1747. * This routine updates reduced costs of all (except fixed) non-basic
  1748. * variables for the adjacent basis. */
  1749. static void update_cbar(struct csa *csa)
  1750. {
  1751. #ifdef GLP_DEBUG
  1752. int n = csa->n;
  1753. #endif
  1754. double *cbar = csa->cbar;
  1755. int q = csa->q;
  1756. int trow_nnz = csa->trow_nnz;
  1757. int *trow_ind = csa->trow_ind;
  1758. double *trow_vec = csa->trow_vec;
  1759. int j, pos;
  1760. double new_dq;
  1761. #ifdef GLP_DEBUG
  1762. xassert(1 <= q && q <= n);
  1763. #endif
  1764. /* compute reduced cost of xB[p] in the adjacent basis, where it
  1765. will replace xN[q] */
  1766. #ifdef GLP_DEBUG
  1767. xassert(trow_vec[q] != 0.0);
  1768. #endif
  1769. new_dq = (cbar[q] /= trow_vec[q]);
  1770. /* update reduced costs of other non-basic variables (except
  1771. xN[q], because it will be replaced by xB[p]) */
  1772. for (pos = 1; pos <= trow_nnz; pos++)
  1773. { j = trow_ind[pos];
  1774. /* skip xN[q] */
  1775. if (j == q) continue;
  1776. cbar[j] -= trow_vec[j] * new_dq;
  1777. }
  1778. return;
  1779. }
  1780. /***********************************************************************
  1781. * update_gamma - update steepest edge coefficients
  1782. *
  1783. * This routine updates steepest-edge coefficients for the adjacent
  1784. * basis. */
  1785. static void update_gamma(struct csa *csa)
  1786. { int m = csa->m;
  1787. #ifdef GLP_DEBUG
  1788. int n = csa->n;
  1789. #endif
  1790. char *type = csa->type;
  1791. int *A_ptr = csa->A_ptr;
  1792. int *A_ind = csa->A_ind;
  1793. double *A_val = csa->A_val;
  1794. int *head = csa->head;
  1795. char *refsp = csa->refsp;
  1796. double *gamma = csa->gamma;
  1797. int q = csa->q;
  1798. int tcol_nnz = csa->tcol_nnz;
  1799. int *tcol_ind = csa->tcol_ind;
  1800. double *tcol_vec = csa->tcol_vec;
  1801. int p = csa->p;
  1802. int trow_nnz = csa->trow_nnz;
  1803. int *trow_ind = csa->trow_ind;
  1804. double *trow_vec = csa->trow_vec;
  1805. double *u = csa->work3;
  1806. int i, j, k, pos, beg, end, ptr;
  1807. double gamma_q, delta_q, pivot, s, t, t1, t2;
  1808. #ifdef GLP_DEBUG
  1809. xassert(1 <= p && p <= m);
  1810. xassert(1 <= q && q <= n);
  1811. #endif
  1812. /* the basis changes, so decrease the count */
  1813. xassert(csa->refct > 0);
  1814. csa->refct--;
  1815. /* recompute gamma[q] for the current basis more accurately and
  1816. compute auxiliary vector u */
  1817. gamma_q = delta_q = (refsp[head[m+q]] ? 1.0 : 0.0);
  1818. for (i = 1; i <= m; i++) u[i] = 0.0;
  1819. for (pos = 1; pos <= tcol_nnz; pos++)
  1820. { i = tcol_ind[pos];
  1821. if (refsp[head[i]])
  1822. { u[i] = t = tcol_vec[i];
  1823. gamma_q += t * t;
  1824. }
  1825. else
  1826. u[i] = 0.0;
  1827. }
  1828. xassert(csa->valid);
  1829. bfd_btran(csa->bfd, u);
  1830. /* update gamma[k] for other non-basic variables (except fixed
  1831. variables and xN[q], because it will be replaced by xB[p]) */
  1832. pivot = trow_vec[q];
  1833. #ifdef GLP_DEBUG
  1834. xassert(pivot != 0.0);
  1835. #endif
  1836. for (pos = 1; pos <= trow_nnz; pos++)
  1837. { j = trow_ind[pos];
  1838. /* skip xN[q] */
  1839. if (j == q) continue;
  1840. /* compute t */
  1841. t = trow_vec[j] / pivot;
  1842. /* compute inner product s = N'[j] * u */
  1843. k = head[m+j]; /* x[k] = xN[j] */
  1844. if (k <= m)
  1845. s = u[k];
  1846. else
  1847. { s = 0.0;
  1848. beg = A_ptr[k-m];
  1849. end = A_ptr[k-m+1];
  1850. for (ptr = beg; ptr < end; ptr++)
  1851. s -= A_val[ptr] * u[A_ind[ptr]];
  1852. }
  1853. /* compute gamma[k] for the adjacent basis */
  1854. t1 = gamma[j] + t * t * gamma_q + 2.0 * t * s;
  1855. t2 = (refsp[k] ? 1.0 : 0.0) + delta_q * t * t;
  1856. gamma[j] = (t1 >= t2 ? t1 : t2);
  1857. if (gamma[j] < DBL_EPSILON) gamma[j] = DBL_EPSILON;
  1858. }
  1859. /* compute gamma[q] for the adjacent basis */
  1860. if (type[head[p]] == GLP_FX)
  1861. gamma[q] = 1.0;
  1862. else
  1863. { gamma[q] = gamma_q / (pivot * pivot);
  1864. if (gamma[q] < DBL_EPSILON) gamma[q] = DBL_EPSILON;
  1865. }
  1866. return;
  1867. }
  1868. /***********************************************************************
  1869. * err_in_bbar - compute maximal relative error in primal solution
  1870. *
  1871. * This routine returns maximal relative error:
  1872. *
  1873. * max |beta[i] - bbar[i]| / (1 + |beta[i]|),
  1874. *
  1875. * where beta and bbar are, respectively, directly computed and the
  1876. * current (updated) values of basic variables.
  1877. *
  1878. * NOTE: The routine is intended only for debugginig purposes. */
  1879. static double err_in_bbar(struct csa *csa)
  1880. { int m = csa->m;
  1881. double *bbar = csa->bbar;
  1882. int i;
  1883. double e, emax, *beta;
  1884. beta = xcalloc(1+m, sizeof(double));
  1885. eval_beta(csa, beta);
  1886. emax = 0.0;
  1887. for (i = 1; i <= m; i++)
  1888. { e = fabs(beta[i] - bbar[i]) / (1.0 + fabs(beta[i]));
  1889. if (emax < e) emax = e;
  1890. }
  1891. xfree(beta);
  1892. return emax;
  1893. }
  1894. /***********************************************************************
  1895. * err_in_cbar - compute maximal relative error in dual solution
  1896. *
  1897. * This routine returns maximal relative error:
  1898. *
  1899. * max |cost[j] - cbar[j]| / (1 + |cost[j]|),
  1900. *
  1901. * where cost and cbar are, respectively, directly computed and the
  1902. * current (updated) reduced costs of non-basic non-fixed variables.
  1903. *
  1904. * NOTE: The routine is intended only for debugginig purposes. */
  1905. static double err_in_cbar(struct csa *csa)
  1906. { int m = csa->m;
  1907. int n = csa->n;
  1908. char *stat = csa->stat;
  1909. double *cbar = csa->cbar;
  1910. int j;
  1911. double e, emax, cost, *pi;
  1912. pi = xcalloc(1+m, sizeof(double));
  1913. eval_pi(csa, pi);
  1914. emax = 0.0;
  1915. for (j = 1; j <= n; j++)
  1916. { if (stat[j] == GLP_NS) continue;
  1917. cost = eval_cost(csa, pi, j);
  1918. e = fabs(cost - cbar[j]) / (1.0 + fabs(cost));
  1919. if (emax < e) emax = e;
  1920. }
  1921. xfree(pi);
  1922. return emax;
  1923. }
  1924. /***********************************************************************
  1925. * err_in_gamma - compute maximal relative error in steepest edge cff.
  1926. *
  1927. * This routine returns maximal relative error:
  1928. *
  1929. * max |gamma'[j] - gamma[j]| / (1 + |gamma'[j]),
  1930. *
  1931. * where gamma'[j] and gamma[j] are, respectively, directly computed
  1932. * and the current (updated) steepest edge coefficients for non-basic
  1933. * non-fixed variable x[j].
  1934. *
  1935. * NOTE: The routine is intended only for debugginig purposes. */
  1936. static double err_in_gamma(struct csa *csa)
  1937. { int n = csa->n;
  1938. char *stat = csa->stat;
  1939. double *gamma = csa->gamma;
  1940. int j;
  1941. double e, emax, temp;
  1942. emax = 0.0;
  1943. for (j = 1; j <= n; j++)
  1944. { if (stat[j] == GLP_NS)
  1945. { xassert(gamma[j] == 1.0);
  1946. continue;
  1947. }
  1948. temp = eval_gamma(csa, j);
  1949. e = fabs(temp - gamma[j]) / (1.0 + fabs(temp));
  1950. if (emax < e) emax = e;
  1951. }
  1952. return emax;
  1953. }
  1954. /***********************************************************************
  1955. * change_basis - change basis header
  1956. *
  1957. * This routine changes the basis header to make it corresponding to
  1958. * the adjacent basis. */
  1959. static void change_basis(struct csa *csa)
  1960. { int m = csa->m;
  1961. #ifdef GLP_DEBUG
  1962. int n = csa->n;
  1963. char *type = csa->type;
  1964. #endif
  1965. int *head = csa->head;
  1966. char *stat = csa->stat;
  1967. int q = csa->q;
  1968. int p = csa->p;
  1969. int p_stat = csa->p_stat;
  1970. int k;
  1971. #ifdef GLP_DEBUG
  1972. xassert(1 <= q && q <= n);
  1973. #endif
  1974. if (p < 0)
  1975. { /* xN[q] goes to its opposite bound */
  1976. #ifdef GLP_DEBUG
  1977. k = head[m+q]; /* x[k] = xN[q] */
  1978. xassert(1 <= k && k <= m+n);
  1979. xassert(type[k] == GLP_DB);
  1980. #endif
  1981. switch (stat[q])
  1982. { case GLP_NL:
  1983. /* xN[q] increases */
  1984. stat[q] = GLP_NU;
  1985. break;
  1986. case GLP_NU:
  1987. /* xN[q] decreases */
  1988. stat[q] = GLP_NL;
  1989. break;
  1990. default:
  1991. xassert(stat != stat);
  1992. }
  1993. }
  1994. else
  1995. { /* xB[p] leaves the basis, xN[q] enters the basis */
  1996. #ifdef GLP_DEBUG
  1997. xassert(1 <= p && p <= m);
  1998. k = head[p]; /* x[k] = xB[p] */
  1999. switch (p_stat)
  2000. { case GLP_NL:
  2001. /* xB[p] goes to its lower bound */
  2002. xassert(type[k] == GLP_LO || type[k] == GLP_DB);
  2003. break;
  2004. case GLP_NU:
  2005. /* xB[p] goes to its upper bound */
  2006. xassert(type[k] == GLP_UP || type[k] == GLP_DB);
  2007. break;
  2008. case GLP_NS:
  2009. /* xB[p] goes to its fixed value */
  2010. xassert(type[k] == GLP_NS);
  2011. break;
  2012. default:
  2013. xassert(p_stat != p_stat);
  2014. }
  2015. #endif
  2016. /* xB[p] <-> xN[q] */
  2017. k = head[p], head[p] = head[m+q], head[m+q] = k;
  2018. stat[q] = (char)p_stat;
  2019. }
  2020. return;
  2021. }
  2022. /***********************************************************************
  2023. * set_aux_obj - construct auxiliary objective function
  2024. *
  2025. * The auxiliary objective function is a separable piecewise linear
  2026. * convex function, which is the sum of primal infeasibilities:
  2027. *
  2028. * z = t[1] + ... + t[m+n] -> minimize,
  2029. *
  2030. * where:
  2031. *
  2032. * / lb[k] - x[k], if x[k] < lb[k]
  2033. * |
  2034. * t[k] = < 0, if lb[k] <= x[k] <= ub[k]
  2035. * |
  2036. * \ x[k] - ub[k], if x[k] > ub[k]
  2037. *
  2038. * This routine computes objective coefficients for the current basis
  2039. * and returns the number of non-zero terms t[k]. */
  2040. static int set_aux_obj(struct csa *csa, double tol_bnd)
  2041. { int m = csa->m;
  2042. int n = csa->n;
  2043. char *type = csa->type;
  2044. double *lb = csa->lb;
  2045. double *ub = csa->ub;
  2046. double *coef = csa->coef;
  2047. int *head = csa->head;
  2048. double *bbar = csa->bbar;
  2049. int i, k, cnt = 0;
  2050. double eps;
  2051. /* use a bit more restrictive tolerance */
  2052. tol_bnd *= 0.90;
  2053. /* clear all objective coefficients */
  2054. for (k = 1; k <= m+n; k++)
  2055. coef[k] = 0.0;
  2056. /* walk through the list of basic variables */
  2057. for (i = 1; i <= m; i++)
  2058. { k = head[i]; /* x[k] = xB[i] */
  2059. if (type[k] == GLP_LO || type[k] == GLP_DB ||
  2060. type[k] == GLP_FX)
  2061. { /* x[k] has lower bound */
  2062. eps = tol_bnd * (1.0 + kappa * fabs(lb[k]));
  2063. if (bbar[i] < lb[k] - eps)
  2064. { /* and violates it */
  2065. coef[k] = -1.0;
  2066. cnt++;
  2067. }
  2068. }
  2069. if (type[k] == GLP_UP || type[k] == GLP_DB ||
  2070. type[k] == GLP_FX)
  2071. { /* x[k] has upper bound */
  2072. eps = tol_bnd * (1.0 + kappa * fabs(ub[k]));
  2073. if (bbar[i] > ub[k] + eps)
  2074. { /* and violates it */
  2075. coef[k] = +1.0;
  2076. cnt++;
  2077. }
  2078. }
  2079. }
  2080. return cnt;
  2081. }
  2082. /***********************************************************************
  2083. * set_orig_obj - restore original objective function
  2084. *
  2085. * This routine assigns scaled original objective coefficients to the
  2086. * working objective function. */
  2087. static void set_orig_obj(struct csa *csa)
  2088. { int m = csa->m;
  2089. int n = csa->n;
  2090. double *coef = csa->coef;
  2091. double *obj = csa->obj;
  2092. double zeta = csa->zeta;
  2093. int i, j;
  2094. for (i = 1; i <= m; i++)
  2095. coef[i] = 0.0;
  2096. for (j = 1; j <= n; j++)
  2097. coef[m+j] = zeta * obj[j];
  2098. return;
  2099. }
  2100. /***********************************************************************
  2101. * check_stab - check numerical stability of basic solution
  2102. *
  2103. * If the current basic solution is primal feasible (or pseudo feasible
  2104. * on phase I) within a tolerance, this routine returns zero, otherwise
  2105. * it returns non-zero. */
  2106. static int check_stab(struct csa *csa, double tol_bnd)
  2107. { int m = csa->m;
  2108. #ifdef GLP_DEBUG
  2109. int n = csa->n;
  2110. #endif
  2111. char *type = csa->type;
  2112. double *lb = csa->lb;
  2113. double *ub = csa->ub;
  2114. double *coef = csa->coef;
  2115. int *head = csa->head;
  2116. int phase = csa->phase;
  2117. double *bbar = csa->bbar;
  2118. int i, k;
  2119. double eps;
  2120. /* walk through the list of basic variables */
  2121. for (i = 1; i <= m; i++)
  2122. { k = head[i]; /* x[k] = xB[i] */
  2123. #ifdef GLP_DEBUG
  2124. xassert(1 <= k && k <= m+n);
  2125. #endif
  2126. if (phase == 1 && coef[k] < 0.0)
  2127. { /* x[k] must not be greater than its lower bound */
  2128. #ifdef GLP_DEBUG
  2129. xassert(type[k] == GLP_LO || type[k] == GLP_DB ||
  2130. type[k] == GLP_FX);
  2131. #endif
  2132. eps = tol_bnd * (1.0 + kappa * fabs(lb[k]));
  2133. if (bbar[i] > lb[k] + eps) return 1;
  2134. }
  2135. else if (phase == 1 && coef[k] > 0.0)
  2136. { /* x[k] must not be less than its upper bound */
  2137. #ifdef GLP_DEBUG
  2138. xassert(type[k] == GLP_UP || type[k] == GLP_DB ||
  2139. type[k] == GLP_FX);
  2140. #endif
  2141. eps = tol_bnd * (1.0 + kappa * fabs(ub[k]));
  2142. if (bbar[i] < ub[k] - eps) return 1;
  2143. }
  2144. else
  2145. { /* either phase = 1 and coef[k] = 0, or phase = 2 */
  2146. if (type[k] == GLP_LO || type[k] == GLP_DB ||
  2147. type[k] == GLP_FX)
  2148. { /* x[k] must not be less than its lower bound */
  2149. eps = tol_bnd * (1.0 + kappa * fabs(lb[k]));
  2150. if (bbar[i] < lb[k] - eps) return 1;
  2151. }
  2152. if (type[k] == GLP_UP || type[k] == GLP_DB ||
  2153. type[k] == GLP_FX)
  2154. { /* x[k] must not be greater then its upper bound */
  2155. eps = tol_bnd * (1.0 + kappa * fabs(ub[k]));
  2156. if (bbar[i] > ub[k] + eps) return 1;
  2157. }
  2158. }
  2159. }
  2160. /* basic solution is primal feasible within a tolerance */
  2161. return 0;
  2162. }
  2163. /***********************************************************************
  2164. * check_feas - check primal feasibility of basic solution
  2165. *
  2166. * If the current basic solution is primal feasible within a tolerance,
  2167. * this routine returns zero, otherwise it returns non-zero. */
  2168. static int check_feas(struct csa *csa, double tol_bnd)
  2169. { int m = csa->m;
  2170. #ifdef GLP_DEBUG
  2171. int n = csa->n;
  2172. char *type = csa->type;
  2173. #endif
  2174. double *lb = csa->lb;
  2175. double *ub = csa->ub;
  2176. double *coef = csa->coef;
  2177. int *head = csa->head;
  2178. double *bbar = csa->bbar;
  2179. int i, k;
  2180. double eps;
  2181. xassert(csa->phase == 1);
  2182. /* walk through the list of basic variables */
  2183. for (i = 1; i <= m; i++)
  2184. { k = head[i]; /* x[k] = xB[i] */
  2185. #ifdef GLP_DEBUG
  2186. xassert(1 <= k && k <= m+n);
  2187. #endif
  2188. if (coef[k] < 0.0)
  2189. { /* check if x[k] still violates its lower bound */
  2190. #ifdef GLP_DEBUG
  2191. xassert(type[k] == GLP_LO || type[k] == GLP_DB ||
  2192. type[k] == GLP_FX);
  2193. #endif
  2194. eps = tol_bnd * (1.0 + kappa * fabs(lb[k]));
  2195. if (bbar[i] < lb[k] - eps) return 1;
  2196. }
  2197. else if (coef[k] > 0.0)
  2198. { /* check if x[k] still violates its upper bound */
  2199. #ifdef GLP_DEBUG
  2200. xassert(type[k] == GLP_UP || type[k] == GLP_DB ||
  2201. type[k] == GLP_FX);
  2202. #endif
  2203. eps = tol_bnd * (1.0 + kappa * fabs(ub[k]));
  2204. if (bbar[i] > ub[k] + eps) return 1;
  2205. }
  2206. }
  2207. /* basic solution is primal feasible within a tolerance */
  2208. return 0;
  2209. }
  2210. /***********************************************************************
  2211. * eval_obj - compute original objective function
  2212. *
  2213. * This routine computes the current value of the original objective
  2214. * function. */
  2215. static double eval_obj(struct csa *csa)
  2216. { int m = csa->m;
  2217. int n = csa->n;
  2218. double *obj = csa->obj;
  2219. int *head = csa->head;
  2220. double *bbar = csa->bbar;
  2221. int i, j, k;
  2222. double sum;
  2223. sum = obj[0];
  2224. /* walk through the list of basic variables */
  2225. for (i = 1; i <= m; i++)
  2226. { k = head[i]; /* x[k] = xB[i] */
  2227. #ifdef GLP_DEBUG
  2228. xassert(1 <= k && k <= m+n);
  2229. #endif
  2230. if (k > m)
  2231. sum += obj[k-m] * bbar[i];
  2232. }
  2233. /* walk through the list of non-basic variables */
  2234. for (j = 1; j <= n; j++)
  2235. { k = head[m+j]; /* x[k] = xN[j] */
  2236. #ifdef GLP_DEBUG
  2237. xassert(1 <= k && k <= m+n);
  2238. #endif
  2239. if (k > m)
  2240. sum += obj[k-m] * get_xN(csa, j);
  2241. }
  2242. return sum;
  2243. }
  2244. /***********************************************************************
  2245. * display - display the search progress
  2246. *
  2247. * This routine displays some information about the search progress
  2248. * that includes:
  2249. *
  2250. * the search phase;
  2251. *
  2252. * the number of simplex iterations performed by the solver;
  2253. *
  2254. * the original objective value;
  2255. *
  2256. * the sum of (scaled) primal infeasibilities;
  2257. *
  2258. * the number of basic fixed variables. */
  2259. static void display(struct csa *csa, const glp_smcp *parm, int spec)
  2260. { int m = csa->m;
  2261. #ifdef GLP_DEBUG
  2262. int n = csa->n;
  2263. #endif
  2264. char *type = csa->type;
  2265. double *lb = csa->lb;
  2266. double *ub = csa->ub;
  2267. int phase = csa->phase;
  2268. int *head = csa->head;
  2269. double *bbar = csa->bbar;
  2270. int i, k, cnt;
  2271. double sum;
  2272. if (parm->msg_lev < GLP_MSG_ON) goto skip;
  2273. if (parm->out_dly > 0 &&
  2274. 1000.0 * xdifftime(xtime(), csa->tm_beg) < parm->out_dly)
  2275. goto skip;
  2276. if (csa->it_cnt == csa->it_dpy) goto skip;
  2277. if (!spec && csa->it_cnt % parm->out_frq != 0) goto skip;
  2278. /* compute the sum of primal infeasibilities and determine the
  2279. number of basic fixed variables */
  2280. sum = 0.0, cnt = 0;
  2281. for (i = 1; i <= m; i++)
  2282. { k = head[i]; /* x[k] = xB[i] */
  2283. #ifdef GLP_DEBUG
  2284. xassert(1 <= k && k <= m+n);
  2285. #endif
  2286. if (type[k] == GLP_LO || type[k] == GLP_DB ||
  2287. type[k] == GLP_FX)
  2288. { /* x[k] has lower bound */
  2289. if (bbar[i] < lb[k])
  2290. sum += (lb[k] - bbar[i]);
  2291. }
  2292. if (type[k] == GLP_UP || type[k] == GLP_DB ||
  2293. type[k] == GLP_FX)
  2294. { /* x[k] has upper bound */
  2295. if (bbar[i] > ub[k])
  2296. sum += (bbar[i] - ub[k]);
  2297. }
  2298. if (type[k] == GLP_FX) cnt++;
  2299. }
  2300. xprintf("%c%6d: obj = %17.9e infeas = %10.3e (%d)\n",
  2301. phase == 1 ? ' ' : '*', csa->it_cnt, eval_obj(csa), sum, cnt);
  2302. csa->it_dpy = csa->it_cnt;
  2303. skip: return;
  2304. }
  2305. /***********************************************************************
  2306. * store_sol - store basic solution back to the problem object
  2307. *
  2308. * This routine stores basic solution components back to the problem
  2309. * object. */
  2310. static void store_sol(struct csa *csa, glp_prob *lp, int p_stat,
  2311. int d_stat, int ray)
  2312. { int m = csa->m;
  2313. int n = csa->n;
  2314. double zeta = csa->zeta;
  2315. int *head = csa->head;
  2316. char *stat = csa->stat;
  2317. double *bbar = csa->bbar;
  2318. double *cbar = csa->cbar;
  2319. int i, j, k;
  2320. #ifdef GLP_DEBUG
  2321. xassert(lp->m == m);
  2322. xassert(lp->n == n);
  2323. #endif
  2324. /* basis factorization */
  2325. #ifdef GLP_DEBUG
  2326. xassert(!lp->valid && lp->bfd == NULL);
  2327. xassert(csa->valid && csa->bfd != NULL);
  2328. #endif
  2329. lp->valid = 1, csa->valid = 0;
  2330. lp->bfd = csa->bfd, csa->bfd = NULL;
  2331. memcpy(&lp->head[1], &head[1], m * sizeof(int));
  2332. /* basic solution status */
  2333. lp->pbs_stat = p_stat;
  2334. lp->dbs_stat = d_stat;
  2335. /* objective function value */
  2336. lp->obj_val = eval_obj(csa);
  2337. /* simplex iteration count */
  2338. lp->it_cnt = csa->it_cnt;
  2339. /* unbounded ray */
  2340. lp->some = ray;
  2341. /* basic variables */
  2342. for (i = 1; i <= m; i++)
  2343. { k = head[i]; /* x[k] = xB[i] */
  2344. #ifdef GLP_DEBUG
  2345. xassert(1 <= k && k <= m+n);
  2346. #endif
  2347. if (k <= m)
  2348. { GLPROW *row = lp->row[k];
  2349. row->stat = GLP_BS;
  2350. row->bind = i;
  2351. row->prim = bbar[i] / row->rii;
  2352. row->dual = 0.0;
  2353. }
  2354. else
  2355. { GLPCOL *col = lp->col[k-m];
  2356. col->stat = GLP_BS;
  2357. col->bind = i;
  2358. col->prim = bbar[i] * col->sjj;
  2359. col->dual = 0.0;
  2360. }
  2361. }
  2362. /* non-basic variables */
  2363. for (j = 1; j <= n; j++)
  2364. { k = head[m+j]; /* x[k] = xN[j] */
  2365. #ifdef GLP_DEBUG
  2366. xassert(1 <= k && k <= m+n);
  2367. #endif
  2368. if (k <= m)
  2369. { GLPROW *row = lp->row[k];
  2370. row->stat = stat[j];
  2371. row->bind = 0;
  2372. #if 0
  2373. row->prim = get_xN(csa, j) / row->rii;
  2374. #else
  2375. switch (stat[j])
  2376. { case GLP_NL:
  2377. row->prim = row->lb; break;
  2378. case GLP_NU:
  2379. row->prim = row->ub; break;
  2380. case GLP_NF:
  2381. row->prim = 0.0; break;
  2382. case GLP_NS:
  2383. row->prim = row->lb; break;
  2384. default:
  2385. xassert(stat != stat);
  2386. }
  2387. #endif
  2388. row->dual = (cbar[j] * row->rii) / zeta;
  2389. }
  2390. else
  2391. { GLPCOL *col = lp->col[k-m];
  2392. col->stat = stat[j];
  2393. col->bind = 0;
  2394. #if 0
  2395. col->prim = get_xN(csa, j) * col->sjj;
  2396. #else
  2397. switch (stat[j])
  2398. { case GLP_NL:
  2399. col->prim = col->lb; break;
  2400. case GLP_NU:
  2401. col->prim = col->ub; break;
  2402. case GLP_NF:
  2403. col->prim = 0.0; break;
  2404. case GLP_NS:
  2405. col->prim = col->lb; break;
  2406. default:
  2407. xassert(stat != stat);
  2408. }
  2409. #endif
  2410. col->dual = (cbar[j] / col->sjj) / zeta;
  2411. }
  2412. }
  2413. return;
  2414. }
  2415. /***********************************************************************
  2416. * free_csa - deallocate common storage area
  2417. *
  2418. * This routine frees all the memory allocated to arrays in the common
  2419. * storage area (CSA). */
  2420. static void free_csa(struct csa *csa)
  2421. { xfree(csa->type);
  2422. xfree(csa->lb);
  2423. xfree(csa->ub);
  2424. xfree(csa->coef);
  2425. xfree(csa->obj);
  2426. xfree(csa->A_ptr);
  2427. xfree(csa->A_ind);
  2428. xfree(csa->A_val);
  2429. xfree(csa->head);
  2430. xfree(csa->stat);
  2431. xfree(csa->N_ptr);
  2432. xfree(csa->N_len);
  2433. xfree(csa->N_ind);
  2434. xfree(csa->N_val);
  2435. xfree(csa->bbar);
  2436. xfree(csa->cbar);
  2437. xfree(csa->refsp);
  2438. xfree(csa->gamma);
  2439. xfree(csa->tcol_ind);
  2440. xfree(csa->tcol_vec);
  2441. xfree(csa->trow_ind);
  2442. xfree(csa->trow_vec);
  2443. xfree(csa->work1);
  2444. xfree(csa->work2);
  2445. xfree(csa->work3);
  2446. xfree(csa->work4);
  2447. xfree(csa);
  2448. return;
  2449. }
  2450. /***********************************************************************
  2451. * spx_primal - core LP solver based on the primal simplex method
  2452. *
  2453. * SYNOPSIS
  2454. *
  2455. * #include "glpspx.h"
  2456. * int spx_primal(glp_prob *lp, const glp_smcp *parm);
  2457. *
  2458. * DESCRIPTION
  2459. *
  2460. * The routine spx_primal is a core LP solver based on the two-phase
  2461. * primal simplex method.
  2462. *
  2463. * RETURNS
  2464. *
  2465. * 0 LP instance has been successfully solved.
  2466. *
  2467. * GLP_EITLIM
  2468. * Iteration limit has been exhausted.
  2469. *
  2470. * GLP_ETMLIM
  2471. * Time limit has been exhausted.
  2472. *
  2473. * GLP_EFAIL
  2474. * The solver failed to solve LP instance. */
  2475. int spx_primal(glp_prob *lp, const glp_smcp *parm)
  2476. { struct csa *csa;
  2477. int binv_st = 2;
  2478. /* status of basis matrix factorization:
  2479. 0 - invalid; 1 - just computed; 2 - updated */
  2480. int bbar_st = 0;
  2481. /* status of primal values of basic variables:
  2482. 0 - invalid; 1 - just computed; 2 - updated */
  2483. int cbar_st = 0;
  2484. /* status of reduced costs of non-basic variables:
  2485. 0 - invalid; 1 - just computed; 2 - updated */
  2486. int rigorous = 0;
  2487. /* rigorous mode flag; this flag is used to enable iterative
  2488. refinement on computing pivot rows and columns of the simplex
  2489. table */
  2490. int check = 0;
  2491. int p_stat, d_stat, ret;
  2492. /* allocate and initialize the common storage area */
  2493. csa = alloc_csa(lp);
  2494. init_csa(csa, lp);
  2495. if (parm->msg_lev >= GLP_MSG_DBG)
  2496. xprintf("Objective scale factor = %g\n", csa->zeta);
  2497. loop: /* main loop starts here */
  2498. /* compute factorization of the basis matrix */
  2499. if (binv_st == 0)
  2500. { ret = invert_B(csa);
  2501. if (ret != 0)
  2502. { if (parm->msg_lev >= GLP_MSG_ERR)
  2503. { xprintf("Error: unable to factorize the basis matrix (%d"
  2504. ")\n", ret);
  2505. xprintf("Sorry, basis recovery procedure not implemented"
  2506. " yet\n");
  2507. }
  2508. xassert(!lp->valid && lp->bfd == NULL);
  2509. lp->bfd = csa->bfd, csa->bfd = NULL;
  2510. lp->pbs_stat = lp->dbs_stat = GLP_UNDEF;
  2511. lp->obj_val = 0.0;
  2512. lp->it_cnt = csa->it_cnt;
  2513. lp->some = 0;
  2514. ret = GLP_EFAIL;
  2515. goto done;
  2516. }
  2517. csa->valid = 1;
  2518. binv_st = 1; /* just computed */
  2519. /* invalidate basic solution components */
  2520. bbar_st = cbar_st = 0;
  2521. }
  2522. /* compute primal values of basic variables */
  2523. if (bbar_st == 0)
  2524. { eval_bbar(csa);
  2525. bbar_st = 1; /* just computed */
  2526. /* determine the search phase, if not determined yet */
  2527. if (csa->phase == 0)
  2528. { if (set_aux_obj(csa, parm->tol_bnd) > 0)
  2529. { /* current basic solution is primal infeasible */
  2530. /* start to minimize the sum of infeasibilities */
  2531. csa->phase = 1;
  2532. }
  2533. else
  2534. { /* current basic solution is primal feasible */
  2535. /* start to minimize the original objective function */
  2536. set_orig_obj(csa);
  2537. csa->phase = 2;
  2538. }
  2539. xassert(check_stab(csa, parm->tol_bnd) == 0);
  2540. /* working objective coefficients have been changed, so
  2541. invalidate reduced costs */
  2542. cbar_st = 0;
  2543. display(csa, parm, 1);
  2544. }
  2545. /* make sure that the current basic solution remains primal
  2546. feasible (or pseudo feasible on phase I) */
  2547. if (check_stab(csa, parm->tol_bnd))
  2548. { /* there are excessive bound violations due to round-off
  2549. errors */
  2550. if (parm->msg_lev >= GLP_MSG_ERR)
  2551. xprintf("Warning: numerical instability (primal simplex,"
  2552. " phase %s)\n", csa->phase == 1 ? "I" : "II");
  2553. /* restart the search */
  2554. csa->phase = 0;
  2555. binv_st = 0;
  2556. rigorous = 5;
  2557. goto loop;
  2558. }
  2559. }
  2560. xassert(csa->phase == 1 || csa->phase == 2);
  2561. /* on phase I we do not need to wait until the current basic
  2562. solution becomes dual feasible; it is sufficient to make sure
  2563. that no basic variable violates its bounds */
  2564. if (csa->phase == 1 && !check_feas(csa, parm->tol_bnd))
  2565. { /* the current basis is primal feasible; switch to phase II */
  2566. csa->phase = 2;
  2567. set_orig_obj(csa);
  2568. cbar_st = 0;
  2569. display(csa, parm, 1);
  2570. }
  2571. /* compute reduced costs of non-basic variables */
  2572. if (cbar_st == 0)
  2573. { eval_cbar(csa);
  2574. cbar_st = 1; /* just computed */
  2575. }
  2576. /* redefine the reference space, if required */
  2577. switch (parm->pricing)
  2578. { case GLP_PT_STD:
  2579. break;
  2580. case GLP_PT_PSE:
  2581. if (csa->refct == 0) reset_refsp(csa);
  2582. break;
  2583. default:
  2584. xassert(parm != parm);
  2585. }
  2586. /* at this point the basis factorization and all basic solution
  2587. components are valid */
  2588. xassert(binv_st && bbar_st && cbar_st);
  2589. /* check accuracy of current basic solution components (only for
  2590. debugging) */
  2591. if (check)
  2592. { double e_bbar = err_in_bbar(csa);
  2593. double e_cbar = err_in_cbar(csa);
  2594. double e_gamma =
  2595. (parm->pricing == GLP_PT_PSE ? err_in_gamma(csa) : 0.0);
  2596. xprintf("e_bbar = %10.3e; e_cbar = %10.3e; e_gamma = %10.3e\n",
  2597. e_bbar, e_cbar, e_gamma);
  2598. xassert(e_bbar <= 1e-5 && e_cbar <= 1e-5 && e_gamma <= 1e-3);
  2599. }
  2600. /* check if the iteration limit has been exhausted */
  2601. if (parm->it_lim < INT_MAX &&
  2602. csa->it_cnt - csa->it_beg >= parm->it_lim)
  2603. { if (bbar_st != 1 || csa->phase == 2 && cbar_st != 1)
  2604. { if (bbar_st != 1) bbar_st = 0;
  2605. if (csa->phase == 2 && cbar_st != 1) cbar_st = 0;
  2606. goto loop;
  2607. }
  2608. display(csa, parm, 1);
  2609. if (parm->msg_lev >= GLP_MSG_ALL)
  2610. xprintf("ITERATION LIMIT EXCEEDED; SEARCH TERMINATED\n");
  2611. switch (csa->phase)
  2612. { case 1:
  2613. p_stat = GLP_INFEAS;
  2614. set_orig_obj(csa);
  2615. eval_cbar(csa);
  2616. break;
  2617. case 2:
  2618. p_stat = GLP_FEAS;
  2619. break;
  2620. default:
  2621. xassert(csa != csa);
  2622. }
  2623. chuzc(csa, parm->tol_dj);
  2624. d_stat = (csa->q == 0 ? GLP_FEAS : GLP_INFEAS);
  2625. store_sol(csa, lp, p_stat, d_stat, 0);
  2626. ret = GLP_EITLIM;
  2627. goto done;
  2628. }
  2629. /* check if the time limit has been exhausted */
  2630. if (parm->tm_lim < INT_MAX &&
  2631. 1000.0 * xdifftime(xtime(), csa->tm_beg) >= parm->tm_lim)
  2632. { if (bbar_st != 1 || csa->phase == 2 && cbar_st != 1)
  2633. { if (bbar_st != 1) bbar_st = 0;
  2634. if (csa->phase == 2 && cbar_st != 1) cbar_st = 0;
  2635. goto loop;
  2636. }
  2637. display(csa, parm, 1);
  2638. if (parm->msg_lev >= GLP_MSG_ALL)
  2639. xprintf("TIME LIMIT EXCEEDED; SEARCH TERMINATED\n");
  2640. switch (csa->phase)
  2641. { case 1:
  2642. p_stat = GLP_INFEAS;
  2643. set_orig_obj(csa);
  2644. eval_cbar(csa);
  2645. break;
  2646. case 2:
  2647. p_stat = GLP_FEAS;
  2648. break;
  2649. default:
  2650. xassert(csa != csa);
  2651. }
  2652. chuzc(csa, parm->tol_dj);
  2653. d_stat = (csa->q == 0 ? GLP_FEAS : GLP_INFEAS);
  2654. store_sol(csa, lp, p_stat, d_stat, 0);
  2655. ret = GLP_ETMLIM;
  2656. goto done;
  2657. }
  2658. /* display the search progress */
  2659. display(csa, parm, 0);
  2660. /* choose non-basic variable xN[q] */
  2661. chuzc(csa, parm->tol_dj);
  2662. if (csa->q == 0)
  2663. { if (bbar_st != 1 || cbar_st != 1)
  2664. { if (bbar_st != 1) bbar_st = 0;
  2665. if (cbar_st != 1) cbar_st = 0;
  2666. goto loop;
  2667. }
  2668. display(csa, parm, 1);
  2669. switch (csa->phase)
  2670. { case 1:
  2671. if (parm->msg_lev >= GLP_MSG_ALL)
  2672. xprintf("PROBLEM HAS NO FEASIBLE SOLUTION\n");
  2673. p_stat = GLP_NOFEAS;
  2674. set_orig_obj(csa);
  2675. eval_cbar(csa);
  2676. chuzc(csa, parm->tol_dj);
  2677. d_stat = (csa->q == 0 ? GLP_FEAS : GLP_INFEAS);
  2678. break;
  2679. case 2:
  2680. if (parm->msg_lev >= GLP_MSG_ALL)
  2681. xprintf("OPTIMAL SOLUTION FOUND\n");
  2682. p_stat = d_stat = GLP_FEAS;
  2683. break;
  2684. default:
  2685. xassert(csa != csa);
  2686. }
  2687. store_sol(csa, lp, p_stat, d_stat, 0);
  2688. ret = 0;
  2689. goto done;
  2690. }
  2691. /* compute pivot column of the simplex table */
  2692. eval_tcol(csa);
  2693. if (rigorous) refine_tcol(csa);
  2694. sort_tcol(csa, parm->tol_piv);
  2695. /* check accuracy of the reduced cost of xN[q] */
  2696. { double d1 = csa->cbar[csa->q]; /* less accurate */
  2697. double d2 = reeval_cost(csa); /* more accurate */
  2698. xassert(d1 != 0.0);
  2699. if (fabs(d1 - d2) > 1e-5 * (1.0 + fabs(d2)) ||
  2700. !(d1 < 0.0 && d2 < 0.0 || d1 > 0.0 && d2 > 0.0))
  2701. { if (parm->msg_lev >= GLP_MSG_DBG)
  2702. xprintf("d1 = %.12g; d2 = %.12g\n", d1, d2);
  2703. if (cbar_st != 1 || !rigorous)
  2704. { if (cbar_st != 1) cbar_st = 0;
  2705. rigorous = 5;
  2706. goto loop;
  2707. }
  2708. }
  2709. /* replace cbar[q] by more accurate value keeping its sign */
  2710. if (d1 > 0.0)
  2711. csa->cbar[csa->q] = (d2 > 0.0 ? d2 : +DBL_EPSILON);
  2712. else
  2713. csa->cbar[csa->q] = (d2 < 0.0 ? d2 : -DBL_EPSILON);
  2714. }
  2715. /* choose basic variable xB[p] */
  2716. switch (parm->r_test)
  2717. { case GLP_RT_STD:
  2718. chuzr(csa, 0.0);
  2719. break;
  2720. case GLP_RT_HAR:
  2721. chuzr(csa, 0.30 * parm->tol_bnd);
  2722. break;
  2723. default:
  2724. xassert(parm != parm);
  2725. }
  2726. if (csa->p == 0)
  2727. { if (bbar_st != 1 || cbar_st != 1 || !rigorous)
  2728. { if (bbar_st != 1) bbar_st = 0;
  2729. if (cbar_st != 1) cbar_st = 0;
  2730. rigorous = 1;
  2731. goto loop;
  2732. }
  2733. display(csa, parm, 1);
  2734. switch (csa->phase)
  2735. { case 1:
  2736. if (parm->msg_lev >= GLP_MSG_ERR)
  2737. xprintf("Error: unable to choose basic variable on ph"
  2738. "ase I\n");
  2739. xassert(!lp->valid && lp->bfd == NULL);
  2740. lp->bfd = csa->bfd, csa->bfd = NULL;
  2741. lp->pbs_stat = lp->dbs_stat = GLP_UNDEF;
  2742. lp->obj_val = 0.0;
  2743. lp->it_cnt = csa->it_cnt;
  2744. lp->some = 0;
  2745. ret = GLP_EFAIL;
  2746. break;
  2747. case 2:
  2748. if (parm->msg_lev >= GLP_MSG_ALL)
  2749. xprintf("PROBLEM HAS UNBOUNDED SOLUTION\n");
  2750. store_sol(csa, lp, GLP_FEAS, GLP_NOFEAS,
  2751. csa->head[csa->m+csa->q]);
  2752. ret = 0;
  2753. break;
  2754. default:
  2755. xassert(csa != csa);
  2756. }
  2757. goto done;
  2758. }
  2759. /* check if the pivot element is acceptable */
  2760. if (csa->p > 0)
  2761. { double piv = csa->tcol_vec[csa->p];
  2762. double eps = 1e-5 * (1.0 + 0.01 * csa->tcol_max);
  2763. if (fabs(piv) < eps)
  2764. { if (parm->msg_lev >= GLP_MSG_DBG)
  2765. xprintf("piv = %.12g; eps = %g\n", piv, eps);
  2766. if (!rigorous)
  2767. { rigorous = 5;
  2768. goto loop;
  2769. }
  2770. }
  2771. }
  2772. /* now xN[q] and xB[p] have been chosen anyhow */
  2773. /* compute pivot row of the simplex table */
  2774. if (csa->p > 0)
  2775. { double *rho = csa->work4;
  2776. eval_rho(csa, rho);
  2777. if (rigorous) refine_rho(csa, rho);
  2778. eval_trow(csa, rho);
  2779. }
  2780. /* accuracy check based on the pivot element */
  2781. if (csa->p > 0)
  2782. { double piv1 = csa->tcol_vec[csa->p]; /* more accurate */
  2783. double piv2 = csa->trow_vec[csa->q]; /* less accurate */
  2784. xassert(piv1 != 0.0);
  2785. if (fabs(piv1 - piv2) > 1e-8 * (1.0 + fabs(piv1)) ||
  2786. !(piv1 > 0.0 && piv2 > 0.0 || piv1 < 0.0 && piv2 < 0.0))
  2787. { if (parm->msg_lev >= GLP_MSG_DBG)
  2788. xprintf("piv1 = %.12g; piv2 = %.12g\n", piv1, piv2);
  2789. if (binv_st != 1 || !rigorous)
  2790. { if (binv_st != 1) binv_st = 0;
  2791. rigorous = 5;
  2792. goto loop;
  2793. }
  2794. /* use more accurate version in the pivot row */
  2795. if (csa->trow_vec[csa->q] == 0.0)
  2796. { csa->trow_nnz++;
  2797. xassert(csa->trow_nnz <= csa->n);
  2798. csa->trow_ind[csa->trow_nnz] = csa->q;
  2799. }
  2800. csa->trow_vec[csa->q] = piv1;
  2801. }
  2802. }
  2803. /* update primal values of basic variables */
  2804. update_bbar(csa);
  2805. bbar_st = 2; /* updated */
  2806. /* update reduced costs of non-basic variables */
  2807. if (csa->p > 0)
  2808. { update_cbar(csa);
  2809. cbar_st = 2; /* updated */
  2810. /* on phase I objective coefficient of xB[p] in the adjacent
  2811. basis becomes zero */
  2812. if (csa->phase == 1)
  2813. { int k = csa->head[csa->p]; /* x[k] = xB[p] -> xN[q] */
  2814. csa->cbar[csa->q] -= csa->coef[k];
  2815. csa->coef[k] = 0.0;
  2816. }
  2817. }
  2818. /* update steepest edge coefficients */
  2819. if (csa->p > 0)
  2820. { switch (parm->pricing)
  2821. { case GLP_PT_STD:
  2822. break;
  2823. case GLP_PT_PSE:
  2824. if (csa->refct > 0) update_gamma(csa);
  2825. break;
  2826. default:
  2827. xassert(parm != parm);
  2828. }
  2829. }
  2830. /* update factorization of the basis matrix */
  2831. if (csa->p > 0)
  2832. { ret = update_B(csa, csa->p, csa->head[csa->m+csa->q]);
  2833. if (ret == 0)
  2834. binv_st = 2; /* updated */
  2835. else
  2836. { csa->valid = 0;
  2837. binv_st = 0; /* invalid */
  2838. }
  2839. }
  2840. /* update matrix N */
  2841. if (csa->p > 0)
  2842. { del_N_col(csa, csa->q, csa->head[csa->m+csa->q]);
  2843. if (csa->type[csa->head[csa->p]] != GLP_FX)
  2844. add_N_col(csa, csa->q, csa->head[csa->p]);
  2845. }
  2846. /* change the basis header */
  2847. change_basis(csa);
  2848. /* iteration complete */
  2849. csa->it_cnt++;
  2850. if (rigorous > 0) rigorous--;
  2851. goto loop;
  2852. done: /* deallocate the common storage area */
  2853. free_csa(csa);
  2854. /* return to the calling program */
  2855. return ret;
  2856. }
  2857. /* eof */