coin-or / coin-or/python-mip

Model.var_by_name throws error if the model contains no variables when using gurobi as solver

Open
#422 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
Linear Programming
Stars
600
Forks
108
PR merge metrics
No merged PRs in 30d

Description

**Describe the bug**

The behavior of the function `model.get_var_by_name()` is unexpected when using Gurobi as solver. According to the docstring, the function should return None if no variable with that name exists. However, when using it with Gurobi, I get an error `ParameterNotAvailable` when using the function on a model **without any variables**.

Image

**To Reproduce**
1. Create an instance of a model with Gurobi as solver
2. Use the `model.var_by_name(name="randomname")` function.

```
model = mip.Model(solver_name=mip.GUROBI)
variable = model.var_by_name(name="variablename")
```

**Expected behavior**
From the docstring, None should be returned.

**Desktop (please complete the following information):**
- Operating System, version: WSL2 on Windows 11
- Python version: 3.13.11
- Python-MIP version (we recommend you to test with the latest version):

**Additional context**
The behavior of Gurobi is different than for example HiGHS here, which does not throw the error. In my opinion the behavior of `Model.var_by_name()` should not be solver dependent.

The exact same problem exists for the constraint version of this function, namely `model.constrs_by_name()`

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.