def get_2state_gaussian_seq(lens,dims=2,means1=[2,2,2,2],means2=[5,5,5,5],vars1=[1,1,1,1],vars2=[1,1,1,1],anom_prob=1.0): seqs = co.matrix(0.0, (dims, lens)) lbls = co.matrix(0, (1,lens)) marker = 0 # generate first state sequence for d in range(dims): seqs[d,:] = co.normal(1,lens)*vars1[d] + means1[d] prob = np.random.uniform() if prob<anom_prob: # add second state blocks while (True): max_block_len = 0.6*lens min_block_len = 0.1*lens block_len = np.int(max_block_len*np.single(co.uniform(1))+3) block_start = np.int(lens*np.single(co.uniform(1))) if (block_len - (block_start+block_len-lens)-3>min_block_len): break block_len = min(block_len,block_len - (block_start+block_len-lens)-3) lbls[block_start:block_start+block_len-1] = 1 marker = 1 for d in range(dims): #print block_len seqs[d,block_start:block_start+block_len-1] = co.normal(1,block_len-1)*vars2[d] + means2[d] return (seqs, lbls, marker)
def test(): m, n = 100, 1000 setseed() A = normal(m, n) b = normal(m) x = l1regls(A, b) return x
def test_l1(self): setseed(100) m, n = 500, 250 P = normal(m, n) q = normal(m, 1) u1 = l1(P, q) u2 = l1blas(P, q) self.assertAlmostEqualLists(list(u1), list(u2), places=3)
def test_cvxopt_sparse(self): m = 100 n = 20 mu = cvxopt.exp(cvxopt.normal(m)) F = cvxopt.normal(m, n) D = cvxopt.spdiag(cvxopt.uniform(m)) x = Variable(m) exp = square(norm2(D * x))
def test_l1(self): setseed(100) m,n = 500,250 P = normal(m,n) q = normal(m,1) u1,st1 = l1(P,q) u2,st2 = l1blas(P,q) self.assertTrue(st1 == 'optimal') self.assertTrue(st2 == 'optimal') self.assertAlmostEqualLists(list(u1),list(u2),places=3)
def test_l1(self): from cvxopt import normal, setseed import l1 setseed(100) m,n = 500,250 P = normal(m,n) q = normal(m,1) u1 = l1.l1(P,q) u2 = l1.l1blas(P,q) self.assertAlmostEqualLists(list(u1),list(u2),places=3)
def test_l1(self): from cvxopt import normal, setseed import l1 setseed(100) m, n = 500, 250 P = normal(m, n) q = normal(m, 1) u1 = l1.l1(P, q) u2 = l1.l1blas(P, q) self.assertAlmostEqualLists(list(u1), list(u2), places=3)
def test_l1regls(self): setseed(100) m,n = 250,500 A = normal(m,n) b = normal(m,1) x,st = l1regls(A,b) self.assertTrue(st == 'optimal') # Check optimality conditions (list should be empty, e.g., False) self.assertFalse([t for t in zip(A.T*(A*x-b),x) if abs(t[1])>1e-6 and abs(t[0]) > 1.0])
def test_l1(): np.random.seed(42) m, n = 500, 100 P, q = cvxopt.normal(m, n), cvxopt.normal(m, 1) u = l1(P, q) qfit = P * u residual = qfit - q np.random.seed(None) mean_abs_res = sum(abs(residual)) / len(residual) print "mean abs residual: %s" % mean_abs_res assert mean_abs_res < 1.0
def test_l1regls(self): from cvxopt import normal, setseed import l1regls setseed(100) m,n = 250,500 A = normal(m,n) b = normal(m,1) x = l1regls.l1regls(A,b) # Check optimality conditions (list should be empty, e.g., False) self.assertFalse([t for t in zip(A.T*(A*x-b),x) if abs(t[1])>1e-6 and abs(t[0]) > 1.0])
def test_l1regls(self): setseed(100) m, n = 250, 500 A = normal(m, n) b = normal(m, 1) x = l1regls(A, b) # Check optimality conditions (list should be empty, e.g., False) self.assertFalse([ t for t in zip(A.T * (A * x - b), x) if abs(t[1]) > 1e-6 and abs(t[0]) > 1.0 ])
def setUp(self): """ Use cvxopt to get ground truth values """ from cvxopt import lapack, solvers, matrix, spdiag, log, div, normal, setseed from cvxopt.modeling import variable, op, max, sum solvers.options['show_progress'] = 0 setseed() m, n = 100, 30 A = normal(m, n) b = normal(m, 1) b /= (1.1 * max(abs(b))) self.m, self.n, self.A, self.b = m, n, A, b # l1 approximation # minimize || A*x + b ||_1 x = variable(n) op(sum(abs(A * x + b))).solve() self.x1 = x.value # l2 approximation # minimize || A*x + b ||_2 bprime = -matrix(b) Aprime = matrix(A) lapack.gels(Aprime, bprime) self.x2 = bprime[:n] # Deadzone approximation # minimize sum(max(abs(A*x+b)-0.5, 0.0)) x = variable(n) dzop = op(sum(max(abs(A * x + b) - 0.5, 0.0))) dzop.solve() self.obj_dz = sum( np.max([np.abs(A * x.value + b) - 0.5, np.zeros((m, 1))], axis=0)) # Log barrier # minimize -sum (log ( 1.0 - (A*x+b)**2)) def F(x=None, z=None): if x is None: return 0, matrix(0.0, (n, 1)) y = A * x + b if max(abs(y)) >= 1.0: return None f = -sum(log(1.0 - y**2)) gradf = 2.0 * A.T * div(y, 1 - y**2) if z is None: return f, gradf.T H = A.T * spdiag(2.0 * z[0] * div(1.0 + y**2, (1.0 - y**2)**2)) * A return f, gradf.T, H self.cxlb = solvers.cp(F)['x']
def setUp(self): """ Use cvxopt to get ground truth values """ from cvxopt import lapack,solvers,matrix,spdiag,log,div,normal,setseed from cvxopt.modeling import variable,op,max,sum solvers.options['show_progress'] = 0 setseed() m,n = 100,30 A = normal(m,n) b = normal(m,1) b /= (1.1*max(abs(b))) self.m,self.n,self.A,self.b = m,n,A,b # l1 approximation # minimize || A*x + b ||_1 x = variable(n) op(sum(abs(A*x+b))).solve() self.x1 = x.value # l2 approximation # minimize || A*x + b ||_2 bprime = -matrix(b) Aprime = matrix(A) lapack.gels(Aprime,bprime) self.x2 = bprime[:n] # Deadzone approximation # minimize sum(max(abs(A*x+b)-0.5, 0.0)) x = variable(n) dzop = op(sum(max(abs(A*x+b)-0.5, 0.0))) dzop.solve() self.obj_dz = sum(np.max([np.abs(A*x.value+b)-0.5,np.zeros((m,1))],axis=0)) # Log barrier # minimize -sum (log ( 1.0 - (A*x+b)**2)) def F(x=None, z=None): if x is None: return 0, matrix(0.0,(n,1)) y = A*x+b if max(abs(y)) >= 1.0: return None f = -sum(log(1.0 - y**2)) gradf = 2.0 * A.T * div(y, 1-y**2) if z is None: return f, gradf.T H = A.T * spdiag(2.0*z[0]*div(1.0+y**2,(1.0-y**2)**2))*A return f,gradf.T,H self.cxlb = solvers.cp(F)['x']
def get_2state_anom_seq(lens, comb_block_len, anom_prob=1.0, num_blocks=1): seqs = co.matrix(0.0, (1, lens)) lbls = co.matrix(0, (1, lens)) marker = 0 # generate first state sequence, gaussian noise 0=mean, 1=variance seqs[0,:] = co.normal(1, lens)*1.0 bak = co.matrix(seqs) prob = np.random.uniform() if prob<anom_prob: # add second state blocks block_len = np.int(np.floor(comb_block_len/float(num_blocks))) marker = 1 # add a single block blen = 0 for b in xrange(np.int(num_blocks)): if (b==num_blocks-1 and b>1): block_len = np.round(comb_block_len-blen) start = np.int(np.random.uniform()*float(lens-block_len+1)) #print start #print start+block_len lbls[0,start:start+block_len] = 1 seqs[0,start:start+block_len] = bak[0,start:start+block_len]+4.0 blen += block_len print('Anomamly block lengths (target/reality)= {0}/{1} '.format(comb_block_len, blen)) return (seqs, lbls, marker)
def train_dc(self, max_iter=50): """ Solve the optimization problem with a sequential convex programming/DC-programming approach: Iteratively, find the most likely configuration of the latent variables and then, optimize for the model parameter using fixed latent states. """ N = self.sobj.get_num_samples() DIMS = self.sobj.get_num_dims() # intermediate solutions # latent variables latent = [0.0]*N sol = normal(DIMS,1) psi = matrix(0.0, (DIMS,N)) # (dim x exm) old_psi = matrix(0.0, (DIMS,N)) # (dim x exm) threshold = matrix(0.0) obj = -1 iter = 0 # terminate if objective function value doesn't change much while iter<max_iter and (iter<2 or sum(sum(abs(np.array(psi-old_psi))))>=0.001): print('Starting iteration {0}.'.format(iter)) print(sum(sum(abs(np.array(psi-old_psi))))) iter += 1 old_psi = matrix(psi) # 1. linearize # for the current solution compute the # most likely latent variable configuration mean = matrix(0.0, (DIMS, 1)) for i in range(N): (foo, latent[i], psi[:,i]) = self.sobj.argmax(sol, i, add_prior=True) mean += psi[:,i] mpsi = matrix(psi) mean /= float(N) #for i in range(N): #mphi[:,i] -= mean # 2. solve the intermediate convex optimization problem A = mpsi*mpsi.trans() print A.size W = matrix(0.0, (DIMS,DIMS)) syev(A,W,jobz='V') print W print A print A*A.trans() #sol = (W[3:,0].trans() * A[:,3:].trans()).trans() #sol = (W[3:,0].trans() * A[:,3:].trans()).trans() sol = A[:,DIMS-1] print sol print(sum(sum(abs(np.array(psi-old_psi))))) self.sol = sol self.latent = latent return (sol, latent, threshold)
def get_GW_cut(self, graph): ######################################################################## # RETURNS AVERAGE GEOMANNS WILLIAMSON CUT FOR A GIVEN GRAPH ######################################################################## G = graph N = len(G.nodes()) # Allocate weights to the edges. for (i, j) in G.edges(): G[i][j]['weight'] = 1.0 maxcut = pic.Problem() # Add the symmetric matrix variable. X = maxcut.add_variable('X', (N, N), 'symmetric') # Retrieve the Laplacian of the graph. LL = 1 / 4. * nx.laplacian_matrix(G).todense() L = pic.new_param('L', LL) # Constrain X to have ones on the diagonal. maxcut.add_constraint(pic.tools.diag_vect(X) == 1) # Constrain X to be positive semidefinite. maxcut.add_constraint(X >> 0) # Set the objective. maxcut.set_objective('max', L | X) # Solve the problem. maxcut.solve(verbose=0, solver='cvxopt') # Use a fixed RNG seed so the result is reproducable. cvx.setseed(1) # Perform a Cholesky factorization. V = X.value cvxopt.lapack.potrf(V) for i in range(N): for j in range(i + 1, N): V[i, j] = 0 # Do up to 100 projections. Stop if we are within a factor 0.878 of the SDP # optimal value. count = 0 obj_sdp = maxcut.obj_value() obj = 0 while (obj < 0.878 * obj_sdp): r = cvx.normal(N, 1) x = cvx.matrix(np.sign(V * r)) o = (x.T * L * x).value if o > obj: x_cut = x obj = o count += 1 x = x_cut return obj, count
def sp_rand(m,n,a): """ Generates an m-by-n sparse 'd' matrix with round(a*m*n) nonzeros. """ if m == 0 or n == 0: return spmatrix([], [], [], (m,n)) nnz = min(max(0, int(round(a*m*n))), m*n) nz = matrix(random.sample(range(m*n), nnz), tc='i') return spmatrix(normal(nnz,1), nz%m, nz/m, (m,n))
def setUp(self): from cvxopt import matrix, normal, spdiag, misc, lapack from ubsdp import ubsdp m, n = 10, 10 A = normal(m**2, n) # Z0 random positive definite with maximum e.v. less than 1.0. Z0 = normal(m, m) Z0 = Z0 * Z0.T w = matrix(0.0, (m, 1)) a = +Z0 lapack.syev(a, w, jobz='V') wmax = max(w) if wmax > 0.9: w = (0.9 / wmax) * w Z0 = a * spdiag(w) * a.T # c = -A'(Z0) c = matrix(0.0, (n, 1)) misc.sgemv(A, Z0, c, dims={ 'l': 0, 'q': [], 's': [m] }, trans='T', alpha=-1.0) # Z1 = I - Z0 Z1 = -Z0 Z1[::m + 1] += 1.0 x0 = normal(n, 1) X0 = normal(m, m) X0 = X0 * X0.T S0 = normal(m, m) S0 = S0 * S0.T # B = A(x0) - X0 + S0 B = matrix(A * x0 - X0[:] + S0[:], (m, m)) X = ubsdp(c, A, B) (self.m, self.n, self.c, self.A, self.B, self.Xubsdp) = (m, n, c, A, B, X)
def get_n_gaussians(num=100, cluster=2, dims=2): dmeans = co.random((cluster, dims)) dvars = co.ones((cluster, dims)) data = co.matrix(0.0, (dims, num)) for c in range(cluster): for d in range(dims): data[d,:] = co.normal(1,num)*dvars[c,d] + dmeans[c,d] return data
def get_psd_matrix(p): tmp = matrix(normal((p)**2),(p,p))/2.0 tmp = tmp + tmp.T while(1): try: lapack.potrf(+tmp) break except: tmp = tmp + .1*eye(p) return tmp
def sp_rand(m,n,a): ''' Generates an mxn sparse 'd' matrix with round(a*m*n) nonzeros. Provided by cvxopt. ''' if m == 0 or n == 0: return spmatrix([], [], [], (m,n)) nnz = min(max(0, int(round(a*m*n))), m*n) nz = matrix(random.sample(range(m*n), nnz), tc='i') J = matrix([k//m for k in nz]) return spmatrix(normal(nnz,1), nz%m, J, (m,n))
def test_minres(): setseed(2) n=35 G=matrix(np.eye(n), tc='d') for jj in range(5): gg=normal(n,1) hh=gg*gg.T G+=(hh+hh.T)*0.5 G+=normal(n,1)*normal(1,n) G=(G+G.T)/2 b=normal(n,1) svx=+b gesv(G,svx) tol=1e-10 show=False maxit=None t1=t.time() # Create a MINRES class m = MINRES(G,b) m.option['show'] = show m.option['rtol'] = tol m.solve() mg=max(G-G.T) if mg>1e-14:sym='No' else: sym='Yes' alg='MINRES' print alg print "Is linear operator symmetric? (Symmetry is required) " + sym print "n: %3g iterations: %3g" % (n, m.iter) print " norms computed in ", alg print " ||x|| %9.4e ||r|| %9.4e " %( nrm2(m.x), nrm2(G*m.x -m.b))
def train(self, max_iter=50): """ Solve the LatentSVDD optimization problem with a sequential convex programming/DC-programming approach: Iteratively, find the most likely configuration of the latent variables and then, optimize for the model parameter using fixed latent states. """ N = self.sobj.get_num_samples() DIMS = self.sobj.get_num_dims() # intermediate solutions # latent variables latent = [0] * N sol = 10.0 * normal(DIMS, 1) psi = matrix(0.0, (DIMS, N)) # (dim x exm) old_psi = matrix(0.0, (DIMS, N)) # (dim x exm) threshold = 0 obj = -1 iter = 0 # terminate if objective function value doesn't change much while iter < max_iter and ( iter < 2 or sum(sum(abs(np.array(psi - old_psi)))) >= 0.001): print('Starting iteration {0}.'.format(iter)) print(sum(sum(abs(np.array(psi - old_psi))))) iter += 1 old_psi = matrix(psi) # 1. linearize # for the current solution compute the # most likely latent variable configuration for i in range(N): # min_z ||sol - Psi(x,z)||^2 = ||sol||^2 + min_z -2<sol,Psi(x,z)> + ||Psi(x,z)||^2 # Hence => ||sol||^2 - max_z 2<sol,Psi(x,z)> - ||Psi(x,z)||^2 foo, latent[i], psi[:, i] = self.sobj.argmax(sol, i, opt_type='quadratic') # 2. solve the intermediate convex optimization problem kernel = get_kernel(psi, psi) svdd = SvddDualQP(kernel, self.C) svdd.fit() threshold = svdd.get_radius() inds = svdd.svs alphas = svdd.get_support() sol = psi[:, inds] * alphas self.sol = sol self.latent = latent return sol, latent, threshold
def test_case3(self): m, n = 500, 100 setseed(100) A = normal(m,n) b = normal(m) x1 = variable(n) lp1 = op(max(abs(A*x1-b))) lp1.solve() self.assertTrue(lp1.status == 'optimal') x2 = variable(n) lp2 = op(sum(abs(A*x2-b))) lp2.solve() self.assertTrue(lp2.status == 'optimal') x3 = variable(n) lp3 = op(sum(max(0, abs(A*x3-b)-0.75, 2*abs(A*x3-b)-2.25))) lp3.solve() self.assertTrue(lp3.status == 'optimal')
def test_minres(): setseed(2) n = 35 G = matrix(np.eye(n), tc='d') for jj in range(5): gg = normal(n, 1) hh = gg * gg.T G += (hh + hh.T) * 0.5 G += normal(n, 1) * normal(1, n) G = (G + G.T) / 2 b = normal(n, 1) svx = +b gesv(G, svx) tol = 1e-10 show = False maxit = None t1 = t.time() # Create a MINRES class m = MINRES(G, b) m.option['show'] = show m.option['rtol'] = tol m.solve() mg = max(G - G.T) if mg > 1e-14: sym = 'No' else: sym = 'Yes' alg = 'MINRES' print alg print "Is linear operator symmetric? (Symmetry is required) " + sym print "n: %3g iterations: %3g" % (n, m.iter) print " norms computed in ", alg print " ||x|| %9.4e ||r|| %9.4e " % (nrm2(m.x), nrm2(G * m.x - m.b))
def test_case3(self): m, n = 500, 100 setseed(100) A = normal(m, n) b = normal(m) x1 = variable(n) lp1 = op(max(abs(A * x1 - b))) lp1.solve() self.assertTrue(lp1.status == 'optimal') x2 = variable(n) lp2 = op(sum(abs(A * x2 - b))) lp2.solve() self.assertTrue(lp2.status == 'optimal') x3 = variable(n) lp3 = op( sum(max(0, abs(A * x3 - b) - 0.75, 2 * abs(A * x3 - b) - 2.25))) lp3.solve() self.assertTrue(lp3.status == 'optimal')
def test_cvxopt_sparse(self): m = 100 n = 20 mu = cvxopt.exp( cvxopt.normal(m) ) F = cvxopt.normal(m, n) D = cvxopt.spdiag( cvxopt.uniform(m) ) x = Variable(m) exp = square(norm2(D*x)) # # TODO # # Test scipy sparse matrices. # def test_scipy_sparse(self): # m = 100 # n = 20 # mu = cvxopt.exp( cvxopt.normal(m) ) # F = cvxopt.normal(m, n) # x = Variable(m) # D = scipy.sparse.spdiags(1.5, 0, m, m ) # exp = square(norm2(D*x))
def random_projection(V, L, Zvp): # random projection algorithm # Repeat 100 times or until we are within a factor .878 of # the SDP optimal value Zvp count, obj, lobo = 0, 0, .878 * Zvp while (count < 100 or obj < lobo): r = cvx.normal(V.size[0]) x = cvx.matrix(np.sign(V * r)) o = (x.T * L * x).value[0] if o > obj: x_cut = x obj = o count += 1 return x_cut
def train_dc(self, max_iter=50): """ Solve the LatentSVDD optimization problem with a sequential convex programming/DC-programming approach: Iteratively, find the most likely configuration of the latent variables and then, optimize for the model parameter using fixed latent states. """ N = self.sobj.get_num_samples() DIMS = self.sobj.get_num_dims() # intermediate solutions # latent variables latent = [0]*N sol = 10.0*normal(DIMS,1) psi = matrix(0.0, (DIMS,N)) # (dim x exm) old_psi = matrix(0.0, (DIMS,N)) # (dim x exm) threshold = 0 obj = -1 iter = 0 # terminate if objective function value doesn't change much while iter<max_iter and (iter<2 or sum(sum(abs(np.array(psi-old_psi))))>=0.001): print('Starting iteration {0}.'.format(iter)) print(sum(sum(abs(np.array(psi-old_psi))))) iter += 1 old_psi = matrix(psi) # 1. linearize # for the current solution compute the # most likely latent variable configuration for i in range(N): # min_z ||sol - Psi(x,z)||^2 = ||sol||^2 + min_z -2<sol,Psi(x,z)> + ||Psi(x,z)||^2 # Hence => ||sol||^2 - max_z 2<sol,Psi(x,z)> - ||Psi(x,z)||^2 (foo, latent[i], psi[:,i]) = self.sobj.argmax(sol, i, opt_type='quadratic') # 2. solve the intermediate convex optimization problem kernel = Kernel.get_kernel(psi,psi) svdd = SVDD(kernel, self.C) svdd.train_dual() threshold = svdd.get_threshold() inds = svdd.get_support_dual() alphas = svdd.get_support_dual_values() sol = psi[:,inds]*alphas self.sol = sol self.latent = latent return (sol, latent, threshold)
def get_2state_anom_seq(lens, comb_block_len, anom_prob=1.0, num_blocks=1): seqs = co.matrix(0.0, (1, lens)) lbls = co.matrix(0, (1, lens), tc='i') marker = 1 # generate first state sequence, gaussian noise 0=mean, 1=variance seqs[0, :] = co.normal(1, lens) * 1.0 bak = co.matrix(seqs) prob = np.random.uniform() if prob < anom_prob: marker = 0 # add a single block blen = 0 for b in xrange(np.int(num_blocks)): if (b == num_blocks - 1 and b > 1): block_len = np.round(comb_block_len - blen) else: # add second state blocks block_len = np.int( np.floor( (comb_block_len - blen) / float(num_blocks - b))) bcnt = 0 isDone = False while isDone == False and bcnt < 100000: bcnt += 1 start = np.random.randint(low=0, high=lens - block_len + 1) if (sum(lbls[0, start:start + block_len]) == 0): #print start #print block_len #print start+block_len lbls[0, start:start + block_len] = 1 seqs[0, start:start + block_len] = (bak[0, start:start + block_len] + 0.6) isDone = True break if isDone: blen += block_len print('Anomamly block lengths (target/reality)= {0}/{1} '.format( comb_block_len, blen)) if not comb_block_len == blen: print('Warning! Anomamly block length error.') return (seqs, lbls, marker)
def setUp(self): from cvxopt import matrix, normal, spdiag, misc, lapack from ubsdp import ubsdp m, n = 10, 10 A = normal(m**2, n) # Z0 random positive definite with maximum e.v. less than 1.0. Z0 = normal(m,m) Z0 = Z0 * Z0.T w = matrix(0.0, (m,1)) a = +Z0 lapack.syev(a, w, jobz = 'V') wmax = max(w) if wmax > 0.9: w = (0.9/wmax) * w Z0 = a * spdiag(w) * a.T # c = -A'(Z0) c = matrix(0.0, (n,1)) misc.sgemv(A, Z0, c, dims = {'l': 0, 'q': [], 's': [m]}, trans = 'T', alpha = -1.0) # Z1 = I - Z0 Z1 = -Z0 Z1[::m+1] += 1.0 x0 = normal(n,1) X0 = normal(m,m) X0 = X0*X0.T S0 = normal(m,m) S0 = S0*S0.T # B = A(x0) - X0 + S0 B = matrix(A*x0 - X0[:] + S0[:], (m,m)) X = ubsdp(c, A, B) (self.m, self.n, self.c, self.A, self.B, self.Xubsdp) = (m, n, c, A, B, X)
def snl_problem(seed=None, **kwargs): """ Uniformly scatters N sensors in unit hypercube and generates partial EDM matrix ARGUMENTS: seed : random generator seed kwargs: n : number of sensors k : dimension of space neighbors : number of nearest neighbors noise : measurement distance noise RETURNS: X : n x k data matrix where X[i,:] is the position of sensor i EDM : partial Euclidean distance matrix where nearest neighbor distances are filled NN : adjacency matrix for nearest neighbor distances """ n = kwargs.get('nsensors', 100) k = kwargs.get('dim', 3) neighbors = kwargs.get('neighbors', 5) noise = kwargs.get('noise', 0.0) # generating problem data random.seed(seed) X = matrix([random.uniform(0, 1) for i in xrange(n * k)], (n, k)) EDM, NN = get_EDM(X, neighbors) I, J, V = EDM.I, EDM.J, EDM.V EDMnoise = spmatrix(normal(len(I), 1), I, J, (n, n)) EDMnoise = (EDMnoise + EDMnoise.T) / 2 EDM[:] = mul(EDM, (1.0 + noise * EDMnoise))[:] for i in xrange(n): EDM[i, i] = 0.0 return X, EDM, NN
def test_mrcompletion(self): Ac = cp.cspmatrix(self.symb) + self.A Y = cp.mrcompletion(Ac) C = Y * Y.T Ap = cp.cspmatrix(Ac.symb) Ap.add_projection(C, beta=0.0, reordered=True) diff = list((Ac.spmatrix() - Ap.spmatrix()).V) self.assertAlmostEqualLists(diff, len(diff) * [0.0]) U = normal(17, 2) cp.syr2(Ac, U[:, 0], U[:, 0], alpha=0.5, beta=0.0) cp.syr2(Ac, U[:, 1], U[:, 1], alpha=0.5, beta=1.0) Y = cp.mrcompletion(Ac) Ap.add_projection(Y * Y.T, beta=0.0, reordered=True) diff = list((Ac.spmatrix() - Ap.spmatrix()).V) self.assertAlmostEqualLists(diff, len(diff) * [0.0]) Ap.add_projection(U * U.T, beta=0.0, reordered=False) diff = list((Ac.spmatrix() - Ap.spmatrix()).V) self.assertAlmostEqualLists(diff, len(diff) * [0.0])
def sampler(A, Vr, verbose=False): # Generate 1000 samples and apply rounding A = A.toarray() A[A == 0.] = -1 Am = matrix(A) vals = [] fbest = float('-inf') xbest = None for k in range(10000): x = matrix( [-1. if xi >= 0 else 1. for xi in Vr * normal(Vr.size[1], 1)]) f = blas.dot(x, Am * x) if f > fbest: if verbose == True: print f print "with" fbest = f xbest = x vals.append(f) x1 = np.array([0 if xi >= 0.0 else 1 for xi in xbest]) return x1
def get_2state_anom_seq(lens, comb_block_len, anom_prob=1.0, num_blocks=1): seqs = co.matrix(0.0, (1, lens)) lbls = co.matrix(0, (1, lens)) marker = 0 # generate first state sequence, gaussian noise 0=mean, 1=variance seqs[0, :] = co.normal(1, lens) * 1.0 bak = co.matrix(seqs) prob = np.random.uniform() if prob < anom_prob: # add second state blocks block_len = np.int(np.floor(comb_block_len / float(num_blocks))) marker = 1 # add a single block blen = 0 for b in xrange(np.int(num_blocks)): if (b == num_blocks - 1 and b > 1): block_len = np.round(comb_block_len - blen) isDone = False while isDone == False: start = np.int(np.random.uniform() * float(lens - block_len + 1)) if (sum(lbls[0, start:start + block_len]) == 0): #print start #print start+block_len lbls[0, start:start + block_len] = 1 seqs[0, start:start + block_len] = bak[0, start:start + block_len] + 4.0 isDone = True break blen += block_len print('Anomamly block lengths (target/reality)= {0}/{1} '.format( comb_block_len, blen)) return seqs, lbls, marker
def cvx(): # Figures 6.8-10, pages 313-314 # Quadratic smoothing. from math import pi from cvxopt import blas, lapack, matrix, sin, mul, normal n = 4000 t = matrix(list(range(n)), tc='d') ex = 0.5 * mul(sin(2 * pi / n * t), sin(0.01 * t)) corr = ex + 0.05 * normal(n, 1) # A = D'*D is an n by n tridiagonal matrix with -1.0 on the # upper/lower diagonal and 1, 2, 2, ..., 2, 2, 1 on the diagonal. Ad = matrix([1.0] + (n - 2) * [2.0] + [1.0]) As = matrix(-1.0, (n - 1, 1)) nopts = 50 deltas = -10.0 + 20.0 / (nopts - 1) * matrix(list(range(nopts))) cost1, cost2 = [], [] for delta in deltas: xr = +corr lapack.ptsv(1.0 + 10 ** delta * Ad, 10 ** delta * As, xr) cost1 += [blas.nrm2(xr - corr)] cost2 += [blas.nrm2(xr[1:] - xr[:-1])] # Find solutions with ||xhat - xcorr || roughly equal to 8.0, 3.1, 1.0. time.sleep(1) mv1, k1 = min(zip([abs(c - 8.0) for c in cost1], range(nopts))) xr1 = +corr lapack.ptsv(1.0 + 10 ** deltas[k1] * Ad, 10 ** deltas[k1] * As, xr1) mv2, k2 = min(zip([abs(c - 3.1) for c in cost1], range(nopts))) xr2 = +corr lapack.ptsv(1.0 + 10 ** deltas[k2] * Ad, 10 ** deltas[k2] * As, xr2) mv3, k3 = min(zip([abs(c - 1.0) for c in cost1], range(nopts))) xr3 = +corr lapack.ptsv(1.0 + 10 ** deltas[k3] * Ad, 10 ** deltas[k3] * As, xr3)
def test_readme_examples(self): import cvxopt import numpy # Problem data. m = 30 n = 20 A = cvxopt.normal(m,n) b = cvxopt.normal(m) # Construct the problem. x = Variable(n) objective = Minimize(sum_entries(square(A*x - b))) constraints = [0 <= x, x <= 1] p = Problem(objective, constraints) # The optimal objective is returned by p.solve(). result = p.solve() # The optimal value for x is stored in x.value. print(x.value) # The optimal Lagrange multiplier for a constraint # is stored in constraint.dual_value. print(constraints[0].dual_value) #################################################### # Scalar variable. a = Variable() # Column vector variable of length 5. x = Variable(5) # Matrix variable with 4 rows and 7 columns. A = Variable(4, 7) #################################################### # Positive scalar parameter. m = Parameter(sign="positive") # Column vector parameter with unknown sign (by default). c = Parameter(5) # Matrix parameter with negative entries. G = Parameter(4, 7, sign="negative") # Assigns a constant value to G. G.value = -numpy.ones((4, 7)) # Raises an error for assigning a value with invalid sign. with self.assertRaises(Exception) as cm: G.value = numpy.ones((4,7)) self.assertEqual(str(cm.exception), "Invalid sign for Parameter value.") #################################################### a = Variable() x = Variable(5) # expr is an Expression object after each assignment. expr = 2*x expr = expr - a expr = sum_entries(expr) + norm(x, 2) #################################################### import numpy as np import cvxopt from multiprocessing import Pool # Problem data. n = 10 m = 5 A = cvxopt.normal(n,m) b = cvxopt.normal(n) gamma = Parameter(sign="positive") # Construct the problem. x = Variable(m) objective = Minimize(sum_entries(square(A*x - b)) + gamma*norm(x, 1)) p = Problem(objective) # Assign a value to gamma and find the optimal x. def get_x(gamma_value): gamma.value = gamma_value result = p.solve() return x.value gammas = np.logspace(-1, 2, num=2) # Serial computation. x_values = [get_x(value) for value in gammas] #################################################### n = 10 mu = cvxopt.normal(1, n) sigma = cvxopt.normal(n,n) sigma = sigma.T*sigma gamma = Parameter(sign="positive") gamma.value = 1 x = Variable(n) # Constants: # mu is the vector of expected returns. # sigma is the covariance matrix. # gamma is a Parameter that trades off risk and return. # Variables: # x is a vector of stock holdings as fractions of total assets. expected_return = mu*x risk = quad_form(x, sigma) objective = Maximize(expected_return - gamma*risk) p = Problem(objective, [sum_entries(x) == 1]) result = p.solve() # The optimal expected return. print(expected_return.value) # The optimal risk. print(risk.value) ########################################### N = 50 M = 40 n = 10 data = [] for i in range(N): data += [(1, cvxopt.normal(n, mean=1.0, std=2.0))] for i in range(M): data += [(-1, cvxopt.normal(n, mean=-1.0, std=2.0))] # Construct problem. gamma = Parameter(sign="positive") gamma.value = 0.1 # 'a' is a variable constrained to have at most 6 non-zero entries. a = Variable(n)#mi.SparseVar(n, nonzeros=6) b = Variable() slack = [pos(1 - label*(sample.T*a - b)) for (label, sample) in data] objective = Minimize(norm(a, 2) + gamma*sum(slack)) p = Problem(objective) # Extensions can attach new solve methods to the CVXPY Problem class. #p.solve(method="admm") p.solve() # Count misclassifications. errors = 0 for label, sample in data: if label*(sample.T*a - b).value < 0: errors += 1 print("%s misclassifications" % errors) print(a.value) print(b.value)
x0 = matrix(-lmbda[0]+1.0, (n,1)) s0 = +w s0[::n+1] += x0 # Initial feasible z is identity. z0 = matrix(0.0, (n,n)) z0[::n+1] = 1.0 dims = {'l': 0, 'q': [], 's': [n]} sol = solvers.conelp(c, Fs, w[:], dims, kktsolver = Fkkt, primalstart = {'x': x0, 's': s0[:]}, dualstart = {'z': z0[:]}) return sol['x'], matrix(sol['z'], (n,n)) n = 10 w = normal(n,n) run_go = True if len(sys.argv[1:]) > 0: if sys.argv[1] == "-sp": helpers.sp_reset("./sp.data") helpers.sp_activate() run_go = False x, z = mcsdp(w) if run_go: print "\n *** running GO test ***" helpers.run_go_test("../testmcsdp", {'x': x, 'z': z, 'data': w}) else: print "w ", helpers.strSpe(w, "%.17f") #print "x=\n", helpers.strSpe(x, "%.17f") #print "z=\n", helpers.strSpe(z, "%.17f")
# Figures 6.8-10, pages 313-314 # Quadratic smoothing. from math import pi from cvxopt import blas, lapack, matrix, sin, mul, normal try: import pylab except ImportError: pylab_installed = False else: pylab_installed = True n = 4000 t = matrix(list(range(n)), tc='d') ex = 0.5 * mul( sin(2*pi/n * t), sin(0.01 * t)) corr = ex + 0.05 * normal(n,1) if pylab_installed: pylab.figure(1, facecolor='w', figsize=(8,5)) pylab.subplot(211) pylab.plot(t, ex) pylab.ylabel('x[i]') pylab.xlabel('i') pylab.title('Original and corrupted signal (fig. 6.8)') pylab.subplot(212) pylab.plot(t, corr) pylab.ylabel('xcor[i]') pylab.xlabel('i') # A = D'*D is an n by n tridiagonal matrix with -1.0 on the # upper/lower diagonal and 1, 2, 2, ..., 2, 2, 1 on the diagonal. Ad = matrix([1.0] + (n-2)*[2.0] + [1.0]) As = matrix(-1.0, (n-1,1))
# The robust LP example of section 10.5 (Examples). from cvxopt import normal, uniform from cvxopt.modeling import variable, dot, op, sum from cvxopt.blas import nrm2 m, n = 500, 100 A = normal(m, n) b = uniform(m) c = normal(n) x = variable(n) op(dot(c, x), A * x + sum(abs(x)) <= b).solve() x2 = variable(n) y = variable(n) op(dot(c, x2), [A * x2 + sum(y) <= b, -y <= x2, x2 <= y]).solve() print("\nDifference between two solutions %e" % nrm2(x.value - x2.value))
def get_gaussian(num,dims=2,means=[0,0],vars=[1,1]): data = co.matrix(0.0,(dims,num)) for d in range(dims): data[d,:] = co.normal(1,num)*vars[d] + means[d] return data
def test_basic_no_gsl(self): import sys sys.modules['gsl'] = None import cvxopt cvxopt.normal(4, 8) cvxopt.uniform(4, 8)
# The 1-norm support vector classifier of section 10.5 (Examples). from cvxopt import normal, setseed from cvxopt.modeling import variable, op, max, sum from cvxopt.blas import nrm2 m, n = 500, 100 A = normal(m,n) x = variable(A.size[1],'x') u = variable(A.size[0],'u') op(sum(abs(x)) + sum(u), [A*x >= 1-u, u >= 0]).solve() x2 = variable(A.size[1],'x') op(sum(abs(x2)) + sum(max(0, 1 - A*x2))).solve() print("\nDifference between two solutions: %e" %nrm2(x.value - x2.value))
# Original comments below # Section 6.1.2 # Boyd & Vandenberghe "Convex Optimization" # Original by Lieven Vandenberghe # Adapted for CVX Argyris Zymnis - 10/2005 # # Comparison of the ell1, ell2, deadzone-linear and log-barrier # penalty functions for the approximation problem: # minimize phi(A*x-b), # # where phi(x) is the penalty function # Generate input data m, n = 100, 30 A = cvxopt.normal(m,n) #np.random.randn(m,n) b = cvxopt.normal(m,1) #np.random.randn(m,1) # l-1 approximation x1 = Variable(n) objective1 = Minimize( norm(A*x1-b, 1) ) p1 = Problem(objective1, []) #p1 = Problem(Minimize( norm(A*x1-b, 1), [])) # l-2 approximation x2 = Variable(n) objective2 = Minimize( norm(A*x2-b, 2) ) p2 = Problem(objective2, []) # deadzone approximation # minimize sum(deadzone(Ax+b,0.5))
x0 = matrix([uls[:n], 1.1 * abs(rls)]) s0 = +h Fi(x0, s0, alpha=-1, beta=1) # z0 = [ (1+w)/2; (1-w)/2 ] where w = (.9/||rls||_inf) * rls # if rls is nonzero and w = 0 otherwise. if max(abs(rls)) > 1e-10: w = .9 / max(abs(rls)) * rls else: w = matrix(0.0, (m, 1)) z0 = matrix([.5 * (1 + w), .5 * (1 - w)]) dims = {'l': 2 * m, 'q': [], 's': []} sol = solvers.conelp(c, Fi, h, dims, kktsolver=Fkkt, primalstart={ 'x': x0, 's': s0 }, dualstart={'z': z0}) return sol['x'][:n], sol['z'][m:] - sol['z'][:m] setseed() m, n = 1000, 100 P, q = normal(m, n), normal(m, 1) x, y = l1(P, q)
# attention: this is the shape parameter of a Gaussian # which is 1/sigma^2 k_param = 2.4 N_pos = 10 N_neg = 10 N_unl = 10 # generate training labels Dy = np.zeros(N_pos+N_neg+N_unl, dtype=np.int) Dy[:N_pos] = 1 Dy[N_pos+N_unl:] = -1 # generate training data co.setseed(11) Dtrainp = co.normal(2,N_pos)*0.6 Dtrainu = co.normal(2,N_unl)*0.6 Dtrainn = co.normal(2,N_neg)*0.6 Dtrain21 = Dtrainn-1 Dtrain21[0,:] = Dtrainn[0,:]+1 Dtrain22 = -Dtrain21 # training data Dtrain = co.matrix([[Dtrainp], [Dtrainu], [Dtrainn+0.8]]) Dtrain = np.array(Dtrain) # build the training kernel kernel = get_kernel(Dtrain, Dtrain, type=k_type, param=k_param) # use SSAD
def test_numpy_scalars(self): n = 6 eps = 1e-6 cvxopt.setseed(10) P0 = cvxopt.normal(n, n) eye = cvxopt.spmatrix(1.0, range(n), range(n)) P0 = P0.T * P0 + eps * eye print(P0) P1 = cvxopt.normal(n, n) P1 = P1.T*P1 P2 = cvxopt.normal(n, n) P2 = P2.T*P2 P3 = cvxopt.normal(n, n) P3 = P3.T*P3 q0 = cvxopt.normal(n, 1) q1 = cvxopt.normal(n, 1) q2 = cvxopt.normal(n, 1) q3 = cvxopt.normal(n, 1) r0 = cvxopt.normal(1, 1) r1 = cvxopt.normal(1, 1) r2 = cvxopt.normal(1, 1) r3 = cvxopt.normal(1, 1) slack = Variable() # Form the problem x = Variable(n) objective = Minimize( 0.5*quad_form(x,P0) + q0.T*x + r0 + slack) constraints = [0.5*quad_form(x,P1) + q1.T*x + r1 <= slack, 0.5*quad_form(x,P2) + q2.T*x + r2 <= slack, 0.5*quad_form(x,P3) + q3.T*x + r3 <= slack, ] # We now find the primal result and compare it to the dual result # to check if strong duality holds i.e. the duality gap is effectively zero p = Problem(objective, constraints) primal_result = p.solve() # Note that since our data is random, we may need to run this program multiple times to get a feasible primal # When feasible, we can print out the following values print(x.value) # solution lam1 = constraints[0].dual_value lam2 = constraints[1].dual_value lam3 = constraints[2].dual_value print(type(lam1)) P_lam = P0 + lam1*P1 + lam2*P2 + lam3*P3 q_lam = q0 + lam1*q1 + lam2*q2 + lam3*q3 r_lam = r0 + lam1*r1 + lam2*r2 + lam3*r3 dual_result = -0.5*q_lam.T.dot(P_lam).dot(q_lam) + r_lam print(dual_result.shape) self.assertEquals(intf.size(dual_result), (1,1))
from cvxopt import matrix, spmatrix, normal, setseed, blas, lapack, solvers import nucnrm # Solves a randomly generated nuclear norm minimization problem # # minimize || A(x) + B ||_* # # with n variables, and matrices A(x), B of size p x q. setseed(0) p, q, n = 100, 100, 100 A = normal(p*q, n) B = normal(p, q) # options['feastol'] = 1e-6 # options['refinement'] = 3 sol = nucnrm.nrmapp(A, B) x = sol['x'] Z = sol['Z'] s = matrix(0.0, (p,1)) X = matrix(A *x, (p, q)) + B lapack.gesvd(+X, s) nrmX = sum(s) lapack.gesvd(+Z, s) nrmZ = max(s) res = matrix(0.0, (n, 1))
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. """ from cvxpy import * import numpy as np import cvxopt from multiprocessing import Pool # Problem data. n = 10 m = 5 A = cvxopt.normal(n,m) b = cvxopt.normal(n) gamma = Parameter(nonneg=True) # Construct the problem. x = Variable(m) objective = Minimize(sum_squares(A*x - b) + gamma*norm(x, 1)) p = Problem(objective) # Assign a value to gamma and find the optimal x. def get_x(gamma_value): gamma.value = gamma_value result = p.solve() return x.value gammas = np.logspace(-1, 2, num=100)
from cvxpy import * import numpy as np import cvxopt from multiprocessing import Pool # Problem data. n = 10 m = 5 A = cvxopt.normal(n, m) b = cvxopt.normal(n) gamma = Parameter(sign="positive") # Construct the problem. x = Variable(m) objective = Minimize(sum_squares(A * x - b) + gamma * norm(x, 1)) p = Problem(objective) # Assign a value to gamma and find the optimal x. def get_x(gamma_value): gamma.value = gamma_value result = p.solve() return x.value gammas = np.logspace(-1, 2, num=100) # Serial computation. x_values = [get_x(value) for value in gammas] # Parallel computation. pool = Pool(processes=4) par_x = pool.map(get_x, gammas)
def F(x=None, z=None): if x is None: return 0, matrix(1.0, (n, 1)) if min(x) <= 0.0: return None f = -sum(log(x)) Df = -(x**-1).T if z is None: return matrix(f), Df H = spdiag(z[0] * x**-2) return f, Df, H return solvers.cp(F, A=A, b=b)['x'] # Randomly generate a feasible problem m, n = 50, 500 y = normal(m, 1) # Random A with A'*y > 0. s = uniform(n, 1) A = normal(m, n) r = s - A.T * y # A = A - (1/y'*y) * y*r' blas.ger(y, r, A, alpha=1.0 / blas.dot(y, y)) # Random feasible x > 0. x = uniform(n, 1) b = A * x x = acent(A, b)
# # data = ax1.scatter([1],[2]) # # ax1.scatter([1],[2]) # xdata = [1] # ydata = [1] # ax1.plot(xdata, ydata, 'o') # # data.set_ydata(arange(2)) # cid = fig1.canvas.mpl_connect('button_press_event', onclick) # plt.show() # # Problem data. m = 4 n = 3 A = cvxopt.normal(m,n) b = cvxopt.normal(m) # # Construct the problem. x = Variable(n) objective = Minimize(sum(square(A*x - b))) constraints = [0 <= x, x <= 0.5] # constraints = [A*x + b <= 0] p = Problem(objective, constraints) # The optimal objective is returned by p.solve(). result = p.solve() # The optimal value for x is stored in x.value. print x.value # # The optimal Lagrange multiplier for a constraint # # is stored in constraint.dual_value.
# Ported from cvx matlab to cvxpy by Misrab Faizullah-Khan # Original comments below # Boyd & Vandenberghe, "Convex Optimization" # Joelle Skaf - 08/23/05 # # Solved a QCQP with 3 inequalities: # minimize 1/2 x'*P0*x + q0'*r + r0 # s.t. 1/2 x'*Pi*x + qi'*r + ri <= 0 for i=1,2,3 # and verifies that strong duality holds. # Input data n = 6 eps = sys.float_info.epsilon P0 = cvxopt.normal(n, n) eye = cvxopt.spmatrix(1.0, range(n), range(n)) P0 = P0.T * P0 + eps * eye print P0 P1 = cvxopt.normal(n, n) P1 = P1.T * P1 P2 = cvxopt.normal(n, n) P2 = P2.T * P2 P3 = cvxopt.normal(n, n) P3 = P3.T * P3 q0 = cvxopt.normal(n, 1) q1 = cvxopt.normal(n, 1) q2 = cvxopt.normal(n, 1)
def test_basic_no_gsl(self): import sys sys.modules['gsl'] = None import cvxopt cvxopt.normal(4,8) cvxopt.uniform(4,8)