ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Wed, 18 Aug 2021 18:31:29 +0200Sage9.3 getting MILP model status after solve()https://ask.sagemath.org/question/58417/sage93-getting-milp-model-status-after-solve/I'm using Sage9.3 MixedIntegerLinearProgram. After attempting to solve a milp problem M using M.solve(), when the program stops it could be<br>
- M is solved with solution<br>
- M is infeasible<br>
- timeout (which I have imposed)<br>
- terminated by some external factor<br>
Is there a way (or function) to determine the status of M?
I'm using both GLPK and Gurobi, so would appreciate solutions for both solvers.
Thanks.Thu, 12 Aug 2021 04:18:10 +0200https://ask.sagemath.org/question/58417/sage93-getting-milp-model-status-after-solve/Answer by dsejas for <p>I'm using Sage9.3 MixedIntegerLinearProgram. After attempting to solve a milp problem M using M.solve(), when the program stops it could be<br>
- M is solved with solution<br>
- M is infeasible<br>
- timeout (which I have imposed)<br>
- terminated by some external factor<br></p>
<p>Is there a way (or function) to determine the status of M?
I'm using both GLPK and Gurobi, so would appreciate solutions for both solvers.
Thanks.</p>
https://ask.sagemath.org/question/58417/sage93-getting-milp-model-status-after-solve/?answer=58452#post-id-58452Hello, @Sim! I don't know about Gurobi, so perhaps its behaviour could be different, but at least with GLPK, you have the following possibilities:
1. If `M` has a solution: The instruction `M.solve()` should show you the optimum value of the objective function. Additionally, you can write `M.get_values(x)`, where `x` is the family of variables of the MILP, in order to get the values that produce the optimum.
2. `M` is infeasible: Sage should show you the message "`MIPSolverException: GLPK: Problem has no feasible solution`".
3. Timeout (which you have imposed): Given that you imposed the time restriction, it should be as easy as to add a `print` statement with a message such as "Time constraint exceeded!"
Concerning the option "terminated by some external factor", it is not clear to me what you mean by "external factor". However, any event I could think of that could fit this description, should be rare enough to not have to worry about it. (Perhaps you could elaborate on this or present a couple of hypothetical examples.)
I hope this helps!Sat, 14 Aug 2021 07:29:32 +0200https://ask.sagemath.org/question/58417/sage93-getting-milp-model-status-after-solve/?answer=58452#post-id-58452Comment by Sim for <p>Hello, <a href="/users/32121/sim/">@Sim</a>! I don't know about Gurobi, so perhaps its behaviour could be different, but at least with GLPK, you have the following possibilities:</p>
<ol>
<li>If <code>M</code> has a solution: The instruction <code>M.solve()</code> should show you the optimum value of the objective function. Additionally, you can write <code>M.get_values(x)</code>, where <code>x</code> is the family of variables of the MILP, in order to get the values that produce the optimum.</li>
<li><code>M</code> is infeasible: Sage should show you the message "<code>MIPSolverException: GLPK: Problem has no feasible solution</code>".</li>
<li>Timeout (which you have imposed): Given that you imposed the time restriction, it should be as easy as to add a <code>print</code> statement with a message such as "Time constraint exceeded!"</li>
</ol>
<p>Concerning the option "terminated by some external factor", it is not clear to me what you mean by "external factor". However, any event I could think of that could fit this description, should be rare enough to not have to worry about it. (Perhaps you could elaborate on this or present a couple of hypothetical examples.)</p>
<p>I hope this helps!</p>
https://ask.sagemath.org/question/58417/sage93-getting-milp-model-status-after-solve/?comment=58533#post-id-58533Hi @dsejas,
Thanks for your suggestion.<br>
Terminated by some external factor refers to the program terminating prematurely, e.g. by "Ctrl+c" or "kill" instruction<br>
I'm running the program in the background so currently I'm outputting a log file of the entire solving process and manually check if there is an optimal solution found, infeasible or timeout.<br>
But I would like to automate this process.<br>
An example pseudocode<br>
M.solve()
if M is solved:<br>
\#output the optimal solution<br>
if M is infeasible:<br>
\#output infeasible message<br>
if M is timeout:<br>
\#output best upper and lower bound so far<br>
if M is terminated by external factor:<br>
\#output best upper and lower bound so farWed, 18 Aug 2021 18:31:29 +0200https://ask.sagemath.org/question/58417/sage93-getting-milp-model-status-after-solve/?comment=58533#post-id-58533