I am puzzled by the following:
using JuMP
m = Model()
@variable(m,x)
code = quote
try
1
catch e
@NLconstraint($m, 0 .<= exp($x))
end
end
eval(code)
ERROR: At REPL[240]:5: `@NLconstraint(Feasibility
Subject to
, [1, 2] .<= exp(x))`: expected comparison operator (<=, >=, or ==).
Why is the @NLconstraint
macro being evaluated here if the catch
block shouldn’t be entered into? (btw, I know that the .<=
operator is not allowed in this macro).
Context: I ran into this while trying to setup a try
catch
block that will call the JuMP.@constraints
macro for a vectorized constraint expression. If that fails, then I want it to call the JuMP.@NLconstraints
macro in the catch
block:
The following works when I don’t use vectorized constraints:
function add_con(m, con_quote)
quote
try
@constraints($m,$con_quote)
catch
@NLconstraints($m,$con_quote)
end
end
end
m1 = Model()
@variable(m1,x[1:2])
con_quote1 = :(
begin
con[i=1:2], [1,2][i] <= x[i] <= [5,6][i]
end
)
eval(add_con(m1,con_quote1))
print(m1)
# Feasibility
# Subject to
# con[1] : x[1] in [1.0, 5.0]
# con[2] : x[2] in [2.0, 6.0]
When I switch over to a vectorized constraint, for some reason it tries to call the @NLconstraints
macro instead:
m2 = Model()
@variable(m2,x[1:2])
con_quote2 = :(
begin
con, [1,2] .<= x .<= [5,6]
end
)
eval(add_con(m2,con_quote2))
print(m2)
# ERROR: LoadError: `@NLconstraint(Feasibility
# Subject to
# , con, [1, 2] .<= x .<= [5, 6])`: only ranged rows of the form lb <= expr <= ub are supported.
However, the @constraints
macro should work with this type of constraint:
m3 = Model()
@variable(m3,x[1:2])
@constraints(m3,begin
con, [1,2] .<= x .<= [5,6]
end)
print(m3)
# Feasibility
# Subject to
# con : x[1] in [1.0, 5.0]
# con : x[2] in [2.0, 6.0]
Any help/insights would be appreciated.