# How to continue for loop even if model is infeasible

**URL:** <https://discourse.julialang.org/t/how-to-continue-for-loop-even-if-model-is-infeasible/104219>\
**Category:** Optimization (Mathematical)\
**Tags:** jump, gurobi\
**Created:** [September 25, 2023, 12:12pm UTC](https://discourse.julialang.org/t/how-to-continue-for-loop-even-if-model-is-infeasible/104219 "2023-09-25T12:12:17Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![cdomenig](https://avatars.discourse-cdn.com/v4/letter/c/71e660/32.png) [@cdomenig](https://discourse.julialang.org/u/cdomenig)\
**Post date:** [September 25, 2023, 12:12pm UTC](https://discourse.julialang.org/t/how-to-continue-for-loop-even-if-model-is-infeasible/104219/1 "2023-09-25T12:12:17Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![mike\_k](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mike_k/32/211864_2.png) [@mike\_k](https://discourse.julialang.org/u/mike_k)\
**Post date:** [September 25, 2023, 1:50pm UTC](https://discourse.julialang.org/t/how-to-continue-for-loop-even-if-model-is-infeasible/104219/2 "2023-09-25T13:50:42Z")

</div>

Welcome!  
You could, for instance, only query variable values if you found some result using the [result\_count()](https://jump.dev/JuMP.jl/stable/api/JuMP/#result_count) or [has\_values()](https://jump.dev/JuMP.jl/stable/api/JuMP/#has_values) function. Something like

```julia
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](https://jump.dev/JuMP.jl/stable/moi/reference/models/#MathOptInterface.TerminationStatusCode), because infeasibility is not implied when no results were found (which could be also the case if a time limit etc is reached).

---

<div class="post-metadata">

**Author:** ![cdomenig](https://avatars.discourse-cdn.com/v4/letter/c/71e660/32.png) [@cdomenig](https://discourse.julialang.org/u/cdomenig)\
**Post date:** [September 25, 2023, 2:16pm UTC](https://discourse.julialang.org/t/how-to-continue-for-loop-even-if-model-is-infeasible/104219/3 "2023-09-25T14:16:54Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![daro.slai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/daro.slai/32/48757_2.png) [@daro.slai](https://discourse.julialang.org/u/daro.slai)\
**Post date:** [September 26, 2023, 8:11am UTC](https://discourse.julialang.org/t/how-to-continue-for-loop-even-if-model-is-infeasible/104219/4 "2023-09-26T08:11:24Z")

</div>

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

```julia
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!
