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

Source Code for Module pypower.poly2pwl

 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  """Converts polynomial cost variable to piecewise linear. 
18  """ 
19   
20  from numpy import ones, zeros, r_ 
21   
22  from idx_cost import MODEL, COST, NCOST, PW_LINEAR 
23   
24  from totcost import totcost 
25   
26   
27 -def poly2pwl(polycost, Pmin, Pmax, npts):
28 """Converts polynomial cost variable to piecewise linear. 29 30 Converts the polynomial cost variable C{polycost} into a piece-wise linear 31 cost by evaluating at zero and then at C{npts} evenly spaced points between 32 C{Pmin} and C{Pmax}. If C{Pmin <= 0} (such as for reactive power, where 33 C{P} really means C{Q}) it just uses C{npts} evenly spaced points between 34 C{Pmin} and C{Pmax}. 35 """ 36 pwlcost = polycost 37 ## size of piece being changed 38 m, n = polycost.shape 39 ## change cost model 40 pwlcost[:, MODEL] = PW_LINEAR * ones(m) 41 ## zero out old data 42 pwlcost[:, COST:COST + n] = zeros(pwlcost[:, COST:COST + n].shape) 43 ## change number of data points 44 pwlcost[:, NCOST] = npts * ones(m) 45 46 for i in range(m): 47 if Pmin[i] == 0: 48 step = (Pmax[i] - Pmin[i]) / (npts - 1) 49 xx = range(Pmin[i], step, Pmax[i]) 50 elif Pmin[i] > 0: 51 step = (Pmax[i] - Pmin[i]) / (npts - 2) 52 xx = r_[0, range(Pmin[i], step, Pmax[i])] 53 elif Pmin[i] < 0 & Pmax[i] > 0: ## for when P really means Q 54 step = (Pmax[i] - Pmin[i]) / (npts - 1) 55 xx = range(Pmin[i], step, Pmax[i]) 56 yy = totcost(polycost[i, :], xx) 57 pwlcost[i, COST:2:(COST + 2*(npts-1) )] = xx 58 pwlcost[i, (COST+1):2:(COST + 2*(npts-1) + 1)] = yy 59 60 return pwlcost
61