-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathwrapper.jl
471 lines (417 loc) · 14 KB
/
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
#=
Copyright (c) 2018-2022 Chris Coey, Lea Kapelevich, and contributors
This Julia package Hypatia.jl is released under the MIT license; see LICENSE
file in the root directory or at https://github.com/jump-dev/Hypatia.jl
=#
#=
MathOptInterface wrapper of Hypatia solver
=#
"""
$(TYPEDEF)
A MathOptInterface optimizer type for Hypatia.
"""
mutable struct Optimizer{T <: Real} <: MOI.AbstractOptimizer
solver::Solvers.Solver{T} # Hypatia solver object
# data for transforming certificates
obj_sense::MOI.OptimizationSense
zeros_idxs::Vector{UnitRange{Int}}
moi_cones::Vector{MOI.AbstractVectorSet}
moi_cone_idxs::Vector{UnitRange{Int}}
function Optimizer{T}(; options...) where {T <: Real}
opt = new{T}()
opt.solver = Solvers.Solver{T}(; options...)
return opt
end
end
Optimizer(; options...) = Optimizer{Float64}(; options...) # default to Float64
MOI.is_empty(opt::Optimizer) = (opt.solver.status == Solvers.NotLoaded)
MOI.empty!(opt::Optimizer) = (opt.solver.status = Solvers.NotLoaded)
MOI.get(::Optimizer, ::MOI.SolverName) = "Hypatia"
MOI.get(opt::Optimizer, ::MOI.RawSolver) = opt.solver
MOI.supports(::Optimizer, ::MOI.Silent) = true
function MOI.set(opt::Optimizer, ::MOI.Silent, value::Bool)
opt.solver.verbose = !value
return
end
MOI.get(opt::Optimizer, ::MOI.Silent) = !opt.solver.verbose
MOI.supports(::Optimizer, ::MOI.TimeLimitSec) = true
function MOI.set(opt::Optimizer, ::MOI.TimeLimitSec, value::Union{Real, Nothing})
opt.solver.time_limit = something(value, Inf)
return
end
function MOI.get(opt::Optimizer, ::MOI.TimeLimitSec)
if isfinite(opt.solver.time_limit)
return opt.solver.time_limit
end
return
end
function MOI.get(opt::Optimizer, ::MOI.SolveTimeSec)
if opt.solver.status == Solvers.NotLoaded
error("solve has not been called")
end
return opt.solver.solve_time
end
MOI.get(opt::Optimizer, ::MOI.RawStatusString) = string(opt.solver.status)
MOI.get(opt::Optimizer, ::MOI.BarrierIterations) = opt.solver.num_iters
function MOI.set(opt::Optimizer, param::MOI.RawOptimizerAttribute, value)
return setproperty!(opt.solver, Symbol(param.name), value)
end
function MOI.get(opt::Optimizer, param::MOI.RawOptimizerAttribute)
return getproperty(opt.solver, Symbol(param.name))
end
function MOI.supports(
::Optimizer{T},
::Union{MOI.ObjectiveSense, MOI.ObjectiveFunction{<:Union{VI, SAF{T}}}},
) where {T <: Real}
return true
end
function MOI.supports_constraint(
::Optimizer{T},
::Type{<:Union{VV, VAF{T}}},
::Type{<:Union{MOI.Zeros, SupportedCone{T}}},
) where {T <: Real}
return true
end
function MOI.copy_to(opt::Optimizer{T}, src::MOI.ModelLike) where {T <: Real}
idx_map = MOI.Utilities.IndexMap()
# variables
n = MOI.get(src, MOI.NumberOfVariables()) # columns of A
j = 0
for vj in MOI.get(src, MOI.ListOfVariableIndices())
j += 1
idx_map[vj] = VI(j)
end
@assert j == n
for attr in MOI.get(src, MOI.ListOfVariableAttributesSet())
if attr == MOI.VariableName() || attr == MOI.VariablePrimalStart()
continue
end
throw(MOI.UnsupportedAttribute(attr))
end
# objective
opt.obj_sense = MOI.MIN_SENSE
model_c = zeros(T, n)
obj_offset = zero(T)
for attr in MOI.get(src, MOI.ListOfModelAttributesSet())
if attr == MOI.Name()
continue
elseif attr == MOI.ObjectiveSense()
opt.obj_sense = MOI.get(src, MOI.ObjectiveSense())
elseif attr isa MOI.ObjectiveFunction
F = MOI.get(src, MOI.ObjectiveFunctionType())
if !(F <: Union{VI, SAF{T}})
error("objective function type $F not supported")
end
obj = convert(SAF{T}, MOI.get(src, MOI.ObjectiveFunction{F}()))
for t in obj.terms
model_c[idx_map[t.variable].value] += t.coefficient
end
obj_offset = obj.constant
else
throw(MOI.UnsupportedAttribute(attr))
end
end
if opt.obj_sense == MOI.MAX_SENSE
model_c .*= -1
obj_offset *= -1
end
# constraints
get_src_cons(F, S) = MOI.get(src, MOI.ListOfConstraintIndices{F, S}())
get_con_fun(con_idx) = MOI.get(src, MOI.ConstraintFunction(), con_idx)
get_con_set(con_idx) = MOI.get(src, MOI.ConstraintSet(), con_idx)
# equality constraints
(IA, JA, VA) = (Int[], Int[], T[])
model_b = T[]
opt.zeros_idxs = zeros_idxs = Vector{UnitRange{Int}}()
for F in (VV, VAF{T}), ci in get_src_cons(F, MOI.Zeros)
fi = get_con_fun(ci)
si = get_con_set(ci)
_con_IJV(IA, JA, VA, model_b, zeros_idxs, fi, si, idx_map)
idx_map[ci] = MOI.ConstraintIndex{F, MOI.Zeros}(length(zeros_idxs))
end
model_A = dropzeros!(sparse(IA, JA, VA, length(model_b), n))
# conic constraints
(IG, JG, VG) = (Int[], Int[], T[])
model_h = T[]
moi_cones = MOI.AbstractVectorSet[]
moi_cone_idxs = Vector{UnitRange{Int}}()
cones = Cones.Cone{T}[]
# build up one nonnegative cone
for F in (VV, VAF{T}), ci in get_src_cons(F, MOI.Nonnegatives)
fi = get_con_fun(ci)
si = get_con_set(ci)
_con_IJV(IG, JG, VG, model_h, moi_cone_idxs, fi, si, idx_map)
push!(moi_cones, si)
idx_map[ci] = MOI.ConstraintIndex{F, MOI.Nonnegatives}(length(moi_cones))
end
if !isempty(moi_cones)
push!(cones, cone_from_moi(T, MOI.Nonnegatives(length(model_h))))
end
# other conic constraints
for (F, S) in MOI.get(src, MOI.ListOfConstraintTypesPresent())
if !MOI.supports_constraint(opt, F, S)
throw(MOI.UnsupportedConstraint{F, S}())
end
for attr in MOI.get(src, MOI.ListOfConstraintAttributesSet{F, S}())
if attr == MOI.ConstraintName() ||
attr == MOI.ConstraintPrimalStart() ||
attr == MOI.ConstraintDualStart()
continue
end
throw(MOI.UnsupportedAttribute(attr))
end
if S == MOI.Zeros || S == MOI.Nonnegatives
continue # already copied these constraints
end
for ci in get_src_cons(F, S)
fi = get_con_fun(ci)
si = get_con_set(ci)
_con_IJV(IG, JG, VG, model_h, moi_cone_idxs, fi, si, idx_map)
push!(cones, cone_from_moi(T, si))
push!(moi_cones, si)
idx_map[ci] = MOI.ConstraintIndex{F, S}(length(moi_cones))
end
end
model_G = dropzeros!(sparse(IG, JG, VG, length(model_h), n))
opt.moi_cone_idxs = moi_cone_idxs
opt.moi_cones = moi_cones
# finalize model and load into solver
model = Models.Model{T}(
model_c,
model_A,
model_b,
model_G,
model_h,
cones;
obj_offset = obj_offset,
)
Solvers.load(opt.solver, model)
return idx_map
end
MOI.optimize!(opt::Optimizer) = Solvers.solve(opt.solver)
function MOI.modify(
opt::Optimizer{T},
::MOI.ObjectiveFunction{SAF{T}},
chg::MOI.ScalarConstantChange{T},
) where {T}
obj_offset = chg.new_constant
if opt.obj_sense == MOI.MAX_SENSE
obj_offset = -obj_offset
end
Solvers.modify_obj_offset(opt.solver, obj_offset)
return
end
function MOI.modify(
opt::Optimizer{T},
::MOI.ObjectiveFunction{SAF{T}},
chg::MOI.ScalarCoefficientChange{T},
) where {T}
new_c = chg.new_coefficient
if opt.obj_sense == MOI.MAX_SENSE
new_c = -new_c
end
Solvers.modify_c(opt.solver, [chg.variable.value], [new_c])
return
end
function MOI.modify(
opt::Optimizer{T},
ci::MOI.ConstraintIndex{VAF{T}, MOI.Zeros},
chg::MOI.VectorConstantChange{T},
) where {T}
idxs = opt.zeros_idxs[ci.value]
Solvers.modify_b(opt.solver, idxs, chg.new_constant)
return
end
function MOI.modify(
opt::Optimizer{T},
ci::MOI.ConstraintIndex{VAF{T}, <:SupportedCone{T}},
chg::MOI.VectorConstantChange{T},
) where {T}
i = ci.value
idxs = opt.moi_cone_idxs[i]
set = opt.moi_cones[i]
new_h = chg.new_constant
if needs_rescale(set)
rescale_affine(set, new_h)
end
if needs_permute(set)
new_h = h[permute_idxs(set)]
end
Solvers.modify_h(opt.solver, idxs, new_h)
return
end
function MOI.get(opt::Optimizer, ::MOI.TerminationStatus)
status = opt.solver.status
if status in (Solvers.NotLoaded, Solvers.Loaded)
return MOI.OPTIMIZE_NOT_CALLED
elseif status == Solvers.Optimal
return MOI.OPTIMAL
elseif status == Solvers.NearOptimal
return MOI.ALMOST_OPTIMAL
elseif status == Solvers.PrimalInfeasible || status == Solvers.PrimalInconsistent
return MOI.INFEASIBLE
elseif status == Solvers.NearPrimalInfeasible
return MOI.ALMOST_INFEASIBLE
elseif status == Solvers.DualInfeasible || status == Solvers.DualInconsistent
return MOI.DUAL_INFEASIBLE
elseif status == Solvers.NearDualInfeasible
return MOI.ALMOST_DUAL_INFEASIBLE
elseif status == Solvers.SlowProgress
return MOI.SLOW_PROGRESS
elseif status == Solvers.IterationLimit
return MOI.ITERATION_LIMIT
elseif status == Solvers.TimeLimit
return MOI.TIME_LIMIT
elseif status == Solvers.NumericalFailure
return MOI.NUMERICAL_ERROR
elseif status in (Solvers.IllPosed, Solvers.NearIllPosed)
return MOI.OTHER_LIMIT
else
@warn("Hypatia status $(opt.solver.status) not handled")
return MOI.OTHER_ERROR
end
end
function MOI.get(opt::Optimizer, attr::MOI.PrimalStatus)
if attr.result_index != 1
return MOI.NO_SOLUTION
end
status = opt.solver.status
if status == Solvers.Optimal
return MOI.FEASIBLE_POINT
elseif status == Solvers.NearOptimal
return MOI.NEARLY_FEASIBLE_POINT
elseif status == Solvers.PrimalInfeasible
return MOI.INFEASIBLE_POINT
elseif status == Solvers.DualInfeasible
return MOI.INFEASIBILITY_CERTIFICATE
elseif status == Solvers.NearDualInfeasible
return MOI.NEARLY_INFEASIBILITY_CERTIFICATE
elseif status in (Solvers.IllPosed, Solvers.NearIllPosed)
return MOI.OTHER_RESULT_STATUS
else
return MOI.UNKNOWN_RESULT_STATUS
end
end
function MOI.get(opt::Optimizer, attr::MOI.DualStatus)
if attr.result_index != 1
return MOI.NO_SOLUTION
end
status = opt.solver.status
if status == Solvers.Optimal
return MOI.FEASIBLE_POINT
elseif status == Solvers.NearOptimal
return MOI.NEARLY_FEASIBLE_POINT
elseif status == Solvers.PrimalInfeasible
return MOI.INFEASIBILITY_CERTIFICATE
elseif status == Solvers.NearPrimalInfeasible
return MOI.NEARLY_INFEASIBILITY_CERTIFICATE
elseif status == Solvers.DualInfeasible
return MOI.INFEASIBLE_POINT
elseif status in (Solvers.IllPosed, Solvers.NearIllPosed)
return MOI.OTHER_RESULT_STATUS
else
return MOI.UNKNOWN_RESULT_STATUS
end
end
_sense_val(sense::MOI.OptimizationSense) = (sense == MOI.MAX_SENSE ? -1 : 1)
function MOI.get(opt::Optimizer, attr::MOI.ObjectiveValue)
MOI.check_result_index_bounds(opt, attr)
return _sense_val(opt.obj_sense) * opt.solver.primal_obj
end
function MOI.get(opt::Optimizer, attr::MOI.DualObjectiveValue)
MOI.check_result_index_bounds(opt, attr)
return _sense_val(opt.obj_sense) * opt.solver.dual_obj
end
MOI.get(opt::Optimizer, ::MOI.ResultCount) = 1
function MOI.get(opt::Optimizer, attr::MOI.VariablePrimal, vi::VI)
MOI.check_result_index_bounds(opt, attr)
return opt.solver.result.x[vi.value]
end
function MOI.get(
opt::Optimizer{T},
attr::MOI.ConstraintDual,
ci::MOI.ConstraintIndex{<:Union{VV, VAF{T}}, MOI.Zeros},
) where {T}
MOI.check_result_index_bounds(opt, attr)
return opt.solver.result.y[opt.zeros_idxs[ci.value]]
end
function MOI.get(
opt::Optimizer{T},
attr::MOI.ConstraintDual,
ci::MOI.ConstraintIndex{<:Union{VV, VAF{T}}, <:SupportedCone{T}},
) where {T}
MOI.check_result_index_bounds(opt, attr)
i = ci.value
z_i = opt.solver.result.z[opt.moi_cone_idxs[i]]
return untransform_affine(opt.moi_cones[i], z_i)
end
function MOI.get(
opt::Optimizer{T},
attr::MOI.ConstraintPrimal,
ci::MOI.ConstraintIndex{<:Union{VV, VAF{T}}, <:SupportedCone{T}},
) where {T}
MOI.check_result_index_bounds(opt, attr)
i = ci.value
s_i = opt.solver.result.s[opt.moi_cone_idxs[i]]
return untransform_affine(opt.moi_cones[i], s_i)
end
function _con_IJV(
IM::Vector{Int},
JM::Vector{Int},
VM::Vector{T},
vect::Vector{T},
idxs_vect::Vector{UnitRange{Int}},
func::VV,
set::MOI.AbstractVectorSet,
idx_map::MOI.IndexMap,
) where {T <: Real}
dim = MOI.output_dimension(func)
start = length(vect)
idxs = start .+ (1:dim)
push!(idxs_vect, idxs)
append!(vect, zero(T) for _ in 1:dim)
if needs_permute(set)
append!(IM, invperm(permute_idxs(set)) .+ start)
else
append!(IM, idxs)
end
append!(JM, idx_map[vi].value for vi in func.variables)
append!(VM, -one(T) for _ in 1:dim)
if needs_rescale(set)
@views rescale_affine(set, VM[(end - dim + 1):end])
end
return
end
function _con_IJV(
IM::Vector{Int},
JM::Vector{Int},
VM::Vector{T},
vect::Vector{T},
idxs_vect::Vector{UnitRange{Int}},
func::VAF{T},
set::MOI.AbstractVectorSet,
idx_map::MOI.IndexMap,
) where {T <: Real}
dim = MOI.output_dimension(func)
start = length(vect)
idxs = start .+ (1:dim)
push!(idxs_vect, idxs)
append!(JM, idx_map[vt.scalar_term.variable].value for vt in func.terms)
append!(VM, -vt.scalar_term.coefficient for vt in func.terms)
append!(vect, func.constants)
if needs_rescale(set)
@views vm = VM[(end - length(func.terms) + 1):end]
rescale_affine(set, func, vm)
@views rescale_affine(set, vect[idxs])
end
if needs_permute(set)
perm_idxs = permute_idxs(set)
@views vect[idxs] = vect[perm_idxs .+ start]
func_idxs = permute_affine(set, func)
func_idxs .+= start
append!(IM, func_idxs)
else
append!(IM, start + vt.output_index for vt in func.terms)
end
return
end