Reset Solver in Julia

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.

2 Likes

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:

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

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 :smile:

2 Likes

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:

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).

1 Like

It is a bit weird A_i_mats is generated every iteration:

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

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

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.

1 Like

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 :sweat_smile:. Thanks for coming into the thread and clearing things up!

1 Like

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.

No problem.

I would refactor your code to:

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.

1 Like