1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
38 m, n = polycost.shape
39
40 pwlcost[:, MODEL] = PW_LINEAR * ones(m)
41
42 pwlcost[:, COST:COST + n] = zeros(pwlcost[:, COST:COST + n].shape)
43
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:
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