/
MOI_callbacks.jl
463 lines (436 loc) · 14.3 KB
/
MOI_callbacks.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
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
module TestCallbacks
using CPLEX
using Random
using Test
const MOI = CPLEX.MOI
function callback_simple_model()
model = CPLEX.Optimizer()
MOI.set(model, MOI.Silent(), true)
MOI.set(model, MOI.NumberOfThreads(), 1)
MOI.set(model, MOI.RawParameter("CPX_PARAM_PREIND"), 0)
MOI.set(model, MOI.RawParameter("CPX_PARAM_HEURFREQ"), -1)
MOI.Utilities.loadfromstring!(model, """
variables: x, y
maxobjective: y
c1: x in Integer()
c2: y in Integer()
c3: x in Interval(0.0, 2.5)
c4: y in Interval(0.0, 2.5)
""")
x = MOI.get(model, MOI.VariableIndex, "x")
y = MOI.get(model, MOI.VariableIndex, "y")
return model, x, y
end
function callback_knapsack_model()
model = CPLEX.Optimizer()
MOI.set(model, MOI.Silent(), true)
MOI.set(model, MOI.NumberOfThreads(), 1)
MOI.set(model, MOI.RawParameter("CPX_PARAM_PREIND"), 0)
MOI.set(model, MOI.RawParameter("CPX_PARAM_HEURFREQ"), -1)
N = 30
x = MOI.add_variables(model, N)
MOI.add_constraints(model, MOI.SingleVariable.(x), MOI.ZeroOne())
MOI.set.(model, MOI.VariablePrimalStart(), x, 0.0)
Random.seed!(1)
item_weights, item_values = rand(N), rand(N)
MOI.add_constraint(
model,
MOI.ScalarAffineFunction(MOI.ScalarAffineTerm.(item_weights, x), 0.0),
MOI.LessThan(10.0)
)
MOI.set(
model,
MOI.ObjectiveFunction{MOI.ScalarAffineFunction{Float64}}(),
MOI.ScalarAffineFunction(MOI.ScalarAffineTerm.(item_values, x), 0.0)
)
MOI.set(model, MOI.ObjectiveSense(), MOI.MAX_SENSE)
return model, x, item_weights
end
function test_LazyConstraint()
model, x, y = callback_simple_model()
lazy_called = false
MOI.set(model, MOI.LazyConstraintCallback(), cb_data -> begin
lazy_called = true
x_val = MOI.get(model, MOI.CallbackVariablePrimal(cb_data), x)
y_val = MOI.get(model, MOI.CallbackVariablePrimal(cb_data), y)
@test MOI.supports(model, MOI.LazyConstraint(cb_data))
if y_val - x_val > 1 + 1e-6
MOI.submit(
model,
MOI.LazyConstraint(cb_data),
MOI.ScalarAffineFunction{Float64}(
MOI.ScalarAffineTerm.([-1.0, 1.0], [x, y]),
0.0
),
MOI.LessThan{Float64}(1.0)
)
elseif y_val + x_val > 3 + 1e-6
MOI.submit(
model,
MOI.LazyConstraint(cb_data),
MOI.ScalarAffineFunction{Float64}(
MOI.ScalarAffineTerm.([1.0, 1.0], [x, y]),
0.0
), MOI.LessThan{Float64}(3.0)
)
end
end)
@test MOI.supports(model, MOI.LazyConstraintCallback())
MOI.optimize!(model)
@test lazy_called
@test MOI.get(model, MOI.VariablePrimal(), x) == 1
@test MOI.get(model, MOI.VariablePrimal(), y) == 2
end
function test_OptimizeInProgress()
model, x, y = callback_simple_model()
MOI.set(model, MOI.LazyConstraintCallback(), cb_data -> begin
@test_throws(
MOI.OptimizeInProgress(MOI.VariablePrimal()),
MOI.get(model, MOI.VariablePrimal(), x)
)
@test_throws(
MOI.OptimizeInProgress(MOI.ObjectiveValue()),
MOI.get(model, MOI.ObjectiveValue())
)
@test_throws(
MOI.OptimizeInProgress(MOI.ObjectiveBound()),
MOI.get(model, MOI.ObjectiveBound())
)
end)
MOI.optimize!(model)
end
function test_LazyConstraint_UserCut()
model, x, y = callback_simple_model()
cb = nothing
MOI.set(model, MOI.LazyConstraintCallback(), cb_data -> begin
cb = cb_data
MOI.submit(
model,
MOI.UserCut(cb_data),
MOI.ScalarAffineFunction([MOI.ScalarAffineTerm(1.0, x)], 0.0),
MOI.LessThan(2.0)
)
end)
@test_throws(
MOI.InvalidCallbackUsage(
MOI.LazyConstraintCallback(),
MOI.UserCut(cb)
),
MOI.optimize!(model)
)
end
function test_LazyConstraint_HeuristicSolution()
model, x, y = callback_simple_model()
cb = nothing
MOI.set(model, MOI.LazyConstraintCallback(), cb_data -> begin
cb = cb_data
MOI.submit(
model,
MOI.HeuristicSolution(cb_data),
[x],
[2.0]
)
end)
@test_throws(
MOI.InvalidCallbackUsage(
MOI.LazyConstraintCallback(),
MOI.HeuristicSolution(cb)
),
MOI.optimize!(model)
)
end
function test_UserCut()
model, x, item_weights = callback_knapsack_model()
user_cut_submitted = false
MOI.set(model, MOI.UserCutCallback(), cb_data -> begin
terms = MOI.ScalarAffineTerm{Float64}[]
accumulated = 0.0
for (i, xi) in enumerate(x)
if MOI.get(model, MOI.CallbackVariablePrimal(cb_data), xi) > 0.0
push!(terms, MOI.ScalarAffineTerm(1.0, xi))
accumulated += item_weights[i]
end
end
@test MOI.supports(model, MOI.UserCut(cb_data))
if accumulated > 10.0
MOI.submit(
model,
MOI.UserCut(cb_data),
MOI.ScalarAffineFunction{Float64}(terms, 0.0),
MOI.LessThan{Float64}(length(terms) - 1)
)
user_cut_submitted = true
end
end)
@test MOI.supports(model, MOI.UserCutCallback())
MOI.optimize!(model)
@test user_cut_submitted
end
function test_UserCut_LazyConstraint()
model, x, item_weights = callback_knapsack_model()
cb = nothing
MOI.set(model, MOI.UserCutCallback(), cb_data -> begin
cb = cb_data
MOI.submit(
model,
MOI.LazyConstraint(cb_data),
MOI.ScalarAffineFunction(MOI.ScalarAffineTerm.(1.0, x), 0.0),
MOI.LessThan(5.0)
)
end)
@test_throws(
MOI.InvalidCallbackUsage(
MOI.UserCutCallback(),
MOI.LazyConstraint(cb)
),
MOI.optimize!(model)
)
end
function test_UserCut_HeuristicSolution()
model, x, item_weights = callback_knapsack_model()
cb = nothing
MOI.set(model, MOI.UserCutCallback(), cb_data -> begin
cb = cb_data
MOI.submit(
model,
MOI.HeuristicSolution(cb_data),
[x[1]],
[0.0]
)
end)
@test_throws(
MOI.InvalidCallbackUsage(
MOI.UserCutCallback(),
MOI.HeuristicSolution(cb)
),
MOI.optimize!(model)
)
end
function test_Heuristic()
model, x, item_weights = callback_knapsack_model()
callback_called = false
MOI.set(model, MOI.HeuristicCallback(), cb_data -> begin
x_vals = MOI.get.(model, MOI.CallbackVariablePrimal(cb_data), x)
@test MOI.supports(model, MOI.HeuristicSolution(cb_data))
@test MOI.submit(
model,
MOI.HeuristicSolution(cb_data),
x,
floor.(x_vals)
) == MOI.HEURISTIC_SOLUTION_UNKNOWN
callback_called = true
end)
@test MOI.supports(model, MOI.HeuristicCallback())
MOI.optimize!(model)
@test callback_called
end
function test_Heuristic_LazyConstraint()
model, x, item_weights = callback_knapsack_model()
cb = nothing
MOI.set(model, MOI.HeuristicCallback(), cb_data -> begin
cb = cb_data
MOI.submit(
model,
MOI.LazyConstraint(cb_data),
MOI.ScalarAffineFunction(MOI.ScalarAffineTerm.(1.0, x), 0.0),
MOI.LessThan(5.0)
)
end)
@test_throws(
MOI.InvalidCallbackUsage(
MOI.HeuristicCallback(),
MOI.LazyConstraint(cb)
),
MOI.optimize!(model)
)
end
function test_Heuristic_UserCut()
model, x, item_weights = callback_knapsack_model()
cb = nothing
MOI.set(model, MOI.HeuristicCallback(), cb_data -> begin
cb = cb_data
MOI.submit(
model,
MOI.UserCut(cb_data),
MOI.ScalarAffineFunction(MOI.ScalarAffineTerm.(1.0, x), 0.0),
MOI.LessThan(5.0)
)
end)
@test_throws(
MOI.InvalidCallbackUsage(
MOI.HeuristicCallback(),
MOI.UserCut(cb)
),
MOI.optimize!(model)
)
end
function test_CallbackFunction_OptimizeInProgress()
model, x, y = callback_simple_model()
MOI.set(model, CPLEX.CallbackFunction(), (cb_data, cb_context) -> begin
@test_throws(
MOI.OptimizeInProgress(MOI.VariablePrimal()),
MOI.get(model, MOI.VariablePrimal(), x)
)
@test_throws(
MOI.OptimizeInProgress(MOI.ObjectiveValue()),
MOI.get(model, MOI.ObjectiveValue())
)
@test_throws(
MOI.OptimizeInProgress(MOI.ObjectiveBound()),
MOI.get(model, MOI.ObjectiveBound())
)
end)
@test MOI.supports(model, CPLEX.CallbackFunction())
MOI.optimize!(model)
end
function test_CallbackFunction_LazyConstraint()
model, x, y = callback_simple_model()
cb_calls = Clong[]
function callback_function(
cb_data::CPLEX.CallbackContext, cb_context::Clong
)
push!(cb_calls, cb_context)
if cb_context != CPX_CALLBACKCONTEXT_CANDIDATE
return
end
CPLEX.load_callback_variable_primal(cb_data, cb_context)
x_val = MOI.get(model, MOI.CallbackVariablePrimal(cb_data), x)
y_val = MOI.get(model, MOI.CallbackVariablePrimal(cb_data), y)
if y_val - x_val > 1 + 1e-6
MOI.submit(model, MOI.LazyConstraint(cb_data),
MOI.ScalarAffineFunction{Float64}(
MOI.ScalarAffineTerm.([-1.0, 1.0], [x, y]),
0.0
),
MOI.LessThan{Float64}(1.0)
)
elseif y_val + x_val > 3 + 1e-6
MOI.submit(model, MOI.LazyConstraint(cb_data),
MOI.ScalarAffineFunction{Float64}(
MOI.ScalarAffineTerm.([1.0, 1.0], [x, y]),
0.0
),
MOI.LessThan{Float64}(3.0)
)
end
end
MOI.set(model, CPLEX.CallbackFunction(), callback_function)
MOI.optimize!(model)
@test MOI.get(model, MOI.VariablePrimal(), x) == 1
@test MOI.get(model, MOI.VariablePrimal(), y) == 2
@test length(cb_calls) > 0
end
function test_CallbackFunction_UserCut()
model, x, item_weights = callback_knapsack_model()
user_cut_submitted = false
cb_calls = Clong[]
MOI.set(model, CPLEX.CallbackFunction(), (cb_data, cb_context) -> begin
push!(cb_calls, cb_context)
if cb_context != CPX_CALLBACKCONTEXT_RELAXATION
return
end
CPLEX.load_callback_variable_primal(cb_data, cb_context)
terms = MOI.ScalarAffineTerm{Float64}[]
accumulated = 0.0
for (i, xi) in enumerate(x)
if MOI.get(model, MOI.CallbackVariablePrimal(cb_data), xi) > 0.0
push!(terms, MOI.ScalarAffineTerm(1.0, xi))
accumulated += item_weights[i]
end
end
if accumulated > 10.0
MOI.submit(
model,
MOI.UserCut(cb_data),
MOI.ScalarAffineFunction{Float64}(terms, 0.0),
MOI.LessThan{Float64}(length(terms) - 1)
)
user_cut_submitted = true
end
end)
MOI.optimize!(model)
@test user_cut_submitted
end
function test_CallbackFunction_HeuristicSolution()
model, x, item_weights = callback_knapsack_model()
callback_called = false
cb_calls = Clong[]
MOI.set(model, CPLEX.CallbackFunction(), (cb_data, cb_context) -> begin
push!(cb_calls, cb_context)
if cb_context != CPX_CALLBACKCONTEXT_RELAXATION
return
end
CPLEX.load_callback_variable_primal(cb_data, cb_context)
x_vals = MOI.get.(model, MOI.CallbackVariablePrimal(cb_data), x)
@test MOI.submit(
model,
MOI.HeuristicSolution(cb_data),
x,
floor.(x_vals)
) == MOI.HEURISTIC_SOLUTION_UNKNOWN
callback_called = true
end)
MOI.optimize!(model)
@test callback_called
end
function test_CPXcallbackabort()
model = CPLEX.Optimizer()
MOI.set(model, MOI.Silent(), true)
x = MOI.add_variable(model)
MOI.add_constraint(model, MOI.SingleVariable(x), MOI.Integer())
MOI.set(model, MOI.NumberOfThreads(), 1)
MOI.set(model, CPLEX.CallbackFunction(), (cb_data, context_id) -> begin
@show context_id
CPXcallbackabort(cb_data)
end)
MOI.optimize!(model)
@test MOI.get(model, MOI.TerminationStatus()) == MOI.INTERRUPTED
end
"""
test_InterruptException()
This test simulates an InterruptException being thrown. It is a little
complicated due to the delayed handling of `terminate`, which _schedules_ a
request for termination, rather than terminating immediately. This means CPLEX
may continue to call the callback after the interruption.
First, we must ensure that InterruptException() is only thrown once. Double
interrupting would interrupt our handling of the first interrupt!
Second, if the model is too simplisitic, CPLEX may be able to prove optimality
after we have interrupted, but before it has decided to actually exit the solve.
"""
function test_InterruptException()
model = CPLEX.Optimizer()
MOI.set(model, MOI.Silent(), true)
x = MOI.add_variable(model)
MOI.add_constraint(model, MOI.SingleVariable(x), MOI.Integer())
MOI.set(model, MOI.ObjectiveSense(), MOI.MIN_SENSE)
MOI.set(
model,
MOI.ObjectiveFunction{MOI.SingleVariable}(),
MOI.SingleVariable(x),
)
MOI.set(model, MOI.NumberOfThreads(), 1)
i = 0.0
interrupt_thrown = false
MOI.set(model, CPLEX.CallbackFunction(), (cb_data, cb_where) -> begin
if cb_where != CPLEX.CPX_CALLBACKCONTEXT_CANDIDATE
return
end
MOI.submit(
model,
MOI.LazyConstraint(cb_data),
MOI.ScalarAffineFunction{Float64}(
[MOI.ScalarAffineTerm(1.0, x)], 0.0
),
MOI.GreaterThan{Float64}(i)
)
i += 1
if !interrupt_thrown
interrupt_thrown = true
throw(InterruptException())
end
end)
MOI.optimize!(model)
@test MOI.get(model, MOI.TerminationStatus()) == MOI.INTERRUPTED
end
end # module TestCallbacks
runtests(TestCallbacks)