ValueError using model.addMConstr since gurobipy 11.0.0
AnsweredSince updating to gurobipy I receive an ValueError when I try to use addMConstr if I include constraint names. Here is a minimal reproducible example:
import gurobipy as gpfrom gurobipy import GRB
import scipy.sparse as spmodel = gp.Model()
lb = np.array([0, 0])
ub = np.array([1, 1])
obj = np.array([1, 0])
vtypes = np.array([GRB.CONTINUOUS, GRB.CONTINUOUS], dtype='S1')
names = np.array(['a', 'b'], dtype='S54')
vals = np.array([1, 1])
rows = np.array([0, 1])
cols = np.array([0, 1])
A = sp.csr_array((vals, (rows, cols)))
gp_vars = model.addMVar((2, ), lb, ub, obj, vtypes, names)
sense = np.array(['>', '>'], dtype='S1')
rhs = np.array([1, 1])
constr_names = np.array(['A', 'B'])
model.addMConstr(A, gp_vars, sense, rhs, constr_names)
This gives me an ValueError ("The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()")
My numpy version is 1.26.4
Also I seem to be unable to install version 10.0.0 via pip anymore since it gives me following error message:
C:\>pip install gurobipy==10
ERROR: Ignored the following yanked versions: 11.0.0b1
ERROR: Could not find a version that satisfies the requirement gurobipy==10 (from versions: 11.0.0)
ERROR: No matching distribution found for gurobipy==10
before gurobipy 11 came out it worked perfectly fine though.
Any suggestions and help is very much appreciated.
-
Official comment
Christian,
in all versions 10.0.* and 11.0.0 you can use a list of strings as the 'name' argument for Model.addMConstr to give individual names per constraint. That's in line with the docstring of this method. For versions 10.0.* it just happened that an ndarray type for this argument was handled gracefully, but no longer in version 11.0.0. To be on the safe side, please pass in a list of names, e.g., do
model.addMConstr(A, gp_vars, sense, rhs, constr_names.tolist())
With the next technical release 11.0.1 the method will handle an ndarray name argument gracefully again, but we cannot make a promise that it will do so future versions.
-
Hi Christian,
Concerning the ValueError: The constr_names array is the problem, see the documentation of Model.addMConstr()
name: Single string defining the name for new constraints. The given name will be subscripted by the index of the constraint in the matrix.
For the installation issue, note that Python 3.12. is not supported by Gurobi 10.0.3 or earlier. If you want to use Verison 10, please downgrade to Python 3.11.
Best regards,
Marika0 -
Hi Marika,
thanks for the quick response! I know this problem actually existed before version 10 of gurobi. I also created a post regarding that a while ago (https://support.gurobi.com/hc/en-us/community/posts/8827103170065-Gurobipy-addMConstr-raises-GurobiError-Name-too-long-when-passing-list-of-names). But it did work in gurobi 10 so I´m wondering why this feature was removed in version 11? Do you maybe know if there is already a feature request for this? Otherwise I will create one.
Best regards,
Christian0
Please sign in to leave a comment.
Comments
3 comments