Package pypower :: Package t :: Module t_qps_pypower
[hide private]
[frames] | no frames]

Source Code for Module pypower.t.t_qps_pypower

  1  # Copyright (C) 2010-2011 Power System Engineering Research Center (PSERC) 
  2  # Copyright (C) 2011 Richard Lincoln 
  3  # 
  4  # PYPOWER is free software: you can redistribute it and/or modify 
  5  # it under the terms of the GNU General Public License as published 
  6  # by the Free Software Foundation, either version 3 of the License, 
  7  # or (at your option) any later version. 
  8  # 
  9  # PYPOWER is distributed in the hope that it will be useful, 
 10  # but WITHOUT ANY WARRANTY], without even the implied warranty of 
 11  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
 12  # GNU General Public License for more details. 
 13  # 
 14  # You should have received a copy of the GNU General Public License 
 15  # along with PYPOWER. If not, see <http://www.gnu.org/licenses/>. 
 16   
 17  """Tests of C{qps_pypower} QP solvers. 
 18  """ 
 19   
 20  from numpy import array, zeros, shape, Inf 
 21   
 22  from scipy.sparse import csr_matrix as sparse 
 23   
 24  from pypower.ppoption import ppoption 
 25  from pypower.cplex_options import cplex_options 
 26  from pypower.mosek_options import mosek_options 
 27  from pypower.qps_pypower import qps_pypower 
 28   
 29  from pypower.t.t_begin import t_begin 
 30  from pypower.t.t_is import t_is 
 31  from pypower.t.t_end import t_end 
 32  from pypower.t.t_ok import t_ok 
 33  from pypower.t.t_skip import t_skip 
 34   
 35   
36 -def t_qps_pypower(quiet=False):
37 """Tests of C{qps_pypower} QP solvers. 38 39 @author: Ray Zimmerman (PSERC Cornell) 40 @author: Richard Lincoln 41 """ 42 algs = [200, 250, 400, 500, 600] 43 names = ['PIPS', 'sc-PIPS', 'IPOPT', 'CPLEX', 'MOSEK'] 44 check = [None, None, 'ipopt', 'cplex', 'mosek'] 45 46 n = 36 47 t_begin(n * len(algs), quiet) 48 49 for k in range(len(algs)): 50 if check[k] is not None and not have_fcn(check[k]): 51 t_skip(n, '%s not installed' % names[k]) 52 else: 53 opt = {'verbose': 0, 'alg': algs[k]} 54 55 if names[k] == 'PIPS' or names[k] == 'sc-PIPS': 56 opt['pips_opt'] = {} 57 opt['pips_opt']['comptol'] = 1e-8 58 if names[k] == 'CPLEX': 59 # alg = 0 ## default uses barrier method with NaN bug in lower lim multipliers 60 alg = 2 ## use dual simplex 61 ppopt = ppoption(CPLEX_LPMETHOD = alg, CPLEX_QPMETHOD = min([4, alg])) 62 opt['cplex_opt'] = cplex_options([], ppopt) 63 64 if names[k] == 'MOSEK': 65 # alg = 5 ## use dual simplex 66 ppopt = ppoption() 67 # ppopt = ppoption(ppopt, MOSEK_LP_ALG = alg) 68 ppopt = ppoption(ppopt, MOSEK_GAP_TOL=1e-9) 69 opt['mosek_opt'] = mosek_options([], ppopt) 70 71 t = '%s - 3-d LP : ' % names[k] 72 ## example from 'doc linprog' 73 c = array([-5, -4, -6], float) 74 A = sparse([[1, -1, 1], 75 [3, 2, 4], 76 [3, 2, 0]], dtype=float) 77 l = None 78 u = array([20, 42, 30], float) 79 xmin = array([0, 0, 0], float) 80 x0 = None 81 x, f, s, _, lam = qps_pypower(None, c, A, l, u, xmin, None, None, opt) 82 t_is(s, 1, 12, [t, 'success']) 83 t_is(x, [0, 15, 3], 6, [t, 'x']) 84 t_is(f, -78, 6, [t, 'f']) 85 t_is(lam['mu_l'], [0, 0, 0], 13, [t, 'lam.mu_l']) 86 t_is(lam['mu_u'], [0, 1.5, 0.5], 9, [t, 'lam.mu_u']) 87 t_is(lam['lower'], [1, 0, 0], 9, [t, 'lam.lower']) 88 t_is(lam['upper'], zeros(shape(x)), 13, [t, 'lam.upper']) 89 90 t = '%s - unconstrained 3-d quadratic : ' % names[k] 91 ## from http://www.akiti.ca/QuadProgEx0Constr.html 92 H = sparse([ 93 [ 5, -2, -1], 94 [-2, 4, 3], 95 [-1, 3, 5] 96 ], dtype=float) 97 c = array([2, -35, -47], float) 98 x0 = array([0, 0, 0], float) 99 x, f, s, _, lam = qps_pypower(H, c, opt=opt) 100 t_is(s, 1, 12, [t, 'success']) 101 t_is(x, [3, 5, 7], 8, [t, 'x']) 102 t_is(f, -249, 13, [t, 'f']) 103 t_ok(len(lam['mu_l']) == 0, [t, 'lam.mu_l']) 104 t_ok(len(lam['mu_u']) == 0, [t, 'lam.mu_u']) 105 t_is(lam['lower'], zeros(shape(x)), 13, [t, 'lam.lower']) 106 t_is(lam['upper'], zeros(shape(x)), 13, [t, 'lam.upper']) 107 108 t = '%s - constrained 2-d QP : ' % names[k] 109 ## example from 'doc quadprog' 110 H = sparse([[ 1, -1], 111 [-1, 2]], dtype=float) 112 c = array([-2, -6], float) 113 A = sparse([[ 1, 1], 114 [-1, 2], 115 [ 2, 1]], dtype=float) 116 l = None 117 u = array([2, 2, 3], float) 118 xmin = array([0, 0]) 119 x0 = None 120 x, f, s, _, lam = qps_pypower(H, c, A, l, u, xmin, None, x0, opt) 121 t_is(s, 1, 12, [t, 'success']) 122 t_is(x, array([2., 4.]) / 3, 7, [t, 'x']) 123 t_is(f, -74. / 9, 6, [t, 'f']) 124 t_is(lam['mu_l'], [0., 0., 0.], 13, [t, 'lam.mu_l']) 125 t_is(lam['mu_u'], array([28., 4., 0.]) / 9, 7, [t, 'lam.mu_u']) 126 t_is(lam['lower'], zeros(shape(x)), 8, [t, 'lam.lower']) 127 t_is(lam['upper'], zeros(shape(x)), 13, [t, 'lam.upper']) 128 129 t = '%s - constrained 4-d QP : ' % names[k] 130 ## from http://www.jmu.edu/docs/sasdoc/sashtml/iml/chap8/sect12.htm 131 H = sparse([[1003.1, 4.3, 6.3, 5.9], 132 [4.3, 2.2, 2.1, 3.9], 133 [6.3, 2.1, 3.5, 4.8], 134 [5.9, 3.9, 4.8, 10.0]]) 135 c = zeros(4) 136 A = sparse([[ 1, 1, 1, 1], 137 [0.17, 0.11, 0.10, 0.18]]) 138 l = array([1, 0.10]) 139 u = array([1, Inf]) 140 xmin = zeros(4) 141 x0 = array([1, 0, 0, 1], float) 142 x, f, s, _, lam = qps_pypower(H, c, A, l, u, xmin, None, x0, opt) 143 t_is(s, 1, 12, [t, 'success']) 144 t_is(x, array([0, 2.8, 0.2, 0]) / 3, 5, [t, 'x']) 145 t_is(f, 3.29 / 3, 6, [t, 'f']) 146 t_is(lam['mu_l'], array([6.58, 0]) / 3, 6, [t, 'lam.mu_l']) 147 t_is(lam['mu_u'], [0, 0], 13, [t, 'lam.mu_u']) 148 t_is(lam['lower'], [2.24, 0, 0, 1.7667], 4, [t, 'lam.lower']) 149 t_is(lam['upper'], zeros(shape(x)), 13, [t, 'lam.upper']) 150 151 t = '%s - (dict) constrained 4-d QP : ' % names[k] 152 p = {'H': H, 'A': A, 'l': l, 'u': u, 'xmin': xmin, 'x0': x0, 'opt': opt} 153 x, f, s, _, lam = qps_pypower(p) 154 t_is(s, 1, 12, [t, 'success']) 155 t_is(x, array([0, 2.8, 0.2, 0]) / 3, 5, [t, 'x']) 156 t_is(f, 3.29 / 3, 6, [t, 'f']) 157 t_is(lam['mu_l'], array([6.58, 0]) / 3, 6, [t, 'lam.mu_l']) 158 t_is(lam['mu_u'], [0, 0], 13, [t, 'lam.mu_u']) 159 t_is(lam['lower'], [2.24, 0, 0, 1.7667], 4, [t, 'lam.lower']) 160 t_is(lam['upper'], zeros(shape(x)), 13, [t, 'lam.upper']) 161 162 t = '%s - infeasible LP : ' % names[k] 163 p = {'A': sparse([1, 1]), 'c': array([1, 1]), 'u': array([-1]), 164 'xmin': array([0, 0]), 'opt': opt} 165 x, f, s, _, lam = qps_pypower(p) 166 t_ok(s <= 0, [t, 'no success']) 167 168 t_end()
169 170
171 -def have_fcn(name):
172 try: 173 __import__(name) 174 return True 175 except ImportError: 176 return False
177 178 179 if __name__ == '__main__': 180 t_qps_pypower(quiet=False) 181