glpspx02.c 99 KB

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