About Model.getVars in Gurobi

3,578 views
Skip to first unread message

Yongjia Song

unread,
Jan 18, 2010, 5:08:48 PM1/18/10
to gur...@googlegroups.com
Sorry to bother all,

I would like to know how to use the function Model.getVars in Gurobi so that I can get the variables with specific names that I have set up when I add variables with names.

For example,

binvars = []
    for i in range(Last):
        newvar = m.addVar(0.0,1.0,0,GRB.BINARY,"binvar"+str(i))
        binvars += [newvar]

And when I use Callback, how can I get the variables with name "binvar"?

Thanks,

--
Sincerely:
Song Yongjia(宋永佳)

Department of Industrial and Systems Engineering
College of Engineering, University of Wisconsin-Madison
3241 Mechanical Engineering Building
1513 University Avenue, Madison, WI 53706

Ed Rothberg

unread,
Jan 18, 2010, 7:08:26 PM1/18/10
to Gurobi Optimization

Take a look at the first message in this group ('A bonus example').
The example code in that message does almost exactly what you are
trying to do.

Ed


Yongjia Song

unread,
Jan 18, 2010, 9:10:54 PM1/18/10
to gur...@googlegroups.com
Thanks for the example, but what if I have several kinds of integer variables?

For example, I have named integer variables x1,x2,...,xn a name "K[i],
i=1,2,...,n", and integer variables y1,y2,...,yn a name "P[i],
i=1,2,...,n", so how can I extract them seperately because I may have
different heuristic on them.

Thanks a lot!

Best,

Ed Rothberg

unread,
Jan 18, 2010, 9:19:19 PM1/18/10
to Gurobi Optimization

This sounds like a Python programming question.

The following...

[x for x in model.getVars() if x.VarName.find('binvar') != -1]

...gives you a list of all variables whose names contain the string
'binvar'.

Yongjia Song

unread,
Jan 19, 2010, 8:43:08 AM1/19/10
to gur...@googlegroups.com
OK, thanks, I've got it.

Best,

On 1/18/10, Ed Rothberg <roth...@gurobi.com> wrote:
>

Akul Bansal

unread,
Sep 17, 2018, 3:09:30 PM9/17/18
to Gurobi Optimization
This may not be relevant now but I am posting it anyways so that it may be helpful if someone visits this later. You can access variable by their names using the function mentioned here http://www.gurobi.com/documentation/8.0/refman/py_model_getvarbyname.html
Reply all
Reply to author
Forward
0 new messages