Multiple variables in gurobipy model

1,075 views
Skip to first unread message

Laura Catalina Echeverri Guzmán

unread,
Aug 22, 2013, 12:55:21 PM8/22/13
to gur...@googlegroups.com
Hi everyone,

I'm new at Gurobi (and Python) and I'm trying to figure out how to create a matrix of decision variables in a Gurobi optimization model.

I know that the command to add a new variable is, for example,

 x = m.addVar(vtype=GRB.BINARY, name="x")

What is the command or how I can create a "matrix" of decision variables x?




Jakob Sch.

unread,
Aug 24, 2013, 6:27:21 AM8/24/13
to gur...@googlegroups.com
Hi Laura,

I'm not quite shure what you mean by matrix? gurobi can be used to solve linear and quadratic problems, but not semidefinite problems (where I would usually have matrix variables).
To create a "matrix" of decision variables you would have to do something like this:
nrows=3
ncols=4
matrix = []
m=Model()
for i in range(nrows):
    row = []
    for j in range(ncols):
        x = m.addVar(vtype=GRB.BINARY, name="x_{0}_{1}".format(i,j))
        row.append(x)
    matrix.append(row)
m.update()


This would give you a 3x4 matrix with binary variables. Internally this is stored as a list of lists and can be acessed via matrix[i][j] for i-th row and j-th column.
Hope this helps...

Best regards,
Jakob

Laura Catalina Echeverri Guzmán

unread,
Sep 3, 2013, 1:01:45 PM9/3/13
to gur...@googlegroups.com
Thanks Jakob, that is what I was looking for.
Reply all
Reply to author
Forward
0 new messages