Skip to content

Commit 38f0ebd

Browse files
committed
Check constraint and objective is DCP when adding
1 parent 55931d7 commit 38f0ebd

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

src/MOI_wrapper.jl

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ struct Optimizer{T,M} <: MOI.AbstractOptimizer
88
moi_to_convex::OrderedCollections.OrderedDict{MOI.VariableIndex,Any}
99
convex_to_moi::Dict{UInt64,Vector{MOI.VariableIndex}}
1010
constraint_map::Vector{MOI.ConstraintIndex}
11+
1112
function Optimizer(context::Context{T,M}) where {T,M}
1213
return new{T,M}(
1314
context,
@@ -211,6 +212,11 @@ function MOI.add_constraint(
211212
set::MOI.AbstractVectorSet,
212213
) where {T}
213214
constraint = Constraint(_expr(model, func), set)
215+
if vexity(constraint) == Convex.NotDcp()
216+
msg =
217+
"\n\n[Convex.jl] The constraint is not convex according to the axioms of Disciplined Convex Programming.\n\n"
218+
throw(MOI.AddConstraintNotAllowed{typeof(func),typeof(set)}(msg))
219+
end
214220
add_constraint!(model.context, constraint)
215221
push!(model.constraint_map, model.context.constr_to_moi_inds[constraint])
216222
return MOI.ConstraintIndex{typeof(func),typeof(set)}(
@@ -231,10 +237,20 @@ end
231237

232238
function MOI.set(
233239
model::Optimizer{T},
234-
::MOI.ObjectiveFunction{MOI.ScalarNonlinearFunction},
240+
attr::MOI.ObjectiveFunction{MOI.ScalarNonlinearFunction},
235241
func::MOI.ScalarNonlinearFunction,
236242
) where {T}
237-
cfp = conic_form!(model.context, _expr(model, func))
243+
obj_fn = _expr(model, func)
244+
vex = vexity(obj_fn)
245+
if MOI.get(model, MOI.ObjectiveSense()) == MOI.MAX_SENSE
246+
vex = -vex
247+
end
248+
if vex in (Convex.NotDcp(), Convex.ConcaveVexity())
249+
msg =
250+
"\n\n[Convex.jl] The objective is not convex according to the axioms of Disciplined Convex Programming.\n\n"
251+
throw(MOI.SetAttributeNotAllowed(attr, msg))
252+
end
253+
cfp = conic_form!(model.context, obj_fn)
238254
obj = _to_scalar_moi(T, cfp)
239255
MOI.set(model, MOI.ObjectiveFunction{typeof(obj)}(), obj)
240256
return

0 commit comments

Comments
 (0)