# Should models revert to "unoptimized" if they are modified?

**URL:** https://discourse.julialang.org/t/should-models-revert-to-unoptimized-if-they-are-modified/59627
**Category:** Optimization (Mathematical)
**Created:** [April 19, 2021, 11:19pm UTC](https://discourse.julialang.org/t/should-models-revert-to-unoptimized-if-they-are-modified/59627 "2021-04-19T23:19:20Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![gleyland](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gleyland/32/15339_2.png) [@gleyland](https://discourse.julialang.org/u/gleyland)
#### Post date: [April 19, 2021, 11:19pm UTC](https://discourse.julialang.org/t/should-models-revert-to-unoptimized-if-they-are-modified/59627/1 "2021-04-19T23:19:20Z")

</div>

If I `optimize!` a model with GLPK, then add a constraint, I can still access the solution:

```julia
using JuMP, GLPK, Clp;
M = Model();
@variable M A[1:2] >= 0;
@constraint M limit1 sum(a for a in A) <= 1;
@objective M Max sum(a for a in A);
set_optimizer(M, GLPK.Optimizer);
optimize!(M);
@constraint M limit2 sum(a for a in A) <= 2;
value(M[:A][1])
1.0

```

But if I do the same thing in Clp, I get `OptimizeNotCalled()`:

```julia
using JuMP, GLPK, Clp;
M = Model();
@variable M A[1:2] >= 0;
@constraint M limit1 sum(a for a in A) <= 1;
@objective M Max sum(a for a in A);
set_optimizer(M, Clp.Optimizer);
optimize!(M);
@constraint M limit2 sum(a for a in A) <= 2;
value(M[:A][1])
ERROR: OptimizeNotCalled()

```

Should this be consistent? Is it solver specific? Do we care?

(I had inadvertently been relying on GLPK’s behaviour, but I can work with Clp’s).

Thanks!  
Geoff

---

<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, 2021, 5:14pm UTC](https://discourse.julialang.org/t/should-models-revert-to-unoptimized-if-they-are-modified/59627/2 "2021-04-20T17:14:10Z")

</div>

![image](https://global.discourse-cdn.com/julialang/original/3X/3/f/3fbb2953c08a0fb5e21076e7bd41e2292bf22161.png)

> **[Solutions · JuMP](https://jump.dev/JuMP.jl/stable/manual/solutions/#Recommended-workflow)**
>
> Documentation for JuMP.

This is really hard to do consistently. Some solvers still let you access the solution, others don’t. Resolving this would mean adding a check to solvers like GLPK so that at every `value`, `dual`, or `objective_value` call, they check if the problem had been modified since the last solve.

This problem keeps coming up, however, so it looks like we might have to.

---

<div class="post-metadata">

### Author: ![gleyland](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gleyland/32/15339_2.png) [@gleyland](https://discourse.julialang.org/u/gleyland)
#### Post date: [April 20, 2021, 9:06pm UTC](https://discourse.julialang.org/t/should-models-revert-to-unoptimized-if-they-are-modified/59627/3 "2021-04-20T21:06:24Z")

</div>

Thanks @odow! Apologies for missing that the documentation.

(And I think I’ll expand my test suite to more than one optimiser)

---

<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, 2021, 10:06pm UTC](https://discourse.julialang.org/t/should-models-revert-to-unoptimized-if-they-are-modified/59627/4 "2021-04-20T22:06:07Z")

</div>

We could probably fix (i.e., throw an error in all cases) this at the JuMP level: [Error on modify-then-query · Issue #2566 · jump-dev/JuMP.jl · GitHub](https://github.com/jump-dev/JuMP.jl/issues/2566)

---

<div class="post-metadata">

### Author: ![cvanaret](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cvanaret/32/11594_2.png) [@cvanaret](https://discourse.julialang.org/u/cvanaret)
#### Post date: [April 23, 2021, 9:22pm UTC](https://discourse.julialang.org/t/should-models-revert-to-unoptimized-if-they-are-modified/59627/5 "2021-04-23T21:22:05Z")

</div>

It would be interesting to track how the model was modified (was a variable added? A constraint? The objective?) in order to improve the “reoptimizing phase”:

- if the feasible set didn’t change, you can warm start the new problem with the old problem’s solution ;
- if constraints were added, you can use duality (if applicable) to warm start the dual problem.
