Operatioanalyysi 2011, Harjoitus 4, viikko 40 H4t1, Exercise 4.2. H4t2, Exercise 4.3. H4t3, Exercise 4.4. H4t4, Exercise 4.5. H4t5, Exercise 4.6.
(Exercise 4.2.) 1 4.2. Solve the LP max z = x 1 + 2x 2 s.t. x 1 + x 2 6 x 1 + 4x 2 20 x 1,x 2 0 (a) by using simplex_lp_solver, (b) by using glpk.
(Exercise 4.2.(a)) 2 c = [ 1 2 ] ; A = [ 1 1; 1 4 ]; b = [ 6 20 ] ; [z,x,status] = simplex_lp_solver(c,a,b,100) ############################################## # z = 10.667 # x = # 1.3333 # 4.6667 # status = bounded #
(Exercise 4.2.(b)) 3 c = [ 1 2 ] ; A = [ 1 1; 1 4 ]; b = [ 6 20 ] ; param.presol = 0; [x,z,status] = glpk(c,a,b,[0 0],[],"UU","CC",-1,param) ############################################### # x = # 1.3333 # 4.6667 # z = 10.667 # status = 180
H4t2. (Exercise 4.3.) 4 4.3. Consider the LP max z = 4x 1 + 7x 2 s.t. x 1 + 3x 2 5 6x 1 6x 2 3 x 1,x 2 0 (a) Can this LP be solved by simplex_lp_solver? (b) Solve the LP by using glpk. Note that you may have to switch of the glpk s presolver. Type help glpk to find out how to do this.
H4t2. (Exercise 4.3.) 4 4.3. Consider the LP max z = 4x 1 + 7x 2 s.t. x 1 + 3x 2 5 6x 1 6x 2 3 x 1,x 2 0 (a) Can this LP be solved by simplex_lp_solver? (b) Solve the LP by using glpk. Note that you may have to switch of the glpk s presolver. Type help glpk to find out how to do this. (a) LP-mallia ei voi ratkaista simplex_lp_solver skriptillä, sillä b 2 < 0, mikä on vastoin simplex-algoritmin oletuksia.
H4t2. (Exercise 4.3.(b)) 5 c = [ 4 7 ] ; A = [-1 3; -6-6 ]; b = [ 5-3 ] ; param.presol = 0; [x_max,z_max,status] = glpk(c,a,b,[0 0], [], "UU", "CC",-1,param) ################################# # x_max = # 0.50000 # 0.00000 # z_max = 2 # status = 184 Status-koodit 180 Solution is optimal. 182 Solution is infeasible. 183 Problem has no feasible solution. 184 Problem has no unbounded solution. 185 Solution status is undefined.
H4t2. (Exercise 4.3.(b)) 6 Koska status-koodi on 184 (eli Problem has no unbounded solution ), niin ongelmalla on rajoittamaton optimi. Tavoitefunktion arvo saadaan miten hyväksi tahansa.
H4t2. (Exercise 4.3.(b)) 6 Koska status-koodi on 184 (eli Problem has no unbounded solution ), niin ongelmalla on rajoittamaton optimi. Tavoitefunktion arvo saadaan miten hyväksi tahansa. ks. max z = 4x 1 + 7x 2 s.t. x 1 + 3x 2 5 6x 1 6x 2 3 x 1,x 2 0 Kiinnitetää x 2 :n arvo vaikkapa nollaksi ja aletaan kasvattaa x 1 :n arvoa. Mitään rajaa ei tule vastaan, vaan x 1 :n arvo voidaan tehdä miten suureksi tahansa. Siis tavoitefunktiolle voidaan saada tähtitieteellisen hyviä arvoja. (Käytännössä non sense.)
H4t3. (Exercise 4.4.) 7 4.4. Don Guido has peculiar taste: He likes a mixture of Monte Conero and Pinot Grigio (yuck). His rosé must have at least 20% of both grapes. Monte Conero costs e2 per liter and Pinot Grigio costs e3 per liter. Don Guido wants to mix 100 liters of his rosé for the upcoming wedding of his daughter (poor guests). He has infinite supply of Monte Conero, but only 50 liters of Pinot Grigio at his disposal. Also, he has promised to buy at least 10 liters of Monte Conero from his cousin Don Luigi. How should Don Guido mix his rosé to make it as cheap as possible? You can find the answer any way you want, e.g. by using glpk or simplex_lp_solver (if it works), or by simply deducing the obvious (and miss the joy of modeling the problem as an LP).
H4t3. (Exercise 4.4.) 8 Päätösmuuttujat:
H4t3. (Exercise 4.4.) 8 Päätösmuuttujat: x 1 = käytetty Monte Coneron määrä (litraa) x 2 = käytetty Pinot Grigion määrä (litraa)
H4t3. (Exercise 4.4.) 8 Päätösmuuttujat: x 1 = käytetty Monte Coneron määrä (litraa) x 2 = käytetty Pinot Grigion määrä (litraa) Tavoitefunktio:
H4t3. (Exercise 4.4.) 8 Päätösmuuttujat: x 1 = käytetty Monte Coneron määrä (litraa) x 2 = käytetty Pinot Grigion määrä (litraa) Tavoitefunktio: minimoidaan kustannuksia min z = 2x 1 + 3x 2 (euroa)
H4t3. (Exercise 4.4.) 9 Rajoitteet:
H4t3. (Exercise 4.4.) 9 Rajoitteet: Kumpaakin raaka-ainetta tulee olla vähintään 20% lopputuotteesta (1) x 1 0.20 (x 1 + x 2 ) 0.8x 1 0.2x 2 0 ja (2) x 2 0.20 (x 1 + x 2 ) 0.2x 1 + 0.8x 2 0
H4t3. (Exercise 4.4.) 9 Rajoitteet: Kumpaakin raaka-ainetta tulee olla vähintään 20% lopputuotteesta (1) x 1 0.20 (x 1 + x 2 ) 0.8x 1 0.2x 2 0 ja (2) x 2 0.20 (x 1 + x 2 ) 0.2x 1 + 0.8x 2 0 Pino Grigiota on saatavissa enintään 50 litraa (3) x 2 50
H4t3. (Exercise 4.4.) 9 Rajoitteet: Kumpaakin raaka-ainetta tulee olla vähintään 20% lopputuotteesta (1) x 1 0.20 (x 1 + x 2 ) 0.8x 1 0.2x 2 0 ja (2) x 2 0.20 (x 1 + x 2 ) 0.2x 1 + 0.8x 2 0 Pino Grigiota on saatavissa enintään 50 litraa (3) x 2 50 Monte Coneroa on luvattu ostaa ainakin 10 litraa (4) x 1 10
H4t3. (Exercise 4.4.) 9 Rajoitteet: Kumpaakin raaka-ainetta tulee olla vähintään 20% lopputuotteesta (1) x 1 0.20 (x 1 + x 2 ) 0.8x 1 0.2x 2 0 ja (2) x 2 0.20 (x 1 + x 2 ) 0.2x 1 + 0.8x 2 0 Pino Grigiota on saatavissa enintään 50 litraa (3) x 2 50 Monte Coneroa on luvattu ostaa ainakin 10 litraa (4) x 1 10 Roseta tehdään 100 litraa (5) x 1 + x 2 = 100
H4t3. (Exercise 4.4.) 10 LP-malli: min z = 2x 1 + 3x 2 s.t. 0.8x 1 0.2x 2 0 0.2x 1 + 0.8x 2 0 x 2 50 x 1 10 x 1 + x 2 = 100 x 1,x 2 0
H4t3. (Exercise 4.4.) 10 LP-malli: min z = 2x 1 + 3x 2 s.t. 0.8x 1 0.2x 2 0 0.2x 1 + 0.8x 2 0 x 2 50 x 1 10 x 1 + x 2 = 100 x 1,x 2 0 c = [ 2 3 ] ; A = [ 0.8-0.2; -0.2 0.8; 0 1; 1 0; 1 1]; b = [ 0 0 50 10 100 ] ; param.presol = 0; [x,z,status] = glpk(c,a,b,[0 0], [], "LLULS", "CC",1,param) ################################# # x_max = [80, 20] # z_max = 220 # status = 180
H4t4. (Exercise 4.5.) 11 4.5. simplex_lp_solver assumes that the LP is a maximization problem. Modify it so that it takes an additional input value indicating whether the problem is maximization or minimization, and then solves the problem.
H4t4. (Exercise 4.5.) 11 4.5. simplex_lp_solver assumes that the LP is a maximization problem. Modify it so that it takes an additional input value indicating whether the problem is maximization or minimization, and then solves the problem. function [z, x, status] = my_simplex_lp_solver(c, A, b, maxiter=100,sense=1) ## sense == 1 <--> maksimointi ## sense == -1 <--> minimointi if (sense==1) [z, x, status] = simplex_lp_solver(c, A, b, maxiter); else [z, x, status] = simplex_lp_solver(-c, A, b, maxiter); z = -z; end
(Exercise 4.6.) 12 4.6. Find out an LP that is in principle within the scope of the simplex algorithm implemented in simplex_lp_solver, i.e. b 0, but for which simplex_lp_solver fails anyway. Can you find the problem with simplex_lp_solver and fix it? Note that there are many different solutions to this exercise. The main point of this exercise is to show that the function simplex_lp_solver does not meet industry standards.
(Exercise 4.6.) 13 Ongelma: max z = 2x 1 + 3x 2 s.t. 0,8x 1 0,2x 2 0 0,8x 1 + 0,2x 2 0 x 1 + x 2 100 x 1,x 2 0 Seuraava ratkaisu on väärä: c = [ 2 3 ] ; A = [ 0.8-0.2; -0.8 0.2; 1 1 ]; b = [ 0 0 100 ] ; maxiter = 100; [z_max,x_max,status] = simplex_lp_solver(c,a,b,maxiter) ################################ # z_max = 0 # x_max = # 0 # 0 # status = unknown
(Exercise 4.6.) 14 max z = 2x 1 + 3x 2 s.t. 0,8x 1 0,2x 2 0 0,8x 1 + 0,2x 2 0 x 1 + x 2 100 x 1,x 2 0 glpk antaa oikean ratkaisun: c = [ 2 3 ] ; A = [ 0.8-0.2; -0.8 0.2; 1 1]; b = [ 0 0 100 ] ; [x,z,status] = glpk(c,a,b,[0 0], [], "UUU", "CC", -1) ################################# # x = # 20 # 80 # z = 280 # status = 180
(Exercise 4.6.) 15 Ratkaistaan ongelma käsin simplex-tauluilla, jotta näkisimme mahdollisen ongelmakohdan: Esimerkin mukaiset siplex-taulut ovat: (#1) z x 1 x 2 s 1 s 2 s 3 BV RHS 0 1-2 -3 0 0 0 z = 0 1 0 0.8 (-0,2) 1 0 0 s 1 = 0 2 0-0.8 0,2 0 1 0 s 2 = 0 3 0 1 1 0 0 1 s 3 = 100 BV = {s 1,s 2,s 3 }
(Exercise 4.6.) 15 Ratkaistaan ongelma käsin simplex-tauluilla, jotta näkisimme mahdollisen ongelmakohdan: Esimerkin mukaiset siplex-taulut ovat: (#1) z x 1 x 2 s 1 s 2 s 3 BV RHS 0 1-2 -3 0 0 0 z = 0 1 0 0.8 (-0,2) 1 0 0 s 1 = 0 2 0-0.8 0,2 0 1 0 s 2 = 0 3 0 1 1 0 0 1 s 3 = 100 BV = {s 1,s 2,s 3 } kantaan tuleva muuttuja on x 2
(Exercise 4.6.) 15 Ratkaistaan ongelma käsin simplex-tauluilla, jotta näkisimme mahdollisen ongelmakohdan: Esimerkin mukaiset siplex-taulut ovat: (#1) z x 1 x 2 s 1 s 2 s 3 BV RHS 0 1-2 -3 0 0 0 z = 0 1 0 0.8 (-0,2) 1 0 0 s 1 = 0 2 0-0.8 0,2 0 1 0 s 2 = 0 3 0 1 1 0 0 1 s 3 = 100 BV = {s 1,s 2,s 3 } kantaan tuleva muuttuja on x 2 Suhdetesti kannasta poistuvan muuttujan löytämiseksi: rivi 1: 0/ 0,2 < 0 sivuutetaan rivi 2: 0/0,2 = 0 ok voittaja rivi 3: 100/1 = 100 ok Kannasta poistuu s 2.
(Exercise 4.6.) 16 Uusi siplex-taulu: (#2) z x 1 x 2 s 1 s 2 s 3 BV RHS 0 1-14 0 0 15 0 z = 0 1 0 0 0 1 1 0 s 1 = 0 2 0-4 1 0 5 0 x 2 = 0 3 0 5 0 0-5 1 s 3 = 100 BV = {s 1,x 2,s 3 }
(Exercise 4.6.) 16 Uusi siplex-taulu: (#2) z x 1 x 2 s 1 s 2 s 3 BV RHS 0 1-14 0 0 15 0 z = 0 1 0 0 0 1 1 0 s 1 = 0 2 0-4 1 0 5 0 x 2 = 0 3 0 5 0 0-5 1 s 3 = 100 BV = {s 1,x 2,s 3 } kantaan tuleva muuttuja on x 1
(Exercise 4.6.) 16 Uusi siplex-taulu: (#2) z x 1 x 2 s 1 s 2 s 3 BV RHS 0 1-14 0 0 15 0 z = 0 1 0 0 0 1 1 0 s 1 = 0 2 0-4 1 0 5 0 x 2 = 0 3 0 5 0 0-5 1 s 3 = 100 BV = {s 1,x 2,s 3 } kantaan tuleva muuttuja on x 1 Suhdetesti kannasta poistuvan muuttujan löytämiseksi: rivi 1: 0/0 sivuutetaan rivi 2: 0/-4 < 0 sivuutetaan rivi 3: 100/5 = 20 ok <- voittaja Kannasta poistuu s3.
(Exercise 4.6.) 17 Uusi siplex-taulu: (#3) z x 1 x 2 s 1 s 2 s 3 BV RHS 0 : 1 0 0 0 1 0 z = 280 OPTIMI 1 : 0 0 0 1 1 0 s 1 = 0 2 : 0 0 1 0 1 0,8 x 2 = 80 3 : 0 1 0 0-1 0,2 x 1 = 20 BV = s 1,x 2,s 3
(Exercise 4.6.) 18 skriptin simplex_lp_solver testausta: (1) jos lisää new_simplex_tableau skriptiin rivit, jotka tulostavat kantaan tulevan uuden muuttujan (sarake) ja kannasta poistuvan muuttujan (rivi), niin yllä olevalle LP-mallille algoritmin kulku on BV in out BV (s1,s2,s3,s4,s5) 2 (x2) 1 (s1) (x2,s2,s3,s4,s5) (x2,s2,s3,s4,s5) 3 (s1) 1 (x2) (s1,s2,s3,s4,s5)...
(Exercise 4.6.) 18 skriptin simplex_lp_solver testausta: (1) jos lisää new_simplex_tableau skriptiin rivit, jotka tulostavat kantaan tulevan uuden muuttujan (sarake) ja kannasta poistuvan muuttujan (rivi), niin yllä olevalle LP-mallille algoritmin kulku on BV in out BV (s1,s2,s3,s4,s5) 2 (x2) 1 (s1) (x2,s2,s3,s4,s5) (x2,s2,s3,s4,s5) 3 (s1) 1 (x2) (s1,s2,s3,s4,s5)... Syy on siinä, että 0/0 ei ole Inf vaan NaN. Ratkaisuksi skriptin new_simplex_tableau.m rivit 21-26
(Exercise 4.6.) 19 21 ## Set all the "non-limit" ratios to -infinity 22 for i=1:length(ratio) 23 if (ratio(i)==inf ratio(i)<0) 24 ratio(i) = -Inf; 25 endif 26 endfor kirjoitetaan muotoon 21 ## Set all the "non-limit" ratios to -infinity 22 for i=1:length(ratio) 23 ratio(i)=rhs(i)/cin(i); 24 if (abs(cin(i))<1.0e-8) 25 ratio(i) = -Inf; 26 endif 27 if (abs(rhs(i))<1.0e-6 && cin(i)<0) 28 ratio(i) = -Inf; 29 endif 30 endfor