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

オープン
#1,134 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

まだ誰も着手していません。

評価

難易度
3/5
見積もり時間
1〜2日
初心者へのやさしさ
70/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
python
領域
backend, databases

調査の方向性

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.

索引モデルが issue の本文から書いたものです。

説明

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

主要言語
Python
スター
263
フォーク
93
平均マージ
1日 10時間
マージ済み PR(30日)
68

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

PyPSA/linopy のほかの issue

PyPSA/linopy の issue をすべて見る

似ている issue

Python の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。