def jacobian(self, var): # not *quite* the jacobian, but nearly. if var is self.x: # each x_i: exp(x_i) / sum(exp(x)). x = value(self.x) return transpose(exp(x)) / sum(exp(x)) elif var is self.t: return -eye(rows(self.t)) else: raise OptimizationError("illegal jacobian")
def jacobian(self, var): # not *quite* the jacobian, but nearly. if var is self.x: # each x_i: exp(x_i) / sum(exp(x)). x = value(self.x) return transpose(exp(x)) / sum(exp(x)) elif var is self.t: return -eye(rows(self.t)) else: raise OptimizationError('illegal jacobian')
def hessianz(self, firstvar, secondvar, z): if firstvar is secondvar is self.x: # Want diag(e^x)/sum(e^x) - e^x/sum(e^x)*transpose(e^x/sum(e^x)). x = value(self.x) a = sum(exp(x)) return z * (diag(exp(x)) / a - exp(x) / a * transpose(exp(x)) / a) elif firstvar is secondvar is self.t: return zeros(rows(self.t)) elif firstvar is self.x and secondvar is self.t: return zeros(rows(self.x), rows(self.t)) elif firstvar is self.t and secondvar is self.x: return zeros(rows(self.t), rows(self.x)) else: raise OptimizationError('illegal hessian')
def hessianz(self, firstvar, secondvar, z): if firstvar is secondvar is self.x: # Want diag(e^x)/sum(e^x) - e^x/sum(e^x)*transpose(e^x/sum(e^x)). x = value(self.x) a = sum(exp(x)) return z * (diag(exp(x)) / a - exp(x) / a * transpose(exp(x)) / a) elif firstvar is secondvar is self.t: return zeros(rows(self.t)) elif firstvar is self.x and secondvar is self.t: return zeros(rows(self.x), rows(self.t)) elif firstvar is self.t and secondvar is self.x: return zeros(rows(self.t), rows(self.x)) else: raise OptimizationError("illegal hessian")
def classifier2(Y, soft=False): M = Y.size[0] # K = Y*X' / sigma K = matrix(0.0, (width, M)) blas.gemm(X, Y, K, transB='T', alpha=1.0 / sigma, m=width) # c[i] = ||Yi||^2 / sigma ones = matrix(1.0, (max(width, n, M), 1)) c = Y**2 * ones[:n] blas.scal(1.0 / sigma, c) # Kij := Kij - 0.5 * (ci + aj) # = || yi - xj ||^2 / (2*sigma) blas.ger(ones[:width], c, K, alpha=-0.5) blas.ger(a[:width], ones[:M], K, alpha=-0.5) # Kij = exp(Kij) K = exp(K) # complete K lapack.potrs(L11, K) K = matrix([K, matrix(0., (N - width, M))], (N, M)) chompack.trsm(Lc, K, trans='N') chompack.trsm(Lc, K, trans='T') x = matrix(b, (M, 1)) blas.gemv(K, z, x, trans='T', beta=1.0) if soft: return x else: return matrix([2 * (xk > 0.0) - 1 for xk in x])
def classifier2(Y, soft=False): M = Y.size[0] # K = Y*X' / sigma K = matrix(theta, (width, M)) blas.gemm(X, Y, K, transB='T', alpha=1.0 / sigma, beta=-1.0, m=width) K = exp(K) K = div(K - K**-1, K + K**-1) # complete K lapack.potrs(L11, K) K = matrix([K, matrix(0., (N - width, M))], (N, M)) chompack.trsm(Lc, K, trans='N') chompack.trsm(Lc, K, trans='T') x = matrix(b, (M, 1)) blas.gemv(K, z, x, trans='T', beta=1.0) if soft: return x else: return matrix([2 * (xk > 0.0) - 1 for xk in x])
def Gycall(self): system.DAE.y = matrix(system.DAE.y) U = exp(system.DAE.y[system.Bus.a] * 1j) V = mul(system.DAE.y[system.Bus.v] + 0j, U) I = self.Y * V nb = len(system.Bus.a) diagU = spmatrix(U, system.Bus.a, system.Bus.a, (nb, nb), 'z') diagV = spmatrix(V, system.Bus.a, system.Bus.a, (nb, nb), 'z') diagI = spmatrix(I, system.Bus.a, system.Bus.a, (nb, nb), 'z') dS = self.Y * diagU dS = diagV * dS.H.T dS += diagI.H.T * diagU dR = diagI dR = dR - self.Y * diagV dR = diagV.H.T * dR # system.DAE.Gy = matrix(0.0, (system.DAE.ny, system.DAE.ny)) # # system.DAE._list2matrix() # system.DAE.Gy = spmatrix(dR.imag().V, dR.imag().I, dR.imag().J, (system.DAE.ny, system.DAE.ny)) \ # + spmatrix(dR.real().V, dR.real().I, dR.real().J+system.Bus.n, (system.DAE.ny, system.DAE.ny)) \ # + spmatrix(dS.real().V, dS.real().I+system.Bus.n, dS.real().J, (system.DAE.ny, system.DAE.ny)) \ # + spmatrix(dS.imag().V, dS.imag().I+system.Bus.n, dS.imag().J+system.Bus.n, (system.DAE.ny, system.DAE.ny)) Gy = sparse([[dR.imag(), dR.real()], [dS.real(), dS.imag()]]) # system.DAE.Gy = zeros([system.DAE.ny,system.DAE.ny]) system.DAE.Gy = spmatrix(Gy.V, Gy.I, Gy.J, (system.DAE.ny, system.DAE.ny)) system.DAE.Gy = matrix(system.DAE.Gy)
def classifier(Y, soft=False): M = Y.size[0] # K = Y*X' / sigma - theta K = matrix(theta, (M, Nr)) blas.gemm(Y, Xr, K, transB='T', alpha=1.0 / sigma, beta=-1.0) K = exp(K) x = div(K - K**-1, K + K**-1) * zr + b if soft: return x else: return matrix([2 * (xk > 0.0) - 1 for xk in x])
def setx0(self): if self.n == 0: return vg = system.DAE.y[self.bv] vf = mul(self.u, system.Syn6.vf0[self.a]) vrmax = mul(self.u, self.vrmax) vrmin = mul(self.u, self.vrmin) # 检查参数 for i in range(self.n): if self.Te[i] <= 0: self.Te[i] = 1 print('<%i> Te不能小于等于0,设置Te = 1 [s]' % i) if self.Tr[i] <= 0: self.Tr[i] = 0.001 print('<%i> Tr不能小于等于0,设置Te = 0.001 [s]' % i) if self.K0[i] <= 0: self.K0[i] = 400 print('<%i> K0不能小于等于0,设置K0 = 400 ' % i) if self.T1[i] <= 0: self.T1[i] = 0.1 print('<%i> T1不能小于等于0,设置T1 = 0.1 [s]' % i) if self.T4[i] <= 0: self.T4[i] = 0.01 print('<%i> T4不能小于等于0,设置T4 = 0.01 [s]' % i) # Ce = -vf - mul(mul(self.Ae, exp(mul(self.Be, vf))), vf) K1 = mul(self.K0, div(self.T2, self.T1)) K2 = self.K0 - K1 K3 = div(self.T3, self.T4) K4 = 1 - K3 system.DAE.x[self.vm] = mul(self.u, vg) self.vref0 = vg - div(Ce, self.K0) system.DAE.x[self.vr1] = mul(self.u, mul(K2, self.vref0 - vg)) system.DAE.x[self.vr2] = mul(self.u, mul(K4, self.vref0 - vg)) system.DAE.x[self.vf] = vf system.DAE.y[self.vref] = mul(self.u, self.vref0) vr = mul(self.u, mul(self.K0, system.DAE.x[self.vr2])) + mul( K3, mul(K1, self.vref0 - vg) + system.DAE.x[self.vr1]) for i in range(self.n): if vr[i] > self.vrmax[i]: print('Warn: vr1超出最大值vrmax') if vr[i] < self.vrmin[i]: print('Warn: vr1小于最小值vrmin') system.Syn6.vf0[self.a] = 0
def Fxcall(self): if self.n == 0: return vg = system.DAE.y[self.bv] vrmax = mul(self.u, self.vrmax) vrmin = mul(self.u, self.vrmin) system.DAE.Gx = system.DAE.Gx + spmatrix( self.u, self.vfd, self.vf, (system.DAE.ny, system.DAE.nx)) system.DAE.Fx = system.DAE.Fx - spmatrix( div(matrix(1.0, (self.n, 1)), self.Tr), self.vm, self.vm, (system.DAE.nx, system.DAE.nx)) system.DAE.Fy = system.DAE.Fy + spmatrix(div( self.u, self.Tr), self.vm, self.bv, (system.DAE.nx, system.DAE.ny)) vm = system.DAE.x[self.vm] vr1 = system.DAE.x[self.vr1] vr2 = system.DAE.x[self.vr2] vf = system.DAE.x[self.vf] vref = system.DAE.y[self.vref] K1 = div(mul(self.K0, self.T2), self.T1) K2 = self.K0 - K1 K3 = div(self.T4, self.T3) K4 = matrix(1, (self.n, 1)) - K3 vr = mul(self.K0, vr2) + mul(K3, mul(K1, vref - vm) + vr1) Se = mul(self.Ae, exp(mul(self.Be, abs(vf)))) Se = Se + mul(mul(Se, self.Ae), vf) z = matrix(0, (self.n, 1)) for i in range(self.n): if vr[i] < vrmax[i] and vr[i] > vrmin[i]: z[i] = 1 system.DAE.Fx = system.DAE.Fx \ - spmatrix(div(K2, self.T1), self.vr1, self.vm, (system.DAE.nx, system.DAE.nx)) \ - spmatrix(div(matrix(1.0, (self.n, 1)), self.T1), self.vr1, self.vr1, (system.DAE.nx, system.DAE.nx)) \ - spmatrix(div(mul(K4, K1), mul(self.K0, self.T3)), self.vr2, self.vm, (system.DAE.nx, system.DAE.nx)) \ + spmatrix(div(K4, mul(self.T3, self.K0)), self.vr2, self.vr1, (system.DAE.nx, system.DAE.nx)) \ - spmatrix(div(matrix(1.0, (self.n, 1)), self.T3), self.vr2, self.vr2, (system.DAE.nx, system.DAE.nx)) \ - spmatrix(div(1+Se, self.Te), self.vf, self.vf, (system.DAE.nx, system.DAE.nx)) \ + spmatrix(div(mul(z, K3), self.Te), self.vf, self.vr1, (system.DAE.nx, system.DAE.nx)) \ - spmatrix(div(mul(mul(z, K3), K1), self.Te), self.vf, self.vm, (system.DAE.nx, system.DAE.nx)) \ + spmatrix(div(mul(mul(z, self.u), self.K0), self.Te), self.vf, self.vr2, (system.DAE.nx, system.DAE.nx)) system.DAE.Fy = system.DAE.Fy \ + spmatrix(div(K2, self.T1), self.vr1, self.vref, (system.DAE.nx, system.DAE.ny)) \ + spmatrix(div(mul(K4, K1), mul(self.K0, self.T3)), self.vr2, self.vref, (system.DAE.nx, system.DAE.ny)) \ + spmatrix(div(mul(mul(z, K3), K1), self.Te), self.vf, self.vref, (system.DAE.nx, system.DAE.ny))
def Fgp(x = None, z = None): if x is None: return mnl, matrix(0.0, (n,1)) f = matrix(0.0, (mnl+1,1)) Df = matrix(0.0, (mnl+1,n)) # y = F*x+g blas.copy(g, y) base.gemv(F, x, y, beta=1.0) if z is not None: H = matrix(0.0, (n,n)) for i, start, stop in ind: # yi := exp(yi) = exp(Fi*x+gi) ymax = max(y[start:stop]) y[start:stop] = base.exp(y[start:stop] - ymax) # fi = log sum yi = log sum exp(Fi*x+gi) ysum = blas.asum(y, n=stop-start, offset=start) f[i] = ymax + math.log(ysum) # yi := yi / sum(yi) = exp(Fi*x+gi) / sum(exp(Fi*x+gi)) blas.scal(1.0/ysum, y, n=stop-start, offset=start) # gradfi := Fi' * yi # = Fi' * exp(Fi*x+gi) / sum(exp(Fi*x+gi)) base.gemv(F, y, Df, trans='T', m=stop-start, incy=mnl+1, offsetA=start, offsetx=start, offsety=i) if z is not None: # Hi = Fi' * (diag(yi) - yi*yi') * Fi # = Fisc' * Fisc # where # Fisc = diag(yi)^1/2 * (I - 1*yi') * Fi # = diag(yi)^1/2 * (Fi - 1*gradfi') Fsc[:K[i], :] = F[start:stop, :] for k in range(start,stop): blas.axpy(Df, Fsc, n=n, alpha=-1.0, incx=mnl+1, incy=Fsc.size[0], offsetx=i, offsety=k-start) blas.scal(math.sqrt(y[k]), Fsc, inc=Fsc.size[0], offset=k-start) # H += z[i]*Hi = z[i] * Fisc' * Fisc blas.syrk(Fsc, H, trans='T', k=stop-start, alpha=z[i], beta=1.0) if z is None: return f, Df return f, Df, H
def flow(self, y): if y is not None: system.DAE.y = matrix(y) if self.n == 0: return chrg = mul(matrix(self.u), 0.5 * matrix(self.b)) y = div(matrix(self.u) + 0j, matrix(matrix(self.r) + 1j * matrix(self.x))) ts = exp(matrix(self.theta) * math.pi / 180 * 1j) tps = mul(matrix(self.tap_ratio), ts) tpj = tps.H.T ts2 = mul(tps, tpj) Vf = mul(system.DAE.y[self.vf], exp(system.DAE.y[self.af]*1j)) Vt = mul(system.DAE.y[self.vt], exp(system.DAE.y[self.at] * 1j)) I1 = div(mul(Vf, y+1j*chrg),ts2)-div(mul(Vt, y), tpj) I2 = mul(Vt, y + 1j * chrg) - div(mul(Vf, y), tps) MWs = mul(Vf, I1.H.T) MWr = mul(Vt, I2.H.T) return [MWs, MWr]
def setx0(self): vg = system.DAE.y[self.bv] vf = mul(self.u, system.Syn6.vf0[self.a]) vrmax = mul(self.u, self.vrmax) vrmin = mul(self.u, self.vrmin) # 检查参数 for i in range(self.n): if self.Te[i] == 0: self.Te[i] = 1 print('<%i> Te不能小于等于0,设置Te = 1 [s]' % i) if self.Tr[i] <= 0: self.Tr[i] = 0.001 print('<%i> Tr不能小于等于0,设置Te = 0.001 [s]' % i) if self.Ke[i] <= 0: self.Ke[i] = 1 print('<%i> Ke不能小于等于0,设置Ke = 1 ' % i) if self.Tf[i] <= 0: self.Tf[i] = 0.1 print('<%i> T1不能小于等于0,设置T1 = 0.1 [s]' % i) if self.Ta[i] <= 0: self.Ta[i] = 0.1 print('<%i> T4不能小于等于0,设置T4 = 0.1 [s]' % i) # Ce = mul(self.Ke, vf) + mul(mul(self.Ae, exp(mul(self.Be, abs(vf)))), vf) system.DAE.x[self.vm] = mul(self.u, vg) self.vref0 = div(Ce, self.Ka) + vg system.DAE.x[self.vr1] = Ce system.DAE.x[self.vr2] = div(mul(-self.Kf, vf), self.Tf) system.DAE.x[self.vf] = vf system.DAE.y[self.vref] = mul(self.u, self.vref0) for i in range(self.n): if Ce[i] > self.vrmax[i]: print('Warn: vr1超出最大值vrmax') if Ce[i] < self.vrmin[i]: print('Warn: vr1小于最小值vrmin') for i in range(self.n): if self.u[i] == 1: system.Syn6.vf0[self.a[i]] = 0
def gcall(self): system.DAE.y = matrix(system.DAE.y) zeros = [0.0] * system.DAE.ny system.DAE.g = zeros system.DAE.g = matrix(system.DAE.g) Vn = exp(system.DAE.y[system.Bus.a] * 1j) Vc = mul(system.DAE.y[system.Bus.v] + 0j, Vn) Ic = self.Y * Vc S = mul(Vc, Ic.H.T) self.p = S.real() self.q = S.imag() for i in range(system.Bus.n): system.DAE.g[i] = self.p[i] system.DAE.g[i+system.Bus.n] = self.q[i]
def classifier(Y, soft=False): M = Y.size[0] # K = Y*X' / sigma K = matrix(0.0, (M, Nr)) blas.gemm(Y, Xr, K, transB='T', alpha=1.0 / sigma) # c[i] = ||Yi||^2 / sigma ones = matrix(1.0, (max([M, Nr, n]), 1)) c = Y**2 * ones[:n] blas.scal(1.0 / sigma, c) # Kij := Kij - 0.5 * (ci + aj) # = || yi - xj ||^2 / (2*sigma) blas.ger(c, ones, K, alpha=-0.5) blas.ger(ones, a[sv], K, alpha=-0.5) x = exp(K) * zr + b if soft: return x else: return matrix([2 * (xk > 0.0) - 1 for xk in x])
def fcall(self): if self.n == 0: return vg = system.DAE.y[self.bv] vrmax = mul(self.u, self.vrmax) vrmin = mul(self.u, self.vrmin) vm = system.DAE.x[self.vm] vr1 = system.DAE.x[self.vr1] vr2 = system.DAE.x[self.vr2] vf = system.DAE.x[self.vf] vref = system.DAE.y[self.vref] system.DAE.f[self.vm] = div(mul(self.u, vg - system.DAE.x[self.vm]), self.Tr) K1 = div(mul(self.K0, self.T2), self.T1) K2 = self.K0 - K1 K3 = div(self.T4, self.T3) K4 = matrix(1, (self.n, 1)) - K3 vr = mul(self.K0, vr2) + mul(K3, mul(K1, vref - vm) + vr1) system.DAE.f[self.vr1] = div(mul(self.u, mul(K2, vref - vm) - vr1), self.T1) system.DAE.f[self.vr2] = div( mul(self.u, mul(K4, vr1 + mul(K1, vref - vm)) - mul(self.K0, vr2)), mul(self.T3, self.K0)) # hard limit for i in range(self.n): vr[i] = min(vr[i], vrmax[i]) vr[i] = max(vr[i], vrmin[i]) Se = mul(self.Ae, exp(mul(self.Be, abs(vf)))) system.DAE.f[self.vf] = div(mul(self.u, -vf + vr - mul(Se, vf)), self.Te)
def Fxcall(self): vg = system.DAE.y[self.bv] vrmax = mul(self.u, self.vrmax) vrmin = mul(self.u, self.vrmin) system.DAE.Gx = system.DAE.Gx + spmatrix( self.u, self.vfd, self.vf, (system.DAE.ny, system.DAE.nx)) system.DAE.Fx = system.DAE.Fx - spmatrix( div(matrix(1.0, (self.n, 1)), self.Tr), self.vm, self.vm, (system.DAE.nx, system.DAE.nx)) system.DAE.Fy = system.DAE.Fy + spmatrix(div( self.u, self.Tr), self.vm, self.bv, (system.DAE.nx, system.DAE.ny)) vr1 = system.DAE.x[self.vr1] vf = system.DAE.x[self.vf] Ka = mul(self.u, self.Ka) Kf = mul(self.u, self.Kf) K5 = div(Kf, self.Tf) Se = mul(self.Ae, exp(mul(self.Be, abs(vf)))) Se = Se + mul(mul(Se, self.Be), vf) z = matrix(0, (self.n, 1)) for i in range(self.n): if vr1[i] < vrmax[i] and vr1[i] > vrmin[i]: z[i] = 1 system.DAE.Fx = system.DAE.Fx \ - spmatrix(div(matrix(1.0, (self.n, 1)), self.Tf), self.vr2, self.vr2, (system.DAE.nx, system.DAE.nx)) \ - spmatrix(div(K5, self.Tf), self.vr2, self.vf, (system.DAE.nx, system.DAE.nx)) \ - spmatrix(div(self.Ke+Se, self.Te), self.vf, self.vf, (system.DAE.nx, system.DAE.nx)) \ + spmatrix(div(z, self.Te), self.vf, self.vr1, (system.DAE.nx, system.DAE.nx)) \ - spmatrix(div(mul(z, Ka), self.Ta), self.vr1, self.vm, (system.DAE.nx, system.DAE.nx)) \ - spmatrix(div(matrix(1.0, (self.n, 1)), self.Ta), self.vr1, self.vr1, (system.DAE.nx, system.DAE.nx)) \ - spmatrix(div(mul(z, Ka), self.Ta), self.vr1, self.vr2, (system.DAE.nx, system.DAE.nx)) \ - spmatrix(div(mul(mul(z, K5), Ka), self.Ta), self.vr1, self.vf, (system.DAE.nx, system.DAE.nx)) system.DAE.Fy = system.DAE.Fy \ + spmatrix(div(mul(z, Ka), self.Ta), self.vr1, self.vref, (system.DAE.nx, system.DAE.ny))
def fcall(self): vg = system.DAE.y[self.bv] vrmax = mul(self.u, self.vrmax) vrmin = mul(self.u, self.vrmin) vm = system.DAE.x[self.vm] vr1 = system.DAE.x[self.vr1] vr2 = system.DAE.x[self.vr2] vf = system.DAE.x[self.vf] vref = system.DAE.y[self.vref] system.DAE.f[self.vm] = div(mul(self.u, vg - system.DAE.x[self.vm]), self.Tr) # K5 = div(self.Kf, self.Tf) system.DAE.f[self.vr1] = div( mul(self.u, mul(self.Ka, vref - vm - vr2 - mul(K5, vf)) - vr1), self.Ta) system.DAE.f[self.vr2] = div(mul(-self.u, mul(K5, vf) + vr2), self.Tf) # non-windup limit for i in range(self.n): if vr1[i] >= vrmax[i] and system.DAE.f[self.vr1[i]] > 0: system.DAE.f[self.vr1[i]] = 0 if vr1[i] <= vrmin[i] and system.DAE.f[self.vr1[i]] < 0: system.DAE.f[self.vr1[i]] = 0 for i in range(self.n): vr1[i] = min(vr1[i], vrmax[i]) vr1[i] = max(vr1[i], vrmin[i]) system.DAE.x[self.vr1] = mul(self.u, vr1) Se = mul(self.Ae, exp(mul(self.Be, abs(vf)))) system.DAE.f[self.vf] = div( mul(self.u, vr1 - mul(self.Ke, vf) - mul(Se, vf)), self.Te)
def gcall(self): zeros = [0] * system.Bus.n Vn = zeros[:] Vc = zeros[:] for item1, item2 in zip(system.Bus.a, system.Bus.v): Vn[item1] = exp(system.DAE.y[item1] * 1j) Vc[item1] = (system.DAE.y[item2] + 0j) * Vn[item1] # system.DAE.y = matrix(system.DAE.y) # Vn = exp(system.DAE.y[system.Bus.a] * 1j) # Vc = mul(system.DAE.y[system.Bus.v] + 0j, Vn) Vc = matrix(Vc) Ic = self.Y * Vc S = mul(Vc, Ic.H.T) self.p = S.real() self.q = S.imag() for i in range(system.Bus.n): system.DAE.g[i] = self.p[i] system.DAE.g[i + system.Bus.n] = self.q[i]
def Fgp(x=None, z=None): if x is None: return mnl, matrix(0.0, (n, 1)) f = matrix(0.0, (mnl + 1, 1)) Df = matrix(0.0, (mnl + 1, n)) # y = F*x+g blas.copy(g, y) base.gemv(F, x, y, beta=1.0) if z is not None: H = matrix(0.0, (n, n)) for i, start, stop in ind: # yi := exp(yi) = exp(Fi*x+gi) ymax = max(y[start:stop]) y[start:stop] = base.exp(y[start:stop] - ymax) # fi = log sum yi = log sum exp(Fi*x+gi) ysum = blas.asum(y, n=stop - start, offset=start) f[i] = ymax + math.log(ysum) # yi := yi / sum(yi) = exp(Fi*x+gi) / sum(exp(Fi*x+gi)) blas.scal(1.0 / ysum, y, n=stop - start, offset=start) # gradfi := Fi' * yi # = Fi' * exp(Fi*x+gi) / sum(exp(Fi*x+gi)) base.gemv(F, y, Df, trans='T', m=stop - start, incy=mnl + 1, offsetA=start, offsetx=start, offsety=i) if z is not None: # Hi = Fi' * (diag(yi) - yi*yi') * Fi # = Fisc' * Fisc # where # Fisc = diag(yi)^1/2 * (I - 1*yi') * Fi # = diag(yi)^1/2 * (Fi - 1*gradfi') Fsc[:K[i], :] = F[start:stop, :] for k in range(start, stop): blas.axpy(Df, Fsc, n=n, alpha=-1.0, incx=mnl + 1, incy=Fsc.size[0], offsetx=i, offsety=k - start) blas.scal(math.sqrt(y[k]), Fsc, inc=Fsc.size[0], offset=k - start) # H += z[i]*Hi = z[i] * Fisc' * Fisc blas.syrk(Fsc, H, trans='T', k=stop - start, alpha=z[i], beta=1.0) if z is None: return f, Df return f, Df, H
def build_y(self): if self.n == 0: return for i in range(len(self.tap_ratio)): if self.tap_ratio[i] == 0: self.tap_ratio[i] = 1 nb = system.Bus.n # 处理传输线数据和生成导纳矩阵 chrg = mul(matrix(self.u), 0.5 * matrix(self.b)) y = div(matrix(self.u) + 0j, matrix(matrix(self.r) + 1j * matrix(self.x))) ts = exp(matrix(self.theta) * math.pi / 180 * 1j) ts = mul(matrix(self.tap_ratio), ts) ts2 = mul(ts, ts.H.T) # ts2real = ts2.real() # ts2imag = ts2.imag() # for i in range(len(ts2)): # ts2real[i] = round(ts2real[i], 15) # ts2imag[i] = round(ts2imag[i], 15) # ts2 = ts2real + 1j*ts2imag yft = div(-y, ts.H.T) # yftreal = yft.real() # yftimag = yft.imag() ytf = div(-y, ts) # ytfreal = ytf.real() # ytfimag = ytf.imag() yff = div(y, ts2) + 1j*chrg # yffreal = yff.real() # yffimag = yff.imag() ytt = y + 1j*chrg # yttreal = ytt.real() # yttimag = ytt.imag() # for i in range(len(ytt)): # yftreal[i] = round(yftreal[i], 15) # yftimag[i] = round(yftimag[i], 15) # # ytfreal[i] = round(ytfreal[i], 15) # ytfimag[i] = round(ytfimag[i], 15) # # yffreal[i] = round(yffreal[i], 15) # yffimag[i] = round(yffimag[i], 15) # # yttreal[i] = round(yttreal[i], 15) # yttimag[i] = round(yttimag[i], 15) # # yft = yftreal + 1j * yftimag # ytt = yttreal + 1j * yttimag # yff = yffreal + 1j * yffimag # ytf = ytfreal + 1j * ytfimag self.Y = spmatrix(yft, self.af, self.at, (nb, nb)) +\ spmatrix(ytf, self.at, self.af, (nb, nb)) +\ spmatrix(yff, self.af, self.af, (nb, nb)) +\ spmatrix(ytt, self.at, self.at, (nb, nb)) for i in range(nb): if self.Y[i, i] == 0: self.Y[i, i] == -1j*1e-6 system.DAE.Y = self.Y system.DAE.Y_G = self.Y.real() system.DAE.Y_B = self.Y.imag()
def eval(obj): return cvxopt.base.log(sum(exp(obj)))
def setx0(self): # 常数 self.c1 = [0] * self.n self.c2 = [0] * self.n self.c3 = [0] * self.n # 检查参数 for i in range(self.n): # 检查惯性 M 和 x'd if self.M[i] <= 0: self.M[i] = 10 print('%i 惯性M不能小于等于0,设置M = 10 [kWs/kVa]' % i) if self.xd1[i] <= 0: self.xd1[i] = 0.302 print("%i x'd不能小于等于0,设置x'd = 0.302 [p.u.]" % i) # 检查 xd, T'd0 if self.xd[i] <= 0: self.xd[i] = 1.9 print("%i xd不能小于等于0,设置xd = 1.9 [p.u.]" % i) if self.Td01[i] <= 0: self.Td01[i] = 8 print("%i T'd0不能小于等于0,设置T'd0 = 8 [s]" % i) # 检查 T''d0, x''d 和 x''q if self.Td02[i] <= 0: self.Td02[i] = 0.04 print("%i T''d0不能小于等于0,设置T''d0 = 0.04 [s]" % i) if self.xd2[i] <= 0: self.xd2[i] = 0.204 print("%i xd2不能小于等于0,设置xd2= 0.204 [p.u.]" % i) if self.xq2[i] <= 0: self.xq2[i] = 0.30 print("%i xq2不能小于等于0,设置xq2= 0.30 [p.u.]" % i) # 检查 T'q0 if self.Tq01[i] <= 0: self.Tq01[i] = 0.80 print("%i T'q0不能小于等于0,设置T'q0 = 0.80 [s]" % i) # 检查 T''q0 if self.Tq02[i] <= 0: self.Tq02[i] = 0.02 print("%i T''q0不能小于等于0,设置T''q0 = 0.02 [s]" % i) # 检查 xq x'q if self.xq[i] <= 0: self.xq[i] = 1.70 print("%i xq不能小于等于0,设置xq = 1.70 [p.u.]" % i) if self.xq1[i] <= 0: self.xq1[i] = 0.5 print("%i xq1不能小于等于0,设置xq1 = 0.5 [p.u.]" % i) # 检查Taa和饱和因子 # 转子速度 system.DAE._list2matrix() system.DAE.x[self.omega] = self.u # 检查有功、无功比例 # idx = [] # for i in self.a: # for index, j in enumerate(self.a): # if j == i: # idx.append(i) # if len(idx) == 1: # for i in range(self.n): # if self.ganmaP[i] != 1: # self. # 功率和转子角速度 # print(mul(matrix(self.u), matrix(system.Bus.Pg[self.a]))) system.DAE.y[self.p] = mul(mul(self.u, matrix(system.Bus.Pg[self.a])), self.ganmaP) system.DAE.y[self.q] = mul(mul(self.u, matrix(system.Bus.Qg[self.a])), self.ganmaQ) self.Pg0 = system.DAE.y[self.p] Vg = matrix(system.DAE.y[self.v] + 0j) ag = matrix(exp(system.DAE.y[self.a] * 1j)) V = mul(Vg, ag) S = system.DAE.y[self.p] - system.DAE.y[self.q] * 1j I = div(S, V.H.T) delta = np.angle(V + mul((self.ra + self.xq * 1j), I)) # print(mul(matrix(self.u), matrix(delta))) # a = mul(matrix(self.u), matrix(delta)) # print(type(system.DAE.x[0])) system.DAE.x[self.delta] = mul(self.u, matrix(delta)) # d、q轴电压和电流 jpi2 = math.pi / 2 * 1j # jpi2 = 1.5707963267948966j Vdq = mul(self.u + 0j, mul(V, exp(matrix(jpi2 - delta * 1j)))) idq = mul(self.u + 0j, mul(I, exp(matrix(jpi2 - delta * 1j)))) Vd = Vdq.real() Vq = Vdq.imag() Id = idq.real() Iq = idq.imag() # 机械转矩/功率 self.pm0 = mul(Vq + mul(self.ra, Iq), Iq) + \ mul(Vd + mul(self.ra, Id), Id) self.suijipm0.append(self.pm0) system.DAE.y[self.pm] = self.pm0 # 剩余状态变量和场电压 K = div(1, (self.ra**2 + mul(self.xq2, self.xd2))) self.c1 = mul(self.ra, K) self.c2 = mul(self.xd2, K) self.c3 = mul(self.xq2, K) system.DAE.x[self.e2q] = Vq + mul(self.ra, Iq) + mul(self.xd2, Id) system.DAE.x[self.e2d] = Vd + mul(self.ra, Id) - mul(self.xq2, Iq) system.DAE.x[self.e1d] = mul( self.xq - self.xq1 - div(mul(mul(self.Tq02, self.xq2), self.xq - self.xq1), mul(self.Tq01, self.xq1)), Iq) K1 = self.xd - self.xd1 - div( (mul(mul(self.Td02, self.xd2), (self.xd - self.xd1))), mul(self.Td01, self.xd1)) K2 = self.xd1 - self.xd2 + div( mul(mul(self.Td02, self.xd2), self.xd - self.xd1), mul(self.Td01, self.xd1)) system.DAE.x[self.e1q] = system.DAE.x[self.e2q] + mul(K2, Id) - div( mul(self.Taa, mul(K1 + K2, Id) + system.DAE.x[self.e2q]), self.Td01) # print(type(system.Syn6.synsat())) # print(system.Syn6.synsat()) self.vf0 = div( mul(K1, Id) + system.Syn6.synsat(), 1 - div(self.Taa, self.Td01)) system.DAE.y[self.vf] = self.vf0 # !!! 移除静态发电机节点 system.SW.remove(idx='SW_1') for i in range(system.PV.n): idx = 'PV_' + str(i + 1) system.PV.remove(idx)
def kernel_matrix(X, kernel, sigma=1.0, theta=1.0, degree=1, V=None, width=None): """ Computes the kernel matrix or a partial kernel matrix. Input arguments. X is an N x n matrix. kernel is a string with values 'linear', 'rfb', 'poly', or 'tanh'. 'linear': k(u,v) = u'*v/sigma. 'rbf': k(u,v) = exp(-||u - v||^2 / (2*sigma)). 'poly': k(u,v) = (u'*v/sigma)**degree. 'tanh': k(u,v) = tanh(u'*v/sigma - theta). kernel is a sigma and theta are positive numbers. degree is a positive integer. V is an N x N sparse matrix (default is None). width is a positive integer (default is None). Output. Q, an N x N matrix or sparse matrix. If V is a sparse matrix, a partial kernel matrix with the sparsity pattern V is returned. If width is specified and V = 'band', a partial kernel matrix with band sparsity is returned (width is the half-bandwidth). a, an N x 1 matrix with the products <xi,xi>/sigma. """ N, n = X.size #### dense (full) kernel matrix if V is None: if verbose: print("building kernel matrix ..") # Qij = xi'*xj / sigma Q = matrix(0.0, (N, N)) blas.syrk(X, Q, alpha=1.0 / sigma) a = Q[::N + 1] # ai = ||xi||**2 / sigma if kernel == 'linear': pass elif kernel == 'rbf': # Qij := Qij - 0.5 * ( ai + aj ) # = -||xi - xj||^2 / (2*sigma) ones = matrix(1.0, (N, 1)) blas.syr2(a, ones, Q, alpha=-0.5) Q = exp(Q) elif kernel == 'tanh': Q = exp(Q - theta) Q = div(Q - Q**-1, Q + Q**-1) elif kernel == 'poly': Q = Q**degree else: raise ValueError('invalid kernel type') #### general sparse partial kernel matrix elif type(V) is cvxopt.base.spmatrix: if verbose: print("building projected kernel matrix ...") Q = +V base.syrk(X, Q, partial=True, alpha=1.0 / sigma) # ai = ||xi||**2 / sigma a = matrix(Q[::N + 1], (N, 1)) if kernel == 'linear': pass elif kernel == 'rbf': ones = matrix(1.0, (N, 1)) # Qij := Qij - 0.5 * ( ai + aj ) # = -||xi - xj||^2 / (2*sigma) p = chompack.maxcardsearch(V) symb = chompack.symbolic(Q, p) Qc = chompack.cspmatrix(symb) + Q chompack.syr2(Qc, a, ones, alpha=-0.5) Q = Qc.spmatrix(reordered=False) Q.V = exp(Q.V) elif kernel == 'tanh': v = +Q.V v = exp(v - theta) v = div(v - v**-1, v + v**-1) Q.V = v elif kernel == 'poly': Q.V = Q.V**degree else: raise ValueError('invalid kernel type') #### banded partial kernel matrix elif V == 'band' and width is not None: # Lower triangular part of band matrix with bandwidth 2*w+1. if verbose: print("building projected kernel matrix ...") I = [i for k in range(N) for i in range(k, min(width + k + 1, N))] J = [k for k in range(N) for i in range(min(width + 1, N - k))] V = matrix(0.0, (len(I), 1)) oy = 0 for k in range(N): # V[:,k] = Xtrain[k:k+w, :] * Xtrain[k,:].T m = min(width + 1, N - k) blas.gemv(X, X, V, m=m, ldA=N, incx=N, offsetA=k, offsetx=k, offsety=oy) oy += m blas.scal(1.0 / sigma, V) # ai = ||xi||**2 / sigma a = matrix(V[[i for i in range(len(I)) if I[i] == J[i]]], (N, 1)) if kernel == 'linear': Q = spmatrix(V, I, J, (N, N)) elif kernel == 'rbf': Q = spmatrix(V, I, J, (N, N)) ones = matrix(1.0, (N, 1)) # Qij := Qij - 0.5 * ( ai + aj ) # = -||xi - xj||^2 / (2*sigma) symb = chompack.symbolic(Q) Qc = chompack.cspmatrix(symb) + Q chompack.syr2(Qc, a, ones, alpha=-0.5) Q = Qc.spmatrix(reordered=False) Q.V = exp(Q.V) elif kernel == 'tanh': V = exp(V - theta) V = div(V - V**-1, V + V**-1) Q = spmatrix(V, I, J, (N, N)) elif kernel == 'poly': Q = spmatrix(V**degree, I, J, (N, N)) else: raise ValueError('invalid kernel type') else: raise TypeError('invalid type V') return Q, a