Gurobi raises GurobiError when a MIP stops at a limit without an incumbent

Đang mở
#1,134 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 1 ngày

Chưa có ai nhận issue này.

Đánh giá

Độ khó
3/5
Thời gian dự kiến
1-2 ngày
Mức phù hợp với người mới
70/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
python
Lĩnh vực
backend, databases

Hướng nghiên cứu

Start with safe_get_solution and the Gurobi get_solver_solution path in linopy/solvers.py, then compare with the HiGHS handling added in #1133. Check how Model.solve and WindowModel.solve behave when Gurobi reports SolCount == 0; done means both return the limit status without assigning a solution, while still returning an incumbent when one exists.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

bug

[!NOTE]
The following content was generated by AI.

Version Checks
  • I have confirmed this bug exists on the current master branch of Linopy.
Issue Description

When Gurobi stops a MIP at a limit (time, node, solution count) before it has found an incumbent, Model.solve raises GurobiError: Unable to retrieve attribute 'X' instead of returning the ok / time_limit status with no solution.

Cause: time_limit maps to SolverStatus.ok, so safe_get_solution calls the extraction function, which reads m.ObjVal and m.getAttr("X", ...) without checking m.SolCount. HiGHS handles this case since #1133 by checking value_valid; Gurobi needs the equivalent SolCount == 0 guard. The same applies to WindowModel.solve on Gurobi.

Reproducible Example
import numpy as np
import pandas as pd
import linopy

rng = np.random.default_rng(0)
n = 400
m = linopy.Model()
x = m.add_variables(binary=True, coords=[pd.RangeIndex(n, name="item")])
for k in range(30):
    m.add_constraints((rng.random(n) * x).sum() <= rng.random() * n / 4, name=f"c{k}")
m.add_objective(-(rng.random(n) * x).sum())
m.solve("gurobi", io_api="direct", TimeLimit=0.0, OutputFlag=0)
Traceback
  File "linopy/model.py", line 2287, in solve
  ...
  File "linopy/solvers.py", line 1479, in safe_get_solution
    return func()
  File "linopy/solvers.py", line 2668, in get_solver_solution
    sol = np.array(m.getAttr("X", vars_), dtype=float)
gurobipy._exception.GurobiError: Unable to retrieve attribute 'X'
Expected Behavior

m.solve(...) returns ("ok", "time_limit") with no solution assigned, as HiGHS does for the same case. If an incumbent exists, it is returned.

Installed Versions

linopy 0.10.0.post1.dev86 (master), gurobipy 13.0.1

Ngôn ngữ chính
Python
Star
263
Fork
93
Merge trung bình
1 ngày 10 giờ
Pull request đã merge (30 ngày)
68

Chuẩn bị môi trường

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của PyPSA/linopy

Tất cả issue của PyPSA/linopy

Issue tương tự

Thêm issue về Python

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.