We are making a program to do a simplex method of maximization, and we have a list of lists called tabList. When we set a variable small = min(tabList[-1]) which should find the smallest element of the last list in tab list. However it says that it is NoneType so not subscriptable. We have checked the type and it is type list.
Here is the part of the code with the error:
def solveTab(tabList):
print(tabList)
print(type(tabList[-1]))
flag = True
while (flag):
small = min(tabList[-1])
if (small < 0):
locCol = locColSmall(tabList, small)
locRow = locRowSmall(tabList, locCol)
tabList = simpTab(tabList, locCol, locRow)
else:
flag = False
print(tabList)
solveTab(maxTab(zList, constraintList, numConstraints, numVariables))
Here is the python message:
Enter 1 for maximization, 0 for minimization: 1
Enter number of variables: 3
Enter number of constraints: 3
z = ax1 + bx2 + cx3 ...
Enter coefficients of z expression like a b c etc: 3 2 1
starting input for constraint 1
ax1 + bx2 + cx3 ... = b
Enter coefficients for constraint like a b c etc followed by b: 2 1 1 150
starting input for constraint 2
ax1 + bx2 + cx3 ... = b
Enter coefficients for constraint like a b c etc followed by b: 2 2 8 200
starting input for constraint 3
ax1 + bx2 + cx3 ... = b
Enter coefficients for constraint like a b c etc followed by b: 2 3 1 320
[[2, 1, 1, 1, 0, 0, 0, 150], [2, 2, 8, 0, 1, 0, 0, 200], [2, 3, 1, 0, 0, 1, 0, 320], [-3, -2, -1, 0, 0, 0, 1, 0]]
<class 'list'>
Traceback (most recent call last):
File "C:/Users/real0/Desktop/school/Spring 2022/Math Modeling/final project.py", line 104, in <module>
solveTab(maxTab(zList, constraintList, numConstraints, numVariables))
File "C:/Users/real0/Desktop/school/Spring 2022/Math Modeling/final project.py", line 95, in solveTab
small = min(tabList[-1])
TypeError: 'NoneType' object is not subscriptable
>>>
Here is the entire program if it helps:
minMax = int(input("Enter 1 for maximization, 0 for minimization: "))
numVariables = int(input("Enter number of variables: "))
numConstraints = int(input("Enter number of constraints: "))
#Get z list elements
print("z = ax1 + bx2 + cx3 ...")
x = input("Enter coefficients of z expression like a b c etc: ")
zList = [int(i) for i in x.split()]
# Check if numVariables == number of variables input
if len(zList) != numVariables:
print("Number of variables and number of variables input do not match")
constraintList = []
for i in range(numConstraints):
i += 1
print("starting input for constraint ",i)
t = 0
tmpList = []
print("ax1 + bx2 + cx3 ... = b")
x = input("Enter coefficients for constraint like a b c etc followed by b: ")
l = [int(i) for i in x.split()]
constraintList.append(l)
#print(zList)
#print(constraintList)
def maxTab(zList, constraintList, numConstraints, numVariables):
tabList = []
tmpList = []
for i in range(numConstraints):
tmpList = []
for t in range(numVariables):
tmpList.append(constraintList[i][t])
slackList =[]
for t in range(numConstraints):
if i == t:
slackList.append(1)
else:
slackList.append(0)
tmpList += slackList
tmpList.append(0)
tmpList.append(constraintList[i][-1])
tabList.append(tmpList)
tmpList = []
for i in range(numVariables):
tmpList.append(zList[i] * -1)
for i in range(numConstraints):
tmpList.append(0)
tmpList.append(1)
tmpList.append(0)
tabList.append(tmpList)
#print(tabList)
return(tabList)
def locColSmall(tabList, small):
for lcv in range(numVariables + numConstraints + 2):
if (tabList[-1][lcv] == small):
return lcv
def locRowSmall(tabList, locCol):
minRow = 0
for lcv in range(numConstraints):
if (tabList[lcv][-1]/tabList[lcv][locCol] < tabList[minRow][-1]/tabList[lcv][locCol]):
minRow = lcv
return minRow
def simpTab(tabList, locCol, locRow):
val = tabList[locRow][locCol]
tabList[locRow] = [lcv/val for lcv in tabList[locRow]]
for lcv in range(len(tabList)):
if (lcv != locRow):
val = tabList[lcv][locCol]
for lcv2 in range(len(tabList[lcv])):
tabList[lcv][lcv2] -= tabList[locCol][lcv]*val
def solveTab(tabList):
print(tabList)
print(type(tabList[-1]))
flag = True
while (flag):
small = min(tabList[-1])
if (small < 0):
locCol = locColSmall(tabList, small)
locRow = locRowSmall(tabList, locCol)
tabList = simpTab(tabList, locCol, locRow)
else:
flag = False
print(tabList)
solveTab(maxTab(zList, constraintList, numConstraints, numVariables))
The function "simpTab" is made to automatically update "tabList" so no need to assign tabList to the result of simpTab! (Unless you don't directly modify tabList and want to return a value from simpTab [Which is not in your case])
So simply change:
tabList = simpTab(tabList, locCol, locRow)
to
simpTab(tabList, locCol, locRow)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.