# Strange behavior with deepcopy

**URL:** <https://discourse.julialang.org/t/strange-behavior-with-deepcopy/23342>\
**Category:** Optimization (Mathematical)\
**Tags:** jump\
**Created:** [April 20, 2019, 11:53am UTC](https://discourse.julialang.org/t/strange-behavior-with-deepcopy/23342 "2019-04-20T11:53:15Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Thuener](https://avatars.discourse-cdn.com/v4/letter/t/7ba0ec/32.png) [@Thuener](https://discourse.julialang.org/u/Thuener)\
**Post date:** [April 20, 2019, 11:53am UTC](https://discourse.julialang.org/t/strange-behavior-with-deepcopy/23342/1 "2019-04-20T11:53:16Z")

</div>

I’m getting a strange behavior with deepcopy. Deepcopy is still not supported? There is any way to copy JuMP model?

```julia
using JuMP, Clp

m = Model(solver = ClpSolver())
@variable(m, 0 <= x <= 2 )
@variable(m, 0 <= y <= 30 )
@objective(m, Max, 5x + 3*y )
@constraint(m, 1x + 5y <= 3.0 )
status = solve(m)
status ≠ :Optimal && erro(" status ≠ :Optimal $(status)")

m2 = deepcopy(m)
@constraint(m, 10*x + 15*y <= 3.0 ) # Add constraint to original problem
status = solve(m2)

```

And getting this error:

```julia
Array length must match number of rows in the model
chg_row_lower(::Clp.ClpCInterface.ClpModel, ::Array{Float64,1}) at ClpCInterface.jl:452
setconstrLB!(::Clp.ClpMathProgSolverInterface.ClpMathProgModel, ::Array{Float64,1}) at ClpSolverInterface.jl:131
#build#119(::Bool, ::Bool, ::JuMP.ProblemTraits, ::Function, ::JuMP.Model) at solvers.jl:337
(::JuMP.#kw##build)(::Array{Any,1}, ::JuMP.#build, ::JuMP.Model) at <missing>:0
#solve#116(::Bool, ::Bool, ::Bool, ::Array{Any,1}, ::Function, ::JuMP.Model) at solvers.jl:168
solve(::JuMP.Model) at solvers.jl:150
include_string(::String, ::String) at loading.jl:522
include_string(::String, ::String, ::Int64) at eval.jl:30
include_string(::Module, ::String, ::String, ::Int64, ::Vararg{Int64,N} where N) at eval.jl:34
(::Atom.##102#107{String,Int64,String})() at eval.jl:82
withpath(::Atom.##102#107{String,Int64,String}, ::Void) at utils.jl:30
withpath(::Function, ::String) at eval.jl:38
hideprompt(::Atom.##101#106{String,Int64,String}) at repl.jl:62
macro expansion at eval.jl:80 [inlined]
(::Atom.##100#105{Dict{String,Any}})() at task.jl:80

```

This doesn’t happen with copy(m).

---

<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:** [April 20, 2019, 5:26pm UTC](https://discourse.julialang.org/t/strange-behavior-with-deepcopy/23342/2 "2019-04-20T17:26:02Z")

</div>

You can’t use `deepcopy` on a JuMP model. Mostly because after `solve` it has a `Clp` model in `internamModel` and that leads to all kinds of issues.

Basically what is happening here is that `m2` stores a pointer to the Clp model, so calling `solve(m2)` is equivalent to calling `solve(m)`, but `m2` doesn’t have the additional RHS information from the constraint you added.

---

<div class="post-metadata">

**Author:** ![Thuener](https://avatars.discourse-cdn.com/v4/letter/t/7ba0ec/32.png) [@Thuener](https://discourse.julialang.org/u/Thuener)\
**Post date:** [April 21, 2019, 1:04pm UTC](https://discourse.julialang.org/t/strange-behavior-with-deepcopy/23342/3 "2019-04-21T13:04:33Z")

</div>

Ok. Thanks @odow!  
There is any workaround? If I remove internamModel, for example?

Copy isn’t working for me either in JuMP 0.18.5, there is an issue with matrix constraints:

```julia
using JuMP
using CPLEX

m = Model(solver = CplexSolver())
@variable(m, 0 <= x <= 2 )
@variable(m, 0 <= y <= 30 )

@objective(m, Max, 5x + 3*y )
@constraint(m, 1x + 5y <= 3.0 )
@constraint(m, cons[i = 1:2, j=1:2], i*x + j*y <= 3.0 )
copy(m)

```

```julia
MethodError: no method matching copy(::Array{JuMP.ConstraintRef,2}, ::JuMP.Model)e[0m
Closest candidates are:
  copy(e[91m::JuMP.Variablee[39m, ::JuMP.Model) at /home/tas/.julia/v0.6/JuMP/src/JuMP.jl:502
  copy(e[91m::Voide[39m, ::JuMP.Model) at /home/tas/.julia/v0.6/JuMP/src/JuMP.jl:503
  copy(e[91m::JuMP.GenericAffExpr{Float64,JuMP.Variable}e[39m, ::JuMP.Model) at /home/tas/.julia/v0.6/JuMP/src/affexpr.jl:119
  ...
copy(::JuMP.Model) at JuMP.jl:300
include_string(::String, ::String) at loading.jl:522
include_string(::String, ::String, ::Int64) at eval.jl:30
include_string(::Module, ::String, ::String, ::Int64, ::Vararg{Int64,N} where N) at eval.jl:34
(::Atom.##102#107{String,Int64,String})() at eval.jl:82
withpath(::Atom.##102#107{String,Int64,String}, ::Void) at utils.jl:30
withpath(::Function, ::String) at eval.jl:38
hideprompt(::Atom.##101#106{String,Int64,String}) at repl.jl:62
macro expansion at eval.jl:80 [inlined]
(::Atom.##100#105{Dict{String,Any}})() at task.jl:80

```

---

<div class="post-metadata">

**Author:** ![Thuener](https://avatars.discourse-cdn.com/v4/letter/t/7ba0ec/32.png) [@Thuener](https://discourse.julialang.org/u/Thuener)\
**Post date:** [April 21, 2019, 2:50pm UTC](https://discourse.julialang.org/t/strange-behavior-with-deepcopy/23342/4 "2019-04-21T14:50:25Z")

</div>

In JuMP 0.19.0 this code works.

---

<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:** [April 21, 2019, 3:43pm UTC](https://discourse.julialang.org/t/strange-behavior-with-deepcopy/23342/5 "2019-04-21T15:43:17Z")

</div>

Great that it works in 0.19. Do you need to stay on 0.18.5?

---

<div class="post-metadata">

**Author:** ![Thuener](https://avatars.discourse-cdn.com/v4/letter/t/7ba0ec/32.png) [@Thuener](https://discourse.julialang.org/u/Thuener)\
**Post date:** [April 21, 2019, 6:13pm UTC](https://discourse.julialang.org/t/strange-behavior-with-deepcopy/23342/6 "2019-04-21T18:13:34Z")

</div>

I will port sometime in the future, but not now. The issue with copying constraints is just because of the named constraints. I removed the names and seems that everything is working now.

---

<div class="post-metadata">

**Author:** ![Thuener](https://avatars.discourse-cdn.com/v4/letter/t/7ba0ec/32.png) [@Thuener](https://discourse.julialang.org/u/Thuener)\
**Post date:** [May 8, 2019, 10:15am UTC](https://discourse.julialang.org/t/strange-behavior-with-deepcopy/23342/7 "2019-05-08T10:15:45Z")

</div>

Just to finish this topic.  
There are many people that use deepcopy with JuMP models and don’t know about this issue. Just two suggestions, I think this should be clear in JuMP documentation and using deepcopy in JuMP model should return an error.
