tcokyasar
tcokyasar

Reputation: 592

Using Gurobi in Python and adding variables

I am trying to write my first Gurobi optimization code and this is where I am stuck with:

I have the following dictionary for my first subscript:

input for k in range(1,11): i[k] = int(k) print i

output {1: 1, 2: 2, 3: 3, 4: 4, 5: 5, 6: 6, 7: 7, 8: 8, 9: 9, 10: 10}

And, I have the following dictionaries for my second subscript:

c_il = {1: 2, 2: 1, 3: 1, 4: 4, 5: 3, 6: 4, 7: 3, 8: 2, 9: 1, 10: 4}

c_iu = {1: 3, 2: 2, 3: 2, 4: 5, 5: 4, 6: 5, 7: 4, 8: 3, 9: 2, 10: 5}

I am trying to create variables as following:

x = m.addVars(i, c_il, vtype=GRB.BINARY, name="x")
x = m.addVars(i, c_iu, vtype=GRB.BINARY, name="x")

Apparently, it is not giving what I am looking for. What I am looking for is x_(i),(c_il) and x_(i),(c_iu); ignore parenthesis.

More clearly, the following is what I am trying to obtain by using dicts i, c_il, and c_iu:

{1: <gurobi.Var x[1,2]>,
 2: <gurobi.Var x[2,1]>,
 3: <gurobi.Var x[3,1]>,
 4: <gurobi.Var x[4,5]>,
 5: <gurobi.Var x[5,3]>,
 6: <gurobi.Var x[6,4]>,
 7: <gurobi.Var x[7,3]>,
 8: <gurobi.Var x[8,2]>,
 9: <gurobi.Var x[9,1]>,
 10: <gurobi.Var x[10,4]>,
 11: <gurobi.Var x[1,3]>,
 12: <gurobi.Var x[2,2]>,
 13: <gurobi.Var x[3,2]>,
 14: <gurobi.Var x[4,5]>,
 15: <gurobi.Var x[5,4]>,
 16: <gurobi.Var x[6,5]>,
 17: <gurobi.Var x[7,4]>,
 18: <gurobi.Var x[8,3]>,
 19: <gurobi.Var x[9,2]>,
 20: <gurobi.Var x[10,5]>}

Since I am using dictionaries everywhere, I want to keep it consistent by continuing to use dictionaries so that I can do multiplications and additions with my parameters which are all in dictionaries. Is there any way to create these variables with m.addVars or m.addVar?

Thanks!

Edit: Modified to make it more clear.

Upvotes: 0

Views: 3955

Answers (2)

tcokyasar
tcokyasar

Reputation: 592

After a few years of experience, I can easily write the below as an answer. Since the past myself was concerned with keeping the dictionaries as is (I highly criticize and question...), a quick solution is as follows.

x = {}
for (i,j) in c_il.items():
    x[i,j] = m.addVar(vtype=GRB.BINARY, name="x%s"%str([i,j]))
for (i,j) in c_iu.items():
    x[i,j] = m.addVar(vtype=GRB.BINARY, name="x%s"%str([i,j]))

Alternatively,

x = {(i,j): m.addVar(vtype=GRB.BINARY, name="x%s"%str([i,j])) 
         for (i,j) in c_il.items()}
for (i,j) in c_iu.items():
    x[i,j] = m.addVar(vtype=GRB.BINARY, name="x%s"%str([i,j]))

One liner alternative:

x = {(i,j): m.addVar(vtype=GRB.BINARY, name="x%s"%str([i,j])) 
     for (i,j) in [(k,l) for (k,l) in c_il.items()] + [(k,l) for (k,l) in c_iu.items()]}

Upvotes: 1

Greg Glockner
Greg Glockner

Reputation: 5653

It looks like you want to create 10 variables that are indexed by something. The best way to do this is to create the two indexes as lists. If you want x[12], x[21], then write:

from gurobipy import *

m = Model()
il = [ 12, 21, 31, 44, 53, 64, 73, 82, 91, 104 ]
x = m.addVars(il, vtype=GRB.BINARY, name="x")

And if you want to write x[1,2], x[2,1], then write:

from gurobipy import *

m = Model()
il = [ (1,2), (2,1), (3,1), (4,4), (5,3), (6,4), (7,3), (8,2), (9,1), (10,4) ]
x = m.addVars(il, vtype=GRB.BINARY, name="x")

Upvotes: 1

Related Questions