1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
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