Skip to content

Commit ca53242

Browse files
authored
Check constraint and objective is DCP when adding (#714)
1 parent 55931d7 commit ca53242

File tree

2 files changed

+70
-2
lines changed

2 files changed

+70
-2
lines changed

src/MOI_wrapper.jl

Lines changed: 16 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,10 @@ 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 = "\n\n[Convex.jl] The constraint is not convex according to the axioms of Disciplined Convex Programming.\n\n"
217+
throw(MOI.AddConstraintNotAllowed{typeof(func),typeof(set)}(msg))
218+
end
214219
add_constraint!(model.context, constraint)
215220
push!(model.constraint_map, model.context.constr_to_moi_inds[constraint])
216221
return MOI.ConstraintIndex{typeof(func),typeof(set)}(
@@ -231,10 +236,19 @@ end
231236

232237
function MOI.set(
233238
model::Optimizer{T},
234-
::MOI.ObjectiveFunction{MOI.ScalarNonlinearFunction},
239+
attr::MOI.ObjectiveFunction{MOI.ScalarNonlinearFunction},
235240
func::MOI.ScalarNonlinearFunction,
236241
) where {T}
237-
cfp = conic_form!(model.context, _expr(model, func))
242+
obj_fn = _expr(model, func)
243+
vex = vexity(obj_fn)
244+
if MOI.get(model, MOI.ObjectiveSense()) == MOI.MAX_SENSE
245+
vex = -vex
246+
end
247+
if vex in (Convex.NotDcp(), Convex.ConcaveVexity())
248+
msg = "\n\n[Convex.jl] The objective is not convex according to the axioms of Disciplined Convex Programming.\n\n"
249+
throw(MOI.SetAttributeNotAllowed(attr, msg))
250+
end
251+
cfp = conic_form!(model.context, obj_fn)
238252
obj = _to_scalar_moi(T, cfp)
239253
MOI.set(model, MOI.ObjectiveFunction{typeof(obj)}(), obj)
240254
return

test/MOI_wrapper.jl

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,60 @@ function test_scalar_nonlinear_function()
108108
return
109109
end
110110

111+
function test_not_dcp_constraint()
112+
inner = Convex.Optimizer(ECOS.Optimizer)
113+
model = MOI.Utilities.CachingOptimizer(
114+
MOI.Utilities.UniversalFallback(MOI.Utilities.Model{Float64}()),
115+
MOI.Bridges.full_bridge_optimizer(inner, Float64),
116+
)
117+
x = MOI.add_variable(model)
118+
f = MOI.ScalarNonlinearFunction(:^, Any[x, 2])
119+
MOI.add_constraint(model, f, MOI.GreaterThan(1.0))
120+
F, S = MOI.VectorNonlinearFunction, MOI.Nonnegatives
121+
@test_throws(
122+
MOI.AddConstraintNotAllowed{F,S},
123+
MOI.Utilities.attach_optimizer(model),
124+
)
125+
return
126+
end
127+
128+
function test_not_dcp_objective()
129+
inner = Convex.Optimizer(ECOS.Optimizer)
130+
model = MOI.Utilities.CachingOptimizer(
131+
MOI.Utilities.UniversalFallback(MOI.Utilities.Model{Float64}()),
132+
MOI.Bridges.full_bridge_optimizer(inner, Float64),
133+
)
134+
x = MOI.add_variable(model)
135+
f = MOI.ScalarNonlinearFunction(:^, Any[x, 2])
136+
MOI.set(model, MOI.ObjectiveSense(), MOI.MAX_SENSE)
137+
attr = MOI.ObjectiveFunction{typeof(f)}()
138+
MOI.set(model, attr, f)
139+
@test_throws(
140+
MOI.SetAttributeNotAllowed{typeof(attr)},
141+
MOI.Utilities.attach_optimizer(model),
142+
)
143+
return
144+
end
145+
146+
function test_not_dcp_objective_min()
147+
inner = Convex.Optimizer(ECOS.Optimizer)
148+
model = MOI.Utilities.CachingOptimizer(
149+
MOI.Utilities.UniversalFallback(MOI.Utilities.Model{Float64}()),
150+
MOI.Bridges.full_bridge_optimizer(inner, Float64),
151+
)
152+
x = MOI.add_variable(model)
153+
g = MOI.ScalarNonlinearFunction(:^, Any[x, 2])
154+
f = MOI.ScalarNonlinearFunction(:-, Any[g])
155+
MOI.set(model, MOI.ObjectiveSense(), MOI.MIN_SENSE)
156+
attr = MOI.ObjectiveFunction{typeof(f)}()
157+
MOI.set(model, attr, f)
158+
@test_throws(
159+
MOI.SetAttributeNotAllowed{typeof(attr)},
160+
MOI.Utilities.attach_optimizer(model),
161+
)
162+
return
163+
end
164+
111165
end # TestMOIWrapper
112166

113167
TestMOIWrapper.runtests()

0 commit comments

Comments
 (0)