Exemplo n.º 1
0
 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)
Exemplo n.º 2
0
    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
        ])
Exemplo n.º 3
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')
Exemplo n.º 4
0
    def set_up_matrix(self, build_B=False):

        from kvxopt import normal

        # Types of tranpose systems:
        # 'N', solves A*X = B.
        # 'T', solves A^T*X = B.
        # 'C', solves A^H*X = B.
        if self._complex:
            self._trans = ['N', 'T', 'C']
        else:
            self._trans = ['N', 'T']

        self._A = self.read_mtx()
        if self._complex:
            self._A = +self._A + self._A * 1j

        if build_B:
            self.b = normal(self._A.size[0], self._columns_B)
            if self._complex:
                self.b = +self.b * 1j
Exemplo n.º 5
0
    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)
    rls = A * xls[:n] - b
    pylab.hist(list(rls), m // 5)
Exemplo n.º 6
0
 def test_basic_no_gsl(self):
     import sys
     sys.modules['gsl'] = None
     import kvxopt
     kvxopt.normal(4, 8)
     kvxopt.uniform(4, 8)
Exemplo n.º 7
0
# The robust LP example of section 10.5 (Examples).

from kvxopt import normal, uniform
from kvxopt.modeling import variable, dot, op, sum
from kvxopt.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))
Exemplo n.º 8
0
        step = 1.0
        while 1-step*max(y) < 0: step *= BETA 
        while True:
            if -sum(log(1-step*y)) < ALPHA*step*lam: break
            step *= BETA
        x += step*v


# Generate an analytic centering problem  
#
#    -b1 <=  Ar*x <= b2 
#
# with random mxn Ar and random b1, b2.

m, n  = 500, 500
Ar = normal(m,n);
A = matrix([Ar, -Ar])
b = uniform(2*m,1)

x, ntdecrs = acent(A, b)  
try: 
    import pylab
except ImportError: 
    pass
else:
    pylab.semilogy(range(len(ntdecrs)), ntdecrs, 'o', 
             range(len(ntdecrs)), ntdecrs, '-')
    pylab.xlabel('Iteration number')
    pylab.ylabel('Newton decrement')
    pylab.show()
Exemplo n.º 9
0
    S = matrix(0.0, (m, m))
    v = matrix(0.0, (m, 1))

    def Fkkt(x, z, W):
        ds = (2.0 * div(1 + x**2, (1 - x**2)**2))**-0.5
        Asc = A * spdiag(ds)
        blas.syrk(Asc, S)
        S[::m + 1] += 1.0
        lapack.potrf(S)
        a = z[0]

        def g(x, y, z):
            x[:] = mul(x, ds) / a
            blas.gemv(Asc, x, v)
            lapack.potrs(S, v)
            blas.gemv(Asc, v, x, alpha=-1.0, beta=1.0, trans='T')
            x[:] = mul(x, ds)

        return g

    return solvers.cp(F, kktsolver=Fkkt)['x']


m, n = 200, 2000
setseed()
A = normal(m, n)
x = uniform(n, 1)
b = A * x
x = l2ac(A, b)
Exemplo n.º 10
0
    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)
Exemplo n.º 11
0
            x[:n] -= mul( div(d1**2 - d2**2, d1**2 + d2**2), x[n:]) 
            lapack.potrs(S, x)
            
            # Solve for x[n:]:
            #
            #    (d1**-2 + d2**-2) * x[n:] = x[n:] + (d1**-2 - d2**-2)*x[:n]
             
            x[n:] += mul( d1**-2 - d2**-2, x[:n])
            x[n:] = div( x[n:], d1**-2 + d2**-2)

            # z := z + W^-T * G*x 
            z[:n] += div( x[:n] - x[n:2*n], d1) 
            z[n:2*n] += div( -x[:n] - x[n:2*n], d2) 
            z[2*n:] += As*x[:n]

        return f

    dims = {'l': 2*n, 'q': [m+1], 's': []}
    sol = solvers.conelp(c, G, h, dims, kktsolver = Fkkt)
    if sol['status'] == 'optimal':
        return sol['x'][:n],  sol['z'][-m:]
    else:
        return None, None

setseed()
m, n = 100, 100
A, b = normal(m,n), normal(m,1)

x, z = qcl1(A, b)
if x is None: print("infeasible")
Exemplo n.º 12
0
    c = matrix(1.0, (n, 1))

    # Initial feasible x:  x = 1.0 - min(lambda(w)).
    lmbda = matrix(0.0, (n, 1))
    lapack.syevx(+w, lmbda, range='I', il=1, iu=1)
    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 = 100
w = normal(n, n)
x, z = mcsdp(w)