How to continue for loop even if model is infeasible

Hi everybody,

I am currently running a MILP model in a for loop, where I am trying out around 800 different parameter combinations (a kind of sensivity analysis). Sometimes the model doesn’t find a feasible solutions (this is expected) and then interrupts the entire for loop. I am using the Gurobi solver and getting this error: “Result index of attribute MathOptInterface.VariablePrimal(1) out of bounds. There are currently 0 solution(s) in the model.”

Is there any way to just skip to next parameter combination and continue the loop?

I tried implementing this using the code below, but the infeasible configurations were still interrupting the loop.

try
optimize(M!)
catch
#handle the error
end

Thank you for any help in advance!

1 Like

Welcome!
You could, for instance, only query variable values if you found some result using the result_count() or has_values() function. Something like

try
   optimize!(M)
   if result_count(M) > 0
      # query values of interest
   else
      @info "no results found"
   end
...

Alternatively / additionally, you could check for the termination status codes, because infeasibility is not implied when no results were found (which could be also the case if a time limit etc is reached).

1 Like

Thank you so much for the warm welcome.

I thought the interruption was coming from the infeasible model itself, but you pointed me in the right direction. The interruption was due to calling “value(…)” on an infeasible model.

1 Like

Following on the idea of termination_status what I do is something like:

if termination_status(model) == LOCALY_INFEASIBLE
   # what you want to do
end

What I’m looking for now are ways to salvage the optimization cause my application is real-time MPC.
If somebody has any info on this that’d be great.
cheers!