-
-
Notifications
You must be signed in to change notification settings - Fork 399
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
sum comparison parses wrongly without error #2461
Comments
The error is throw in |
Mostly because of this (simplified, lines removed): julia> @macroexpand @constraint(model, sum(x == i && x == i for i = 1:2) <= 1)
ss0 = JuMP.MutableArithmetics.Zero()
for i = 1:2
ss0 = JuMP.MutableArithmetics.operate!(
JuMP.MutableArithmetics.add_mul,
ss0,
x == i && x == i
)
end It seems reasonable to define It doesn't help @Wikunia though, but that is because @blegat, the error you're thinking of is julia> @constraint(model, sum(x == i for i = 1:2) <= 1)
ERROR: LoadError: Unexpected comparison in expression `x == i`.
Stacktrace:
[1] error(::String) at ./error.jl:33
[2] _rewrite(::Bool, ::Bool, ::Expr, ::Symbol, ::Array{Any,1}, ::Array{Any,1}, ::Symbol) at /Users/oscar/.julia/packages/MutableArithmetics/bPWR4/src/rewrite.jl:629
[3] #8 at /Users/oscar/.julia/packages/MutableArithmetics/bPWR4/src/rewrite.jl:227 [inlined]
[4] rewrite_generator(::Expr, ::MutableArithmetics.var"#8#10"{Symbol,Bool,Bool,Array{Any,1},Array{Any,1}}) at /Users/oscar/.julia/packages/MutableArithmetics/bPWR4/src/rewrite.jl:143
[5] rewrite_generator(::Expr, ::MutableArithmetics.var"#8#10"{Symbol,Bool,Bool,Array{Any,1},Array{Any,1}}) at /Users/oscar/.julia/packages/MutableArithmetics/bPWR4/src/rewrite.jl:168
[6] (::MutableArithmetics.var"#7#9"{Bool,Bool,Expr,Array{Any,1},Array{Any,1},Symbol})(::Symbol) at /Users/oscar/.julia/packages/MutableArithmetics/bPWR4/src/rewrite.jl:225
[7] _start_summing(::Nothing, ::MutableArithmetics.var"#7#9"{Bool,Bool,Expr,Array{Any,1},Array{Any,1},Symbol}) at /Users/oscar/.julia/packages/MutableArithmetics/bPWR4/src/rewrite.jl:332
[8] _parse_generator_sum at /Users/oscar/.julia/packages/MutableArithmetics/bPWR4/src/rewrite.jl:222 [inlined]
[9] _parse_generator(::Bool, ::Bool, ::Expr, ::Nothing, ::Array{Any,1}, ::Array{Any,1}, ::Symbol) at /Users/oscar/.julia/packages/MutableArithmetics/bPWR4/src/rewrite.jl:196
[10] _rewrite(::Bool, ::Bool, ::Expr, ::Nothing, ::Array{Any,1}, ::Array{Any,1}, ::Symbol) at /Users/oscar/.julia/packages/MutableArithmetics/bPWR4/src/rewrite.jl:611
[11] _rewrite(::Bool, ::Bool, ::Expr, ::Nothing, ::Array{Any,1}, ::Array{Any,1}) at /Users/oscar/.julia/packages/MutableArithmetics/bPWR4/src/rewrite.jl:403
[12] _rewrite(::Bool, ::Bool, ::Expr, ::Nothing, ::Array{Any,1}, ::Array{Any,1}, ::Symbol) at /Users/oscar/.julia/packages/MutableArithmetics/bPWR4/src/rewrite.jl:429
[13] _rewrite at /Users/oscar/.julia/packages/MutableArithmetics/bPWR4/src/rewrite.jl:403 [inlined]
[14] rewrite_and_return(::Expr) at /Users/oscar/.julia/packages/MutableArithmetics/bPWR4/src/rewrite.jl:272
[15] rewrite at /Users/oscar/.julia/packages/MutableArithmetics/bPWR4/src/rewrite.jl:261 [inlined]
[16] parse_one_operator_constraint(::Function, ::Bool, ::Val{:<=}, ::Expr, ::Int64) at /Users/oscar/.julia/dev/JuMP/src/macros.jl:198
[17] parse_constraint(::Function, ::Symbol, ::Expr, ::Int64) at /Users/oscar/.julia/dev/JuMP/src/macros.jl:211
[18] parse_constraint_head(::Function, ::Val{:call}, ::Symbol, ::Expr, ::Int64) at /Users/oscar/.julia/dev/JuMP/src/macros.jl:206
[19] parse_constraint_expr at /Users/oscar/.julia/dev/JuMP/src/macros.jl:203 [inlined]
[20] _constraint_macro(::Tuple{Symbol,Expr}, ::Symbol, ::typeof(parse_constraint_expr), ::LineNumberNode) at /Users/oscar/.julia/dev/JuMP/src/macros.jl:438
[21] @constraint(::LineNumberNode, ::Module, ::Vararg{Any,N} where N) at /Users/oscar/.julia/dev/JuMP/src/macros.jl:521
in expression starting at REPL[18]:1 |
#2729 adds a better error message for inequality comparisons. But I don't think we can throw a better error for |
Closing because I don't think we can easily support this. We now throw better error messages for The correct way to implement this is to overload @constraint(
model,
BooleanConstraint(sum(x == i && x == i for i = 1:2) <= 1),
) |
I'm currently thinking about allowing comparisons inside a sum for my ConstraintSolver.jl package. (See: Wikunia/ConstraintSolver.jl#254)
Unfortunately I encountered the following problem: When combining comparisons inside the sum constraint with
&&
i.eit currently gets parsed as
0 <= 1
instead of throwing an error as it does for things like:It's quite unlikely that someone encounters this but would be nice if it throws an error.
The text was updated successfully, but these errors were encountered: