transforming binary gurobi tupledict into np.array
回答済みHello,
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
-
正式なコメント
This post is more than three years old. Some information may not be up to date. For current information, please check the Gurobi Documentation or Knowledge Base. If you need more help, please create a new post in the community forum, or try Gurobot, our chatbot interface offering instant, expert-level support. -
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
投稿コメントは受け付けていません。
コメント
2件のコメント