[英]How to print the output of pulp decision variables into matrix format
我想將模型的解導出為矩陣形式,因此將變量添加到 Soln but key error 。 請幫助解決這個問題。
或者請建議任何其他方式將解決方案導出為 m*n 的表格格式。
KeyError Traceback (most recent call last)
<ipython-input-52-c86dd2a00da5> in <module>()
82 for i in range (1,Box+1):
83 for j in range (1, Pallet+1):
---> 84 Soln[i][j]=x[i][j]
85
86
KeyError: 1
from pulp import *
import numpy as np
Box=6
Pallet=6
Variable_range=Box*Pallet
x = {}
from pulp import LpMaximize, LpProblem, LpStatus, lpSum, LpVariable
# Define the model
model = LpProblem(name="Container Loading", sense=LpMaximize)
# Define the decision variables
#Decision variables X
for i in range(1, Box+1):
for j in range (1,Pallet+1):
x[(i,j)] = pulp.LpVariable('x' + str(i) + '_' + str(j), 0, 1, LpBinary)
# Add constraints
#constraint for restricting unique number of boxes based on orientation
for i in range (1, (Box//2)+1):
for j in range (1,Pallet+1):
model += x[(i*2-1,j)] + x[(i*2,j)] <= 1
#print (model)
#Set the objective
model += lpSum(x.values())
# Solve the optimization problem
status = model.solve()
Soln= [[0]*Pallet]*Box
for i in range (1,Box+1):
for j in range (1, Pallet+1):
Soln[i][j]=x[i][j] # Error in this line
print(Soln)
Soln= np.zeros((Box,Pallet))
print (Soln[5][5])
for i in range (1,Box+1):
for j in range (1, Pallet+1):
value=x[(i,j)].value()
Soln[i-1][j-1]=value
這個修改給了我我期望的輸出
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.