Skip to content

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

@linde-frolke

Description

@linde-frolke

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()

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions