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_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_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')
m, n = A.size def F(x=None, z=None): if x is None: return 0, matrix(0.0, (n, 1)) y = A * x - b w = sqrt(rho + y**2) f = sum(w) Df = div(y, w).T * A if z is None: return f, Df H = A.T * spdiag(z[0] * rho * (w**-3)) * A return f, Df, H return solvers.cp(F)['x'] setseed() m, n = 500, 100 A = normal(m, n) b = normal(m, 1) xh = robls(A, b, 0.1) try: import pylab except ImportError: pass else: # Least-squares solution. pylab.subplot(211) xls = +b lapack.gels(+A, xls)
# The norm and penalty approximation problems of section 10.5 (Examples). from kvxopt import normal, setseed from kvxopt.modeling import variable, op, max, sum setseed(0) m, n = 500, 100 A = normal(m,n) b = normal(m) x1 = variable(n) prob1=op(max(abs(A*x1+b))) prob1.solve() x2 = variable(n) prob2=op(sum(abs(A*x2+b))) prob2.solve() x3 = variable(n) prob3=op(sum(max(0, abs(A*x3+b)-0.75, 2*abs(A*x3+b)-2.25))) prob3.solve() try: import pylab except ImportError: pass else: pylab.subplot(311) pylab.hist(list(A*x1.value + b), m//5) pylab.subplot(312) pylab.hist(list(A*x2.value + b), m//5) pylab.subplot(313) pylab.hist(list(A*x3.value + b), m//5)