addvars names example

154 views
Skip to first unread message

Wilmer Henao

unread,
Oct 2, 2018, 3:53:23 PM10/2/18
to Gurobi Optimization
Hi everybody.

I'm using gurobipy and I'm trying to create a reference to a list of variables so that I can easily fetch its values after an optimization.

I create the variables and references more or less like this:

    m = Model("SOLVECONTINUOUS")
    z_plus = m.addVars(range(1000), lb = 0.0, obj = 1.0, vtype = GRB.CONTINUOUS, names = "z_plus")

and I was expecting to be able to fetch a value with an expression like this:

    m.getVarByName("z[345]")

Reading the documentation you'd get that impression. http://www.gurobi.com/documentation/8.0/refman/py_model_addvars.html

But it turns out that the name was never assigned:

    In: [x.VarName for x in m.getVars()]
     Out: ['C0',
     'C1',
     'C2',
     'C3',
     'C4', ...

what am I doing wrong?

praveenkumar beedanal

unread,
Oct 3, 2018, 6:34:32 AM10/3/18
to Gurobi Optimization
First thing is 

z_plus = m.addVars(range(1000), lb = 0.0, obj = 1.0, vtype = GRB.CONTINUOUS, names = "z_plus")
it should be "name" not "names"

Another thing is  you should use reference properly, 

it should be  m.getVarByName("z_plus [345]") not as you mentioned.

Hope you got the point.

Wilmer Henao

unread,
Oct 3, 2018, 6:34:32 AM10/3/18
to Gurobi Optimization
I meant: m.getVarByName("z_plus[345]")

Tobias Achterberg

unread,
Oct 3, 2018, 6:37:27 AM10/3/18
to gur...@googlegroups.com
But in any case, going through names is not needed and inefficient (it involves a hash
table lookup). You can just use your z_plus container, i.e., the return value of m.addVars():

z_plus = m.addVars(range(1000), lb = 0.0, obj = 1.0, vtype = GRB.CONTINUOUS, name = "z_plus")

...

m.optimize()

...

print z_plus[0].X


Regards,

Tobias
Reply all
Reply to author
Forward
0 new messages