Professional Documents
Culture Documents
Op Tim Ization
Op Tim Ization
Op Tim Ization
Question 1:
Solve the following linear programming problem using the simplex
method.
Maximize Z = 3x 1 + 2x 2 + 4x 3
Subjected to:
x 1 + x 2 + 2x 3 ≤ 4,
2x 1 + 3x 3 ≤ 5,
2x 1 + x 2 + 3x 3 ≤ 7,
x1 , x2 , x3 ≥ 0
Ans 1. (i)
In [47]:
Ans 1. (ii)
file:///C:/Users/madha/Desktop/WRS-Tutorial-3.html 1/7
9/16/2019 Tutorial-3
In [1]:
import numpy as np
print("Enter the number of variables in Objective functio
n")
a=int(input())
print("Enter the number of constraint equations (excludin
g x>0 conditions)")
b=int(input())
ar=np.zeros((b+1,a+b+3)) # initializing simplex ma
trix with 0
for i in range(b+1):
ar[i][a+i]=1
for i in range(b+1): # Entering the coefficients in simp
lex matrix
if(i<b):
print("For Constraint Equation:",i+1)
else:
print("For Objective Function")
for j in range(a+b+2):
if(i<b):
if(j<a):
print("enter the coefficient of x",j+1)
ar[i][j]=float(input())
elif(j==a+b+1):
print("enter the coefficient of b",i+1)
ar[i][j]=float(input())
else:
if(j<a):
print("enter the coefficient of x",j+1)
ar[i][j]=float(input())*(-1)
file:///C:/Users/madha/Desktop/WRS-Tutorial-3.html 2/7
9/16/2019 Tutorial-3
file:///C:/Users/madha/Desktop/WRS-Tutorial-3.html 3/7
9/16/2019 Tutorial-3
In [2]:
d=2
arr=ar.copy()
ind_ent=[] # defining a matrix which
will store the values of x which will be entering
for i in range(a+b): # first entering variabl
es are slack variables, so initializing with them
if(i>=a):
ind_ent.append(i)
print("Table No. 1")
print('\n'.join(['\t'.join([str(cell) for cell in row]) f
or row in arr]))
min_f=np.min(arr[-1]) # storing the minimum
value of the last row
# print(min_f)
while(min_f<0):
min_ind =np.argmin(arr[-1]) # storing the index of
element having minimum value in the last row
# print("index of last row",min_ind)
for i in range(b+1):
if(arr[i][min_ind]>0):
arr[i][-1]=float(arr[i][-2]/arr[i][min_ind])
# calculating the value of b/a
# print(i,"-1",arr[i][-1])
else:
arr[i][-1]=np.inf
# if a<0 then setting it as infinity
# print(i,"-1",arr[i][-1])
min_ind_b=np.argmin(arr[:,-1])
# storing the index of element having minimum value in la
st column
# print("index of last coulom",min_ind_b)
pivot=arr[min_ind_b][min_ind]
# pivot element
# print(pivot)
for j in range(a+b+2):
arr[min_ind_b][j]=arr[min_ind_b][j]/pivot
# print(min_ind_b,j,arr[min_ind_b][j])
for i in range(b+1):
if(i!=min_ind_b):
file:///C:/Users/madha/Desktop/WRS-Tutorial-3.html 4/7
9/16/2019 Tutorial-3
for j in range(a+b+2):
if(j!=min_ind):
arr[i][j]=arr[i][j]-(arr[min_ind_b][j
]*arr[i][min_ind])
# print(i,j,arr[i][j])
for i in range(b+1):
if(i!=min_ind_b):
arr[i][min_ind]=0
for i in range(len(ind_ent)):
if(i==min_ind_b):
# print("before",i)
ind_ent[i]=min_ind
# print("after",i)
print("\nTable No.", d,"\n")
d=d+1
print('\n'.join(['\t'.join([str(round(cell,2)) for ce
ll in row]) for row in arr]))
min_f=np.min(arr[-1])
# print("\nEnter '1' for minimum value of objective funct
ion and '2' for maximum value of ojective function")
# c=int(input())
# if(c==1):
# print("mimimum value is",-1*arr[-1][-2])
# else:
print("\nMaximum value is",arr[-1][-2])
arr_=[0 for i in range(a+b)]
for i in range(len(ind_ent)):
arr_[ind_ent[i]]=round(arr[i][-2],2)
print("X: [" , " , ".join([str(cell) for cell in arr_]),
"]",sep="")
file:///C:/Users/madha/Desktop/WRS-Tutorial-3.html 5/7
9/16/2019 Tutorial-3
Table No. 1
1.0 1.0 2.0 1.0 0.0 0.0
0.0 4.0 0.0
2.0 0.0 3.0 0.0 1.0 0.0
0.0 5.0 0.0
2.0 1.0 3.0 0.0 0.0 1.0
0.0 7.0 0.0
-3.0 -2.0 -4.0 0.0 0.0 0.0
1.0 0.0 0.0
Table No. 2
Table No. 3
Table No. 4
file:///C:/Users/madha/Desktop/WRS-Tutorial-3.html 6/7
9/16/2019 Tutorial-3
file:///C:/Users/madha/Desktop/WRS-Tutorial-3.html 7/7