1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
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
52 c = zeros((ng, maxN))
53 for n in arange(minN, maxN + 1):
54 k = find(gencost[:, NCOST] == n)
55 c[k, :n] = gencost[k, (COST + n - 1):COST - 1:-1]
56
57
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
69 if len(c) == 0:
70 f = zeros(Pg.shape)
71 else:
72 f = c[:, :1].flatten()
73 for k in range(1, c.shape[1]):
74 f = f + c[:, k] * Pg**k
75
76 return f
77