6

I want to know that when I defined a multi-dimension variables in Gurobi, how can I extract all the value of the solution and organize them in to a Numpy array according to the original coordinate of the variable.

I have the following decision variables defined in Gurobi using Python API:

for i in range(N):
  for t in range(M):
      Station_Size[i,t] = m.addVar(ub=Q, name = 'Station_Size_%s_%s' %(i,t))
      for j in range(N):
         Admission[i,j,t] = m.addVar(ub = Arrival_Rate[t,i,j], obj=-1, name = 'Admission_Rate_%s_%s_%s' %(i,j,t))
         Return[i,j,t] = m.addVar(name = 'Return_Rate_%s_%s_%s' %(i,j,t))

I have the problem solved and I have three dictionary:

Station_Size, Admission and Return

I know that the solution can be accessed as:

Station_Size[i,t].X, Admission[i,j,t].X and Return[i,j,t].X

I want to creat three Numpy array such that:

Array_Station_Size[i,t] = Station_Size[i,t].X
Array_Admission[i,j,t] = Admission[i,j,t].X

I can definitely do this by creating three loops and creat the Numpy Array element by element. It's do-able if the loop doesn't take a lot of time. But I just want to know if there is a better way to do this. Please comment if I did not make myself clear.

Loading Zone
  • 177
  • 2
  • 12

2 Answers2

2

I figured this problem out.

Do the following:

Array_Station_Size = np.array()
Array_Station_Size[i,] = [Station_Size[i,t].X for t in rang(T)]
Loading Zone
  • 177
  • 2
  • 12
2

Assuming your model's name is m, do the following:

Array_Station_Size = m.getAttr('x', Station_Size)

which is a gurobipy.tupledict now. see gurobi doc here http://www.gurobi.com/documentation/8.1/quickstart_windows/py_results.html

Mou Cai
  • 96
  • 1
  • 3