Skip to content

Commit

Permalink
Add test
Browse files Browse the repository at this point in the history
  • Loading branch information
blegat committed Nov 6, 2018
1 parent 1d2b753 commit 3fe8bfd
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 23 deletions.
41 changes: 27 additions & 14 deletions src/constraints.jl
Original file line number Diff line number Diff line change
Expand Up @@ -148,31 +148,43 @@ end
abstract type AbstractConstraint end

"""
function_object(constraint::AbstractConstraint)
jump_function(constraint::JuMP.AbstractConstraint)
Return the function of the constraint `constraint` in the function-in-set form.
Return the function of the constraint `constraint` in the function-in-set form
as a `JuMP.AbstractJuMPScalar` or `Vector{JuMP.AbstractJuMPScalar}`.
"""
function function_object end
function jump_function end

"""
set_object(constraint::AbstractConstraint)
moi_function(constraint::JuMP.AbstractConstraint)
Return the set of the constraint `constraint` in the function-in-set form.
Return the function of the constraint `constraint` in the function-in-set form
as a `MathOptInterface.AbstractFunction`.
"""
function set_object end

function moi_function_and_set(c::ScalarConstraint)
return (moi_function(function_object(constraint)), set_object(constraint))
function moi_function(constraint::JuMP.AbstractConstraint)
return moi_function(jump_function(constraint))
end

"""
moi_set(constraint::AbstractConstraint)
Return the set of the constraint `constraint` in the function-in-set form as a
`MathOptInterface.AbstractSet`.
moi_set(s::AbstractVectorSet, dim::Int)
Returns the MOI set of dimension `dim` corresponding to the JuMP set `s`.
"""
function moi_set end

struct ScalarConstraint{F <: AbstractJuMPScalar,
S <: MOI.AbstractScalarSet} <: AbstractConstraint
func::F
set::S
end

function_object(constraint::ScalarConstraint) = c.func
set_object(constraint::ScalarConstraint) = c.set
jump_function(constraint::ScalarConstraint) = constraint.func
moi_set(constraint::ScalarConstraint) = constraint.set
shape(::ScalarConstraint) = ScalarShape()
function constraint_object(ref::ConstraintRef{Model, MOICON{FuncType, SetType}}) where
{FuncType <: MOI.AbstractScalarFunction, SetType <: MOI.AbstractScalarSet}
Expand All @@ -194,8 +206,8 @@ function VectorConstraint(func::Vector{<:AbstractJuMPScalar},
VectorConstraint(func, set, VectorShape())
end

function_object(constraint::VectorConstraint) = c.func
set_object(constraint::VectorConstraint) = c.set
jump_function(constraint::VectorConstraint) = constraint.func
moi_set(constraint::VectorConstraint) = constraint.set
shape(c::VectorConstraint) = c.shape
function constraint_object(ref::ConstraintRef{Model, MOICON{FuncType, SetType}}) where
{FuncType <: MOI.AbstractVectorFunction, SetType <: MOI.AbstractVectorSet}
Expand All @@ -211,7 +223,8 @@ end
Add a constraint `c` to `Model m` and sets its name.
"""
function add_constraint(m::Model, c::AbstractConstraint, name::String="")
f, s = moi_function_and_set(c)
f = moi_function(c)
s = moi_set(c)
if !MOI.supports_constraint(m.moi_backend, typeof(f), typeof(s))
if m.moi_backend isa MOI.Bridges.LazyBridgeOptimizer
bridge_message = " and there are no bridges that can reformulate it into supported constraints."
Expand Down
4 changes: 2 additions & 2 deletions src/print.jl
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ Return a `String` representing the function of the constraint `constraint`
using print mode `print_mode`.
"""
function function_string(print_mode::PrintMode, constraint::AbstractConstraint)
return function_string(print_mode, function_object(constraint))
return function_string(print_mode, jump_function(constraint))
end

function in_set_string(print_mode, set::MOI.LessThan)
Expand Down Expand Up @@ -366,7 +366,7 @@ Return a `String` representing the membership to the set of the constraint
`constraint` using print mode `print_mode`.
"""
function in_set_string(print_mode, constraint::AbstractConstraint)
return in_set_string(print_mode, set_object(constraint))
return in_set_string(print_mode, moi_set(constraint))
end

# constraint_object is a JuMP constraint object like AffExprConstraint.
Expand Down
7 changes: 0 additions & 7 deletions src/sets.jl
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
abstract type AbstractVectorSet end

"""
moi_set(s::AbstractVectorSet, dim::Int)
Returns the MOI set of dimension `dim` corresponding to the JuMP set `s`.
"""
function moi_set end

# Used in `@constraint model f in s`
function build_constraint(_error::Function, f::AbstractVector,
s::AbstractVectorSet)
Expand Down
49 changes: 49 additions & 0 deletions test/print.jl
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,43 @@ Base.:(*)(α::Float64, u::UnitNumber) = UnitNumber(α * u.α)
Base.abs(u::UnitNumber) = UnitNumber(abs(u.α))
Base.isless(u::UnitNumber, v::UnitNumber) = isless(u.α, v.α)

# Used to test extensibility of JuMP printing for `JuMP.AbstractConstraint`
struct CustomConstraint{S <: JuMP.AbstractShape} <: JuMP.AbstractConstraint
function_str::String
in_set_str::String
shape::S
end
function JuMP.function_string(print_mode::JuMP.PrintMode,
constraint::CustomConstraint)
return constraint.function_str
end
function JuMP.in_set_string(print_mode::JuMP.PrintMode,
constraint::CustomConstraint)
return constraint.in_set_str
end
struct CustomIndex
value::Int
end
function JuMP.add_constraint(model::JuMP.Model, constraint::CustomConstraint,
name::String)
if !haskey(model.ext, :custom)
model.ext[:custom_constraints] = CustomConstraint[]
model.ext[:custom_names] = String[]
end
constraints = model.ext[:custom_constraints]
push!(constraints, constraint)
push!(model.ext[:custom_names], name)
return JuMP.ConstraintRef(model, CustomIndex(length(constraints)),
constraint.shape)
end
function JuMP.constraint_object(cref::JuMP.ConstraintRef{JuMP.Model,
CustomIndex})
return cref.model.ext[:custom_constraints][cref.index.value]
end
function JuMP.name(cref::JuMP.ConstraintRef{JuMP.Model, CustomIndex})
return cref.model.ext[:custom_names][cref.index.value]
end

@testset "Printing" begin

@testset "expressions" begin
Expand Down Expand Up @@ -151,6 +188,18 @@ Base.isless(u::UnitNumber, v::UnitNumber) = isless(u.α, v.α)
io_test(REPLMode, constr, "(subexpression[1] - parameter[1]) - 0.0 $le 0")
io_test(IJuliaMode, constr, "(subexpression_{1} - parameter_{1}) - 0.0 \\leq 0")
end

@testset "Custom constraint" begin
model = Model()
function test_constraint(function_str, in_set_str, name)
constraint = CustomConstraint(function_str, in_set_str,
JuMP.ScalarShape())
cref = JuMP.add_constraint(model, constraint, name)
@show string(cref)
end
test_constraint("fun", "set", "name")
test_constraint("a", "b", "c")
end
end

function printing_test(ModelType::Type{<:JuMP.AbstractModel})
Expand Down

0 comments on commit 3fe8bfd

Please sign in to comment.