Example #1
0
	def __init__(self, data, target):
		self.data = matrix(data).trans()
		self.target = matrix(target)
		self.Pmatrix = matrix(0, (self.data.size[0], self.data.size[0]), 'd')
		self.q = matrix(-1, (self.data.size[0],1),'d')
		self.h = matrix(0, (self.data.size[0],1), 'd')
		self.G = spdiag(matrix(-1, (1, self.data.size[0]), 'd'))
		self.alphas = []
		self.alphas_x = []
		self.G_slack = matrix([self.G, spdiag(matrix(1, (1, self.data.size[0]), 'd'))])
		self.h_slack = matrix([self.h, matrix(1, (self.data.size[0],1), 'd')] )
Example #2
0
 def F(x=None, z=None):
     if x is None:
         return 0, matrix(0.5, (n, 1))
     if min(x) <= 0.0:
         return None
     f = sum(x)
     Df = -(x**-1).T
     if z is None:
         return f, Df
     H = spdiag(z[0] * x**-2)
     return f, Df, H
Example #3
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'
Example #4
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)
Example #5
0
    def what_is_the_matrix(X, Z):
        """find the optimal morphing matrix A such that A * src_dist = target_dist"""

        #print "============ What is the Matrix? ==============="

        n = len(Z)
        N = n**2

        #print "X =", X.T
        #print "Z =", Z.T

        # Equality Constraints
        A_list = []
        b_list = []
        #  -- the columns of the matrix must be valid PDF's
        A_pdf = matrix(0.0, (n, N), 'd')
        for i in range(n):
            A_pdf[i, n * i:n * i + n] = 1.0
        b_pdf = matrix(1.0, (n, 1), 'd')
        #print "A_pdf ="
        #print A_pdf
        #print "b_pdf ="
        #print b_pdf
        A_list.append(A_pdf)
        b_list.append(b_pdf)

        #  -- the matrix must morph X to Z
        A_morph = matrix(0.0, (n, N), 'd')
        for i in range(n):
            matrix_vers = matrix(0.0, (n, n), 'd')
            matrix_vers[i, :] = X.T
            row = matrix(matrix_vers, (1, N), 'd')
            A_morph[i, :] = row
        b_morph = matrix(Z, (n, 1), 'd')
        A_list.append(A_morph)
        b_list.append(b_morph)

        #print "A_morph ="
        #print A_morph
        #print "b_morph ="
        #print b_morph

        # concatenate all our equality constraints into one coeff matrix and one b vector
        #A_list = [A_morph, A_pdf]
        #b_list = [b_morph, b_pdf]
        #A_list = [A_pdf]
        #b_list = [b_pdf]
        #A = matrix(A_list)
        A = sparse(A_list)
        b = matrix(b_list)

        #print "A ="
        #print A
        #print "b ="
        #print b

        # Inequality Constraints -- in order to be a valid PDF, each cell a_ij must be 0 <= a_ij <= 1
        G_list = []
        h_list = []
        G_lt = spdiag(matrix(1.0, (N, 1), 'd'))
        h_lt = matrix(1.0, (N, 1), 'd')
        # cvw: as mentioned in the comment above in find_the_one(), the "less than" constraints are
        #      in fact redundant given that we already require the columns to sum to 1.0 and require
        #      (below) that each prob is >= 0.  yay for smaller KKT matrices.
        #G_list.append(G_lt)
        #h_list.append(h_lt)

        G_gt = spdiag(matrix(-1.0, (N, 1), 'd'))
        h_gt = matrix(0.0, (N, 1), 'd')
        G_list.append(G_gt)
        h_list.append(h_gt)

        # cvw: I guess we could add some more constraints if we really wanted to..
        #      i.e. only downgrade the bit rate 10% of the time or less
        #      but for now these'll do

        G = sparse(G_list)
        h = matrix(h_list)
        #print "G ="
        #print G
        #print "h ="
        #print h

        #print "vectorized cost matrix ="
        #print c.T

        # now run the cvxopt solver to get our answer
        #print "running cvxopt lp() solver..."
        ans = solvers.lp(cost_matrix, G=G, h=h, A=A, b=b, solver='glpk')
        ##print ans['x']
        #print "answer = ", ans
        A = None
        if ans['x']:
            cost = cost_matrix.T * ans['x']

            # A is the morphing matrix
            A = matrix(ans['x'], (n, n), 'd')

        return A
Example #6
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}
Example #7
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}
Example #8
0
    if a < d:
        p, q = d, a
    else:
        p, q = a, d
    x0 = y[:]
    
S = matrix(0.0, (q,1))

# First, compute two initial points on the tradeoff curve
lamT = [50.0, 0.02]
nrmT = [0.0, 0.0]
errT = [0.0, 0.0]

for i in xrange(len(lamT)):
    Cd = matrix(2.0*lamT[i], (n,1))
    C = base.spdiag(Cd)
    d = -base.mul(Cd, x0)
    
    sol = nucnrm.nrmapp(A, B, C = C, d = d)
    x = sol['x']
    
    lapack.gesvd(matrix(A*x, (p,q)) + B, S)
    nrmT[i] = sum(S)
    errT[i] = blas.dot(x-x0, x-x0)
   
# plot the tradeoff curve upper/lower bounds with the initial 2 points
pylab.figure(0)
N = 200
slope = -matrix(lamT)
errM = matrix(errT)
nrmM = matrix(nrmT)
Example #9
0
    if a < d:
        p, q = d, a
    else:
        p, q = a, d
    x0 = y[:]

S = matrix(0.0, (q, 1))

# First, compute two initial points on the tradeoff curve
lamT = [50.0, 0.02]
nrmT = [0.0, 0.0]
errT = [0.0, 0.0]

for i in xrange(len(lamT)):
    Cd = matrix(2.0 * lamT[i], (n, 1))
    C = base.spdiag(Cd)
    d = -base.mul(Cd, x0)

    sol = nucnrm.nrmapp(A, B, C=C, d=d)
    x = sol['x']

    lapack.gesvd(matrix(A * x, (p, q)) + B, S)
    nrmT[i] = sum(S)
    errT[i] = blas.dot(x - x0, x - x0)

# plot the tradeoff curve upper/lower bounds with the initial 2 points
pylab.figure(0)
N = 200
slope = -matrix(lamT)
errM = matrix(errT)
nrmM = matrix(nrmT)
    def what_is_the_matrix(X, Z):
        """find the optimal morphing matrix A such that A * src_dist = target_dist"""

        #print "============ What is the Matrix? ==============="

        n = len(Z)
        N = n**2

        #print "X =", X.T
        #print "Z =", Z.T

        # Equality Constraints
        A_list = []
        b_list = []
        #  -- the columns of the matrix must be valid PDF's
        A_pdf = matrix(0.0, (n,N), 'd')
        for i in range(n):
            A_pdf[i,n*i:n*i+n] = 1.0
        b_pdf = matrix(1.0, (n,1), 'd')
        #print "A_pdf ="
        #print A_pdf
        #print "b_pdf ="
        #print b_pdf
        A_list.append(A_pdf)
        b_list.append(b_pdf)

        #  -- the matrix must morph X to Z
        A_morph = matrix(0.0, (n,N), 'd')
        for i in range(n):
            matrix_vers = matrix(0.0, (n,n), 'd')
            matrix_vers[i,:] = X.T
            row = matrix(matrix_vers, (1,N), 'd')
            A_morph[i,:] = row
        b_morph = matrix(Z, (n,1), 'd')
        A_list.append(A_morph)
        b_list.append(b_morph)

        #print "A_morph ="
        #print A_morph
        #print "b_morph ="
        #print b_morph

        # concatenate all our equality constraints into one coeff matrix and one b vector
        #A_list = [A_morph, A_pdf]
        #b_list = [b_morph, b_pdf]
        #A_list = [A_pdf]
        #b_list = [b_pdf]
        #A = matrix(A_list)
        A = sparse(A_list)
        b = matrix(b_list)

        #print "A ="
        #print A
        #print "b ="
        #print b

        # Inequality Constraints -- in order to be a valid PDF, each cell a_ij must be 0 <= a_ij <= 1
        G_list = []
        h_list = []
        G_lt = spdiag(matrix(1.0, (N,1), 'd'))
        h_lt = matrix(1.0, (N,1), 'd')
        # cvw: as mentioned in the comment above in find_the_one(), the "less than" constraints are
        #      in fact redundant given that we already require the columns to sum to 1.0 and require
        #      (below) that each prob is >= 0.  yay for smaller KKT matrices.
        #G_list.append(G_lt)
        #h_list.append(h_lt)

        G_gt = spdiag(matrix(-1.0, (N,1), 'd'))
        h_gt = matrix(0.0, (N,1), 'd')
        G_list.append(G_gt)
        h_list.append(h_gt)

        # cvw: I guess we could add some more constraints if we really wanted to..
        #      i.e. only downgrade the bit rate 10% of the time or less
        #      but for now these'll do

        G = sparse(G_list)
        h = matrix(h_list)
        #print "G ="
        #print G
        #print "h ="
        #print h

        #print "vectorized cost matrix ="
        #print c.T

        # now run the cvxopt solver to get our answer
        #print "running cvxopt lp() solver..."
        ans = solvers.lp(cost_matrix, G=G, h=h, A=A, b=b, solver='glpk')
        ##print ans['x']
        #print "answer = ", ans
        A = None
        if ans['x']:
            cost = cost_matrix.T * ans['x']

            # A is the morphing matrix
            A = matrix(ans['x'], (n,n), 'd')

        return A
Example #11
0
def findpos(a, b, r, bounds=None, float_r=True):
    alpha = 10.0 # preference for moving circles over shrinking them
    beta = 100 # Every circle would ideally be 100 pixels larger
    small = 0.01 # Initial radius for float_r
    # P and q represent the energy function, distance from (a,b), that we are
    # trying to minimize

    N = len(a)
    n = 2 * N

    pos = []
    pos.extend(a)
    pos.extend(b)
    if float_r:
        pos.extend(r)
    ideals = matrix(pos, tc='d')
    q = -ideals
    init = ideals
    if float_r:
        q[2*N:3*N] -= beta
        q[2*N:3*N] *= alpha
        init[2*N:3*N] = small

    if float_r:
        n = 3 * N
        
    ones = matrix(1, (1, n), tc='d')
    if float_r:
        ones[2*N:3*N] = alpha
    P = spdiag(ones)
    
    x = []
    I = []
    J = []
    hlist = []
    
    # x, I, and J, are the sparse coordinates for building G
    # G and h provide all the constraints
    # this loop adds the constraint that the projection of (the vector
    # between any two centers) onto (the vector connecting their ideal locations)
    # must not be shorter than the sum of their radii
    c = 0
    for i in xrange(N):
        for j in xrange(i+1, N):
            dx = a[i]-a[j]
            dy = b[i]-b[j]
            d = sqrt(dx*dx + dy*dy)
                        
            x.append(-dx)
            I.append(c)
            J.append(i)
            
            x.append(dx)
            I.append(c)
            J.append(j)
            
            x.append(-dy)
            I.append(c)
            J.append(i + N)
            
            x.append(dy)
            I.append(c)
            J.append(j + N)
            
            if float_r:
                x.append(d)
                I.append(c)
                J.append(i + 2*N)
            
                x.append(d)
                I.append(c)
                J.append(j + 2*N)
                
                hlist.append(0)
            else:
                hlist.append(-d*(r[i] + r[j]))
            
            c += 1
    
    if float_r:
        # Place upper and lower bounds on each radius
        for i in xrange(N):
            x.append(1)
            I.append(c)
            J.append(i + 2*N)
            hlist.append(r[i])
            
            c += 1
            
            x.append(-1)
            I.append(c)
            J.append(i + 2*N)
            hlist.append(0)
            
            c += 1

    if bounds is not None:
            lbx = bounds[0]
            ubx = bounds[1]
            lby = bounds[2]
            uby = bounds[3]
            for i in xrange(N):
                x.append(-1)
                I.append(c)
                J.append(i)
                
                x.append(1)
                I.append(c)
                J.append(i + 2*N)
                
                hlist.append(-lbx)
                c += 1

                x.append(1)
                I.append(c)
                J.append(i)
                
                x.append(1)
                I.append(c)
                J.append(i + 2*N)
                
                hlist.append(ubx)
                c += 1
                
                x.append(-1)
                I.append(c)
                J.append(i + N)
                
                x.append(1)
                I.append(c)
                J.append(i + 2*N)
                
                hlist.append(-lby)
                c += 1

                x.append(1)
                I.append(c)
                J.append(i + N)
                
                x.append(1)
                I.append(c)
                J.append(i + 2*N)
                
                hlist.append(uby)
                c += 1
    
    else:
        if bounds is not None:
            lbx = bounds[0]
            ubx = bounds[1]
            lby = bounds[2]
            uby = bounds[3]
            for i in xrange(N):
                x.append(-1)
                I.append(c)
                J.append(i)
                hlist.append(-r[i]-lbx)
                c += 1

                x.append(1)
                I.append(c)
                J.append(i)
                hlist.append(ubx-r[i])
                c += 1
                
                x.append(-1)
                I.append(c)
                J.append(i + N)
                hlist.append(-r[i]-lby)
                c += 1

                x.append(1)
                I.append(c)
                J.append(i + N)
                hlist.append(uby-r[i])
                c += 1
    
    G = spmatrix(x, I, J)
    h = matrix(hlist, tc='d')
    
    #this line actually solves the problem
    initvals = {}
    initvals['x'] = init
    #print init
    if float_r:
        initvals['s'] = h - G*init
        #print initvals['s']
    initvals['y'] = matrix(1, (0,1), tc='d')
    # z is actually supposed to satisfy Px + G*s + Ay + c = 0, so this definition
    # is extremely far from correct
    #initvals['z'] = 0 * h 
    
    outdict = qp(P, q, G, h, initvals=initvals)
    #print outdict
    if outdict['status'] == 'optimal':    
        bestpos = outdict['x']
        #print bestpos - ideals
        
        bestx = bestpos[:N]
        besty = bestpos[N:2*N]
        if float_r:
            bestr = bestpos[2*N:3*N]
        else:
            bestr = r
    
        return (list(bestx), list(besty), list(bestr))
    else:
        return None
Example #12
0
File: base.py Project: 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
Example #13
0
    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
        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, range(self.n), 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, range(self.n, ), 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,range(self.n),range(self.n))
            return X0, sol
Example #14
0
              random.normalvariate(0.5, 1),
              1.0)
             for i in range(5)]

classB = [(random.normalvariate(0.0, 0.5), random.normalvariate(-0.5,
                                                                0.5), -1.0)
          for i in range(10)]

data = classA + classB
random.shuffle(data)

# Build P,q,h and G
P = buildP(data)
N = 20
q = matrix(-1.0, (N, 1))  # Vector of size N containing only -1
G = matrix(spdiag(
    N * [-1.0]))  # Diagonal matrix with -1 on the diagonal, 0 otherwise
h = matrix(0.0, (N, 1))  # Vector of size N containing only 0

# QP Optimizer
r = qp(matrix(P), matrix(q), matrix(G), matrix(h))
alpha = list(r['x'])

# Get support vectors
sv = getSupportVectors(alpha)

# Plot data
pylab.hold(True)
pylab.plot([p[0] for p in classA], [p[1] for p in classA], 'bo')
pylab.plot([p[0] for p in classB], [p[1] for p in classB], 'ro')

# Plot boundaries