# Chapter-15 : Constrained Optimization¶

## Ex:15.1 Pg: 388¶

In [3]:
regular=[7, 10, 9 ,150]#
premium=[11, 8, 6, 175]#
res_avail=[77, 80]#
#total profit(to be maximized)=z=150*x1+175*x2
#total gas used=7*x1+11*x2 (has to be less than 77 m**3/week)
#similarly other constraints are developed
print "Maximize z=150*x1+175*x2"
print "subject to"
print "7*x1+11*x2<=77 (Material constraint)"
print "10*x1+8*x2<=80 (Time constraint)"
print "x1<=9 (Regular storage constraint)"
print "x2<=6 (Premium storage constraint)"
print "x1,x2>=0 (Positivity constraint)"

Maximize z=150*x1+175*x2
subject to
7*x1+11*x2<=77 (Material constraint)
10*x1+8*x2<=80 (Time constraint)
x1<=9 (Regular storage constraint)
x2<=6 (Premium storage constraint)
x1,x2>=0 (Positivity constraint)


## Ex:15.2 Pg: 389¶

In [6]:
%matplotlib inline
from matplotlib.pyplot import plot,xlabel,ylabel,title ,show
x21=[];x22=[];x23=[];x24=[];x25=[];x26=[]

for x1 in range(0,9):
x21.append(-(7/11)*x1+7)
x22.append((80-10*x1)/8)
x23.append(6)
x24.append(-150*x1/175)
x25.append((600-150*x1)/175)
x26.append((1400-150*x1)/175)

x1=range(0,9)

plot(x1,x24)
plot(x1,x25)
plot(x1,x26)

plot(x1,x21)#
plot(x1,x22)#
plot(x1,x23)#
title('x2 vs x1')
xlabel('x1 (tonnes)')
ylabel('x2 (tonnes)')
show()


## Ex:15.3 Pg: 399¶

In [10]:
x1=[0,4.888889, 3.888889]
x2=[0,7, 11]#
x3=[0,10, 8]#
x4=[0,150, 175]#
x5=[0,77, 80, 9, 6]
profit=[0,x1[(1)]*x4[(1)], x1[(2)]*x4[(2)]]#
total=[0,x1[(1)]*x3[(1)]+x1[(2)]*x3[(2)], x1[(1)]*x3[(1)]+x1[(2)]*x3[(2)], x1[(1)], x1[(2)], profit[(1)]+profit[(2)]]
e=1000#

while e>total[(5)]:
if total[(1)]<=x5[(1)]:
if total[(2)]<=x5[(2)]:
if total[(3)]<=x5[(3)]:
if total[(4)]<=x5[(4)]:
l=1#

if l==1:
x1[(1)]=x1[(1)]+4.888889
x1[(2)]=x1[(2)]+3.888889#
profit=[0,x1[(1)]*x4[(1)], x1[(2)]*x4[(2)]]
total[(5)]=profit[(1)]+profit[(2)]

print "The maximized profit is=",total[(5)]

The maximized profit is= 1413.888925


## Ex:15.4 Pg: 401¶

In [12]:
from math import pi,exp

Mt=2000##kg
g=9.8##m/s**2
c0=200##$c1=56##$/m
c2=0.1##$/m**2 vc=20##m/s kc=3##kg/(s*m**2) z0=500##m t=27# r=2.943652# n=6# A=2*pi*r*r# l=(2**0.5)*r# c=3*A# m=Mt/n# def f(t): y=(z0+g*m*m/(c*c)*(1-exp(-c*t/m)))*c/(g*m)# return y while abs(f(t)-t)>0.00001: t=t+0.00001 v=g*m*(1-exp(-c*t/m))/c# print "The final value of velocity=%0.3f"%v print n,"The final no. of load parcels=",n print "The chute radius=%0.3f m"%r print "The minimum cost = %0.3f$"%((c0+c1*l+c2*A*A)*n)

The final value of velocity=19.980
6 The final no. of load parcels= 6
The chute radius=2.944 m
The minimum cost = 4377.264 \$


## Ex:15.5 Pg: 406¶

In [17]:
from scipy.optimize import fmin
from math import sin
def fx(x):
y=-(2*sin(x))+x**2/10
return y
x=fmin(fx,0)
print "After maximization:"
print "x=",x
print "f(x)=",fx(x)

Optimization terminated successfully.
Current function value: -1.775726
Iterations: 26
Function evaluations: 52
After maximization:
x= [ 1.4275625]
f(x)= [-1.77572565]


## Ex:15.6 Pg: 407¶

In [22]:
from scipy.optimize import fmin
def fx(x):
f=-(2*x[0]*x[1]+2*x[0]-x[0]**2-2*x[1]**2)
return f
x=fmin(fx,[-1, 1])
print "After maximization:"
print "x=",x
print "f(x)=",fx(x)

Optimization terminated successfully.
Current function value: -2.000000
Iterations: 45
Function evaluations: 86
After maximization:
x= [ 1.99993372  0.99996476]
f(x)= -1.99999999779


## Ex:15.7 Pg: 408¶

In [23]:
from scipy.optimize import fmin
from math import sin

def fx(x):
y=-(2*sin(x)-x**2/10)
return y
x=fmin(fx,0)
print "After maximization:"
print "x=",x
print "f(x)=",fx(x)

Optimization terminated successfully.
Current function value: -1.775726
Iterations: 26
Function evaluations: 52
After maximization:
x= [ 1.4275625]
f(x)= [-1.77572565]