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

Source Code for Module pypower.modcost

  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  """Modifies generator costs by shifting or scaling (F or X). 
 18  """ 
 19   
 20  import sys 
 21   
 22  from numpy import zeros, ones, arange, dot, cumsum, flatnonzero as find 
 23   
 24  from idx_cost import MODEL, NCOST, PW_LINEAR, POLYNOMIAL, COST 
 25   
 26   
27 -def modcost(gencost, alpha, modtype='SCALE_F'):
28 """Modifies generator costs by shifting or scaling (F or X). 29 30 For each generator cost F(X) (for real or reactive power) in 31 C{gencost}, this function modifies the cost by scaling or shifting 32 the function by C{alpha}, depending on the value of C{modtype}, and 33 and returns the modified C{gencost}. Rows of C{gencost} can be a mix 34 of polynomial or piecewise linear costs. 35 36 C{modtype} takes one of the 4 possible values (let F_alpha(X) denote the 37 the modified function):: 38 SCALE_F (default) : F_alpha(X) == F(X) * ALPHA 39 SCALE_X : F_alpha(X * ALPHA) == F(X) 40 SHIFT_F : F_alpha(X) == F(X) + ALPHA 41 SHIFT_X : F_alpha(X + ALPHA) == F(X) 42 43 @author: Ray Zimmerman (PSERC Cornell) 44 @author: Richard Lincoln 45 """ 46 gencost = gencost.copy() 47 48 ng, m = gencost.shape 49 if ng != 0: 50 ipwl = find(gencost[:, MODEL] == PW_LINEAR) 51 ipol = find(gencost[:, MODEL] == POLYNOMIAL) 52 c = gencost[ipol, COST:m] 53 54 if modtype == 'SCALE_F': 55 gencost[ipol, COST:m] = alpha * c 56 gencost[ipwl, COST+1:m:2] = alpha * gencost[ipwl, COST + 1:m:2] 57 elif modtype == 'SCALE_X': 58 for k in range(len(ipol)): 59 n = gencost[ipol[k], NCOST].astype(int) 60 for i in range(n): 61 gencost[ipol[k], COST + i] = c[k, i] / alpha**(n - i - 1) 62 gencost[ipwl, COST:m - 1:2] = alpha * gencost[ipwl, COST:m - 1:2] 63 elif modtype == 'SHIFT_F': 64 for k in range(len(ipol)): 65 n = gencost[ipol[k], NCOST].astype(int) 66 gencost[ipol[k], COST + n - 1] = alpha + c[k, n - 1] 67 gencost[ipwl, COST+1:m:2] = alpha + gencost[ipwl, COST + 1:m:2] 68 elif modtype == 'SHIFT_X': 69 for k in range(len(ipol)): 70 n = gencost[ipol[k], NCOST].astype(int) 71 gencost[ipol[k], COST:COST + n] = \ 72 polyshift(c[k, :n].T, alpha).T 73 gencost[ipwl, COST:m - 1:2] = alpha + gencost[ipwl, COST:m - 1:2] 74 else: 75 sys.stderr.write('modcost: "%s" is not a valid modtype\n' % modtype) 76 77 return gencost
78 79
80 -def polyshift(c, a):
81 """Returns the coefficients of a horizontally shifted polynomial. 82 83 C{d = polyshift(c, a)} shifts to the right by C{a}, the polynomial whose 84 coefficients are given in the column vector C{c}. 85 86 Example: For any polynomial with C{n} coefficients in C{c}, and any values 87 for C{x} and shift C{a}, the C{f - f0} should be zero:: 88 x = rand 89 a = rand 90 c = rand(n, 1); 91 f0 = polyval(c, x) 92 f = polyval(polyshift(c, a), x+a) 93 """ 94 n = len(c) 95 d = zeros(c.shape) 96 A = pow(-a * ones(n), arange(n)) 97 b = ones(n) 98 for k in range(n): 99 d[n - k - 1] = dot(b, c[n - k - 1::-1] * A[:n - k]) 100 b = cumsum(b[:n - k - 1]) 101 102 return d
103