Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add extension points for more generic @constraint syntax #2051

Merged
merged 9 commits into from
May 12, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions src/macros.jl
Original file line number Diff line number Diff line change
Expand Up @@ -181,12 +181,16 @@ function parse_constraint_expr(_error::Function, expr::Expr)
return parse_constraint_head(_error, Val(expr.head), expr.args...)
end
function parse_constraint_head(_error::Function, ::Val{:call}, args...)
return parse_constraint(_error, args...)
return parse_constraint_call(_error, Val(args[1]), args[2:end]...)
end
function parse_constraint(_error::Function, args...)
return parse_constraint_call(_error, Val(args[1]), args[2:end]...)
end

function parse_constraint(_error::Function, sense::Symbol, lhs, rhs)
(sense, vectorized) = _check_vectorized(sense)
vectorized, parse_one_operator_constraint(_error, vectorized, Val(sense), lhs, rhs)...
function parse_constraint_call(_error::Function, sense::Val, F...)
sense_symbol = typeof(sense).parameters[1]
(sense, vectorized) = _check_vectorized(sense_symbol)
vectorized, parse_one_operator_constraint(_error, vectorized, Val(sense), F...)...
end

function parse_ternary_constraint(_error::Function, vectorized::Bool, lb, ::Union{Val{:(<=)}, Val{:(≤)}}, aff, rsign::Union{Val{:(<=)}, Val{:(≤)}}, ub)
Expand Down Expand Up @@ -236,7 +240,7 @@ end
function parse_constraint_head(_error::Function, ::Val, args...)
_unknown_constraint_expr(_error)
end
function parse_constraint(_error::Function, args...)
function parse_constraint_call(_error::Function, args...)
_unknown_constraint_expr(_error)
end

Expand Down
16 changes: 15 additions & 1 deletion test/macros.jl
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,20 @@ function custom_expression_test(ModelType::Type{<:JuMP.AbstractModel})
end
end

function JuMP.parse_constraint_call(_error::Function, ::Val{:f}, x)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not JuMP.parse_constraint_head(_error::Function, ::Val{:call}, ::Val{:f}, x) ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I remove my changes in src/macros.jl and adapt the test as you suggest, this is what I get:

ERROR: LoadError: LoadError: LoadError: In `@constraint(model, con_ref, f(x))`: Constraints must be in one of the following forms:
       expr1 <= expr2
       expr1 >= expr2
       expr1 == expr2
       lb <= expr <= ub
Stacktrace:
 [1] error(::String, ::String) at .\error.jl:42
 [2] _macro_error(::Symbol, ::Array{Any,1}, ::String) at C:\Users\Thibaut\.julia\dev\JuMP\src\macros.jl:891
 [3] (::JuMP.var"#_error#68"{Symbol})(::String) at C:\Users\Thibaut\.julia\dev\JuMP\src\macros.jl:363
 [4] _unknown_constraint_expr(::JuMP.var"#_error#68"{Symbol}) at C:\Users\Thibaut\.julia\dev\JuMP\src\macros.jl:240
 [5] parse_constraint(::Function, ::Symbol, ::Symbol) at C:\Users\Thibaut\.julia\dev\JuMP\src\macros.jl:249
 [6] parse_constraint_head(::Function, ::Val{:call}, ::Symbol, ::Symbol) at C:\Users\Thibaut\.julia\dev\JuMP\src\macros.jl:184
 [7] parse_constraint_expr at C:\Users\Thibaut\.julia\dev\JuMP\src\macros.jl:181 [inlined]
 [8] _constraint_macro(::Tuple{Symbol,Symbol,Expr}, ::Symbol, ::typeof(parse_constraint_expr)) at C:\Users\Thibaut\.julia\dev\JuMP\src\macros.jl:404
 [9] @constraint(::LineNumberNode, ::Module, ::Vararg{Any,N} where N) at C:\Users\Thibaut\.julia\dev\JuMP\src\macros.jl:487
 [10] include(::String) at .\client.jl:439
 [11] top-level scope at C:\Users\Thibaut\.julia\dev\JuMP\test\runtests.jl:23
 [12] include(::String) at .\client.jl:439
 [13] top-level scope at none:6
in expression starting at C:\Users\Thibaut\.julia\dev\JuMP\test\macros.jl:186
in expression starting at C:\Users\Thibaut\.julia\dev\JuMP\test\macros.jl:182
in expression starting at C:\Users\Thibaut\.julia\dev\JuMP\test\runtests.jl:23

return false, :(), :(build_constraint($_error, $(esc(x)), $(esc(CustomType()))))
end
function custom_function_test(ModelType::Type{<:JuMP.AbstractModel})
@testset "Custom function" begin
model = ModelType()
@variable(model, x)
@constraint(model, con_ref, f(x))
con = JuMP.constraint_object(con_ref)
@test jump_function(con) == x
@test moi_set(con) isa CustomSet
end
end

function macros_test(ModelType::Type{<:JuMP.AbstractModel}, VariableRefType::Type{<:JuMP.AbstractVariableRef})
@testset "build_constraint on variable" begin
m = ModelType()
Expand Down Expand Up @@ -357,8 +371,8 @@ function macros_test(ModelType::Type{<:JuMP.AbstractModel}, VariableRefType::Typ
end

build_constraint_keyword_test(ModelType)

custom_expression_test(ModelType)
custom_function_test(ModelType)
end

@testset "Macros for JuMP.Model" begin
Expand Down