简体   繁体   English

如何将纸浆决策变量的输出打印成矩阵格式

[英]How to print the output of pulp decision variables into matrix format

I want to export the solution of the model into a matrix form hence adding the variables into Soln but key error .我想将模型的解导出为矩阵形式,因此将变量添加到 Soln but key error 。 please help to solve this.请帮助解决这个问题。

or please suggest any other way to export the solution to a table format of m*n.或者请建议任何其他方式将解决方案导出为 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

this modification gave me the output i expected这个修改给了我我期望的输出

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM