The MOSEK Python optimizer API manual Version 7.0 (Revision 141)

Optimizer API for Python - Documentation - Mosek Optimizer API for Python - Documentation - Mosek

25.11.2015 Views

62 CHAPTER 5. BASIC API TUTORIAL 34 if solsta in [ mosek.solsta.optimal, 35 mosek.solsta.near optimal ]: 36 print ("An optimal basic solution is located.") 37 task.solutionsummary(mosek.streamtype.log) 38 elif solsta in [ mosek.solsta.dual infeas cer, 39 mosek.solsta.near dual infeas cer ]: 40 print ("Dual infeasibility certificate found.") 41 elif solsta in [ mosek.solsta.prim infeas cerl, 42 mosek.solsta.near prim infeas cer ]: 43 print("Primal infeasibility certificate found.\n"); 44 elif solsta == mosek.solsta.sta unknown: 45 # The solutions status is unknown. The termination code 46 # indicating why the optimizer terminated prematurely. 47 print ("The solution status is unknown.") 48 print ("Termination code: %s" % str(trmcode)) 49 else: 50 print ("An unexpected solution status is obtained.") 51 52 except mosek.MosekException as err: 53 print ("Generic error:") 54 print (err) 55 56 if name == ’ main ’: 57 import sys 58 main(sys.argv[1:]) 5.10 Problem modification and reoptimization Often one might want to solve not just a single optimization problem, but a sequence of problem, each differing only slightly from the previous one. This section demonstrates how to modify and re-optimize an existing problem. The example we study is a simple production planning model. 5.10.1 Example: Production planning A company manufactures three types of products. Suppose the stages of manufacturing can be split into three parts, namely Assembly, Polishing and Packing. In the table below we show the time required for each stage as well as the profit associated with each product. Product no. Assembly (minutes) Polishing (minutes) Packing (minutes) Profit ($) 0 2 3 2 1.50 1 4 2 3 2.50 2 3 3 2 3.00 With the current resources available, the company has 100, 000 minutes of assembly time, 50, 000 minutes of polishing time and 60, 000 minutes of packing time available per year. Now the question is how many items of each product the company should produce each year in order to maximize profit?

5.10. PROBLEM MODIFICATION AND REOPTIMIZATION 63 Denoting the number of items of each type by x 0 , x 1 and x 2 , this problem can be formulated as the linear optimization problem: and maximize 1.5x 0 + 2.5x 1 + 3.0x 2 subject to 2x 0 + 4x 1 + 3x 2 ≤ 100000, 3x 0 + 2x 1 + 3x 2 ≤ 50000, 2x 0 + 3x 1 + 2x 2 ≤ 60000, x 0 , x 1 , x 2 ≥ 0. The following code loads this problem into the optimization task. 35 # Create a MOSEK environment 36 env = mosek.Env () 37 [ production.py ] 38 # Create a task 39 task = env.Task(0,0) 40 # Attach a printer to the task 41 task.set Stream (mosek.streamtype.log, streamprinter) 42 43 # Bound keys for constraints 44 bkc = [mosek.boundkey.up, 45 mosek.boundkey.up, 46 mosek.boundkey.up] 47 # Bound values for constraints 48 blc = array ([-inf, -inf, -inf]) 49 buc = array ([100000.0 , 50000.0, 60000.0]) 50 51 # Bound keys for variables 52 bkx = [mosek.boundkey.lo, 53 mosek.boundkey.lo, 54 mosek.boundkey.lo] 55 # Bound values for variables 56 blx = array ([ 0.0, 0.0, 0.0]) 57 bux = array ([+inf, +inf, +inf]) 58 59 # Objective coefficients 60 csub = array([ 0, 1, 2 ]) 61 cval = array([ 1.5, 2.5, 3.0 ]) 62 63 # We input the A matrix column-wise 64 # asub contains row indexes 65 asub = array([ 0, 1, 2, 66 0, 1, 2, 67 0, 1, 2]) 68 # acof contains coefficients 69 acof = array([ 2.0, 3.0, 2.0, 70 4.0, 2.0, 3.0, 71 3.0, 3.0, 2.0 ]) 72 # aptrb and aptre contains the offsets into asub and acof where 73 # columns start and end respectively 74 aptrb = array([ 0, 3, 6 ]) 75 aptre = array([ 3, 6, 9 ])

62 CHAPTER 5. BASIC <strong>API</strong> TUTORIAL<br />

34 if solsta in [ mosek.solsta.optimal,<br />

35 mosek.solsta.near optimal ]:<br />

36 print ("An optimal basic solution is located.")<br />

37 task.solutionsummary(mosek.streamtype.log)<br />

38 elif solsta in [ mosek.solsta.dual infeas cer,<br />

39 mosek.solsta.near dual infeas cer ]:<br />

40 print ("Dual infeasibility certificate found.")<br />

41 elif solsta in [ mosek.solsta.prim infeas cerl,<br />

42 mosek.solsta.near prim infeas cer ]:<br />

43 print("Primal infeasibility certificate found.\n");<br />

44 elif solsta == mosek.solsta.sta unknown:<br />

45 # <strong>The</strong> solutions status is unknown. <strong>The</strong> termination code<br />

46 # indicating why the <strong>optimizer</strong> terminated prematurely.<br />

47 print ("<strong>The</strong> solution status is unknown.")<br />

48 print ("Termination code: %s" % str(trmcode))<br />

49 else:<br />

50 print ("An unexpected solution status is obtained.")<br />

51<br />

52 except mosek.MosekException as err:<br />

53 print ("Generic error:")<br />

54 print (err)<br />

55<br />

56 if name == ’ main ’:<br />

57 import sys<br />

58 main(sys.argv[1:])<br />

5.10 Problem modification and reoptimization<br />

Often one might want to solve not just a single optimization problem, but a sequence of problem, each<br />

differing only slightly from the previous one. This section demonstrates how to modify and re-optimize<br />

an existing problem. <strong>The</strong> example we study is a simple production planning model.<br />

5.10.1 Example: Production planning<br />

A company manufactures three types of products. Suppose the stages of manufacturing can be split<br />

into three parts, namely Assembly, Polishing and Packing. In the table below we show the time<br />

required for each stage as well as the profit associated with each product.<br />

Product no. Assembly (minutes) Polishing (minutes) Packing (minutes) Profit ($)<br />

0 2 3 2 1.50<br />

1 4 2 3 2.50<br />

2 3 3 2 3.00<br />

With the current resources available, the company has 100, 000 minutes of assembly time, 50, 000<br />

minutes of polishing time and 60, 000 minutes of packing time available per year.<br />

Now the question is how many items of each product the company should produce each year in order<br />

to maximize profit?

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!