The MOSEK Python optimizer API manual Version 7.0 (Revision 141)
Optimizer API for Python - Documentation - Mosek Optimizer API for Python - Documentation - Mosek
26 CHAPTER 5. BASIC API TUTORIAL having the bounds maximize 3x 0 + 1x 1 + 5x 2 + 1x 3 subject to 3x 0 + 1x 1 + 2x 2 = 30, 2x 0 + 1x 1 + 3x 2 + 1x 3 ≥ 15, 2x 1 + 3x 3 ≤ 25, 0 ≤ x 0 ≤ ∞, 0 ≤ x 1 ≤ 10, 0 ≤ x 2 ≤ ∞, 0 ≤ x 3 ≤ ∞. 5.2.1.1 Solving the problem To solve the problem above we go through the following steps: • Create an environment. • Create an optimization task. • Load a problem into the task object. • Optimization. • Extracting the solution. Below we explain each of these steps. For the complete source code see section 5.2.1.2. Create an environment. Before setting up the optimization problem, a MOSEK environment must be created. All tasks in the program should share the same environment. 31 # Make mosek environment 32 with mosek.Env() as env: [ lo1.py ] Create an optimization task. Next, an empty task object is created: [ lo1.py ] 33 # Create a task object 34 with env.Task(0,0) as task: 35 # Attach a log stream printer to the task 36 task.set Stream (mosek.streamtype.log, streamprinter) We also connect a call-back function to the task log stream. Messages related to the task are passed to the call-back function. In this case the stream call-back function writes its messages to the standard output stream.
5.2. LINEAR OPTIMIZATION 27 Load a problem into the task object. Before any problem data can be set, variables and constraints must be added to the problem via calls to the functions Task.appendcons and Task.appendvars. [ lo1.py ] 74 # Append ’numcon’ empty constraints. 75 # The constraints will initially have no bounds. 76 task.appendcons(numcon) 77 78 # Append ’numvar’ variables. 79 # The variables will initially be fixed at zero (x=0). 80 task.appendvars(numvar) New variables can now be referenced from other functions with indexes in 0, . . . , numvar − 1 and new constraints can be referenced with indexes in 0, . . . , numcon−1. More variables / constraints can be appended later as needed, these will be assigned indexes from numvar/numcon and up. Next step is to set the problem data. We loop over each variable index j = 0, . . . , numvar − 1 calling functions to set problem data. We first set the objective coefficient c j = c[j] by calling the function Task.putcj. 84 task.putcj(j,c[j]) [ lo1.py ] The bounds on variables are stored in the arrays 47 # Bound keys for variables 48 bkx = [mosek.boundkey.lo, 49 mosek.boundkey.ra, 50 mosek.boundkey.lo, 51 mosek.boundkey.lo] 52 53 # Bound values for variables 54 blx = [ 0.0, 0.0, 0.0, 0.0] 55 bux = [+inf, 10.0, +inf, +inf] [ lo1.py ] and are set with calls to Task.putvarbound. 86 # Set the bounds on variable j 87 # blx[j]
- Page 1 and 2: The MOSEK Python optimizer API manu
- Page 3 and 4: Contents 1 Changes and new features
- Page 5 and 6: CONTENTS v 8 A case study 97 8.1 Po
- Page 7 and 8: CONTENTS vii 14.2.1 Caveats . . . .
- Page 9 and 10: CONTENTS ix A.2.70 Task.getdviolvar
- Page 11 and 12: CONTENTS xi A.2.162Task.isdouparnam
- Page 13 and 14: CONTENTS xiii A.2.254Task.readparam
- Page 15 and 16: CONTENTS xv B.1.45 dparam.mio near
- Page 17 and 18: CONTENTS xvii B.2.68 iparam.log sim
- Page 19 and 20: CONTENTS xix B.2.160iparam.sim refa
- Page 21 and 22: CONTENTS xxi D.18 Long integer info
- Page 23 and 24: Contact information Phone +45 3917
- Page 25 and 26: License agreement Before using the
- Page 27 and 28: Chapter 1 Changes and new features
- Page 29 and 30: 1.4. API CHANGES 7 1.3.3 Mixed-inte
- Page 31 and 32: 1.10. SUMMARY OF API CHANGES 9 •
- Page 33 and 34: 1.10. SUMMARY OF API CHANGES 11 •
- Page 35 and 36: 1.10. SUMMARY OF API CHANGES 13 •
- Page 37 and 38: Chapter 2 About this manual This ma
- Page 39 and 40: Chapter 3 Getting support and help
- Page 41 and 42: Chapter 4 Testing installation and
- Page 43 and 44: Chapter 5 Basic API tutorial In thi
- Page 45 and 46: 5.1. THE BASICS 23 5 # 6 # Purpose:
- Page 47: 5.2. LINEAR OPTIMIZATION 25 and the
- Page 51 and 52: 5.2. LINEAR OPTIMIZATION 29 Optimiz
- Page 53 and 54: 5.2. LINEAR OPTIMIZATION 31 88 task
- Page 55 and 56: 5.2. LINEAR OPTIMIZATION 33 51 mose
- Page 57 and 58: 5.3. CONIC QUADRATIC OPTIMIZATION 3
- Page 59 and 60: 5.3. CONIC QUADRATIC OPTIMIZATION 3
- Page 61 and 62: 5.4. SEMIDEFINITE OPTIMIZATION 39 m
- Page 63 and 64: 5.4. SEMIDEFINITE OPTIMIZATION 41 4
- Page 65 and 66: 5.4. SEMIDEFINITE OPTIMIZATION 43 1
- Page 67 and 68: 5.5. QUADRATIC OPTIMIZATION 45 with
- Page 69 and 70: 5.5. QUADRATIC OPTIMIZATION 47 68 #
- Page 71 and 72: 5.5. QUADRATIC OPTIMIZATION 49 5.5.
- Page 73 and 74: 5.5. QUADRATIC OPTIMIZATION 51 81 a
- Page 75 and 76: 5.6. THE SOLUTION SUMMARY 53 • Th
- Page 77 and 78: 5.7. INTEGER OPTIMIZATION 55 21 # f
- Page 79 and 80: 5.7. INTEGER OPTIMIZATION 57 137 sy
- Page 81 and 82: 5.8. THE SOLUTION SUMMARY FOR MIXED
- Page 83 and 84: 5.9. RESPONSE HANDLING 61 Note that
- Page 85 and 86: 5.10. PROBLEM MODIFICATION AND REOP
- Page 87 and 88: 5.10. PROBLEM MODIFICATION AND REOP
- Page 89 and 90: 5.11. SOLUTION ANALYSIS 67 151 # Pu
- Page 91 and 92: 5.13. CONVENTIONS EMPLOYED IN THE A
- Page 93 and 94: 5.13. CONVENTIONS EMPLOYED IN THE A
- Page 95 and 96: 5.13. CONVENTIONS EMPLOYED IN THE A
- Page 97 and 98: 5.13. CONVENTIONS EMPLOYED IN THE A
5.2. LINEAR OPTIMIZATION 27<br />
Load a problem into the task object.<br />
Before any problem data can be set, variables and constraints must be added to the problem via<br />
calls to the functions Task.appendcons and Task.appendvars.<br />
[ lo1.py ]<br />
74 # Append ’numcon’ empty constraints.<br />
75 # <strong>The</strong> constraints will initially have no bounds.<br />
76 task.appendcons(numcon)<br />
77<br />
78 # Append ’numvar’ variables.<br />
79 # <strong>The</strong> variables will initially be fixed at zero (x=0).<br />
80 task.appendvars(numvar)<br />
New variables can now be referenced from other functions with indexes in 0, . . . , numvar − 1 and<br />
new constraints can be referenced with indexes in 0, . . . , numcon−1. More variables / constraints<br />
can be appended later as needed, these will be assigned indexes from numvar/numcon and up.<br />
Next step is to set the problem data. We loop over each variable index j = 0, . . . , numvar − 1<br />
calling functions to set problem data. We first set the objective coefficient c j = c[j] by calling<br />
the function Task.putcj.<br />
84 task.putcj(j,c[j])<br />
[ lo1.py ]<br />
<strong>The</strong> bounds on variables are stored in the arrays<br />
47 # Bound keys for variables<br />
48 bkx = [mosek.boundkey.lo,<br />
49 mosek.boundkey.ra,<br />
50 mosek.boundkey.lo,<br />
51 mosek.boundkey.lo]<br />
52<br />
53 # Bound values for variables<br />
54 blx = [ 0.0, 0.0, 0.0, 0.0]<br />
55 bux = [+inf, 10.0, +inf, +inf]<br />
[ lo1.py ]<br />
and are set with calls to Task.putvarbound.<br />
86 # Set the bounds on variable j<br />
87 # blx[j]