-
Notifications
You must be signed in to change notification settings - Fork 13
/
lpx.c
1505 lines (1383 loc) · 46.9 KB
/
lpx.c
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
/* lpx.c (old GLPK API) */
/* Written by Andrew Makhorin <mao@gnu.org>, August 2013. */
/* This file contains routines that implement the old GLPK API as it
* was defined in GLPK 4.48.
*
* To compile an existing project using these routines you need to add
* to the project this file and the header lpx.h.
*
* Please note that you may mix calls to old and new GLPK API routines
* (except calls to glp_create_prob and glp_delete_prob). */
#include <float.h>
#include <limits.h>
#include "lpx.h"
#define xassert glp_assert
#define xerror glp_error
struct CPS
{ /* control parameters */
LPX *lp;
/* pointer to corresponding problem object */
int msg_lev;
/* level of messages output by the solver:
0 - no output
1 - error messages only
2 - normal output
3 - full output (includes informational messages) */
int scale;
/* scaling option:
0 - no scaling
1 - equilibration scaling
2 - geometric mean scaling
3 - geometric mean scaling, then equilibration scaling */
int dual;
/* dual simplex option:
0 - use primal simplex
1 - use dual simplex */
int price;
/* pricing option (for both primal and dual simplex):
0 - textbook pricing
1 - steepest edge pricing */
double relax;
/* relaxation parameter used in the ratio test; if it is zero,
the textbook ratio test is used; if it is non-zero (should be
positive), Harris' two-pass ratio test is used; in the latter
case on the first pass basic variables (in the case of primal
simplex) or reduced costs of non-basic variables (in the case
of dual simplex) are allowed to slightly violate their bounds,
but not more than (relax * tol_bnd) or (relax * tol_dj) (thus,
relax is a percentage of tol_bnd or tol_dj) */
double tol_bnd;
/* relative tolerance used to check if the current basic solution
is primal feasible */
double tol_dj;
/* absolute tolerance used to check if the current basic solution
is dual feasible */
double tol_piv;
/* relative tolerance used to choose eligible pivotal elements of
the simplex table in the ratio test */
int round;
/* solution rounding option:
0 - report all computed values and reduced costs "as is"
1 - if possible (allowed by the tolerances), replace computed
values and reduced costs which are close to zero by exact
zeros */
double obj_ll;
/* lower limit of the objective function; if on the phase II the
objective function reaches this limit and continues decreasing,
the solver stops the search */
double obj_ul;
/* upper limit of the objective function; if on the phase II the
objective function reaches this limit and continues increasing,
the solver stops the search */
int it_lim;
/* simplex iterations limit; if this value is positive, it is
decreased by one each time when one simplex iteration has been
performed, and reaching zero value signals the solver to stop
the search; negative value means no iterations limit */
double tm_lim;
/* searching time limit, in seconds; if this value is positive,
it is decreased each time when one simplex iteration has been
performed by the amount of time spent for the iteration, and
reaching zero value signals the solver to stop the search;
negative value means no time limit */
int out_frq;
/* output frequency, in iterations; this parameter specifies how
frequently the solver sends information about the solution to
the standard output */
double out_dly;
/* output delay, in seconds; this parameter specifies how long
the solver should delay sending information about the solution
to the standard output; zero value means no delay */
int branch; /* MIP */
/* branching heuristic:
0 - branch on first variable
1 - branch on last variable
2 - branch using heuristic by Driebeck and Tomlin
3 - branch on most fractional variable */
int btrack; /* MIP */
/* backtracking heuristic:
0 - select most recent node (depth first search)
1 - select earliest node (breadth first search)
2 - select node using the best projection heuristic
3 - select node with best local bound */
double tol_int; /* MIP */
/* absolute tolerance used to check if the current basic solution
is integer feasible */
double tol_obj; /* MIP */
/* relative tolerance used to check if the value of the objective
function is not better than in the best known integer feasible
solution */
int mps_info; /* lpx_write_mps */
/* if this flag is set, the routine lpx_write_mps outputs several
comment cards that contains some information about the problem;
otherwise the routine outputs no comment cards */
int mps_obj; /* lpx_write_mps */
/* this parameter tells the routine lpx_write_mps how to output
the objective function row:
0 - never output objective function row
1 - always output objective function row
2 - output objective function row if and only if the problem
has no free rows */
int mps_orig; /* lpx_write_mps */
/* if this flag is set, the routine lpx_write_mps uses original
row and column symbolic names; otherwise the routine generates
plain names using ordinal numbers of rows and columns */
int mps_wide; /* lpx_write_mps */
/* if this flag is set, the routine lpx_write_mps uses all data
fields; otherwise the routine keeps fields 5 and 6 empty */
int mps_free; /* lpx_write_mps */
/* if this flag is set, the routine lpx_write_mps omits column
and vector names everytime if possible (free style); otherwise
the routine never omits these names (pedantic style) */
int mps_skip; /* lpx_write_mps */
/* if this flag is set, the routine lpx_write_mps skips empty
columns (i.e. which has no constraint coefficients); otherwise
the routine outputs all columns */
int lpt_orig; /* lpx_write_lpt */
/* if this flag is set, the routine lpx_write_lpt uses original
row and column symbolic names; otherwise the routine generates
plain names using ordinal numbers of rows and columns */
int presol; /* lpx_simplex */
/* LP presolver option:
0 - do not use LP presolver
1 - use LP presolver */
int binarize; /* lpx_intopt */
/* if this flag is set, the routine lpx_intopt replaces integer
columns by binary ones */
int use_cuts; /* lpx_intopt */
/* if this flag is set, the routine lpx_intopt tries generating
cutting planes:
LPX_C_COVER - mixed cover cuts
LPX_C_CLIQUE - clique cuts
LPX_C_GOMORY - Gomory's mixed integer cuts
LPX_C_ALL - all cuts */
double mip_gap; /* MIP */
/* relative MIP gap tolerance */
struct CPS *link;
/* pointer to CPS for another problem object */
};
static struct CPS *cps_ptr = NULL;
/* initial pointer to CPS linked list */
static struct CPS *find_cps(LPX *lp)
{ /* find CPS for specified problem object */
struct CPS *cps;
for (cps = cps_ptr; cps != NULL; cps = cps->link)
if (cps->lp == lp) break;
/* if cps is NULL (not found), the problem object was created
with glp_create_prob rather than with lpx_create_prob */
xassert(cps != NULL);
return cps;
}
static void reset_cps(struct CPS *cps)
{ /* reset control parameters to default values */
cps->msg_lev = 3;
cps->scale = 1;
cps->dual = 0;
cps->price = 1;
cps->relax = 0.07;
cps->tol_bnd = 1e-7;
cps->tol_dj = 1e-7;
cps->tol_piv = 1e-9;
cps->round = 0;
cps->obj_ll = -DBL_MAX;
cps->obj_ul = +DBL_MAX;
cps->it_lim = -1;
cps->tm_lim = -1.0;
cps->out_frq = 200;
cps->out_dly = 0.0;
cps->branch = 2;
cps->btrack = 3;
cps->tol_int = 1e-5;
cps->tol_obj = 1e-7;
cps->mps_info = 1;
cps->mps_obj = 2;
cps->mps_orig = 0;
cps->mps_wide = 1;
cps->mps_free = 0;
cps->mps_skip = 0;
cps->lpt_orig = 0;
cps->presol = 0;
cps->binarize = 0;
cps->use_cuts = 0;
cps->mip_gap = 0.0;
return;
}
LPX *lpx_create_prob(void)
{ /* create problem object */
LPX *lp;
struct CPS *cps;
lp = glp_create_prob();
cps = (CPS*) glp_alloc(1, sizeof(struct CPS));
cps->lp = lp;
reset_cps(cps);
cps->link = cps_ptr;
cps_ptr = cps;
return lp;
}
void lpx_set_prob_name(LPX *lp, const char *name)
{ /* assign (change) problem name */
glp_set_prob_name(lp, name);
return;
}
void lpx_set_obj_name(LPX *lp, const char *name)
{ /* assign (change) objective function name */
glp_set_obj_name(lp, name);
return;
}
void lpx_set_obj_dir(LPX *lp, int dir)
{ /* set (change) optimization direction flag */
glp_set_obj_dir(lp, dir - LPX_MIN + GLP_MIN);
return;
}
int lpx_add_rows(LPX *lp, int nrs)
{ /* add new rows to problem object */
return glp_add_rows(lp, nrs);
}
int lpx_add_cols(LPX *lp, int ncs)
{ /* add new columns to problem object */
return glp_add_cols(lp, ncs);
}
void lpx_set_row_name(LPX *lp, int i, const char *name)
{ /* assign (change) row name */
glp_set_row_name(lp, i, name);
return;
}
void lpx_set_col_name(LPX *lp, int j, const char *name)
{ /* assign (change) column name */
glp_set_col_name(lp, j, name);
return;
}
void lpx_set_row_bnds(LPX *lp, int i, int type, double lb, double ub)
{ /* set (change) row bounds */
glp_set_row_bnds(lp, i, type - LPX_FR + GLP_FR, lb, ub);
return;
}
void lpx_set_col_bnds(LPX *lp, int j, int type, double lb, double ub)
{ /* set (change) column bounds */
glp_set_col_bnds(lp, j, type - LPX_FR + GLP_FR, lb, ub);
return;
}
void lpx_set_obj_coef(glp_prob *lp, int j, double coef)
{ /* set (change) obj. coefficient or constant term */
glp_set_obj_coef(lp, j, coef);
return;
}
void lpx_set_mat_row(LPX *lp, int i, int len, const int ind[],
const double val[])
{ /* set (replace) row of the constraint matrix */
glp_set_mat_row(lp, i, len, ind, val);
return;
}
void lpx_set_mat_col(LPX *lp, int j, int len, const int ind[],
const double val[])
{ /* set (replace) column of the constraint matrix */
glp_set_mat_col(lp, j, len, ind, val);
return;
}
void lpx_load_matrix(LPX *lp, int ne, const int ia[], const int ja[],
const double ar[])
{ /* load (replace) the whole constraint matrix */
glp_load_matrix(lp, ne, ia, ja, ar);
return;
}
void lpx_del_rows(LPX *lp, int nrs, const int num[])
{ /* delete specified rows from problem object */
glp_del_rows(lp, nrs, num);
return;
}
void lpx_del_cols(LPX *lp, int ncs, const int num[])
{ /* delete specified columns from problem object */
glp_del_cols(lp, ncs, num);
return;
}
void lpx_delete_prob(LPX *lp)
{ /* delete problem object */
struct CPS *cps = find_cps(lp);
if (cps_ptr == cps)
cps_ptr = cps->link;
else
{ struct CPS *prev;
for (prev = cps_ptr; prev != NULL; prev = prev->link)
if (prev->link == cps) break;
xassert(prev != NULL);
prev->link = cps->link;
}
glp_free(cps);
glp_delete_prob(lp);
return;
}
const char *lpx_get_prob_name(LPX *lp)
{ /* retrieve problem name */
return glp_get_prob_name(lp);
}
const char *lpx_get_obj_name(LPX *lp)
{ /* retrieve objective function name */
return glp_get_obj_name(lp);
}
int lpx_get_obj_dir(LPX *lp)
{ /* retrieve optimization direction flag */
return glp_get_obj_dir(lp) - GLP_MIN + LPX_MIN;
}
int lpx_get_num_rows(LPX *lp)
{ /* retrieve number of rows */
return glp_get_num_rows(lp);
}
int lpx_get_num_cols(LPX *lp)
{ /* retrieve number of columns */
return glp_get_num_cols(lp);
}
const char *lpx_get_row_name(LPX *lp, int i)
{ /* retrieve row name */
return glp_get_row_name(lp, i);
}
const char *lpx_get_col_name(LPX *lp, int j)
{ /* retrieve column name */
return glp_get_col_name(lp, j);
}
int lpx_get_row_type(LPX *lp, int i)
{ /* retrieve row type */
return glp_get_row_type(lp, i) - GLP_FR + LPX_FR;
}
double lpx_get_row_lb(glp_prob *lp, int i)
{ /* retrieve row lower bound */
double lb;
lb = glp_get_row_lb(lp, i);
if (lb == -DBL_MAX) lb = 0.0;
return lb;
}
double lpx_get_row_ub(glp_prob *lp, int i)
{ /* retrieve row upper bound */
double ub;
ub = glp_get_row_ub(lp, i);
if (ub == +DBL_MAX) ub = 0.0;
return ub;
}
void lpx_get_row_bnds(glp_prob *lp, int i, int *typx, double *lb,
double *ub)
{ /* retrieve row bounds */
if (typx != NULL) *typx = lpx_get_row_type(lp, i);
if (lb != NULL) *lb = lpx_get_row_lb(lp, i);
if (ub != NULL) *ub = lpx_get_row_ub(lp, i);
return;
}
int lpx_get_col_type(LPX *lp, int j)
{ /* retrieve column type */
return glp_get_col_type(lp, j) - GLP_FR + LPX_FR;
}
double lpx_get_col_lb(glp_prob *lp, int j)
{ /* retrieve column lower bound */
double lb;
lb = glp_get_col_lb(lp, j);
if (lb == -DBL_MAX) lb = 0.0;
return lb;
}
double lpx_get_col_ub(glp_prob *lp, int j)
{ /* retrieve column upper bound */
double ub;
ub = glp_get_col_ub(lp, j);
if (ub == +DBL_MAX) ub = 0.0;
return ub;
}
void lpx_get_col_bnds(glp_prob *lp, int j, int *typx, double *lb,
double *ub)
{ /* retrieve column bounds */
if (typx != NULL) *typx = lpx_get_col_type(lp, j);
if (lb != NULL) *lb = lpx_get_col_lb(lp, j);
if (ub != NULL) *ub = lpx_get_col_ub(lp, j);
return;
}
double lpx_get_obj_coef(LPX *lp, int j)
{ /* retrieve obj. coefficient or constant term */
return glp_get_obj_coef(lp, j);
}
int lpx_get_num_nz(LPX *lp)
{ /* retrieve number of constraint coefficients */
return glp_get_num_nz(lp);
}
int lpx_get_mat_row(LPX *lp, int i, int ind[], double val[])
{ /* retrieve row of the constraint matrix */
return glp_get_mat_row(lp, i, ind, val);
}
int lpx_get_mat_col(LPX *lp, int j, int ind[], double val[])
{ /* retrieve column of the constraint matrix */
return glp_get_mat_col(lp, j, ind, val);
}
void lpx_create_index(LPX *lp)
{ /* create the name index */
glp_create_index(lp);
return;
}
int lpx_find_row(LPX *lp, const char *name)
{ /* find row by its name */
return glp_find_row(lp, name);
}
int lpx_find_col(LPX *lp, const char *name)
{ /* find column by its name */
return glp_find_col(lp, name);
}
void lpx_delete_index(LPX *lp)
{ /* delete the name index */
glp_delete_index(lp);
return;
}
void lpx_scale_prob(LPX *lp)
{ /* scale problem data */
switch (lpx_get_int_parm(lp, LPX_K_SCALE))
{ case 0:
/* no scaling */
glp_unscale_prob(lp);
break;
case 1:
/* equilibration scaling */
glp_scale_prob(lp, GLP_SF_EQ);
break;
case 2:
/* geometric mean scaling */
glp_scale_prob(lp, GLP_SF_GM);
break;
case 3:
/* geometric mean scaling, then equilibration scaling */
glp_scale_prob(lp, GLP_SF_GM | GLP_SF_EQ);
break;
default:
xassert(lp != lp);
}
return;
}
void lpx_unscale_prob(LPX *lp)
{ /* unscale problem data */
glp_unscale_prob(lp);
return;
}
void lpx_set_row_stat(LPX *lp, int i, int stat)
{ /* set (change) row status */
glp_set_row_stat(lp, i, stat - LPX_BS + GLP_BS);
return;
}
void lpx_set_col_stat(LPX *lp, int j, int stat)
{ /* set (change) column status */
glp_set_col_stat(lp, j, stat - LPX_BS + GLP_BS);
return;
}
void lpx_std_basis(LPX *lp)
{ /* construct standard initial LP basis */
glp_std_basis(lp);
return;
}
void lpx_adv_basis(LPX *lp)
{ /* construct advanced initial LP basis */
glp_adv_basis(lp, 0);
return;
}
void lpx_cpx_basis(LPX *lp)
{ /* construct Bixby's initial LP basis */
glp_cpx_basis(lp);
return;
}
static void fill_smcp(LPX *lp, glp_smcp *parm)
{ glp_init_smcp(parm);
switch (lpx_get_int_parm(lp, LPX_K_MSGLEV))
{ case 0: parm->msg_lev = GLP_MSG_OFF; break;
case 1: parm->msg_lev = GLP_MSG_ERR; break;
case 2: parm->msg_lev = GLP_MSG_ON; break;
case 3: parm->msg_lev = GLP_MSG_ALL; break;
default: xassert(lp != lp);
}
switch (lpx_get_int_parm(lp, LPX_K_DUAL))
{ case 0: parm->meth = GLP_PRIMAL; break;
case 1: parm->meth = GLP_DUAL; break;
default: xassert(lp != lp);
}
switch (lpx_get_int_parm(lp, LPX_K_PRICE))
{ case 0: parm->pricing = GLP_PT_STD; break;
case 1: parm->pricing = GLP_PT_PSE; break;
default: xassert(lp != lp);
}
if (lpx_get_real_parm(lp, LPX_K_RELAX) == 0.0)
parm->r_test = GLP_RT_STD;
else
parm->r_test = GLP_RT_HAR;
parm->tol_bnd = lpx_get_real_parm(lp, LPX_K_TOLBND);
parm->tol_dj = lpx_get_real_parm(lp, LPX_K_TOLDJ);
parm->tol_piv = lpx_get_real_parm(lp, LPX_K_TOLPIV);
parm->obj_ll = lpx_get_real_parm(lp, LPX_K_OBJLL);
parm->obj_ul = lpx_get_real_parm(lp, LPX_K_OBJUL);
if (lpx_get_int_parm(lp, LPX_K_ITLIM) < 0)
parm->it_lim = INT_MAX;
else
parm->it_lim = lpx_get_int_parm(lp, LPX_K_ITLIM);
if (lpx_get_real_parm(lp, LPX_K_TMLIM) < 0.0)
parm->tm_lim = INT_MAX;
else
parm->tm_lim =
(int)(1000.0 * lpx_get_real_parm(lp, LPX_K_TMLIM));
parm->out_frq = lpx_get_int_parm(lp, LPX_K_OUTFRQ);
parm->out_dly =
(int)(1000.0 * lpx_get_real_parm(lp, LPX_K_OUTDLY));
switch (lpx_get_int_parm(lp, LPX_K_PRESOL))
{ case 0: parm->presolve = GLP_OFF; break;
case 1: parm->presolve = GLP_ON; break;
default: xassert(lp != lp);
}
return;
}
int lpx_simplex(LPX *lp)
{ /* easy-to-use driver to the simplex method */
glp_smcp parm;
int ret;
fill_smcp(lp, &parm);
ret = glp_simplex(lp, &parm);
switch (ret)
{ case 0: ret = LPX_E_OK; break;
case GLP_EBADB:
case GLP_ESING:
case GLP_ECOND:
case GLP_EBOUND: ret = LPX_E_FAULT; break;
case GLP_EFAIL: ret = LPX_E_SING; break;
case GLP_EOBJLL: ret = LPX_E_OBJLL; break;
case GLP_EOBJUL: ret = LPX_E_OBJUL; break;
case GLP_EITLIM: ret = LPX_E_ITLIM; break;
case GLP_ETMLIM: ret = LPX_E_TMLIM; break;
case GLP_ENOPFS: ret = LPX_E_NOPFS; break;
case GLP_ENODFS: ret = LPX_E_NODFS; break;
default: xassert(ret != ret);
}
return ret;
}
int lpx_exact(LPX *lp)
{ /* easy-to-use driver to the exact simplex method */
glp_smcp parm;
int ret;
fill_smcp(lp, &parm);
ret = glp_exact(lp, &parm);
switch (ret)
{ case 0: ret = LPX_E_OK; break;
case GLP_EBADB:
case GLP_ESING:
case GLP_EBOUND:
case GLP_EFAIL: ret = LPX_E_FAULT; break;
case GLP_EITLIM: ret = LPX_E_ITLIM; break;
case GLP_ETMLIM: ret = LPX_E_TMLIM; break;
default: xassert(ret != ret);
}
return ret;
}
int lpx_get_status(glp_prob *lp)
{ /* retrieve generic status of basic solution */
int status;
switch (glp_get_status(lp))
{ case GLP_OPT: status = LPX_OPT; break;
case GLP_FEAS: status = LPX_FEAS; break;
case GLP_INFEAS: status = LPX_INFEAS; break;
case GLP_NOFEAS: status = LPX_NOFEAS; break;
case GLP_UNBND: status = LPX_UNBND; break;
case GLP_UNDEF: status = LPX_UNDEF; break;
default: xassert(lp != lp);
}
return status;
}
int lpx_get_prim_stat(glp_prob *lp)
{ /* retrieve status of primal basic solution */
return glp_get_prim_stat(lp) - GLP_UNDEF + LPX_P_UNDEF;
}
int lpx_get_dual_stat(glp_prob *lp)
{ /* retrieve status of dual basic solution */
return glp_get_dual_stat(lp) - GLP_UNDEF + LPX_D_UNDEF;
}
double lpx_get_obj_val(LPX *lp)
{ /* retrieve objective value (basic solution) */
return glp_get_obj_val(lp);
}
int lpx_get_row_stat(LPX *lp, int i)
{ /* retrieve row status (basic solution) */
return glp_get_row_stat(lp, i) - GLP_BS + LPX_BS;
}
double lpx_get_row_prim(LPX *lp, int i)
{ /* retrieve row primal value (basic solution) */
return glp_get_row_prim(lp, i);
}
double lpx_get_row_dual(LPX *lp, int i)
{ /* retrieve row dual value (basic solution) */
return glp_get_row_dual(lp, i);
}
void lpx_get_row_info(glp_prob *lp, int i, int *tagx, double *vx,
double *dx)
{ /* obtain row solution information */
if (tagx != NULL) *tagx = lpx_get_row_stat(lp, i);
if (vx != NULL) *vx = lpx_get_row_prim(lp, i);
if (dx != NULL) *dx = lpx_get_row_dual(lp, i);
return;
}
int lpx_get_col_stat(LPX *lp, int j)
{ /* retrieve column status (basic solution) */
return glp_get_col_stat(lp, j) - GLP_BS + LPX_BS;
}
double lpx_get_col_prim(LPX *lp, int j)
{ /* retrieve column primal value (basic solution) */
return glp_get_col_prim(lp, j);
}
double lpx_get_col_dual(glp_prob *lp, int j)
{ /* retrieve column dual value (basic solution) */
return glp_get_col_dual(lp, j);
}
void lpx_get_col_info(glp_prob *lp, int j, int *tagx, double *vx,
double *dx)
{ /* obtain column solution information */
if (tagx != NULL) *tagx = lpx_get_col_stat(lp, j);
if (vx != NULL) *vx = lpx_get_col_prim(lp, j);
if (dx != NULL) *dx = lpx_get_col_dual(lp, j);
return;
}
int lpx_get_ray_info(LPX *lp)
{ /* determine what causes primal unboundness */
return glp_get_unbnd_ray(lp);
}
void lpx_check_kkt(LPX *lp, int scaled, LPXKKT *kkt)
{ /* check Karush-Kuhn-Tucker conditions */
int m = glp_get_num_rows(lp);
int ae_ind, re_ind;
double ae_max, re_max;
xassert(scaled == scaled);
glp_check_kkt(lp, GLP_SOL, GLP_KKT_PE, &ae_max, &ae_ind, &re_max,
&re_ind);
kkt->pe_ae_max = ae_max;
kkt->pe_ae_row = ae_ind;
kkt->pe_re_max = re_max;
kkt->pe_re_row = re_ind;
if (re_max <= 1e-9)
kkt->pe_quality = 'H';
else if (re_max <= 1e-6)
kkt->pe_quality = 'M';
else if (re_max <= 1e-3)
kkt->pe_quality = 'L';
else
kkt->pe_quality = '?';
glp_check_kkt(lp, GLP_SOL, GLP_KKT_PB, &ae_max, &ae_ind, &re_max,
&re_ind);
kkt->pb_ae_max = ae_max;
kkt->pb_ae_ind = ae_ind;
kkt->pb_re_max = re_max;
kkt->pb_re_ind = re_ind;
if (re_max <= 1e-9)
kkt->pb_quality = 'H';
else if (re_max <= 1e-6)
kkt->pb_quality = 'M';
else if (re_max <= 1e-3)
kkt->pb_quality = 'L';
else
kkt->pb_quality = '?';
glp_check_kkt(lp, GLP_SOL, GLP_KKT_DE, &ae_max, &ae_ind, &re_max,
&re_ind);
kkt->de_ae_max = ae_max;
if (ae_ind == 0)
kkt->de_ae_col = 0;
else
kkt->de_ae_col = ae_ind - m;
kkt->de_re_max = re_max;
if (re_ind == 0)
kkt->de_re_col = 0;
else
kkt->de_re_col = ae_ind - m;
if (re_max <= 1e-9)
kkt->de_quality = 'H';
else if (re_max <= 1e-6)
kkt->de_quality = 'M';
else if (re_max <= 1e-3)
kkt->de_quality = 'L';
else
kkt->de_quality = '?';
glp_check_kkt(lp, GLP_SOL, GLP_KKT_DB, &ae_max, &ae_ind, &re_max,
&re_ind);
kkt->db_ae_max = ae_max;
kkt->db_ae_ind = ae_ind;
kkt->db_re_max = re_max;
kkt->db_re_ind = re_ind;
if (re_max <= 1e-9)
kkt->db_quality = 'H';
else if (re_max <= 1e-6)
kkt->db_quality = 'M';
else if (re_max <= 1e-3)
kkt->db_quality = 'L';
else
kkt->db_quality = '?';
kkt->cs_ae_max = 0.0, kkt->cs_ae_ind = 0;
kkt->cs_re_max = 0.0, kkt->cs_re_ind = 0;
kkt->cs_quality = 'H';
return;
}
int lpx_warm_up(LPX *lp)
{ /* "warm up" LP basis */
int ret;
ret = glp_warm_up(lp);
if (ret == 0)
ret = LPX_E_OK;
else if (ret == GLP_EBADB)
ret = LPX_E_BADB;
else if (ret == GLP_ESING)
ret = LPX_E_SING;
else if (ret == GLP_ECOND)
ret = LPX_E_SING;
else
xassert(ret != ret);
return ret;
}
int lpx_eval_tab_row(LPX *lp, int k, int ind[], double val[])
{ /* compute row of the simplex tableau */
return glp_eval_tab_row(lp, k, ind, val);
}
int lpx_eval_tab_col(LPX *lp, int k, int ind[], double val[])
{ /* compute column of the simplex tableau */
return glp_eval_tab_col(lp, k, ind, val);
}
int lpx_transform_row(LPX *lp, int len, int ind[], double val[])
{ /* transform explicitly specified row */
return glp_transform_row(lp, len, ind, val);
}
int lpx_transform_col(LPX *lp, int len, int ind[], double val[])
{ /* transform explicitly specified column */
return glp_transform_col(lp, len, ind, val);
}
int lpx_prim_ratio_test(LPX *lp, int len, const int ind[],
const double val[], int how, double tol)
{ /* perform primal ratio test */
int piv;
piv = glp_prim_rtest(lp, len, ind, val, how, tol);
xassert(0 <= piv && piv <= len);
return piv == 0 ? 0 : ind[piv];
}
int lpx_dual_ratio_test(LPX *lp, int len, const int ind[],
const double val[], int how, double tol)
{ /* perform dual ratio test */
int piv;
piv = glp_dual_rtest(lp, len, ind, val, how, tol);
xassert(0 <= piv && piv <= len);
return piv == 0 ? 0 : ind[piv];
}
int lpx_interior(LPX *lp)
{ /* easy-to-use driver to the interior-point method */
int ret;
ret = glp_interior(lp, NULL);
switch (ret)
{ case 0: ret = LPX_E_OK; break;
case GLP_EFAIL: ret = LPX_E_FAULT; break;
case GLP_ENOFEAS: ret = LPX_E_NOFEAS; break;
case GLP_ENOCVG: ret = LPX_E_NOCONV; break;
case GLP_EITLIM: ret = LPX_E_ITLIM; break;
case GLP_EINSTAB: ret = LPX_E_INSTAB; break;
default: xassert(ret != ret);
}
return ret;
}
int lpx_ipt_status(glp_prob *lp)
{ /* retrieve status of interior-point solution */
int status;
switch (glp_ipt_status(lp))
{ case GLP_UNDEF: status = LPX_T_UNDEF; break;
case GLP_OPT: status = LPX_T_OPT; break;
default: xassert(lp != lp);
}
return status;
}
double lpx_ipt_obj_val(LPX *lp)
{ /* retrieve objective value (interior point) */
return glp_ipt_obj_val(lp);
}
double lpx_ipt_row_prim(LPX *lp, int i)
{ /* retrieve row primal value (interior point) */
return glp_ipt_row_prim(lp, i);
}
double lpx_ipt_row_dual(LPX *lp, int i)
{ /* retrieve row dual value (interior point) */
return glp_ipt_row_dual(lp, i);
}
double lpx_ipt_col_prim(LPX *lp, int j)
{ /* retrieve column primal value (interior point) */
return glp_ipt_col_prim(lp, j);
}
double lpx_ipt_col_dual(LPX *lp, int j)
{ /* retrieve column dual value (interior point) */
return glp_ipt_col_dual(lp, j);
}
void lpx_set_class(LPX *lp, int klass)
{ /* set problem class */
xassert(lp == lp);
if (!(klass == LPX_LP || klass == LPX_MIP))
xerror("lpx_set_class: invalid problem class\n");
return;
}
int lpx_get_class(LPX *lp)
{ /* determine problem klass */
return glp_get_num_int(lp) == 0 ? LPX_LP : LPX_MIP;
}
void lpx_set_col_kind(LPX *lp, int j, int kind)
{ /* set (change) column kind */
glp_set_col_kind(lp, j, kind - LPX_CV + GLP_CV);
return;
}
int lpx_get_col_kind(LPX *lp, int j)
{ /* retrieve column kind */
return glp_get_col_kind(lp, j) == GLP_CV ? LPX_CV : LPX_IV;
}
int lpx_get_num_int(LPX *lp)
{ /* retrieve number of integer columns */
return glp_get_num_int(lp);
}
int lpx_get_num_bin(LPX *lp)
{ /* retrieve number of binary columns */
return glp_get_num_bin(lp);
}
static int solve_mip(LPX *lp, int presolve)
{ glp_iocp parm;
int ret;
glp_init_iocp(&parm);
switch (lpx_get_int_parm(lp, LPX_K_MSGLEV))
{ case 0: parm.msg_lev = GLP_MSG_OFF; break;
case 1: parm.msg_lev = GLP_MSG_ERR; break;
case 2: parm.msg_lev = GLP_MSG_ON; break;
case 3: parm.msg_lev = GLP_MSG_ALL; break;
default: xassert(lp != lp);
}
switch (lpx_get_int_parm(lp, LPX_K_BRANCH))
{ case 0: parm.br_tech = GLP_BR_FFV; break;
case 1: parm.br_tech = GLP_BR_LFV; break;
case 2: parm.br_tech = GLP_BR_DTH; break;
case 3: parm.br_tech = GLP_BR_MFV; break;
default: xassert(lp != lp);
}
switch (lpx_get_int_parm(lp, LPX_K_BTRACK))
{ case 0: parm.bt_tech = GLP_BT_DFS; break;
case 1: parm.bt_tech = GLP_BT_BFS; break;
case 2: parm.bt_tech = GLP_BT_BPH; break;
case 3: parm.bt_tech = GLP_BT_BLB; break;
default: xassert(lp != lp);
}
parm.tol_int = lpx_get_real_parm(lp, LPX_K_TOLINT);
parm.tol_obj = lpx_get_real_parm(lp, LPX_K_TOLOBJ);
if (lpx_get_real_parm(lp, LPX_K_TMLIM) < 0.0 ||
lpx_get_real_parm(lp, LPX_K_TMLIM) > 1e6)
parm.tm_lim = INT_MAX;
else
parm.tm_lim =
(int)(1000.0 * lpx_get_real_parm(lp, LPX_K_TMLIM));
parm.mip_gap = lpx_get_real_parm(lp, LPX_K_MIPGAP);
if (lpx_get_int_parm(lp, LPX_K_USECUTS) & LPX_C_GOMORY)
parm.gmi_cuts = GLP_ON;
else
parm.gmi_cuts = GLP_OFF;
if (lpx_get_int_parm(lp, LPX_K_USECUTS) & LPX_C_MIR)
parm.mir_cuts = GLP_ON;
else
parm.mir_cuts = GLP_OFF;
if (lpx_get_int_parm(lp, LPX_K_USECUTS) & LPX_C_COVER)
parm.cov_cuts = GLP_ON;
else
parm.cov_cuts = GLP_OFF;
if (lpx_get_int_parm(lp, LPX_K_USECUTS) & LPX_C_CLIQUE)
parm.clq_cuts = GLP_ON;
else
parm.clq_cuts = GLP_OFF;
parm.presolve = presolve;
if (lpx_get_int_parm(lp, LPX_K_BINARIZE))
parm.binarize = GLP_ON;
ret = glp_intopt(lp, &parm);
switch (ret)
{ case 0: ret = LPX_E_OK; break;
case GLP_ENOPFS: ret = LPX_E_NOPFS; break;
case GLP_ENODFS: ret = LPX_E_NODFS; break;
case GLP_EBOUND:
case GLP_EROOT: ret = LPX_E_FAULT; break;
case GLP_EFAIL: ret = LPX_E_SING; break;
case GLP_EMIPGAP: ret = LPX_E_MIPGAP; break;
case GLP_ETMLIM: ret = LPX_E_TMLIM; break;
default: xassert(ret != ret);
}
return ret;
}
int lpx_integer(LPX *lp)
{ /* easy-to-use driver to the branch-and-bound method */
return solve_mip(lp, GLP_OFF);
}
int lpx_intopt(LPX *lp)
{ /* easy-to-use driver to the branch-and-bound method */
return solve_mip(lp, GLP_ON);
}
int lpx_mip_status(glp_prob *lp)