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

Source Code for Module pypower.polycost

 1  # Copyright (C) 2009-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  """Evaluates polynomial generator cost & derivatives. 
18  """ 
19   
20  import sys 
21   
22  from numpy import zeros, arange, flatnonzero as find 
23   
24  from idx_cost import MODEL, NCOST, PW_LINEAR, COST 
25   
26   
27 -def polycost(gencost, Pg, der=0):
28 """Evaluates polynomial generator cost & derivatives. 29 30 C{f = polycost(gencost, Pg)} returns the vector of costs evaluated at C{Pg} 31 32 C{df = polycost(gencost, Pg, 1)} returns the vector of first derivatives 33 of costs evaluated at C{Pg} 34 35 C{d2f = polycost(gencost, Pg, 2)} returns the vector of second derivatives 36 of costs evaluated at C{Pg} 37 38 C{gencost} must contain only polynomial costs 39 C{Pg} is in MW, not p.u. (works for C{Qg} too) 40 41 @author: Ray Zimmerman (PSERC Cornell) 42 @author: Richard Lincoln 43 """ 44 if any(gencost[:, MODEL] == PW_LINEAR): 45 sys.stderr.write('polycost: all costs must be polynomial\n') 46 47 ng = len(Pg) 48 maxN = max( gencost[:, NCOST].astype(int) ) 49 minN = min( gencost[:, NCOST].astype(int) ) 50 51 ## form coefficient matrix where 1st column is constant term, 2nd linear, etc. 52 c = zeros((ng, maxN)) 53 for n in arange(minN, maxN + 1): 54 k = find(gencost[:, NCOST] == n) ## cost with n coefficients 55 c[k, :n] = gencost[k, (COST + n - 1):COST - 1:-1] 56 57 ## do derivatives 58 for d in range(1, der + 1): 59 if c.shape[1] >= 2: 60 c = c[:, 1:maxN - d + 1] 61 else: 62 c = zeros((ng, 1)) 63 break 64 65 for k in range(2, maxN - d + 1): 66 c[:, k-1] = c[:, k-1] * k 67 68 ## evaluate polynomial 69 if len(c) == 0: 70 f = zeros(Pg.shape) 71 else: 72 f = c[:, :1].flatten() ## constant term 73 for k in range(1, c.shape[1]): 74 f = f + c[:, k] * Pg**k 75 76 return f
77