Gurobipy addMConstr() raises GurobiError: Name too long when passing list of names
AnsweredI am trying to add constraints via the model.addMConstr() method. For debugging purposes I want to add names to the variables (so I can use them in e.g. a .lp file). However when I try to pass a list of corresponding names via the keyword name I get the following error message:
gurobipy.GurobiError: Name too long (maximum name length is 255 characters)
However, the longest variable name is "only" 47 characters long. I think it checks the sum of all characters instead of each variable name.
My code is:
model.addMVar(scipy_csr_matrix, gp_mvars, sense, b, name=names)
I checked the dimensions of the matrix, variables, sense, b and names and they are correct.
Thanks for any advice on how to solve this problem!
-
Could you please post a minimal reproducible example? Which Gurobi version are you using?
0 -
Sure, following code will raise the described error:
import gurobipy as gp
import scipy.sparse as sp
import numpy as np
model = gp.Model()
n = 1000
data = np.full(n, 1)
row = np.arange(n)
col = np.arange(n)
sense = np.full(n, '=')
b = np.full(n, 1)
names = list(map(str, range(n)))
A = sp.csr_matrix((data, (row, col)))
x = model.addMVar(A.get_shape()[1])
model.addMConstr(A, x, sense, b, names)I am using gurobipy 9.5.2 and guroby version 912 on windows 10.
0 -
Thank you for posting this example.
I can reproduce the issue and am investigating. I will post here about my findings.
0 -
After investigation, the \(\texttt{name}\) argument is indeed meant to be exactly 1 string, which will then be automatically indexed \(\texttt{name[i]}\) for each constraint. The current documentation does not properly reflect this.
I marked this as a feature request and this will probably be implemented in a future release.
0 -
Thanks for the quick response! I´m not sure I got this right, though. The problem is the implementation via gurobipy or in gurobi itself? Do you know where I could have a look in the future if I want to see if this has been implemented? Until now I have only found release notes for main relases of gurobipy (like 9.5) but since I guess this is more of a minor feature I am not sure where to look for it in the future. Thanks in advance!
0 -
The problem is the implementation via gurobipy or in gurobi itself?
Currently, there is no problem with either implementation. The addMVar function is not meant to accept a list of strings for constraint names but rather only a single string which will be automatically used for name generation. The documentation does not make it clear enough and will be improved with the upcoming release.
Do you know where I could have a look in the future if I want to see if this has been implemented?
Changes in the API will only be implemented in a major version and will be mentioned in the corresponding release notes. Minor changes in the documentation usually do not make it into the release notes.
0 -
For anyone wondering: From version 10.0.0 this is implemented and working correctly.
2
Please sign in to leave a comment.
Comments
7 comments