-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathglpapi21.c
1343 lines (1334 loc) · 49.8 KB
/
glpapi21.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
/* glpapi21.c (stand-alone LP/MIP solver) */
/***********************************************************************
* 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: <[email protected]>.
*
* 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 "glpapi.h"
#include "glpgmp.h"
struct csa
{ /* common storage area */
glp_prob *prob;
/* LP/MIP problem object */
glp_bfcp bfcp;
/* basis factorization control parameters */
glp_smcp smcp;
/* simplex method control parameters */
glp_iptcp iptcp;
/* interior-point method control parameters */
glp_iocp iocp;
/* integer optimizer control parameters */
glp_tran *tran;
/* model translator workspace */
glp_graph *graph;
/* network problem object */
int format;
/* problem file format: */
#define FMT_MPS_DECK 1 /* fixed MPS */
#define FMT_MPS_FILE 2 /* free MPS */
#define FMT_LP 3 /* CPLEX LP */
#define FMT_GLP 4 /* GLPK LP/MIP */
#define FMT_MATHPROG 5 /* MathProg */
#define FMT_MIN_COST 6 /* DIMACS min-cost flow */
#define FMT_MAX_FLOW 7 /* DIMACS maximum flow */
#if 1 /* 06/VIII-2011 */
#define FMT_CNF 8 /* DIMACS CNF-SAT */
#endif
const char *in_file;
/* name of input problem file */
#define DATA_MAX 10
/* maximal number of input data files */
int ndf;
/* number of input data files specified */
const char *in_data[1+DATA_MAX];
/* name(s) of input data file(s) */
const char *out_dpy;
/* name of output file to send display output; NULL means the
display output is sent to the terminal */
int seed;
/* seed value to be passed to the MathProg translator; initially
set to 1; 0x80000000 means the value is omitted */
int solution;
/* solution type flag: */
#define SOL_BASIC 1 /* basic */
#define SOL_INTERIOR 2 /* interior-point */
#define SOL_INTEGER 3 /* mixed integer */
const char *in_res;
/* name of input solution file in raw format */
int dir;
/* optimization direction flag:
0 - not specified
GLP_MIN - minimization
GLP_MAX - maximization */
int scale;
/* automatic problem scaling flag */
const char *out_sol;
/* name of output solution file in printable format */
const char *out_res;
/* name of output solution file in raw format */
const char *out_ranges;
/* name of output file to write sensitivity analysis report */
int check;
/* input data checking flag; no solution is performed */
const char *new_name;
/* new name to be assigned to the problem */
const char *out_mps;
/* name of output problem file in fixed MPS format */
const char *out_freemps;
/* name of output problem file in free MPS format */
const char *out_cpxlp;
/* name of output problem file in CPLEX LP format */
const char *out_glp;
/* name of output problem file in GLPK format */
const char *out_pb;
/* name of output problem file in OPB format */
const char *out_npb;
/* name of output problem file in normalized OPB format */
#if 1 /* 06/VIII-2011 */
const char *out_cnf;
/* name of output problem file in DIMACS CNF-SAT format */
#endif
const char *log_file;
/* name of output file to hardcopy terminal output */
int crash;
/* initial basis option: */
#define USE_STD_BASIS 1 /* use standard basis */
#define USE_ADV_BASIS 2 /* use advanced basis */
#define USE_CPX_BASIS 3 /* use Bixby's basis */
#define USE_INI_BASIS 4 /* use initial basis from ini_file */
const char *ini_file;
/* name of input file containing initial basis */
int exact;
/* flag to use glp_exact rather than glp_simplex */
int xcheck;
/* flag to check final basis with glp_exact */
int nomip;
/* flag to consider MIP as pure LP */
#if 1 /* 15/VIII-2011 */
int minisat;
/* option to solve feasibility problem with MiniSat solver */
int use_bnd;
/* option to bound objective function */
int obj_bnd;
/* upper (minization) or lower (maximization) objective bound */
#endif
};
static void print_help(const char *my_name)
{ /* print help information */
xprintf("Usage: %s [options...] filename\n", my_name);
xprintf("\n");
xprintf("General options:\n");
xprintf(" --mps read LP/MIP problem in fixed MPS fo"
"rmat\n");
xprintf(" --freemps read LP/MIP problem in free MPS for"
"mat (default)\n");
xprintf(" --lp read LP/MIP problem in CPLEX LP for"
"mat\n");
xprintf(" --glp read LP/MIP problem in GLPK format "
"\n");
xprintf(" --math read LP/MIP model written in GNU Ma"
"thProg modeling\n");
xprintf(" language\n");
xprintf(" -m filename, --model filename\n");
xprintf(" read model section and optional dat"
"a section from\n");
xprintf(" filename (same as --math)\n");
xprintf(" -d filename, --data filename\n");
xprintf(" read data section from filename (fo"
"r --math only);\n");
xprintf(" if model file also has data section"
", it is ignored\n");
xprintf(" -y filename, --display filename\n");
xprintf(" send display output to filename (fo"
"r --math only);\n");
xprintf(" by default the output is sent to te"
"rminal\n");
xprintf(" --seed value initialize pseudo-random number gen"
"erator used in\n");
xprintf(" MathProg model with specified seed "
"(any integer);\n");
xprintf(" if seed value is ?, some random see"
"d will be used\n");
xprintf(" --mincost read min-cost flow problem in DIMAC"
"S format\n");
xprintf(" --maxflow read maximum flow problem in DIMACS"
" format\n");
#if 1 /* 06/VIII-2011 */
xprintf(" --cnf read CNF-SAT problem in DIMACS form"
"at\n");
#endif
xprintf(" --simplex use simplex method (default)\n");
xprintf(" --interior use interior point method (LP only)"
"\n");
xprintf(" -r filename, --read filename\n");
xprintf(" read solution from filename rather "
"to find it with\n");
xprintf(" the solver\n");
xprintf(" --min minimization\n");
xprintf(" --max maximization\n");
xprintf(" --scale scale problem (default)\n");
xprintf(" --noscale do not scale problem\n");
xprintf(" -o filename, --output filename\n");
xprintf(" write solution to filename in print"
"able format\n");
xprintf(" -w filename, --write filename\n");
xprintf(" write solution to filename in plain"
" text format\n");
xprintf(" --ranges filename\n");
xprintf(" write sensitivity analysis report t"
"o filename in\n");
xprintf(" printable format (simplex only)\n");
xprintf(" --tmlim nnn limit solution time to nnn seconds "
"\n");
xprintf(" --memlim nnn limit available memory to nnn megab"
"ytes\n");
xprintf(" --check do not solve problem, check input d"
"ata only\n");
xprintf(" --name probname change problem name to probname\n");
xprintf(" --wmps filename write problem to filename in fixed "
"MPS format\n");
xprintf(" --wfreemps filename\n");
xprintf(" write problem to filename in free M"
"PS format\n");
xprintf(" --wlp filename write problem to filename in CPLEX "
"LP format\n");
xprintf(" --wglp filename write problem to filename in GLPK f"
"ormat\n");
#if 0
xprintf(" --wpb filename write problem to filename in OPB fo"
"rmat\n");
xprintf(" --wnpb filename write problem to filename in normal"
"ized OPB format\n");
#endif
#if 1 /* 06/VIII-2011 */
xprintf(" --wcnf filename write problem to filename in DIMACS"
" CNF-SAT format\n");
#endif
xprintf(" --log filename write copy of terminal output to fi"
"lename\n");
xprintf(" -h, --help display this help information and e"
"xit\n");
xprintf(" -v, --version display program version and exit\n")
;
xprintf("\n");
xprintf("LP basis factorization options:\n");
xprintf(" --luf LU + Forrest-Tomlin update\n");
xprintf(" (faster, less stable; default)\n");
xprintf(" --cbg LU + Schur complement + Bartels-Gol"
"ub update\n");
xprintf(" (slower, more stable)\n");
xprintf(" --cgr LU + Schur complement + Givens rota"
"tion update\n");
xprintf(" (slower, more stable)\n");
xprintf("\n");
xprintf("Options specific to simplex solver:\n");
xprintf(" --primal use primal simplex (default)\n");
xprintf(" --dual use dual simplex\n");
xprintf(" --std use standard initial basis of all s"
"lacks\n");
xprintf(" --adv use advanced initial basis (default"
")\n");
xprintf(" --bib use Bixby's initial basis\n");
xprintf(" --ini filename use as initial basis previously sav"
"ed with -w\n");
xprintf(" (disables LP presolver)\n");
xprintf(" --steep use steepest edge technique (defaul"
"t)\n");
xprintf(" --nosteep use standard \"textbook\" pricing\n"
);
xprintf(" --relax use Harris' two-pass ratio test (de"
"fault)\n");
xprintf(" --norelax use standard \"textbook\" ratio tes"
"t\n");
xprintf(" --presol use presolver (default; assumes --s"
"cale and --adv)\n");
xprintf(" --nopresol do not use presolver\n");
xprintf(" --exact use simplex method based on exact a"
"rithmetic\n");
xprintf(" --xcheck check final basis using exact arith"
"metic\n");
xprintf("\n");
xprintf("Options specific to interior-point solver:\n");
xprintf(" --nord use natural (original) ordering\n");
xprintf(" --qmd use quotient minimum degree orderin"
"g\n");
xprintf(" --amd use approximate minimum degree orde"
"ring (default)\n");
xprintf(" --symamd use approximate minimum degree orde"
"ring\n");
xprintf("\n");
xprintf("Options specific to MIP solver:\n");
xprintf(" --nomip consider all integer variables as c"
"ontinuous\n");
xprintf(" (allows solving MIP as pure LP)\n");
xprintf(" --first branch on first integer variable\n")
;
xprintf(" --last branch on last integer variable\n");
xprintf(" --mostf branch on most fractional variable "
"\n");
xprintf(" --drtom branch using heuristic by Driebeck "
"and Tomlin\n");
xprintf(" (default)\n");
xprintf(" --pcost branch using hybrid pseudocost heur"
"istic (may be\n");
xprintf(" useful for hard instances)\n");
xprintf(" --dfs backtrack using depth first search "
"\n");
xprintf(" --bfs backtrack using breadth first searc"
"h\n");
xprintf(" --bestp backtrack using the best projection"
" heuristic\n");
xprintf(" --bestb backtrack using node with best loca"
"l bound\n");
xprintf(" (default)\n");
xprintf(" --intopt use MIP presolver (default)\n");
xprintf(" --nointopt do not use MIP presolver\n");
xprintf(" --binarize replace general integer variables b"
"y binary ones\n");
xprintf(" (assumes --intopt)\n");
xprintf(" --fpump apply feasibility pump heuristic\n")
;
xprintf(" --gomory generate Gomory's mixed integer cut"
"s\n");
xprintf(" --mir generate MIR (mixed integer roundin"
"g) cuts\n");
xprintf(" --cover generate mixed cover cuts\n");
xprintf(" --clique generate clique cuts\n");
xprintf(" --cuts generate all cuts above\n");
xprintf(" --mipgap tol set relative mip gap tolerance to t"
"ol\n");
#if 1 /* 15/VIII-2011 */
xprintf(" --minisat translate integer feasibility probl"
"em to CNF-SAT\n");
xprintf(" and solve it with MiniSat solver\n")
;
xprintf(" --objbnd bound add inequality obj <= bound (minimi"
"zation) or\n");
xprintf(" obj >= bound (maximization) to inte"
"ger feasibility\n");
xprintf(" problem (assumes --minisat)\n");
#endif
xprintf("\n");
xprintf("For description of the MPS and CPLEX LP formats see Refe"
"rence Manual.\n");
xprintf("For description of the modeling language see \"GLPK: Mod"
"eling Language\n");
xprintf("GNU MathProg\". Both documents are included in the GLPK "
"distribution.\n");
xprintf("\n");
xprintf("See GLPK web page at <http://www.gnu.org/software/glpk/g"
"lpk.html>.\n");
xprintf("\n");
xprintf("Please report bugs to <[email protected]>.\n");
return;
}
static void print_version(int briefly)
{ /* print version information */
xprintf("GLPSOL: GLPK LP/MIP Solver, v%s\n", glp_version());
if (briefly) goto done;
xprintf("\n");
xprintf("Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, "
"2007, 2008,\n");
xprintf("2009, 2010, 2011, 2013 Andrew Makhorin, Department for A"
"pplied\n");
xprintf("Informatics, Moscow Aviation Institute, Moscow, Russia. "
"All rights\n");
xprintf("reserved. E-mail: <[email protected]>.\n");
xprintf("\n");
xprintf("This program has ABSOLUTELY NO WARRANTY.\n");
xprintf("\n");
xprintf("This program is free software; you may re-distribute it "
"under the terms\n");
xprintf("of the GNU General Public License version 3 or later.\n")
;
done: return;
}
static int parse_cmdline(struct csa *csa, int argc, const char *argv[])
{ /* parse command-line parameters */
int k;
#define p(str) (strcmp(argv[k], str) == 0)
for (k = 1; k < argc; k++)
{ if (p("--mps"))
csa->format = FMT_MPS_DECK;
else if (p("--freemps"))
csa->format = FMT_MPS_FILE;
else if (p("--lp") || p("--cpxlp"))
csa->format = FMT_LP;
else if (p("--glp"))
csa->format = FMT_GLP;
else if (p("--math") || p("-m") || p("--model"))
csa->format = FMT_MATHPROG;
else if (p("-d") || p("--data"))
{ k++;
if (k == argc || argv[k][0] == '\0' || argv[k][0] == '-')
{ xprintf("No input data file specified\n");
return 1;
}
if (csa->ndf == DATA_MAX)
{ xprintf("Too many input data files\n");
return 1;
}
csa->in_data[++(csa->ndf)] = argv[k];
}
else if (p("-y") || p("--display"))
{ k++;
if (k == argc || argv[k][0] == '\0' || argv[k][0] == '-')
{ xprintf("No display output file specified\n");
return 1;
}
if (csa->out_dpy != NULL)
{ xprintf("Only one display output file allowed\n");
return 1;
}
csa->out_dpy = argv[k];
}
else if (p("--seed"))
{ k++;
if (k == argc || argv[k][0] == '\0' ||
argv[k][0] == '-' && !isdigit((unsigned char)argv[k][1]))
{ xprintf("No seed value specified\n");
return 1;
}
if (strcmp(argv[k], "?") == 0)
csa->seed = 0x80000000;
else if (str2int(argv[k], &csa->seed))
{ xprintf("Invalid seed value `%s'\n", argv[k]);
return 1;
}
}
else if (p("--mincost"))
csa->format = FMT_MIN_COST;
else if (p("--maxflow"))
csa->format = FMT_MAX_FLOW;
#if 1 /* 06/VIII-2011 */
else if (p("--cnf"))
csa->format = FMT_CNF;
#endif
else if (p("--simplex"))
csa->solution = SOL_BASIC;
else if (p("--interior"))
csa->solution = SOL_INTERIOR;
#if 1 /* 28/V-2010 */
else if (p("--alien"))
csa->iocp.alien = GLP_ON;
#endif
else if (p("-r") || p("--read"))
{ k++;
if (k == argc || argv[k][0] == '\0' || argv[k][0] == '-')
{ xprintf("No input solution file specified\n");
return 1;
}
if (csa->in_res != NULL)
{ xprintf("Only one input solution file allowed\n");
return 1;
}
csa->in_res = argv[k];
}
else if (p("--min"))
csa->dir = GLP_MIN;
else if (p("--max"))
csa->dir = GLP_MAX;
else if (p("--scale"))
csa->scale = 1;
else if (p("--noscale"))
csa->scale = 0;
else if (p("-o") || p("--output"))
{ k++;
if (k == argc || argv[k][0] == '\0' || argv[k][0] == '-')
{ xprintf("No output solution file specified\n");
return 1;
}
if (csa->out_sol != NULL)
{ xprintf("Only one output solution file allowed\n");
return 1;
}
csa->out_sol = argv[k];
}
else if (p("-w") || p("--write"))
{ k++;
if (k == argc || argv[k][0] == '\0' || argv[k][0] == '-')
{ xprintf("No output solution file specified\n");
return 1;
}
if (csa->out_res != NULL)
{ xprintf("Only one output solution file allowed\n");
return 1;
}
csa->out_res = argv[k];
}
else if (p("--ranges") || p("--bounds"))
{ k++;
if (k == argc || argv[k][0] == '\0' || argv[k][0] == '-')
{ xprintf("No output file specified to write sensitivity a"
"nalysis report\n");
return 1;
}
if (csa->out_ranges != NULL)
{ xprintf("Only one output file allowed to write sensitivi"
"ty analysis report\n");
return 1;
}
csa->out_ranges = argv[k];
}
else if (p("--tmlim"))
{ int tm_lim;
k++;
if (k == argc || argv[k][0] == '\0' || argv[k][0] == '-')
{ xprintf("No time limit specified\n");
return 1;
}
if (str2int(argv[k], &tm_lim) || tm_lim < 0)
{ xprintf("Invalid time limit `%s'\n", argv[k]);
return 1;
}
if (tm_lim <= INT_MAX / 1000)
csa->smcp.tm_lim = csa->iocp.tm_lim = 1000 * tm_lim;
else
csa->smcp.tm_lim = csa->iocp.tm_lim = INT_MAX;
}
else if (p("--memlim"))
{ int mem_lim;
k++;
if (k == argc || argv[k][0] == '\0' || argv[k][0] == '-')
{ xprintf("No memory limit specified\n");
return 1;
}
if (str2int(argv[k], &mem_lim) || mem_lim < 1)
{ xprintf("Invalid memory limit `%s'\n", argv[k]);
return 1;
}
glp_mem_limit(mem_lim);
}
else if (p("--check"))
csa->check = 1;
else if (p("--name"))
{ k++;
if (k == argc || argv[k][0] == '\0' || argv[k][0] == '-')
{ xprintf("No problem name specified\n");
return 1;
}
if (csa->new_name != NULL)
{ xprintf("Only one problem name allowed\n");
return 1;
}
csa->new_name = argv[k];
}
else if (p("--wmps"))
{ k++;
if (k == argc || argv[k][0] == '\0' || argv[k][0] == '-')
{ xprintf("No fixed MPS output file specified\n");
return 1;
}
if (csa->out_mps != NULL)
{ xprintf("Only one fixed MPS output file allowed\n");
return 1;
}
csa->out_mps = argv[k];
}
else if (p("--wfreemps"))
{ k++;
if (k == argc || argv[k][0] == '\0' || argv[k][0] == '-')
{ xprintf("No free MPS output file specified\n");
return 1;
}
if (csa->out_freemps != NULL)
{ xprintf("Only one free MPS output file allowed\n");
return 1;
}
csa->out_freemps = argv[k];
}
else if (p("--wlp") || p("--wcpxlp") || p("--wlpt"))
{ k++;
if (k == argc || argv[k][0] == '\0' || argv[k][0] == '-')
{ xprintf("No CPLEX LP output file specified\n");
return 1;
}
if (csa->out_cpxlp != NULL)
{ xprintf("Only one CPLEX LP output file allowed\n");
return 1;
}
csa->out_cpxlp = argv[k];
}
else if (p("--wglp"))
{ k++;
if (k == argc || argv[k][0] == '\0' || argv[k][0] == '-')
{ xprintf("No GLPK LP/MIP output file specified\n");
return 1;
}
if (csa->out_glp != NULL)
{ xprintf("Only one GLPK LP/MIP output file allowed\n");
return 1;
}
csa->out_glp = argv[k];
}
else if (p("--wpb"))
{ k++;
if (k == argc || argv[k][0] == '\0' || argv[k][0] == '-')
{ xprintf("No problem output file specified\n");
return 1;
}
if (csa->out_pb != NULL)
{ xprintf("Only one OPB output file allowed\n");
return 1;
}
csa->out_pb = argv[k];
}
else if (p("--wnpb"))
{ k++;
if (k == argc || argv[k][0] == '\0' || argv[k][0] == '-')
{ xprintf("No problem output file specified\n");
return 1;
}
if (csa->out_npb != NULL)
{ xprintf("Only one normalized OPB output file allowed\n");
return 1;
}
csa->out_npb = argv[k];
}
#if 1 /* 06/VIII-2011 */
else if (p("--wcnf"))
{ k++;
if (k == argc || argv[k][0] == '\0' || argv[k][0] == '-')
{ xprintf("No problem output file specified\n");
return 1;
}
if (csa->out_cnf != NULL)
{ xprintf("Only one output DIMACS CNF-SAT file allowed\n");
return 1;
}
csa->out_cnf = argv[k];
}
#endif
else if (p("--log"))
{ k++;
if (k == argc || argv[k][0] == '\0' || argv[k][0] == '-')
{ xprintf("No log file specified\n");
return 1;
}
if (csa->log_file != NULL)
{ xprintf("Only one log file allowed\n");
return 1;
}
csa->log_file = argv[k];
}
else if (p("-h") || p("--help"))
{ print_help(argv[0]);
return -1;
}
else if (p("-v") || p("--version"))
{ print_version(0);
return -1;
}
else if (p("--luf"))
csa->bfcp.type = GLP_BF_FT;
else if (p("--cbg"))
csa->bfcp.type = GLP_BF_BG;
else if (p("--cgr"))
csa->bfcp.type = GLP_BF_GR;
else if (p("--primal"))
csa->smcp.meth = GLP_PRIMAL;
else if (p("--dual"))
csa->smcp.meth = GLP_DUAL;
else if (p("--std"))
csa->crash = USE_STD_BASIS;
else if (p("--adv"))
csa->crash = USE_ADV_BASIS;
else if (p("--bib"))
csa->crash = USE_CPX_BASIS;
else if (p("--ini"))
{ csa->crash = USE_INI_BASIS;
csa->smcp.presolve = GLP_OFF;
k++;
if (k == argc || argv[k][0] == '\0' || argv[k][0] == '-')
{ xprintf("No initial basis file specified\n");
return 1;
}
if (csa->ini_file != NULL)
{ xprintf("Only one initial basis file allowed\n");
return 1;
}
csa->ini_file = argv[k];
}
else if (p("--steep"))
csa->smcp.pricing = GLP_PT_PSE;
else if (p("--nosteep"))
csa->smcp.pricing = GLP_PT_STD;
else if (p("--relax"))
csa->smcp.r_test = GLP_RT_HAR;
else if (p("--norelax"))
csa->smcp.r_test = GLP_RT_STD;
else if (p("--presol"))
csa->smcp.presolve = GLP_ON;
else if (p("--nopresol"))
csa->smcp.presolve = GLP_OFF;
else if (p("--exact"))
csa->exact = 1;
else if (p("--xcheck"))
csa->xcheck = 1;
else if (p("--nord"))
csa->iptcp.ord_alg = GLP_ORD_NONE;
else if (p("--qmd"))
csa->iptcp.ord_alg = GLP_ORD_QMD;
else if (p("--amd"))
csa->iptcp.ord_alg = GLP_ORD_AMD;
else if (p("--symamd"))
csa->iptcp.ord_alg = GLP_ORD_SYMAMD;
else if (p("--nomip"))
csa->nomip = 1;
else if (p("--first"))
csa->iocp.br_tech = GLP_BR_FFV;
else if (p("--last"))
csa->iocp.br_tech = GLP_BR_LFV;
else if (p("--drtom"))
csa->iocp.br_tech = GLP_BR_DTH;
else if (p("--mostf"))
csa->iocp.br_tech = GLP_BR_MFV;
else if (p("--pcost"))
csa->iocp.br_tech = GLP_BR_PCH;
else if (p("--dfs"))
csa->iocp.bt_tech = GLP_BT_DFS;
else if (p("--bfs"))
csa->iocp.bt_tech = GLP_BT_BFS;
else if (p("--bestp"))
csa->iocp.bt_tech = GLP_BT_BPH;
else if (p("--bestb"))
csa->iocp.bt_tech = GLP_BT_BLB;
else if (p("--intopt"))
csa->iocp.presolve = GLP_ON;
else if (p("--nointopt"))
csa->iocp.presolve = GLP_OFF;
else if (p("--binarize"))
csa->iocp.presolve = csa->iocp.binarize = GLP_ON;
else if (p("--fpump"))
csa->iocp.fp_heur = GLP_ON;
else if (p("--gomory"))
csa->iocp.gmi_cuts = GLP_ON;
else if (p("--mir"))
csa->iocp.mir_cuts = GLP_ON;
else if (p("--cover"))
csa->iocp.cov_cuts = GLP_ON;
else if (p("--clique"))
csa->iocp.clq_cuts = GLP_ON;
else if (p("--cuts"))
csa->iocp.gmi_cuts = csa->iocp.mir_cuts =
csa->iocp.cov_cuts = csa->iocp.clq_cuts = GLP_ON;
else if (p("--mipgap"))
{ double mip_gap;
k++;
if (k == argc || argv[k][0] == '\0' || argv[k][0] == '-')
{ xprintf("No relative gap tolerance specified\n");
return 1;
}
if (str2num(argv[k], &mip_gap) || mip_gap < 0.0)
{ xprintf("Invalid relative mip gap tolerance `%s'\n",
argv[k]);
return 1;
}
csa->iocp.mip_gap = mip_gap;
}
#if 1 /* 15/VIII-2011 */
else if (p("--minisat"))
csa->minisat = 1;
else if (p("--objbnd"))
{ k++;
if (k == argc || argv[k][0] == '\0' ||
argv[k][0] == '-' && !isdigit((unsigned char)argv[k][1]))
{ xprintf("No objective bound specified\n");
return 1;
}
csa->minisat = 1;
csa->use_bnd = 1;
if (str2int(argv[k], &csa->obj_bnd))
{ xprintf("Invalid objective bound `%s' (should be integer"
" value)\n", argv[k]);
return 1;
}
}
#endif
else if (argv[k][0] == '-' ||
(argv[k][0] == '-' && argv[k][1] == '-'))
{ xprintf("Invalid option `%s'; try %s --help\n",
argv[k], argv[0]);
return 1;
}
else
{ if (csa->in_file != NULL)
{ xprintf("Only one input problem file allowed\n");
return 1;
}
csa->in_file = argv[k];
}
}
#undef p
return 0;
}
typedef struct { double rhs, pi; } v_data;
typedef struct { double low, cap, cost, x; } a_data;
int glp_main(int argc, const char *argv[])
{ /* stand-alone LP/MIP solver */
struct csa _csa, *csa = &_csa;
int ret;
glp_long start;
/* perform initialization */
csa->prob = glp_create_prob();
glp_get_bfcp(csa->prob, &csa->bfcp);
glp_init_smcp(&csa->smcp);
csa->smcp.presolve = GLP_ON;
glp_init_iptcp(&csa->iptcp);
glp_init_iocp(&csa->iocp);
csa->iocp.presolve = GLP_ON;
csa->tran = NULL;
csa->graph = NULL;
csa->format = FMT_MPS_FILE;
csa->in_file = NULL;
csa->ndf = 0;
csa->out_dpy = NULL;
csa->seed = 1;
csa->solution = SOL_BASIC;
csa->in_res = NULL;
csa->dir = 0;
csa->scale = 1;
csa->out_sol = NULL;
csa->out_res = NULL;
csa->out_ranges = NULL;
csa->check = 0;
csa->new_name = NULL;
csa->out_mps = NULL;
csa->out_freemps = NULL;
csa->out_cpxlp = NULL;
csa->out_glp = NULL;
csa->out_pb = NULL;
csa->out_npb = NULL;
#if 1 /* 06/VIII-2011 */
csa->out_cnf = NULL;
#endif
csa->log_file = NULL;
csa->crash = USE_ADV_BASIS;
csa->ini_file = NULL;
csa->exact = 0;
csa->xcheck = 0;
csa->nomip = 0;
#if 1 /* 15/VIII-2011 */
csa->minisat = 0;
csa->use_bnd = 0;
csa->obj_bnd = 0;
#endif
/* parse command-line parameters */
ret = parse_cmdline(csa, argc, argv);
if (ret < 0)
{ ret = EXIT_SUCCESS;
goto done;
}
if (ret > 0)
{ ret = EXIT_FAILURE;
goto done;
}
/*--------------------------------------------------------------*/
/* remove all output files specified in the command line */
if (csa->out_dpy != NULL) remove(csa->out_dpy);
if (csa->out_sol != NULL) remove(csa->out_sol);
if (csa->out_res != NULL) remove(csa->out_res);
if (csa->out_ranges != NULL) remove(csa->out_ranges);
if (csa->out_mps != NULL) remove(csa->out_mps);
if (csa->out_freemps != NULL) remove(csa->out_freemps);
if (csa->out_cpxlp != NULL) remove(csa->out_cpxlp);
if (csa->out_glp != NULL) remove(csa->out_glp);
if (csa->out_pb != NULL) remove(csa->out_pb);
if (csa->out_npb != NULL) remove(csa->out_npb);
#if 1 /* 06/VIII-2011 */
if (csa->out_cnf != NULL) remove(csa->out_cnf);
#endif
if (csa->log_file != NULL) remove(csa->log_file);
/*--------------------------------------------------------------*/
/* open log file, if required */
if (csa->log_file != NULL)
{ if (glp_open_tee(csa->log_file))
{ xprintf("Unable to create log file\n");
ret = EXIT_FAILURE;
goto done;
}
}
/*--------------------------------------------------------------*/
/* print version information */
print_version(1);
/*--------------------------------------------------------------*/
/* print parameters specified in the command line */
if (argc > 1)
{ int k, len = INT_MAX;
xprintf("Parameter(s) specified in the command line:");
for (k = 1; k < argc; k++)
{ if (len > 72)
xprintf("\n"), len = 0;
xprintf(" %s", argv[k]);
len += 1 + strlen(argv[k]);
}
xprintf("\n");
}
/*--------------------------------------------------------------*/
/* read problem data from the input file */
if (csa->in_file == NULL)
{ xprintf("No input problem file specified; try %s --help\n",
argv[0]);
ret = EXIT_FAILURE;
goto done;
}
if (csa->format == FMT_MPS_DECK)
{ ret = glp_read_mps(csa->prob, GLP_MPS_DECK, NULL,
csa->in_file);
if (ret != 0)
err1: { xprintf("MPS file processing error\n");
ret = EXIT_FAILURE;
goto done;
}
}
else if (csa->format == FMT_MPS_FILE)
{ ret = glp_read_mps(csa->prob, GLP_MPS_FILE, NULL,
csa->in_file);
if (ret != 0) goto err1;
}
else if (csa->format == FMT_LP)
{ ret = glp_read_lp(csa->prob, NULL, csa->in_file);
if (ret != 0)
{ xprintf("CPLEX LP file processing error\n");
ret = EXIT_FAILURE;
goto done;
}
}
else if (csa->format == FMT_GLP)
{ ret = glp_read_prob(csa->prob, 0, csa->in_file);
if (ret != 0)
{ xprintf("GLPK LP/MIP file processing error\n");
ret = EXIT_FAILURE;
goto done;
}
}
else if (csa->format == FMT_MATHPROG)
{ int k;
/* allocate the translator workspace */
csa->tran = glp_mpl_alloc_wksp();
/* set seed value */
if (csa->seed == 0x80000000)
{ csa->seed = glp_time().lo;
xprintf("Seed value %d will be used\n", csa->seed);
}
_glp_mpl_init_rand(csa->tran, csa->seed);
/* read model section and optional data section */
if (glp_mpl_read_model(csa->tran, csa->in_file, csa->ndf > 0))
err2: { xprintf("MathProg model processing error\n");
ret = EXIT_FAILURE;
goto done;
}
/* read optional data section(s), if necessary */
for (k = 1; k <= csa->ndf; k++)
{ if (glp_mpl_read_data(csa->tran, csa->in_data[k]))
goto err2;
}
/* generate the model */
if (glp_mpl_generate(csa->tran, csa->out_dpy)) goto err2;
/* build the problem instance from the model */
glp_mpl_build_prob(csa->tran, csa->prob);
}
else if (csa->format == FMT_MIN_COST)
{ csa->graph = glp_create_graph(sizeof(v_data), sizeof(a_data));
ret = glp_read_mincost(csa->graph, offsetof(v_data, rhs),
offsetof(a_data, low), offsetof(a_data, cap),
offsetof(a_data, cost), csa->in_file);
if (ret != 0)
{ xprintf("DIMACS file processing error\n");
ret = EXIT_FAILURE;
goto done;
}
glp_mincost_lp(csa->prob, csa->graph, GLP_ON,
offsetof(v_data, rhs), offsetof(a_data, low),
offsetof(a_data, cap), offsetof(a_data, cost));
glp_set_prob_name(csa->prob, csa->in_file);
}
else if (csa->format == FMT_MAX_FLOW)
{ int s, t;
csa->graph = glp_create_graph(sizeof(v_data), sizeof(a_data));
ret = glp_read_maxflow(csa->graph, &s, &t,
offsetof(a_data, cap), csa->in_file);
if (ret != 0)
{ xprintf("DIMACS file processing error\n");
ret = EXIT_FAILURE;
goto done;
}
glp_maxflow_lp(csa->prob, csa->graph, GLP_ON, s, t,
offsetof(a_data, cap));
glp_set_prob_name(csa->prob, csa->in_file);
}
#if 1 /* 06/VIII-2011 */
else if (csa->format == FMT_CNF)
{ ret = glp_read_cnfsat(csa->prob, csa->in_file);
if (ret != 0)
{ xprintf("DIMACS file processing error\n");
ret = EXIT_FAILURE;
goto done;
}
glp_set_prob_name(csa->prob, csa->in_file);
}
#endif
else
xassert(csa != csa);
/*--------------------------------------------------------------*/
/* change problem name, if required */
if (csa->new_name != NULL)
glp_set_prob_name(csa->prob, csa->new_name);