aboutsummaryrefslogtreecommitdiffstats
path: root/test/monniaux/glpk-4.65/src/draft/glpipm.c
blob: 2b3a81762324b3ff579b014f5502d828e2663345 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
/* glpipm.c */

/***********************************************************************
*  This code is part of GLPK (GNU Linear Programming Kit).
*
*  Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
*  2009, 2010, 2011, 2013 Andrew Makhorin, Department for Applied
*  Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
*  reserved. E-mail: <mao@gnu.org>.
*
*  GLPK is free software: you can redistribute it and/or modify it
*  under the terms of the GNU General Public License as published by
*  the Free Software Foundation, either version 3 of the License, or
*  (at your option) any later version.
*
*  GLPK is distributed in the hope that it will be useful, but WITHOUT
*  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
*  or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
*  License for more details.
*
*  You should have received a copy of the GNU General Public License
*  along with GLPK. If not, see <http://www.gnu.org/licenses/>.
***********************************************************************/

#include "env.h"
#include "glpipm.h"
#include "glpmat.h"

#define ITER_MAX 100
/* maximal number of iterations */

struct csa
{     /* common storage area */
      /*--------------------------------------------------------------*/
      /* LP data */
      int m;
      /* number of rows (equality constraints) */
      int n;
      /* number of columns (structural variables) */
      int *A_ptr; /* int A_ptr[1+m+1]; */
      int *A_ind; /* int A_ind[A_ptr[m+1]]; */
      double *A_val; /* double A_val[A_ptr[m+1]]; */
      /* mxn-matrix A in storage-by-rows format */
      double *b; /* double b[1+m]; */
      /* m-vector b of right-hand sides */
      double *c; /* double c[1+n]; */
      /* n-vector c of objective coefficients; c[0] is constant term of
         the objective function */
      /*--------------------------------------------------------------*/
      /* LP solution */
      double *x; /* double x[1+n]; */
      double *y; /* double y[1+m]; */
      double *z; /* double z[1+n]; */
      /* current point in primal-dual space; the best point on exit */
      /*--------------------------------------------------------------*/
      /* control parameters */
      const glp_iptcp *parm;
      /*--------------------------------------------------------------*/
      /* working arrays and variables */
      double *D; /* double D[1+n]; */
      /* diagonal nxn-matrix D = X*inv(Z), where X = diag(x[j]) and
         Z = diag(z[j]) */
      int *P; /* int P[1+m+m]; */
      /* permutation mxm-matrix P used to minimize fill-in in Cholesky
         factorization */
      int *S_ptr; /* int S_ptr[1+m+1]; */
      int *S_ind; /* int S_ind[S_ptr[m+1]]; */
      double *S_val; /* double S_val[S_ptr[m+1]]; */
      double *S_diag; /* double S_diag[1+m]; */
      /* symmetric mxm-matrix S = P*A*D*A'*P' whose upper triangular
         part without diagonal elements is stored in S_ptr, S_ind, and
         S_val in storage-by-rows format, diagonal elements are stored
         in S_diag */
      int *U_ptr; /* int U_ptr[1+m+1]; */
      int *U_ind; /* int U_ind[U_ptr[m+1]]; */
      double *U_val; /* double U_val[U_ptr[m+1]]; */
      double *U_diag; /* double U_diag[1+m]; */
      /* upper triangular mxm-matrix U defining Cholesky factorization
         S = U'*U; its non-diagonal elements are stored in U_ptr, U_ind,
         U_val in storage-by-rows format, diagonal elements are stored
         in U_diag */
      int iter;
      /* iteration number (0, 1, 2, ...); iter = 0 corresponds to the
         initial point */
      double obj;
      /* current value of the objective function */
      double rpi;
      /* relative primal infeasibility rpi = ||A*x-b||/(1+||b||) */
      double rdi;
      /* relative dual infeasibility rdi = ||A'*y+z-c||/(1+||c||) */
      double gap;
      /* primal-dual gap = |c'*x-b'*y|/(1+|c'*x|) which is a relative
         difference between primal and dual objective functions */
      double phi;
      /* merit function phi = ||A*x-b||/max(1,||b||) +
                            + ||A'*y+z-c||/max(1,||c||) +
                            + |c'*x-b'*y|/max(1,||b||,||c||) */
      double mu;
      /* duality measure mu = x'*z/n (used as barrier parameter) */
      double rmu;
      /* rmu = max(||A*x-b||,||A'*y+z-c||)/mu */
      double rmu0;
      /* the initial value of rmu on iteration 0 */
      double *phi_min; /* double phi_min[1+ITER_MAX]; */
      /* phi_min[k] = min(phi[k]), where phi[k] is the value of phi on
         k-th iteration, 0 <= k <= iter */
      int best_iter;
      /* iteration number, on which the value of phi reached its best
         (minimal) value */
      double *best_x; /* double best_x[1+n]; */
      double *best_y; /* double best_y[1+m]; */
      double *best_z; /* double best_z[1+n]; */
      /* best point (in the sense of the merit function phi) which has
         been reached on iteration iter_best */
      double best_obj;
      /* objective value at the best point */
      double *dx_aff; /* double dx_aff[1+n]; */
      double *dy_aff; /* double dy_aff[1+m]; */
      double *dz_aff; /* double dz_aff[1+n]; */
      /* affine scaling direction */
      double alfa_aff_p, alfa_aff_d;
      /* maximal primal and dual stepsizes in affine scaling direction,
         on which x and z are still non-negative */
      double mu_aff;
      /* duality measure mu_aff = x_aff'*z_aff/n in the boundary point
         x_aff' = x+alfa_aff_p*dx_aff, z_aff' = z+alfa_aff_d*dz_aff */
      double sigma;
      /* Mehrotra's heuristic parameter (0 <= sigma <= 1) */
      double *dx_cc; /* double dx_cc[1+n]; */
      double *dy_cc; /* double dy_cc[1+m]; */
      double *dz_cc; /* double dz_cc[1+n]; */
      /* centering corrector direction */
      double *dx; /* double dx[1+n]; */
      double *dy; /* double dy[1+m]; */
      double *dz; /* double dz[1+n]; */
      /* final combined direction dx = dx_aff+dx_cc, dy = dy_aff+dy_cc,
         dz = dz_aff+dz_cc */
      double alfa_max_p;
      double alfa_max_d;
      /* maximal primal and dual stepsizes in combined direction, on
         which x and z are still non-negative */
};

/***********************************************************************
*  initialize - allocate and initialize common storage area
*
*  This routine allocates and initializes the common storage area (CSA)
*  used by interior-point method routines. */

static void initialize(struct csa *csa)
{     int m = csa->m;
      int n = csa->n;
      int i;
      if (csa->parm->msg_lev >= GLP_MSG_ALL)
         xprintf("Matrix A has %d non-zeros\n", csa->A_ptr[m+1]-1);
      csa->D = xcalloc(1+n, sizeof(double));
      /* P := I */
      csa->P = xcalloc(1+m+m, sizeof(int));
      for (i = 1; i <= m; i++) csa->P[i] = csa->P[m+i] = i;
      /* S := A*A', symbolically */
      csa->S_ptr = xcalloc(1+m+1, sizeof(int));
      csa->S_ind = adat_symbolic(m, n, csa->P, csa->A_ptr, csa->A_ind,
         csa->S_ptr);
      if (csa->parm->msg_lev >= GLP_MSG_ALL)
         xprintf("Matrix S = A*A' has %d non-zeros (upper triangle)\n",
            csa->S_ptr[m+1]-1 + m);
      /* determine P using specified ordering algorithm */
      if (csa->parm->ord_alg == GLP_ORD_NONE)
      {  if (csa->parm->msg_lev >= GLP_MSG_ALL)
            xprintf("Original ordering is being used\n");
         for (i = 1; i <= m; i++)
            csa->P[i] = csa->P[m+i] = i;
      }
      else if (csa->parm->ord_alg == GLP_ORD_QMD)
      {  if (csa->parm->msg_lev >= GLP_MSG_ALL)
            xprintf("Minimum degree ordering (QMD)...\n");
         min_degree(m, csa->S_ptr, csa->S_ind, csa->P);
      }
      else if (csa->parm->ord_alg == GLP_ORD_AMD)
      {  if (csa->parm->msg_lev >= GLP_MSG_ALL)
            xprintf("Approximate minimum degree ordering (AMD)...\n");
         amd_order1(m, csa->S_ptr, csa->S_ind, csa->P);
      }
      else if (csa->parm->ord_alg == GLP_ORD_SYMAMD)
      {  if (csa->parm->msg_lev >= GLP_MSG_ALL)
            xprintf("Approximate minimum degree ordering (SYMAMD)...\n")
               ;
         symamd_ord(m, csa->S_ptr, csa->S_ind, csa->P);
      }
      else
         xassert(csa != csa);
      /* S := P*A*A'*P', symbolically */
      xfree(csa->S_ind);
      csa->S_ind = adat_symbolic(m, n, csa->P, csa->A_ptr, csa->A_ind,
         csa->S_ptr);
      csa->S_val = xcalloc(csa->S_ptr[m+1], sizeof(double));
      csa->S_diag = xcalloc(1+m, sizeof(double));
      /* compute Cholesky factorization S = U'*U, symbolically */
      if (csa->parm->msg_lev >= GLP_MSG_ALL)
         xprintf("Computing Cholesky factorization S = L*L'...\n");
      csa->U_ptr = xcalloc(1+m+1, sizeof(int));
      csa->U_ind = chol_symbolic(m, csa->S_ptr, csa->S_ind, csa->U_ptr);
      if (csa->parm->msg_lev >= GLP_MSG_ALL)
         xprintf("Matrix L has %d non-zeros\n", csa->U_ptr[m+1]-1 + m);
      csa->U_val = xcalloc(csa->U_ptr[m+1], sizeof(double));
      csa->U_diag = xcalloc(1+m, sizeof(double));
      csa->iter = 0;
      csa->obj = 0.0;
      csa->rpi = 0.0;
      csa->rdi = 0.0;
      csa->gap = 0.0;
      csa->phi = 0.0;
      csa->mu = 0.0;
      csa->rmu = 0.0;
      csa->rmu0 = 0.0;
      csa->phi_min = xcalloc(1+ITER_MAX, sizeof(double));
      csa->best_iter = 0;
      csa->best_x = xcalloc(1+n, sizeof(double));
      csa->best_y = xcalloc(1+m, sizeof(double));
      csa->best_z = xcalloc(1+n, sizeof(double));
      csa->best_obj = 0.0;
      csa->dx_aff = xcalloc(1+n, sizeof(double));
      csa->dy_aff = xcalloc(1+m, sizeof(double));
      csa->dz_aff = xcalloc(1+n, sizeof(double));
      csa->alfa_aff_p = 0.0;
      csa->alfa_aff_d = 0.0;
      csa->mu_aff = 0.0;
      csa->sigma = 0.0;
      csa->dx_cc = xcalloc(1+n, sizeof(double));
      csa->dy_cc = xcalloc(1+m, sizeof(double));
      csa->dz_cc = xcalloc(1+n, sizeof(double));
      csa->dx = csa->dx_aff;
      csa->dy = csa->dy_aff;
      csa->dz = csa->dz_aff;
      csa->alfa_max_p = 0.0;
      csa->alfa_max_d = 0.0;
      return;
}

/***********************************************************************
*  A_by_vec - compute y = A*x
*
*  This routine computes matrix-vector product y = A*x, where A is the
*  constraint matrix. */

static void A_by_vec(struct csa *csa, double x[], double y[])
{     /* compute y = A*x */
      int m = csa->m;
      int *A_ptr = csa->A_ptr;
      int *A_ind = csa->A_ind;
      double *A_val = csa->A_val;
      int i, t, beg, end;
      double temp;
      for (i = 1; i <= m; i++)
      {  temp = 0.0;
         beg = A_ptr[i], end = A_ptr[i+1];
         for (t = beg; t < end; t++) temp += A_val[t] * x[A_ind[t]];
         y[i] = temp;
      }
      return;
}

/***********************************************************************
*  AT_by_vec - compute y = A'*x
*
*  This routine computes matrix-vector product y = A'*x, where A' is a
*  matrix transposed to the constraint matrix A. */

static void AT_by_vec(struct csa *csa, double x[], double y[])
{     /* compute y = A'*x, where A' is transposed to A */
      int m = csa->m;
      int n = csa->n;
      int *A_ptr = csa->A_ptr;
      int *A_ind = csa->A_ind;
      double *A_val = csa->A_val;
      int i, j, t, beg, end;
      double temp;
      for (j = 1; j <= n; j++) y[j] = 0.0;
      for (i = 1; i <= m; i++)
      {  temp = x[i];
         if (temp == 0.0) continue;
         beg = A_ptr[i], end = A_ptr[i+1];
         for (t = beg; t < end; t++) y[A_ind[t]] += A_val[t] * temp;
      }
      return;
}

/***********************************************************************
*  decomp_NE - numeric factorization of matrix S = P*A*D*A'*P'
*
*  This routine implements numeric phase of Cholesky factorization of
*  the matrix S = P*A*D*A'*P', which is a permuted matrix of the normal
*  equation system. Matrix D is assumed to be already computed. */

static void decomp_NE(struct csa *csa)
{     adat_numeric(csa->m, csa->n, csa->P, csa->A_ptr, csa->A_ind,
         csa->A_val, csa->D, csa->S_ptr, csa->S_ind, csa->S_val,
         csa->S_diag);
      chol_numeric(csa->m, csa->S_ptr, csa->S_ind, csa->S_val,
         csa->S_diag, csa->U_ptr, csa->U_ind, csa->U_val, csa->U_diag);
      return;
}

/***********************************************************************
*  solve_NE - solve normal equation system
*
*  This routine solves the normal equation system:
*
*     A*D*A'*y = h.
*
*  It is assumed that the matrix A*D*A' has been previously factorized
*  by the routine decomp_NE.
*
*  On entry the array y contains the vector of right-hand sides h. On
*  exit this array contains the computed vector of unknowns y.
*
*  Once the vector y has been computed the routine checks for numeric
*  stability. If the residual vector:
*
*     r = A*D*A'*y - h
*
*  is relatively small, the routine returns zero, otherwise non-zero is
*  returned. */

static int solve_NE(struct csa *csa, double y[])
{     int m = csa->m;
      int n = csa->n;
      int *P = csa->P;
      int i, j, ret = 0;
      double *h, *r, *w;
      /* save vector of right-hand sides h */
      h = xcalloc(1+m, sizeof(double));
      for (i = 1; i <= m; i++) h[i] = y[i];
      /* solve normal equation system (A*D*A')*y = h */
      /* since S = P*A*D*A'*P' = U'*U, then A*D*A' = P'*U'*U*P, so we
         have inv(A*D*A') = P'*inv(U)*inv(U')*P */
      /* w := P*h */
      w = xcalloc(1+m, sizeof(double));
      for (i = 1; i <= m; i++) w[i] = y[P[i]];
      /* w := inv(U')*w */
      ut_solve(m, csa->U_ptr, csa->U_ind, csa->U_val, csa->U_diag, w);
      /* w := inv(U)*w */
      u_solve(m, csa->U_ptr, csa->U_ind, csa->U_val, csa->U_diag, w);
      /* y := P'*w */
      for (i = 1; i <= m; i++) y[i] = w[P[m+i]];
      xfree(w);
      /* compute residual vector r = A*D*A'*y - h */
      r = xcalloc(1+m, sizeof(double));
      /* w := A'*y */
      w = xcalloc(1+n, sizeof(double));
      AT_by_vec(csa, y, w);
      /* w := D*w */
      for (j = 1; j <= n; j++) w[j] *= csa->D[j];
      /* r := A*w */
      A_by_vec(csa, w, r);
      xfree(w);
      /* r := r - h */
      for (i = 1; i <= m; i++) r[i] -= h[i];
      /* check for numeric stability */
      for (i = 1; i <= m; i++)
      {  if (fabs(r[i]) / (1.0 + fabs(h[i])) > 1e-4)
         {  ret = 1;
            break;
         }
      }
      xfree(h);
      xfree(r);
      return ret;
}

/***********************************************************************
*  solve_NS - solve Newtonian system
*
*  This routine solves the Newtonian system:
*
*     A*dx               = p
*
*           A'*dy +   dz = q
*
*     Z*dx        + X*dz = r
*
*  where X = diag(x[j]), Z = diag(z[j]), by reducing it to the normal
*  equation system:
*
*     (A*inv(Z)*X*A')*dy = A*inv(Z)*(X*q-r)+p
*
*  (it is assumed that the matrix A*inv(Z)*X*A' has been factorized by
*  the routine decomp_NE).
*
*  Once vector dy has been computed the routine computes vectors dx and
*  dz as follows:
*
*     dx = inv(Z)*(X*(A'*dy-q)+r)
*
*     dz = inv(X)*(r-Z*dx)
*
*  The routine solve_NS returns the same code which was reported by the
*  routine solve_NE (see above). */

static int solve_NS(struct csa *csa, double p[], double q[], double r[],
      double dx[], double dy[], double dz[])
{     int m = csa->m;
      int n = csa->n;
      double *x = csa->x;
      double *z = csa->z;
      int i, j, ret;
      double *w = dx;
      /* compute the vector of right-hand sides A*inv(Z)*(X*q-r)+p for
         the normal equation system */
      for (j = 1; j <= n; j++)
         w[j] = (x[j] * q[j] - r[j]) / z[j];
      A_by_vec(csa, w, dy);
      for (i = 1; i <= m; i++) dy[i] += p[i];
      /* solve the normal equation system to compute vector dy */
      ret = solve_NE(csa, dy);
      /* compute vectors dx and dz */
      AT_by_vec(csa, dy, dx);
      for (j = 1; j <= n; j++)
      {  dx[j] = (x[j] * (dx[j] - q[j]) + r[j]) / z[j];
         dz[j] = (r[j] - z[j] * dx[j]) / x[j];
      }
      return ret;
}

/***********************************************************************
*  initial_point - choose initial point using Mehrotra's heuristic
*
*  This routine chooses a starting point using a heuristic proposed in
*  the paper:
*
*  S. Mehrotra. On the implementation of a primal-dual interior point
*  method. SIAM J. on Optim., 2(4), pp. 575-601, 1992.
*
*  The starting point x in the primal space is chosen as a solution of
*  the following least squares problem:
*
*     minimize    ||x||
*
*     subject to  A*x = b
*
*  which can be computed explicitly as follows:
*
*     x = A'*inv(A*A')*b
*
*  Similarly, the starting point (y, z) in the dual space is chosen as
*  a solution of the following least squares problem:
*
*     minimize    ||z||
*
*     subject to  A'*y + z = c
*
*  which can be computed explicitly as follows:
*
*     y = inv(A*A')*A*c
*
*     z = c - A'*y
*
*  However, some components of the vectors x and z may be non-positive
*  or close to zero, so the routine uses a Mehrotra's heuristic to find
*  a more appropriate starting point. */

static void initial_point(struct csa *csa)
{     int m = csa->m;
      int n = csa->n;
      double *b = csa->b;
      double *c = csa->c;
      double *x = csa->x;
      double *y = csa->y;
      double *z = csa->z;
      double *D = csa->D;
      int i, j;
      double dp, dd, ex, ez, xz;
      /* factorize A*A' */
      for (j = 1; j <= n; j++) D[j] = 1.0;
      decomp_NE(csa);
      /* x~ = A'*inv(A*A')*b */
      for (i = 1; i <= m; i++) y[i] = b[i];
      solve_NE(csa, y);
      AT_by_vec(csa, y, x);
      /* y~ = inv(A*A')*A*c */
      A_by_vec(csa, c, y);
      solve_NE(csa, y);
      /* z~ = c - A'*y~ */
      AT_by_vec(csa, y,z);
      for (j = 1; j <= n; j++) z[j] = c[j] - z[j];
      /* use Mehrotra's heuristic in order to choose more appropriate
         starting point with positive components of vectors x and z */
      dp = dd = 0.0;
      for (j = 1; j <= n; j++)
      {  if (dp < -1.5 * x[j]) dp = -1.5 * x[j];
         if (dd < -1.5 * z[j]) dd = -1.5 * z[j];
      }
      /* note that b = 0 involves x = 0, and c = 0 involves y = 0 and
         z = 0, so we need to be careful */
      if (dp == 0.0) dp = 1.5;
      if (dd == 0.0) dd = 1.5;
      ex = ez = xz = 0.0;
      for (j = 1; j <= n; j++)
      {  ex += (x[j] + dp);
         ez += (z[j] + dd);
         xz += (x[j] + dp) * (z[j] + dd);
      }
      dp += 0.5 * (xz / ez);
      dd += 0.5 * (xz / ex);
      for (j = 1; j <= n; j++)
      {  x[j] += dp;
         z[j] += dd;
         xassert(x[j] > 0.0 && z[j] > 0.0);
      }
      return;
}

/***********************************************************************
*  basic_info - perform basic computations at the current point
*
*  This routine computes the following quantities at the current point:
*
*  1) value of the objective function:
*
*     F = c'*x + c[0]
*
*  2) relative primal infeasibility:
*
*     rpi = ||A*x-b|| / (1+||b||)
*
*  3) relative dual infeasibility:
*
*     rdi = ||A'*y+z-c|| / (1+||c||)
*
*  4) primal-dual gap (relative difference between the primal and the
*     dual objective function values):
*
*     gap = |c'*x-b'*y| / (1+|c'*x|)
*
*  5) merit function:
*
*     phi = ||A*x-b|| / max(1,||b||) + ||A'*y+z-c|| / max(1,||c||) +
*
*         + |c'*x-b'*y| / max(1,||b||,||c||)
*
*  6) duality measure:
*
*     mu = x'*z / n
*
*  7) the ratio of infeasibility to mu:
*
*     rmu = max(||A*x-b||,||A'*y+z-c||) / mu
*
*  where ||*|| denotes euclidian norm, *' denotes transposition. */

static void basic_info(struct csa *csa)
{     int m = csa->m;
      int n = csa->n;
      double *b = csa->b;
      double *c = csa->c;
      double *x = csa->x;
      double *y = csa->y;
      double *z = csa->z;
      int i, j;
      double norm1, bnorm, norm2, cnorm, cx, by, *work, temp;
      /* compute value of the objective function */
      temp = c[0];
      for (j = 1; j <= n; j++) temp += c[j] * x[j];
      csa->obj = temp;
      /* norm1 = ||A*x-b|| */
      work = xcalloc(1+m, sizeof(double));
      A_by_vec(csa, x, work);
      norm1 = 0.0;
      for (i = 1; i <= m; i++)
         norm1 += (work[i] - b[i]) * (work[i] - b[i]);
      norm1 = sqrt(norm1);
      xfree(work);
      /* bnorm = ||b|| */
      bnorm = 0.0;
      for (i = 1; i <= m; i++) bnorm += b[i] * b[i];
      bnorm = sqrt(bnorm);
      /* compute relative primal infeasibility */
      csa->rpi = norm1 / (1.0 + bnorm);
      /* norm2 = ||A'*y+z-c|| */
      work = xcalloc(1+n, sizeof(double));
      AT_by_vec(csa, y, work);
      norm2 = 0.0;
      for (j = 1; j <= n; j++)
         norm2 += (work[j] + z[j] - c[j]) * (work[j] + z[j] - c[j]);
      norm2 = sqrt(norm2);
      xfree(work);
      /* cnorm = ||c|| */
      cnorm = 0.0;
      for (j = 1; j <= n; j++) cnorm += c[j] * c[j];
      cnorm = sqrt(cnorm);
      /* compute relative dual infeasibility */
      csa->rdi = norm2 / (1.0 + cnorm);
      /* by = b'*y */
      by = 0.0;
      for (i = 1; i <= m; i++) by += b[i] * y[i];
      /* cx = c'*x */
      cx = 0.0;
      for (j = 1; j <= n; j++) cx += c[j] * x[j];
      /* compute primal-dual gap */
      csa->gap = fabs(cx - by) / (1.0 + fabs(cx));
      /* compute merit function */
      csa->phi = 0.0;
      csa->phi += norm1 / (bnorm > 1.0 ? bnorm : 1.0);
      csa->phi += norm2 / (cnorm > 1.0 ? cnorm : 1.0);
      temp = 1.0;
      if (temp < bnorm) temp = bnorm;
      if (temp < cnorm) temp = cnorm;
      csa->phi += fabs(cx - by) / temp;
      /* compute duality measure */
      temp = 0.0;
      for (j = 1; j <= n; j++) temp += x[j] * z[j];
      csa->mu = temp / (double)n;
      /* compute the ratio of infeasibility to mu */
      csa->rmu = (norm1 > norm2 ? norm1 : norm2) / csa->mu;
      return;
}

/***********************************************************************
*  make_step - compute next point using Mehrotra's technique
*
*  This routine computes the next point using the predictor-corrector
*  technique proposed in the paper:
*
*  S. Mehrotra. On the implementation of a primal-dual interior point
*  method. SIAM J. on Optim., 2(4), pp. 575-601, 1992.
*
*  At first, the routine computes so called affine scaling (predictor)
*  direction (dx_aff,dy_aff,dz_aff) which is a solution of the system:
*
*     A*dx_aff                       = b - A*x
*
*               A'*dy_aff +   dz_aff = c - A'*y - z
*
*     Z*dx_aff            + X*dz_aff = - X*Z*e
*
*  where (x,y,z) is the current point, X = diag(x[j]), Z = diag(z[j]),
*  e = (1,...,1)'.
*
*  Then, the routine computes the centering parameter sigma, using the
*  following Mehrotra's heuristic:
*
*     alfa_aff_p = inf{0 <= alfa <= 1 | x+alfa*dx_aff >= 0}
*
*     alfa_aff_d = inf{0 <= alfa <= 1 | z+alfa*dz_aff >= 0}
*
*     mu_aff = (x+alfa_aff_p*dx_aff)'*(z+alfa_aff_d*dz_aff)/n
*
*     sigma = (mu_aff/mu)^3
*
*  where alfa_aff_p is the maximal stepsize along the affine scaling
*  direction in the primal space, alfa_aff_d is the maximal stepsize
*  along the same direction in the dual space.
*
*  After determining sigma the routine computes so called centering
*  (corrector) direction (dx_cc,dy_cc,dz_cc) which is the solution of
*  the system:
*
*     A*dx_cc                     = 0
*
*              A'*dy_cc +   dz_cc = 0
*
*     Z*dx_cc           + X*dz_cc = sigma*mu*e - X*Z*e
*
*  Finally, the routine computes the combined direction
*
*     (dx,dy,dz) = (dx_aff,dy_aff,dz_aff) + (dx_cc,dy_cc,dz_cc)
*
*  and determines maximal primal and dual stepsizes along the combined
*  direction:
*
*     alfa_max_p = inf{0 <= alfa <= 1 | x+alfa*dx >= 0}
*
*     alfa_max_d = inf{0 <= alfa <= 1 | z+alfa*dz >= 0}
*
*  In order to prevent the next point to be too close to the boundary
*  of the positive ortant, the routine decreases maximal stepsizes:
*
*     alfa_p = gamma_p * alfa_max_p
*
*     alfa_d = gamma_d * alfa_max_d
*
*  where gamma_p and gamma_d are scaling factors, and computes the next
*  point:
*
*     x_new = x + alfa_p * dx
*
*     y_new = y + alfa_d * dy
*
*     z_new = z + alfa_d * dz
*
*  which becomes the current point on the next iteration. */

static int make_step(struct csa *csa)
{     int m = csa->m;
      int n = csa->n;
      double *b = csa->b;
      double *c = csa->c;
      double *x = csa->x;
      double *y = csa->y;
      double *z = csa->z;
      double *dx_aff = csa->dx_aff;
      double *dy_aff = csa->dy_aff;
      double *dz_aff = csa->dz_aff;
      double *dx_cc = csa->dx_cc;
      double *dy_cc = csa->dy_cc;
      double *dz_cc = csa->dz_cc;
      double *dx = csa->dx;
      double *dy = csa->dy;
      double *dz = csa->dz;
      int i, j, ret = 0;
      double temp, gamma_p, gamma_d, *p, *q, *r;
      /* allocate working arrays */
      p = xcalloc(1+m, sizeof(double));
      q = xcalloc(1+n, sizeof(double));
      r = xcalloc(1+n, sizeof(double));
      /* p = b - A*x */
      A_by_vec(csa, x, p);
      for (i = 1; i <= m; i++) p[i] = b[i] - p[i];
      /* q = c - A'*y - z */
      AT_by_vec(csa, y,q);
      for (j = 1; j <= n; j++) q[j] = c[j] - q[j] - z[j];
      /* r = - X * Z * e */
      for (j = 1; j <= n; j++) r[j] = - x[j] * z[j];
      /* solve the first Newtonian system */
      if (solve_NS(csa, p, q, r, dx_aff, dy_aff, dz_aff))
      {  ret = 1;
         goto done;
      }
      /* alfa_aff_p = inf{0 <= alfa <= 1 | x + alfa*dx_aff >= 0} */
      /* alfa_aff_d = inf{0 <= alfa <= 1 | z + alfa*dz_aff >= 0} */
      csa->alfa_aff_p = csa->alfa_aff_d = 1.0;
      for (j = 1; j <= n; j++)
      {  if (dx_aff[j] < 0.0)
         {  temp = - x[j] / dx_aff[j];
            if (csa->alfa_aff_p > temp) csa->alfa_aff_p = temp;
         }
         if (dz_aff[j] < 0.0)
         {  temp = - z[j] / dz_aff[j];
            if (csa->alfa_aff_d > temp) csa->alfa_aff_d = temp;
         }
      }
      /* mu_aff = (x+alfa_aff_p*dx_aff)' * (z+alfa_aff_d*dz_aff) / n */
      temp = 0.0;
      for (j = 1; j <= n; j++)
         temp += (x[j] + csa->alfa_aff_p * dx_aff[j]) *
                 (z[j] + csa->alfa_aff_d * dz_aff[j]);
      csa->mu_aff = temp / (double)n;
      /* sigma = (mu_aff/mu)^3 */
      temp = csa->mu_aff / csa->mu;
      csa->sigma = temp * temp * temp;
      /* p = 0 */
      for (i = 1; i <= m; i++) p[i] = 0.0;
      /* q = 0 */
      for (j = 1; j <= n; j++) q[j] = 0.0;
      /* r = sigma * mu * e - X * Z * e */
      for (j = 1; j <= n; j++)
         r[j] = csa->sigma * csa->mu - dx_aff[j] * dz_aff[j];
      /* solve the second Newtonian system with the same coefficients
         but with altered right-hand sides */
      if (solve_NS(csa, p, q, r, dx_cc, dy_cc, dz_cc))
      {  ret = 1;
         goto done;
      }
      /* (dx,dy,dz) = (dx_aff,dy_aff,dz_aff) + (dx_cc,dy_cc,dz_cc) */
      for (j = 1; j <= n; j++) dx[j] = dx_aff[j] + dx_cc[j];
      for (i = 1; i <= m; i++) dy[i] = dy_aff[i] + dy_cc[i];
      for (j = 1; j <= n; j++) dz[j] = dz_aff[j] + dz_cc[j];
      /* alfa_max_p = inf{0 <= alfa <= 1 | x + alfa*dx >= 0} */
      /* alfa_max_d = inf{0 <= alfa <= 1 | z + alfa*dz >= 0} */
      csa->alfa_max_p = csa->alfa_max_d = 1.0;
      for (j = 1; j <= n; j++)
      {  if (dx[j] < 0.0)
         {  temp = - x[j] / dx[j];
            if (csa->alfa_max_p > temp) csa->alfa_max_p = temp;
         }
         if (dz[j] < 0.0)
         {  temp = - z[j] / dz[j];
            if (csa->alfa_max_d > temp) csa->alfa_max_d = temp;
         }
      }
      /* determine scale factors (not implemented yet) */
      gamma_p = 0.90;
      gamma_d = 0.90;
      /* compute the next point */
      for (j = 1; j <= n; j++)
      {  x[j] += gamma_p * csa->alfa_max_p * dx[j];
         xassert(x[j] > 0.0);
      }
      for (i = 1; i <= m; i++)
         y[i] += gamma_d * csa->alfa_max_d * dy[i];
      for (j = 1; j <= n; j++)
      {  z[j] += gamma_d * csa->alfa_max_d * dz[j];
         xassert(z[j] > 0.0);
      }
done: /* free working arrays */
      xfree(p);
      xfree(q);
      xfree(r);
      return ret;
}

/***********************************************************************
*  terminate - deallocate common storage area
*
*  This routine frees all memory allocated to the common storage area
*  used by interior-point method routines. */

static void terminate(struct csa *csa)
{     xfree(csa->D);
      xfree(csa->P);
      xfree(csa->S_ptr);
      xfree(csa->S_ind);
      xfree(csa->S_val);
      xfree(csa->S_diag);
      xfree(csa->U_ptr);
      xfree(csa->U_ind);
      xfree(csa->U_val);
      xfree(csa->U_diag);
      xfree(csa->phi_min);
      xfree(csa->best_x);
      xfree(csa->best_y);
      xfree(csa->best_z);
      xfree(csa->dx_aff);
      xfree(csa->dy_aff);
      xfree(csa->dz_aff);
      xfree(csa->dx_cc);
      xfree(csa->dy_cc);
      xfree(csa->dz_cc);
      return;
}

/***********************************************************************
*  ipm_main - main interior-point method routine
*
*  This is a main routine of the primal-dual interior-point method.
*
*  The routine ipm_main returns one of the following codes:
*
*  0 - optimal solution found;
*  1 - problem has no feasible (primal or dual) solution;
*  2 - no convergence;
*  3 - iteration limit exceeded;
*  4 - numeric instability on solving Newtonian system.
*
*  In case of non-zero return code the routine returns the best point,
*  which has been reached during optimization. */

static int ipm_main(struct csa *csa)
{     int m = csa->m;
      int n = csa->n;
      int i, j, status;
      double temp;
      /* choose initial point using Mehrotra's heuristic */
      if (csa->parm->msg_lev >= GLP_MSG_ALL)
         xprintf("Guessing initial point...\n");
      initial_point(csa);
      /* main loop starts here */
      if (csa->parm->msg_lev >= GLP_MSG_ALL)
         xprintf("Optimization begins...\n");
      for (;;)
      {  /* perform basic computations at the current point */
         basic_info(csa);
         /* save initial value of rmu */
         if (csa->iter == 0) csa->rmu0 = csa->rmu;
         /* accumulate values of min(phi[k]) and save the best point */
         xassert(csa->iter <= ITER_MAX);
         if (csa->iter == 0 || csa->phi_min[csa->iter-1] > csa->phi)
         {  csa->phi_min[csa->iter] = csa->phi;
            csa->best_iter = csa->iter;
            for (j = 1; j <= n; j++) csa->best_x[j] = csa->x[j];
            for (i = 1; i <= m; i++) csa->best_y[i] = csa->y[i];
            for (j = 1; j <= n; j++) csa->best_z[j] = csa->z[j];
            csa->best_obj = csa->obj;
         }
         else
            csa->phi_min[csa->iter] = csa->phi_min[csa->iter-1];
         /* display information at the current point */
         if (csa->parm->msg_lev >= GLP_MSG_ON)
            xprintf("%3d: obj = %17.9e; rpi = %8.1e; rdi = %8.1e; gap ="
               " %8.1e\n", csa->iter, csa->obj, csa->rpi, csa->rdi,
               csa->gap);
         /* check if the current point is optimal */
         if (csa->rpi < 1e-8 && csa->rdi < 1e-8 && csa->gap < 1e-8)
         {  if (csa->parm->msg_lev >= GLP_MSG_ALL)
               xprintf("OPTIMAL SOLUTION FOUND\n");
            status = 0;
            break;
         }
         /* check if the problem has no feasible solution */
         temp = 1e5 * csa->phi_min[csa->iter];
         if (temp < 1e-8) temp = 1e-8;
         if (csa->phi >= temp)
         {  if (csa->parm->msg_lev >= GLP_MSG_ALL)
               xprintf("PROBLEM HAS NO FEASIBLE PRIMAL/DUAL SOLUTION\n")
                  ;
            status = 1;
            break;
         }
         /* check for very slow convergence or divergence */
         if (((csa->rpi >= 1e-8 || csa->rdi >= 1e-8) && csa->rmu /
               csa->rmu0 >= 1e6) ||
               (csa->iter >= 30 && csa->phi_min[csa->iter] >= 0.5 *
               csa->phi_min[csa->iter - 30]))
         {  if (csa->parm->msg_lev >= GLP_MSG_ALL)
               xprintf("NO CONVERGENCE; SEARCH TERMINATED\n");
            status = 2;
            break;
         }
         /* check for maximal number of iterations */
         if (csa->iter == ITER_MAX)
         {  if (csa->parm->msg_lev >= GLP_MSG_ALL)
               xprintf("ITERATION LIMIT EXCEEDED; SEARCH TERMINATED\n");
            status = 3;
            break;
         }
         /* start the next iteration */
         csa->iter++;
         /* factorize normal equation system */
         for (j = 1; j <= n; j++) csa->D[j] = csa->x[j] / csa->z[j];
         decomp_NE(csa);
         /* compute the next point using Mehrotra's predictor-corrector
            technique */
         if (make_step(csa))
         {  if (csa->parm->msg_lev >= GLP_MSG_ALL)
               xprintf("NUMERIC INSTABILITY; SEARCH TERMINATED\n");
            status = 4;
            break;
         }
      }
      /* restore the best point */
      if (status != 0)
      {  for (j = 1; j <= n; j++) csa->x[j] = csa->best_x[j];
         for (i = 1; i <= m; i++) csa->y[i] = csa->best_y[i];
         for (j = 1; j <= n; j++) csa->z[j] = csa->best_z[j];
         if (csa->parm->msg_lev >= GLP_MSG_ALL)
            xprintf("Best point %17.9e was reached on iteration %d\n",
               csa->best_obj, csa->best_iter);
      }
      /* return to the calling program */
      return status;
}

/***********************************************************************
*  NAME
*
*  ipm_solve - core LP solver based on the interior-point method
*
*  SYNOPSIS
*
*  #include "glpipm.h"
*  int ipm_solve(glp_prob *P, const glp_iptcp *parm);
*
*  DESCRIPTION
*
*  The routine ipm_solve is a core LP solver based on the primal-dual
*  interior-point method.
*
*  The routine assumes the following standard formulation of LP problem
*  to be solved:
*
*     minimize
*
*        F = c[0] + c[1]*x[1] + c[2]*x[2] + ... + c[n]*x[n]
*
*     subject to linear constraints
*
*        a[1,1]*x[1] + a[1,2]*x[2] + ... + a[1,n]*x[n] = b[1]
*
*        a[2,1]*x[1] + a[2,2]*x[2] + ... + a[2,n]*x[n] = b[2]
*
*              . . . . . .
*
*        a[m,1]*x[1] + a[m,2]*x[2] + ... + a[m,n]*x[n] = b[m]
*
*     and non-negative variables
*
*        x[1] >= 0, x[2] >= 0, ..., x[n] >= 0
*
*  where:
*  F                    is the objective function;
*  x[1], ..., x[n]      are (structural) variables;
*  c[0]                 is a constant term of the objective function;
*  c[1], ..., c[n]      are objective coefficients;
*  a[1,1], ..., a[m,n]  are constraint coefficients;
*  b[1], ..., b[n]      are right-hand sides.
*
*  The solution is three vectors x, y, and z, which are stored by the
*  routine in the arrays x, y, and z, respectively. These vectors
*  correspond to the best primal-dual point found during optimization.
*  They are approximate solution of the following system (which is the
*  Karush-Kuhn-Tucker optimality conditions):
*
*     A*x      = b      (primal feasibility condition)
*
*     A'*y + z = c      (dual feasibility condition)
*
*     x'*z     = 0      (primal-dual complementarity condition)
*
*     x >= 0, z >= 0    (non-negativity condition)
*
*  where:
*  x[1], ..., x[n]      are primal (structural) variables;
*  y[1], ..., y[m]      are dual variables (Lagrange multipliers) for
*                       equality constraints;
*  z[1], ..., z[n]      are dual variables (Lagrange multipliers) for
*                       non-negativity constraints.
*
*  RETURNS
*
*  0  LP has been successfully solved.
*
*  GLP_ENOCVG
*     No convergence.
*
*  GLP_EITLIM
*     Iteration limit exceeded.
*
*  GLP_EINSTAB
*     Numeric instability on solving Newtonian system.
*
*  In case of non-zero return code the routine returns the best point,
*  which has been reached during optimization. */

int ipm_solve(glp_prob *P, const glp_iptcp *parm)
{     struct csa _dsa, *csa = &_dsa;
      int m = P->m;
      int n = P->n;
      int nnz = P->nnz;
      GLPROW *row;
      GLPCOL *col;
      GLPAIJ *aij;
      int i, j, loc, ret, *A_ind, *A_ptr;
      double dir, *A_val, *b, *c, *x, *y, *z;
      xassert(m > 0);
      xassert(n > 0);
      /* allocate working arrays */
      A_ptr = xcalloc(1+m+1, sizeof(int));
      A_ind = xcalloc(1+nnz, sizeof(int));
      A_val = xcalloc(1+nnz, sizeof(double));
      b = xcalloc(1+m, sizeof(double));
      c = xcalloc(1+n, sizeof(double));
      x = xcalloc(1+n, sizeof(double));
      y = xcalloc(1+m, sizeof(double));
      z = xcalloc(1+n, sizeof(double));
      /* prepare rows and constraint coefficients */
      loc = 1;
      for (i = 1; i <= m; i++)
      {  row = P->row[i];
         xassert(row->type == GLP_FX);
         b[i] = row->lb * row->rii;
         A_ptr[i] = loc;
         for (aij = row->ptr; aij != NULL; aij = aij->r_next)
         {  A_ind[loc] = aij->col->j;
            A_val[loc] = row->rii * aij->val * aij->col->sjj;
            loc++;
         }
      }
      A_ptr[m+1] = loc;
      xassert(loc-1 == nnz);
      /* prepare columns and objective coefficients */
      if (P->dir == GLP_MIN)
         dir = +1.0;
      else if (P->dir == GLP_MAX)
         dir = -1.0;
      else
         xassert(P != P);
      c[0] = dir * P->c0;
      for (j = 1; j <= n; j++)
      {  col = P->col[j];
         xassert(col->type == GLP_LO && col->lb == 0.0);
         c[j] = dir * col->coef * col->sjj;
      }
      /* allocate and initialize the common storage area */
      csa->m = m;
      csa->n = n;
      csa->A_ptr = A_ptr;
      csa->A_ind = A_ind;
      csa->A_val = A_val;
      csa->b = b;
      csa->c = c;
      csa->x = x;
      csa->y = y;
      csa->z = z;
      csa->parm = parm;
      initialize(csa);
      /* solve LP with the interior-point method */
      ret = ipm_main(csa);
      /* deallocate the common storage area */
      terminate(csa);
      /* determine solution status */
      if (ret == 0)
      {  /* optimal solution found */
         P->ipt_stat = GLP_OPT;
         ret = 0;
      }
      else if (ret == 1)
      {  /* problem has no feasible (primal or dual) solution */
         P->ipt_stat = GLP_NOFEAS;
         ret = 0;
      }
      else if (ret == 2)
      {  /* no convergence */
         P->ipt_stat = GLP_INFEAS;
         ret = GLP_ENOCVG;
      }
      else if (ret == 3)
      {  /* iteration limit exceeded */
         P->ipt_stat = GLP_INFEAS;
         ret = GLP_EITLIM;
      }
      else if (ret == 4)
      {  /* numeric instability on solving Newtonian system */
         P->ipt_stat = GLP_INFEAS;
         ret = GLP_EINSTAB;
      }
      else
         xassert(ret != ret);
      /* store row solution components */
      for (i = 1; i <= m; i++)
      {  row = P->row[i];
         row->pval = row->lb;
         row->dval = dir * y[i] * row->rii;
      }
      /* store column solution components */
      P->ipt_obj = P->c0;
      for (j = 1; j <= n; j++)
      {  col = P->col[j];
         col->pval = x[j] * col->sjj;
         col->dval = dir * z[j] / col->sjj;
         P->ipt_obj += col->coef * col->pval;
      }
      /* free working arrays */
      xfree(A_ptr);
      xfree(A_ind);
      xfree(A_val);
      xfree(b);
      xfree(c);
      xfree(x);
      xfree(y);
      xfree(z);
      return ret;
}

/* eof */