Package pypower :: Module makeAy
[hide private]
[frames] | no frames]

Source Code for Module pypower.makeAy

  1  # Copyright (C) 1996-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  """Make the A matrix and RHS for the CCV formulation. 
 18  """ 
 19   
 20  from numpy import array, diff, any, zeros, r_, flatnonzero as find 
 21  #from scipy.sparse import csr_matrix as sparse 
 22  from scipy.sparse import lil_matrix as sparse 
 23   
 24  from idx_cost import MODEL, PW_LINEAR, NCOST, COST 
 25   
 26   
27 -def makeAy(baseMVA, ng, gencost, pgbas, qgbas, ybas):
28 """Make the A matrix and RHS for the CCV formulation. 29 30 Constructs the parameters for linear "basin constraints" on C{Pg}, C{Qg} 31 and C{Y} used by the CCV cost formulation, expressed as:: 32 33 Ay * x <= by 34 35 where C{x} is the vector of optimization variables. The starting index 36 within the C{x} vector for the active, reactive sources and the C{y} 37 variables should be provided in arguments C{pgbas}, C{qgbas}, C{ybas}. 38 The number of generators is C{ng}. 39 40 Assumptions: All generators are in-service. Filter any generators 41 that are offline from the C{gencost} matrix before calling L{makeAy}. 42 Efficiency depends on C{Qg} variables being after C{Pg} variables, and 43 the C{y} variables must be the last variables within the vector C{x} for 44 the dimensions of the resulting C{Ay} to be conformable with C{x}. 45 46 @author: Carlos E. Murillo-Sanchez (PSERC Cornell & Universidad 47 Autonoma de Manizales) 48 @author: Richard Lincoln 49 """ 50 ## find all pwl cost rows in gencost, either real or reactive 51 iycost = find(gencost[:, MODEL] == PW_LINEAR) 52 53 ## this is the number of extra "y" variables needed to model those costs 54 ny = iycost.shape[0] 55 56 if ny == 0: 57 Ay = zeros((0, ybas + ny - 1)) ## TODO: Check size (- 1) 58 by = array([]) 59 return Ay, by 60 61 ## if p(i),p(i+1),c(i),c(i+1) define one of the cost segments, then 62 ## the corresponding constraint on Pg (or Qg) and Y is 63 ## c(i+1) - c(i) 64 ## Y >= c(i) + m * (Pg - p(i)), m = --------------- 65 ## p(i+1) - p(i) 66 ## 67 ## this becomes m * Pg - Y <= m*p(i) - c(i) 68 69 ## Form A matrix. Use two different loops, one for the PG/Qg coefs, 70 ## then another for the y coefs so that everything is filled in the 71 ## same order as the compressed column sparse format used by matlab 72 ## this should be the quickest. 73 74 m = sum( gencost[iycost, NCOST].astype(int) ) ## total number of cost points 75 Ay = sparse((m - ny, ybas + ny - 1)) 76 by = array([]) 77 ## First fill the Pg or Qg coefficients (since their columns come first) 78 ## and the rhs 79 k = 0 80 for i in iycost: 81 ns = gencost[i, NCOST].astype(int) ## # of cost points segments = ns-1 82 p = gencost[i, COST:COST + 2 * ns - 1:2] / baseMVA 83 c = gencost[i, COST + 1:COST + 2 * ns:2] 84 m = diff(c) / diff(p) ## slopes for Pg (or Qg) 85 if any(diff(p) == 0): 86 print 'makeAy: bad x axis data in row ##i of gencost matrix' % i 87 b = m * p[:ns - 1] - c[:ns - 1] ## and rhs 88 by = r_[by, b] 89 if i > ng: 90 sidx = qgbas + (i - ng) - 1 ## this was for a q cost 91 else: 92 sidx = pgbas + i - 1 ## this was for a p cost 93 94 ## FIXME: Bug in SciPy 0.7.2 prevents setting with a sequence 95 # Ay[k:k + ns - 1, sidx] = m 96 for ii, kk in enumerate(range(k, k + ns - 1)): 97 Ay[kk, sidx] = m[ii] 98 99 k = k + ns - 1 100 ## Now fill the y columns with -1's 101 k = 0 102 j = 0 103 for i in iycost: 104 ns = gencost[i, NCOST].astype(int) 105 ## FIXME: Bug in SciPy 0.7.2 prevents setting with a sequence 106 # Ay[k:k + ns - 1, ybas + j - 1] = -ones(ns - 1) 107 for kk in range(k, k + ns - 1): 108 Ay[kk, ybas + j - 1] = -1 109 k = k + ns - 1 110 j = j + 1 111 112 return Ay.tocsr(), by
113