You are on page 1of 13

CSSE 444 Final Exam Peter Klein CM1745

February 21, 2012


1. a. The following is the model that will be solved by the cities.
Let s index the set of supply cities S, partitioned into S1 and S2 indexed by s1
and s2 respectively. Similarly let d index the set of demand cities D, partitioned
into D1 and D2 indexed by d1 and d2 respectively. The model is described in
terms of the following parameters:
p1 - price of shipping a package from S1 to D1
p2 - price of shipping a package from S2 to D2
es - demand at each demand city
ed - supply at each supply city
We let x
sd
be the decision variable for the number of packages sent from city s to
city d. A solution to the following model wold minimize net cost:
max (p1

s1d1
x
s1d1
) + (
p1+p2
3

s1d1
x
s1d1
) + (p2

s2d2
x
s2d2
)
s.t.

d
x
sd
es, s

s
x
sd
ed, d
b.
c. See implementation in section A.1.
1
2. a. The extreme points can be found by examining each basis. Only the basis which
generate feasible solutions are listed for brevity.
For n = 2,
x
1
x
2
5 5
5 0
0 25
0 0
For n = 3,
x
1
x
2
x
3
5 5 65
5 5 0
5 0 85
5 0 0
0 25 25
0 25 0
0 0 125
0 0 0
b. To run the simplex method for n = 2, we must rst convert to the canonical
form by adding slack variables. Each step in the execution of the simplex method
beginning at the origin is listed in the following tables:
x Basis Prices In
max
Out
(0, 0, 5, 25) s
1
, s
2
(2, 1) x
1
(5, 25/4) s
1
(5, 0, 0, 5) x
1
, s
2
(1, 2) x
2
(5) s
2
(5, 5, 0, 0) x
1
, x
2
(2, 1) s
1
(5) x
1
(0, 25, 5, 0) x
2
, s
1
(2, 1)
And similarly for n = 3:
x Basis Prices In
max
Out
(0, 0, 0, 5, 25, 125) s
1
, s
2
, s
3
(4, 2, 1) x
1
(5, 25/4, 125/8) s
1
(5, 0, 0, 0, 5, 85) x
1
, s
2
, s
3
(2, 1, 4) x
2
(5, 85/4) s
2
(5, 5, 0, 0, 0, 65) x
1
, x
2
, s
3
(1, 4, 2) s
1
(5, 65/8) x
1
(0, 25, 0, 5, 0, 25) x
2
, s
1
, s
3
(4, 1, 2) x
3
(25) s
3
(0, 25, 25, 5, 0, 0) x
2
, x
3
, s
1
(4, 2, 1) x
1
(25/4, 5) s
1
(5, 5, 65, 0, 0, 0) x
1
, x
2
, x
3
(4, 2, 1) s
2
(5) x
2
(5, 0, 85, 0, 5, 0) x
1
, x
3
, s
2
(2, 4, 1) s
1
(5) x
1
(0, 0, 125, 5, 25, 0) x
3
, s
1
, s
2
(4, 2, 1)
Therefore, using Dantzigs pivot rule, we can see that the simplex method visits
all extreme points in their systems.
1
1
See the python code at the end for my implementation of the Simplex Method (complete with latex
table generation.
2
c. For linear programs generated by the formula
max
_
n

i=1
2
n1
x
i
:
_
k1

i=1
2
ki+1
x
i
_
+ x
k
5
k
, for k = 1, 2, . . . , n, x 0
_
we have seen for cases of n = 2, n = 3, and n = 4 (not shown) that the number of
extreme points is equal to 4,8, and 16 respectively. For each increment of n, one
new dimension and one new constraint is added to the existing problem. Since
the new constraint (
_

n1
i=1
2
ni+1
x
i
_
+x
n
5
n
) will intersect all of the edges of
the original problem, it will eectively double the number of extreme points. This
explains why the number of extreme points for any value of n is 2
n
.
The number of steps to solve a linear program is related to the number of extreme
points. In problems of the given formula, the improving direction is also the
direction with the most limited movement, so in order to nd the optimal solution,
the Simplex Method will need to traverse all extreme points in the system.
Therefore systems of this nature are all exponential, specically with runtime 2
n
.
While these results do not hold for every system, they do apply generally to most
that exist in the real world.
3
3. Let x
1
and x
2
be feasible solutions to the linear problem min
_
c
T
x : Ax = b, x 0
_
where c is a linear combination of the rows of A. It can be shown that all feasible
solutions are optimal. We know that
Ax
1
= b x
1
0
Ax
2
= b x
2
0
and because c is a linear combination of the rows of A, there exists some n such that
c
T
= n
T
A.
So for any feasible solution,
Ax
1
= Ax
2
n
T
Ax
1
= n
T
Ax
2
(n
T
A)x
1
= (n
T
A)x
2
c
T
x
1
= c
T
x
2
Since the objective values are equivalent for all feasible solutions, every solution must
be optimal.
4
4. Let x

, y

, and s

satisfy
Ay

= c, y 0
A
T
x

+ s

= b, s 0
(y

)
T
s

= , > 0
With the variables x,y and b,c being switched from the book to t our books convention
of x being the solution variable of the maximization problem
Show that

b
T
y

min
_
b
T
y : Ay = c, y 0
_


First note that
= (y

)
T
s

= (y

)
T
(b A
T
x

)
= ((y

)
T
b) ((y

)
T
A
T
x

)
= b
T
y

(Ay

)
T
x

= b
T
y

c
T
x

Let y

be the optimal solution to min


_
b
T
y : Ay = c, y 0
_
so b
T
y

= c
T
x

by a
corollary to the weak duality theorem,

b
T
y

min
_
b
T
y : Ay = c, y 0
_

b
T
y

b
T
y

b
T
y

c
T
x

and because c
T
y

> c
T
y

as c
T
y

is the optimal solution to a maximization problem,

b
T
y

c
T
x

b
T
y

c
T
x

b
T
y

c
T
x


Therefore, for any choice of x

, y

, and s

b
T
y

min
_
b
T
y : Ay = c, y 0
_

must
be true.
5
5. Show that z() = min
_
c
T
x : Ax = b + b, x 0
_
is convex over its domain.
6
6. Use Gordons Theorem of the alternative to show that if argmin
_
c
T
x : Ax = b, x 0
_
is unbounded, then the strict interior of the dual is empty, ie
_
(y, s) : A
T
y + s = c, s > 0
_
= .
7
A Code
A.1 Pyomo
A.1.1 Klein F 1c.py
1 from coopr . pyomo import
2 from coopr . opt import
3
4 # I ns t a nt i a t e t he model
5 M = AbstractModel ( )
6
7 # Set s
8 M. Supply = Set ( )
9 M. Suppl y1 = Set ( wi t hi n=M. Supply )
10 M. Suppl y2 = Set ( wi t hi n=M. Supply )
11
12 M. Demand = Set ( )
13 M. Demand1 = Set ( wi t hi n=M. Demand)
14 M. Demand2 = Set ( wi t hi n=M. Demand)
15
16 # Parameters
17 M. EachSupply = Param( )
18 M. EachDemand = Param( )
19 M. MaxAveragePrice = Param( )
20
21 # Var i abl es
22 M. NumProduct = Var (M. Products , wi t hi n=NonNegati veI ntegers )
23
24 # Obj e c t i v e
25 def Cal c Pr of i t (M) :
26 return sum( M. NumProduct [ p ] (M. Revenue [ p ]
27 sum( M. TimeOnMachine [ p ,m] /60 M. LaborCost [m] for m in M.
Machines )
28 M. Mat eri al Cost [ p ] ) for p in M. Products )
29 M. TotProf = Obj ect i ve ( r ul e=Cal cPr of i t , s ens e=maximize )
30
31 # Cons t r ai nt s
32
33 def EnsureTimeLimit (M, m) :
34 return sum (M. NumProduct [ p ] M. TimeOnMachine [ p ,m] /60 for p in M.
Products ) \
35 <= M. HoursPerWeek
36 M. EnsureTimeLimit = Cons t r ai nt (M. Machines , r ul e=EnsureTimeLimit )
37
38 def EnsureDemandLimit (M, p) :
8
39 return M. NumProduct [ p ] <= M. DemandLimit [ p ]
40 M. EnsureDemandLimit = Cons t r ai nt (M. Products , r ul e=EnsureDemandLimit )
41
42 #M. ppr i nt ( )
43 # Creat e a probl em i ns t ance
44 i ns t anc e = M. c r e at e ( Kl e i n 2 3 . dat )
45 #i ns t ance . ppr i nt ( )
46
47 # I ndi c at e which s o l v e r t o use
48 Opt = Sol ver Fact or y ( gl pk )
49
50 # Don t keep temporary f i l e s
51 Opt . ke e pFi l e s = Fal s e
52
53 # Generate a s ol ut i on
54 Sol n = Opt . s ol ve ( i ns t anc e )
55 Sol n . wr i t e ( )
A.1.2 Klein F 1c.dat
1 s e t Supply := Se a t t l e SanDiego Denver Dal l as ;
2 s e t Suppl y1 := Se a t t l e SanDiego ;
3 s e t Suppl y2 := Denver Dal l as ;
4
5 s e t Demand := NewYork Boston Atl anta Miami ;
6 s e t Demand1 := Atl anta Miami ;
7 s e t Demand2 := NewYork Boston ;
8
9 param EachSupply := 15000 ;
10 param EachDemand := 10000 ;
11 param MaxAveragePrice := 7. 95 ;
9
A.2 Simplex Implementation
This is my Python/NumPy implementation of a simplex solver as modied for problem 2.
It can only run on systems already in canonical form and always starts at the origin. Since
assignment 7, I xed a couple more small bugs, added a bit of error handling, and added
the ability to dump out a latex table of each step in the calculation.
A.2.1 Klein Simplex.py
1 from numpy import
2 from numpy. l i n a l g import
3
4 def si mpl ex max (A, b , c , names=None , l at e x=Fal s e ) :
5 return s i mpl ex (A, b , c , argmax , max, names , l at e x )
6 def si mpl ex mi n (A, b , c , names=None , l at e x=Fal s e ) :
7 return s i mpl ex (A, b , c , argmin , min , names , l at e x )
8
9 def e ( s i z e , e ) :
10 return eye ( s i z e ) [ e ]
11 def s i mpl ex (A, b , c , ar gf , f , names=None , l at e x=Fal s e ) :
12 i f l at e x :
13 print $x$ & Bas i s & Pr i c e s & In & $\lambda {max}$ & Out \\\\
\\ hl i ne
14
15 s i z e = l en (A[ 0 ] )
16 bas i s = l en (A)
17 i f not names :
18 names = [ x %d%n for n in range ( 1 , s i z e bas i s +1) ] + [ s %d%n
for n in range ( 1 , bas i s +1) ]
19 B = array ( range ( s i z e bas i s , s i z e ) )
20
21 N = s e t d i f f 1 d ( range ( 0 , s i z e ) ,B)
22
23 s t ep = 1
24 x = z e r os ( s i z e )
25 x [ B]=b
26
27 while True :
28 i f not l at e x :
29 print ==== Step %d : B={%s } , N={%s } % ( step , . j o i n ( l i s t (
array ( names ) [ B] ) ) , . j o i n ( l i s t ( array ( names ) [ N] ) ) )
30 print > x=%s %x
31 print > o bj e c t i ve=%s %c . dot ( x)
32 print > x [ b]=%s %(x [ B] , )
33 el se :
10
34 print $\\ l e f t (%s \\ r i ght ) $&$%s$& % ( , . j o i n (map( f r ac , x) )
, $ , $ . j o i n ( l i s t ( array ( names ) [ B] ) ) ) ,
35
36 # Cal cul at e pr i c e s and choose ent er i ng
37 pr i c e s = c [ N] . T c [ B] . T. dot ( i nv (A[ : , B] ) ) . dot (A[ : , N] )
38
39 i f not l at e x :
40 print \ncNT ( pr i c e s ) = %s %( pr i c e s )
41 el se :
42 print $\\ l e f t (%s \\ r i ght ) $& % ( , . j o i n (map( f r ac , pr i c e s ) )
, ) ,
43
44 # Check f or end condi t i on
45 i f ( f ( pr i c e s ) < 0 and f i s max) or ( f ( pr i c e s ) >= 0 and f i s min
) :
46 break
47
48 e nt e r i ng = N[ ar gf ( pr i c e s ) ]
49
50 i f not l at e x :
51 print > e nt e r i ng : %s % ( names [ e nt e r i ng ] )
52 el se :
53 print $%s$& % ( names [ e nt e r i ng ] ) ,
54
55 # Cal cul at e s t ep s i z e s and choose l e av i ng
56 i nd = where(i nv (A[ : , B] ) . dot(A[ : , e nt e r i ng ] . T) > 0)
57 s t e ps = x [ B] [ i nd ] / i nv (A[ : , B] ) . dot(A[ : , e nt e r i ng ] . T) [ i nd ]
58 l e avi ng = B[ i nd ] [ argmin ( s t e ps ) ]
59 l = min( s t e ps )
60 i f not l at e x :
61 print x [ B] / i nv (A[ : , B] ) . dot(A[ : , e nt e r i ng ] . T)
62 print \nlambda max = min{%s }\n > lambda = %s \n > l e avi ng
: %s \n\n % ( s t eps , l , names [ l e avi ng ] )
63 el se :
64 print $\\ l e f t (%s \\ r i ght ) $ & $%s$ \\\\ % ( , . j o i n (map(
f r ac , s t e ps ) ) , names [ l e avi ng ] )
65
66 # Update x
67 x [ B] = x [ B] l i nv (A[ : , B] ) . dot (A[ : , N] ) . dot ( e ( s i z e bas i s , ar gf (
pr i c e s ) ) )
68 x [ e nt e r i ng ] = l
69
70 # Update b as i s
71 B = uni on1d ( s e t d i f f 1 d (B, [ l e avi ng ] ) , [ e nt e r i ng ] )
72 N = s e t d i f f 1 d ( range ( 0 , s i z e ) ,B)
73
11
74 # Bookkeepi ng
75 s t ep += 1
76
77 i f not l at e x :
78 print \n==== Fi ni s hed wi th x=%s and o bj e c t i ve=%s \n % ( x , c . dot
( x) )
79 el se :
80 print &\\\\
81
82 def a l l s o l (A, b , c , names=None , l at e x=Fal s e ) :
83 from i t e r t o o l s import combi nati ons
84 s i z e = l en (A[ 0 ] )
85 bas i s = l en (A)
86 i f not names :
87 names = [ x %d%n for n in range ( 1 , s i z e bas i s +1) ] + [ s %d%n
for n in range ( 1 , bas i s +1) ]
88
89 Bs = combi nati ons ( range ( s i z e ) , bas i s )
90 for B in Bs :
91 x = z e r os ( s i z e )
92 try :
93 x [ array (B) ] =i nv (A[ : , B] ) . dot ( b)
94 except :
95 continue
96 i f a l l ( x>=0) :
97 i f not l at e x :
98 print For B=(%s ) ,N=(%s ) , x=%s % (
99 . j o i n ( l i s t ( array ( names ) [ array (B) ] ) ) ,
100 . j o i n ( l i s t ( array ( names ) [ s e t d i f f 1 d ( range ( 0 ,
s i z e ) ,B) ] ) ) ,
101 map( f r ac , x)
102 )
103 el se :
104 print %s \\\\% (
105 & . j o i n (map( f r ac , x) ) )
106 #pr i nt $%s$ & $%s$ & %s & %s \\\\% (
107 # $ , $ . j oi n ( l i s t ( array ( names ) [ array (B) ] ) ) ,
108 # $ , $ . j oi n ( l i s t ( array ( names ) [ s e t d i f f 1 d ( range
(0 , s i z e ) ,B) ] ) ) ,
109 # & . j oi n (map( s t r , map( i nt , x ) ) ) ,
110 # c . dot ( x ) )
111
112 def f r a c ( n) :
113 from f r a c t i o ns import Fr act i on
114 return s t r ( Fr act i on ( n) . l i mi t denomi nat or ( ) )
12
A.2.2 Klein F2b.py
This le simply contains the problem data and calls the previously dened simplex max
method.
1 from numpy import
2 import kl e i n s i mpl e x as si mpl ex
3
4 print ============== For n=2:
5 A = array ( [ [ 1 , 0 , 1 , 0 ] ,
6 [ 4 , 1 , 0 , 1 ] ] )
7 b = array ( [ 5 , 2 5 ] )
8 c = array ( [ 2 , 1 , 0 , 0 ] )
9 si mpl ex . a l l s o l (A, b , c , l at e x=True )
10 si mpl ex . si mpl ex max (A, b , c , l at e x=Fal s e )
11
12 print \n\n============== For n=3:
13 A = array ( [ [ 1 , 0 , 0 , 1 , 0 , 0 ] ,
14 [ 4 , 1 , 0 , 0 , 1 , 0 ] ,
15 [ 8 , 4 , 1 , 0 , 0 , 1 ] ] )
16 b = array ( [ 5 , 2 5 , 1 2 5 ] )
17 c = array ( [ 4 , 2 , 1 , 0 , 0 , 0 ] )
18 si mpl ex . a l l s o l (A, b , c , l at e x=True )
19 si mpl ex . si mpl ex max (A, b , c , l at e x=Fal s e )
20
21 print \n\n============== For n=4:
22 A = array ( [ [ 1 , 0 , 0 , 0 , 1 , 0 , 0 , 0 ] ,
23 [ 4 , 1 , 0 , 0 , 0 , 1 , 0 , 0] ,
24 [ 8 , 4 , 1 , 0 , 0 , 0 , 1 , 0] ,
25 [ 1 6 , 8 , 4 , 1 , 0 , 0 , 0 , 1 ] ] )
26 b = array ( [ 5 , 25 , 125 , 625] )
27 c = array ( [ 8 , 4 , 2 , 1 , 0 , 0 , 0 , 0 ] )
28 si mpl ex . a l l s o l (A, b , c , l at e x=True )
29 si mpl ex . si mpl ex max (A, b , c , l at e x=True )
13

You might also like