Op Tim Ization

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

9/16/2019 Tutorial-3

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

i) Solve using scipy (command) optimisation in python.


ii) Develop program to solve using simplex method (using simplex
tables).

Ans 1. (i)
In [47]:

from scipy.optimize import linprog as lp


opt=lp([-3,-2,-4,0,0,0],A_eq=[[1,1,2,1,0,0],[2,0,3,0,1,0
],[2,1,3,0,0,1]],b_eq=[4,5,7],method='simplex')
print("Maximum value of objective function",round(opt['fu
n']*-1,2),"\nX:",opt['x'])

Maximum value of objective function 10.5


X: [2.5 1.5 0. 0. 0. 0.5]

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

Enter the number of variables in Objective f


unction
3
Enter the number of constraint equations (ex
cluding x>0 conditions)
3
For Constraint Equation: 1
enter the coefficient of x 1
1
enter the coefficient of x 2
1
enter the coefficient of x 3
2
enter the coefficient of b 1
4
For Constraint Equation: 2
enter the coefficient of x 1
2
enter the coefficient of x 2
0
enter the coefficient of x 3
3
enter the coefficient of b 2
5
For Constraint Equation: 3
enter the coefficient of x 1
2
enter the coefficient of x 2
1
enter the coefficient of x 3
3
enter the coefficient of b 3
7
For Objective Function
enter the coefficient of x 1
3
enter the coefficient of x 2
2
enter the coefficient of x 3
4

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

-0.33 1.0 0.0 1.0 -0.67 0.0


0.0 0.67 2.0
0.67 0.0 1.0 0.0 0.33 0.0
0.0 1.67 1.67
0.0 1.0 0.0 0.0 -1.0 1.0
0.0 2.0 2.33
-0.33 -2.0 0.0 0.0 1.33 0.0
1.0 6.67 inf

Table No. 3

-0.33 1.0 0.0 1.0 -0.67 0.0


0.0 0.67 0.67
0.67 0.0 1.0 0.0 0.33 0.0
0.0 1.67 inf
0.33 0.0 0.0 -1.0 -0.33 1.0
0.0 1.33 2.0
-1.0 0.0 0.0 2.0 0.0 0.0
1.0 8.0 inf

Table No. 4

0.0 1.0 0.5 1.0 -0.5 0.0


0.0 1.5 inf
1.0 0.0 1.5 0.0 0.5 0.0
0.0 2.5 2.5
0.0 0.0 -0.5 -1.0 -0.5 1.0
0.0 0.5 4.0
0.0 0.0 1.5 2.0 0.5 0.0

file:///C:/Users/madha/Desktop/WRS-Tutorial-3.html 6/7
9/16/2019 Tutorial-3

1.0 10.5 inf

Maximum value is 10.5


X: [2.5 , 1.5 , 0 , 0 , 0 , 0.5]

file:///C:/Users/madha/Desktop/WRS-Tutorial-3.html 7/7

You might also like