-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMOI_wrapper.jl
359 lines (320 loc) · 9.78 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
using MathOptInterface
const MOI = MathOptInterface
include("scaled_psd_cone_bridge.jl")
import LinearAlgebra
# SeDuMi solves the primal/dual pair
# min c'x, max b'y
# s.t. Ax = b, c - A'x ∈ K
# x ∈ K
# where K is a product of `MOI.Zeros`, `MOI.Nonnegatives`,
# `MOI.SecondOrderCone`, `MOI.RotatedSecondOrderCone` and
# `SeDuMi.ScaledPSDCone`.
# This wrapper copies the MOI problem to the SeDuMi dual so the natively
# supported supported sets are `VectorAffineFunction`-in-`S` where `S` is one
# of the sets just listed above.
MOI.Utilities.@product_of_sets(
Cones,
MOI.Zeros,
MOI.Nonnegatives,
MOI.SecondOrderCone,
MOI.RotatedSecondOrderCone,
ScaledPSDCone,
)
const OptimizerCache = MOI.Utilities.GenericModel{
Float64,
MOI.Utilities.ObjectiveContainer{Float64},
MOI.Utilities.VariablesContainer{Float64},
MOI.Utilities.MatrixOfConstraints{
Float64,
MOI.Utilities.MutableSparseMatrixCSC{
Float64,
Int,
MOI.Utilities.OneBasedIndexing,
},
Vector{Float64},
Cones{Float64},
},
}
mutable struct Solution
x::Vector{Float64}
y::Vector{Float64}
slack::Vector{Float64}
objective_value::Float64
dual_objective_value::Float64
objective_constant::Float64
info::Dict{String,Any}
end
mutable struct Optimizer <: MOI.AbstractOptimizer
cones::Union{Nothing,Cones{Float64}}
sol::Union{Nothing,Solution}
silent::Bool
options::Dict{Symbol,Any}
function Optimizer()
return new(nothing, nothing, false, Dict{Symbol,Any}())
end
end
function MOI.default_cache(::Optimizer, ::Type{Float64})
return MOI.Utilities.UniversalFallback(OptimizerCache())
end
function MOI.get(::Optimizer, ::MOI.Bridges.ListOfNonstandardBridges)
return [ScaledPSDConeBridge{Float64}]
end
MOI.get(::Optimizer, ::MOI.SolverName) = "SeDuMi"
function MOI.is_empty(optimizer::Optimizer)
return optimizer.cones === nothing
end
function MOI.empty!(optimizer::Optimizer)
optimizer.cones = nothing
optimizer.sol = nothing
return
end
###
### MOI.RawOptimizerAttribute
###
function MOI.set(optimizer::Optimizer, param::MOI.RawOptimizerAttribute, value)
optimizer.options[Symbol(param.name)] = value
return
end
function MOI.get(optimizer::Optimizer, param::MOI.RawOptimizerAttribute)
return optimizer.options[Symbol(param.name)]
end
###
### MOI.Silent
###
MOI.supports(::Optimizer, ::MOI.Silent) = true
function MOI.set(optimizer::Optimizer, ::MOI.Silent, value::Bool)
return optimizer.silent = value
end
MOI.get(optimizer::Optimizer, ::MOI.Silent) = optimizer.silent
###
### MOI.AbstractModelAttribute
###
function MOI.supports(
::Optimizer,
::Union{
MOI.ObjectiveSense,
MOI.ObjectiveFunction{MOI.ScalarAffineFunction{Float64}},
},
)
return true
end
function MOI.supports_constraint(
::Optimizer,
::Type{MOI.VectorAffineFunction{Float64}},
::Type{
<:Union{
MOI.Zeros,
MOI.Nonnegatives,
MOI.SecondOrderCone,
MOI.RotatedSecondOrderCone,
ScaledPSDCone,
},
},
)
return true
end
function _map_sets(f, sets, ::Type{S}) where {S}
F = MOI.VectorAffineFunction{Float64}
cis = MOI.get(sets, MOI.ListOfConstraintIndices{F,S}())
return Int[f(MOI.get(sets, MOI.ConstraintSet(), ci)) for ci in cis]
end
function MOI.optimize!(dest::Optimizer, src::OptimizerCache)
MOI.empty!(dest)
index_map = MOI.Utilities.identity_index_map(src)
Ac = src.constraints
A = Ac.coefficients
model_attributes = MOI.get(src, MOI.ListOfModelAttributesSet())
objective_function_attr =
MOI.ObjectiveFunction{MOI.ScalarAffineFunction{Float64}}()
b = zeros(A.n)
max_sense = MOI.get(src, MOI.ObjectiveSense()) == MOI.MAX_SENSE
objective_constant = 0.0
if objective_function_attr in MOI.get(src, MOI.ListOfModelAttributesSet())
obj = MOI.get(src, objective_function_attr)
objective_constant = MOI.constant(obj)
for term in obj.terms
b[term.variable.value] += (max_sense ? 1 : -1) * term.coefficient
end
end
# If m == n, SeDuMi thinks we give A'.
# See https://github.com/sqlp/sedumi/issues/42#issuecomment-451300096
A = SparseMatrixCSC(A.m, A.n, A.colptr, A.rowval, -A.nzval)
if A.m != A.n
A = SparseMatrixCSC(A')
end
options = dest.options
if dest.silent
options = copy(options)
options[:fid] = 0
end
K = Cone(
Ac.sets.num_rows[1],
Ac.sets.num_rows[2] - Ac.sets.num_rows[1],
_map_sets(MOI.dimension, Ac, MOI.SecondOrderCone),
_map_sets(MOI.dimension, Ac, MOI.RotatedSecondOrderCone),
_map_sets(MOI.side_dimension, Ac, ScaledPSDCone),
)
c = Ac.constants
x, y, info = sedumi(A, b, c, K; options...)
dest.cones = deepcopy(Ac.sets)
objective_value = (max_sense ? 1 : -1) * LinearAlgebra.dot(b, y)
dual_objective_value = (max_sense ? 1 : -1) * LinearAlgebra.dot(c, x)
dest.sol = Solution(
x,
y,
c - A' * y,
objective_value,
dual_objective_value,
objective_constant,
info,
)
return index_map, false
end
function MOI.optimize!(
dest::Optimizer,
src::MOI.Utilities.UniversalFallback{OptimizerCache},
)
MOI.Utilities.throw_unsupported(src)
return MOI.optimize!(dest, src.model)
end
function MOI.optimize!(dest::Optimizer, src::MOI.ModelLike)
cache = OptimizerCache()
index_map = MOI.copy_to(cache, src)
MOI.optimize!(dest, cache)
return index_map, false
end
function MOI.get(optimizer::Optimizer, ::MOI.SolveTimeSec)
return optimizer.sol.info["cpusec"]
end
function MOI.get(optimizer::Optimizer, ::MOI.RawStatusString)
return string(
"feasratio = ",
optimizer.sol.info["feasratio"],
", pinf = ",
optimizer.sol.info["pinf"],
", dinf = ",
optimizer.sol.info["dinf"],
"numerr = ",
optimizer.sol.info["numerr"],
)
end
# Implements getter for result value and statuses
# SeDuMI returns one of the following values (based on SeDuMi_Guide_11 by Pólik):
# feasratio: 1.0 problem with complementary solution
# -1.0 strongly infeasible problem
# between -1.0 and 1.0 nasty problem
# pinf = 1.0 : y is infeasibility certificate => SeDuMi primal/MOI dual is infeasible
# dinf = 1.0 : x is infeasibility certificate => SeDuMi dual/MOI primal is infeasible
# pinf = 0.0 = dinf : x and y are near feasible
# numerr: 0 desired accuracy (specified by pars.eps) is achieved
# 1 reduced accuracy (specified by pars.bigeps) is achieved
# 2 failure due to numerical problems
function MOI.get(optimizer::Optimizer, ::MOI.TerminationStatus)
if optimizer.sol isa Nothing
return MOI.OPTIMIZE_NOT_CALLED
end
pinf = optimizer.sol.info["pinf"]
dinf = optimizer.sol.info["dinf"]
numerr = optimizer.sol.info["numerr"]
if numerr == 2
return MOI.NUMERICAL_ERROR
end
@assert iszero(numerr) || isone(numerr)
accurate = iszero(numerr)
if isone(pinf)
if accurate
return MOI.DUAL_INFEASIBLE
else
return MOI.ALMOST_DUAL_INFEASIBLE
end
end
if isone(dinf)
if accurate
return MOI.INFEASIBLE
else
return MOI.ALMOST_INFEASIBLE
end
end
@assert iszero(pinf) && iszero(dinf)
# TODO when do we return SLOW_PROGRESS ?
# Maybe we should use feasratio
if accurate
return MOI.OPTIMAL
else
return MOI.ALMOST_OPTIMAL
end
end
function MOI.get(optimizer::Optimizer, attr::MOI.ObjectiveValue)
MOI.check_result_index_bounds(optimizer, attr)
value = optimizer.sol.objective_value
if !MOI.Utilities.is_ray(MOI.get(optimizer, MOI.PrimalStatus()))
value += optimizer.sol.objective_constant
end
return value
end
function MOI.get(optimizer::Optimizer, attr::MOI.DualObjectiveValue)
MOI.check_result_index_bounds(optimizer, attr)
value = optimizer.sol.dual_objective_value
if !MOI.Utilities.is_ray(MOI.get(optimizer, MOI.DualStatus()))
value += optimizer.sol.objective_constant
end
return value
end
function MOI.get(
optimizer::Optimizer,
attr::Union{MOI.PrimalStatus,MOI.DualStatus},
)
if attr.result_index > MOI.get(optimizer, MOI.ResultCount()) ||
optimizer.sol isa Nothing
return MOI.NO_SOLUTION
end
pinf = optimizer.sol.info["pinf"]
dinf = optimizer.sol.info["dinf"]
numerr = optimizer.sol.info["numerr"]
if numerr == 2
return MOI.UNKNOWN_RESULT_STATUS
end
@assert iszero(numerr) || isone(numerr)
accurate = iszero(numerr)
if isone(attr isa MOI.PrimalStatus ? pinf : dinf)
if accurate
return MOI.INFEASIBILITY_CERTIFICATE
else
return MOI.NEARLY_INFEASIBILITY_CERTIFICATE
end
end
if isone(attr isa MOI.PrimalStatus ? dinf : pinf)
return MOI.INFEASIBLE_POINT
end
@assert iszero(pinf) && iszero(dinf)
if accurate
return MOI.FEASIBLE_POINT
else
return MOI.NEARLY_FEASIBLE_POINT
end
end
function MOI.get(
optimizer::Optimizer,
attr::MOI.VariablePrimal,
vi::MOI.VariableIndex,
)
MOI.check_result_index_bounds(optimizer, attr)
return optimizer.sol.y[vi.value]
end
function MOI.get(
optimizer::Optimizer,
attr::MOI.ConstraintPrimal,
ci::MOI.ConstraintIndex,
)
MOI.check_result_index_bounds(optimizer, attr)
return optimizer.sol.slack[MOI.Utilities.rows(optimizer.cones, ci)]
end
function MOI.get(
optimizer::Optimizer,
attr::MOI.ConstraintDual,
ci::MOI.ConstraintIndex,
)
MOI.check_result_index_bounds(optimizer, attr)
return optimizer.sol.x[MOI.Utilities.rows(optimizer.cones, ci)]
end
MOI.get(optimizer::Optimizer, ::MOI.ResultCount) = 1