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.
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
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)
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
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)
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.
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 xrange(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, xrange(n), xrange(n), (n, n)) t *= 2.0 return U
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
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
to the slope of the line connecting the two points. """ import pylab, math, pickle, nucnrm, sysid from cvxopt import base, blas, lapack, solvers from cvxopt.base import normal, matrix, spmatrix, mul maxiter = 8 data = "random" #data = "sysid" solvers.options['show_progress'] = False if data is "random": # Generate random problem data base.setseed(0) p, q, n = 20, 20, 40 A = normal(p*q, n) B = normal(p, q) x0 = normal(n, 1) elif data is "sysid": iddata = pickle.load(open("CD_player_arm.bin","r")) N = 100 u = iddata['u'][:,:N] y = iddata['y'][:,:N] 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
to the slope of the line connecting the two points. """ import pylab, math, pickle, nucnrm, sysid from cvxopt import base, blas, lapack, solvers from cvxopt.base import normal, matrix, spmatrix, mul maxiter = 8 data = "random" #data = "sysid" solvers.options['show_progress'] = False if data is "random": # Generate random problem data base.setseed(0) p, q, n = 20, 20, 40 A = normal(p * q, n) B = normal(p, q) x0 = normal(n, 1) elif data is "sysid": iddata = pickle.load(open("CD_player_arm.bin", "r")) N = 100 u = iddata['u'][:, :N] y = iddata['y'][:, :N] 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
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 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, 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 xrange(N)], (n**2, 1)) ], [ spmatrix( normal(nz * m, 1), [i for j in xrange(m) for i in random.sample(Il, nz)], [j for j in xrange(m) for i in xrange(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 xrange(N)], (n**2, 1)) ], [ spmatrix(normal(nnz, 1), [ i for j in xrange(m) for i in random.sample(Il, nz[j]) ], [j for j in xrange(m) for i in xrange(nz[j])], (n**2, m)) ]]) else: raise TypeError # compute A0 u = +S0.V for k in xrange(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 xrange(m): base.gemv(A[:, k + 1][Il], X0.V, u, trans='T', alpha=2.0) self._b[k] = u