gurobipy.GurobiError: Name too long (maximum name length is 255 characters (python)
AnsweredA similar issue to the post in https://support.gurobi.com/hc/en-us/community/posts/8827103170065-Gurobipy-addMConstr-raises-GurobiError-Name-too-long-when-passing-list-of-names?page=1#community_comment_24815782077969 (gurobipy.GurobiError: Name too long (maximum name length is 255 characters)), which is occurred when adding variables by calling model.addVars().
I figured out the reason for this after printing the name of another variable with four indices, as follows (here, I replaced the original text with multiple "x" to hide information):
x[<xxxxxxxxx.xxxxxxxxxx.xxxxxxxx object at 0x000002B85AD43750>,<xxxxxxx.xxxxxxxxx.xxxxxxxx object at 0x000002B85B07A7D0>, xxxxxx.XXXXX,<xxxxxxx.xxxxxxx.xxxxxxx object at 0x000002B85B07AA10>]
The addVars method automatically gives the name indices as the full address information of each corresponding object, resulting long name.
Is there any way to change the indices into a self-defined string?
Best regards
-
Official comment
This post is more than two years old. Some information may not be up to date. For current information, please check the Gurobi Documentation or Knowledge Base. If you need more help, please create a new post in the community forum, or try Gurobot, our chatbot interface offering instant, expert-level support. -
Hi Chao,
One option is to supply all the names as a list to the addVars function. As and example, instead of
A = ["mylongindexname_a", "mylongindexname_b", "mylongindexname_c"]
B = [1,2]
x = m.addVars(A,B, name="my_var")use
import itertools
def make_name(key):
indexA, indexB = key
return f"my_var_{indexA.split('_')[1]}_{indexB}"
names = [make_name(key) for key in itertools.product(A,B)]
x = m.addVars(itertools.product(A,B), name=names)You will need to define your make_name function to map the keys to your desired (and simplified) name structure.
- Riley
0 -
Dear Riley,
Thank you so much for your help! My problem is perfectly solved.
-Chao
0
Post is closed for comments.
Comments
3 comments