# JuMPv0.21 and deepcopy

**URL:** <https://discourse.julialang.org/t/jumpv0-21-and-deepcopy/36051>\
**Category:** Optimization (Mathematical)\
**Tags:** jump, deepcopy\
**Created:** [March 16, 2020, 6:03pm UTC](https://discourse.julialang.org/t/jumpv0-21-and-deepcopy/36051 "2020-03-16T18:03:44Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![dmetivie](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dmetivie/32/6926_2.png) [@dmetivie](https://discourse.julialang.org/u/dmetivie)\
**Post date:** [March 16, 2020, 6:03pm UTC](https://discourse.julialang.org/t/jumpv0-21-and-deepcopy/36051/1 "2020-03-16T18:03:44Z")

</div>

Hi,  
In JuMP Version 0.21 it says

> Calling `deepcopy(::AbstractModel)` now throws an error.  
> Is there a new way to do `deepcopy()`?  
> As far as I understand `copy()` is not what I want because the copied and original model are modified together (I am not sure to get the usage).

Ideally what I would like to do is define some model with optimize, constraints, objective variable etc…  
Then copy all that in some model `ML0` then

```julia
for m in 1 to M
 ML = deepcopy(ML0)
 @constraint( f(m) )
 optimize!(ML)
end

```

---

<div class="post-metadata">

**Author:** ![ericphanson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ericphanson/32/215186_2.png) [@ericphanson](https://discourse.julialang.org/u/ericphanson)\
**Post date:** [March 16, 2020, 6:08pm UTC](https://discourse.julialang.org/t/jumpv0-21-and-deepcopy/36051/2 "2020-03-16T18:08:04Z")

</div>

I would just turn the code that produces the model into a function,

```julia
function make_model()
    ...
    return model
end

```

and then change the loop to use the function instead, e.g.

```julia
for m in 1 to M
 ML = make_model()
 @constraint(ML, f(m) )
 optimize!(ML)
end

```

By the way, I’ve recategorized the question to the domains/optimization category which is more appropriate for JuMP questions.

---

<div class="post-metadata">

**Author:** ![dmetivie](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dmetivie/32/6926_2.png) [@dmetivie](https://discourse.julialang.org/u/dmetivie)\
**Post date:** [March 16, 2020, 6:28pm UTC](https://discourse.julialang.org/t/jumpv0-21-and-deepcopy/36051/3 "2020-03-16T18:28:23Z")

</div>

The problem with that is that I have to recreate from scratch the model at each iteration.  
Typically in my case, `ML0` that I want to copy, is very long to build (and way faster to copy as I noticed when I was using `deepcopy()` with the previous JuMP versions).

---

<div class="post-metadata">

**Author:** ![ericphanson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ericphanson/32/215186_2.png) [@ericphanson](https://discourse.julialang.org/u/ericphanson)\
**Post date:** [March 16, 2020, 6:34pm UTC](https://discourse.julialang.org/t/jumpv0-21-and-deepcopy/36051/4 "2020-03-16T18:34:13Z")

</div>

Maybe one of the JuMP devs can chime in then, since they know more than me about this, but I’m not sure there is a way to do that. I believe the problem is that `deepcopy`ing is not guaranteed to correctly do what you want it to do, because the model is also held by the solver which may be written in another language, and `deepcopy`ing won’t extend to copying things on the solver level too.

Maybe you can set it up so you can simply modify constraints within a single model? [Constraints · JuMP](https://www.juliaopt.org/JuMP.jl/stable/constraints/#Constraint-modifications-1)

---

<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:** [March 16, 2020, 7:32pm UTC](https://discourse.julialang.org/t/jumpv0-21-and-deepcopy/36051/5 "2020-03-16T19:32:15Z")

</div>

@ericphanson is correct on all counts.

Use constraint modifications, or delete the constraint and add a new one.

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [March 17, 2020, 2:56am UTC](https://discourse.julialang.org/t/jumpv0-21-and-deepcopy/36051/6 "2020-03-17T02:56:44Z")

</div>

I will just chime in because this change (`deepcopy` throwing an error) was my contribution (first suggested by @Thuener [here](https://discourse.julialang.org/t/strange-behavior-with-deepcopy/23342/7), then suggested by me again [here](https://discourse.julialang.org/t/jump-gurobi-bug-value-of-variable-in-solved-model-changes-after-deleting-another-variable/29619/7), and finally implemented by me [here](https://github.com/JuliaOpt/JuMP.jl/pull/2079)).

The fact is: `deepcopy` has never ever worked, it always had basically the same behavior as `copy` that you say it is incompatible with your usage. There was never a code that correctly implemented `deepcopy` and now it was thrown out (replaced by an error), the fact `deepcopy` was implemented for JuMP.Model was mere accident, because `deepcopy` [is automatically defined for all types](https://github.com/JuliaLang/julia/blob/2d5741174ce3e6a394010d2e470e4269ca54607f/base/deepcopy.jl#L57) but this does not work correctly/out-of-the-box if the type in question has a pointer to an external C object.

If your code was working correctly with old `deepcopy` then either: it was incorrect; or it will work with `copy` too. The other solutions given in this thread are far better however.

---

<div class="post-metadata">

**Author:** ![dmetivie](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dmetivie/32/6926_2.png) [@dmetivie](https://discourse.julialang.org/u/dmetivie)\
**Post date:** [March 17, 2020, 4:22pm UTC](https://discourse.julialang.org/t/jumpv0-21-and-deepcopy/36051/7 "2020-03-17T16:22:09Z")

</div>

Thanks, everyone.  
Constraint modifications work well and faster than before.  
@Henrique_Becker, indeed maybe in my code it was doing the same as `copy`. However, I am sure at some point in time and version (before we could do constraints modifications), I tested in-depth `deepcopy()` vs ‘copy()’ with JuMP and actually saw a different behavior but as you said it was probably an uncontrolled effect.

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [March 17, 2020, 5:03pm UTC](https://discourse.julialang.org/t/jumpv0-21-and-deepcopy/36051/8 "2020-03-17T17:03:17Z")

</div>

The behavior of `copy` and `deepcopy` come from different code (`copy` has [a specific implementation in JuMP](https://github.com/JuliaOpt/JuMP.jl/blob/master/src/copy.jl), while `deepcopy` [defaulted to the generic type fallback](https://github.com/JuliaLang/julia/blob/2d5741174ce3e6a394010d2e470e4269ca54607f/base/deepcopy.jl#L57)), so it is entirely possible that you may have observed some difference between them; however, `deepcopy` should not really be fully working, as @Thuener suggested to make it an error exactly because they [were surprised by a silent change to the deepcopied model after changing the original model](https://discourse.julialang.org/t/strange-behavior-with-deepcopy/23342).
