Skip to content

Commit d19955e

Browse files
blegatodow
andauthored
Support vector sets in MOI wrapper (#669)
* Support vector sets in MOI wrapper * Add comment * Fix * Update MOI_wrapper.jl * Update MOI_wrapper.jl --------- Co-authored-by: Oscar Dowson <[email protected]>
1 parent 532dacf commit d19955e

File tree

2 files changed

+27
-28
lines changed

2 files changed

+27
-28
lines changed

src/Constraint.jl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,17 @@
33
# Use of this source code is governed by a BSD-style license that can be found
44
# in the LICENSE file or at https://opensource.org/license/bsd-2-clause
55

6-
mutable struct Constraint{S<:MOI.AbstractSet}
6+
mutable struct Constraint{S<:MOI.AbstractVectorSet}
77
child::AbstractExpr
88
set::S
99
dual::Union{Value,Nothing}
1010

11-
function Constraint(child::AbstractExpr, set::MOI.AbstractSet)
11+
function Constraint(child::AbstractExpr, set::MOI.AbstractVectorSet)
1212
return new{typeof(set)}(child, set, nothing)
1313
end
1414
end
1515

16-
function Constraint{S}(child::AbstractExpr) where {S<:MOI.AbstractSet}
16+
function Constraint{S}(child::AbstractExpr) where {S<:MOI.AbstractVectorSet}
1717
return Constraint(child, set_with_size(S, size(child)))
1818
end
1919

@@ -34,7 +34,7 @@ end
3434

3535
head(io::IO, c::Constraint) = head(io, c.set)
3636

37-
function head(io::IO, set::MOI.AbstractSet)
37+
function head(io::IO, set::MOI.AbstractVectorSet)
3838
return print(io, replace("$(typeof(set))", "MathOptInterface" => "MOI"))
3939
end
4040

@@ -80,7 +80,7 @@ function vexity(
8080
return ConvexVexity()
8181
end
8282

83-
function vexity(::Any, set::MOI.AbstractSet)
83+
function vexity(::Any, set::MOI.AbstractVectorSet)
8484
return error(
8585
"`Convex.vexity(vex, ::$(typeof(set)))`: is not yet implemented. Please open an issue at https://github.com/jump-dev/Convex.jl",
8686
)

src/MOI_wrapper.jl

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -116,10 +116,18 @@ function MOI.add_constraint(
116116
end
117117

118118
function MOI.supports_constraint(
119-
::Optimizer{T},
120-
::Type{MOI.ScalarNonlinearFunction},
121-
::Type{<:Union{MOI.EqualTo{T},MOI.GreaterThan{T},MOI.LessThan{T}}},
122-
) where {T}
119+
::Optimizer,
120+
::Type{MOI.VectorNonlinearFunction},
121+
::Type{<:MOI.AbstractVectorSet},
122+
)
123+
# This can cause false positives because:
124+
# 1) some sets might not be supported by Convex.jl (e.g., `vexity` might
125+
# be missing)
126+
# 2) whether we support the constraint can depend on the vexity of the
127+
# function, which we currently don't know.
128+
# Rather than attempt an enumeration of supported sets here, let's just
129+
# pass things on and hope that there is a nice error message elsewhere in
130+
# the callchain.
123131
return true
124132
end
125133

@@ -189,28 +197,20 @@ function _expr(model::Optimizer, f::MOI.ScalarNonlinearFunction)
189197
return throw(MOI.UnsupportedNonlinearOperator(f.head))
190198
end
191199

192-
function MOI.get(::Optimizer, ::MOI.ListOfSupportedNonlinearOperators)
193-
return Symbol[:+, :-, :*, :/, :^, :min, :max, :abs, :sqrt, :exp, :log]
194-
end
195-
196-
function _constraint(expr::AbstractExpr, set::MOI.EqualTo)
197-
return expr == MOI.constant(set)
200+
function _expr(model::Optimizer, f::MOI.VectorNonlinearFunction)
201+
return vcat(_expr.(model, f.rows)...)
198202
end
199203

200-
function _constraint(expr::AbstractExpr, set::MOI.LessThan)
201-
return expr <= MOI.constant(set)
202-
end
203-
204-
function _constraint(expr::AbstractExpr, set::MOI.GreaterThan)
205-
return expr >= MOI.constant(set)
204+
function MOI.get(::Optimizer, ::MOI.ListOfSupportedNonlinearOperators)
205+
return Symbol[:+, :-, :*, :/, :^, :min, :max, :abs, :sqrt, :exp, :log]
206206
end
207207

208208
function MOI.add_constraint(
209209
model::Optimizer{T},
210-
func::MOI.ScalarNonlinearFunction,
211-
set::MOI.AbstractScalarSet,
210+
func::MOI.VectorNonlinearFunction,
211+
set::MOI.AbstractVectorSet,
212212
) where {T}
213-
constraint = _constraint(_expr(model, func), set)
213+
constraint = Constraint(_expr(model, func), set)
214214
add_constraint!(model.context, constraint)
215215
push!(model.constraint_map, model.context.constr_to_moi_inds[constraint])
216216
return MOI.ConstraintIndex{typeof(func),typeof(set)}(
@@ -317,10 +317,9 @@ end
317317
function MOI.get(
318318
model::Optimizer,
319319
attr::Union{MOI.ConstraintDual,MOI.ConstraintPrimal},
320-
ci::MOI.ConstraintIndex{MOI.ScalarNonlinearFunction,S},
321-
) where {S<:MOI.AbstractScalarSet}
322-
ret = MOI.get(model.context.model, attr, model.constraint_map[ci.value])
323-
return ret[]
320+
ci::MOI.ConstraintIndex{MOI.VectorNonlinearFunction,S},
321+
) where {S<:MOI.AbstractVectorSet}
322+
return MOI.get(model.context.model, attr, model.constraint_map[ci.value])
324323
end
325324

326325
function MOI.get(model::Optimizer, I::Type{<:MOI.Index}, name::String)

0 commit comments

Comments
 (0)