Hello,
I’m new to Julia and working with Julia/JuMP for optimization purposes. I want to use if-else-statements in the constraints I’m defining to write them more compact. When trying so I ran in some strange behaviour.
Im using Julia v1.5.1
and JuMP v0.21.3.
1.) Just Julia
: Writing the following into a .jl and running with include()
if 1 == 2
1
end
+ 5
returns the correct value 5
.
But writing the following into a .jl and running with include() gives an error.
if 1 == 2 1 end + 5
The Error is: ERROR: LoadError: MethodError: no method matching +(::Nothing, ::Int64)
2.) Using Julia
and JuMP
: When defining the following function Test()
the resulting constraint con
in the .lp file is not like you would expect it
using JuMP
m = Model()
function Test(m::Model,set_constr::Int64)
@variable(m, x[1:5]>=0)
@constraint(m,con,
if set_constr == 1
x[1]
else
if set_constr == 2
0.1*x[2]
end
+
x[4]
+
if set_constr == 3
x[5]
else 0 end
end
<= 0)
JuMP.write_to_file(m, "model.lp")
empty!(m)
end
For Test(m,1)
we get: con: 1 x_1_ <= 0
like we want to have.
For Test(m,2)
we get: con: <= 0
while we would like to get con: 0.1 x_2_ + x_4_ <= 0
For Test(m,3)
we get con: 1 x_5_ <= 0
while we would like to get con: x_4_ + x_5_ <= 0
Is my understanding of the if-else-statement false here or is this a bug?
Thanks for your help and kind regards
Konstantin