-
Notifications
You must be signed in to change notification settings - Fork 0
/
cpxfbbt_main.c
219 lines (154 loc) · 5.95 KB
/
cpxfbbt_main.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
/*
* Fix point FBBT as a cutting plane in Cplex
*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <sys/time.h>
#include <sys/resource.h>
#include "cplex.h"
#include "cmdline.h"
struct option_s {
int frequency;
int maxDepth;
};
int fixpointfbbt (CPXCENVptr env,
void *cbdata,
int wherefrom,
void *cbhandle,
int *useraction_p);
int main (int argc, char **argv) {
int status, i;
CPXENVptr env = CPXopenCPLEX (&status);
double maxTime;
char
addcuts = 0,
ifHelp = 0;
int presolve;
struct option_s opt;
tpar options [] = {{ 'f', CSTR() "fixpt", 0, &addcuts, TTOGGLE, CSTR() "add fixpoint FBBT (default: off)"}
,{'p', CSTR() "presolve", 1, &presolve, TINT, CSTR() "Use Cplex's presolve (FBBT): 0 is off, 1 is default, 2 is aggressive -- default: 1"}
,{'t', CSTR() "maxtime", -1, &maxTime, TDOUBLE, CSTR() "Maximum CPU time (default: no limit)"}
,{'d', CSTR() "maxdepth", -1, &opt.maxDepth, TINT, CSTR() "Maximum BB depth for applying procedure (default: no limit)"}
,{'q', CSTR() "frequency", 1, &opt.frequency, TINT, CSTR() "Frequency of calls (default: every node if active); negative means stop if first call ineffective"}
,{'h', CSTR() "help", 0, &ifHelp, TTOGGLE, CSTR() "Print this help and exit"}
,{0, CSTR() "", 0, NULL, TTOGGLE, CSTR() ""} // THIS ENTRY ALWAYS AT THE END
};
char **filenames;
set_default_args (options); // default parameter values
filenames = readargs (argc, argv, options); // parse command line
if (ifHelp || (argc < 1) || !filenames)
print_help (argv [0], options);
if (ifHelp || !filenames) {
if (!ifHelp)
printf ("No file specified, exiting\n");
for (i=0; filenames [i]; ++i)
free (filenames [i]);
free (filenames);
return 0;
}
/* Turn on output to the screen */
if (maxTime > 0)
status = CPXsetdblparam (env, CPX_PARAM_TILIM, maxTime);
status = CPXsetintparam (env, CPX_PARAM_SCRIND, CPX_ON);
//status = CPXsetintparam (env, CPX_PARAM_PREIND, CPX_OFF); // turn off presolve. TODO: restore presolve
CPXLPptr mip = CPXcreateprob (env, &status, "cpx+fbbt");
if (mip == NULL) {
printf ("CPXcreateprob, Failed to create LP1, error code %d\n", status);
for (i=0; filenames [i]; ++i)
free (filenames [i]);
free (filenames);
exit (-1);
}
status = CPXreadcopyprob (env, mip, *filenames, NULL); /* Read MIP from file */
if (addcuts)
status = CPXsetusercutcallbackfunc (env, fixpointfbbt, &opt);
/*
status = CPXsetintparam (env, CPX_PARAM_MIPCBREDLP,
CPX_OFF); //UNNECESSARY -- Let MIP callbacks work on the original
model, otherwise CPLEX works on the presolved model
status = CPXsetintparam (env, CPX_PARAM_MIPSEARCH,
CPX_MIPSEARCH_TRADITIONAL); Turn on traditional search for use
with control callbacks
*/
if (presolve &&
(CPXsetintparam (env, CPX_PARAM_IMPLBD, (presolve == 1) ? 0 : 2) || /* 0: let Cpx choose, 2: aggressive */
CPXsetintparam (env, CPX_PARAM_PRESLVND, (presolve == 1) ? 0 : 2) || /* 2: force, 3: force plus probing, 0: let Cplex choose */
CPXsetintparam (env, CPX_PARAM_PREPASS, (presolve == 1) ? -1 : 5) || /* presolve passes: -1: auto, 5 */
CPXsetintparam (env, CPX_PARAM_PREIND, 1)))
printf ("Warning: could not tell Cplex to use presolver");
status = CPXmipopt (env, mip); /* Optimize the problem and obtain solution */
if (status)
printf ("Failed to optimize MIP, error code %d\n", status);
else {
double objval, *x;
int ncols, i, nprint = 0;
status = CPXgetobjval (env, mip, &objval);
if (!status) {
printf ("Solution value = %f\n", objval);
ncols = CPXgetnumcols (env, mip);
x = (double *) malloc (ncols * sizeof (double));
status = CPXgetx (env, mip, x, 0, ncols-1);
for (i=0; i<ncols; ++i) {
if (fabs (x [i]) > 1e-6) {
printf ("(%d,%g) ", i, x [i]);
if (!(++nprint % 10))
printf ("\n");
}
}
if (++nprint % 10)
printf ("\n");
free (x);
} else
printf ("Could not retrieve solution");
}
// print first part of the output line
{
struct rusage usage;
double lb, ub;
int status;
char
*summary = "unknown",
ubs [50] = "inf";
CPXgetobjval (env, mip, &ub);
CPXgetbestobjval (env, mip, &lb);
if (getrusage (RUSAGE_SELF, &usage)) {
fprintf (stderr, "Error: can't run getrusage\n");
exit (-1);
}
printf ("Stats: full,0.%d,%s,%d,%d,%d,-1,-1,-1,-1,%g,-1,%g,%g,%d,-1,-1,-1,-1,-1,-1,-1,",
addcuts,
*filenames,
CPXgetnumcols (env, mip),
CPXgetnumint (env, mip) +
CPXgetnumbin (env, mip),
CPXgetnumrows (env, mip),
usage.ru_utime.tv_sec + (double) usage.ru_utime.tv_usec * 1.e-6,
lb,
ub,
CPXgetnodecnt (env, mip));
// print second part
fixpointfbbt (env, NULL, 0, NULL, NULL);
// print final two strings
CPXgetobjval (env, mip, &ub);
if (ub < 1e19)
sprintf (ubs, "%g", ub);
status = CPXgetstat (env, mip);
switch (status) {
case CPXMIP_OPTIMAL: summary = "done"; break;
case CPXMIP_INFEASIBLE: summary = "infeasible"; break;
case CPXMIP_UNBOUNDED: summary = "unbounded"; break;
case CPXMIP_TIME_LIM_FEAS:
case CPXMIP_TIME_LIM_INFEAS: summary = "timelimit"; break;
default: break;
}
printf ("%s,%s\n",summary,ubs);
}
if (mip != NULL) status = CPXfreeprob (env, &mip);
if (env != NULL) status = CPXcloseCPLEX (&env);
for (i=0; filenames [i]; ++i)
free (filenames [i]);
free (filenames);
return status;
}