# Reset Solver in Julia

**URL:** https://discourse.julialang.org/t/reset-solver-in-julia/63756
**Category:** Optimization (Mathematical)
**Tags:** question, gurobi
**Created:** [June 29, 2021, 2:06pm UTC](https://discourse.julialang.org/t/reset-solver-in-julia/63756 "2021-06-29T14:06:27Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![Micah\_Mungal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/micah_mungal/32/14660_2.png) [@Micah\_Mungal](https://discourse.julialang.org/u/Micah_Mungal)
#### Post date: [June 29, 2021, 2:06pm UTC](https://discourse.julialang.org/t/reset-solver-in-julia/63756/1 "2021-06-29T14:06:27Z")

</div>

I am trying to do a sensitivity analysis study using Julia and Gurobi as the solver. In doing the sensitivity analysis simulation, I am changing the coefficient of one of the optimization variables, building the model and getting the solution. This procedure is then repeated with a different value for the coefficient. So basically, I have written a script which calls a function to perform the optimization in a loop using Visual Studio Code. I have noticed that I am getting some weird results, in that if I run the simulation in a loop I get different results compared to if I were to manually change the coefficient, run the simulation, get the results, close VS code and then reopen VS code and rerun with a different coefficient value. Not sure if the previous solution produced by the solver is affecting the solution produced. Is there anyway to reset the solution produced by the solver or some way of getting the same effect of closing and reopening VS code.

---

<div class="post-metadata">

### Author: ![fimiller](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fimiller/32/51226_2.png) [@fimiller](https://discourse.julialang.org/u/fimiller)
#### Post date: [March 4, 2024, 5:12pm UTC](https://discourse.julialang.org/t/reset-solver-in-julia/63756/2 "2024-03-04T17:12:56Z")

</div>

I am in a similar situation. Did you find a solution? I am finding things like `empty!(model)` did not work. I think `MOIU.reset_optimizer(model)` does not work either.

I think the following worked:

```julia
for i in 1:n
     model = Model(Optimizer)
     ... # setting up objective, constraints, etc. 
    set_optimizer(Optimizer) # same opt as above  
    optimize!(model)
    ... # stuff after, looking at solution, etc.

    MOIU.reset_optimizer(model)

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: [March 4, 2024, 7:53pm UTC](https://discourse.julialang.org/t/reset-solver-in-julia/63756/3 "2024-03-04T19:53:30Z")

</div>

Hi @fimiller,

Do you have a reproducible example of your problem? You shouldn’t ever need to call `reset_optimizer`.

p.s., @Micah_Mungal, sorry for not seeing this post at the time. I have since moved it to the “Optimization (Mathematical)” section 😄

---

<div class="post-metadata">

### Author: ![fimiller](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fimiller/32/51226_2.png) [@fimiller](https://discourse.julialang.org/u/fimiller)
#### Post date: [March 4, 2024, 8:06pm UTC](https://discourse.julialang.org/t/reset-solver-in-julia/63756/4 "2024-03-04T20:06:47Z")

</div>

I don’t easily have a minimal working example. Additionally, there was a chance that things were behaving as intended (i.e., not needing to call reset optimizer.). As for the specific matrix formulations, I unfortunately do not think I can go into those details at the moment. Sorry!

But here is the gist of my code:

```julia
n_tests = 3 
for i in 1:n_tests
     A_i_mats = build_data_matrix(...)
     C = cost_matrix(...)
     local model = Model(COSMO.Optimizer)
     @variable(model, X[1:size(A_i_mats[1])[1],1:size(A_i_mats[1])[1]], PSD)
     local b = spzeros(size(A_i_mats)[1])
     @constraint(model, c[i=1:size(A_i_mats)[1]], dot(A_i_mats[i], X) == b[i])
     @objective(model, Min,dot(C,X))
     set_optimizer(model, COSMO.Optimizer)
     optimize!(model)
     println(solution_summary(model))

     MOIU.reset_optimizer(model)

```

The first optimization algorithm outputs what is expected, but the second run didn’t. I suspected this had to do with warm starting / something along that line (or it is has to do with my data matrix and such, in which case this is not a JuMP problem).

---

<div class="post-metadata">

### Author: ![Dan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan/32/42581_2.png) [@Dan](https://discourse.julialang.org/u/Dan)
#### Post date: [March 4, 2024, 8:14pm UTC](https://discourse.julialang.org/t/reset-solver-in-julia/63756/5 "2024-03-04T20:14:29Z")

</div>

It is a bit weird `A_i_mats` is generated every iteration:

> [@fimiller](#):
>
> `A_i_mats = build_data_matrix(...)`

And yet it is subscripted with `i` (iteration index):

> [@fimiller](#):
>
> `dot(A_i_mats[i], X)`

Maybe some bits of the sample code are not accurate enough (also adding a little `end` at the end wouldn’t hurt).

---

<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 4, 2024, 8:17pm UTC](https://discourse.julialang.org/t/reset-solver-in-julia/63756/6 "2024-03-04T20:17:27Z")

</div>

You’re building a new `model` each iteration, so `MOIU.reset_optimizer(model)` cannot be going anything and it cannot be the problem. You are also not warm-starting.

I’m going to guess that this is an issue with your `build_data_matrix` or `cost-matrix`, but it’s impossible to tell without a reproducible example.

---

<div class="post-metadata">

### Author: ![fimiller](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fimiller/32/51226_2.png) [@fimiller](https://discourse.julialang.org/u/fimiller)
#### Post date: [March 4, 2024, 8:20pm UTC](https://discourse.julialang.org/t/reset-solver-in-julia/63756/7 "2024-03-04T20:20:04Z")

</div>

I see. I gave my stuff another run without the `reset_optimizer` and you’re right! Looks like that wasn’t the culprit and it was in my cost and constraint matrices 😅. Thanks for coming into the thread and clearing things up!

---

<div class="post-metadata">

### Author: ![fimiller](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fimiller/32/51226_2.png) [@fimiller](https://discourse.julialang.org/u/fimiller)
#### Post date: [March 4, 2024, 8:21pm UTC](https://discourse.julialang.org/t/reset-solver-in-julia/63756/8 "2024-03-04T20:21:45Z")

</div>

Yeah, sorry it wasn’t the most rigorous psuedocode for the problem. It’s creating a large SDP where the constraint matrices are typically indexed by `i`, so I guess the loop indexing would have been better to do by `test` or something like that.

---

<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 4, 2024, 8:27pm UTC](https://discourse.julialang.org/t/reset-solver-in-julia/63756/9 "2024-03-04T20:27:22Z")

</div>

No problem.

I would refactor your code to:

```julia
function solve_problem(A_i_mats, C)
    model = Model(COSMO.Optimizer)
    m, n = size(A_i_mats[1])
    @variable(model, X[1:m, 1:n], PSD)
    # @constraint(model, c[i=1:length(A_i_mats)], dot(A_i_mats[i], X) == 0)
    # or ...
    for Ai in A_i_mats
        @constraint(model, dot(Ai, X) == 0)
    end
    @objective(model, Min, dot(C, X))
    optimize!(model)
    println(solution_summary(model))
    return value.(X)
end

n_tests = 3 
X = Any[]
for i in 1:n_tests
     A_i_mats = build_data_matrix(...)
     C = cost_matrix(...)
     X_i = solve_problem(A_i_mats, C)
     push!(X, X_i)
end 

```

You want to the JuMP portion of the code to have well defined inputs and outputs. That should help you isolate the problem when debugging.
