PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, August 18, 2022

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

 August 18, 2022     arrays, output, printing, pulp, python     No comments   

Issue

I want to export the solution of the model into a matrix form hence adding the variables into 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.

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)

Solution

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



Answered By - user11150618
Answer Checked By - Timothy Miller (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing