/
mockoptimizer.jl
1156 lines (1036 loc) · 32 KB
/
mockoptimizer.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
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
# 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.
# An Int-valued attribute
struct MockModelAttribute <: MOI.AbstractModelAttribute end
# An Int-valued attribute
struct MockVariableAttribute <: MOI.AbstractVariableAttribute end
# An Int-valued attribute
struct MockConstraintAttribute <: MOI.AbstractConstraintAttribute end
# A mock optimizer used for testing.
"""
MockOptimizer
`MockOptimizer` is a fake optimizer especially useful for testing. Its main
feature is that it can store the values that should be returned for each
attribute.
"""
mutable struct MockOptimizer{MT<:MOI.ModelLike,T} <: MOI.AbstractOptimizer
inner_model::MT
# Flags
supports_names::Bool # Allows to test with optimizer not supporting names
add_var_allowed::Bool # If false, the optimizer throws AddVariableNotAllowed
add_con_allowed::Bool # If false, the optimizer throws AddConstraintNotAllowed
modify_allowed::Bool # If false, the optimizer throws Modify...NotAllowed
delete_allowed::Bool # If false, the optimizer throws DeleteNotAllowed
scalar_function_constant_non_zero::Bool
# Computes `ObjectiveValue` by evaluating the `ObjectiveFunction` with
# `VariablePrimal`. See `get_fallback`.
eval_objective_value::Bool
# Computes `DualObjectiveValue` using `get_fallback`
eval_dual_objective_value::Bool
# Computes `ConstraintDual` of constraints with `VariableIndex` or
# `VectorOfVariables` functions by evaluating the `ConstraintDual` of
# constraints having the variable in the function. See `get_fallback`.
eval_variable_constraint_dual::Bool
# Attributes
# The attributes set by `MOI.optimize!` cannot be set to `model`.
# We detect them with `is_set_by_optimize` and store them in the following:
optimizer_attributes::Dict{MOI.AbstractOptimizerAttribute,Any}
model_attributes::Dict{MOI.AbstractModelAttribute,Any}
submitted::Dict{MOI.AbstractSubmittable,Vector{Tuple}}
mock_model_attribute::Int
mock_variable_attribute::Dict{MOI.VariableIndex,Int}
mock_constraint_attribute::Dict{MOI.ConstraintIndex,Int}
#
optimize!::Function
optimize_called::Bool
termination_status::MOI.TerminationStatusCode
result_count::Int
objective_value::Dict{Int,T}
dual_objective_value::Dict{Int,T}
# Primal solution
primal_status::Dict{Int,MOI.ResultStatusCode}
variable_primal::Dict{MOI.VariableIndex,Dict{Int,T}}
callback_variable_primal::Dict{MOI.VariableIndex,T}
# Dual solution
dual_status::Dict{Int,MOI.ResultStatusCode}
constraint_dual::Dict{MOI.ConstraintIndex,Dict{Int,Any}}
# Constraint conflicts
compute_conflict_called::Bool
conflict_status::MOI.ConflictStatusCode
constraint_conflict_status::Dict{
MOI.ConstraintIndex,
MOI.ConflictParticipationStatusCode,
}
# Basis status
constraint_basis_status::Dict{
MOI.ConstraintIndex,
Dict{Int,MOI.BasisStatusCode},
}
variable_basis_status::Dict{MOI.VariableIndex,Dict{Int,MOI.BasisStatusCode}}
end
function MockOptimizer(
inner_model::MOI.ModelLike,
T = Float64;
supports_names = true,
add_var_allowed = true,
add_con_allowed = true,
eval_objective_value = true,
eval_dual_objective_value = true,
eval_variable_constraint_dual = true,
scalar_function_constant_non_zero = false,
)
return MockOptimizer{typeof(inner_model),T}(
inner_model,
# Flags
supports_names,
add_var_allowed,
add_con_allowed,
true,
true,
scalar_function_constant_non_zero,
eval_objective_value,
eval_dual_objective_value,
eval_variable_constraint_dual,
# Attributes
Dict{MOI.AbstractOptimizerAttribute,Any}(),
Dict{MOI.AbstractModelAttribute,Any}(),
Dict{MOI.AbstractSubmittable,Vector{Tuple}}(),
0,
Dict{MOI.VariableIndex,Int}(),
Dict{MOI.ConstraintIndex,Int}(),
#
(::MockOptimizer) -> nothing,
false,
MOI.OPTIMIZE_NOT_CALLED,
1,
Dict{Int,T}(),
Dict{Int,T}(),
# PrimalStatus
Dict{Int,MOI.ResultStatusCode}(),
Dict{MOI.VariableIndex,Dict{Int,T}}(),
Dict{MOI.VariableIndex,T}(),
# DualStatus
Dict{Int,MOI.ResultStatusCode}(),
Dict{MOI.ConstraintIndex,Dict{Int,Any}}(),
#
false,
MOI.COMPUTE_CONFLICT_NOT_CALLED,
Dict{MOI.ConstraintIndex,MOI.ConflictParticipationStatusCode}(),
# Basis status
Dict{MOI.ConstraintIndex,Dict{Int,MOI.BasisStatusCode}}(),
Dict{MOI.VariableIndex,Dict{Int,MOI.BasisStatusCode}}(),
)
end
"""
All user-facing indices are xor'd with this mask to produce unusual indices.
This is good at catching bugs in solvers which assume indices are ordered 1, 2,
3, ...
"""
const _INTERNAL_XOR_MASK = Int64(12345678)
xor_index(vi::VI) = VI(xor(vi.value, _INTERNAL_XOR_MASK))
xor_index(ci::CI{F,S}) where {F,S} = CI{F,S}(xor(ci.value, _INTERNAL_XOR_MASK))
xor_indices(x) = map_indices(xor_index, x)
function MOI.add_variable(mock::MockOptimizer)
if mock.add_var_allowed
return xor_index(MOI.add_variable(mock.inner_model))
else
throw(MOI.AddVariableNotAllowed())
end
end
function MOI.add_constraint(
mock::MockOptimizer,
func::MOI.AbstractFunction,
set::MOI.AbstractSet,
)
if mock.add_con_allowed
ci = MOI.add_constraint(mock.inner_model, xor_indices(func), set)
return xor_index(ci)
else
throw(MOI.AddConstraintNotAllowed{typeof(func),typeof(set)}())
end
end
function MOI.add_constraint(
mock::MockOptimizer,
func::MOI.ScalarAffineFunction{T},
set::MOI.AbstractSet,
) where {T}
if !mock.add_con_allowed
throw(MOI.AddConstraintNotAllowed{typeof(func),typeof(set)}())
elseif mock.scalar_function_constant_non_zero && !iszero(func.constant)
throw(
MOI.ScalarFunctionConstantNotZero{T,typeof(func),typeof(set)}(
func.constant,
),
)
end
ci = MOI.add_constraint(mock.inner_model, xor_indices(func), set)
return xor_index(ci)
end
function MOI.add_constraint(
mock::MockOptimizer,
func::MOI.VariableIndex,
set::MOI.AbstractSet,
)
if !mock.add_con_allowed
throw(MOI.AddConstraintNotAllowed{typeof(func),typeof(set)}())
end
try
ci = MOI.add_constraint(mock.inner_model, xor_indices(func), set)
return xor_index(ci)
catch err
if (err isa MOI.LowerBoundAlreadySet) ||
(err isa MOI.UpperBoundAlreadySet)
throw(typeof(err)(xor_index(err.vi)))
else
rethrow(err)
end
end
end
function MOI.optimize!(mock::MockOptimizer)
mock.optimize_called = true
mock.optimize!(mock)
return
end
function MOI.compute_conflict!(model::MockOptimizer)
model.compute_conflict_called = true
return
end
function throw_mock_unsupported_names(attr)
return throw(
MOI.UnsupportedAttribute(
attr,
"The MockOptimizer was configured not to support names for " *
"testing purpose using the `support_names=false` constructor keyword " *
"argument.",
),
)
end
function MOI.supports(
::MockOptimizer,
::Union{MOI.VariablePrimal,MockVariableAttribute},
::Type{MOI.VariableIndex},
)
return true
end
function MOI.supports(
mock::MockOptimizer,
attr::MOI.AbstractVariableAttribute,
IdxT::Type{MOI.VariableIndex},
)
return MOI.supports(mock.inner_model, attr, IdxT)
end
function MOI.supports(
::MockOptimizer,
::Union{MOI.ConstraintDual,MockConstraintAttribute},
::Type{<:MOI.ConstraintIndex},
)
return true
end
function MOI.supports(
mock::MockOptimizer,
attr::MOI.AbstractConstraintAttribute,
IdxT::Type{<:MOI.ConstraintIndex},
)
return MOI.supports(mock.inner_model, attr, IdxT)
end
MOI.supports(mock::MockOptimizer, ::MockModelAttribute) = true
function MOI.set(
mock::MockOptimizer,
::MOI.TerminationStatus,
value::MOI.TerminationStatusCode,
)
mock.termination_status = value
return
end
function MOI.set(mock::MockOptimizer, attr::MOI.ObjectiveValue, value::Real)
mock.objective_value[attr.result_index] = value
return
end
function MOI.set(mock::MockOptimizer, attr::MOI.DualObjectiveValue, value::Real)
mock.dual_objective_value[attr.result_index] = value
return
end
function MOI.set(
mock::MockOptimizer,
attr::MOI.PrimalStatus,
value::MOI.ResultStatusCode,
)
mock.primal_status[attr.result_index] = value
return
end
function MOI.set(
mock::MockOptimizer,
attr::MOI.DualStatus,
value::MOI.ResultStatusCode,
)
mock.dual_status[attr.result_index] = value
return
end
function MOI.set(
mock::MockOptimizer,
::MOI.ConflictStatus,
value::MOI.ConflictStatusCode,
)
mock.conflict_status = value
return
end
function MOI.get(mock::MockOptimizer, ::MOI.ConflictStatus)
if mock.compute_conflict_called
return mock.conflict_status
end
return MOI.COMPUTE_CONFLICT_NOT_CALLED
end
function MOI.set(mock::MockOptimizer, ::MockModelAttribute, value::Integer)
mock.mock_model_attribute = value
return
end
function MOI.supports(mock::MockOptimizer, attr::MOI.AbstractOptimizerAttribute)
# `supports` is not defined if `is_set_by_optimize(attr)` so we pass it to
# `mock.inner_model`.
return MOI.supports(mock.inner_model, attr)
end
function MOI.set(
mock::MockOptimizer,
attr::MOI.AbstractOptimizerAttribute,
value,
)
if MOI.is_set_by_optimize(attr)
mock.optimizer_attributes[attr] = value
else
MOI.set(mock.inner_model, attr, value)
end
return
end
function MOI.supports(mock::MockOptimizer, attr::MOI.AbstractModelAttribute)
# `supports` is not defined if `is_set_by_optimize(attr)` so we pass it to
# `mock.inner_model`.
return MOI.supports(mock.inner_model, attr)
end
function MOI.set(mock::MockOptimizer, attr::MOI.AbstractModelAttribute, value)
if MOI.is_set_by_optimize(attr)
mock.model_attributes[attr] = value
else
MOI.set(mock.inner_model, attr, xor_indices(value))
end
return
end
function MOI.set(
mock::MockOptimizer,
attr::MOI.AbstractVariableAttribute,
idx::MOI.VariableIndex,
value,
)
MOI.set(mock.inner_model, attr, xor_index(idx), xor_indices(value))
return
end
function MOI.set(
mock::MockOptimizer,
attr::MOI.VariablePrimal,
idx::MOI.VariableIndex,
value,
)
_safe_set_result(mock.variable_primal, attr, idx, value)
return
end
function MOI.set(
mock::MockOptimizer,
::MOI.CallbackVariablePrimal,
idx::MOI.VariableIndex,
value,
)
mock.callback_variable_primal[xor_index(idx)] = value
return
end
function MOI.set(
mock::MockOptimizer,
::MockVariableAttribute,
idx::MOI.VariableIndex,
value,
)
mock.mock_variable_attribute[xor_index(idx)] = value
return
end
function MOI.set(
mock::MockOptimizer,
attr::MOI.AbstractConstraintAttribute,
idx::MOI.ConstraintIndex,
value,
)
MOI.set(mock.inner_model, attr, xor_index(idx), value)
return
end
function MOI.set(
mock::MockOptimizer,
::MockConstraintAttribute,
idx::MOI.ConstraintIndex,
value,
)
mock.mock_constraint_attribute[xor_index(idx)] = value
return
end
function MOI.set(
mock::MockOptimizer,
attr::MOI.ConstraintDual,
idx::MOI.ConstraintIndex,
value,
)
_safe_set_result(mock.constraint_dual, attr, idx, value)
return
end
function MOI.set(
mock::MockOptimizer,
attr::MOI.ConstraintBasisStatus,
idx::MOI.ConstraintIndex,
value,
)
_safe_set_result(mock.constraint_basis_status, attr, idx, value)
return
end
function MOI.set(
mock::MockOptimizer,
attr::MOI.VariableBasisStatus,
idx::MOI.VariableIndex,
value,
)
_safe_set_result(mock.variable_basis_status, attr, idx, value)
return
end
function MOI.set(
mock::MockOptimizer,
::MOI.ConstraintConflictStatus,
idx::MOI.ConstraintIndex,
value,
)
mock.constraint_conflict_status[xor_index(idx)] = value
return
end
MOI.get(mock::MockOptimizer, ::MOI.RawSolver) = mock
function MOI.get(mock::MockOptimizer, attr::MOI.AbstractOptimizerAttribute)
if MOI.is_set_by_optimize(attr)
return mock.optimizer_attributes[attr]
else
return MOI.get(mock.inner_model, attr)
end
end
function MOI.get(mock::MockOptimizer, attr::MOI.AbstractModelAttribute)
if MOI.is_set_by_optimize(attr)
return mock.model_attributes[attr]
else
return xor_indices(MOI.get(mock.inner_model, attr))
end
end
#####
##### Names
#####
function MOI.supports(mock::MockOptimizer, attr::MOI.Name)
return mock.supports_names && MOI.supports(mock.inner_model, attr)
end
function MOI.set(mock::MockOptimizer, attr::MOI.Name, value)
if !mock.supports_names
throw_mock_unsupported_names(attr)
end
MOI.set(mock.inner_model, attr, value)
return
end
function MOI.supports(
mock::MockOptimizer,
attr::MOI.VariableName,
IdxT::Type{MOI.VariableIndex},
)
return mock.supports_names && MOI.supports(mock.inner_model, attr, IdxT)
end
function MOI.set(
mock::MockOptimizer,
attr::MOI.VariableName,
index::MOI.VariableIndex,
value,
)
if !mock.supports_names
throw_mock_unsupported_names(attr)
end
MOI.set(mock.inner_model, attr, xor_index(index), value)
return
end
function MOI.supports(
mock::MockOptimizer,
attr::MOI.ConstraintName,
IdxT::Type{<:MOI.ConstraintIndex},
)
return mock.supports_names && MOI.supports(mock.inner_model, attr, IdxT)
end
function MOI.set(
mock::MockOptimizer,
attr::MOI.ConstraintName,
index::MOI.ConstraintIndex,
value,
)
if !mock.supports_names
throw_mock_unsupported_names(attr)
end
MOI.set(mock.inner_model, attr, xor_index(index), value)
return
end
function MOI.get(b::MockOptimizer, IdxT::Type{<:MOI.Index}, name::String)
index = MOI.get(b.inner_model, IdxT, name)
return index === nothing ? nothing : xor_index(index)
end
#####
##### Results
#####
MOI.get(mock::MockOptimizer, ::MOI.ResultCount) = mock.result_count
function MOI.set(mock::MockOptimizer, ::MOI.ResultCount, x)
mock.result_count = x
return
end
MOI.get(mock::MockOptimizer, ::MOI.TerminationStatus) = mock.termination_status
function MOI.get(mock::MockOptimizer, attr::MOI.ObjectiveValue)
if mock.eval_objective_value
return get_fallback(mock, attr)
end
MOI.check_result_index_bounds(mock, attr)
return get(mock.objective_value, attr.result_index, NaN)
end
function MOI.get(
mock::MockOptimizer{<:MOI.ModelLike,T},
attr::MOI.DualObjectiveValue,
) where {T}
if mock.eval_dual_objective_value
return get_fallback(mock, attr, T)
end
MOI.check_result_index_bounds(mock, attr)
return get(mock.dual_objective_value, attr.result_index, NaN)
end
function MOI.get(mock::MockOptimizer, attr::MOI.PrimalStatus)
if attr.result_index > mock.result_count
return MOI.NO_SOLUTION
end
return get(mock.primal_status, attr.result_index, MOI.NO_SOLUTION)
end
function MOI.get(mock::MockOptimizer, attr::MOI.DualStatus)
if attr.result_index > mock.result_count
return MOI.NO_SOLUTION
end
return get(mock.dual_status, attr.result_index, MOI.NO_SOLUTION)
end
MOI.get(mock::MockOptimizer, ::MockModelAttribute) = mock.mock_model_attribute
function MOI.get(
mock::MockOptimizer,
attr::MOI.AbstractVariableAttribute,
idx::MOI.VariableIndex,
)
return xor_indices(MOI.get(mock.inner_model, attr, xor_index(idx)))
end
function MOI.get(
mock::MockOptimizer,
::MockVariableAttribute,
idx::MOI.VariableIndex,
)
return mock.mock_variable_attribute[xor_index(idx)]
end
function MOI.get(
mock::MockOptimizer,
attr::MOI.VariablePrimal,
idx::MOI.VariableIndex,
)
MOI.check_result_index_bounds(mock, attr)
MOI.throw_if_not_valid(mock, idx)
return _safe_get_result(mock.variable_primal, attr, idx, "primal")
end
function MOI.get(
mock::MockOptimizer,
::MOI.CallbackVariablePrimal,
idx::MOI.VariableIndex,
)
if !MOI.is_valid(mock, idx)
throw(MOI.InvalidIndex(idx))
end
primal = get(mock.callback_variable_primal, xor_index(idx), nothing)
if primal === nothing
error("No mock callback primal is set for variable `", idx, "`.")
end
return primal
end
function MOI.get(
mock::MockOptimizer,
attr::MOI.ConstraintPrimal,
idx::MOI.ConstraintIndex,
)
return get_fallback(mock, attr, idx)
end
function MOI.get(
mock::MockOptimizer,
attr::MOI.AbstractConstraintAttribute,
idx::MOI.ConstraintIndex,
)
# If it is thrown by `mock.inner_model`, the index will be xor'ed.
MOI.throw_if_not_valid(mock, idx)
return MOI.get(mock.inner_model, attr, xor_index(idx))
end
function MOI.get(
mock::MockOptimizer,
attr::MOI.CanonicalConstraintFunction,
idx::MOI.ConstraintIndex,
)
# If it is thrown by `mock.inner_model`, the index will be xor'ed.
MOI.throw_if_not_valid(mock, idx)
# After xoring the indices, the order might not be respected anymore
return canonical(
xor_indices(MOI.get(mock.inner_model, attr, xor_index(idx))),
)
end
function MOI.get(
mock::MockOptimizer,
attr::Union{MOI.CanonicalConstraintFunction,MOI.ConstraintFunction},
idx::MOI.ConstraintIndex,
)
# If it is thrown by `mock.inner_model`, the index will be xor'ed.
MOI.throw_if_not_valid(mock, idx)
return xor_indices(MOI.get(mock.inner_model, attr, xor_index(idx)))
end
function MOI.get(
mock::MockOptimizer,
attr::MOI.ConstraintDual,
idx::MOI.ConstraintIndex{F},
) where {F}
MOI.throw_if_not_valid(mock, idx)
if mock.eval_variable_constraint_dual &&
(F == MOI.VariableIndex || F == MOI.VectorOfVariables)
return get_fallback(mock, attr, idx)
else
MOI.check_result_index_bounds(mock, attr)
return _safe_get_result(mock.constraint_dual, attr, idx, "dual")
end
end
function MOI.get(
mock::MockOptimizer,
::MockConstraintAttribute,
idx::MOI.ConstraintIndex,
)
return mock.mock_constraint_attribute[xor_index(idx)]
end
function MOI.get(
mock::MockOptimizer,
attr::MOI.ConstraintBasisStatus,
idx::MOI.ConstraintIndex,
)
MOI.check_result_index_bounds(mock, attr)
MOI.throw_if_not_valid(mock, idx)
return _safe_get_result(
mock.constraint_basis_status,
attr,
idx,
"basis status",
)
end
function MOI.get(
mock::MockOptimizer,
attr::MOI.VariableBasisStatus,
idx::MOI.VariableIndex,
)
MOI.check_result_index_bounds(mock, attr)
MOI.throw_if_not_valid(mock, idx)
return _safe_get_result(
mock.variable_basis_status,
attr,
idx,
"basis status",
)
end
function MOI.get(
mock::MockOptimizer,
::MOI.ConstraintConflictStatus,
idx::MOI.ConstraintIndex,
)
MOI.throw_if_not_valid(mock, idx)
return mock.constraint_conflict_status[xor_index(idx)]
end
function _safe_set_result(
dict::Dict{K,V},
attr::MOI.AnyAttribute,
index::K,
value,
) where {K,V}
xored = xor_index(index)
if !haskey(dict, xored)
dict[xored] = V()
end
return dict[xored][attr.result_index] = value
end
function _safe_get_result(
dict::Dict,
attr::MOI.AnyAttribute,
index::MOI.Index,
name::String,
)
index_name = index isa MOI.VariableIndex ? "variable" : "constraint"
result_to_value = get(dict, xor_index(index), nothing)
if result_to_value === nothing
error("No mock $name is set for ", index_name, " `", index, "`.")
end
value = get(result_to_value, attr.result_index, nothing)
if value === nothing
error(
"No mock $name is set for $(index_name) `$(index)` at result " *
"index `$(attr.result_index)`.",
)
end
return value
end
MOI.get(::MockOptimizer, ::MOI.SolverName) = "Mock"
MOI.get(::MockOptimizer, ::MOI.SolverVersion) = "v0.1.0"
function MOI.empty!(mock::MockOptimizer)
MOI.empty!(mock.inner_model)
mock.mock_model_attribute = 0
empty!(mock.mock_variable_attribute)
empty!(mock.mock_constraint_attribute)
mock.optimize_called = false
mock.termination_status = MOI.OPTIMIZE_NOT_CALLED
mock.compute_conflict_called = false
mock.conflict_status = MOI.COMPUTE_CONFLICT_NOT_CALLED
empty!(mock.constraint_conflict_status)
empty!(mock.objective_value)
empty!(mock.dual_objective_value)
empty!(mock.primal_status)
empty!(mock.dual_status)
empty!(mock.variable_primal)
empty!(mock.callback_variable_primal)
empty!(mock.constraint_dual)
empty!(mock.constraint_basis_status)
empty!(mock.variable_basis_status)
empty!(mock.optimizer_attributes)
empty!(mock.model_attributes)
empty!(mock.submitted)
return
end
function MOI.is_empty(mock::MockOptimizer)
# Assumes that variable and constraint attributes can't be set if
# mock.inner_model is empty.
# TODO: Default values are currently copied in three places, not good.
return MOI.is_empty(mock.inner_model) &&
mock.mock_model_attribute == 0 &&
!mock.optimize_called &&
mock.termination_status == MOI.OPTIMIZE_NOT_CALLED &&
isempty(mock.objective_value) &&
isempty(mock.dual_objective_value) &&
isempty(mock.primal_status) &&
isempty(mock.dual_status) &&
isempty(mock.constraint_basis_status) &&
isempty(mock.variable_basis_status) &&
isempty(mock.optimizer_attributes) &&
isempty(mock.model_attributes) &&
isempty(mock.submitted)
end
function MOI.is_valid(mock::MockOptimizer, idx::MOI.Index)
return MOI.is_valid(mock.inner_model, xor_index(idx))
end
function MOI.delete(mock::MockOptimizer, index::MOI.VariableIndex)
if !mock.delete_allowed
throw(MOI.DeleteNotAllowed(index))
end
if !MOI.is_valid(mock, index)
# The index thrown by `mock.inner_model` would be xored
throw(MOI.InvalidIndex(index))
end
MOI.delete(mock.inner_model, xor_index(index))
delete!(mock.variable_primal, index)
delete!(mock.callback_variable_primal, index)
delete!(mock.variable_basis_status, index)
return
end
function MOI.delete(mock::MockOptimizer, indices::Vector{MOI.VariableIndex})
if !mock.delete_allowed && !isempty(indices)
throw(MOI.DeleteNotAllowed(first(indices)))
end
for index in indices
# The index thrown by `mock.inner_model` would be xored
MOI.throw_if_not_valid(mock, index)
end
MOI.delete(mock.inner_model, xor_index.(indices))
for index in indices
delete!(mock.variable_primal, index)
delete!(mock.callback_variable_primal, index)
delete!(mock.variable_basis_status, index)
end
return
end
function MOI.delete(mock::MockOptimizer, index::MOI.ConstraintIndex)
if !mock.delete_allowed
throw(MOI.DeleteNotAllowed(index))
end
if !MOI.is_valid(mock, index)
# The index thrown by `mock.inner_model` would be xored
throw(MOI.InvalidIndex(index))
end
MOI.delete(mock.inner_model, xor_index(index))
delete!(mock.constraint_dual, index)
delete!(mock.constraint_basis_status, index)
return
end
function MOI.modify(
mock::MockOptimizer,
c::CI,
change::MOI.AbstractFunctionModification,
)
if !mock.modify_allowed
throw(MOI.ModifyConstraintNotAllowed(c, change))
end
MOI.modify(mock.inner_model, xor_index(c), xor_indices(change))
return
end
function MOI.set(
mock::MockOptimizer,
::MOI.ConstraintSet,
c::CI{<:MOI.AbstractFunction,S},
set::S,
) where {S<:MOI.AbstractSet}
MOI.set(mock.inner_model, MOI.ConstraintSet(), xor_index(c), set)
return
end
function MOI.set(
mock::MockOptimizer,
::MOI.ConstraintFunction,
c::CI{F},
func::F,
) where {F<:MOI.AbstractFunction}
MOI.set(
mock.inner_model,
MOI.ConstraintFunction(),
xor_index(c),
xor_indices(func),
)
return
end
function MOI.modify(
mock::MockOptimizer,
obj::MOI.ObjectiveFunction,
change::MOI.AbstractFunctionModification,
)
if !mock.modify_allowed
throw(MOI.ModifyObjectiveNotAllowed(change))
end
MOI.modify(mock.inner_model, obj, xor_indices(change))
return
end
MOI.supports(::MockOptimizer, ::MOI.AbstractSubmittable) = true
function MOI.submit(mock::MockOptimizer, sub::MOI.AbstractSubmittable, args...)
if !haskey(mock.submitted, sub)
mock.submitted[sub] = Tuple[]
end
push!(mock.submitted[sub], args)
return
end
# TODO: transform
function MOI.supports_constraint(
mock::MockOptimizer,
F::Type{<:MOI.AbstractFunction},
S::Type{<:MOI.AbstractSet},
)
return MOI.supports_constraint(mock.inner_model, F, S)
end
function MOI.supports_add_constrained_variable(
mock::MockOptimizer,
S::Type{<:MOI.AbstractScalarSet},
)
return MOI.supports_add_constrained_variable(mock.inner_model, S)
end
function MOI.supports_add_constrained_variables(
mock::MockOptimizer,
S::Type{<:MOI.AbstractVectorSet},
)
return MOI.supports_add_constrained_variables(mock.inner_model, S)
end
# Add this method to avoid ambiguity
function MOI.supports_add_constrained_variables(
mock::MockOptimizer,
::Type{MOI.Reals},
)
return MOI.supports_add_constrained_variables(mock.inner_model, MOI.Reals)
end
function MOI.copy_to(dest::MockOptimizer, src::MOI.ModelLike; kwargs...)
return default_copy_to(dest, src; kwargs...)
end
function MOI.supports_incremental_interface(mock::MockOptimizer)
return MOI.supports_incremental_interface(mock.inner_model)
end
function final_touch(uf::MockOptimizer, index_map)
return final_touch(uf.inner_model, index_map)
end
"""
set_mock_optimize!(mock::MockOptimizer, opt::Function...)
Sets multiple optimize! function. The first is to be used the first time
`MOI.optimize!(mock)` is called, the second function is to be used the second
time, ...
"""
function set_mock_optimize!(mock::MockOptimizer, opts::Function...)
mock.optimize! = _recursive_mock_optimize(opts...)
return
end
_recursive_mock_optimize(optimize::Function) = optimize
function _recursive_mock_optimize(optimize::Function, tail::Function...)
return (mock::MockOptimizer) -> begin
optimize(mock)
mock.optimize! = _recursive_mock_optimize(tail...)
return
end
end
"""
mock_optimize!(
mock::MockOptimizer,
termination_status::MOI.TerminationStatusCode,
primal::Union{
Tuple{MOI.ResultStatusCode,<:Vector},
MOI.ResultStatusCode,
<:Vector,
}
dual_status::MOI.ResultStatusCode,
constraint_duals::Pair{Tuple{DataTypeDataType},<:Vector}...;
constraint_basis_status = Pair{Tuple{DataTypeDataType},<:Vector}[],
variable_basis_status = MOI.BasisStatusCode[],
constraint_conflict_status = Pair{Tuple{Type,Type},<:Vector}[],
)
Fake the result of a call to `optimize!` in the mock optimizer by storing the
solution.
## Arguments