Check if model is unbounded and raise error
回答済みI am running a model for several instances, and I am saving these instances' objectives and solving time. When I check the output of the cases, some of the models are unbounded but still, an objective and solving time is calculated. However, I want to exclude these results. How can I check if a model is unbounded and raise an error? I tried to use the GRB.OPTIMAL status, but that status is 2 (optimal) even though the output mentions that the model is unbounded.
here is an example of such an output:
Gurobi Optimizer version 9.5.0 build v9.5.0rc5 (win64)
Thread count: 6 physical cores, 12 logical processors, using up to 12 threads
Optimize a model with 170 rows, 830 columns and 1670 nonzeros
Model fingerprint: 0x9282574f
Variable types: 810 continuous, 20 integer (20 binary)
Coefficient statistics:
Matrix range [6e-01, 4e+05]
Objective range [1e-02, 3e+03]
Bounds range [1e+00, 1e+00]
RHS range [4e+03, 1e+15]
Warning: Model contains large rhs
Consider reformulating model or setting NumericFocus parameter
to avoid numerical issues.
Found heuristic solution: objective 3.330361e+31
Presolve removed 20 rows and 0 columns
Presolve time: 0.00s
Explored 0 nodes (0 simplex iterations) in 0.00 seconds (0.00 work units)
Thread count was 1 (of 12 available processors)
Solution count 1: 3.33036e+31
No other solutions better than 0
Model is unbounded
Best objective 3.330361052383e+31, best bound -, gap -
GRB.OPTIMAL 2
the optimization time=9.0342e-05
objective:3.3303610523833124e+31
-
Could you share a snippet of the code you are using to check your model's status after optimization?
Please note that the range of your right hand side constants is huge and you should definitely try to improve the numerics of your model, see Guidelines for Numerical Issues for more details.
Best regards,
Jaromił0 -
Hi Jaromił,
I use this code to check the optimality:print(f"GRB.OPTIMAL {grb.GRB.OPTIMAL}")
I check the status by printing the output into a file:
sys.stdout = open('filename.txt')
and then solving the model
opt_model = grb.Model(name="MIP Model")
# variables and constraints are addedopt_model.ModelSense = grb.GRB.MAXIMIZE
opt_model.setObjective(objective)
opt_model.setParam('MIPGap', OPT_MODEL_GAP)
opt_model.setParam('Worklimit', WORK_LIMIT)prob_obj = opt_model.optimize()
I agree that the model should not be unbounded at all and I will check the data, but I want to generate a message in case the model is still unbounded.
0 -
The line
print(f"GRB.OPTIMAL {grb.GRB.OPTIMAL}")
will always print
GRB.OPTIMAL 2
because you are literally requesting the value of the enumerator \(\texttt{grb.GRB.OPTIMAL}\.)
To get the current model status, you have to retrieve its attribute
print(f"GRB.OPTIMAL {opt_model.Status}")
Best regards,
Jaromił0 -
Thank you very much! That helped me a lot.
0
サインインしてコメントを残してください。
コメント
4件のコメント