示例#1
0
    def Gycall(self):

        system.DAE._list2matrix()
        delta = system.DAE.x[self.delta]
        ag = system.DAE.y[self.a]
        vg = mul(self.u, system.DAE.y[self.v])
        ss = sin(delta - ag)
        cc = cos(delta - ag)

        M1 = mul(vg, (mul(self.c1, cc) - mul(self.c3, ss)))
        M2 = mul(-vg, (mul(self.c2, cc) + mul(self.c1, ss)))
        M3 = -(mul(self.c1, ss) + mul(self.c3, cc))
        M4 = (mul(self.c2, ss) - mul(self.c1, cc))

        self.J11 = mul(vg,
                       (mul(self.Id - M2, cc) - mul(mul(M1 + self.Iq, ss))))
        self.J12 = mul(-self.Id, ss) - mul(self.Iq, cc) - mul(
            vg, (mul(M3, ss) + mul(M4, cc)))
        self.J21 = mul(vg, mul(M2 - self.Id, ss) - mul(M1 + self.Iq, cc))
        self.J22 = mul(-self.Id, cc) + mul(self.Iq, ss) - mul(
            vg,
            mul(M3, cc) - mul(M4, ss))

        system.DAE.Gy = system.DAE.Gy -spmatrix(self.u, self.a, self.p, (system.DAE.ny, system.DAE.ny)) \
                        - spmatrix(self.u, self.v, self.q, (system.DAE.ny, system.DAE.ny)) \
                        - spmatrix([1]*self.n, self.p, self.p, (system.DAE.ny, system.DAE.ny)) \
                        - spmatrix([1] * self.n, self.q, self.q, (system.DAE.ny, system.DAE.ny)) \
                        - spmatrix(self.J11, self.p, self.a, (system.DAE.ny, system.DAE.ny)) \
                        - spmatrix(self.J12, self.p, self.v, (system.DAE.ny, system.DAE.ny)) \
                        - spmatrix(self.J21, self.q, self.a, (system.DAE.ny, system.DAE.ny)) \
                        - spmatrix(self.J22, self.q, self.v, (system.DAE.ny, system.DAE.ny)) \
                        - spmatrix([1] * self.n, self.pm, self.pm, (system.DAE.ny, system.DAE.ny)) \
                        - spmatrix([1] * self.n, self.vf, self.vf, (system.DAE.ny, system.DAE.ny)) \
示例#2
0
文件: base.py 项目: cvxopt/smcp
    def _gen_bandsdp(self,n,m,bw,seed):
        """Random data generator for SDP with band structure"""
        setseed(seed)

        I = matrix([ i for j in range(n) for i in range(j,min(j+bw+1,n))])
        J = matrix([ j for j in range(n) for i in range(j,min(j+bw+1,n))])
        V = spmatrix(0.,I,J,(n,n))
        Il = misc.sub2ind((n,n),I,J)
        Id = matrix([i for i in range(len(Il)) if I[i]==J[i]])

        # generate random y with norm 1
        y0 = normal(m,1)
        y0 /= blas.nrm2(y0)

        # generate random S0
        S0 = mk_rand(V,cone='posdef',seed=seed)
        X0 = mk_rand(V,cone='completable',seed=seed)

        # generate random A1,...,Am and set A0 = sum Ai*yi + S0
        A_ = normal(len(I),m+1,std=1./len(I))
        u = +S0.V
        blas.gemv(A_[:,1:],y0,u,beta=1.0)
        A_[:,0] = u
        # compute b
        x = +X0.V
        x[Id] *= 0.5
        self._b = matrix(0.,(m,1))
        blas.gemv(A_[:,1:],x,self._b,trans='T',alpha=2.0)
        # form A
        self._A = spmatrix(A_[:],
                     [i for j in range(m+1) for i in Il],
                     [j for j in range(m+1) for i in Il],(n**2,m+1))

        self._X0 = X0; self._y0 = y0; self._S0 = S0
示例#3
0
文件: base.py 项目: cvxopt/smcp
    def _gen_mtxnormsdp(self,p,q,r,d,seed):
        """
        Random data generator for matrix norm minimization SDP.
        """
        setseed(seed)
        n = p + q;

        I = matrix([ i for j in range(q) for i in range(q,n)])
        J = matrix([ j for j in range(q) for i in range(q,n)])
        Il = list(misc.sub2ind((n,n),I,J))

        if type(d) is float:
            nz = min(max(1, int(round(d*p*q))), p*q)
            A = sparse([[spmatrix(normal(p*q,1),Il,[0 for i in range(p*q)],(n**2,1))],
                        [spmatrix(normal(nz*r,1),
                                  [i for j in range(r) for i in random.sample(Il,nz)],
                                  [j for j in range(r) for i in range(nz)],(n**2,r))]])
        elif type(d) is list:
            if len(d) == r:
                nz = [min(max(1, int(round(v*p*q))), p*q) for v in d]
                nnz = sum(nz)
                A = sparse([[spmatrix(normal(p*q,1),Il,[0 for i in range(p*q)],(n**2,1))],
                            [spmatrix(normal(nnz,1),
                                      [i for j in range(r) for i in random.sample(Il,nz[j])],
                                      [j for j in range(r) for i in range(nz[j])],(n**2,r))]])
        else: raise TypeError
        self._A = sparse([[A],[spmatrix(-1.,list(range(0,n**2,n+1)),[0 for i in range(n)],(n**2,1))]])

        self._b = matrix(0.,(r+1,1))
        self._b[-1] = -1.
示例#4
0
 def optimize(self): 
     
     
     numvar = len(self.bundle)
     
     P = cm(self.P)
     q = cm(- matrix(self.bvec)).T
     
     
     valz = [-1. for x in range(numvar)] + [1. for x in range(numvar)]
     rcoordz = range(numvar) + [numvar for x in range(numvar)]
     ccoordz = range(numvar) + range(numvar)
     
     G = spmatrix(valz, rcoordz, ccoordz)
     h = spmatrix(1., [numvar], [0])
     h = cm(h)
     
     p_dict = cvxopt.solvers.qp(P, q, G=G, h=h)
     #print p_dict['status']
     xx = p_dict['x']
     self.itercount = self.itercount + 1
     print self.itercount
     xx=mat(xx)
     W = csr_matrix(self.bundle[0].shape)
     for i in range(len(self.bundle)):
         W = W+float(-xx[i]/self.lamb)*self.bundle[i]
     return W
示例#5
0
    def Gycall(self):

        if self.n == 0:
            return

        T1 = self.T1
        T2 = self.T2
        T3 = self.T3
        T4 = self.T4

        Kp = self.Kp
        Kv = self.Kv

        vsmax = self.vsmax
        vsmin = self.vsmin

        vss = system.DAE.y[self.vss]
        z = [0] * self.n
        z = matrix(z)
        for i in range(self.n):
            if vss[i] < vsmax[i] and vss[i] > vsmin[i]:
                if self.u[i]:
                    z[i] = 1

        system.DAE.Gy = system.DAE.Gy - spmatrix(1, self.vss, self.vss, (system.DAE.ny, system.DAE.ny))\
                             + spmatrix(z, self.vref, self.vss, (system.DAE.ny, system.DAE.ny))

        A = div(T1, T2)
        C = div(T3, T4)
        E = mul(C, A)
        system.DAE.Gy = system.DAE.Gy + spmatrix(mul(z, mul(Kv, E)), self.vss, self.v, (system.DAE.ny, system.DAE.ny)) \
                        + spmatrix(mul(z, mul(Kp, E)), self.vss, self.p, (system.DAE.ny, system.DAE.ny))
示例#6
0
    def gcall(self):

        if self.n == 0:
            return

        slip = system.DAE.x[self.slip]

        V = mul(self.u, system.DAE.y[self.v])  # 后续得修改
        t = system.DAE.y[self.a]
        st = sin(t)
        ct = cos(t)

        Vr = mul(V, st)
        Vm = mul(V, ct)

        e1r = system.DAE.x[self.e1r]
        e1m = system.DAE.x[self.e1m]

        a03 = mul(self.rs, self.rs) + mul(self.x1, self.x1)
        a13 = div(self.rs, a03)
        a23 = div(self.x1, a03)
        a33 = self.x0 - self.x1

        Im = mul(-a23, -Vr - e1r + mul(a13, (Vm - e1m)))
        Ir = mul(a13, -Vr - e1r) + mul(a23, Vm - e1m)

        system.DAE.g = system.DAE.g \
                       + spmatrix(mul(-Vr, Ir)+mul(Vm, Im), self.a, matrix(0, (self.n, 1)), (system.DAE.ny, 1)) \
                       + spmatrix(mul(Vr, Im) + mul(Vm, Ir), self.v, matrix(0, (self.n, 1)), (system.DAE.ny, 1))
示例#7
0
    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)
示例#8
0
    def gcall(self):

        self.vfd = system.Syn6.vf[self.a]



        system.DAE.g = system.DAE.g + spmatrix(system.DAE.x[self.vf], self.vfd, [0]*self.n, (system.DAE.ny, 1)) \
                       + spmatrix(mul(self.u, self.vref0)-system.DAE.y[self.vref], self.vref, [0]*self.n, (system.DAE.ny, 1))
示例#9
0
    def Fxcall(self):

        if self.n == 0:
            return

        k = div(1, self.Tw)

        system.DAE.Fx = system.DAE.Fx - spmatrix(
            k, self.vw, self.vw, (system.DAE.nx, system.DAE.nx))
        system.DAE.Fy = system.DAE.Fy + spmatrix(
            k, self.vw, self.ws, (system.DAE.nx, system.DAE.ny))
示例#10
0
    def Gycall(self):

        if self.n == 0:
            return

        V = mul(2 * self.u, system.DAE.y[self.v])

        system.DAE.Gy = system.DAE.Gy + spmatrix(
            mul(self.dat[0], V), self.a, self.v,
            (system.DAE.ny, system.DAE.ny)) - spmatrix(
                mul(self.dat[1], V), self.v, self.v,
                (system.DAE.ny, system.DAE.ny))
示例#11
0
    def Gycall(self):
        if self.n == 0:
            return
        system.DAE.Gy[self.a, :] = 0
        system.DAE.Gy[:, self.a] = 0
        system.DAE.Gy[self.v, :] = 0
        system.DAE.Gy[:, self.v] = 0

        m = system.DAE.ny

        system.DAE.Gy = system.DAE.Gy + spmatrix(1.0, self.a, self.a, (m, m))
        system.DAE.Gy = system.DAE.Gy + spmatrix(1.0, self.v, self.v, (m, m))
示例#12
0
    def gcall(self):

        if self.n == 0:
            return

        V = system.DAE.y[self.v]
        V2 = mul(self.u, V, V)
        V2 = matrix(V2)

        system.DAE.g = system.DAE.g + spmatrix(
            mul(self.dat[0], V2), self.a, [0],
            (system.DAE.ny, 1)) - spmatrix(mul(self.dat[1], V2), self.v, [0],
                                           (system.DAE.ny, 1))
示例#13
0
文件: base.py 项目: cvxopt/smcp
    def _gen_randsdp(self,V,m,d,seed):
        """
        Random data generator
        """
        setseed(seed)
        n = self._n
        V = chompack.tril(V)
        N = len(V)
        I = V.I; J = V.J
        Il = misc.sub2ind((n,n),I,J)
        Id = matrix([i for i in range(len(Il)) if I[i]==J[i]])

        # generate random y with norm 1
        y0 = normal(m,1)
        y0 /= blas.nrm2(y0)

        # generate random S0, X0
        S0 = mk_rand(V,cone='posdef',seed=seed)
        X0 = mk_rand(V,cone='completable',seed=seed)

        # generate random A1,...,Am
        if type(d) is float:
            nz = min(max(1, int(round(d*N))), N)
            A = sparse([[spmatrix(normal(N,1),Il,[0 for i in range(N)],(n**2,1))],
                        [spmatrix(normal(nz*m,1),
                                  [i for j in range(m) for i in random.sample(Il,nz)],
                                  [j for j in range(m) for i in range(nz)],(n**2,m))]])
        elif type(d) is list:
            if len(d) == m:
                nz = [min(max(1, int(round(v*N))), N) for v in d]
                nnz = sum(nz)
                A = sparse([[spmatrix(normal(N,1),Il,[0 for i in range(N)],(n**2,1))],
                            [spmatrix(normal(nnz,1),
                                      [i for j in range(m) for i in random.sample(Il,nz[j])],
                                      [j for j in range(m) for i in range(nz[j])],(n**2,m))]])
        else: raise TypeError

        # compute A0
        u = +S0.V
        for k in range(m):
            base.gemv(A[:,k+1][Il],matrix(y0[k]),u,beta=1.0,trans='N')
        A[Il,0] = u
        self._A = A

        # compute b
        X0[Il[Id]] *= 0.5
        self._b = matrix(0.,(m,1))
        u = matrix(0.)
        for k in range(m):
            base.gemv(A[:,k+1][Il],X0.V,u,trans='T',alpha=2.0)
            self._b[k] = u
示例#14
0
    def Gycall(self):

        if self.n == 0:
            return

        V = 2 * system.DAE.y[self.v]
        g = matrix(self.g)
        b = matrix(self.b)
        m = system.DAE.ny

        system.DAE.Gy = system.DAE.Gy + spmatrix(mul(V, g), self.a, self.v,
                                                 (m, m)) - spmatrix(
                                                     mul(V, b), self.v, self.v,
                                                     (m, m))
示例#15
0
    def _gen_mtxnormsdp(self, p, q, r, d, seed):
        """
        Random data generator for matrix norm minimization SDP.
        """
        setseed(seed)
        n = p + q

        I = matrix([i for j in range(q) for i in range(q, n)])
        J = matrix([j for j in range(q) for i in range(q, n)])
        Il = misc.sub2ind((n, n), I, J)

        if type(d) is float:
            nz = min(max(1, int(round(d * p * q))), p * q)
            A = sparse(
                [[
                    spmatrix(normal(p * q, 1), Il, [0 for i in range(p * q)],
                             (n**2, 1))
                ],
                 [
                     spmatrix(
                         normal(nz * r, 1),
                         [i for j in range(r) for i in random.sample(Il, nz)],
                         [j for j in range(r) for i in range(nz)], (n**2, r))
                 ]])
        elif type(d) is list:
            if len(d) == r:
                nz = [min(max(1, int(round(v * p * q))), p * q) for v in d]
                nnz = sum(nz)
                A = sparse([[
                    spmatrix(normal(p * q, 1), Il, [0 for i in range(p * q)],
                             (n**2, 1))
                ],
                            [
                                spmatrix(normal(nnz, 1), [
                                    i for j in range(r)
                                    for i in random.sample(Il, nz[j])
                                ], [j for j in range(r) for i in range(nz[j])],
                                         (n**2, r))
                            ]])
        else:
            raise TypeError
        self._A = sparse([[A],
                          [
                              spmatrix(-1., range(0, n**2, n + 1),
                                       [0 for i in range(n)], (n**2, 1))
                          ]])

        self._b = matrix(0., (r + 1, 1))
        self._b[-1] = -1.
示例#16
0
    def gcall(self):

        if self.n == 0:
            return
        omega_m = system.DAE.x[self.omega_m]
        idr = system.DAE.x[self.idr]
        iqr = system.DAE.x[self.iqr]

        pwa = system.DAE.y[self.pwa]
        V = system.DAE.y[self.v]
        t = system.DAE.y[self.a]
        st = sin(t)
        ct = cos(t)

        rs = self.rs
        rr = self.rr
        xm = self.xm
        as1 = rs**2 + self.dat1**2
        a13 = div(rs, as1)
        a23 = div(self.dat1, as1)
        a33 = self.dat2

        vds = mul(-V, st)
        vqs = mul(V, ct)

        ids = mul(-a13, vds - mul(xm, iqr)) - mul(a23, vqs + mul(xm, idr))
        iqs = mul(a23, vds - mul(xm, iqr)) - mul(a13, vqs + mul(xm, idr))

        ones = matrix(1.0, (self.n, 1))

        vdr = mul(-rr, idr) + mul(ones - omega_m, mul(a33, iqr) + mul(xm, iqs))
        vqr = mul(-rr, iqr) - mul(ones - omega_m, mul(a33, idr) + mul(xm, ids))

        p = mul(vds, ids) + mul(vqs, iqs) + mul(vdr, idr) + mul(vqr, iqr)
        q = div(mul(-V, mul(xm, idr) + V), self.dat1)

        xomega = system.DAE.x[self.omega_m]
        xomega1 = matrix(0.0, (self.n, 1))
        for i in range(self.n):
            xomega1[i] = max([min([2 * xomega[i] - 1, 1]), 0])

        pwa1 = mul(self.u, mul(self.Sn, div(xomega1,
                                            system.Settings.mva))) - pwa
        ones = [0] * self.n
        system.DAE.g = system.DAE.g - \
                       spmatrix(mul(self.u, p), self.a, ones, (system.DAE.ny, 1)) - \
                       spmatrix(mul(self.u, q), self.v, ones, (system.DAE.ny, 1)) + \
                       spmatrix(mul(self.u, self.dat6)-system.DAE.y[self.vref], self.vref, ones, (system.DAE.ny, 1)) + \
                       spmatrix(pwa1, self.pwa, ones, (system.DAE.ny, 1))
示例#17
0
    def gcall(self):

        if self.n == 0:
            return

        a_s = div(matrix(1.0, (self.n, 1)), self.Ts)
        ac = div(matrix(1.0, (self.n, 1)), self.Tc)
        a5 = div(matrix(1.0, (self.n, 1)), self.T5)
        K1 = mul(self.T3, ac)
        K2 = 1 - K1
        K3 = mul(self.T4, a5)
        K4 = 1 - K3
        pmech = system.DAE.x[self.tg3] + mul(K3, system.DAE.x[self.tg2]) + mul(K1, system.DAE.x[self.tg1])
        system.DAE.g = system.DAE.g \
                       + spmatrix(mul(self.u, pmech), self.pm, matrix(0, (self.n, 1)), (system.DAE.ny, 1)) \
                       + spmatrix(mul(self.u, self.wref0) - system.DAE.y[self.wref], self.wref, matrix(0, (self.n, 1)), (system.DAE.ny, 1))
示例#18
0
def form_coeffs_constraint_eq_sparse_upper(monoms, sqroot_monoms):
    """
    Forms the coefficients of the constraint equations given matrices monoms, sqroot_monoms
    whose rows correspond to the multi-indices in the convex hull and 1/2 the convex hull
    of the multi-indices of a polynomial.
    Constraint matrices are returned in spmatrix form; only upper triangular elements given.
    :param monoms:
    :param sqroot_monoms:
    :return:
    """

    num = sqroot_monoms.shape[0]
    constraints = []
    for i in range(monoms.shape[0]):
        constraint_i_rows = []
        constraint_i_cols = []
        count_nontriv = 0
        for j in range(num):
            for k in range(j, num):
                if np.all(monoms[i] == (sqroot_monoms[j] + sqroot_monoms[k])):
                    constraint_i_rows.append(j)
                    constraint_i_cols.append(k)
                    count_nontriv += 1
        if count_nontriv:
            constraints.append(
                spmatrix(1, constraint_i_rows, constraint_i_cols, (num, num)))
    return constraints
示例#19
0
	def build_inv_weight(w, exact_mask=None):
		# as it is this function now if, for a certain k, w[k,k] is zero and 
		# exact_mask[k] is true at the same time, the first take precedence,
		# that is the corresponing parameter will not be matched at all. 
		I = []
		J = []
		E = []
		kk = 0
		for k in range(0,w.size[0]):
			 if not w[k,k] == 0:
			 	I.append(kk)
				J.append(k)
				E.append(1.0)
				kk += 1
			 if exact_mask is not None and w[k,k] == 0:
				del exact_mask[kk]
		I.append(kk-1)
		J.append(k)
		E.append(0.0)
		M = cb.spmatrix(E, I, J)
		w_r = M*w*M.T
		i_w_r = cb.matrix(linalg.inv(w_r))
		if exact_mask is None:
			return i_w_r, M
		for k in range(0,len(exact_mask)):
			if exact_mask[k]:
				i_w_r[k,:] = 0.0
				i_w_r[:,k] = 0.0
		return i_w_r, M
示例#20
0
    def Gycall(self):

        if self.n == 0:
            return

        system.DAE.Gy = system.DAE.Gy - spmatrix(
            [1] * self.n, self.vref, self.vref, (system.DAE.ny, system.DAE.ny))
示例#21
0
def Hankel_basis(m, n, p = 1, q = 1):
    
    """
    Return the Hankel basis matrix B, a 'd' spmatrix of size 
    (m*n*p*q, (m+n-1)*p*q), that satisfies
    
        H[:] = B * x[:]
        
    where
    
            [ h_1   h_2     ...  h_n       ]
        H = [ h_2   h_3     ...  h_{n+1}   ]
            [  :     :            :        ]
            [ h_m   h_{m+1} ...  h_{m+n-1} ],

        x = [ h_1, h_2, ..., h_{m+n-1} ],
        
    and h_i, i=1...(m+n-1), are the Hankel blocks of size (p,q).
        
    INPUT
    m       number of block rows
    
    n       number of block columns
    
    p       number of rows of a Hankel block
    
    q       number of columns of a Hankel block    
    """
    
    Bi = [ (j*q+l)*m*p+i*p+k for i in xrange(m) for j in xrange(n) for 
        k in xrange(p) for l in xrange(q) ]
    Bj = [ (i+j)*p*q+l*p+k for i in xrange(m) for j in xrange(n) for 
        k in xrange(p) for l in xrange(q) ]
    
    return spmatrix(1.0, Bi, Bj)
示例#22
0
def Hankel_basis(m, n, p=1, q=1):
    """
    Return the Hankel basis matrix B, a 'd' spmatrix of size 
    (m*n*p*q, (m+n-1)*p*q), that satisfies
    
        H[:] = B * x[:]
        
    where
    
            [ h_1   h_2     ...  h_n       ]
        H = [ h_2   h_3     ...  h_{n+1}   ]
            [  :     :            :        ]
            [ h_m   h_{m+1} ...  h_{m+n-1} ],

        x = [ h_1, h_2, ..., h_{m+n-1} ],
        
    and h_i, i=1...(m+n-1), are the Hankel blocks of size (p,q).
        
    INPUT
    m       number of block rows
    
    n       number of block columns
    
    p       number of rows of a Hankel block
    
    q       number of columns of a Hankel block    
    """

    Bi = [(j * q + l) * m * p + i * p + k for i in xrange(m) for j in xrange(n)
          for k in xrange(p) for l in xrange(q)]
    Bj = [(i + j) * p * q + l * p + k for i in xrange(m) for j in xrange(n)
          for k in xrange(p) for l in xrange(q)]

    return spmatrix(1.0, Bi, Bj)
def read_mps_preprocess(filepath):
    problem = op()
    problem.fromfile(filepath)
    mat_form = problem._inmatrixform(format='dense')
    format = 'dense'
    assert mat_form
    lp, vmap, mmap = mat_form

    variables = lp.variables()
    x = variables[0]
    c = lp.objective._linear._coeff[x]
    inequalities = lp._inequalities
    G = inequalities[0]._f._linear._coeff[x]
    h = -inequalities[0]._f._constant
    equalities = lp._equalities
    A, b = None, None
    if equalities:
        A = equalities[0]._f._linear._coeff[x]
        b = -equalities[0]._f._constant
    elif format == 'dense':
        A = matrix(0.0, (0, len(x)))
        b = matrix(0.0, (0, 1))
    else:
        A = spmatrix(0.0, [], [], (0, len(x)))  # CRITICAL
        b = matrix(0.0, (0, 1))

    c = np.array(c).flatten()
    G = np.array(G)
    h = np.array(h).flatten()
    A = np.array(A)
    b = np.array(b).flatten()

    return c, G, h, A, b
示例#24
0
    def load_model(self,file_name):
        import bz2,struct
        sys.__stderr__.write('Loading model...\n')
        input = bz2.BZ2File(file_name+'.f','r')
        for line in input.readlines():
            k,v = line.strip().split()
            self._f_model.training_features.table[k] = int(v)
        input.close()
        self._f_model.training_features.fid = len(self._f_model.training_features.table)

        self.features_size = self._f_model.get_size_of_features()
        sys.__stderr__.write('%d features are loaded\n'%(self.features_size))

        input = open(file_name+'.b','rb')
        w = []
        while True:
            f = input.read(4)
            if not f:
                break
            w.append(struct.unpack('f',f)[0])
        input.close()

        self.v = spmatrix(0,[0],[0],(len(w),1))
        for i,f in enumerate(w):
            if f != 0:
                self.v[i] = f
        sys.__stderr__.write('weight vector (%d) are loaded\n'%(len(w)))
示例#25
0
    def build_y(self):

        for i in range(len(self.tap_ratio)):
            if self.tap_ratio[i] == 0:
                self.tap_ratio[i] = 1
        # print(self.tap_ratio)

        # chrg = np.mat(self.b) * 0.5
        # chrg = chrg.T
        #
        # print(chrg)
        chrg = np.array(np.zeros((len(self.b), 1), complex))
        for i in range(len(self.x)):
            chrg[i] = complex(0.0, self.b[i] * 0.5)

        #print(chrg)
        #zeros = [0] * len(self.x)
        #y = matrix(zeros, (len(self.x), 1, complex))
        y = np.array(np.zeros((len(self.x), 1), complex))
        for i in range(len(self.x)):
            y[i] = 1.0 / complex(self.r[i], self.x[i])
        #print(y)

        ts = np.array(np.zeros((len(self.theta), 1), complex))
        for i in range(len(self.theta)):
            ts[i] = complex(
                self.tap_ratio[i] * cos(self.theta[i] * math.pi / 180),
                self.tap_ratio[i] * sin(self.theta[i] * math.pi / 180))
        #print(ts)

        ts2 = ts * ts.conj()
        #print(ts2)

        y1 = -y / ts.conj()
        #print(y1)

        self.Y = spmatrix(y1, self.af, self.at, (system.Bus.n, system.Bus.n)) +\
            spmatrix(y1, self.at, self.af, (system.Bus.n, system.Bus.n)) +\
            spmatrix(y/ts2 + chrg, self.af, self.af, (system.Bus.n, system.Bus.n)) +\
            spmatrix(y + chrg, self.at, self.at, (system.Bus.n, system.Bus.n))
        system.DAE.Y = self.Y

        system.DAE.Y_G = self.Y.real()

        system.DAE.Y_B = self.Y.imag()
        print(system.DAE.Y_G)
        print(system.DAE.Y_B)
示例#26
0
def to_cvxopt_spmatrix(A):

    if sp.issparse(A):
        cx = A.tocoo()
    else:
        cx = sp.coo_matrix(A)

    return spmatrix(cx.data, cx.row, cx.col)
def each_weight_greater_than_0_constraint(
        assets_number: int) -> Tuple[matrix, matrix]:
    """
    Creates a constraint which assures that no weight has a negative value.
    """
    G = spmatrix(-1.0, range(assets_number), range(assets_number))
    h = matrix(0.0, (assets_number, 1))
    return G, h
示例#28
0
 def state_matrix():
     Gyx = matrix(system.DAE.Gx)
     linsolve(sparse(system.DAE.Gy), Gyx)
     I = []
     J = []
     for i in range(system.DAE.nx):
         I.append(i)
         J.append(i)
     return system.DAE.Fx - system.DAE.Fy * Gyx - spmatrix(1e-6, I, J)
示例#29
0
    def train(self,N=1,k=1):
        from dig_parser import DIGParser
        self._dig_parser = DIGParser()
        Q = spdiag([2]*self.features_size)
        G = spmatrix(0,[0],[0],(k,self.features_size))
        Wc = spmatrix(0,[0],[0],(self.features_size,1))
        self.v = spmatrix(0,[0],[0],(self.features_size,1))
        complete = N * len(self._training_data)
        round = 0
        for loop in range(N):
            for tid,(W,T,A,H) in enumerate(self._training_data):
                round += 1
                print 'round %d/%d'%(round,complete)
                F = self.features_instance[tid]
                gold_deps,gold_features = [],Features()
                for c,h in enumerate(H):
                    if h != -1:
                        gold_deps.append((h,c))
                        if h > c:
                            gold_features += F['%d-%d-L'%(c,h)]
                        else:
                            gold_features += F['%d-%d-R'%(h,c)]
                gold_deps.sort()

                print 'creating score table...'
                Sc = self._create_Sc(W,T,F,Wc)

                text = []
                for i in range(1,len(W),1):
                    text.append('%s/%s'%(W[i],T[i]))
                text = ' '.join(text)
                print 'input length = %d'%(len(W))
                print 'parsing %d best...'%(k)
                trees = self._dig_parser.parse_mira_tagged_text(text,Sc,k,format='naist')

                n_trees = len(trees)
                if n_trees < k:
                    for j in range(k-n_trees):
                        trees.append(trees[-1])
                        
                print 'updating weight...'
                Wc = self._update_weight(Q,G,Wc,F,gold_deps,gold_features,trees)
                self.v = self.v + Wc
                print 'done\n'
示例#30
0
def robustLS_toep(q, r, delta=None, seed=0):
    """
    Random data generator for matrix norm minimization SDP.
    
    Al,b = robustLS_toep(q,r,delta=0.1,seed=0])
    
    PURPOSE
    Generates random data for a robust least squares problem
    with Toeplitz structure:
     
      minimize e_wc(x)^2
    
    where
    
      e_wc(x)=sup_{norm(u)<=1} norm((Ab+A1*u1+...+Ar*ur)*x - b),
    
      Ab,A1,...,Ar \in \reals^{p \times q}        
    
    The parameter r determines the number of nonzero diagonals
    in Ab (1 <= r <= p).

    ARGUMENTS   
    q         integer
    
    r         integer

    delta     float
    
    RETURNS
    Al        list of CVXOPT matrices, [Ab,A1,...,Ar]

    b         CVXOPT matrix

    """
    setseed(seed)

    p = q + r - 1
    Alist = [
        sparse(
            misc.toeplitz(
                matrix([normal(r, 1), matrix(0., (p - r, 1))], (p, 1)),
                matrix(0., (q, 1))))
    ]
    x = normal(q, 1)
    b = normal(p, 1, std=0.1)
    b += Alist[0] * x

    if delta is None:
        delta = 1. / r

    for i in xrange(r):
        Alist.append(
            spmatrix(delta, xrange(i, min(p, q + i)), xrange(min(q, p - i)),
                     (p, q)))

    return robustLS_SDP(Alist, b)
示例#31
0
文件: svm.py 项目: breuderink/golem
def cvxopt_svm(K, labels, c, D=None):
  log = logging.getLogger('golem.nodes.svm.cvxopt_svm')
  c = float(c)
  m = K.shape[0]
  if D == None: # normal SVM
    D = cvx.spmatrix(1, range(m), range(m))
  else: # latent error SVM with spread matrix D
    D = cvx.matrix(D)
  labels = np.atleast_2d(labels)

  assert np.all(np.unique(labels) == [-1, 1])
  assert K.shape[0] == K.shape[1]
  
  log.debug('Creating QP-target')
  # (4) min W(a) = (1/2) * a^T * P * a - \vec{1}^T a
  label_matrix = np.dot(labels.T, labels)
  P = cvx.matrix(K * label_matrix)
  q = cvx.matrix([-1. for i in range(m)])

  log.debug('Creating QP-constraints')
  # (2) 0 < all alphas < c/m, using Ga <= h
  # (3) sum(a_i * y_i) = 0, using Aa = b
  # (2) is solved in two parts, first 0 < alphas, then alphas < c / m
  G1 = cvx.spmatrix(-1, range(m), range(m))
  G2 = D.T
  G = cvx.sparse([G1, G2])
  h = cvx.matrix([0. for i in range(m)] + [c/m for i in range(m)])
  A = cvx.matrix(labels)
  r = cvx.matrix(0.)

  log.debug('Solving QP')
  cvxopt.solvers.options['show_progress'] = False
  sol = cvxopt.solvers.qp(P, q, G, h, A, r)
  if sol['status'] != 'optimal':
    log.warning('QP solution status: ' + sol['status'])
  log.debug('solver.status = ' + sol['status'])
  alphas = np.asarray(sol['x']) # column vector!
  
  bias = np.mean(labels.flatten() - np.dot(labels.flatten() * alphas.T, K))
  # alpha_i gets close to zero, but does not always equal zero:
  alphas[alphas < np.max(alphas) * ALPHA_RTOL] = 0
  return alphas.flatten(), bias
示例#32
0
文件: base.py 项目: cvxopt/smcp
 def get_A(self,i=None):
     """Returns A if i is None, otherwise returns Ai as spmatrix"""
     if i==None and self._A: return self._A
     elif i>=0 and i<=self.m and self._A:
         r = self._A.CCS[1][self._A.CCS[0][i]:self._A.CCS[0][i+1]]
         v = self._A.CCS[2][self._A.CCS[0][i]:self._A.CCS[0][i+1]]
         if isinstance(v,float): I = [r/self.n]; J = [r%self.n]
         else: I,J = misc.ind2sub(self.n,r)
         return spmatrix(v,I,J,(self.n,self.n))
     elif self._A: raise ValueError("index is out of range")
     else: raise AttributeError("SDP object has not been initialized")
示例#33
0
    def gcall(self):
        system.DAE._list2matrix()
        delta = system.DAE.x[self.delta]
        omega = system.DAE.x[self.omega]
        e1q = system.DAE.x[self.e1q]
        e1d = system.DAE.x[self.e1d]
        e2q = system.DAE.x[self.e2q]
        e2d = system.DAE.x[self.e2d]

        ag = system.DAE.y[self.a]
        vg = mul(self.u, system.DAE.y[self.v])
        ss = sin(delta - ag)
        cc = cos(delta - ag)

        self.Id = mul(-vg, (mul(self.c1, ss) + mul(self.c3, cc)))
        self.Iq = mul(vg, (mul(self.c2, ss) - mul(self.c1, cc)))

        self.Id = self.Id + mul(self.c1, e2d) + mul(self.c3, e2q)
        self.Iq = self.Iq - mul(self.c2, e2d) + mul(self.c1, e2q)

        ones = [0] * self.n
        system.DAE.g = system.DAE.g -\
                       spmatrix(system.DAE.y[self.p], self.a, ones, (system.DAE.ny, 1)) - \
                       spmatrix(system.DAE.y[self.q], self.v, ones, (system.DAE.ny, 1)) + \
                       spmatrix(mul(vg, (mul(self.Id, ss)+mul(self.Iq, cc)))-system.DAE.y[self.p], self.p, ones, (system.DAE.ny, 1)) + \
                       spmatrix(mul(vg, (mul(self.Id, cc) - mul(self.Iq, ss)))-system.DAE.y[self.q], self.q, ones, (system.DAE.ny, 1)) + \
                       spmatrix(mul(self.u, (self.pm0 - system.DAE.y[self.pm])), self.pm, ones, (system.DAE.ny, 1)) + \
                       spmatrix(mul(self.u, (self.vf0 - system.DAE.y[self.vf])), self.vf, ones, (system.DAE.ny, 1))
def upper_bound_constraint(assets_number: int, upper_constraints: Union[float, Sequence[float]]) \
        -> Tuple[matrix, matrix]:
    """
    Creates a constraint which assures that no weight exceeds its upper bound value.
    """
    upper_constraints = prepare_upper_bounds_vector(assets_number,
                                                    upper_constraints)
    upper_constraints = matrix(upper_constraints)

    G = spmatrix(1.0, range(assets_number), range(assets_number))
    h = matrix(upper_constraints)

    return G, h
示例#35
0
def _calculate_lovasz_embeddings_(A):
    """Calculate the lovasz embeddings for the given graph.

    Parameters
    ----------
    A : np.array, ndim=2
        The adjacency matrix.

    Returns
    -------
    lovasz_theta : float
        Returns the lovasz theta number.

    optimization_solution : np.array
        Returns the slack variable solution
        of the primal optimization program.

    """
    if A.shape[0] == 1:
        return 1.0
    else:
        tf_adjm = (np.abs(A) <= min_weight)
        np.fill_diagonal(tf_adjm, False)
        i_list, j_list = np.nonzero(np.triu(tf_adjm.astype(int), k=1))

        nv = A.shape[0]
        ne = len(i_list)

        x_list = list()
        e_list = list()
        for (e, (i, j)) in enumerate(zip(i_list, j_list)):
            e_list.append(int(e)), x_list.append(int(i * nv + j))
            if tf_adjm[i, j]:
                e_list.append(int(e)), x_list.append(int(i * nv + j))

    # Add on the last row, diagonal elements
    e_list = e_list + (nv * [ne])
    x_list = x_list + [int(i * nv + i) for i in range(nv)]

    # initialise g sparse (to values -1, based on two list that
    # define index and one that defines shape
    g_sparse = spmatrix(-1, x_list, e_list, (nv * nv, ne + 1))

    # Initialise optimization parameters
    h = matrix(-1.0, (nv, nv))
    c = matrix([0.0] * ne + [1.0])

    # Solve the convex optimization problem
    sol = sdp(c, Gs=[g_sparse], hs=[h])

    return np.array(sol['ss'][0]), sol['x'][ne]
示例#36
0
    def Gycall(self):

        if self.n == 0:
            return

        slip = system.DAE.x[self.slip]

        V = mul(self.u, system.DAE.y[self.v])  # 后续得修改
        t = system.DAE.y[self.a]
        st = sin(t)
        ct = cos(t)

        Vr = mul(V, st)
        Vm = mul(V, ct)

        e1r = system.DAE.x[self.e1r]
        e1m = system.DAE.x[self.e1m]

        a03 = mul(self.rs, self.rs) + mul(self.x1, self.x1)
        a13 = div(self.rs, a03)
        a23 = div(self.x1, a03)
        a33 = self.x0 - self.x1

        Im = mul(-a23, -Vr - e1r + mul(a13, (Vm - e1m)))
        Ir = mul(a13, -Vr - e1r) + mul(a23, Vm - e1m)

        Am = mul(a23, Vm) - mul(a13, Vr)
        Ar = mul(-a13, Vm) - mul(a23, Vr)
        Bm = mul(a23, st) + mul(a13, ct)
        Br = mul(-a13, st) + mul(a23, ct)

        system.DAE.Gy = system.DAE.Gy \
                       + spmatrix(mul(-Vm, Ir)-mul(Vr, Ar)-mul(Vr, Im)+mul(Vm, Am), self.a, self.a, (system.DAE.ny, system.DAE.ny)) \
                       + spmatrix(mul(-st, Ir)-mul(Vr, Br)+mul(ct, Im)+mul(Vm, Bm), self.a, self.v, (system.DAE.ny, system.DAE.ny)) \
                       + spmatrix(mul(Vm, Im)+mul(Vr, Am)-mul(Vr, Ir)+mul(Vm, Ar), self.v, self.a,
                                   (system.DAE.ny, system.DAE.ny)) \
                        + spmatrix(mul(st, Im)+mul(Vr, Bm)+mul(ct, Ir)+mul(Vm, Br), self.v, self.v,
                                   (system.DAE.ny, system.DAE.ny))
示例#37
0
    def _gen_bandsdp(self, n, m, bw, seed):
        """Random data generator for SDP with band structure"""
        setseed(seed)

        I = matrix(
            [i for j in xrange(n) for i in xrange(j, min(j + bw + 1, n))])
        J = matrix(
            [j for j in xrange(n) for i in xrange(j, min(j + bw + 1, n))])
        V = spmatrix(0., I, J, (n, n))
        Il = misc.sub2ind((n, n), I, J)
        Id = matrix([i for i in xrange(len(Il)) if I[i] == J[i]])

        # generate random y with norm 1
        y0 = normal(m, 1)
        y0 /= blas.nrm2(y0)

        # generate random S0
        S0 = mk_rand(V, cone='posdef', seed=seed)
        X0 = mk_rand(V, cone='completable', seed=seed)

        # generate random A1,...,Am and set A0 = sum Ai*yi + S0
        A_ = normal(len(I), m + 1, std=1. / len(I))
        u = +S0.V
        blas.gemv(A_[:, 1:], y0, u, beta=1.0)
        A_[:, 0] = u
        # compute b
        x = +X0.V
        x[Id] *= 0.5
        self._b = matrix(0., (m, 1))
        blas.gemv(A_[:, 1:], x, self._b, trans='T', alpha=2.0)
        # form A
        self._A = spmatrix(A_[:], [i for j in xrange(m + 1) for i in Il],
                           [j for j in xrange(m + 1) for i in Il],
                           (n**2, m + 1))

        self._X0 = X0
        self._y0 = y0
        self._S0 = S0
示例#38
0
    def Gycall(self):
        system.DAE.y = matrix(system.DAE.y)
        V = 2 * system.DAE.y[self.v]  #shunt bus 的索引?
        conductance = self.g  #网络中每条母线的电导列向量
        conductance = matrix(conductance)
        susceptance = self.b  #电纳列向量
        susceptance = matrix(susceptance)

        m = len(system.DAE.y)  #代数变量的个数
        conducv = mul(conductance, V)

        suscepv = mul(susceptance, V)

        spconducv = spmatrix(conducv, self.a, self.v, (m, m), 'z')

        spcsuscepv = spmatrix(suscepv, self.v, self.v, (m, m), 'z')

        system.DAE.Gy = system.DAE.Gy + spmatrix(conducv, self.a, self.v,
                                                 (m, m), 'z') - spmatrix(
                                                     suscepv, self.v, self.v,
                                                     (m, m), 'z')
        system.DAE.Gy = sparse(system.DAE.Gy)
        system.DAE.Gy = matrix(system.DAE.Gy.real())
示例#39
0
文件: base.py 项目: cvxopt/smcp
def robustLS_toep(q,r,delta=None,seed=0):
    """
    Random data generator for matrix norm minimization SDP.

    Al,b = robustLS_toep(q,r,delta=0.1,seed=0])

    PURPOSE
    Generates random data for a robust least squares problem
    with Toeplitz structure:

      minimize e_wc(x)^2

    where

      e_wc(x)=sup_{norm(u)<=1} norm((Ab+A1*u1+...+Ar*ur)*x - b),

      Ab,A1,...,Ar \in \reals^{p \times q}

    The parameter r determines the number of nonzero diagonals
    in Ab (1 <= r <= p).

    ARGUMENTS
    q         integer

    r         integer

    delta     float

    RETURNS
    Al        list of CVXOPT matrices, [Ab,A1,...,Ar]

    b         CVXOPT matrix

    """
    setseed(seed)

    p = q+r-1
    Alist = [sparse(misc.toeplitz(matrix([normal(r,1),matrix(0.,(p-r,1))],(p,1)),matrix(0.,(q,1))))]
    x = normal(q,1)
    b = normal(p,1,std=0.1)
    b += Alist[0]*x

    if delta is None:
        delta = 1./r

    for i in range(r):
        Alist.append(spmatrix(delta,range(i,min(p,q+i)),range(min(q,p-i)),(p,q)))

    return robustLS_SDP(Alist, b)
示例#40
0
def compute_eigs(As):
    (mu, N) = eig(matrix(As))
    N = matrix(N)
    n = len(mu)
    idx = range(n)
    W = matrix(spmatrix(1.0, idx, idx, (n, n), v.typecode))
    gesv(N, W)
    pf = mul(abs(W.T), abs(V))
    b = matrix(1.0, (1, n))
    WN = b * pf
    pf = pf.T
    for item in idx:
        mur = mu[item].real
        mui = mu[item].imag
        mu[item] = complex(round(mur, 5), round(mui, 5))
        pf[item, :] /= WN[item]
示例#41
0
 def get_A(self, i=None):
     """Returns A if i is None, otherwise returns Ai as spmatrix"""
     if i == None and self._A: return self._A
     elif i >= 0 and i <= self.m and self._A:
         r = self._A.CCS[1][self._A.CCS[0][i]:self._A.CCS[0][i + 1]]
         v = self._A.CCS[2][self._A.CCS[0][i]:self._A.CCS[0][i + 1]]
         if isinstance(v, float):
             I = [r / self.n]
             J = [r % self.n]
         else:
             I, J = misc.ind2sub(self.n, r)
         return spmatrix(v, I, J, (self.n, self.n))
     elif self._A:
         raise ValueError, "index is out of range"
     else:
         raise AttributeError, "SDP object has not been initialized"
示例#42
0
文件: base.py 项目: cvxopt/smcp
def mk_rand(V,cone='posdef',seed=0):
    """
    Generates random matrix U with sparsity pattern V.
    - U is positive definite if cone is 'posdef'.
    - U is completable if cone is 'completable'.
    """
    if not (cone=='posdef' or cone=='completable'):
        raise ValueError("cone must be 'posdef' (default) or 'completable' ")

    from cvxopt import amd
    setseed(seed)
    n = V.size[0]

    U = +V
    U.V *= 0.0
    for i in range(n):
        u = normal(n,1)/sqrt(n)
        base.syrk(u,U,beta=1.0,partial=True)

    # test if U is in cone: if not, add multiple of identity
    t = 0.1; Ut = +U
    p = amd.order(Ut)
    # Vc, NF = chompack.embed(U,p)
    symb = chompack.symbolic(U,p)
    Vc = chompack.cspmatrix(symb) + U
    while True:
        # Uc = chompack.project(Vc,Ut)
        Uc = chompack.cspmatrix(symb) + Ut
        try:
            if cone=='posdef':
                # positive definite?
                # Y = chompack.cholesky(Uc)
                Y = Uc.copy()
                chompack.cholesky(Y)
            elif cone=='completable':
                # completable?
                # Y = chompack.completion(Uc)
                Y = Uc.copy()
                chompack.completion(Y)
            # Success: Ut is in cone
            U = +Ut
            break
        except:
            Ut = U + spmatrix(t,range(n),range(n),(n,n))
            t*=2.0
    return U
示例#43
0
def Lplus(L):
	try:
		nrow,ncol = L.shape
		Lcoo = L.tocoo()
		L = spmatrix(Lcoo.data.tolist(),Lcoo.row.tolist(),Lcoo.col.tolist())
	except AttributeError:
		nrow,ncol = L.size
	ones = matrix(-1.0/nrow,(nrow,ncol))
	ones+= spdiag(matrix(1.0,(1,nrow)))
	red = L[:-1,:-1]
	sol = ones[:-1,:]
	linsolve(red,sol,uplo='L')
	sol = array(sol)
	s = matrix(0.0,(1,ncol))
	s[0,:]=sol.sum(0)/nrow
	lplus = zeros((nrow,ncol))
	lplus[nrow-1,:] = -s[0,:]
	lplus[:-1,:] = sol-s
	return array(lplus)
示例#44
0
def cvxopt_modeling2cvxopt_matrices(op):
    '''
    Converts cvxopt.modeling.op instance into its matrices c,A,b,G,h 
    where op represents the problem
    
    minimize c'x
    st       Ax = b
             Gx+h = s
             s >= 0
    '''
    t = op._inmatrixform(format)

    if t is None:
        lp1 = self
    else:
        lp1, vmap, mmap = t[0], t[1], t[2]

    variables = lp1.variables()
    if not variables: 
        raise TypeError('lp must have at least one variable')
    x = variables[0]
    c = lp1.objective._linear._coeff[x]
    #if _isspmatrix(c): c = matrix(c, tc='d')

    inequalities = lp1._inequalities
    if not inequalities:
        raise TypeError('lp must have at least one inequality')
    G = inequalities[0]._f._linear._coeff[x]
    h = -inequalities[0]._f._constant

    equalities = lp1._equalities
    if equalities:
        A = equalities[0]._f._linear._coeff[x]
        b = -equalities[0]._f._constant
    elif format == 'dense':
        A = matrix(0.0, (0,len(x)))
        b = matrix(0.0, (0,1))
    else:
        A = spmatrix(0.0, [], [],  (0,len(x)))
        b = matrix(0.0, (0,1))
    
    return(c,A,b,G,h)
示例#45
0
文件: base.py 项目: cvxopt/smcp
    def solve_phase1(self,kktsolver='chol',MM = 1e5):
        """
        Solves primal Phase I problem using the feasible
        start solver.

        Returns primal feasible X.

        """
        from cvxopt import cholmod, amd
        k = 1e-3

        # compute Schur complement matrix
        Id = [i*(self.n+1) for i in range(self.n)]
        As = self._A[:,1:]
        As[Id,:] /= sqrt(2.0)

        M = spmatrix([],[],[],(self.m,self.m))
        base.syrk(As,M,trans='T')
        u = +self.b

        # compute least-norm solution
        F = cholmod.symbolic(M)
        cholmod.numeric(M,F)
        cholmod.solve(F,u)
        x = 0.5*self._A[:,1:]*u
        X0 = spmatrix(x[self.V[:].I],self.V.I,self.V.J,(self.n,self.n))

        # test feasibility
        p = amd.order(self.V)
        #Xc,Nf = chompack.embed(X0,p)
        #E = chompack.project(Xc,spmatrix(1.0,range(self.n),range(self.n)))
        symb = chompack.symbolic(self.V,p)
        Xc = chompack.cspmatrix(symb) + X0

        try:
            # L = chompack.completion(Xc)
            L = Xc.copy()
            chompack.completion(L)
            # least-norm solution is feasible
            return X0,None
        except:
            pass

        # create Phase I SDP object
        trA = matrix(0.0,(self.m+1,1))
        e = matrix(1.0,(self.n,1))
        Aa = self._A[Id,1:]
        base.gemv(Aa,e,trA,trans='T')
        trA[-1] = MM
        P1 = SDP()
        P1._A = misc.phase1_sdp(self._A,trA)
        P1._b = matrix([self.b-k*trA[:self.m],MM])
        P1._agg_sparsity()

        # find feasible starting point for Phase I problem
        tMIN = 0.0
        tMAX = 1.0
        while True:
            t = (tMIN+tMAX)/2.0
            #Xt = chompack.copy(Xc)
            #chompack.axpy(E,Xt,t)
            Xt = Xc.copy() + spmatrix(t,list(range(self.n)),list(range(self.n)))

            try:
                # L = chompack.completion(Xt)
                L = Xt.copy()
                chompack.completion(L)
                tMAX = t
                if tMAX - tMIN < 1e-1:
                    break
            except:
                tMAX *= 2.0
                tMIN = t

        tt = t + 1.0
        U = X0 + spmatrix(tt,list(range(self.n,)),list(range(self.n)))
        trU = sum(U[:][Id])

        Z0 = spdiag([U,spmatrix([tt+k,MM-trU],[0,1],[0,1],(2,2))])
        sol = P1.solve_feas(primalstart = {'x':Z0}, kktsolver = kktsolver)

        s = sol['x'][-2,-2] - k
        if s > 0:
            return None,P1
        else:

            sol.pop('y')
            sol.pop('s')
            X0 = sol.pop('x')[:self.n,:self.n]\
                - spmatrix(s,list(range(self.n)),list(range(self.n)))
            return X0,sol
示例#46
0
文件: base.py 项目: cvxopt/smcp
 def V(self):
     """Sparsity pattern (CVXOPT spmatrix)"""
     I,J = misc.ind2sub(self.n,self.I)
     return spmatrix(0.,I,J,(self.n,self.n))
示例#47
0
    m, N, p = u.size[0], u.size[1], y.size[0]
    r = min(int(30/p),int((N+1.0)/(p+m+1)+1.0))
    a = r*p
    c = r*m
    b = N-r+1
    d = b-c
    U = sysid.Hankel(u,r,b,p=m,q=1)
    Vt = matrix(0.0,(b,b))
    Stemp = matrix(0.0,(c,1))
    Un = matrix(0.0,(b,d))
    lapack.gesvd(U,Stemp,jobvt='A',Vt=Vt)
    Un[:,:] = Vt.T[:,c:]

    AA = sysid.Hankel_basis(r,b,p=p,q=1)
    A = matrix(0.0,(a*d,p*N))
    temp = spmatrix([],[],[],(a,b),'d')
    temp2 = matrix(0.0,(a,d))
    for ii in xrange(p*N):
        temp[:] = AA[:,ii]
        base.gemm(temp,Un,temp2)
        A[:,ii] = temp2[:]
    B = matrix(0.0,(a,d))
   
    # flip the matrix if columns is more than rows
    if a < d:
        Itrans = [i+j*a for i in xrange(a) for j in xrange(d)]
        B[:] = B[Itrans]
        B.size = (d,a)
        for ii in xrange(p*N):
            A[:,ii] = A[Itrans,ii]
    
示例#48
0
def sysid(y, u, vsig, svth = None):

    """
    System identification using the subspace method and nuclear norm 
    optimization.  Estimate a linear time-invariant state-space model 
    given inputs and outputs.  The algorithm is described in [1].
    

    INPUT
    y       'd' matrix of size (p, N).  y are the measured outputs, p is 
            the number of outputs, and N is the number of data points 
            measured. 
    
    u       'd' matrix of size (m, N).  u are the inputs, m is the number 
            of inputs, and N is the number of data points.
    
    vsig    a weighting parameter in the nuclear norm optimization, its 
            value is approximately the 1-sigma output noise level
    
    svth    an optional parameter, if specified, the model order is 
            determined as the number of singular values greater than svth 
            times the maximum singular value.  The default value is 1E-3 
    
    OUTPUT
    sol     a dictionary with the following words
            -- 'A', 'B', 'C', 'D' are the state-space matrices
            -- 'svN', the original singular values of the Hankel matrix
            -- 'sv', the optimized singular values of the Hankel matrix
            -- 'x0', the initial state x(0)
            -- 'n', the model order

    [1] Zhang Liu and Lieven Vandenberghe. "Interior-point method for 
        nuclear norm approximation with application to system 
        identification."  

    """

    m, N, p = u.size[0], u.size[1], y.size[0]
    if y.size[1] != N:
        raise ValueError, "y and u must have the same length"
           
    # Y = G*X + H*U + V, Y has size a x b, U has size c x b, Un has b x d
    r = min(int(30/p),int((N+1.0)/(p+m+1)+1.0))
    a = r*p
    c = r*m
    b = N-r+1
    d = b-c
    
    # construct Hankel matrix Y
    Y = Hankel(y,r,b,p=p,q=1)
    
    # construct Hankel matrix U
    U = Hankel(u,r,b,p=m,q=1)
    
    # compute Un = null(U) and YUn = Y*Un
    Vt = matrix(0.0,(b,b))
    Stemp = matrix(0.0,(c,1))
    Un = matrix(0.0,(b,d))
    YUn = matrix(0.0,(a,d))
    lapack.gesvd(U,Stemp,jobvt='A',Vt=Vt)
    Un[:,:] = Vt.T[:,c:]
    blas.gemm(Y,Un,YUn)
    
    # compute original singular values
    svN = matrix(0.0,(min(a,d),1))
    lapack.gesvd(YUn,svN)
    
    # variable, [y(1);...;y(N)]
    # form the coefficient matrices for the nuclear norm optimization
    # minimize | Yh * Un |_* + alpha * | y - yh |_F
    AA = Hankel_basis(r,b,p=p,q=1)
    A = matrix(0.0,(a*d,p*N))
    temp = spmatrix([],[],[],(a,b),'d')
    temp2 = matrix(0.0,(a,d))
    for ii in xrange(p*N):
        temp[:] = AA[:,ii]
        base.gemm(temp,Un,temp2)
        A[:,ii] = temp2[:]
    B = matrix(0.0,(a,d))

    # flip the matrix if columns is more than rows
    if a < d:
        Itrans = [i+j*a for i in xrange(a) for j in xrange(d)]
        B[:] = B[Itrans]
        B.size = (d,a)
        for ii in xrange(p*N):
            A[:,ii] = A[Itrans,ii]
      
    # regularized term
    x0 = y[:]
    Qd = matrix(2.0*svN[0]/p/N/(vsig**2),(p*N,1))

    # solve the nuclear norm optimization
    sol = nrmapp(A, B, C = base.spdiag(Qd), d = -base.mul(x0, Qd))
    status = sol['status']
    x = sol['x']
    
    # construct YhUn and take the svd
    YhUn = matrix(B)
    blas.gemv(A,x,YhUn,beta=1.0)
    if a < d:
        YhUn = YhUn.T
    Uh = matrix(0.0,(a,d))
    sv = matrix(0.0,(d,1))
    lapack.gesvd(YhUn,sv,jobu='S',U=Uh)

    # determine model order
    if svth is None:
        svth = 1E-3
    svthn = sv[0]*svth
    n=1
    while sv[n] >= svthn and n < 10:
        n=n+1
    
    # estimate A, C
    Uhn = Uh[:,:n]
    for ii in xrange(n):
        blas.scal(sv[ii],Uhn,n=a,offset=ii*a)
    syseC = Uhn[:p,:]
    Als = Uhn[:-p,:]
    Bls = Uhn[p:,:]
    lapack.gels(Als,Bls)
    syseA = Bls[:n,:]
    Als[:,:] = Uhn[:-p,:]
    Bls[:,:] = Uhn[p:,:]
    blas.gemm(Als,syseA,Bls,beta=-1.0)
    Aerr = blas.nrm2(Bls)
    
    # stabilize A
    Sc = matrix(0.0,(n,n),'z')
    w = matrix(0.0, (n,1), 'z')
    Vs = matrix(0.0, (n,n), 'z')
    def F(w):
        return (abs(w) < 1.0)
    
    Sc[:,:] = syseA
    ns = lapack.gees(Sc, w, Vs, select = F)
    while ns < n:
        #print "stabilize matrix A"
        w[ns:] = w[ns:]**-1
        Sc[::n+1] = w
        Sc = Vs*Sc*Vs.H
        syseA[:,:] = Sc.real()
        Sc[:,:] = syseA
        ns = lapack.gees(Sc, w, Vs, select = F)

    # estimate B,D,x0 stored in vector [x0; vec(D); vec(B)]
    F1 = matrix(0.0,(p*N,n))
    F1[:p,:] = syseC
    for ii in xrange(1,N):
        F1[ii*p:(ii+1)*p,:] = F1[(ii-1)*p:ii*p,:]*syseA
    F2 = matrix(0.0,(p*N,p*m))
    ut = u.T
    for ii in xrange(p):
        F2[ii::p,ii::p] = ut
    F3 = matrix(0.0,(p*N,n*m))
    F3t = matrix(0.0,(p*(N-1),n*m))
    for ii in xrange(1,N):
        for jj in xrange(p):
            for kk in xrange(n):
                F3t[jj:jj+(N-ii)*p:p,kk::n] = ut[:N-ii,:]*F1[(ii-1)*p+jj,kk]
        F3[ii*p:,:] = F3[ii*p:,:] + F3t[:(N-ii)*p,:]
    
    F = matrix([[F1],[F2],[F3]])
    yls = y[:]
    Sls = matrix(0.0,(F.size[1],1))
    Uls = matrix(0.0,(F.size[0],F.size[1]))
    Vtls = matrix(0.0,(F.size[1],F.size[1]))
    lapack.gesvd(F, Sls, jobu='S', jobvt='S', U=Uls, Vt=Vtls)
    Frank=len([ii for ii in xrange(Sls.size[0]) if Sls[ii] >= 1E-6])
    #print 'Rank deficiency = ', F.size[1] - Frank
    xx = matrix(0.0,(F.size[1],1))
    xx[:Frank] = Uls.T[:Frank,:] * yls
    xx[:Frank] = base.mul(xx[:Frank],Sls[:Frank]**-1)
    xx[:] = Vtls.T[:,:Frank]*xx[:Frank] 
    blas.gemv(F,xx,yls,beta=-1.0)
    xxerr = blas.nrm2(yls)
    
    x0 = xx[:n]
    syseD = xx[n:n+p*m]
    syseD.size = (p,m)
    syseB = xx[n+p*m:]
    syseB.size = (n,m)
    
    return {'A': syseA, 'B': syseB, 'C': syseC, 'D': syseD, 'svN': svN, 'sv': \
        sv, 'x0': x0, 'n': n, 'Aerr': Aerr, 'xxerr': xxerr}