ERROR VariableNotOwned{VariableRef} when using add_constraint

Hi, I am tryng to solve a MIP model (VRP) by solving a relaxed version of the model and repeatedly identifying violated contraints and adding those contraints to the model using add_constraint(model1, cons).
But I get this ERROR message example: “VariableNotOwned{VariableRef}(x[2,1])”
The variable x[2,1] is in the model and I cant find how to solve it.
Thanks in advance for any information.

1 Like

Can you provide a reproducible example?

You get this error if you mix variables between two separate models.

julia> using JuMP

julia> model = Model();

julia> @variable(model, x);

julia> model2 = Model();

julia> @constraint(model2, x >= 1)
ERROR: VariableNotOwned{VariableRef}(x)

We should make the error more explanatory: Make VariableNotOwned error more explanatory · Issue #3517 · jump-dev/JuMP.jl · GitHub

Hi odow, thanks for the reply. Its not the case of two separate models. I use only one model wich I call model1.
The model is bit long to write here, so I’ll try to reproduce the project structure, hoping it can be usefull.

function identify_cuts(Vx_val, Instance)
return allcuts

function create_model(Instance)
model1 = Model(Gurobi.Optimizer)
Vtotal = Instance[8] #Example Vtotal = collect(1:20)
@variable(model1, 0 <= x[i in Vtotal, j in Vtotal] <= 1)
return model1

function solve_VRP(Instance, model1, runtime)
runtime_limit = runtime
set_time_limit_sec(model1, runtime_limit)
relaxation = true
model1 = create_model(Instance)

its = 0
while true && its <= 100
    its += 1
    println("[$its] z = ", objective_value(model1))

    x_val = value.(model1[:x])
    #x = model1[:x]
    if !relaxation
        println("z = ", objective_value(model1))
        println("Iterations = ", its)
        return x_val
    Vtotal = Instance[8] 
    Vx_val = []
    for i in collect(1:length(Vtotal)), j in collect(1:length(Vtotal))
        if x_val[i,j] > 0.001
            push!(Vx_val, (i, j, x_val[i,j]))

    allcuts = identify_cuts(Vx_val, Instance)
    if length(allcuts) == 0
        if relaxation
            relaxation = false
            println("z = ", objective_value(model1))
            println("Iterations = ", its)
            return sol_x

    for cons in allcuts
        add_constraint(model1, cons)


Can you provide more detail? It’s hard to say what’s wrong based on this. It looks okay.

I use only one model which I call model1

Then you must have other models or variables in the same scope.

What is identify_cuts?

Do you have the full stack trace of the error? What line generates the error? What is add_constraint(model1, cons)?