JuMP returning incorrect value after deleting/adding anonymous variable and constraint

When I use an anonymous variable and constraint, optimize, delete and re-add them, then optimize again, I get two different solutions, the second of which is incorrect. However, the model is still printing correctly. What might be going on here?

(JuMP v0.21.2, Gurobi v0.7.6, Julia v1.4)

using JuMP
using Gurobi

m = Model(Gurobi.Optimizer)
set_optimizer_attribute(m,"OutputFlag",0)

@variable(m, 0<=y<=4)
x = @variable(m, base_name="x",lower_bound=0,upper_bound=4) 
con = @constraint(m, x+y == 3)
@objective(m, Max, x+y)
print(m)
optimize!(m)
println("x1= ",value(x),"; y1= ",value(y),"; objv = ",objective_value(m))

delete(m,con)
delete(m,x)

x = @variable(m, base_name="x",lower_bound=0,upper_bound=4) 
con = @constraint(m, x+y == 3)
@objective(m, Max, x+y)
print(m)
optimize!(m)
println("x2= ",value(x),"; y2= ",value(y),"; objv = ",objective_value(m))

This gives the following output:

Max x + y
Subject to
 x + y = 3.0
 y ≥ 0.0
 x ≥ 0.0
 y ≤ 4.0
 x ≤ 4.0
x1= 0.0; y1= 3.0; objv = 3.0
Max x + y
Subject to
 x + y = 3.0
 y ≥ 0.0
 x ≥ 0.0
 y ≤ 4.0
 x ≤ 4.0
x2= 1.0; y2= 0.0; objv = -0.0
1 Like

It looks like you have indeed found a bug. Let me take a look.

Opened an issue: https://github.com/JuliaOpt/Gurobi.jl/issues/315. Will contact Gurobi support.

2 Likes