Optimisation Problems Anuradha Singhania Manas Ranjan Das FOSSEE IIT BOMBAY December 14, 2011
1. Linear Linear Programming Programming Maximize 3x1 + x2 + 3x3 f or 2x1 + x2 + x3 2 x1 + 2x2 + 3x3 5 2x1 + 2x2 + x3 6 x1, x2, x3 0
≥
≤ ≤ ≤
SOLUTION
1
//
This
is
a
linear
programming
c on st ra in ts . This inbuilt
function . It
scilab
using
Quapro
toolbox
can
programm can
linpro
i nst a l l ed
with
uses also
under
karmarkar be
function
linear
solved
( linpro
Optimization
by
com man d
the
matrix
in
, an in
is
in
A TO TO MS MS , i t
ato msInstal l ( quapro ) )
2 3
mode ( 0 ) ;
4
A = [
5 6 7
2 1 1 1 2 3 2 2 1 ];
//
of
linear
hand
side
part
of
inequality
c o n st r a i nt s . 8
b = [2;5;6];
//
i ne qu al it y 9
c = [3;1;3];
the
right
−
of
linear
c on st ra in ts .
//
the
linear
the
objective
f u nc t i on . 10 11
lb = [ 0 ; 0 ; 0 ] ; / / t h e l o w e r b o u n d s [ x op o p t , f o p t , e x i t f l a g , i t e r , y o p t ] =karmarkar ( [ ] , [ ] , c , [ ] , [ ] , [ ] , [ ] , [ ] , A, b , lb ) ; / / S o l v e s a l i n e a r o p ti m iz a ti o n
12 13 14 15
problem
disp ( xopt (1 ) , ”x1= ”x1=” ) disp ( xopt (2 ) , ”x2= ”x2=” ) disp ( xopt (3 ) , ”x3= ”x3=” ) disp ( f o p t , ” t h e o b j e c t i v e f u n c t i o n v a l u e a t o p ti mu m
1
16 17
18 19 20
”) disp ( i t e r , ” t h e n u m b er o f i t e r a t i o n s =” =” ) disp ( y op o p t , ” a s t r u c t c o n t a i ni n i n g t he h e d ua ua l s o l u t i o n . The s t r uc u c t ur u r e y o p t h a s f ou o u r f i e l d s : i n eq eq l i n , e q l i n , u pp p p eerr , l o w e r . ” ) disp ( y o p t . i n e q l i n , ” y o p t . i n e q l i n =” =” ) disp ( yopt . lower , ”yopt . low er=” er=” ) browsevar () / / / / b r o w s e v a r c a n s h o w a l l v a r i a b l e s browsevar type
of
can
be
costumized
variables .
v ar ia bl e
It ’ s
to
also
show
all
possible
or
to
.
some
exclude
names .
2. Quadratic Quadratic Programming: Programming: Minimize −2 2 0 f (x) = 12 xT x+ −2 0 2
T
x
f or x1 + x2 2+ 2 x1 + x2 2
≥
−
√ ≥−
SOLUTION
1
//
This
is
a
linear
c o n s t r a i n t s . This inbuilt
programm
function . It
scilab
using
Quapro
toolbox
can
quadrtatic
i nst a l l ed
can
quapro
uses
also
function
under by
programm qp
be
solve
solved
( quapro
Optimization
com man d
with
in
is
linear , an
in in
A TO TO MS MS , i t
ato msInstal l ( quapro ) )
2 3
mode ( 0 )
4
Q = [2 0;0 2 ] ; // matrix
5 6 7 8 9
real
( dimension
positive n
x
de fi ni te
symmetric
n) .
p =[ −2; −2]; / / r e a l ( c o l u m n ) v e c t o r ( d i m e n s i o n n ) C1=[1 1=[1 1; − 1 1 ] ; b1=[(2+2ˆ1/2) ; − 2 ] ; column v e c t o r of lower bounds XL=[0;0]; // XU=[1 e10 ; 1 e10 ] ; / / c o l u m n v e c t o r o f u p p e r b o u n d s −
−
2
10 11
me=2; / / n u m b e r o f e q u a l i t y c o n s t r a i n t s [ x op o p t , i a c t , i t e r , f o p t ] = q p s o l v e ( Q, Q, p , C 1 , b 1 , me me ) qp
solve
is
a
linear
quadratic
//
programming
solver 12 13 14
15 16
17
18
disp ( xopt (1 ) , ”x1= ”x1=” ) disp ( xopt (2 ) , ”x2= ”x2=” ) disp ( f o p t , ” t h e o b j e c t i v e f u n c t i o n v a l u e a t o p ti mu m
=” ) disp ( i t e r ( 1 ) , ” Th The n u m b e r o f m a i n i t e r a t i o n s =” =” ) disp ( i t e r ( 2 ) , ” ”H How ma m any c o n s t r a i n t s w eerr e d e l e t e d a f t e r t he h e y b e c a m e a c t i v e =” ) disp ( i a c t , ” v ec e c to to r , i n d i c a t o r o f a c t i v e c o n s t r a i n t s . The f i r s t n o n z e ro r o e n t r i e s g i v e t he h e i nd n d ex ex o f t he he a c t i v e c o n s t r a i n t s ” ) browsevar () / / b r o w s e v a r c a n s h o w a l l v a r i a b l e s . browsevar type
of
can
be
costumized
variables .
v ar ia bl e
It ’ s
also
to
show
all
possible
to
or
some
exclude
names .
3. Non-Linear Non-Linear Programming Programming:: Minimize (x1 − 2)2 + (x2 − 1)2 f or g 1 : x2 + x21 2 x1 + x2 x2 0
−
≥
≤
≤0
Find Contour Contour Surface Plot Plot and Unconstrained Unconstrained Minima Minima of the following problems
4. f (x) = x21 + x22 + x1 x2 SOLUTION 1
// This
programm
minimimum f mi ns ea rc h
of
computes given
the
u nc on st ra in ed
function
f un ct io n .
3
using
an
inbuilt
2 3
mode ( 0 ) function y=f (x )
y=x(1)ˆ2 +x(2)ˆ2+x(1) ∗ x ( 2 ) ;
4 5
endfunction
6 7
o pt p t = o pt p t i ms m s e t ( ” To To lX ” , 1 . e −2 ) ; f un c t i o nm a n a g e s
structure , f ie ld s
which
( for
MaxIter ” ,
the is
” o pt ion s ”
a
example ,
struct
//
O p t im s e t
data
with
a
” MaxFunEvals ” ,
set
o f
”
etc . . . )
8 9
[ x , f v a l , e x i t f l a g , o ut u t pu p u t ] = f m in i n s ea e a r ch ch ( f , [ − 1 .2 .2 1 ] ) ; / / c o m p u t e s t h e u n c o n s t r a i n e d minimimum
−
Mea d
of
given
function
with
the
Nelder
algorithm .
10 11 12 13
disp ( x , ”The minimu minimum m=” ) disp ( fv al , ”T ”Th he minimum fu nc ti on val ue=” ue=” )
exitflag
/ / The
status
of
the
fminsearch values 14
output
of
//A
flag
associated
a l g o r i t h m . The
will
give
with help
detai ls
of
exist f i l e
for
di f f er en t
ex i t f l a g .
struct
information
which
about
stores
the
exit
detailed of
the
algorithm
. 15
browsevar ()
16
//
∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗
plot 17 18 19 20
Level
curves
of
a
surface
on
a
2D
∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗
d e f f ( ’ [ w]= f ( x1 , x2 ) ’ , ’w= ’w=x1ˆ2+x2ˆ2+x1 x1ˆ2+x2ˆ2+x1 x2 ’ ) x 1 = [ 0 : 0 . 2 5 : 6 ] ; x 2 = [ 0 : 0 . 2 5 : 6 ] ; z=f e v a l ( x1 , x2 , f ) ; contour2d ( x1 , x2 , z ,1 0 ) x t i t l e ( ’ Contour pl ot of w=x1ˆ2 x1ˆ2+x2ˆ2 +x2ˆ2+x1 x2 ’ )
∗
∗
5. (x2 − x1 )2 + 8x1 x2 − x1 + x2 + 3 SOLUTION
1
// This
programm
minimimum
of
computes given
the
u nc on st ra in ed
function
4
using
an
inbuilt
f mi ns ea rc h 2 3
mode ( 0 ) function y=f (x )
y = − (((x( 2) ) −(x( 1) ) ) ˆ2+8∗ ( x ( 1 ) ) ∗ (x (2 ) )+(x )+(x (1 ) ) ∗ (x (2 ) ) ) ;
4
5 6
f un ct io n .
endfunction
o pt p t = o pt p t i ms m s e t ( ” To To lX ” , 1 . e −2 ) ; f u n c t i on m a na ge s
structure , f ie ld s
7
−
9 10
Mea d
is
a
example ,
struct
data
with
a
” MaxFunEvals ” ,
set
o f
”
etc . . . )
of
given
function
with
the
Nelder
algorithm .
disp ( x , ”The minim minimum um= =” ) disp ( fv al , ”T ”Th he minimum fu nc ti on val ue=” ue=” )
exitflag status
/ / The of
values
output
of
//A
flag
the
fminsearch
11
” o p t io ns ”
Optimset
[ x , f v a l , e x i t f l a g , o ut u t pu p u t ] = f m in i n s ea e a r ch ch ( f , [ − 1 .2 .2 1 ] ) ; / / c o m p u t e s t h e u n c o n s t r a i n e d minimimum
8
which
( for
MaxIter ” ,
the
//
associated
a l g o r i t h m . The
will
give
with help
detai ls
of
exist f i l e
for
di f f er en t
ex i t f l a g .
struct
information
which
about
stores
the
exit
detailed of
the
algorithm
. 12
browsevar ()
13
//
∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗
plot 14
15 16 17
Level
curves
of
a
surface
on
a
2D
∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗
d e f f ( ’ [ w]= f (x1 , x2) ’ , ’w=(x ’w=(x22 x1)ˆ2+8 x1 x2 x1+x2
−
∗ ∗ −
+3 ’ ) x 1 = [ 0 : 0 . 2 5 : 6 ] ; x 2 = [ 0 : 0 . 2 5 : 6 ] ; z=f e v a l ( x1 , x2 , f ) ; contour2d ( x1 , x2 , z ,1 0 ) x t i t l e ( ’ C on on to to ur u r p l o t o f w=( x2 x2−x1)ˆ2+8∗ x1 ∗ x2 −x1+ x2+3 x2+3 ’ , ’ x1 ’ , ’ x2 ’ )
6. (x21 − 1.5x1 x2 + 2x22)x21 SOLUTION
5
1
// This
programm
minimimum
of
f mi ns ea rc h 2 3
given
the
u nc on st ra in ed
function
using
an
inbuilt
f un ct io n .
mode ( 0 ) function y=f (x )
y = ;
4 5 6
computes
−( (x (x ( 1 ) ) ˆ 2 − 1 . 5 ∗ ( x ( 1 ) ) ∗ (x (2 ) ) )
endfunction
7 8
o pt p t = o pt p t i ms m s e t ( ” To To lX ” , 1 . e −2 ) ; f u n c t i on m a na ge s
structure , f ie ld s
9
−
11 12
Mea d
is
a
example ,
struct
data
with
a
set
” MaxFunEvals ” ,
o f
”
etc . . . )
of
given
function
with
the
Nelder
algorithm .
disp ( x , ”The ”The minimu minimum m=” ) disp ( fv al , ”T ”Th he minimum fu nc ti on val ue=” ue=” )
exitflag status
/ / The of
values
output
of
//A
flag
the
fminsearch
13
” o p t io ns ”
Optimset
[ x , f v a l , e x i t f l a g , o ut u t pu p u t ] = f m in i n s ea e a r ch ch ( f , [ − 1 .2 .2 1 ] ) ; / / c o m p u t e s t h e u n c o n s t r a i n e d minimimum
10
which
( for
MaxIter ” ,
the
//
associated
a l g o r i t h m . The
will
give
with help
detai ls
of
exist f i l e
for
di f f er en t
ex i t f l a g .
struct
information
which
about
stores
the
exit
detailed of
the
algorithm
. 14
browsevar ()
15
//
∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗
plot
Level
curves
of
a
surface
on
a
2D
∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗
16 17
d e f f ( ’ [w]= [w]= f ( x1 , x2 ) ’ , ’w=((( x1 ) ˆ2 ) ( x 1 ) ˆ 2
∗
x2 ) ∗ ( x 2 ) ∗ ( x 1 ) ˆ 2 + 2 ∗ (x2) ˆ2) ∗ (x1 ) ˆ2 ’ ) i nl in e
18
// an
f u n c ti on
x 1 = [ 0 : 0 . 2 5 : 6 ] ; x 2 = [ 0 : 0 . 2 5 : 6 ] ; z=f e v a l ( x1 , x2 , f ) ; m ul ti pl e
e va lu at io n
19 20
−1 .5∗(
contour2d ( x1 , x2 , z ,1 0 )
6
//
x t i t l e ( ’ C on on to to ur u r p l o t o f w= ( (( (( x 1 ) ˆ 2 )
21
) +2 ∗ (x2 ) ˆ2) (x1 ) ˆ2 ’ , ’x1 ’ , ’ x2 ’ )
− 1 . 5 ∗ ( x 2 ) ∗ ( x2
GRAPHICAL COMPUTATION OF Q6
1
//
This
is
a
linear
programming
c on st ra in ts . This inbuilt
function . It
scilab
using
Quapro
toolbox
can
programm can
linpro
i nst a l l ed
with
uses also
under
karmarkar be
function
linear
solved
( linpro
Optimization
by
com man d
the
matrix
in
, an in
is
in
A TO TO MS MS , i t
ato msInstal l ( quapro ) )
2 3
mode ( 0 ) ;
4
A = [
5 6 7
2 1 1 1 2 3 2 2 1 ];
//
of
linear
hand
side
part
of
inequality
c o n st r a i nt s . 8
b = [2;5;6];
//
i ne qu al it y 9
c = [3;1;3];
the
right
−
of
linear
c on st ra in ts .
//
the
linear
the
objective
f u nc t i on . 10 11
lb = [ 0 ; 0 ; 0 ] ; / / t h e l o w e r b o u n d s [ x op o p t , f o p t , e x i t f l a g , i t e r , y o p t ] =karmarkar ( [ ] , [ ] , c , [ ] , [ ] , [ ] , [ ] , [ ] , A, b , lb ) ; / / S o l v e s a l i n e a r o p ti m iz a ti o n
12 13 14 15
16 17
18 19
problem
disp ( xopt (1 ) , ”x1= ”x1=” ) disp ( xopt (2 ) , ”x2= ”x2=” ) disp ( xopt (3 ) , ”x3= ”x3=” ) disp ( f o p t , ” t h e o b j e c t i v e f u n c t i o n v a l u e a t o p ti mu m
”) disp ( i t e r , ” t h e n u m b er o f i t e r a t i o n s =” =” ) disp ( y op o p t , ” a s t r u c t c o n t a i ni n i n g t he h e d ua ua l s o l u t i o n . The s t r uc u c t ur u r e y o p t h a s f ou o u r f i e l d s : i n eq eq l i n , e q l i n , u pp p p eerr , l o w e r . ” ) disp ( y o p t . i n e q l i n , ” y o p t . i n e q l i n =” =” ) disp ( yopt . lower , ”yopt . low er=” er=” ) 7
20
browsevar ()
// //
browsevar type
of
can
browsevar be
costumized
variables .
v ar ia bl e
can
It ’ s
names .
8
also
show to
all
show
variables . all
possible
to
or
some
exclude