Gurobi Optimization Result Writing into Csv file

80 views
Skip to first unread message

Taner Cokyasar

unread,
May 30, 2017, 12:41:08 AM5/30/17
to Gurobi Optimization
I am using Gurobi 7 to solve my MIP. I have several different variables. However, I am specifically interested in two of those, "x" and "y" namely. For the reference, I am giving my code that shows how I added x and y variables into the solver:

    # Creating Variables
    x = {}
    y = {}

    # Adding Variables
    for i in range(I):
        x[i+1,P[i]-d[0]] = m.addVar(vtype=GRB.BINARY, name="x%s" % str([i+1,P[i]-d[0]]))
        x[i+1,P[i]] = m.addVar(vtype=GRB.BINARY, name="x%s" % str([i+1,P[i]]))
    for i in range(I):
        for k in range(len(rangevalue)):
            y[i+1, rangevalue[k] - E[i]] = m.addVar(vtype=GRB.BINARY,
                                   name="y%s" % str([i+1, rangevalue[k] - E[i]]))

Even though the above code may not really make any sense, I just wanted to show it in case you may use it for my problem.

After I solve the problem, I get the following results:

`m.printAttr('X')`


        Variable            X 
    -------------------------
         x[1, 3]            1 
    sigmaminus[1]          874 
         x[2, 2]            1 
    sigmaminus[2]         1010 
         x[3, 2]            1 
    sigmaminus[3]         1945 
         x[4, 4]            1 
    sigmaplus[4]           75 
         x[5, 4]            1 
    sigmaminus[5]         1153 
         x[6, 5]            1 
    sigmaminus[6]          280 
         x[7, 3]            1 
    sigmaplus[7]         1138 
         x[8, 2]            1 
    sigmaplus[8]          538 
         x[9, 1]            1 
    sigmaplus[9]         2432 
        x[10, 5]            1 
    sigmaminus[10]          480 
        omega[1]           12 
        OMEGA[1]           12 
        omega[2]            9 
        OMEGA[2]           12 
        omega[3]            8 
        OMEGA[3]            9 
        omega[4]            8 
        OMEGA[4]            8 
        OMEGA[5]            8 
         y[1, 2]            1 
         y[2, 9]            1 
         y[3, 5]            1 
         y[4, 6]            1 
         y[5, 4]            1 
         y[6, 6]            1 
         y[7, 3]            1 
        y[8, 11]            1 
         y[9, 8]            1 
        y[10, 1]            1 
      phiplus[6]            1 
     phiminus[7]            1 
    phiminus[10]            1 

I specifically want to display x and y variables with their indexes. Other variables are not necessary. My question is how can I write these results into an csv file on one column as following?

    x[1,3]
    x[2,2]
    x[3,2]
    .
    .
    .
    x[10,5]
    y[1,2]
    y[2,9]
    y[3,5]
    .
    .
    .
    y[10,1]

I do not need their corresponding value which can only be "1" since they are binary variables. I just need to write the variables which have the value "1".

Thanks,
Taner

Taner Cokyasar

unread,
May 30, 2017, 3:49:24 PM5/30/17
to Gurobi Optimization
I got my answer. For future reference, my answer is:

var_x = []
var_y = []

for var in m.getVars():
    if 'x' == str(var.VarName[0]) and var.X > 0.1:
        var_x.append(var.VarName)
    if 'y' == str(var.VarName[0]) and var.X > 0.1:
        var_y.append(var.VarName)

# Write results to csv
with open(Results, 'wb') as myfile:
    wr = csv.writer(myfile, quoting=csv.QUOTE_ALL)
    wr.writerows(zip(var_x, var_y))
Reply all
Reply to author
Forward
0 new messages