forked from jump-dev/Gurobi.jl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
grb_params.jl
336 lines (303 loc) · 7.76 KB
/
grb_params.jl
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
# Gurobi solver parameters
# This list was obtained through AWK (with Gurobi 5.6)
# command:
#
# grep GRB_INT_PAR gurobi_c.h | awk '{ printf("%s,\n", $3) }'
#
const GRB_INT_PARAMS =[
"BarIterLimit",
"SolutionLimit",
"Method",
"ScaleFlag",
"SimplexPricing",
"Quad",
"NormAdjust",
"Sifting",
"SiftMethod",
"BarCorrectors",
"BarHomogeneous",
"BarOrder",
"Crossover",
"CrossoverBasis",
"BranchDir",
"DegenMoves",
"Disconnected",
"MinRelNodes",
"MIPFocus",
"NodeMethod",
"NoRelHeuristic",
"PumpPasses",
"RINS",
"SubMIPNodes",
"Symmetry",
"VarBranch",
"SolutionNumber",
"ZeroObjNodes",
"Cuts",
"CliqueCuts",
"CoverCuts",
"FlowCoverCuts",
"FlowPathCuts",
"GUBCoverCuts",
"ImpliedCuts",
"ProjImpliedCuts",
"MIPSepCuts",
"MIRCuts",
"StrongCGCuts",
"ModKCuts",
"ZeroHalfCuts",
"NetworkCuts",
"SubMIPCuts",
"InfProofCuts",
"CutAggPasses",
"CutPasses",
"GomoryPasses",
"WorkerPort",
"Aggregate",
"AggFill",
"ConcurrentMIP",
"ConcurrentMIPJobs", # deprecated after Gurobi7
"ConcurrentJobs",
"DisplayInterval",
"DistributedMIPJobs",
"DualReductions",
"IISMethod",
"InfUnbdInfo",
"LazyConstraints",
"LogToConsole",
"MIQCPMethod",
"NumericFocus",
"NonBlocking", # deprecated after Gurobi7
"OutputFlag",
"PreCrush",
"PreDepRow",
"PreDual",
"PrePasses",
"PreQLinearize",
"Presolve",
"PreSparsify",
"PreMIQCPForm",
"QCPDual",
"Record",
"Seed",
"Threads",
"TuneResults",
"TuneCriterion",
"TuneTrials",
"TuneOutput",
"TuneJobs",
"PreMIQPMethod", # deprecated after Gurobi7
"UpdateMode",
"ObjNumber",
"MultiObjMethod",
"MultiObjPre",
"PoolSolutions",
"PoolSearchMode",
"NonConvex",
]
# This list was obtained through AWK (with Gurobi 5.6)
# command:
#
# grep GRB_DBL_PAR gurobi_c.h | awk '{ printf("%s,\n", $3) }'
#
const GRB_DBL_PARAMS = [
"Cutoff",
"IterationLimit",
"NodeLimit",
"TimeLimit",
"BestObjStop",
"BestBdStop",
"FeasibilityTol",
"IntFeasTol",
"MarkowitzTol",
"MIPGap",
"MIPGapAbs",
"OptimalityTol",
"PSDTol",
"PerturbValue",
"ObjScale",
"BarConvTol",
"BarQCPConvTol",
"Heuristics",
"ImproveStartGap",
"ImproveStartTime",
"ImproveStartNodes",
"NodefileStart",
"FeasRelaxBigM",
"PreSOS1BigM",
"PreSOS2BigM",
"TuneTimeLimit",
"PoolGap"]
# This list was obtained through AWK (with Gurobi 5.6)
# command:
#
# grep GRB_STR_PAR gurobi_c.h | awk '{ printf("%s,\n", $3) }'
#
const GRB_STR_PARAMS = [
"NodefileDir",
"ServerPool",
"ServerPassword",
"LogFile",
"WorkerPool",
"ResultFile",
"Dummy"]
#################################################
#
# Parameter getter & setter
#
#################################################
const GRB_MAX_STRLEN = 512 # gurobi.c define this value as 512
# lower-level functions
function get_int_param(env::Env, name::String)
@assert isascii(name)
a = Ref{Cint}()
ret = @grb_ccall(getintparam, Cint, (Ptr{Cvoid}, Ptr{Cchar}, Ptr{Cint}),
env, name, a)
if ret != 0
throw(GurobiError(env, ret))
end
convert(Int, a[])
end
function get_dbl_param(env::Env, name::String)
@assert isascii(name)
a = Ref{Float64}()
ret = @grb_ccall(getdblparam, Cint, (Ptr{Cvoid}, Ptr{Cchar}, Ptr{Float64}),
env, name, a)
if ret != 0
throw(GurobiError(env, ret))
end
a[]::Float64
end
function get_str_param(env::Env, name::String)
@assert isascii(name)
buf = Array{Cchar}(undef, GRB_MAX_STRLEN)
ret = @grb_ccall(getstrparam, Cint, (Ptr{Cvoid}, Ptr{Cchar}, Ptr{Cchar}),
env, name, buf)
if ret != 0
throw(GurobiError(env, ret))
end
unsafe_string(pointer(buf))
end
function set_int_param!(env::Env, name::String, v::Integer)
@assert isascii(name)
ret = @grb_ccall(setintparam, Cint, (Ptr{Cvoid}, Ptr{Cchar}, Cint),
env, name, v)
if ret != 0
throw(GurobiError(env, ret))
end
end
function set_dbl_param!(env::Env, name::String, v::Real)
@assert isascii(name)
ret = @grb_ccall(setdblparam, Cint, (Ptr{Cvoid}, Ptr{Cchar}, Float64),
env, name, v)
if ret != 0
throw(GurobiError(env, ret))
end
end
function set_str_param!(env::Env, name::String, v::String)
@assert isascii(name)
@assert isascii(v)
ret = @grb_ccall(setstrparam, Cint, (Ptr{Cvoid}, Ptr{Cchar}, Ptr{Cchar}),
env, name, v)
if ret != 0
throw(GurobiError(env, ret))
end
end
# for existing models
function get_int_param(m::Model, name::String)
@assert isascii(name)
modenv = @grb_ccall(getenv, Ptr{Cvoid}, (Ptr{Cvoid},), m)
@assert modenv != C_NULL
a = Ref{Cint}()
ret = @grb_ccall(getintparam, Cint, (Ptr{Cvoid}, Ptr{Cchar}, Ptr{Cint}),
modenv, name, a)
if ret != 0
throw(GurobiError(Env(modenv), ret))
end
convert(Int, a[])
end
function get_dbl_param(m::Model, name::String)
@assert isascii(name)
modenv = @grb_ccall(getenv, Ptr{Cvoid}, (Ptr{Cvoid},), m)
@assert modenv != C_NULL
a = Ref{Float64}()
ret = @grb_ccall(getdblparam, Cint, (Ptr{Cvoid}, Ptr{Cchar}, Ptr{Float64}),
modenv, name, a)
if ret != 0
throw(GurobiError(Env(modenv), ret))
end
a[]::Float64
end
function get_str_param(m::Model, name::String)
@assert isascii(name)
modenv = @grb_ccall(getenv, Ptr{Cvoid}, (Ptr{Cvoid},), m)
@assert modenv != C_NULL
buf = Array{Cchar}(undef, GRB_MAX_STRLEN)
ret = @grb_ccall(getstrparam, Cint, (Ptr{Cvoid}, Ptr{Cchar}, Ptr{Cchar}),
modenv, name, buf)
if ret != 0
throw(GurobiError(Env(modenv), ret))
end
unsafe_string(pointer(buf))
end
function set_int_param!(m::Model, name::String, v::Integer)
@assert isascii(name)
modenv = @grb_ccall(getenv, Ptr{Cvoid}, (Ptr{Cvoid},), m)
@assert modenv != C_NULL
ret = @grb_ccall(setintparam, Cint, (Ptr{Cvoid}, Ptr{Cchar}, Cint),
modenv, name, v)
if ret != 0
throw(GurobiError(Env(modenv), ret))
end
end
function set_dbl_param!(m::Model, name::String, v::Real)
@assert isascii(name)
modenv = @grb_ccall(getenv, Ptr{Cvoid}, (Ptr{Cvoid},), m)
@assert modenv != C_NULL
ret = @grb_ccall(setdblparam, Cint, (Ptr{Cvoid}, Ptr{Cchar}, Float64),
modenv, name, v)
if ret != 0
throw(GurobiError(Env(modenv), ret))
end
end
function set_str_param!(m::Model, name::String, v::String)
@assert isascii(name)
@assert isascii(v)
modenv = @grb_ccall(getenv, Ptr{Cvoid}, (Ptr{Cvoid},), m)
@assert modenv != C_NULL
ret = @grb_ccall(setstrparam, Cint, (Ptr{Cvoid}, Ptr{Cchar}, Ptr{Cchar}),
modenv, name, v)
if ret != 0
throw(GurobiError(Env(modenv), ret))
end
end
# higher level functions
function getparam(env::Union{Env,Model}, name::String)
@assert isascii(name)
if name in GRB_INT_PARAMS
return get_int_param(env, name)
elseif name in GRB_DBL_PARAMS
return get_dbl_param(env, name)
elseif name in GRB_STR_PARAMS
return get_str_param(env, name)
else
error("Unrecognized parameter name: $(name).")
end
end
function setparam!(env::Union{Env,Model}, name::String, v)
@assert isascii(name)
if name in GRB_INT_PARAMS
set_int_param!(env, name, v)
elseif name in GRB_DBL_PARAMS
set_dbl_param!(env, name, v)
elseif name in GRB_STR_PARAMS
set_str_param!(env, name, v)
else
error("Unrecognized parameter name: $(name).")
end
end
function setparams!(env::Union{Env,Model}; args...)
for (name, v) in args
setparam!(env, string(name), v)
end
end