-
Notifications
You must be signed in to change notification settings - Fork 99
Expand file tree
/
Copy pathSlackBridge.jl
More file actions
607 lines (573 loc) · 21.8 KB
/
SlackBridge.jl
File metadata and controls
607 lines (573 loc) · 21.8 KB
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
# Copyright (c) 2017: Miles Lubin and contributors
# Copyright (c) 2017: Google Inc.
#
# Use of this source code is governed by an MIT-style license that can be found
# in the LICENSE.md file or at https://opensource.org/licenses/MIT.
module TestObjectiveSlack
using Test
import MathOptInterface as MOI
function runtests()
for name in names(@__MODULE__; all = true)
if startswith("$(name)", "test_")
@testset "$(name)" begin
getfield(@__MODULE__, name)()
end
end
end
return
end
include("../utilities.jl")
function test_SlackBridge_ObjectiveSense_error()
inner = MOI.Utilities.MockOptimizer(MOI.Utilities.Model{Float64}())
model = MOI.Bridges.Objective.Slack{Float64}(inner)
x = MOI.add_variable(model)
f = MOI.ScalarAffineFunction([MOI.ScalarAffineTerm(1.1, x)], 1.2)
attr = MOI.ObjectiveFunction{typeof(f)}()
@test_throws(
MOI.SetAttributeNotAllowed(
attr,
"Set `MOI.ObjectiveSense` before `MOI.ObjectiveFunction` when " *
"using `MOI.Bridges.Objective.SlackBridge`.",
),
MOI.set(model, attr, f),
)
return
end
function test_SlackBridge_ObjectiveSense_modify()
inner = MOI.Utilities.MockOptimizer(MOI.Utilities.Model{Float64}())
model = MOI.Bridges.Objective.Slack{Float64}(inner)
x = MOI.add_variable(model)
f = MOI.ScalarAffineFunction([MOI.ScalarAffineTerm(1.1, x)], -1.2)
MOI.set(model, MOI.ObjectiveSense(), MOI.MIN_SENSE)
MOI.set(model, MOI.ObjectiveFunction{typeof(f)}(), f)
@test_throws(
MOI.SetAttributeNotAllowed,
MOI.set(model, MOI.ObjectiveSense(), MOI.MAX_SENSE),
)
MOI.set(model, MOI.ObjectiveSense(), MOI.FEASIBILITY_SENSE)
MOI.set(model, MOI.ObjectiveSense(), MOI.MAX_SENSE)
MOI.set(model, MOI.ObjectiveFunction{typeof(f)}(), f)
@test MOI.get(model, MOI.ObjectiveFunction{typeof(f)}()) ≈ f
return
end
function test_SlackBridge_ObjectiveFunction_modify()
inner = MOI.Utilities.MockOptimizer(MOI.Utilities.Model{Float64}())
model = MOI.Bridges.Objective.Slack{Float64}(inner)
x = MOI.add_variable(model)
f = MOI.ScalarAffineFunction([MOI.ScalarAffineTerm(1.1, x)], -1.2)
MOI.set(model, MOI.ObjectiveSense(), MOI.MIN_SENSE)
MOI.set(model, MOI.ObjectiveFunction{typeof(f)}(), f)
@test MOI.get(model, MOI.ObjectiveFunction{typeof(f)}()) ≈ f
g = MOI.ScalarAffineFunction([MOI.ScalarAffineTerm(-1.1, x)], 1.2)
MOI.set(model, MOI.ObjectiveFunction{typeof(g)}(), g)
@test MOI.get(model, MOI.ObjectiveFunction{typeof(g)}()) ≈ g
return
end
function test_SlackBridge_get_ObjectiveFunction_MIN()
inner = MOI.Utilities.MockOptimizer(MOI.Utilities.Model{Float64}())
model = MOI.Bridges.Objective.Slack{Float64}(inner)
x = MOI.add_variable(model)
f = MOI.ScalarAffineFunction([MOI.ScalarAffineTerm(1.1, x)], -1.2)
MOI.set(model, MOI.ObjectiveSense(), MOI.MIN_SENSE)
MOI.set(model, MOI.ObjectiveFunction{typeof(f)}(), f)
@test MOI.get(model, MOI.ObjectiveFunction{typeof(f)}()) ≈ f
return
end
function test_SlackBridge_get_ObjectiveFunction_MAX()
inner = MOI.Utilities.MockOptimizer(MOI.Utilities.Model{Float64}())
model = MOI.Bridges.Objective.Slack{Float64}(inner)
x = MOI.add_variable(model)
f = MOI.ScalarAffineFunction([MOI.ScalarAffineTerm(1.1, x)], -1.2)
MOI.set(model, MOI.ObjectiveSense(), MOI.MAX_SENSE)
MOI.set(model, MOI.ObjectiveFunction{typeof(f)}(), f)
@test MOI.get(model, MOI.ObjectiveFunction{typeof(f)}()) ≈ f
return
end
function test_SlackBridge_get_ObjectiveSense()
inner = MOI.Utilities.MockOptimizer(MOI.Utilities.Model{Float64}())
model = MOI.Bridges.Objective.Slack{Float64}(inner)
MOI.set(model, MOI.ObjectiveSense(), MOI.MIN_SENSE)
@test MOI.get(model, MOI.ObjectiveSense()) == MOI.MIN_SENSE
MOI.set(model, MOI.ObjectiveSense(), MOI.MAX_SENSE)
@test MOI.get(model, MOI.ObjectiveSense()) == MOI.MAX_SENSE
return
end
function test_SlackBridge_NumberOfVariables()
inner = MOI.Utilities.MockOptimizer(MOI.Utilities.Model{Float64}())
model = MOI.Bridges.Objective.Slack{Float64}(inner)
x = MOI.add_variable(model)
f = MOI.ScalarAffineFunction([MOI.ScalarAffineTerm(1.1, x)], 1.2)
MOI.set(model, MOI.ObjectiveSense(), MOI.MAX_SENSE)
MOI.set(model, MOI.ObjectiveFunction{typeof(f)}(), f)
@test MOI.get(model, MOI.NumberOfVariables()) == 1
@test MOI.get(inner, MOI.NumberOfVariables()) == 2
MOI.set(model, MOI.ObjectiveSense(), MOI.FEASIBILITY_SENSE)
@test MOI.get(model, MOI.NumberOfVariables()) == 1
@test MOI.get(inner, MOI.NumberOfVariables()) == 1
return
end
function test_SlackBridge_ListOfModelAttributesSet()
inner = MOI.Utilities.MockOptimizer(MOI.Utilities.Model{Float64}())
model = MOI.Bridges.Objective.Slack{Float64}(inner)
x = MOI.add_variable(model)
f = MOI.ScalarAffineFunction([MOI.ScalarAffineTerm(1.1, x)], 1.2)
MOI.set(model, MOI.ObjectiveSense(), MOI.MAX_SENSE)
MOI.set(model, MOI.ObjectiveFunction{typeof(f)}(), f)
attr = MOI.get(model, MOI.ListOfModelAttributesSet())
@test length(attr) == 2
@test MOI.ObjectiveSense() in attr
@test MOI.ObjectiveFunction{typeof(f)}() in attr
attr = MOI.get(inner, MOI.ListOfModelAttributesSet())
@test length(attr) == 2
@test MOI.ObjectiveSense() in attr
@test MOI.ObjectiveFunction{MOI.VariableIndex}() in attr
return
end
function test_SlackBridge_ListOfVariableIndices()
inner = MOI.Utilities.MockOptimizer(MOI.Utilities.Model{Float64}())
model = MOI.Bridges.Objective.Slack{Float64}(inner)
x = MOI.add_variable(model)
f = MOI.ScalarAffineFunction([MOI.ScalarAffineTerm(1.1, x)], 1.2)
MOI.set(model, MOI.ObjectiveSense(), MOI.MAX_SENSE)
MOI.set(model, MOI.ObjectiveFunction{typeof(f)}(), f)
@test MOI.get(model, MOI.ListOfVariableIndices()) == [x]
@test length(MOI.get(inner, MOI.ListOfVariableIndices())) == 2
MOI.set(model, MOI.ObjectiveSense(), MOI.FEASIBILITY_SENSE)
@test MOI.get(model, MOI.ListOfVariableIndices()) == [x]
@test MOI.get(inner, MOI.ListOfVariableIndices()) == [x]
return
end
function test_SlackBridge_ListOfConstraintTypesPresent()
inner = MOI.Utilities.MockOptimizer(MOI.Utilities.Model{Float64}())
model = MOI.Bridges.Objective.Slack{Float64}(inner)
x = MOI.add_variable(model)
f = MOI.ScalarAffineFunction([MOI.ScalarAffineTerm(1.1, x)], 1.2)
MOI.set(model, MOI.ObjectiveSense(), MOI.MAX_SENSE)
MOI.set(model, MOI.ObjectiveFunction{typeof(f)}(), f)
@test MOI.get(model, MOI.ListOfConstraintTypesPresent()) == []
@test MOI.get(inner, MOI.ListOfConstraintTypesPresent()) ==
[(MOI.ScalarAffineFunction{Float64}, MOI.GreaterThan{Float64})]
MOI.set(model, MOI.ObjectiveSense(), MOI.FEASIBILITY_SENSE)
@test MOI.get(model, MOI.ListOfConstraintTypesPresent()) == []
@test MOI.get(inner, MOI.ListOfConstraintTypesPresent()) == []
return
end
function test_SlackBridge_ListOfConstraintIndices()
inner = MOI.Utilities.MockOptimizer(MOI.Utilities.Model{Float64}())
model = MOI.Bridges.Objective.Slack{Float64}(inner)
x = MOI.add_variable(model)
f = MOI.ScalarAffineFunction([MOI.ScalarAffineTerm(1.1, x)], 1.2)
MOI.set(model, MOI.ObjectiveSense(), MOI.MAX_SENSE)
MOI.set(model, MOI.ObjectiveFunction{typeof(f)}(), f)
attr = MOI.ListOfConstraintIndices{
MOI.ScalarAffineFunction{Float64},
MOI.GreaterThan{Float64},
}()
@test MOI.get(model, attr) == []
@test length(MOI.get(inner, attr)) == 1
attr = MOI.ListOfConstraintIndices{
MOI.ScalarAffineFunction{Float64},
MOI.LessThan{Float64},
}()
@test MOI.get(model, attr) == []
@test length(MOI.get(inner, attr)) == 0
MOI.set(model, MOI.ObjectiveSense(), MOI.FEASIBILITY_SENSE)
@test MOI.get(model, attr) == []
@test length(MOI.get(inner, attr)) == 0
return
end
function test_SlackBridge_ObjectiveFunctionValue()
inner = MOI.Utilities.MockOptimizer(MOI.Utilities.Model{Float64}())
model = MOI.Bridges.Objective.Slack{Float64}(inner)
x = MOI.add_variable(model)
MOI.add_constraint(model, x, MOI.GreaterThan(2.0))
f = MOI.ScalarAffineFunction([MOI.ScalarAffineTerm(1.1, x)], -1.2)
MOI.set(model, MOI.ObjectiveSense(), MOI.MIN_SENSE)
MOI.set(model, MOI.ObjectiveFunction{typeof(f)}(), f)
MOI.Utilities.set_mock_optimize!(
inner,
mock -> MOI.Utilities.mock_optimize!(mock, [2.0, 1.0]),
)
MOI.optimize!(model)
@test MOI.get(model, MOI.ObjectiveValue()) ≈ 1.0
return
end
function test_SlackBridge_ObjectiveFunctionValue_2()
inner = MOI.Utilities.MockOptimizer(MOI.Utilities.Model{Float64}())
model = MOI.Bridges.Objective.Slack{Float64}(inner)
x = MOI.add_variable(model)
MOI.add_constraint(model, x, MOI.GreaterThan(1.0))
f = 1.1 * x - 1.2
MOI.set(model, MOI.ObjectiveSense(), MOI.MIN_SENSE)
MOI.set(model, MOI.ObjectiveFunction{typeof(f)}(), f)
MOI.Utilities.set_mock_optimize!(
inner,
mock -> begin
MOI.set(mock, MOI.ResultCount(), 2)
MOI.set(mock, MOI.TerminationStatus(), MOI.OPTIMAL)
MOI.set(mock, MOI.PrimalStatus(1), MOI.FEASIBLE_POINT)
MOI.set(mock, MOI.PrimalStatus(2), MOI.FEASIBLE_POINT)
y = MOI.get(mock, MOI.ListOfVariableIndices())
MOI.set.(mock, MOI.VariablePrimal(1), y, [1.0, -0.1])
MOI.set.(mock, MOI.VariablePrimal(2), y, [2.0, 1.0])
end,
)
MOI.optimize!(model)
@test MOI.get(model, MOI.ObjectiveValue(1)) ≈ -0.1
@test MOI.get(model, MOI.VariablePrimal(1), x) ≈ 1.0
@test MOI.get(model, MOI.ObjectiveValue(2)) ≈ 1.0
@test MOI.get(model, MOI.VariablePrimal(2), x) ≈ 2.0
return
end
function test_original()
mock = MOI.Utilities.MockOptimizer(MOI.Utilities.Model{Float64}())
bridged_mock = MOI.Bridges.Objective.Slack{Float64}(mock)
MOI.Utilities.set_mock_optimize!(
mock,
(mock::MOI.Utilities.MockOptimizer) -> MOI.Utilities.mock_optimize!(
mock,
MOI.OPTIMAL,
(MOI.FEASIBLE_POINT, [1.0, 2.0, 5.0]),
),
(mock::MOI.Utilities.MockOptimizer) -> MOI.Utilities.mock_optimize!(
mock,
MOI.OPTIMAL,
(MOI.FEASIBLE_POINT, [1.0, 2.0, 7.0]),
),
(mock::MOI.Utilities.MockOptimizer) -> MOI.Utilities.mock_optimize!(
mock,
MOI.OPTIMAL,
(MOI.FEASIBLE_POINT, [1.0, 2.0, 2.0]),
),
(mock::MOI.Utilities.MockOptimizer) -> MOI.Utilities.mock_optimize!(
mock,
MOI.OPTIMAL,
(MOI.FEASIBLE_POINT, [1.0, 2.0, 7.0]),
),
)
MOI.Test.test_objective_qp_ObjectiveFunction_edge_cases(
bridged_mock,
MOI.Test.Config(),
)
@test MOI.Bridges.is_objective_bridged(bridged_mock)
@test MOI.get(bridged_mock, MOI.ObjectiveFunctionType()) ==
MOI.ScalarQuadraticFunction{Float64}
@test MOI.get(bridged_mock, MOI.ListOfModelAttributesSet()) == [
MOI.ObjectiveSense(),
MOI.ObjectiveFunction{MOI.ScalarQuadraticFunction{Float64}}(),
]
var_names = ["x", "y"]
xy = MOI.get(bridged_mock, MOI.ListOfVariableIndices())
MOI.set(bridged_mock, MOI.VariableName(), xy, var_names)
abs = MOI.get(mock, MOI.ListOfVariableIndices())
@test length(abs) == 3
MOI.set(mock, MOI.VariableName(), abs[3], "s")
cquad = MOI.get(
mock,
MOI.ListOfConstraintIndices{
MOI.ScalarQuadraticFunction{Float64},
MOI.LessThan{Float64},
}(),
)
MOI.set(bridged_mock, MOI.ConstraintName(), cquad[1], "quad")
s = """
variables: x, y, s
x >= 1.0
y >= 2.0
quad: 1.0 * x * x + 1.0 * x * y + 1.0 * y * y + -1.0 * s <= 0.0
minobjective: s
"""
model = MOI.Utilities.Model{Float64}()
MOI.Utilities.loadfromstring!(model, s)
MOI.Test.util_test_models_equal(
mock,
model,
[var_names; "s"],
["quad"],
[
("x", MOI.GreaterThan{Float64}(1.0)),
("y", MOI.GreaterThan{Float64}(2.0)),
],
)
bridged_var_names = ["x", "y"]
s = """
variables: x, y
x >= 1.0
y >= 2.0
minobjective: 1.0 * x * x + 1.0 * x * y + 1.0 * y * y
"""
model = MOI.Utilities.Model{Float64}()
MOI.Utilities.loadfromstring!(model, s)
MOI.Test.util_test_models_equal(
bridged_mock,
model,
var_names,
String[],
[
("x", MOI.GreaterThan{Float64}(1.0)),
("y", MOI.GreaterThan{Float64}(2.0)),
],
)
err = MOI.SetAttributeNotAllowed(
MOI.ObjectiveSense(),
"Objective bridge of type `$(MOI.Bridges.Objective.SlackBridge{Float64,MOI.ScalarQuadraticFunction{Float64},MOI.ScalarQuadraticFunction{Float64}})`" *
" does not support modifying the objective sense. As a workaround, set" *
" the sense to `MOI.FEASIBILITY_SENSE` to clear the objective function" *
" and bridges.",
)
@test_throws err MOI.set(bridged_mock, MOI.ObjectiveSense(), MOI.MAX_SENSE)
obj = MOI.get(
bridged_mock,
MOI.ObjectiveFunction{MOI.ScalarQuadraticFunction{Float64}}(),
)
MOI.set(bridged_mock, MOI.ObjectiveSense(), MOI.FEASIBILITY_SENSE)
@test !MOI.Bridges.is_objective_bridged(bridged_mock)
@test MOI.get(bridged_mock, MOI.ObjectiveSense()) == MOI.FEASIBILITY_SENSE
@test MOI.get(bridged_mock, MOI.ListOfModelAttributesSet()) ==
[MOI.ObjectiveSense()]
MOI.set(bridged_mock, MOI.ObjectiveSense(), MOI.MAX_SENSE)
MOI.set(
bridged_mock,
MOI.ObjectiveFunction{MOI.ScalarQuadraticFunction{Float64}}(),
obj,
)
abs = MOI.get(mock, MOI.ListOfVariableIndices())
@test length(abs) == 3
MOI.set(mock, MOI.VariableName(), abs[3], "s")
cquad = MOI.get(
mock,
MOI.ListOfConstraintIndices{
MOI.ScalarQuadraticFunction{Float64},
MOI.GreaterThan{Float64},
}(),
)
MOI.set(bridged_mock, MOI.ConstraintName(), cquad[1], "quad")
s = """
variables: x, y, s
x >= 1.0
y >= 2.0
quad: 1.0 * x * x + 1.0 * x * y + 1.0 * y * y + -1.0 * s >= 0.0
maxobjective: s
"""
model = MOI.Utilities.Model{Float64}()
MOI.Utilities.loadfromstring!(model, s)
MOI.Test.util_test_models_equal(
mock,
model,
[var_names; "s"],
["quad"],
[
("x", MOI.GreaterThan{Float64}(1.0)),
("y", MOI.GreaterThan{Float64}(2.0)),
],
)
s = """
variables: x, y
x >= 1.0
y >= 2.0
maxobjective: 1.0 * x * x + 1.0 * x * y + 1.0 * y * y
"""
model = MOI.Utilities.Model{Float64}()
MOI.Utilities.loadfromstring!(model, s)
MOI.Test.util_test_models_equal(
bridged_mock,
model,
var_names,
String[],
[
("x", MOI.GreaterThan{Float64}(1.0)),
("y", MOI.GreaterThan{Float64}(2.0)),
],
)
_test_delete_objective(
bridged_mock,
2,
(
(MOI.ScalarQuadraticFunction{Float64}, MOI.GreaterThan{Float64}, 0),
(MOI.ScalarQuadraticFunction{Float64}, MOI.LessThan{Float64}, 0),
),
)
return
end
function test_quadratic_integration()
mock = MOI.Utilities.MockOptimizer(MOI.Utilities.Model{Float64}())
bridged_mock = MOI.Bridges.Objective.Slack{Float64}(mock)
MOI.Utilities.set_mock_optimize!(
mock,
(mock::MOI.Utilities.MockOptimizer) -> MOI.Utilities.mock_optimize!(
mock,
[4 / 7, 3 / 7, 6 / 7, 13 / 7],
(MOI.ScalarAffineFunction{Float64}, MOI.GreaterThan{Float64}) =>
[5 / 7, 6 / 7],
),
)
MOI.Test.test_quadratic_integration(bridged_mock, MOI.Test.Config())
return
end
function test_quadratic_duplicate_terms()
mock = MOI.Utilities.MockOptimizer(MOI.Utilities.Model{Float64}())
bridged_mock = MOI.Bridges.Objective.Slack{Float64}(mock)
MOI.Utilities.set_mock_optimize!(
mock,
(mock::MOI.Utilities.MockOptimizer) -> MOI.Utilities.mock_optimize!(
mock,
[4 / 7, 3 / 7, 6 / 7, 13 / 7],
(MOI.ScalarAffineFunction{Float64}, MOI.GreaterThan{Float64}) =>
[5 / 7, 6 / 7],
),
(mock::MOI.Utilities.MockOptimizer) -> MOI.Utilities.mock_optimize!(
mock,
[4 / 7, 3 / 7, 6 / 7, -2 * 13 / 7],
(MOI.ScalarAffineFunction{Float64}, MOI.GreaterThan{Float64}) =>
[10 / 7, 12 / 7],
),
)
MOI.Test.test_quadratic_duplicate_terms(bridged_mock, MOI.Test.Config())
return
end
function test_quadratic_nonhomogeneous()
mock = MOI.Utilities.MockOptimizer(MOI.Utilities.Model{Float64}())
bridged_mock = MOI.Bridges.Objective.Slack{Float64}(mock)
MOI.Utilities.set_mock_optimize!(
mock,
(mock::MOI.Utilities.MockOptimizer) -> MOI.Utilities.mock_optimize!(
mock,
[1 / 4, 3 / 4, 2.875],
(MOI.ScalarAffineFunction{Float64}, MOI.EqualTo{Float64}) =>
[11 / 4],
(MOI.ScalarQuadraticFunction{Float64}, MOI.LessThan{Float64}) =>
[-1.0],
),
(mock::MOI.Utilities.MockOptimizer) -> MOI.Utilities.mock_optimize!(
mock,
[1.0, 0.0, 3.0],
(MOI.ScalarAffineFunction{Float64}, MOI.EqualTo{Float64}) =>
[-2.0],
(MOI.ScalarAffineFunction{Float64}, MOI.GreaterThan{Float64}) =>
[1.0],
),
)
MOI.Test.test_quadratic_nonhomogeneous(bridged_mock, MOI.Test.Config())
return
end
function test_runtests()
MOI.Bridges.runtests(
MOI.Bridges.Objective.SlackBridge,
"""
variables: x
minobjective: 1.1 * x + 2.2
""",
"""
variables: x, y
minobjective: y
1.1 * x + -1.0 * y <= -2.2
""",
)
MOI.Bridges.runtests(
MOI.Bridges.Objective.SlackBridge,
"""
variables: x
maxobjective: 1.1 * x + 2.2
""",
"""
variables: x, y
maxobjective: y
1.1 * x + -1.0 * y >= -2.2
""",
)
MOI.Bridges.runtests(
MOI.Bridges.Objective.SlackBridge,
"""
variables: x
maxobjective: ScalarNonlinearFunction(log(x))
""",
"""
variables: x, y
maxobjective: y
ScalarNonlinearFunction(log(x) - y) >= 0.0
""",
)
return
end
function test_complex_objective()
model = MOI.Utilities.UniversalFallback(MOI.Utilities.Model{Float64}())
x = MOI.add_variable(model)
for f in ((1.0 + 1.0im) * x, (1.0 + 1.0im) * x * x)
attr = MOI.ObjectiveFunction{typeof(f)}()
MOI.set(model, attr, f)
dest = MOI.Bridges.full_bridge_optimizer(
MOI.Utilities.Model{Float64}(),
Float64,
)
@test_throws MOI.UnsupportedAttribute(attr) MOI.copy_to(dest, model)
end
return
end
function test_deletion_of_variable_in_slacked_objective()
inner = MOI.Utilities.Model{Float64}()
model = MOI.Bridges.Objective.Slack{Float64}(inner)
x = MOI.add_variables(model, 2)
MOI.set(model, MOI.VariableName(), x, ["x", "y"])
f = 1.0 * x[1] * x[1] + 1.0 * x[2] * x[2]
MOI.set(model, MOI.ObjectiveSense(), MOI.MIN_SENSE)
MOI.set(model, MOI.ObjectiveFunction{typeof(f)}(), f)
MOI.delete(model, x[1])
@test MOI.get(model, MOI.ObjectiveFunction{typeof(f)}()) ≈ 1.0 * x[2] * x[2]
return
end
function test_SlackBridgePrimalDualStart_non_slack()
inner = MOI.Utilities.MockOptimizer(
MOI.Utilities.UniversalFallback(MOI.Utilities.Model{Float64}()),
)
# Should ignore without erroring
MOI.set(inner, MOI.Bridges.Objective.SlackBridgePrimalDualStart(), nothing)
model = MOI.Bridges.Objective.Functionize{Float64}(inner)
x = MOI.add_variable(model)
MOI.set(model, MOI.ObjectiveFunction{typeof(x)}(), x)
# Should ignore without erroring
MOI.set(model, MOI.Bridges.Objective.SlackBridgePrimalDualStart(), nothing)
return
end
function test_SlackBridgePrimalDualStart()
inner = MOI.Utilities.MockOptimizer(
MOI.Utilities.UniversalFallback(MOI.Utilities.Model{Float64}()),
)
model = MOI.Bridges.Objective.Slack{Float64}(inner)
x = MOI.add_variable(model)
MOI.add_constraint(model, x, MOI.GreaterThan(2.0))
f = MOI.ScalarAffineFunction([MOI.ScalarAffineTerm(1.1, x)], -1.2)
MOI.set(model, MOI.ObjectiveSense(), MOI.MIN_SENSE)
MOI.set(model, MOI.ObjectiveFunction{typeof(f)}(), f)
MOI.set(model, MOI.VariablePrimalStart(), x, 2.0)
attr = MOI.Bridges.Objective.SlackBridgePrimalDualStart()
@test MOI.supports(model, attr)
MOI.set(model, attr, nothing)
vars = MOI.get(inner, MOI.ListOfVariableIndices())
primal_start = MOI.get.(inner, MOI.VariablePrimalStart(), vars)
@test primal_start[1] ≈ 2.0
@test primal_start[2] ≈ 1.1 * 2.0 - 1.2
F = MOI.ScalarAffineFunction{Float64}
cis = MOI.get(inner, MOI.ListOfConstraintIndices{F,MOI.LessThan{Float64}}())
@test length(cis) == 1
@test MOI.get(inner, MOI.ConstraintPrimalStart(), cis[1]) ≈ 0.0
@test MOI.get(inner, MOI.ConstraintDualStart(), cis[1]) ≈ -1.0
return
end
function test_SlackBridgePrimalDualStart_unsupported()
attr = MOI.Bridges.Objective.SlackBridgePrimalDualStart()
inner = MOI.Utilities.MockOptimizer(MOI.Utilities.Model{Float64}())
# Check that setting on blank model doesn't error.
@test MOI.supports(inner, attr)
MOI.set(inner, attr, nothing)
model = MOI.Bridges.Objective.Slack{Float64}(inner)
@test MOI.supports(model, attr)
x = MOI.add_variable(model)
MOI.set(model, MOI.ObjectiveSense(), MOI.MIN_SENSE)
f = MOI.ScalarAffineFunction([MOI.ScalarAffineTerm(1.1, x)], -1.2)
MOI.set(model, MOI.ObjectiveFunction{typeof(f)}(), f)
# Unsupported. Should silently skip without error.
MOI.set(model, attr, nothing)
return
end
end # module
TestObjectiveSlack.runtests()