# ERROR VariableNotOwned{VariableRef} when using add\_constraint

**URL:** <https://discourse.julialang.org/t/error-variablenotowned-variableref-when-using-add-constraint/104043>\
**Category:** Optimization (Mathematical)\
**Tags:** jump, gurobi\
**Created:** [September 20, 2023, 12:42am UTC](https://discourse.julialang.org/t/error-variablenotowned-variableref-when-using-add-constraint/104043 "2023-09-20T00:42:46Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Luiz\_Monteiro](https://avatars.discourse-cdn.com/v4/letter/l/b77776/32.png) [@Luiz\_Monteiro](https://discourse.julialang.org/u/Luiz_Monteiro)\
**Post date:** [September 20, 2023, 12:42am UTC](https://discourse.julialang.org/t/error-variablenotowned-variableref-when-using-add-constraint/104043/1 "2023-09-20T00:42:46Z")

</div>

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.  
SDS

---

<div class="post-metadata">

**Author:** ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)\
**Post date:** [September 20, 2023, 1:31am UTC](https://discourse.julialang.org/t/error-variablenotowned-variableref-when-using-add-constraint/104043/2 "2023-09-20T01:31:15Z")

</div>

Can you provide a reproducible example?

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

```julia
julia> using JuMP

julia> model = Model();

julia> @variable(model, x);

julia> model2 = Model();

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

```

We should make the error more explanatory: [Make VariableNotOwned error more explanatory · Issue #3517 · jump-dev/JuMP.jl · GitHub](https://github.com/jump-dev/JuMP.jl/issues/3517)

---

<div class="post-metadata">

**Author:** ![Luiz\_Monteiro](https://avatars.discourse-cdn.com/v4/letter/l/b77776/32.png) [@Luiz\_Monteiro](https://discourse.julialang.org/u/Luiz_Monteiro)\
**Post date:** [September 21, 2023, 9:15pm UTC](https://discourse.julialang.org/t/error-variablenotowned-variableref-when-using-add-constraint/104043/3 "2023-09-21T21:15:19Z")

</div>

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  
end

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  
end

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
    optimize!(model1)
    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
    end
    
    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]))
        end
    end

    allcuts = identify_cuts(Vx_val, Instance)
    if length(allcuts) == 0
        if relaxation
            unset_silent(model1)
            set_integer.(model1[:x])
            relaxation = false
        else
            println("z = ", objective_value(model1))
            println("Iterations = ", its)
            return sol_x
        end
    end

    for cons in allcuts
        add_constraint(model1, cons)
    end
end

```

end

---

<div class="post-metadata">

**Author:** ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)\
**Post date:** [September 21, 2023, 9:26pm UTC](https://discourse.julialang.org/t/error-variablenotowned-variableref-when-using-add-constraint/104043/4 "2023-09-21T21:26:33Z")

</div>

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)`?
