GurobiError: Name too long (maximum name length is 255 characters)

950 views Asked by At

I define a parameter t[i,s] as follows:

for i in Trucks:
    for s in Slots:
        t[i,s]=m.addVar(vtype=GRB.CONTINUOUS, name="t[%s,%s]"%(i,s))

I call the values of t[i,s] from an excel file. I is a list which contains a numbers from 0 to 263, also s is a list from 1 to 24. The problem appears when I run the code the following bug occurs:

GurobiError: Name too long (maximum name length is 255 characters)

How can I fix that?

2

There are 2 answers

0
ekrall On

it is unfortunate that there is no great way to deal with this, or that the limit exists at all. The gurobipy variable constructors work well when using tuplelists to define the sets/indices over which a variable exists.

But then the variable names, which are strings, become a function of the tuplelist, have 255 character limit, and there does not seem to be any good way to control it (unless you start manipulating your tuples, in which case you may be losing information you wanted to keep).

  1. why is there no way you can adjust the 255 character limit if needed? If there is, I have not found it
  2. given that there is a limit, why doesn't gurobipy just truncate the name at 255 characters? as far as I can tell this would not really affect anything besides the way the variable appears when printed to a model file (e.g. .lp format).

it is frustrating that the answer is just "unfortunately it works this way". it can cause a model to fail when in fact there is no great reason for it to be failing

5
mattmilten On

In case the items in Trucks and Slots are just strings, you can limit the length that is passed to the variable name like this:

maxlen = 250
for i in Trucks:
  for s in Slots:
    t[i,s] = m.addVar(vtype=GRB.CONTINUOUS, name="t[%s,%s]"%(i[:maxlen],s[:maxlen]))

Strings in Python can be treated just like any other array and support slicing.