# Obtaining sub-optimal results from the model when the time limit is reached

**URL:** <https://discourse.julialang.org/t/obtaining-sub-optimal-results-from-the-model-when-the-time-limit-is-reached/85635>\
**Category:** Optimization (Mathematical)\
**Tags:** question, jump, optimization, ipopt\
**Created:** [August 11, 2022, 8:45pm UTC](https://discourse.julialang.org/t/obtaining-sub-optimal-results-from-the-model-when-the-time-limit-is-reached/85635 "2022-08-11T20:45:47Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![AAAG](https://avatars.discourse-cdn.com/v4/letter/a/f9ae1b/32.png) [@AAAG](https://discourse.julialang.org/u/AAAG)\
**Post date:** [August 11, 2022, 8:45pm UTC](https://discourse.julialang.org/t/obtaining-sub-optimal-results-from-the-model-when-the-time-limit-is-reached/85635/1 "2022-08-11T20:45:47Z")

</div>

Hi,  
I am working on a model that includes both linear and non-linear constraints. I am using Ipopt and Juniper solvers for my model.

What I am trying to do is to limit the time of the model and obtain the sub-optimal solution of the objective function and the decision variables. I am limiting the time using the following code:

```julia
nl_solver = optimizer_with_attributes(Ipopt.Optimizer, "print_level" => 0)
model = Model(optimizer_with_attributes(Juniper.Optimizer, "time_limit" => 200, "nl_solver"=>nl_solver))

```

I also tried using the following code:

```julia
set_time_limit_sec(model, 200.0)

```

However, the outcome of both implementations is the same. The model stops running and does not provide any results of the objective function and the decision variables. **Is there a way I can limit the time of the model and obtain the sub-optimal results found by the model so far?**

I am trying to limit the time because the model takes a very long time (in days, I am not sure if this is normal) to run. The same model (code) works perfectly fine if I use a smaller number of input parameters (variables) and provides the results within a few hours. But with a higher number of input parameters, the model takes so long and I am not sure if the model is stuck in some infinite loop or if there are any other reasons.

PS: I am new to optimization and JuMP/Julia so I apologize for the incorrect use of terminologies if there are any.

---

<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:** [August 11, 2022, 11:36pm UTC](https://discourse.julialang.org/t/obtaining-sub-optimal-results-from-the-model-when-the-time-limit-is-reached/85635/2 "2022-08-11T23:36:33Z")

</div>

What will be returned depends on the solver and the problem. If it has a feasible solution to return when hitting the time limit, most solvers will return that (but not every solver). It might also have no feasible solution to return.

You can see what Juniper has to return using `termination_status(model)` and `primal_status(model)`. What do you get for this problem?

---

<div class="post-metadata">

**Author:** ![AAAG](https://avatars.discourse-cdn.com/v4/letter/a/f9ae1b/32.png) [@AAAG](https://discourse.julialang.org/u/AAAG)\
**Post date:** [August 12, 2022, 3:47am UTC](https://discourse.julialang.org/t/obtaining-sub-optimal-results-from-the-model-when-the-time-limit-is-reached/85635/3 "2022-08-12T03:47:44Z")

</div>

Thank you for your response @odow.

> [@odow](#):
>
> What do you get for this problem?

So I ran the model with the time limit of 3600 seconds and I got the following output:

```julia
termination_status(model): TIME_LIMIT
primal_status(model): INFEASIBLE_POINT

```

Also, which solver supports this? I might try changing the solver. I am not sure if this will provide any major improvement to the model running time.

---

<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:** [August 12, 2022, 3:56am UTC](https://discourse.julialang.org/t/obtaining-sub-optimal-results-from-the-model-when-the-time-limit-is-reached/85635/4 "2022-08-12T03:56:53Z")

</div>

You could try KNITRO if you have a license, but Juniper is good choice.

In this case, it looks like there is a solution available that you could check. But it looks like it might be infeasible. You could check with `primal_feasbility_report(model)`.

---

<div class="post-metadata">

**Author:** ![AAAG](https://avatars.discourse-cdn.com/v4/letter/a/f9ae1b/32.png) [@AAAG](https://discourse.julialang.org/u/AAAG)\
**Post date:** [August 12, 2022, 7:46pm UTC](https://discourse.julialang.org/t/obtaining-sub-optimal-results-from-the-model-when-the-time-limit-is-reached/85635/5 "2022-08-12T19:46:27Z")

</div>

Thank you for your reply.

I am working on the suggestion you just mentioned. But I have a question.

> [@odow](#):
>
> But it looks like it might be infeasible.

What does “infeasible” means here? (a) Does it mean that the current solution does not meet all the constraints? or (b) does it mean that it meets all the constraints but it is not the optimal value of the objective function?

---

<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:** [August 12, 2022, 8:28pm UTC](https://discourse.julialang.org/t/obtaining-sub-optimal-results-from-the-model-when-the-time-limit-is-reached/85635/6 "2022-08-12T20:28:47Z")

</div>

Infeasible means that the point does not satisfy the constraints.

---

<div class="post-metadata">

**Author:** ![AAAG](https://avatars.discourse-cdn.com/v4/letter/a/f9ae1b/32.png) [@AAAG](https://discourse.julialang.org/u/AAAG)\
**Post date:** [August 12, 2022, 8:34pm UTC](https://discourse.julialang.org/t/obtaining-sub-optimal-results-from-the-model-when-the-time-limit-is-reached/85635/7 "2022-08-12T20:34:51Z")

</div>

> [@odow](#):
>
> You could check with `primal_feasbility_report(model)`.

I am getting the following output:

```julia
primal_feasbility_report(model): Dict{Any, Float64}()

```

What does that mean?

---

<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:** [August 12, 2022, 8:55pm UTC](https://discourse.julialang.org/t/obtaining-sub-optimal-results-from-the-model-when-the-time-limit-is-reached/85635/8 "2022-08-12T20:55:17Z")

</div>

I had a suspicion. The point is actually feasible, so the point is exactly the suboptimal point you want.

But there is a bug in Juniper that reports it as infeasible.

---

<div class="post-metadata">

**Author:** ![AAAG](https://avatars.discourse-cdn.com/v4/letter/a/f9ae1b/32.png) [@AAAG](https://discourse.julialang.org/u/AAAG)\
**Post date:** [August 12, 2022, 9:04pm UTC](https://discourse.julialang.org/t/obtaining-sub-optimal-results-from-the-model-when-the-time-limit-is-reached/85635/9 "2022-08-12T21:04:05Z")

</div>

So is there a way I can access this solution after the time limit is reached?

> [@odow](#):
>
> But there is a bug in Juniper that reports it as infeasible.

And what do you suggest to do if there is a bug in Juniper? Change of solver or something else?

---

<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:** [August 12, 2022, 9:30pm UTC](https://discourse.julialang.org/t/obtaining-sub-optimal-results-from-the-model-when-the-time-limit-is-reached/85635/10 "2022-08-12T21:30:22Z")

</div>

> So is there a way I can access this solution after the time limit is reached?

Just access it as normal with `value(x)` and `objective_value(model)` etc.

> And what do you suggest to do if there is a bug in Juniper?

We’ll get it fixed: [Incorrect PrimalStatus if termination is not optimal · Issue #249 · lanl-ansi/Juniper.jl · GitHub](https://github.com/lanl-ansi/Juniper.jl/issues/249)

But you can probably just use the point (with care).

---

<div class="post-metadata">

**Author:** ![AAAG](https://avatars.discourse-cdn.com/v4/letter/a/f9ae1b/32.png) [@AAAG](https://discourse.julialang.org/u/AAAG)\
**Post date:** [August 17, 2022, 1:38pm UTC](https://discourse.julialang.org/t/obtaining-sub-optimal-results-from-the-model-when-the-time-limit-is-reached/85635/11 "2022-08-17T13:38:45Z")

</div>

> [@odow](#):
>
> We’ll get it fixed

Thank you for your efforts to fix this.

So I have a quick update on what I did so far.

> [@AAAG](#):
>
> Change of solver or something else?

I tried changing the solver and I tested almost all non-commercial solvers. But each had its own errors and issues with my model, which I am not going to explain here.

But currently I am working with **SCIP Solver** , and surprisingly I am able to get the results from the model in a very short time. This is the output using **SCIP** solver:

```julia
SCIP Status : problem is solved [optimal solution found]
Solving Time (sec) : 34.32
Solving Nodes : 7506
Primal Bound : +2.62483019292676e+01 (18 solutions)
Dual Bound : +2.62483019292676e+01
Gap : 0.00 %
objective_value= 26.24830192926761
Status:OPTIMAL

```

While running the exact same model with the exact same inputs using **Juniper** produces the following output:

```julia
#branches: 89082
Obj: 26.24830569524511
objective_value= 26.24830569524511
Status:LOCALLY_SOLVED
Solving Time (sec): 16366.3

```

I verified the outputs I got using SCIP and it satisfies all the constraints in my model. **My question is, why is there a huge difference in the model run time between Juniper and SCIP?** I just want to understand from where this difference is coming, the model or the solver.

---

<div class="post-metadata">

**Author:** ![AAAG](https://avatars.discourse-cdn.com/v4/letter/a/f9ae1b/32.png) [@AAAG](https://discourse.julialang.org/u/AAAG)\
**Post date:** [August 26, 2022, 8:47pm UTC](https://discourse.julialang.org/t/obtaining-sub-optimal-results-from-the-model-when-the-time-limit-is-reached/85635/12 "2022-08-26T20:47:02Z")

</div>

For anyone looking at this post in the future, I will explain how I got around this issue.

As I mentioned in my previous reply, I changed the solver from Juniper to [SCIP](https://github.com/scipopt/SCIP.jl).

With SCIP, I used the time limiting option to limit the time of the model and obtain the sub-optimal solution. This is how I defined the SCIP solver and the time limit (in seconds) option:

```julia
using SCIP
model = Model(() -> SCIP.Optimizer(limits_time = 10800))

```

Once the limit is reached, the SCIP will provide the best solution reached so far.

Hopefully, the bug with Juniper will be fixed. But I wanted to help anyone visiting this post for a similar issue they are facing.

Regards.
