-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathMOI_wrapper.jl
4387 lines (3979 loc) · 144 KB
/
MOI_wrapper.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
@static if v"1.2" > VERSION >= v"1.1"
# see: https://github.com/jump-dev/Xpress.jl/pull/44#issuecomment-585882858
error("Versions 1.1.x of julia are not supported. The current verions is $(VERSION)")
end
import MathOptInterface
using SparseArrays
const MOI = MathOptInterface
const CleverDicts = MOI.Utilities.CleverDicts
@enum(
VariableType,
CONTINUOUS,
BINARY,
INTEGER,
SEMIINTEGER,
SEMICONTINUOUS,
)
@enum(
ConstraintType,
AFFINE,
INDICATOR,
QUADRATIC,
SOC,
RSOC,
SOS_SET
)
@enum(
BoundType,
NONE,
LESS_THAN,
GREATER_THAN,
LESS_AND_GREATER_THAN,
INTERVAL,
EQUAL_TO,
)
@enum(
ObjectiveType,
SINGLE_VARIABLE,
SCALAR_AFFINE,
SCALAR_QUADRATIC,
)
@enum(
CallbackState,
CB_NONE,
CB_GENERIC,
CB_LAZY,
CB_USER_CUT,
CB_HEURISTIC,
)
const SCALAR_SETS = Union{
MOI.GreaterThan{Float64},
MOI.LessThan{Float64},
MOI.EqualTo{Float64},
MOI.Interval{Float64},
}
const SIMPLE_SCALAR_SETS = Union{
MOI.GreaterThan{Float64},
MOI.LessThan{Float64},
MOI.EqualTo{Float64},
}
const INDICATOR_SETS = Union{
MOI.Indicator{MOI.ACTIVATE_ON_ONE,MOI.GreaterThan{Float64}},
MOI.Indicator{MOI.ACTIVATE_ON_ZERO,MOI.GreaterThan{Float64}},
MOI.Indicator{MOI.ACTIVATE_ON_ONE,MOI.LessThan{Float64}},
MOI.Indicator{MOI.ACTIVATE_ON_ZERO,MOI.LessThan{Float64}},
MOI.Indicator{MOI.ACTIVATE_ON_ONE,MOI.EqualTo{Float64}},
MOI.Indicator{MOI.ACTIVATE_ON_ZERO,MOI.EqualTo{Float64}},
}
mutable struct VariableInfo
index::MOI.VariableIndex
column::Int
bound::BoundType
type::VariableType
start::Union{Float64, Nothing}
name::String
# Storage for constraint names associated with variables because Xpress can
# only store names for variables and proper constraints. We can perform an
# optimization and only store three strings for the constraint names
# because, at most, there can be three VariableIndex constraints, e.g.,
# LessThan, GreaterThan, and Integer.
lessthan_name::String
greaterthan_interval_or_equalto_name::String
type_constraint_name::String
# Storage for the lower bound if the variable is the `t` variable in a
# second order cone.
lower_bound_if_soc::Float64
num_soc_constraints::Int # this cannot be more than one in xpress
in_soc::Bool
previous_lower_bound::Float64
previous_upper_bound::Float64
semi_lower_bound::Float64
function VariableInfo(index::MOI.VariableIndex, column::Int)
return new(
index,
column,
NONE,
CONTINUOUS,
nothing,
"",
"",
"",
"",
NaN,
0,
false,
NaN,
NaN,
NaN
)
end
end
mutable struct ConstraintInfo
row::Int
set::MOI.AbstractSet
# Storage for constraint names. Where possible, these are also stored in the
# Xpress model.
# avoid passing names to xpress because it is a slow operation
# perhaps call lazy on calls for writing lps and so on
name::String
type::ConstraintType
ConstraintInfo(row::Int, set::MOI.AbstractSet, type::ConstraintType) = new(row, set, "", type)
end
mutable struct CachedSolution
variable_primal::Vector{Float64}
variable_dual::Vector{Float64}
linear_primal::Vector{Float64}
linear_dual::Vector{Float64}
has_primal_certificate::Bool
has_dual_certificate::Bool
has_feasible_point::Bool
solve_time::Float64
end
mutable struct CallbackCutData
submitted::Bool
cutptrs::Vector{Lib.XPRScut}
end
mutable struct BasisStatus
con_status::Vector{Cint}
var_status::Vector{Cint}
end
mutable struct SensitivityCache
input::Vector{Float64}
output::Vector{Float64}
is_updated::Bool
end
mutable struct IISData
stat::Cint
is_standard_iis::Bool
rownumber::Int # number of rows participating in the IIS
colnumber::Int # number of columns participating in the IIS
miisrow::Vector{Cint} # index of the rows that participate
miiscol::Vector{Cint} # index of the columns that participate
constrainttype::Vector{UInt8} # sense of the rows that participate
colbndtype::Vector{UInt8} # sense of the column bounds that participate
end
mutable struct Optimizer <: MOI.AbstractOptimizer
# The low-level Xpress model.
inner::XpressProblem
# The model name.
name::String
# A flag to keep track of MOI.Silent, which over-rides the OUTPUTLOG
# parameter.
log_level::Int32
# option to show warnings in Windows
show_warning::Bool
# turn off warning by the MOI interface implementation [advanced usage]
moi_warnings::Bool
# false by default - ignores starting points which might be expensive to load.
ignore_start::Bool
# false by default - perform the postsolve routine
post_solve::Bool
# An enum to remember what objective is currently stored in the model.
objective_type::ObjectiveType
# track whether objective function is set and the state of objective sense
is_objective_set::Bool
objective_sense::Union{Nothing,MOI.OptimizationSense}
# A mapping from the MOI.VariableIndex to the Xpress column. VariableInfo
# also stores some additional fields like what bounds have been added, the
# variable type, and the names of VariableIndex-in-Set constraints.
variable_info::CleverDicts.CleverDict{
MOI.VariableIndex,
VariableInfo,
typeof(CleverDicts.key_to_index),
typeof(CleverDicts.index_to_key),
}
# An index that is incremented for each new constraint (regardless of type).
# We can check if a constraint is valid by checking if it is in the correct
# xxx_constraint_info. We should _not_ reset this to zero, since then new
# constraints cannot be distinguished from previously created ones.
last_constraint_index::Int
# ScalarAffineFunction{Float64}-in-Set storage.
# ScalarQuadraticFunction{Float64}-in-Set storage.
# VectorAffineFunction{Float64}-in-Indicator storage.
# VectorOfVariables-in-(R)SOC) storage.
affine_constraint_info::Dict{Int, ConstraintInfo}
# VectorOfVariables-in-Set storage.
sos_constraint_info::Dict{Int, ConstraintInfo}
# Note: we do not have a singlevariable_constraint_info dictionary. Instead,
# data associated with these constraints are stored in the VariableInfo
# objects.
# Mappings from variable and constraint names to their indices. These are
# lazily built on-demand, so most of the time, they are `nothing`.
name_to_variable::Union{Nothing, Dict{String, Union{Nothing, MOI.VariableIndex}}}
name_to_constraint_index::Union{Nothing, Dict{String, Union{Nothing, MOI.ConstraintIndex}}}
# TODO: add functionality to the lower-level API to support querying single
# elements of the solution.
cached_solution::Union{Nothing, CachedSolution}
basis_status::Union{Nothing, BasisStatus}
conflict::Union{Nothing, IISData}
termination_status::MOI.TerminationStatusCode
primal_status::MOI.ResultStatusCode
dual_status::MOI.ResultStatusCode
solve_method::String
solve_relaxation::Bool
#Stores the input and output of derivatives
forward_sensitivity_cache::Union{Nothing, SensitivityCache}
backward_sensitivity_cache::Union{Nothing, SensitivityCache}
# Callback fields.
callback_cached_solution::Union{Nothing, CachedSolution}
cb_cut_data::CallbackCutData
callback_state::CallbackState
cb_exception::Union{Nothing, Exception}
lazy_callback::Union{Nothing, Function}
user_cut_callback::Union{Nothing, Function}
heuristic_callback::Union{Nothing, Function}
has_generic_callback::Bool
callback_data::Union{Nothing, Tuple{Ptr{Nothing}, _CallbackUserData}}
message_callback::Union{Nothing, Tuple{Ptr{Nothing}, _CallbackUserData}}
params::Dict{Any, Any}
"""
Optimizer()
Create a new Optimizer object.
"""
function Optimizer(; kwargs...)
model = new()
model.params = Dict{Any,Any}()
model.log_level = 1 # is xpress default
model.show_warning = true
model.moi_warnings = true
model.ignore_start = false
model.post_solve = true
model.solve_method = ""
model.solve_relaxation = false
model.message_callback = nothing
model.termination_status = MOI.OPTIMIZE_NOT_CALLED
model.primal_status = MOI.NO_SOLUTION
model.dual_status = MOI.NO_SOLUTION
for (name, value) in kwargs
name = MOI.RawOptimizerAttribute(string(name))
model.params[name] = value
end
model.variable_info = CleverDicts.CleverDict{MOI.VariableIndex, VariableInfo}()
model.affine_constraint_info = Dict{Int, ConstraintInfo}()
model.sos_constraint_info = Dict{Int, ConstraintInfo}()
MOI.empty!(model) # inner is initialized here
return model
end
end
Base.show(io::IO, model::Optimizer) = show(io, model.inner)
function MOI.empty!(model::Optimizer)
model.inner = XpressProblem()
for (name, value) in model.params
MOI.set(model, name, value)
end
MOI.set(model, MOI.RawOptimizerAttribute("MPSNAMELENGTH"), 64)
MOI.set(model, MOI.RawOptimizerAttribute("CALLBACKFROMMASTERTHREAD"), 1)
MOI.set(model, MOI.RawOptimizerAttribute("XPRESS_WARNING_WINDOWS"), model.show_warning)
# disable log caching previous state
log_level = model.log_level
log_level != 0 && MOI.set(model, MOI.RawOptimizerAttribute("OUTPUTLOG"), 0)
# silently load a empty model - to avoid useless printing
@checked Lib.XPRSloadlp(model.inner, "", 0, 0, C_NULL, C_NULL, C_NULL, C_NULL, C_NULL, C_NULL, C_NULL, C_NULL, C_NULL, C_NULL)
# re-enable logging
log_level != 0 && MOI.set(model, MOI.RawOptimizerAttribute("OUTPUTLOG"), log_level)
model.name = ""
model.objective_type = SCALAR_AFFINE
model.is_objective_set = false
model.objective_sense = nothing
empty!(model.variable_info)
model.last_constraint_index = 0
empty!(model.affine_constraint_info)
empty!(model.sos_constraint_info)
model.name_to_variable = nothing
model.name_to_constraint_index = nothing
model.cached_solution = nothing
model.basis_status = nothing
model.conflict = nothing
model.termination_status = MOI.OPTIMIZE_NOT_CALLED
model.primal_status = MOI.NO_SOLUTION
model.dual_status = MOI.NO_SOLUTION
model.callback_cached_solution = nothing
model.cb_cut_data = CallbackCutData(false, Array{Lib.XPRScut}(undef,0))
model.callback_state = CB_NONE
model.cb_exception = nothing
model.forward_sensitivity_cache = nothing
model.backward_sensitivity_cache = nothing
model.lazy_callback = nothing
model.user_cut_callback = nothing
model.heuristic_callback = nothing
model.has_generic_callback = false
model.callback_data = nothing
# model.message_callback = nothing
for (name, value) in model.params
MOI.set(model, name, value)
end
return
end
function MOI.is_empty(model::Optimizer)
!isempty(model.name) && return false
model.objective_type != SCALAR_AFFINE && return false
model.is_objective_set == true && return false
model.objective_sense !== nothing && return false
!isempty(model.variable_info) && return false
length(model.affine_constraint_info) != 0 && return false
length(model.sos_constraint_info) != 0 && return false
model.name_to_variable !== nothing && return false
model.name_to_constraint_index !== nothing && return false
model.cached_solution !== nothing && return false
model.basis_status !== nothing && return false
model.conflict !== nothing && return false
model.termination_status != MOI.OPTIMIZE_NOT_CALLED && return false
model.primal_status != MOI.NO_SOLUTION && return false
model.dual_status != MOI.NO_SOLUTION && return false
model.callback_cached_solution !== nothing && return false
# model.cb_cut_data !== nothing && return false
model.callback_state != CB_NONE && return false
model.cb_exception !== nothing && return false
model.lazy_callback !== nothing && return false
model.user_cut_callback !== nothing && return false
model.heuristic_callback !== nothing && return false
model.has_generic_callback && return false
model.callback_data !== nothing && return false
# model.message_callback !== nothing && return false
# otherwise jump complains it is not empty
return true
end
function reset_cached_solution(model::Optimizer)
num_variables = length(model.variable_info)
num_affine = length(model.affine_constraint_info)
if model.cached_solution === nothing
model.cached_solution = CachedSolution(
fill(NaN, num_variables),
fill(NaN, num_variables),
fill(NaN, num_affine),
fill(NaN, num_affine),
false,
false,
false,
NaN
)
else
resize!(model.cached_solution.variable_primal, num_variables)
resize!(model.cached_solution.variable_dual, num_variables)
resize!(model.cached_solution.linear_primal, num_affine)
resize!(model.cached_solution.linear_dual, num_affine)
model.cached_solution.has_primal_certificate = false
model.cached_solution.has_dual_certificate = false
model.cached_solution.has_feasible_point = false
model.cached_solution.solve_time = NaN
end
return model.cached_solution
end
function reset_callback_cached_solution(model::Optimizer)
num_variables = length(model.variable_info)
num_affine = length(model.affine_constraint_info)
if model.callback_cached_solution === nothing
model.callback_cached_solution = CachedSolution(
fill(NaN, num_variables),
fill(NaN, num_variables),
fill(NaN, num_affine),
fill(NaN, num_affine),
false,
false,
false,
NaN
)
else
resize!(model.callback_cached_solution.variable_primal, num_variables)
resize!(model.callback_cached_solution.variable_dual, num_variables)
resize!(model.callback_cached_solution.linear_primal, num_affine)
resize!(model.callback_cached_solution.linear_dual, num_affine)
model.callback_cached_solution.has_primal_certificate = false
model.callback_cached_solution.has_dual_certificate = false
model.callback_cached_solution.has_feasible_point = false
model.callback_cached_solution.solve_time = NaN
end
return model.callback_cached_solution
end
MOI.get(::Optimizer, ::MOI.SolverName) = "Xpress"
# Currently this returns the version of the Xpress package as a whole
# which is different from the Xpress Optimizer version
# the first is a good match because is the version number that appears
# in the dowload package
function MOI.get(optimizer::Optimizer, ::MOI.SolverVersion)
MOI.get(optimizer, MOI.RawOptimizerAttribute("XPRESSVERSION"))
end
function MOI.supports(
::Optimizer,
::MOI.ObjectiveFunction{F}
) where {F <: Union{
MOI.VariableIndex,
MOI.ScalarAffineFunction{Float64},
MOI.ScalarQuadraticFunction{Float64},
}}
return true
end
function MOI.supports_constraint(
::Optimizer, ::Type{MOI.VariableIndex}, ::Type{F}
) where {F <: Union{
MOI.EqualTo{Float64},
MOI.LessThan{Float64},
MOI.GreaterThan{Float64},
MOI.Interval{Float64},
MOI.ZeroOne,
MOI.Integer,
MOI.Semicontinuous{Float64},
MOI.Semiinteger{Float64},
}}
return true
end
function MOI.supports_constraint(
::Optimizer, ::Type{MOI.VectorOfVariables}, ::Type{F}
) where {F <: Union{
MOI.SOS1{Float64},
MOI.SOS2{Float64},
MOI.SecondOrderCone,
MOI.RotatedSecondOrderCone,
}
}
# Xpress only supports disjoint sets of SOC and RSOC (with no affine forms)
# hence we only allow constraints on creation
return true
end
function MOI.supports_add_constrained_variables(
::Optimizer, ::Type{F}
) where {F <: Union{
MOI.SecondOrderCone,
MOI.RotatedSecondOrderCone,
}
}
# Xpress only supports disjoint sets of SOC and RSOC (with no affine forms)
# hence we only allow constraints on creation
return true
end
# We choose _not_ to support ScalarAffineFunction-in-Interval and
# ScalarQuadraticFunction-in-Interval due to the need for range constraints
# and the added complexity.
function MOI.supports_constraint(
::Optimizer, ::Type{MOI.ScalarAffineFunction{Float64}}, ::Type{F}
) where {F <: SIMPLE_SCALAR_SETS}
return true
end
function MOI.supports_constraint(
::Optimizer, ::Type{MOI.ScalarQuadraticFunction{Float64}}, ::Type{F}
) where {F <: Union{
MOI.LessThan{Float64}, MOI.GreaterThan{Float64}
}}
# Note: Xpress does not support quadratic equality constraints.
return true
end
MOI.supports_constraint(::Optimizer,
::Type{<:MOI.VectorAffineFunction},
::Type{T}) where T <: INDICATOR_SETS = true
MOI.supports(::Optimizer, ::MOI.VariableName, ::Type{MOI.VariableIndex}) = true
MOI.supports(::Optimizer, ::MOI.ConstraintName, ::Type{<:MOI.ConstraintIndex}) = true
MOI.supports(::Optimizer, ::MOI.Name) = true
MOI.supports(::Optimizer, ::MOI.Silent) = true
MOI.supports(::Optimizer, ::MOI.NumberOfThreads) = true
MOI.supports(::Optimizer, ::MOI.TimeLimitSec) = true
MOI.supports(::Optimizer, ::MOI.ObjectiveSense) = true
MOI.supports(::Optimizer, ::MOI.RawOptimizerAttribute) = true
function MOI.set(model::Optimizer, param::MOI.RawOptimizerAttribute, value)
# Always store value in params dictionary when setting
# This is because when calling `empty!` we create a new XpressProblem and
# and want to set all the raw parameters and attributes again.
model.params[param] = value
if param == MOI.RawOptimizerAttribute("logfile")
if value == ""
# disable log file
@checked Lib.XPRSsetlogfile(model.inner, C_NULL)
else
@checked Lib.XPRSsetlogfile(model.inner, value)
end
model.inner.logfile = value
reset_message_callback(model)
elseif param == MOI.RawOptimizerAttribute("MOI_POST_SOLVE")
model.post_solve = value
elseif param == MOI.RawOptimizerAttribute("MOI_IGNORE_START")
model.ignore_start = value
elseif param == MOI.RawOptimizerAttribute("MOI_WARNINGS")
model.moi_warnings = value
elseif param == MOI.RawOptimizerAttribute("MOI_SOLVE_MODE")
# https://www.fico.com/fico-xpress-optimization/docs/latest/solver/optimizer/R/HTML/lpoptimize.html
model.solve_method = value
elseif param == MOI.RawOptimizerAttribute("XPRESS_WARNING_WINDOWS")
model.show_warning = value
reset_message_callback(model)
elseif param == MOI.RawOptimizerAttribute("OUTPUTLOG")
model.log_level = value
Xpress.setcontrol!(model.inner, "OUTPUTLOG", value)
reset_message_callback(model)
else
Xpress.setcontrol!(model.inner, param.name, value)
end
return
end
function reset_message_callback(model)
if model.message_callback !== nothing
# remove all message callbacks
@checked Lib.XPRSremovecbmessage(model.inner, C_NULL, C_NULL)
model.message_callback = nothing
end
if model.inner.logfile == "" && # no file -> screen
model.log_level != 0 # has log
model.message_callback = setoutputcb!(model.inner, model.show_warning)
end
end
function MOI.get(model::Optimizer, param::MOI.RawOptimizerAttribute)
if param == MOI.RawOptimizerAttribute("logfile")
return model.inner.logfile
elseif param == MOI.RawOptimizerAttribute("MOI_IGNORE_START")
return model.ignore_start
elseif param == MOI.RawOptimizerAttribute("MOI_POST_SOLVE")
return model.post_solve
elseif param == MOI.RawOptimizerAttribute("MOI_WARNINGS")
return model.moi_warnings
elseif param == MOI.RawOptimizerAttribute("MOI_SOLVE_MODE")
return model.solve_method
elseif param == MOI.RawOptimizerAttribute("XPRESS_WARNING_WINDOWS")
return model.show_warning
else
return Xpress.get_control_or_attribute(model.inner, param.name)
end
end
function MOI.set(model::Optimizer, ::MOI.TimeLimitSec, limit::Real)
# positive values would mean that its stops after `limit` seconds
# iff there is already a MIP solution available.
MOI.set(model, MOI.RawOptimizerAttribute("MAXTIME"), -floor(Int32, limit))
return
end
function MOI.get(model::Optimizer, ::MOI.TimeLimitSec)
# MOI.attribute_value_type(MOI.TimeLimitSec()) = Union{Nothing, Float64}
return convert(Float64, -MOI.get(model, MOI.RawOptimizerAttribute("MAXTIME")))
end
MOI.supports_incremental_interface(::Optimizer) = true
function MOI.copy_to(dest::Optimizer, src::MOI.ModelLike)
return MOI.Utilities.default_copy_to(dest, src)
end
function MOI.get(model::Optimizer, ::MOI.ListOfVariableAttributesSet)
ret = MOI.AbstractVariableAttribute[]
found_name = any(!isempty(info.name) for info in values(model.variable_info))
found_start = any(info.start !== nothing for info in values(model.variable_info))
if found_name
push!(ret, MOI.VariableName())
end
if found_start
push!(ret, MOI.VariablePrimalStart())
end
return ret
end
function MOI.get(model::Optimizer, ::MOI.ListOfModelAttributesSet)
if MOI.is_empty(model)
return Any[]
end
attributes = Any[]
if model.objective_sense !== nothing
push!(attributes, MOI.ObjectiveSense())
end
typ = MOI.get(model, MOI.ObjectiveFunctionType())
if typ !== nothing
push!(attributes, MOI.ObjectiveFunction{typ}())
end
if MOI.get(model, MOI.Name()) != ""
push!(attributes, MOI.Name())
end
return attributes
end
function MOI.get(model::Optimizer, ::MOI.ListOfConstraintAttributesSet{F, S}) where {S, F}
ret = MOI.AbstractConstraintAttribute[]
constraint_indices = MOI.get(model, MOI.ListOfConstraintIndices{F, S}())
found_name = any(!isempty(MOI.get(model, MOI.ConstraintName(), index)) for index in constraint_indices)
if found_name
push!(ret, MOI.ConstraintName())
end
return ret
end
function _indices_and_coefficients(
indices::AbstractVector{<:Integer},
coefficients::AbstractVector{Float64},
model::Optimizer,
f::MOI.ScalarAffineFunction{Float64}
)
for (i, term) in enumerate(f.terms)
indices[i] = _info(model, term.variable).column
coefficients[i] = term.coefficient
end
return indices, coefficients
end
function _indices_and_coefficients(
model::Optimizer, f::MOI.ScalarAffineFunction{Float64}
)
f_canon = MOI.Utilities.canonical(f)
nnz = length(f_canon.terms)
indices = Vector{Cint}(undef, nnz)
coefficients = Vector{Float64}(undef, nnz)
_indices_and_coefficients(indices, coefficients, model, f_canon)
return indices, coefficients
end
function _indices_and_coefficients_indicator(
model::Optimizer, f::MOI.VectorAffineFunction
)
nnz = length(f.terms) - 1
indices = Vector{Cint}(undef, nnz)
coefficients = Vector{Float64}(undef, nnz)
i = 1
for fi in f.terms
if fi.output_index != 1
indices[i] = _info(model,fi.scalar_term.variable).column
coefficients[i] = fi.scalar_term.coefficient
i += 1
end
end
return indices, coefficients
end
function _indices_and_coefficients(
I::AbstractVector{Cint},
J::AbstractVector{Cint},
V::AbstractVector{Float64},
indices::AbstractVector{Cint},
coefficients::AbstractVector{Float64},
model::Optimizer,
f::MOI.ScalarQuadraticFunction
)
for (i, term) in enumerate(f.quadratic_terms)
I[i] = _info(model, term.variable_1).column
J[i] = _info(model, term.variable_2).column
V[i] = term.coefficient
# MOI represents objective as 0.5 x' Q x
# Example: obj = 2x^2 + x*y + y^2
# = 2x^2 + (1/2)*x*y + (1/2)*y*x + y^2
# |x y| * |a b| * |x| = |ax+by bx+cy| * |x| = 0.5ax^2 + bxy + 0.5cy^2
# |b c| |y| |y|
# Hence:
# 0.5*Q = | 2 1/2 | => Q = | 4 1 |
# | 1/2 1 | | 1 2 |
# Only one triangle (upper and lower are equal) is saved in MOI
# Hence:
# ScalarQuadraticTerm.([4.0, 1.0, 2.0], [x, x, y], [x, y, y])
# Xpress ALSO represents objective as 0.5 x' Q x
# Again, only one triangle is added.
# In other words,
# Xpress uses the SAME convention as MOI for OBJECTIVE
# Hence, no modifications are needed for OBJECTIVE.
# However,
# For onstraints, Xpress does NOT have the 0.5 factor in front of the Q matrix
# Hence,
# Only for constraints, MOI -> Xpress => divide all by 2
# Only for constraints, Xpress -> MOI => multiply all by 2
end
for (i, term) in enumerate(f.affine_terms)
indices[i] = _info(model, term.variable).column
coefficients[i] = term.coefficient
end
return
end
function _indices_and_coefficients(
model::Optimizer, f::MOI.ScalarQuadraticFunction
)
f_canon = MOI.Utilities.canonical(f)
nnz_quadratic = length(f_canon.quadratic_terms)
nnz_affine = length(f_canon.affine_terms)
I = Vector{Cint}(undef, nnz_quadratic)
J = Vector{Cint}(undef, nnz_quadratic)
V = Vector{Float64}(undef, nnz_quadratic)
indices = Vector{Cint}(undef, nnz_affine)
coefficients = Vector{Float64}(undef, nnz_affine)
_indices_and_coefficients(I, J, V, indices, coefficients, model, f_canon)
return indices, coefficients, I, J, V
end
_sense_and_rhs(s::MOI.LessThan{Float64}) = (Cchar('L'), s.upper)
_sense_and_rhs(s::MOI.GreaterThan{Float64}) = (Cchar('G'), s.lower)
_sense_and_rhs(s::MOI.EqualTo{Float64}) = (Cchar('E'), s.value)
###
### Variables
###
# Short-cuts to return the VariableInfo associated with an index.
function _info(model::Optimizer, key::MOI.VariableIndex)
if !haskey(model.variable_info, key)
throw(MOI.InvalidIndex(key))
end
return model.variable_info[key]
end
function MOI.add_variable(model::Optimizer)
# Initialize `VariableInfo` with a dummy `VariableIndex` and a column,
# because we need `add_item` to tell us what the `VariableIndex` is.
index = CleverDicts.add_item(
model.variable_info, VariableInfo(MOI.VariableIndex(0), 0)
)
info = _info(model, index)
info.index = index
info.column = length(model.variable_info)
@checked Lib.XPRSaddcols(
model.inner,
1,#length(_dbdl)::Int,
0,#length(_dmatval)::Int,
Ref(0.0),#_dobj::Vector{Float64},
C_NULL,#Cint.(_mrwind::Vector{Int}),
C_NULL,#Cint.(_mrstart::Vector{Int}),
C_NULL,#_dmatval::Vector{Float64},
Ref(-Inf),#_dbdl::Vector{Float64},
Ref(Inf),#_dbdu::Vector{Float64}
)
return index
end
function MOI.add_variables(model::Optimizer, N::Int)
@checked Lib.XPRSaddcols(
model.inner,
N,#length(_dbdl)::Int,
0,#length(_dmatval)::Int,
zeros(N),# _dobj::Vector{Float64},
C_NULL,#Cint.(_mrwind::Vector{Int}),
C_NULL,#Cint.(_mrstart::Vector{Int}),
C_NULL,# _dmatval::Vector{Float64},
fill(-Inf, N),# _dbdl::Vector{Float64},
fill(Inf, N),# _dbdu::Vector{Float64}
)
indices = Vector{MOI.VariableIndex}(undef, N)
num_variables = length(model.variable_info)
for i in 1:N
# Initialize `VariableInfo` with a dummy `VariableIndex` and a column,
# because we need `add_item` to tell us what the `VariableIndex` is.
index = CleverDicts.add_item(
model.variable_info, VariableInfo(MOI.VariableIndex(0), 0)
)
info = _info(model, index)
info.index = index
info.column = num_variables + i
indices[i] = index
end
return indices
end
function MOI.is_valid(model::Optimizer, v::MOI.VariableIndex)
return haskey(model.variable_info, v)
end
function MOI.delete(model::Optimizer, v::MOI.VariableIndex)
info = _info(model, v)
if info.num_soc_constraints > 0
throw(MOI.DeleteNotAllowed(v))
end
@checked Lib.XPRSdelcols(model.inner, 1, Ref{Cint}(info.column - 1))
delete!(model.variable_info, v)
for other_info in values(model.variable_info)
if other_info.column > info.column
other_info.column -= 1
end
end
model.name_to_variable = nothing
# We throw away name_to_constraint_index so we will rebuild VariableIndex
# constraint names without v.
model.name_to_constraint_index = nothing
return
end
function MOI.get(model::Optimizer, ::Type{MOI.VariableIndex}, name::String)
if model.name_to_variable === nothing
_rebuild_name_to_variable(model)
end
if haskey(model.name_to_variable, name)
variable = model.name_to_variable[name]
if variable === nothing
error("Duplicate variable name detected: $(name)")
end
return variable
end
return nothing
end
function _rebuild_name_to_variable(model::Optimizer)
model.name_to_variable = Dict{String, Union{Nothing, MOI.VariableIndex}}()
for (index, info) in model.variable_info
if info.name == ""
continue
end
if haskey(model.name_to_variable, info.name)
model.name_to_variable[info.name] = nothing
else
model.name_to_variable[info.name] = index
end
end
return
end
function MOI.get(model::Optimizer, ::MOI.VariableName, v::MOI.VariableIndex)
return _info(model, v).name
end
function MOI.set(
model::Optimizer, ::MOI.VariableName, v::MOI.VariableIndex, name::String
)
info = _info(model, v)
info.name = name
# Note: don't set the string names in the Xpress C API because it complains
# on duplicate variables.
# That is, don't call `Lib.XPRSaddnames`.
model.name_to_variable = nothing
return
end
###
### Sensitivities
###
struct ForwardSensitivityInputConstraint <: MOI.AbstractConstraintAttribute end
struct ForwardSensitivityOutputVariable <: MOI.AbstractVariableAttribute end
struct BackwardSensitivityInputVariable <: MOI.AbstractVariableAttribute end
struct BackwardSensitivityOutputConstraint <: MOI.AbstractConstraintAttribute end
MOI.is_set_by_optimize(::ForwardSensitivityOutputVariable) = true
MOI.is_set_by_optimize(::BackwardSensitivityOutputConstraint) = true
function forward(model::Optimizer)
rows = @_invoke Lib.XPRSgetintattrib(model.inner, Lib.XPRS_ROWS, _)::Int
spare_rows = @_invoke Lib.XPRSgetintattrib(model.inner, Lib.XPRS_SPAREROWS, _)::Int
cols = @_invoke Lib.XPRSgetintattrib(model.inner, Lib.XPRS_COLS, _)::Int
#1 - Create vector 'aux_vector' of size ROWS of type Float64 (constraints)
aux_vector = copy(model.forward_sensitivity_cache.input)
#2 - Call XPRSftran with vector 'aux_vector' as an argument
@checked Lib.XPRSftran(model.inner, aux_vector)
#3 - Create Dict of Basic variable to All variables
basic_variables_ordered = Vector{Cint}(undef, rows)
@checked Lib.XPRSgetpivotorder(model.inner, basic_variables_ordered)
aux_dict = Dict{Int, Int}()
for i in 1:length(basic_variables_ordered)
if rows+spare_rows <= basic_variables_ordered[i] <= rows+spare_rows + cols - 1
aux_dict[i] = basic_variables_ordered[i] - (rows+spare_rows) + 1
end
end
#5 - Populate vector of All variables with the correct value of the Basic variables
fill!(model.forward_sensitivity_cache.output, 0.0)
for (bi, vi) in aux_dict
model.forward_sensitivity_cache.output[vi] = aux_vector[bi]
end
return
end
function backward(model::Optimizer)
rows = @_invoke Lib.XPRSgetintattrib(model.inner, Lib.XPRS_ROWS, _)::Int
spare_rows = @_invoke Lib.XPRSgetintattrib(model.inner, Lib.XPRS_SPAREROWS, _)::Int
cols = @_invoke Lib.XPRSgetintattrib(model.inner, Lib.XPRS_COLS, _)::Int
#1 - Get Basic variables
basic_variables_ordered = Vector{Int32}(undef, rows)
@checked Lib.XPRSgetpivotorder(model.inner, basic_variables_ordered)
aux_dict = Dict{Int,Int}()
for i in 1:length(basic_variables_ordered)
if rows + spare_rows <= basic_variables_ordered[i] <= rows + spare_rows + cols - 1
aux_dict[i] = basic_variables_ordered[i] - (rows+spare_rows) + 1
end
end
#2 - Create vector 'aux_vector' of size ROWS of type Float64 (constraints) initialized at zero
aux_vector = zeros(rows)
#3 - Populate vector 'aux_vector' with the respective values in the correct positions of the basic variables
for (bi, vi) in aux_dict
aux_vector[bi] = model.backward_sensitivity_cache.input[vi]
end
#4 - Call XPRSbtran with vector 'aux_vector' as an argument
@checked Lib.XPRSbtran(model.inner, aux_vector)
#5 - Set constraint_output equal to vector 'aux_vector'
model.backward_sensitivity_cache.output .= aux_vector
return
end
function MOI.set(
model::Optimizer, ::ForwardSensitivityInputConstraint, ci::MOI.ConstraintIndex, value::Float64
)
rows = @_invoke Lib.XPRSgetintattrib(model.inner, Lib.XPRS_ROWS, _)::Int