transforming binary gurobi tupledict into np.array
AnsweredHello,
I formulated a MILP using the gurobi python interface.
In my model i created a tuple dict x of binary Variables with 2d-keys using model.addVars().
For further calculations, I need to transform the resulting tuple dict into a np.array, with x[i][j] beeing the entry in the i-th row and j-th column of the np.array/matrix.
What is the most efficient way to do that? Or should i already choose something different then model.addVars() to begin with?
I am new to Gurobi and I appreciate any help
Yours,
Lukas
-
Hi Lukas,
You could write the solution values from the tupledict into a numpy array like so:
arr = np.zeros((M, N))
for i in range(M):
for j in range(N):
arr[i,j] = x[i,j].XBut you may find it more convenient to use addMVar instead of addVars. If x is instead stored as an MVar, then retrieving the solution using x.X directly gives you the numpy array you are looking for.
Best regards,
Simon0
Please sign in to leave a comment.
Comments
1 comment