# Model/variable copy error in JuMP 0.18

**URL:** <https://discourse.julialang.org/t/model-variable-copy-error-in-jump-0-18/11449>\
**Category:** Optimization (Mathematical)\
**Tags:** question\
**Created:** [June 5, 2018, 5:27pm UTC](https://discourse.julialang.org/t/model-variable-copy-error-in-jump-0-18/11449 "2018-06-05T17:27:57Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![chkwon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chkwon/32/3632_2.png) [@chkwon](https://discourse.julialang.org/u/chkwon)\
**Post date:** [June 5, 2018, 5:27pm UTC](https://discourse.julialang.org/t/model-variable-copy-error-in-jump-0-18/11449/1 "2018-06-05T17:27:57Z")

</div>

I created a simple model and tried to copy and modify.

```julia
using JuMP, Clp

m = Model(solver=ClpSolver())
set_A = [(1,2), (2,3)]
@variable(m, x[(i,j) in set_A]>=0)
@objective(m, Max, sum(x[(i,j)] for (i,j) in set_A) )
@constraint(m, x[(1,2)]<=10)
@constraint(m, x[(2,3)]<=10)
solve(m)
@show getvalue(x)

m2 = copy(m)
x2 = copy(x, m2)
@constraint(m2, x2[(1,2)]<=1)
@constraint(m2, x2[(2,3)]<=1)
solve(m2)
@show getvalue(x2)

```

The last line creates an error. When tuples were not used in the variable definition, it works fine.

```julia
getvalue(x) = x: 1 dimensions:
[(1, 2)] = 10.0
[(2, 3)] = 10.0
ERROR: LoadError: 
Stacktrace:
 [1] getindex(::ObjectIdDict, ::JuMP.JuMPArray{JuMP.Variable,1,Tuple{Array{Tuple{Int64,Int64},1}}}) at ./associative.jl:363
 [2] _map(::Function, ::JuMP.JuMPArray{JuMP.Variable,1,Tuple{Array{Tuple{Int64,Int64},1}}}) at /Users/chkwon/.julia/v0.6/JuMP/src/JuMPContainer.jl:162
 [3] getvalue(::JuMP.JuMPArray{JuMP.Variable,1,Tuple{Array{Tuple{Int64,Int64},1}}}) at /Users/chkwon/.julia/v0.6/JuMP/src/JuMPContainer.jl:114
 [4] include_from_node1(::String) at ./loading.jl:576
 [5] include(::String) at ./sysimg.jl:14SYSTEM: show(lasterr) caused an error
AssertionError("j isa Array{Variable}")

Stacktrace:
 [1] getindex(::ObjectIdDict, ::JuMP.JuMPArray{JuMP.Variable,1,Tuple{Array{Tuple{Int64,Int64},1}}}) at ./associative.jl:363
 [2] _map(::Function, ::JuMP.JuMPArray{JuMP.Variable,1,Tuple{Array{Tuple{Int64,Int64},1}}}) at /Users/chkwon/.julia/v0.6/JuMP/src/JuMPContainer.jl:162
 [3] getvalue(::JuMP.JuMPArray{JuMP.Variable,1,Tuple{Array{Tuple{Int64,Int64},1}}}) at /Users/chkwon/.julia/v0.6/JuMP/src/JuMPContainer.jl:114
 [4] include_from_node1(::String) at ./loading.jl:576
 [5] include(::String) at ./sysimg.jl:14
 [6] eval(::Module, ::Any) at ./boot.jl:235
 [7] eval_user_input(::Any, ::Base.REPL.REPLBackend) at ./REPL.jl:66
 [8] macro expansion at ./REPL.jl:97 [inlined]
 [9] (::Base.REPL.##1#2{Base.REPL.REPLBackend})() at ./event.jl:73

```

Is this a bug?

---

<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:** [June 5, 2018, 8:58pm UTC](https://discourse.julialang.org/t/model-variable-copy-error-in-jump-0-18/11449/2 "2018-06-05T20:58:27Z")

</div>

The error is from when it tries to print `x2`.

It probably shouldn’t error, but you can go `x2 = m2[:x]` instead of the copy.

It looks like @blegat last touched this stuff:  
[https://github.com/JuliaOpt/JuMP.jl/issues/1019](https://github.com/JuliaOpt/JuMP.jl/issues/1019)  
[https://github.com/JuliaOpt/JuMP.jl/pull/1020](https://github.com/JuliaOpt/JuMP.jl/pull/1020)

---

<div class="post-metadata">

**Author:** ![blegat](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/blegat/32/217090_2.png) [@blegat](https://discourse.julialang.org/u/blegat)\
**Post date:** [June 6, 2018, 6:20am UTC](https://discourse.julialang.org/t/model-variable-copy-error-in-jump-0-18/11449/3 "2018-06-06T06:20:54Z")

</div>

`copy(x, m2)` is not meant to be called manually, it is already called in `copy(m)`.  
[https://github.com/JuliaOpt/JuMP.jl/blob/d3814460359b324afc47ac99a8342927b4082e32/src/JuMP.jl#L299-L304](https://github.com/JuliaOpt/JuMP.jl/blob/d3814460359b324afc47ac99a8342927b4082e32/src/JuMP.jl#L299-L304)  
As you can see, inside `copy(m)`, it also copies, the `varData` entries. If you call `copy(x, m2)` manually, you also need to do `m2.varData[x2] = m.varData[x]`

---

<div class="post-metadata">

**Author:** ![chkwon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chkwon/32/3632_2.png) [@chkwon](https://discourse.julialang.org/u/chkwon)\
**Post date:** [June 6, 2018, 1:55pm UTC](https://discourse.julialang.org/t/model-variable-copy-error-in-jump-0-18/11449/4 "2018-06-06T13:55:24Z")

</div>

Thanks @odow and @blegat.

Either

```julia
x2 = m2[:x]

```

or

```julia
x2 = copy(x, m2);
m2.varData[x2] = m.varData[x]

```

works. I think I should stick to the first one.
