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

92 CHAPTER 7. ADVANCED API TUTORIAL 113 else: 114 print ("x%s = %s" % (basis[varsub[i]] - numcon, w1[varsub[i]])) 115 116 # Solve B^Tx = w2 117 nz = 1 118 varsub[0] = 0 119 120 nz = task.solvewithbasis(1, nz, varsub, w2) 121 122 print ("Solution to B^Tx = w2:") 123 124 for i in range(nz): 125 if basis[varsub[i]] < numcon: 126 print ("xc %s = %s" % (basis[varsub[i]], w2[varsub[i]])) 127 else: 128 print ("x %s = %s" % (basis[varsub[i]] - numcon, w2[varsub[i]]) ) 129 except Exception as e: 130 print (e) 131 132 if name == ’ main ’: 133 main() In the example above the linear system is solved using the optimal basis for (7.4) and the original right-hand side of the problem. Thus the solution to the linear system is the optimal solution to the problem. When running the example program the following output is produced. basis[0] = 1 Basis variable no 0 is xc1. basis[1] = 2 Basis variable no 1 is x0. Solution to Bx = b: x0 = 2.000000e+00 xc1 = -4.000000e+00 Solution to B^Tx = c: x1 = -1.000000e+00 x0 = 1.000000e+00 Please note that the ordering of the basis variables is and thus the basis is given by: It can be verified that [ B = [ ] x c 1 x 0 [ ] x c 1 = x 0 0 1 − 1 1 [ − 4 2 ] ]

7.2. SOLVING LINEAR SYSTEMS INVOLVING THE BASIS MATRIX 93 is a solution to [ 0 1 − 1 1 ] [ ] [ x c 1 2 = x 0 6 ] . 7.2.3 Solving arbitrary linear systems MOSEK can be used to solve an arbitrary (rectangular) linear system Ax = b using the Task.solvewithbasis function without optimizing the problem as in the previous example. This is done by setting up an A matrix in the task, setting all variables to basic and calling the Task.solvewithbasis function with the b vector as input. The solution is returned by the function. Below we demonstrate how to solve the linear system with b = (1, −2) and b = (7, 0) . [ 0 1 − 1 1 ] [ ] x0 = x 1 [ b1 b 2 ] [ solvelinear.py ] 1 from future import with statement 2 ## 3 # Copyright: Copyright (c) MOSEK ApS, Denmark. All rights reserved. 4 # 5 # File : solvelinear.py 6 # 7 # Purpose : To demonstrate the usage of MSK solvewithbasis 8 # when solving the linear system: 9 # 10 # 1.0 x1 = b1 11 # -1.0 x0 + 1.0 x1 = b2 12 # 13 # with two different right hand sides 14 # 15 # b = (1.0, -2.0) 16 # 17 # and 18 # 19 # b = (7.0, 0.0) 20 ## 21 22 import mosek 23 24 def put a( task, 25 aval, 26 asub, 27 ptrb, 28 ptre, 29 numvar, 30 basis): (7.5)

92 CHAPTER 7. ADVANCED <strong>API</strong> TUTORIAL<br />

113 else:<br />

114 print ("x%s = %s" % (basis[varsub[i]] - numcon, w1[varsub[i]]))<br />

115<br />

116 # Solve B^Tx = w2<br />

117 nz = 1<br />

118 varsub[0] = 0<br />

119<br />

120 nz = task.solvewithbasis(1, nz, varsub, w2)<br />

121<br />

122 print ("Solution to B^Tx = w2:")<br />

123<br />

124 for i in range(nz):<br />

125 if basis[varsub[i]] < numcon:<br />

126 print ("xc %s = %s" % (basis[varsub[i]], w2[varsub[i]]))<br />

127 else:<br />

128 print ("x %s = %s" % (basis[varsub[i]] - numcon, w2[varsub[i]]) )<br />

129 except Exception as e:<br />

130 print (e)<br />

131<br />

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

133 main()<br />

In the example above the linear system is solved using the optimal basis for (7.4) and the original<br />

right-hand side of the problem. Thus the solution to the linear system is the optimal solution to the<br />

problem. When running the example program the following output is produced.<br />

basis[0] = 1<br />

Basis variable no 0 is xc1.<br />

basis[1] = 2<br />

Basis variable no 1 is x0.<br />

Solution to Bx = b:<br />

x0 = 2.000000e+00<br />

xc1 = -4.000000e+00<br />

Solution to B^Tx = c:<br />

x1 = -1.000000e+00<br />

x0 = 1.000000e+00<br />

Please note that the ordering of the basis variables is<br />

and thus the basis is given by:<br />

It can be verified that<br />

[<br />

B =<br />

[ ] x<br />

c<br />

1<br />

x 0<br />

[ ] x<br />

c<br />

1<br />

=<br />

x 0<br />

0 1<br />

− 1 1<br />

[ − 4<br />

2<br />

]<br />

]

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

Saved successfully!

Ooh no, something went wrong!