Integers as name for Vars
回答済みHi,
Running the following code doesn't gives an error:
x_vars = m.addVars(341, vtype=gurobi.GRB.BINARY, name=[str(i - 170).zfill(1) for i in range(341)])
whereas
x_vars = m.addVars(341, vtype=gurobi.GRB.BINARY, name=[i - 170 for i in range(341)])
yields the following error:
{TypeError}object of type 'int' has no len()
The documentation (https://www.gurobi.com/documentation/9.1/refman/py_tupledict.html#pythonclass:tupledict) reads "Note that a tupledict key must be a tuple of scalar values (int, float, string, ...)." So, integers are allowed but why is the desired code yielding an error? Thanks
0
-
正式なコメント
This post is more than three 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 why not try our AI Gurobot?. -
Hi Huib,
The addVars function requires strings as inputs for the names of optimization variables. Thus the code
x = m.addVars([0,1],vtype=GRB.BINARY,name=[0,1])
will not work, because the list of names consists of integers and not strings. To name the variables \(0\) and \(1\) one would have to change the code to
x = m.addVars([0,1],vtype=GRB.BINARY,name=["0","1"])
or
x = m.addVars([0,1],vtype=GRB.BINARY,name=[str(0),str(1)])
Best regards,
Jaromił0
投稿コメントは受け付けていません。
コメント
2件のコメント