Skip to content

Commit

Permalink
Merge pull request #1386 from JuliaOpt/bl/suppconerror
Browse files Browse the repository at this point in the history
Add error message for unsupported constraint
  • Loading branch information
blegat authored Jul 31, 2018
2 parents c27c7fc + 46308f3 commit f4130f9
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
11 changes: 10 additions & 1 deletion src/JuMP.jl
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,16 @@ MOI.isvalid(m::Model, cr::ConstraintRef{Model}) = cr.m === m && MOI.isvalid(m.mo
Add a constraint `c` to `Model m` and sets its name.
"""
function addconstraint(m::Model, c::AbstractConstraint, name::String="")
cindex = MOI.addconstraint!(m.moibackend, moi_function_and_set(c)...)
f, s = moi_function_and_set(c)
if !MOI.supportsconstraint(m.moibackend, typeof(f), typeof(s))
if m.moibackend isa MOI.Bridges.LazyBridgeOptimizer
bridge_message = " and there are no bridges that can reformulate it into supported constraints."
else
bridge_message = ", try using `bridge_constraints=true` in the `JuMP.Model` constructor if you believe the constraint can be reformulated to constraints supported by the solver."
end
error("Constraints of type $(typeof(f))-in-$(typeof(s)) are not supported by the solver" * bridge_message)
end
cindex = MOI.addconstraint!(m.moibackend, f, s)
cref = ConstraintRef(m, cindex)
if !isempty(name)
setname(cref, name)
Expand Down
5 changes: 2 additions & 3 deletions test/model.jl
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,12 @@ end
@test model.moibackend isa MOIU.CachingOptimizer
@test model.moibackend === JuMP.caching_optimizer(model)
@variable model x
@constraint model 0 <= x + 1 <= 1
@test_throws ErrorException JuMP.optimize(model)
@test_throws ErrorException @constraint model 0 <= x + 1 <= 1
end
@testset "No bridge automatically added in Direct mode" begin
optimizer = MOIU.MockOptimizer(LPModel{Float64}());
model = Model(backend=optimizer, mode=JuMP.Direct)
@variable model x
@test_throws MethodError @constraint model 0 <= x + 1 <= 1
@test_throws ErrorException @constraint model 0 <= x + 1 <= 1
end
end

0 comments on commit f4130f9

Please sign in to comment.