Пример #1
0
    def test_dsdp(self):
        # Setup problem
        c = matrix([1., -1., 1.])
        G = [
            matrix([[-7., -11., -11., 3.], [7., -18., -18., 8.],
                    [-2., -8., -8., 1.]])
        ]
        G += [
            matrix([[-21., -11., 0., -11., 10., 8., 0., 8., 5.],
                    [0., 10., 16., 10., -10., -10., 16., -10., 3.],
                    [-5., 2., -17., 2., -6., 8., -17., 8., 6.]])
        ]
        h = [matrix([[33., -9.], [-9., 26.]])]
        h += [matrix([[14., 9., 40.], [9., 91., 10.], [40., 10., 15.]])]

        # Solve with default solver
        sol = solvers.sdp(c, Gs=G, hs=h)

        # Solve with DSDP
        sol_dsdp = solvers.sdp(c, Gs=G, hs=h, solver='dsdp')

        # Compare solutions
        self.assertMatrixAlmostEqual(sol['x'], sol_dsdp['x'])
        self.assertMatrixAlmostEqual(sol['zs'][0], sol_dsdp['zs'][0])
        self.assertMatrixAlmostEqual(sol['zs'][1], sol_dsdp['zs'][1])
Пример #2
0
	def __solve(self,c,F0,F,real=False,altsolver=None):
		c = cvxopt.matrix(c,tc='d')
		h = self.__F0toh(F0,real)
		G = self.__FtoG(F,real)
		if not altsolver:
			return solvers.sdp(c,Gs=G,hs=h)
		else:
			return solvers.sdp(c,Gs=G,hs=h,solver='dsdp')
 def test_sdp(self):
     from cvxopt import solvers, dsdp
     c, Gs, hs = self._prob_data
     sol_ref1 = solvers.sdp(c, None, None, Gs, hs)
     self.assertTrue(sol_ref1['status'] == 'optimal')
     sol_ref2 = solvers.sdp(c, Gs=Gs, hs=hs)
     self.assertTrue(sol_ref2['status'] == 'optimal')
     sol1 = solvers.sdp(c, None, None, Gs, hs, solver='dsdp')
     self.assertTrue(sol1['status'] == 'optimal')
     sol2 = solvers.sdp(c, Gs=Gs, hs=hs, solver='dsdp')
     self.assertTrue(sol2['status'] == 'optimal')
     sol3 = dsdp.sdp(c, None, None, Gs, hs)
     self.assertTrue(sol3[0] == 'DSDP_PDFEASIBLE')
     sol4 = dsdp.sdp(c, Gs=Gs, hs=hs)
     self.assertTrue(sol4[0] == 'DSDP_PDFEASIBLE')
Пример #4
0
 def test_sdp(self):
     from cvxopt import solvers, dsdp
     c,Gs,hs = self._prob_data
     sol_ref1 = solvers.sdp(c,None,None,Gs,hs)
     self.assertTrue(sol_ref1['status']=='optimal')
     sol_ref2 = solvers.sdp(c,Gs=Gs,hs=hs)
     self.assertTrue(sol_ref2['status']=='optimal')
     sol1 = solvers.sdp(c,None,None,Gs,hs,solver='dsdp')
     self.assertTrue(sol1['status']=='optimal')
     sol2 = solvers.sdp(c,Gs=Gs,hs=hs,solver='dsdp')
     self.assertTrue(sol2['status']=='optimal')
     sol3 = dsdp.sdp(c,None,None,Gs,hs)
     self.assertTrue(sol3[0] == 'DSDP_PDFEASIBLE')
     sol4 = dsdp.sdp(c,Gs=Gs,hs=hs)
     self.assertTrue(sol4[0] == 'DSDP_PDFEASIBLE')
Пример #5
0
def solve_sdp(c, Gl=None, hl=None, Gsl=[], hsl=[], A=None, b=None):
    """
    Solve Semi-Definite Prgramming
    :param c:
    :param Gl:
    :param hl:
    :param Gsl:
    :param hsl:
    :param A:
    :param b:
    :return:
    """
    if Gl is None:
        Gl = np.zeros((1, len(c)))
        hl = np.zeros((1, 1))
    if not Gsl:
        Gsl = [np.zeros((1, len(c)))]
        hsl = [np.zeros((1, 1))]
    args = [
        matrix(c),
        matrix(Gl),
        matrix(hl), [matrix(Gs) for Gs in Gsl], [matrix(hs) for hs in hsl]
    ]
    if A is not None:
        args.extend([matrix(A), matrix(b)])
    sol = solvers.sdp(*args)
    if 'optimal' not in sol['status']:
        print("CVXOPT FAIL:", sol['status'])
        return None
    return np.array(sol['x']).reshape((len(c), ))
def SDP_query_distribution(A, lambda_, X_pool, k):
    """Solving SDP problem in FIR-based active learning
    to obtain the query distribution
    """

    n = len(A)
    tau = A[0].shape[0]
    """Preparing the variables"""

    # matrix inequality constraints
    G, h = inequality_cvx_matrix(A)
    # equality constraint (for having probabilities)
    if lambda_ > 0:
        d = X_pool.shape[0]
        pool_norms = np.sum(X_pool**2, axis=0)
        # vector c (in the objective)
        cvec = matrix(np.concatenate((-lambda_ * pool_norms, np.ones(tau))))

        # A matrix (LHS of equality)
        left_A = np.concatenate((X_pool, np.ones((1, n))), axis=0)

        A_eq = matrix(np.concatenate((left_A, np.zeros((d + 1, tau))), axis=1))
        # b vector (RHS of equality)
        b_eq = np.zeros(d + 1)
        b_eq[-1] = 1.
        b_eq = matrix(b_eq)

    else:
        cvec = matrix(np.concatenate((np.zeros(n), np.ones(tau))))
        A_eq = matrix(np.concatenate((np.ones(n), np.zeros(tau)))).trans()
        b_eq = matrix(1.)
    """Solving SDP"""
    soln = solvers.sdp(cvec, Gs=G, hs=h, A=A_eq, b=b_eq)

    return soln
Пример #7
0
def test_mmsolvers():
    # simple test to make sure things run
    
    print 'testing simple unimixture with a skipped observation, just to test that things run'
    x = sp.symbols('x')
    M = MomentMatrix(3, [x], morder='grevlex')
    constrs = [x-1.5, x**2-2.5, x**4-8.5]
    #constrs = [x-1.5, x**2-2.5, x**3-4.5, x**4-8.5]
    cin = get_cvxopt_inputs(M, constrs, slack = 1e-5)

    #import MomentMatrixSolver
    #print 'joint_alternating_solver...'
    #y,L = MomentMatrixSolver.sgd_solver(M, constrs, 2, maxiter=101, eta = 0.001)
    #y,X = MomentMatrixSolver.convex_projection_solver(M, constrs, 2, maxiter=2000) 
    #print y
    #print X

    gs = [2-x, 2+x]
    locmatrices = [LocalizingMatrix(M, g) for g in gs]
    Ghs = [get_cvxopt_Gh(lm) for lm in locmatrices]

    Gs=cin['G'] + [Gh['G'] for Gh in Ghs]
    hs=cin['h'] + [Gh['h'] for Gh in Ghs]
    
    sol = cvxsolvers.sdp(cin['c'], Gs=Gs, \
                  hs=hs, A=cin['A'], b=cin['b'])

    print sol['x']
    print abs(sol['x'][3]-4.5)
    assert(abs(sol['x'][3]-4.5) <= 1e-3)

    import extractors
    print extractors.extract_solutions_lasserre(M, sol['x'], Kmax = 2)
    print 'true values are 1 and 2'
Пример #8
0
def test_unimixture():
    print 'testing simple unimixture with a skipped observation'
    x = sp.symbols('x')

    constrs = [x + x**2 - 2, x + 2 * x**4 - 3,
               2 * x**2 + 3 * x**4 - 5]  # always solvable
    constrs = [
        x + x**2 - 1, x**2 + 2 * x**3 - 1, 2 * x**3 + 3 * x**4 - 3,
        2 * x + x**3 - 0
    ]  # always solvable
    constrs = [x - 1.5, x**2 - 2.5,
               x**5 - 16.5]  # solvable with deg=3, but not deg=2
    constrs = [x - 1.5, x**2 - 2.5,
               x**4 - 8.5]  # solvable with deg=3, but not deg=2

    M = mp.MomentMatrix(3, [x], morder='grevlex')

    cin = mp.solvers.get_cvxopt_inputs(M, constrs)
    sol = solvers.sdp(matrix(sc.rand(*cin['c'].size)), Gs=cin['G'], \
                  hs=cin['h'], A=cin['A'], b=cin['b'])

    M.pretty_print(sol)

    print mp.extractors.extract_solutions_lasserre(M, sol['x'], Kmax=2)
    return M, sol
Пример #9
0
def CVXOPT_SDP_Solver(p, solverName):
    if solverName == 'native_CVXOPT_SDP_Solver': solverName = None
    cvxopt_solvers.options['maxiters'] = p.maxIter
    cvxopt_solvers.options['feastol'] = p.contol
    cvxopt_solvers.options['abstol'] = p.ftol
    if p.iprint <= 0:
        cvxopt_solvers.options['show_progress'] = False
        cvxopt_solvers.options['LPX_K_MSGLEV'] = 0
        #cvxopt_solvers.options['MSK_IPAR_LOG'] = 0
    xBounds2Matrix(p)

    #FIXME: if problem is search for MAXIMUM, not MINIMUM!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

    f = copy(p.f).reshape(-1, 1)

    # CVXOPT have some problems with x0 so currently I decided to avoid using the one

    sol = cvxopt_solvers.sdp(Matrix(p.f), Matrix(p.A), Matrix(p.b), \
                             converter_to_CVXOPT_SDP_Matrices_from_OO_SDP_Class(p.S, p.n), \
                             DictToList(p.d), Matrix(p.Aeq), Matrix(p.beq), solverName)
    p.msg = sol['status']
    if p.msg == 'optimal':
        p.istop = SOLVED_WITH_UNIMPLEMENTED_OR_UNKNOWN_REASON
    else:
        p.istop = -100
    if sol['x'] is not None:
        p.xf = asarray(sol['x']).flatten()
        p.ff = sum(p.dotmult(p.f, p.xf))
        #p.duals = concatenate((asarray(sol['y']).flatten(), asarray(sol['z']).flatten()))
    else:
        p.ff = nan
        p.xf = nan * ones(p.n)
Пример #10
0
def test_cvx_sdp():
    """
    Test that the CVX SDP optimization works.
    """
    c = matrix([1., -1., 1.])
    G = [
        matrix([[-7., -11., -11., 3.], [7., -18., -18., 8.],
                [-2., -8., -8., 1.]])
    ]
    G += [
        matrix([[-21., -11., 0., -11., 10., 8., 0., 8., 5.],
                [0., 10., 16., 10., -10., -10., 16., -10., 3.],
                [-5., 2., -17., 2., -6., 8., -17., 8., 6.]])
    ]
    h = [matrix([[33., -9.], [-9., 26.]])]
    h += [matrix([[14., 9., 40.], [9., 91., 10.], [40., 10., 15.]])]
    sol = solvers.sdp(c, Gs=G, hs=h)
    x = np.matrix(sol['x'])
    x_ = np.matrix([
        [-3.68e-01],
        [1.90e+00],
        [-8.88e-01],
    ])
    print x, x_
    assert np.allclose(x, x_, atol=1e-2)
def inv_solver(xs, ps, xmax):
    """Optimization w.r.t. (Q,r)
    
    Parameters
    ----------
    xs: list of (optimal) demand vectors
    ps: list of price vectors
    xmax: maximum demand vector (for each product)
    """
    n, N = len(xs[0]), len(xs)
    # constraint Q <= 0
    G, h = conic_constraint(n)
    # constraint Q*xmax + r >= 0
    A = -linear_constraint(xmax)
    b = matrix(0., (n,1))
    # constraints Q*x^j + r <= p^j
    for j in range(N):
        A = matrix([A, linear_constraint(xs[j])])
        b = matrix([b, matrix(ps[j])])
    # constraint r >= 0
    tmp = matrix([[matrix(0., (n, (n*(n+1))/2))], [-spdiag([1.]*n)]])
    A = matrix([A, tmp])
    b = matrix([b, matrix(0., (n,1))])
    # linear objective
    c = linear_objective(xs)
    x = solvers.sdp(c, A, b, G, h)['x']
    Q, r = matrix(0., (n,n)), matrix(0., (n,1))
    for i in range(n):
        Q[i,i] = x[(n+1)*i-(i*(i+1))/2]
        r[i] = x[(n*(n+1))/2+i]
        for j in range(i+1,n):
            Q[i,j] = x[n*i+j-(i*(i+1))/2]; Q[j,i] = Q[i,j]
    return Q, r
Пример #12
0
def CVXOPT_SDP_Solver(p, solverName):
    if solverName == 'native_CVXOPT_SDP_Solver': solverName = None
    cvxopt_solvers.options['maxiters'] = p.maxIter
    cvxopt_solvers.options['feastol'] = p.contol    
    cvxopt_solvers.options['abstol'] = p.ftol
    if p.iprint <= 0:
        cvxopt_solvers.options['show_progress'] = False
        cvxopt_solvers.options['LPX_K_MSGLEV'] = 0
        #cvxopt_solvers.options['MSK_IPAR_LOG'] = 0
    xBounds2Matrix(p)

    #FIXME: if problem is search for MAXIMUM, not MINIMUM!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

    f = copy(p.f).reshape(-1,1)

    # CVXOPT have some problems with x0 so currently I decided to avoid using the one

    sol = cvxopt_solvers.sdp(Matrix(p.f), Matrix(p.A), Matrix(p.b), \
                             converter_to_CVXOPT_SDP_Matrices_from_OO_SDP_Class(p.S, p.n), \
                             DictToList(p.d), Matrix(p.Aeq), Matrix(p.beq), solverName)
    p.msg = sol['status']
    if p.msg == 'optimal' :  p.istop = SOLVED_WITH_UNIMPLEMENTED_OR_UNKNOWN_REASON
    else: p.istop = -100
    if sol['x'] is not None:
        p.xf = asarray(sol['x']).flatten()
        p.ff = sum(p.dotmult(p.f, p.xf))
        #p.duals = concatenate((asarray(sol['y']).flatten(), asarray(sol['z']).flatten()))
    else:
        p.ff = nan
        p.xf = nan*ones(p.n)
Пример #13
0
def solve_sdp_si_cvx(G,
                     data,
                     p0,
                     p1,
                     a_b_ratio,
                     rho=0.1,
                     max_iter=100,
                     tol=1e-5):
    B = construct_B(G, 1.0)
    B_tilde = construct_B_tilde(B, data, p0, p1, a_b_ratio)
    n = B.shape[0]
    h = [matrix(-B_tilde)]
    c = matrix(np.hstack((np.ones(n + 1), np.zeros(n + 1))))
    g_matrix = matrix(0.0, ((n + 1)**2, 2 * n + 2))
    for i in range(n + 1):
        g_matrix[(n + 1) * i + i, i] = -1
    for _i in range(n + 1, 2 * n + 2):
        i = _i - n - 1
        for j in range(1, n + 1):
            g_matrix[(n + 1) * i + j, _i] = -1
            g_matrix[(n + 1) * j + i, _i] = -1
    solvers.options['abstol'] = tol
    solvers.options['maxiters'] = max_iter
    sol = solvers.sdp(c, Gs=[g_matrix], hs=h)
    X = np.array(sol['zs'][0])
    labels = X[0, 1:] > 0
    return labels.astype(np.int)
Пример #14
0
def solve_ith_GMP(MM, objective, gs, hs, slack=0):
    """
    Generalized moment problem solver
    @param - objective: a sympy expression that is the objective
    @param - gs: list of contraints defining the semialgebraic set K,
    each g corresponds to localizing matrices. 
    @param - hs: constraints on the moments,
     not needed for polynomial optimization.
    @param - slack: add equalities as pairs of inequalities with slack separation.
    """
    cin = get_cvxopt_inputs(MM, hs, slack=slack)
    Bf = MM.get_Bflat()
    objcoeff, __ = MM.get_Ab([objective], cvxoptmode=False)

    locmatrices = [LocalizingMatrix(MM, g) for g in gs]
    Ghs = [get_cvxopt_Gh(lm) for lm in locmatrices]

    # list addition
    Gs = cin['G'] + [Gh['G'] for Gh in Ghs]
    hs = cin['h'] + [Gh['h'] for Gh in Ghs]
    # print Ghs
    solsdp = cvxsolvers.sdp(matrix(objcoeff[0, :]),
                            Gs=Gs,
                            hs=hs,
                            A=cin['A'],
                            b=cin['b'])
    #ipdb.set_trace()
    return solsdp
Пример #15
0
def solve_Q(x_dim, A, B, D):
  # x = [s vec(Z) vec(Q)]
  MAX_ITERS=30
  c_dim = 1 + 2 * x_dim*(x_dim+1)/2
  c = zeros(c_dim)
  c[0] = x_dim
  prev = 1
  for i in range(x_dim):
    vec_pos = prev + i * (i+1)/2 + i
    c[vec_pos] = 1
  cm = matrix(c)

  G = construct_coeff_matrix(x_dim, B)
  G = -G # set negative since s = h - Gx in cvxopt's sdp solver
  #print "G-shape"
  #print shape(G)
  Gs = [matrix(G)]

  h,_ = construct_const_matrix(x_dim, A, B, D)
  #print "h-shape"
  #print shape(h)
  hs = [matrix(h)]

  solvers.options['maxiters'] = MAX_ITERS
  sol = solvers.sdp(cm, Gs = Gs, hs=hs)
  #print sol['x']
  return sol, c, G, h
Пример #16
0
def sdp(Sigma):
    p = Sigma.shape[0]
    identity_p = np.identity(p)
    zero_one = np.repeat(0., p**3)
    zero_one2 = zero_one.copy()
    indexes = np.arange(p) * (
        1 + p + p**2)  # np.arange(p)*p+ np.arange(p)*(p**2) + np.arange(p)
    zero_one[indexes] = 1.
    block_identity = zero_one.reshape(p * p, p)
    zero_one2[indexes] = -1.
    mblock_identity = zero_one2.reshape(p * p, p)

    c = matrix(np.repeat(-1., p))
    G = [matrix(block_identity)] + [matrix(mblock_identity)
                                    ] + [matrix(block_identity)]
    h = [matrix(2. * Sigma)] + [matrix(np.zeros(
        (p, p)))] + [matrix(identity_p)]
    solvers.options['show_progress'] = False
    sol = solvers.sdp(c, Gs=G, hs=h)['x']
    sol = np.array(sol).flatten()
    sol[sol > 1.] = 1.
    sol[sol < 0.] = 0.
    # print(os.getpid(), "\n")

    return (sol)
Пример #17
0
def test_mmsolvers():
    # simple test to make sure things run

    print 'testing simple unimixture with a skipped observation, just to test that things run'
    x = sp.symbols('x')
    M = MomentMatrix(3, [x], morder='grevlex')
    constrs = [x - 1.5, x**2 - 2.5, x**4 - 8.5]
    #constrs = [x-1.5, x**2-2.5, x**3-4.5, x**4-8.5]
    cin = get_cvxopt_inputs(M, constrs, slack=1e-5)

    #import MomentMatrixSolver
    #print 'joint_alternating_solver...'
    #y,L = MomentMatrixSolver.sgd_solver(M, constrs, 2, maxiter=101, eta = 0.001)
    #y,X = MomentMatrixSolver.convex_projection_solver(M, constrs, 2, maxiter=2000)
    #print y
    #print X

    gs = [2 - x, 2 + x]
    locmatrices = [LocalizingMatrix(M, g) for g in gs]
    Ghs = [get_cvxopt_Gh(lm) for lm in locmatrices]

    Gs = cin['G'] + [Gh['G'] for Gh in Ghs]
    hs = cin['h'] + [Gh['h'] for Gh in Ghs]

    sol = cvxsolvers.sdp(cin['c'], Gs=Gs, \
                  hs=hs, A=cin['A'], b=cin['b'])

    print sol['x']
    print abs(sol['x'][3] - 4.5)
    assert (abs(sol['x'][3] - 4.5) <= 1e-3)

    import extractors
    print extractors.extract_solutions_lasserre(M, sol['x'], Kmax=2)
    print 'true values are 1 and 2'
Пример #18
0
def solve_moments_with_convexiterations(MM, constraints, maxrank = 3, slack = 1e-3, maxiter = 200):
    """
    Solve using the moment matrix iteratively using the rank constrained convex iterations
    Use @symbols with basis bounded by degree @deg.
    Also use the constraints.
    """
    cin = get_cvxopt_inputs(MM, constraints)
    Bf = MM.get_Bflat()
    R = np.random.rand(len(MM), len(MM))
    #W = R.dot(R.T)
    W = np.eye(len(MM))
    tau = []
    for i in xrange(maxiter):
        w = Bf.dot(W.flatten())
        #solsdp = cvxsolvers.sdp(matrix(w), Gs=cin['G'], hs=cin['h'], A=cin['A'], b=cin['b'])
        solsdp = cvxsolvers.sdp(matrix(w), Gs=cin['G'], hs=cin['h'], Gl=cin['Gl'], hl=cin['hl'])
        Xstar = MM.numeric_instance(solsdp['x'])
        W,solW = solve_W(Xstar, maxrank)
        W = np.array(W)
        ctau =  np.sum(W * Xstar.flatten())
        if ctau < 1e-3:
            break
        tau.append(ctau)
        
    #ipdb.set_trace()
    print tau
    return solsdp
Пример #19
0
def testsdp(opts):
    c = matrix([1., -1., 1.])
    G = [
        matrix([[-7., -11., -11., 3.], [7., -18., -18., 8.],
                [-2., -8., -8., 1.]])
    ]
    G += [
        matrix([[-21., -11., 0., -11., 10., 8., 0., 8., 5.],
                [0., 10., 16., 10., -10., -10., 16., -10., 3.],
                [-5., 2., -17., 2., -6., 8., -17., -7., 6.]])
    ]
    h = [matrix([[33., -9.], [-9., 26.]])]
    h += [matrix([[14., 9., 40.], [9., 91., 10.], [40., 10., 15.]])]

    sol = solvers.sdp(c, Gs=G, hs=h)
    print "x = \n", str2(sol['x'], "%.9f")
    print "zs[0] = \n", helpers.str2(sol['zs'][0], "%.9f")
    print "zs[1] = \n", helpers.str2(sol['zs'][1], "%.9f")
    helpers.run_go_test(
        "../testsdp", {
            'x': sol['x'],
            'ss0': sol['ss'][0],
            'ss1': sol['ss'][1],
            'zs0': sol['zs'][0],
            'zs0': sol['zs'][1]
        })
Пример #20
0
def solve_moments_with_convexiterations(MM,
                                        constraints,
                                        maxrank=3,
                                        slack=1e-3,
                                        maxiter=200):
    """
    Solve using the moment matrix iteratively using the rank constrained convex iterations
    Use @symbols with basis bounded by degree @deg.
    Also use the constraints.
    """
    cin = get_cvxopt_inputs(MM, constraints)
    Bf = MM.get_Bflat()
    R = np.random.rand(len(MM), len(MM))
    #W = R.dot(R.T)
    W = np.eye(len(MM))
    tau = []
    for i in xrange(maxiter):
        w = Bf.dot(W.flatten())
        #solsdp = cvxsolvers.sdp(matrix(w), Gs=cin['G'], hs=cin['h'], A=cin['A'], b=cin['b'])
        solsdp = cvxsolvers.sdp(matrix(w),
                                Gs=cin['G'],
                                hs=cin['h'],
                                Gl=cin['Gl'],
                                hl=cin['hl'])
        Xstar = MM.numeric_instance(solsdp['x'])
        W, solW = solve_W(Xstar, maxrank)
        W = np.array(W)
        ctau = np.sum(W * Xstar.flatten())
        if ctau < 1e-3:
            break
        tau.append(ctau)

    #ipdb.set_trace()
    print tau
    return solsdp
Пример #21
0
def optimize_polynomial(pol):
    r"""
    Optimization polynomial by representing it as an SDP in the
    appropriate basis
    TODO: Support constraints

    \min_y c^\top y
           subject to M(y) \succeq 0
           y_1 = 1

    @param poly       - polynomial to optimize
    @param basis      - basis for the polynomial (can be 'power',
                        'symmetric', 'symmetric_irreducible')
    @returns (p*, y*) - the optimal value of the polynomial and
                        y* = E_\mu[b(x)].
    """

    # basis in sorted order from 1 to largest
    # Get the largest monomial term
    max_degree = get_max_degree(pol)
    half_degree = map(lambda deg: int(np.ceil(deg / 2.)), max_degree)

    # Generate basis
    basis = list(it.product(*[range(deg + 1) for deg in max_degree]))
    half_basis = list(it.product(*[range(deg + 1) for deg in half_degree]))
    N, M = len(basis), len(half_basis)

    # The coefficient term
    c = np.matrix([float(pol.nth(*elem)) for elem in basis]).T
    # The inequality term, enforcing sos-ness
    G = np.zeros((M**2, N))
    for i, j in it.product(xrange(M), xrange(M)):
        e1, e2 = half_basis[i], half_basis[j]
        monom = tuple_add(e1, e2)
        k = basis.index(monom)
        if k != -1:
            G[M * i + j, k] = 1
    h = np.zeros((M, M))

    # Finally, y_1 = 1
    A = np.zeros((1, N))
    A[0, 0] = 1
    b = np.zeros(1)
    b[0] = 1

    sol = solvers.sdp(
        c=matrix(c),
        Gs=[matrix(-G)],
        hs=[matrix(h)],
        A=matrix(A),
        b=matrix(b),
    )
    assert sol['status'] == 'optimal'

    return dict([(get_monom(pol, coeff), val)
                 for coeff, val in zip(basis, list(sol['x']))])
Пример #22
0
def cheb(A, b, Sigma):

    # Calculates Chebyshev lower bound on Prob(A*x <= b) where
    # x in R^2 has mean zero and covariance Sigma.
    #
    # maximize    1 - tr(Sigma*P) - r
    # subject to  [ P,                    q - (tauk/2)*ak ]
    #             [ (q - (tauk/2)*ak)',   r - 1 + tauk*bk ] >= 0,
    #                                                 k = 0,...,m-1
    #             [ P,   q ]
    #             [ q',  r ] >= 0
    #             tauk >= 0, k=0,...,m-1.
    #
    # variables P[0,0], P[1,0], P[1,1], q[0], q[1], r, tau[0], ...,
    #     tau[m-1].

    m = A.size[0]
    novars = 3 + 2 + 1 + m

    # Cost function.
    c = matrix(0.0, (novars, 1))
    c[0], c[1], c[2] = Sigma[0, 0], 2 * Sigma[1, 0], Sigma[1, 1]
    c[5] = 1.0

    Gs = [spmatrix([], [], [], (9, novars)) for k in range(m + 1)]

    # Coefficients of P, q, r in LMI constraints.
    for k in range(m + 1):
        Gs[k][0, 0] = -1.0  # P[0,0]
        Gs[k][1, 1] = -1.0  # P[1,0]
        Gs[k][4, 2] = -1.0  # P[1,1]
        Gs[k][2, 3] = -1.0  # q[0]
        Gs[k][5, 4] = -1.0  # q[1]
        Gs[k][8, 5] = -1.0  # r
    # Coefficients of tau.
    for k in range(m):
        Gs[k][2, 6 + k] = 0.5 * A[k, 0]
        Gs[k][5, 6 + k] = 0.5 * A[k, 1]
        Gs[k][8, 6 + k] = -b[k]

    hs = [ matrix(8*[0.0] + [-1.0], (3,3)) for k in range(m) ] + \
        [ matrix(0.0, (3,3)) ]

    # Constraints tau >= 0.
    Gl, hl = spmatrix(-1.0, range(m), range(6, 6 + m)), matrix(0.0, (m, 1))

    sol = solvers.sdp(c, Gl, hl, Gs, hs)
    P = matrix(sol['x'][[0, 1, 1, 2]], (2, 2))
    q = matrix(sol['x'][[3, 4]], (2, 1))
    r = sol['x'][5]
    bound = 1.0 - Sigma[0] * P[0] - 2 * Sigma[1] * P[1] - Sigma[3] * P[3] - r

    # Worst-case distribution from dual solution.
    X = [Z[2, :2].T / Z[2, 2] for Z in sol['zs'] if Z[2, 2] > 1e-5]

    return bound, P, q, r, X
Пример #23
0
def cheb(A, b, Sigma):

    # Calculates Chebyshev lower bound on Prob(A*x <= b) where
    # x in R^2 has mean zero and covariance Sigma.
    #
    # maximize    1 - tr(Sigma*P) - r
    # subject to  [ P,                    q - (tauk/2)*ak ]
    #             [ (q - (tauk/2)*ak)',   r - 1 + tauk*bk ] >= 0,
    #                                                 k = 0,...,m-1
    #             [ P,   q ]
    #             [ q',  r ] >= 0
    #             tauk >= 0, k=0,...,m-1.
    #
    # variables P[0,0], P[1,0], P[1,1], q[0], q[1], r, tau[0], ..., 
    #     tau[m-1].

    m = A.size[0]
    novars = 3 + 2 + 1 + m

    # Cost function.
    c = matrix(0.0, (novars,1))
    c[0], c[1], c[2] = Sigma[0,0], 2*Sigma[1,0], Sigma[1,1]
    c[5] = 1.0

    Gs = [ spmatrix([],[],[], (9,novars)) for k in range(m+1) ]

    # Coefficients of P, q, r in LMI constraints.
    for k in range(m+1):
        Gs[k][0,0] = -1.0   # P[0,0]
        Gs[k][1,1] = -1.0   # P[1,0]
        Gs[k][4,2] = -1.0   # P[1,1]
        Gs[k][2,3] = -1.0   # q[0]
        Gs[k][5,4] = -1.0   # q[1]
        Gs[k][8,5] = -1.0   # r
    # Coefficients of tau.
    for k in range(m):
        Gs[k][2, 6+k] = 0.5 * A[k,0]   
        Gs[k][5, 6+k] = 0.5 * A[k,1]   
        Gs[k][8, 6+k] = -b[k]   
    
    hs = [ matrix(8*[0.0] + [-1.0], (3,3)) for k in range(m) ] + \
        [ matrix(0.0, (3,3)) ]

    # Constraints tau >= 0.
    Gl, hl = spmatrix(-1.0, range(m), range(6,6+m)), matrix(0.0, (m,1)) 

    sol = solvers.sdp(c, Gl, hl, Gs, hs)
    P = matrix(sol['x'][[0,1,1,2]], (2,2))  
    q = matrix(sol['x'][[3,4]], (2,1))  
    r = sol['x'][5]
    bound = 1.0 - Sigma[0]*P[0] - 2*Sigma[1]*P[1] - Sigma[3]*P[3] - r

    # Worst-case distribution from dual solution.
    X = [ Z[2,:2].T / Z[2,2] for Z in sol['zs'] if Z[2,2] > 1e-5 ]

    return bound, P, q, r, X
Пример #24
0
def solve_sdp_cvx(G):
    B = construct_B(G)
    n = B.shape[0]
    h = [matrix(-B)]
    c = matrix(1.0, (n, 1))
    g_matrix = spmatrix(-1, [n * i + i for i in range(n)],
                        range(n),
                        size=(n * n, n))
    sol = solvers.sdp(c, Gs=[g_matrix], hs=h)
    return get_labels_sdp2(np.array(sol['zs'][0]))
def SDP_diagonal(cov_matrix, multiKN_par):
    cor_matrix = np.diag(1/np.sqrt(np.diag(cov_matrix))).dot(cov_matrix.dot(np.diag(1/np.sqrt(np.diag(cov_matrix)))))
    dim = cov_matrix.shape[0]
    c = matrix(-np.ones(dim))
    Gl = matrix(np.vstack([-np.identity(dim), np.identity(dim)]))
    hl = matrix(np.hstack([np.zeros(dim), np.ones(dim)]))
    Gs = [matrix(np.vstack([flatten_Ei(l,dim) for l in np.arange(dim)]).T)]
    hs = [matrix(multiKN_par*cor_matrix)]            
    result = solvers.sdp(c,Gl=Gl,hl=hl,Gs=Gs,hs=hs)
    return np.diag((np.array(result['x'])[:,0]))*0.9999*np.diag(cov_matrix)
Пример #26
0
def optimize_polynomial(pol):
    r"""
    Optimization polynomial by representing it as an SDP in the
    appropriate basis
    TODO: Support constraints

    \min_y c^\top y
           subject to M(y) \succeq 0
           y_1 = 1

    @param poly       - polynomial to optimize
    @param basis      - basis for the polynomial (can be 'power',
                        'symmetric', 'symmetric_irreducible')
    @returns (p*, y*) - the optimal value of the polynomial and
                        y* = E_\mu[b(x)].
    """

    # basis in sorted order from 1 to largest
    # Get the largest monomial term
    max_degree = get_max_degree(pol)
    half_degree = map( lambda deg: int(np.ceil(deg/2.)), max_degree)

    # Generate basis
    basis = list(it.product( *[range(deg + 1) for deg in max_degree] ) )
    half_basis = list(it.product( *[range(deg + 1) for deg in half_degree] ) )
    N, M = len(basis), len(half_basis)

    # The coefficient term
    c = np.matrix([float(pol.nth(*elem)) for elem in basis]).T
    # The inequality term, enforcing sos-ness
    G = np.zeros( ( M**2, N ) )
    for i, j in it.product(xrange(M), xrange(M)):
        e1, e2 = half_basis[i], half_basis[j]
        monom = tuple_add(e1,e2)
        k = basis.index(monom)
        if k != -1:
            G[ M * i + j, k ] = 1
    h = np.zeros((M, M))

    # Finally, y_1 = 1
    A = np.zeros((1, N))
    A[0,0] = 1
    b = np.zeros(1)
    b[0] = 1

    sol = solvers.sdp(
            c = matrix(c),
            Gs = [matrix(-G)],
            hs = [matrix(h)],
            A  = matrix(A),
            b  = matrix(b),
            )
    assert sol['status'] == 'optimal'

    return dict([ (get_monom(pol,coeff), val) for coeff, val in zip(basis, list(sol['x'])) ] )
Пример #27
0
def test_1dmog(mus=[-2., 2.], sigs=[1., np.sqrt(3.)], pis=[0.5, 0.5], deg = 3, obsdeg = 6):
    print 'testing 1d mixture of Gaussians'
    # constraints on parameters here
    
    K = len(mus)
    mu,sig = sp.symbols('mu,sigma')
    M = mm.MomentMatrix(deg, [mu, sig], morder='grevlex')

    num_constrs = obsdeg + 1; # so observe num_constrs-1 moments
    H = abs(hermite_coeffs(num_constrs))
    constrs = [0]*num_constrs
    
    for order in range(num_constrs):
        for i in range(order+1):
            constrs[order] = constrs[order] + H[order,i]* mu**(i) * sig**(order-i)
        constrsval = 0
        for k in range(K):
            constrsval += pis[k]*constrs[order].evalf(subs={mu:mus[k], sig:sigs[k]})
        constrs[order] -= constrsval
    print constrs
    cin = M.get_cvxopt_inputs(constrs[1:])

    gs = [sig-0.2, 5-sig, sig+5, 10-mu, mu+10]
    #gs = [sig-0.2]
    locmatrices = [mm.LocalizingMatrix(M, g) for g in gs]
    Ghs = [lm.get_cvxopt_Gh() for lm in locmatrices]

    Gs=cin['G']# + [Gh['G'] for Gh in Ghs]
    hs=cin['h']# + [Gh['h'] for Gh in Ghs]
    
    sol = solvers.sdp(cin['c'],Gs=Gs, \
                  hs=hs, A=cin['A'], b=cin['b'])

    
    ## Q = cin['A'].T*cin['A']
    ## weight = 1;
    ## dims = {}
    ## dims['l'] = 0; dims['q'] = []; dims['s'] = [len(M.row_monos)]
    ## ipdb.set_trace()
    ## sol = solvers.coneqp(Q*weight, cin['A'].T*cin['b']*weight + cin['c'],G=Gs[0], \
    ##               h=hs[0][:], dims = dims, A=cin['A'], b=cin['b'])

    for i,mono in enumerate(M.matrix_monos):
        trueval = 0;
        if i>0:
            for k in range(K):
                trueval += pis[k]*mono.evalf(subs={mu:mus[k], sig:sigs[k]})
        else:
            trueval = 1
        print '%s:\t%f\t%f' % (str(mono), sol['x'][i], trueval)
    print M.extract_solutions_lasserre(sol['x'], Kmax=len(mus))
    #import MomentMatrixSolver as mmsolver
    #print mmsolver.alternating_solver(M, constrs, 2, maxiter=30000)
    return M,sol
Пример #28
0
    def test_dsdp(self):
        # Setup problem
        c = matrix([1., -1., 1.])
        G = [matrix([[-7., -11., -11., 3.],
                     [7., -18., -18., 8.],
                     [-2.,  -8.,  -8., 1.]])]
        G += [matrix([[-21., -11.,   0., -11.,  10.,   8.,   0.,   8., 5.],
                      [0.,  10.,  16.,  10., -10., -10.,  16., -10., 3.],
                      [-5.,   2., -17.,   2.,  -6.,   8., -17.,   8., 6.]])]
        h = [matrix([[33., -9.], [-9., 26.]])]
        h += [matrix([[14., 9., 40.], [9., 91., 10.], [40., 10., 15.]])]

        # Solve with default solver
        sol = solvers.sdp(c, Gs=G, hs=h)

        # Solve with DSDP
        sol_dsdp = solvers.sdp(c, Gs=G, hs=h, solver='dsdp')

        # Compare solutions
        self.assertMatrixAlmostEqual(sol['x'], sol_dsdp['x'])
        self.assertMatrixAlmostEqual(sol['zs'][0], sol_dsdp['zs'][0])
        self.assertMatrixAlmostEqual(sol['zs'][1], sol_dsdp['zs'][1])
Пример #29
0
def _calculate_lovasz_embeddings_(A):
    """Calculate the lovasz embeddings for the given graph.

    Parameters
    ----------
    A : np.array, ndim=2
        The adjacency matrix.

    Returns
    -------
    lovasz_theta : float
        Returns the lovasz theta number.

    optimization_solution : np.array
        Returns the slack variable solution
        of the primal optimization program.

    """
    if A.shape[0] == 1:
        return 1.0
    else:
        tf_adjm = (np.abs(A) <= min_weight)
        np.fill_diagonal(tf_adjm, False)
        i_list, j_list = np.nonzero(np.triu(tf_adjm.astype(int), k=1))

        nv = A.shape[0]
        ne = len(i_list)

        x_list = list()
        e_list = list()
        for (e, (i, j)) in enumerate(zip(i_list, j_list)):
            e_list.append(int(e)), x_list.append(int(i * nv + j))
            if tf_adjm[i, j]:
                e_list.append(int(e)), x_list.append(int(i * nv + j))

    # Add on the last row, diagonal elements
    e_list = e_list + (nv * [ne])
    x_list = x_list + [int(i * nv + i) for i in range(nv)]

    # initialise g sparse (to values -1, based on two list that
    # define index and one that defines shape
    g_sparse = spmatrix(-1, x_list, e_list, (nv * nv, ne + 1))

    # Initialise optimization parameters
    h = matrix(-1.0, (nv, nv))
    c = matrix([0.0] * ne + [1.0])

    # Solve the convex optimization problem
    sol = sdp(c, Gs=[g_sparse], hs=[h])

    return np.array(sol['ss'][0]), sol['x'][ne]
Пример #30
0
def test_unimixture():
    print 'testing simple unimixture with a skipped observation'
    x = sp.symbols('x')
    M = mm.MomentMatrix(3, [x], morder='grevlex')
    constrs = [x-1.5, x**2-2.5, x**4-8.5]
    cin = M.get_cvxopt_inputs(constrs)
    sol = solvers.sdp(cin['c'], Gs=cin['G'], \
                  hs=cin['h'], A=cin['A'], b=cin['b'])

    print sol['x']
    print abs(sol['x'][3]-4.5)
    assert(abs(sol['x'][3]-4.5) <= 1e-5)
    print M.extract_solutions_lasserre(sol['x'], Kmax = 2)
    return M,sol
Пример #31
0
def test_unimixture():
    print 'testing simple unimixture with a skipped observation'
    x = sp.symbols('x')
    M = mm.MomentMatrix(3, [x], morder='grevlex')
    constrs = [x - 1.5, x**2 - 2.5, x**4 - 8.5]
    cin = M.get_cvxopt_inputs(constrs)
    sol = solvers.sdp(cin['c'], Gs=cin['G'], \
                  hs=cin['h'], A=cin['A'], b=cin['b'])

    print sol['x']
    print abs(sol['x'][3] - 4.5)
    assert (abs(sol['x'][3] - 4.5) <= 1e-5)
    print M.extract_solutions_lasserre(sol['x'], Kmax=2)
    return M, sol
Пример #32
0
def solve_basic_constraints(MM, constraints, slack = 1e-2):
    """
    Solve using the moment matrix.
    Use @symbols with basis bounded by degree @deg.
    Also use the constraints.
    """
    cin = get_cvxopt_inputs(MM, constraints, slack=slack)
    Bf = MM.get_Bflat()
    R = np.random.rand(len(MM), len(MM))
    W = R.dot(R.T)
    #W = np.eye(len(MM))
    w = Bf.dot(W.flatten())
    solsdp = cvxsolvers.sdp(c=cin['c'], Gs=cin['G'], hs=cin['h'], Gl=cin['Gl'], hl=cin['hl'])
    #ipdb.set_trace()
    return solsdp
Пример #33
0
def test_bimixture():
    print 'testing simple unimixture with a skipped observation'
    x,y = sp.symbols('x,y')
    constrs = [y+0.5, x-0.5, x+y-0, x**2+y-0, x+y**2-1, x+x**2-1, x*y + y**2, x-4*y - 2.5, y**2*x -0.5] # solvable with deg=3, but not deg=2
        
    M = mp.MomentMatrix(2, (x,y), morder='grevlex')
    
    cin = mp.solvers.get_cvxopt_inputs(M, constrs)
    sol = solvers.sdp(matrix(sc.rand(*cin['c'].size)), Gs=cin['G'], \
                  hs=cin['h'], A=cin['A'], b=cin['b'])

    M.pretty_print(sol)

    print mp.extractors.extract_solutions_lasserre(M, sol['x'], Kmax = 2)
    return M,sol
Пример #34
0
def sdp_solver(cormatrix):
    p = np.shape(cormatrix)[0]
    c = -np.ones(p)

    G1 = np.zeros((p, p * p))
    for i in range(p):
        G1[i, i * p + i] = 1.0

    c = matrix(c)
    Gs = [matrix(G1.T)] + [matrix(G1.T)]
    hs = [matrix(2 * cormatrix)] + [matrix(np.identity(p))]
    G0 = matrix(-(np.identity(p)))
    h0 = matrix(np.zeros(p))

    sol = solvers.sdp(c, G0, h0, Gs, hs)
    return np.array(sol['x'])
Пример #35
0
def optimizePSD(distMat, numdata):
    G0 = matrix(
        np.concatenate([-np.eye(numdata * numdata),
                        np.eye(numdata * numdata)]))
    h0 = matrix(
        np.concatenate([
            np.zeros([numdata * numdata, 1]),
            np.ones([numdata * numdata, 1])
        ]))
    wopt = []
    for nvar in range(len(distMat)):
        c = np.reshape(distMat[nvar], [numdata * numdata, 1])
        stat = np.sort(np.reshape(c, [numdata * numdata]))
        c = matrix(c) - np.median(c)
        sol = solvers.sdp(c, Gl=G0, hl=h0)
        #print (np.round((np.reshape(sol['x'], [numdata, numdata])), 3))
        wopt.append(np.round(np.reshape(sol['x'], [numdata, numdata]), 3))
    return wopt
Пример #36
0
def test_bimixture():
    print 'testing simple unimixture with a skipped observation'
    x, y = sp.symbols('x,y')
    constrs = [
        y + 0.5, x - 0.5, x + y - 0, x**2 + y - 0, x + y**2 - 1, x + x**2 - 1,
        x * y + y**2, x - 4 * y - 2.5, y**2 * x - 0.5
    ]  # solvable with deg=3, but not deg=2

    M = mp.MomentMatrix(2, (x, y), morder='grevlex')

    cin = mp.solvers.get_cvxopt_inputs(M, constrs)
    sol = solvers.sdp(matrix(sc.rand(*cin['c'].size)), Gs=cin['G'], \
                  hs=cin['h'], A=cin['A'], b=cin['b'])

    M.pretty_print(sol)

    print mp.extractors.extract_solutions_lasserre(M, sol['x'], Kmax=2)
    return M, sol
Пример #37
0
def _design_knockoff_sdp(exog):
    """
    Use semidefinite programming to construct a knockoff design
    matrix.

    Requires cvxopt to be installed.
    """

    try:
        from cvxopt import solvers, matrix
    except ImportError:
        raise ValueError("SDP knockoff designs require installation of cvxopt")

    nobs, nvar = exog.shape

    # Standardize exog
    xnm = np.sum(exog**2, 0)
    xnm = np.sqrt(xnm)
    exog /= xnm

    Sigma = np.dot(exog.T, exog)

    c = matrix(-np.ones(nvar))

    h0 = np.concatenate((np.zeros(nvar), np.ones(nvar)))
    h0 = matrix(h0)
    G0 = np.concatenate((-np.eye(nvar), np.eye(nvar)), axis=0)
    G0 = matrix(G0)

    h1 = 2 * Sigma
    h1 = matrix(h1)
    i, j = np.diag_indices(nvar)
    G1 = np.zeros((nvar*nvar, nvar))
    G1[i*nvar + j, i] = 1
    G1 = matrix(G1)

    solvers.options['show_progress'] = False
    sol = solvers.sdp(c, G0, h0, [G1], [h1])
    sl = np.asarray(sol['x']).ravel()

    xcov = np.dot(exog.T, exog)
    exogn = _get_knmat(exog, xcov, sl)

    return exog, exogn, sl
Пример #38
0
def _design_knockoff_sdp(exog):
    """
    Use semidefinite programming to construct a knockoff design
    matrix.

    Requires cvxopt to be installed.
    """

    try:
        from cvxopt import solvers, matrix
    except ImportError:
        raise ValueError("SDP knockoff designs require installation of cvxopt")

    nobs, nvar = exog.shape

    # Standardize exog
    xnm = np.sum(exog**2, 0)
    xnm = np.sqrt(xnm)
    exog /= xnm

    Sigma = np.dot(exog.T, exog)

    c = matrix(-np.ones(nvar))

    h0 = np.concatenate((np.zeros(nvar), np.ones(nvar)))
    h0 = matrix(h0)
    G0 = np.concatenate((-np.eye(nvar), np.eye(nvar)), axis=0)
    G0 = matrix(G0)

    h1 = 2 * Sigma
    h1 = matrix(h1)
    i, j = np.diag_indices(nvar)
    G1 = np.zeros((nvar * nvar, nvar))
    G1[i * nvar + j, i] = 1
    G1 = matrix(G1)

    solvers.options['show_progress'] = False
    sol = solvers.sdp(c, G0, h0, [G1], [h1])
    sl = np.asarray(sol['x']).ravel()

    xcov = np.dot(exog.T, exog)
    exogn = _get_knmat(exog, xcov, sl)

    return exog, exogn, sl
Пример #39
0
def test_unimixture():
    print 'testing simple unimixture with a skipped observation'
    x = sp.symbols('x')
    
    constrs = [x+x**2-2, x+2*x**4-3, 2*x**2+3*x**4-5] # always solvable
    constrs = [x+x**2-1, x**2+2*x**3-1, 2*x**3+3*x**4-3, 2*x+x**3 - 0] # always solvable
    constrs = [x-1.5, x**2-2.5, x**5-16.5] # solvable with deg=3, but not deg=2
    constrs = [x-1.5, x**2-2.5, x**4-8.5] # solvable with deg=3, but not deg=2
    
    M = mp.MomentMatrix(3, [x], morder='grevlex')
    
    cin = mp.solvers.get_cvxopt_inputs(M, constrs)
    sol = solvers.sdp(matrix(sc.rand(*cin['c'].size)), Gs=cin['G'], \
                  hs=cin['h'], A=cin['A'], b=cin['b'])

    M.pretty_print(sol)

    print mp.extractors.extract_solutions_lasserre(M, sol['x'], Kmax = 2)
    return M,sol
Пример #40
0
def solve_basic_constraints(MM, constraints, slack=1e-2):
    """
    Solve using the moment matrix.
    Use @symbols with basis bounded by degree @deg.
    Also use the constraints.
    """
    cin = get_cvxopt_inputs(MM, constraints, slack=slack)
    Bf = MM.get_Bflat()
    R = np.random.rand(len(MM), len(MM))
    W = R.dot(R.T)
    #W = np.eye(len(MM))
    w = Bf.dot(W.flatten())
    solsdp = cvxsolvers.sdp(c=cin['c'],
                            Gs=cin['G'],
                            hs=cin['h'],
                            Gl=cin['Gl'],
                            hl=cin['hl'])
    #ipdb.set_trace()
    return solsdp
Пример #41
0
def test_K_by_D(K=3, D=3, pis=[0.25, 0.25, 0.5], deg=1, degobs=3):
    print 'testing the K by D mixture'
    np.random.seed(0)
    params = np.random.randint(0, 10, size=(K, D))
    print 'the true parameters (K by D): ' + str(params) + '\n' + str(pis)
    xs = sp.symbols('x1:' + str(D + 1))
    print xs
    M = mm.MomentMatrix(deg, xs, morder='grevlex')

    constrs = [0] * len(M.matrix_monos)
    for i, mono in enumerate(M.matrix_monos):
        constrs[i] = mono
        constrsval = 0
        for k in range(K):
            subsk = {xs[i]: params[k, i]
                     for i in range(D)}
            constrsval += pis[k] * mono.evalf(subs=subsk)
        constrs[i] -= constrsval

    #print constrs
    filtered_constrs = [
        constr for constr in constrs[1:]
        if constr.as_poly().total_degree() <= degobs
    ]
    print filtered_constrs
    cin = M.get_cvxopt_inputs(filtered_constrs)
    sol = solvers.sdp(cin['c'], Gs=cin['G'], \
                  hs=cin['h'], A=cin['A'], b=cin['b'])

    print 'Mono:\tEstimmated\tTrue'
    for i, mono in enumerate(M.matrix_monos):
        trueval = 0
        if i > 0:
            for k in range(K):
                subsk = {xs[i]: params[k, i]
                         for i in range(D)}
                trueval += pis[k] * mono.evalf(subs=subsk)
        else:
            trueval = 1
        print '%s:\t%f\t%f' % (str(mono), sol['x'][i], trueval)
    print M.extract_solutions_lasserre(sol['x'], Kmax=K)
    return M, sol
Пример #42
0
def SDP(rho, q, n):
    c = np.eye(2**n).flatten().tolist()
    c = matrix(c)
    A, b = [matrix(Amatrix(n).tolist())
            ], [matrix(np.zeros((1, 2**(n - 1) * (2**n - 1))).tolist())]
    A, b = cvxopt.matrix(A), cvxopt.matrix(b)
    G = [matrix((-1 * np.eye(2**n * 2**n)).tolist())]
    h = [
        matrix((-q[len(rho) - 1] * kronecker_prod(rho[len(rho) - 1])).tolist())
    ]
    for _ in range(len(rho) - 1):
        G += [matrix((-1 * np.eye(2**n * 2**n)).tolist())]
        h += [matrix((-q[_] * kronecker_prod(rho[_])).tolist())]
    solvers.options['show_progress'] = False
    sol = solvers.sdp(c, Gs=G, hs=h, A=A, b=b)
    v = []
    for _ in range(2**n * 2**n):
        v.append(sol['x'][_])
    value = np.array(v) @ np.eye(2**n).flatten()
    return value
Пример #43
0
def solve_Q(x_dim, A, B, D):
    # x = [s vec(Z) vec(Q)]
    c_dim = 1 + 2 * x_dim * (x_dim + 1) / 2
    c = zeros(c_dim)
    c[0] = x_dim
    prev = 1
    for i in range(x_dim):
        vec_pos = prev + i * (i + 1) / 2 + i
        c[vec_pos] = 1
    cm = matrix(c)

    G = construct_coeff_matrix(x_dim, B)
    G = -G  # set negative since s = h - Gx in cvxopt's sdp solver
    Gs = [matrix(G)]

    h, _ = construct_const_matrix(x_dim, A, B, D)
    hs = [matrix(h)]

    sol = solvers.sdp(cm, Gs=Gs, hs=hs)
    return sol, c, G, h
Пример #44
0
def normal_SDP_solver(Z1, Z2, input_dim1, input_dim2, Z3=None, Z4=None):
    C = np.matmul(np.transpose(Z2), Z1)

    if (FLAGS.model_size == 'small' and Z3 is not None and Z4 is not None):
        C -= np.matmul(np.transpose(Z4), Z3)
    elif (Z3 is not None):
        C -= np.matmul(np.transpose(Z3), Z1)

    C = (C + np.transpose(C)) / 2
    A = np.ones((input_dim2, input_dim1))
    b = 1

    c, G, h = convert_dual_SDP(C, A, b)
    solvers.options['show_progress'] = False
    solution = solvers.sdp(c, Gs=G, hs=h)
    #print('solution zs ---- ', solution['zs'][0])
    dual_theta = np.array(solution['zs'][0])
    #print('theta ---- ', dual_theta)
    dual_theta = np.reshape(dual_theta, (input_dim1, input_dim2))
    return dual_theta
Пример #45
0
def testsdp(opts):
    c = matrix([1.,-1.,1.])  
    G = [ matrix([[-7., -11., -11., 3.],  
                  [ 7., -18., -18., 8.],  
                  [-2.,  -8.,  -8., 1.]]) ]  
    G += [ matrix([[-21., -11.,   0., -11.,  10.,   8.,   0.,   8., 5.],  
                   [  0.,  10.,  16.,  10., -10., -10.,  16., -10., 3.],  
                   [ -5.,   2., -17.,   2.,  -6.,   8., -17.,  -7., 6.]]) ]  
    h = [ matrix([[33., -9.], [-9., 26.]]) ]  
    h += [ matrix([[14., 9., 40.], [9., 91., 10.], [40., 10., 15.]]) ]  

    sol = solvers.sdp(c, Gs=G, hs=h)  
    print "x = \n", str2(sol['x'], "%.9f")
    print "zs[0] = \n", helpers.str2(sol['zs'][0], "%.9f")
    print "zs[1] = \n", helpers.str2(sol['zs'][1], "%.9f")
    helpers.run_go_test("../testsdp", {'x': sol['x'],
                                       'ss0': sol['ss'][0],
                                       'ss1': sol['ss'][1],
                                       'zs0': sol['zs'][0],
                                       'zs0': sol['zs'][1]})
Пример #46
0
def wcls(A, Ap, b):
    """
    Solves the robust least squares problem

        minimize sup_{||u||<= 1} || (A + sum_k u[k]*Ap[k])*x - b ||_2^2.

    A is mxn.  Ap is a list of mxn matrices.  b is mx1.
    """

    (m, n), p = A.size, len(Ap)

    # minimize    t + v
    # subject to  [ I         *     *  ]
    #             [ P(x)'     v*I  -*  ] >= 0.
    #             [ (A*x-b)'  0     t  ]
    #
    # where P(x) = [Ap[0]*x, Ap[1]*x, ..., Ap[p-1]*x].
    #
    # Variables x (n), v (1), t(1). 

    novars = n + 2
    M = m + p + 1
    c = matrix( n*[0.0] + 2*[1.0])
    Fs = [spmatrix([],[],[], (M**2, novars))]
    for k in range(n):
        # coefficient of x[k]
        S = spmatrix([], [], [], (M, M))
        for j in range(p):
            S[m+j, :m] = -Ap[j][:,k].T
            S[-1, :m ] = -A[:,k].T
        Fs[0][:,k] = S[:] 
    # coefficient of v
    Fs[0][(M+1)*m : (M+1)*(m+p) : M+1, -2] = -1.0
    # coefficient of t
    Fs[0][-1, -1] = -1.0

    hs = [matrix(0.0, (M, M))]
    hs[0][:(M+1)*m:M+1] = 1.0
    hs[0][-1, :m] = -b.T

    return solvers.sdp(c, None, None, Fs, hs)['x'][:n]
Пример #47
0
def wcls(A, Ap, b):
    """
    Solves the robust least squares problem

        minimize sup_{||u||<= 1} || (A + sum_k u[k]*Ap[k])*x - b ||_2^2.

    A is mxn.  Ap is a list of mxn matrices.  b is mx1.
    """

    (m, n), p = A.size, len(Ap)

    # minimize    t + v
    # subject to  [ I         *     *  ]
    #             [ P(x)'     v*I  -*  ] >= 0.
    #             [ (A*x-b)'  0     t  ]
    #
    # where P(x) = [Ap[0]*x, Ap[1]*x, ..., Ap[p-1]*x].
    #
    # Variables x (n), v (1), t(1).

    novars = n + 2
    M = m + p + 1
    c = matrix(n * [0.0] + 2 * [1.0])
    Fs = [spmatrix([], [], [], (M**2, novars))]
    for k in range(n):
        # coefficient of x[k]
        S = spmatrix([], [], [], (M, M))
        for j in range(p):
            S[m + j, :m] = -Ap[j][:, k].T
            S[-1, :m] = -A[:, k].T
        Fs[0][:, k] = S[:]
    # coefficient of v
    Fs[0][(M + 1) * m:(M + 1) * (m + p):M + 1, -2] = -1.0
    # coefficient of t
    Fs[0][-1, -1] = -1.0

    hs = [matrix(0.0, (M, M))]
    hs[0][:(M + 1) * m:M + 1] = 1.0
    hs[0][-1, :m] = -b.T

    return solvers.sdp(c, None, None, Fs, hs)['x'][:n]
Пример #48
0
def solve_A(x_dim, B, C, E, D, Q, max_iters, show_display):
    # x = [s vec(Z) vec(A)]
    c_dim = int(1 + x_dim * (x_dim + 1) / 2 + x_dim ** 2)
    c = zeros((c_dim, 1))
    c[0] = x_dim
    prev = 1
    for i in range(x_dim):
        vec_pos = int(prev + i * (i + 1) / 2 + i)
        c[vec_pos] = 1.
    cm = matrix(c)

    # Scale objective down by T for numerical stability
    eigsQinv = max([abs(1. / q) for q in eig(Q)[0]])
    eigsE = max([abs(e) for e in eig(E)[0]])
    eigsCB = max([abs(cb) for cb in eig(C - B)[0]])
    S = max(eigsQinv, eigsE, eigsCB)
    Qdown = Q / S
    Edown = E / S
    Cdown = C / S
    Bdown = B / S
    # Ensure that D doesn't have negative eigenvals
    # due to numerical issues
    min_D_eig = min(eig(D)[0])
    if min_D_eig < 0:
        # assume abs(min_D_eig) << 1
        D = D + 2 * abs(min_D_eig) * eye(x_dim)
    Gs, _, _, _ = construct_coeff_matrix(x_dim, Qdown, Cdown, Bdown, Edown)
    for i in range(len(Gs)):
        Gs[i] = -Gs[i] + 1e-6
    hs = construct_const_matrix(x_dim, D)

    solvers.options['maxiters'] = max_iters
    solvers.options['show_progress'] = show_display
    sol = solvers.sdp(cm, Gs=Gs, hs=hs)
    # check norm of A:
    avec = np.array(sol['x'])
    avec = avec[int(1 + x_dim * (x_dim + 1) / 2):]
    A = np.reshape(avec, (x_dim, x_dim), order='F')
    return sol, c, Gs, hs
Пример #49
0
def sdp_expl_solve(sym_mat_list, smallest_eig=0.001):
    """
    :param sym_mat_list: list of symmetric matrices G_0, G_1, ..., G_n of same size
    :param smallest_eig: parameter (default 0) may be set to small positive quantity to force non-degeneracy
    :return: solver_status, a string, either 'optimal', 'infeasible', or 'unknown', and sol_vec, a vector approximately
    optimizing the SDP problem if solver_status is 'optimal', and nan instead
    """
    obj_vec = -matrix(get_explicit_rep_objective(sym_mat_list))
    _hs = sym_mat_list[0] - smallest_eig * np.eye(sym_mat_list[0].shape[0])
    sym_grams = matrix(flatten(sym_mat_list[1:])).T
    sol = solvers.sdp(c=obj_vec,
                      Gs=[-sym_grams],
                      hs=[matrix(_hs)],
                      solver='dsdp',
                      options=DSDP_OPTIONS)
    _status = sol['status']
    if _status == 'optimal':
        return 'Optimal solution found', sol['x']
    elif 'infeasible' in _status:
        return 'infeasible', nan
    else:
        return 'unknown', nan
Пример #50
0
def solve_A(x_dim, B, C, E, D, Q):
  # x = [s vec(Z) vec(A)]
  MAX_ITERS=30
  c_dim = 1 + x_dim*(x_dim+1)/2 + x_dim**2
  c = zeros(c_dim)
  c[0] = x_dim
  prev = 1
  for i in range(x_dim):
    vec_pos = prev + i * (i+1)/2 + i
    c[vec_pos] = 1.
  cm = matrix(c)

  G,_,_,_ = construct_coeff_matrix(x_dim, Q, C, B, E)
  G = -G # set negative since s = h - Gx in cvxopt's sdp solver
  Gs = [matrix(G)]

  h = construct_const_matrix(x_dim, Q, D)
  hs = [matrix(h)]

  solvers.options['maxiters'] = MAX_ITERS
  sol = solvers.sdp(cm, Gs = Gs, hs=hs)
  return sol, c, G, h
Пример #51
0
def solve_W(Xstar, rank):
    """
    minimize trace(Xstar*W)
    s.t.  0 \preceq W \preceq I
    trace(W) = n - rank     
    """
    Balpha = []
    numrow = Xstar.shape[0]
    lowerdiaginds = [(i,j) for (i,j) in \
                     itertools.product(xrange(numrow), xrange(numrow)) if i>j]
    diaginds = [i+i*numrow for i in xrange(numrow)]
    for i in diaginds:
        indices = [i]
        values = [-1]
        Balpha += [spmatrix(values, [0]*len(indices), \
        indices, size=(1,numrow*numrow), tc='d')]
        
    for i,j in lowerdiaginds:
        indices = [i+j*numrow, j+i*numrow]
        values = [-1, -1]
        Balpha += [spmatrix(values, [0]*len(indices), \
        indices, size=(1,numrow*numrow), tc='d')]
        
    Gs = [sparse(Balpha, tc='d').trans()] + [-sparse(Balpha, tc='d').trans()]
    hs = [matrix(np.zeros((numrow, numrow)))] + [matrix(np.eye(numrow))]
    A = sparse(spmatrix([1]*numrow, [0]*numrow, \
        range(numrow), size=(1,numrow*(numrow+1)/2), tc='d'))
    b = matrix([numrow - rank], size=(1,1), tc='d')
    
    x = [np.sum(Xstar.flatten()*matrix(Balphai)) for Balphai in Balpha]
    sol = cvxsolvers.sdp(-matrix(x), Gs=Gs, hs=hs, A=A, b=b)
    w = sol['x']
    Wstar = 0
    for i,val in enumerate(w):
        Wstar += -val*np.array(Balpha[i])
    #ipdb.set_trace()
    return Wstar,sol
Пример #52
0
def maxcutrel(A, solver=None): 
   from cvxopt.base import matrix as m
   from cvxopt.base import spmatrix as spm
   from cvxopt import solvers
   if solver=='dsdp': 
      from cvxopt import dsdp 
   solvers.options['show_progress']=True
#   solvers.options['show_progress']=False

# data type and dimensions
   try: 
      A = A.weighted_adjacency_matrix()
      return maxcutrel(A, solver=solver)
   except (AttributeError,TypeError):
      try:
          A = A.adjacency_matrix()
          return maxcutrel(A, solver=solver)
      except (AttributeError,TypeError):
          pass
      pass
   try: 
      d = A.nrows()

# SDP constraints 
      c = m([-1.]*d)
      G = [spm(1., [i*(d+1) for i in range(d)], range(d), (d*d,d))]
   
      sol = solvers.sdp(c, Gs=G, hs=[m(1.*A.numpy())], solver=solver)
      return 0.25*(sum(sum(A))+sol['primal objective']),sol
   #return sol['primal objective'], sol['status']
   except AttributeError:
      print "Wrong matrix format"
      raise
   else:
      print "Unknown error"
      raise
Пример #53
0
def test_K_by_D(K=3, D=3, pis=[0.25, 0.25, 0.5], deg=1, degobs=3):
    print 'testing the K by D mixture'
    np.random.seed(0); params = np.random.randint(0,10,size=(K,D))
    print 'the true parameters (K by D): '+str(params) + '\n' + str(pis)
    xs = sp.symbols('x1:'+str(D+1))
    print xs
    M = mm.MomentMatrix(deg, xs, morder='grevlex')

    constrs = [0] * len(M.matrix_monos)
    for i,mono in enumerate(M.matrix_monos):
        constrs[i] = mono
        constrsval = 0;
        for k in range(K):
            subsk = {xs[i]:params[k,i] for i in range(D)};
            constrsval += pis[k]*mono.evalf(subs=subsk)
        constrs[i] -= constrsval
        
    #print constrs
    filtered_constrs = [constr for constr in constrs[1:] if constr.as_poly().total_degree()<=degobs]
    print filtered_constrs
    cin = M.get_cvxopt_inputs(filtered_constrs)
    sol = solvers.sdp(cin['c'], Gs=cin['G'], \
                  hs=cin['h'], A=cin['A'], b=cin['b'])

    print 'Mono:\tEstimmated\tTrue'
    for i,mono in enumerate(M.matrix_monos):
        trueval = 0;
        if i>0:
            for k in range(K):
                subsk = {xs[i]:params[k,i] for i in range(D)};
                trueval += pis[k]*mono.evalf(subs=subsk)
        else:
            trueval = 1
        print '%s:\t%f\t%f' % (str(mono), sol['x'][i], trueval)
    print M.extract_solutions_lasserre(sol['x'], Kmax=K)
    return M,sol
Пример #54
0
def solve_ith_GMP(MM, objective, gs, hs, slack = 0):
    """
    Generalized moment problem solver
    @param - objective: a sympy expression that is the objective
    @param - gs: list of contraints defining the semialgebraic set K,
    each g corresponds to localizing matrices. 
    @param - hs: constraints on the moments,
     not needed for polynomial optimization.
    @param - slack: add equalities as pairs of inequalities with slack separation.
    """
    cin = get_cvxopt_inputs(MM, hs, slack=slack)
    Bf = MM.get_Bflat()
    objcoeff,__ = MM.get_Ab([objective], cvxoptmode = False)
    
    locmatrices = [LocalizingMatrix(MM, g) for g in gs]
    Ghs = [get_cvxopt_Gh(lm) for lm in locmatrices]

    # list addition
    Gs=cin['G'] + [Gh['G'] for Gh in Ghs]
    hs=cin['h'] + [Gh['h'] for Gh in Ghs]
    # print Ghs
    solsdp = cvxsolvers.sdp(matrix(objcoeff[0,:]), Gs=Gs, hs=hs, A=cin['A'], b=cin['b'])
    #ipdb.set_trace()
    return solsdp
Пример #55
0
def testsdp(opts):
    c = matrix([1.0, -1.0, 1.0])
    G = [matrix([[-7.0, -11.0, -11.0, 3.0], [7.0, -18.0, -18.0, 8.0], [-2.0, -8.0, -8.0, 1.0]])]
    G += [
        matrix(
            [
                [-21.0, -11.0, 0.0, -11.0, 10.0, 8.0, 0.0, 8.0, 5.0],
                [0.0, 10.0, 16.0, 10.0, -10.0, -10.0, 16.0, -10.0, 3.0],
                [-5.0, 2.0, -17.0, 2.0, -6.0, 8.0, -17.0, -7.0, 6.0],
            ]
        )
    ]
    h = [matrix([[33.0, -9.0], [-9.0, 26.0]])]
    h += [matrix([[14.0, 9.0, 40.0], [9.0, 91.0, 10.0], [40.0, 10.0, 15.0]])]

    solvers.options.update(opts)
    sol = solvers.sdp(c, Gs=G, hs=h)
    # localcones.options.update(opts)
    # sol = localcones.sdp(c, Gs=G, hs=h)
    print "x = \n", helpers.str2(sol["x"], "%.9f")
    print "zs[0] = \n", helpers.str2(sol["zs"][0], "%.9f")
    print "zs[1] = \n", helpers.str2(sol["zs"][1], "%.9f")
    print "\n *** running GO test ***"
    rungo(sol)
Пример #56
0
'''
Created on Apr 16, 2013

@author: Nguyen Huu Hiep
'''

from cvxopt import matrix, solvers

c = matrix([1.,-1.,1.])
G = [ matrix([[-7., -11., -11., 3.],
                  [ 7., -18., -18., 8.],
                  [-2.,  -8.,  -8., 1.]]) ]
G += [ matrix([[-21., -11.,   0., -11.,  10.,   8.,   0.,   8., 5.],
                   [  0.,  10.,  16.,  10., -10., -10.,  16., -10., 3.],
                   [ -5.,   2., -17.,   2.,  -6.,   8., -17.,  -7., 6.]]) ]
h = [ matrix([[33., -9.], [-9., 26.]]) ]
h += [ matrix([[14., 9., 40.], [9., 91., 10.], [40., 10., 15.]]) ]
sol = solvers.sdp(c, Gs=G, hs=h)
print(sol['x'])
#[-3.68e-01]
#[ 1.90e+00]
#[-8.88e-01]
print(sol['zs'][0])
#[ 3.96e-03 -4.34e-03]
#[-4.34e-03  4.75e-03]
print(sol['zs'][1])
#[ 5.58e-02 -2.41e-03  2.42e-02]
#[-2.41e-03  1.04e-04 -1.05e-03]
#[ 2.42e-02 -1.05e-03  1.05e-02]
Пример #57
0
    def find_ellipsoid(self):
    
        # return None if not enough data is available
        if(self.pa.size[1] < 3 or self.pb.size[1] < 3):
            return None
        
        # homogenize coordinates
        pah = self.homogenize(self.pa)
        pbh = self.homogenize(self.pb)
        dim = pah.size[0]
        num_pah = pah.size[1]
        num_pbh = pbh.size[1]
        
        # get the c vector
        num_vars = 1+(dim-1)*(dim-1)+2*num_pah+dim*dim
        c = matrix([0.0]*num_vars, (num_vars,1))
        c[0,0] = -self.c1
        
        for i in xrange(dim-1):
            for j in xrange(dim-1):
                if(i == j):
                    c[1+i*(dim-1)+j,0] = self.c2
        for i in xrange(num_pah):
            c[1+(dim-1)*(dim-1)+i,0] = self.c3
        for i in xrange(num_pah):
            c[1+(dim-1)*(dim-1)+num_pah+i,0] = 0.0
        for i in xrange(dim*dim):
            c[1+(dim-1)*(dim-1)+num_pah+num_pah+i,0] = 0.0

        # get Gl and hl
        # separation constraint
        Gl_t = []
        for i in xrange(num_pah):
            row = [0.0]*(1+(dim-1)*(dim-1)+num_pah)+[0.0]*num_pah
            row[(1+(dim-1)*(dim-1)+num_pah)+i] = -1.0
            for j in xrange(dim):
                for k in xrange(dim):
                    row.append(pah[j,i]*pah[k,i])
            Gl_t.append(row)
        for i in xrange(num_pbh):
            row = [1.0]+[0.0]*((dim-1)*(dim-1)+2*num_pah)
            for j in xrange(dim):
                for k in xrange(dim):
                    row.append(-1.0*pbh[j,i]*pbh[k,i])
            Gl_t.append(row)
        # l1 norm constraint
        for i in xrange(num_pah):
            row = [0.0]*num_vars
            row[1+(dim-1)*(dim-1)+i] = -1.0
            row[1+(dim-1)*(dim-1)+num_pah+i] = -1.0
            Gl_t.append(row)
        for i in xrange(num_pah):
            row = [0.0]*num_vars
            row[1+(dim-1)*(dim-1)+i] = -1.0
            row[1+(dim-1)*(dim-1)+num_pah+i] = 1.0
            Gl_t.append(row)
        # construct Gl and hl
        Gl = matrix(Gl_t).trans()
        col = [0.0]*(num_pah+num_pbh)+[-1.0]*num_pah+[1.0]*num_pah
        hl = matrix(col,(3*num_pah+num_pbh,1))
        
        # get Gs and hs
        # positive semidefinite constraint
        # E must be positive semidefinite
        Gs = []
        for i in xrange(1+(dim-1)*(dim-1)+2*num_pah):
            Gs.append([0.0]*(dim*dim))
        for i in xrange(dim*dim):
            v = [0.0]*(dim*dim)
            rpos = int(math.floor(float(i)/float(dim)))
            cpos = i % dim
            if(rpos == cpos):
                v[i] = -1.0
            else:
                v[i] = -0.5
                v[cpos*dim+rpos] = -0.5
            Gs.append(v)
        Gs = [matrix(Gs)]
        hs = [matrix([0.0]*(dim*dim),(dim,dim))]
        
        # block matrix must be positive semidefinite
        Gs2 = []
        Gs2.append([0.0]*(((dim-1)*2)*((dim-1)*2)))
        for i in xrange((dim-1)*(dim-1)):
            v = [0.0]*(((dim-1)*2)*((dim-1)*2))
            rpos = int(math.floor(float(i)/float(dim-1)))
            cpos = i % (dim-1)
            if(rpos == cpos):
                v[(rpos+dim-1)*(dim-1)*2+(dim-1)+cpos] = -1.0
            else:
                v[(rpos+dim-1)*(dim-1)*2+(dim-1)+cpos] = -0.5
                v[(cpos+dim-1)*(dim-1)*2+(dim-1)+rpos] = -0.5
            Gs2.append(v)
            
        for i in xrange(2*num_pah):
            Gs2.append([0.0]*(((dim-1)*2)*((dim-1)*2)))
        for i in xrange(dim*dim):
            v = [0.0]*(((dim-1)*2)*((dim-1)*2))
            rpos = int(math.floor(float(i)/float(dim)))
            cpos = i % dim
            if(rpos == 0 or cpos == 0):
                Gs2.append(v)
            else:
                rpos = rpos-1
                cpos = cpos-1
                if(rpos == cpos):
                    v[rpos*(dim-1)*2+cpos] = -1.0
                else:
                    v[rpos*(dim-1)*2+cpos] = -0.5
                    v[cpos*(dim-1)*2+rpos] = -0.5
                Gs2.append(v)
            
        Gs.append(matrix(Gs2))
        zero_block = matrix([0.0]*((dim-1)*(dim-1)),((dim-1), (dim-1)))
        eye_block = spmatrix(1.0, range(dim-1), range(dim-1))
        hs2 = matrix([[zero_block, eye_block], [eye_block, zero_block]])
        hs.append(hs2)
        
        # get A and b
        # symmetry constraint
        A_t = []
        for i in xrange(dim):
            for j in xrange(i, dim):
                if(i != j):
                    v = [0.0]*(num_vars)
                    v[1+(dim-1)*(dim-1)+2*num_pah+i*dim+j] = 1.0
                    v[1+(dim-1)*(dim-1)+2*num_pah+j*dim+i] = -1.0
                    A_t.append(v)
        for i in xrange(dim-1):
            for j in xrange(i, dim-1):
                if(i != j):
                    v = [0.0]*(num_vars)
                    v[1+i*(dim-1)+j] = 1.0
                    v[1+j*(dim-1)+i] = -1.0
                    A_t.append(v)
        A = matrix(A_t).trans()
        b = matrix([0.0]*A.size[0],(A.size[0],1))
        
        # solve it
        passed = False
        ntime = 10
        while(passed == False and ntime > 0):
            try:
                sol = solvers.sdp(c, Gl, hl, Gs, hs, A, b)
                passed = True
            except ZeroDivisionError:
                time.sleep(0.001)
                ntime = ntime-1
            except Exception:
                time.sleep(0.001)
                ntime = ntime-1
        if(passed == False):
            tmp_nrow = self.pa.size[0]
            tmp_c = mean_ps(self.pa).trans()
            tmp_E = matrix(spmatrix(1.0, range(tmp_nrow), range(tmp_nrow)))
            tmp_rho = 1.0
            return {'c':tmp_c, 'E':tmp_E, 'rho':tmp_rho}
        
        # parse out solution
        x = sol['x']
        k = x[0]
        E_hat = matrix(x[1+(dim-1)*(dim-1)+2*num_pah:], (dim,dim))
        F = E_hat[1:,1:]
        v = E_hat[1:,0]
        s = E_hat[0,0]
        ipiv = matrix(0, (dim-1,1))
        gesv(-F, v, ipiv)
        c = v
        btm = 1-(s-c.trans()*F*c)+0.00000001
        for i in xrange(F.size[0]):
            for j in xrange(F.size[1]):
                F[i,j] = F[i,j]/btm
        E = F
        rho = k
        
        # function return
        return {'c':c, 'E':E, 'rho':rho}
Пример #58
0
# Two variables (t, u).

G = matrix(0.0, ((n+1)**2, 2)) 
G[-1, 0] = -1.0    # coefficient of t
G[: (n+1)**2-1 : n+2, 1] = -1.0    # coefficient of u
h = matrix( [ [ A.T * A,  b.T * A ], [ A.T * b, b.T * b ] ] ) 
c = matrix(1.0, (2,1))

nopts = 40
alpha1 = [2.0/(nopts//2-1) * alpha for alpha in range(nopts//2) ] + \
    [ 2.0 + (15.0 - 2.0)/(nopts//2) * alpha for alpha in 
        range(1,nopts//2+1) ]
lbnds = [ blas.nrm2(b)**2 ]
for alpha in alpha1[1:]:  
    c[1:] = alpha
    lbnds += [ -blas.dot(c, solvers.sdp(c, Gs=[G], hs=[h])['x']) ]

nopts = 10
alpha2 = [ 1.0/(nopts-1) * alpha for alpha in range(nopts) ] 
ubnds = [ blas.nrm2(b)**2 ]
for alpha in alpha2[1:]:  
    c[1:] = alpha
    ubnds += [ blas.dot(c, solvers.sdp(c, Gs=[G], hs=[-h])['x']) ]

try: import pylab
except ImportError: pass
else:
    pylab.figure(1, facecolor='w')
    pylab.plot(lbnds, alpha1, 'b-', ubnds, alpha2, 'b-')
    kmax = max([ k for k in range(len(alpha1)) if alpha1[k] < 
        blas.nrm2(xls)**2 ])
Пример #59
0
# maximize    w
# subject to  w*I <= V*diag(x)*V' 
#             x >= 0
#             sum(x) = 1

novars = n+1
c = matrix(0.0, (novars,1))
c[-1] = -1.0
Gs = [matrix(0.0, (4,novars))]
for k in range(n):  Gs[0][:,k] = -(V[:,k]*V[:,k].T)[:]
Gs[0][[0,3],-1] = 1.0
hs = [matrix(0.0, (2,2))]
Ge = matrix(0.0, (n, novars))
Ge[:,:n] = G
Ae = matrix(n*[1.0] + [0.0], (1,novars))
sol = solvers.sdp(c, Ge, h, Gs, hs, Ae, b)
xe = sol['x'][:n]
Z = sol['zs'][0]
mu = sol['y'][0]

if pylab_installed:
    pylab.figure(2, facecolor='w', figsize=(6,6)) 
    pylab.plot(V[0,:], V[1,:],'ow', mec = 'k')
    pylab.plot([0], [0], 'k+')
    I = [ k for k in range(n) if xe[k] > 1e-5 ]
    pylab.plot(V[0,I], V[1,I],'or')

# Enclosing ellipse follows from the solution of the dual problem:
#
# minimize    mu
# subject to  diag(V'*Z*V) <= mu*1
Пример #60
0
def qcqprel(P, G, r=0.0 ):
    """
    min x' Q0 x + a0'x
    st
    x Qi x + ai x  + bi <=0
    Input Q size nxn
    a size nx1
    b is scalar
    output c size (n+1)*(n+2)/2 x 1

    x Q x + a'x +  b <= 0
    is converted to
    <q,X> <= 0
    where q is returned by this function 
    """
    def augQ(Q, a=None, b=None):
        n  =Q.size[0]
        if Q is None:
            Q=matrix(0., (n,n))
        if a is None:
            a=matrix(0., (n,1))
        if b is None:
            b=0.
            
        Q0 =matrix([[b, a*0.5], [a.T*0.5, Q]])
        jdx=0
        q=matrix(0., ((n+1)*(n+2)/2, 1) )
        for j in range(n+1):
            idx=matrix(range(j,n+1))
            tmp=Q0[idx, j]
            tmp[1:]*=2
            q[jdx+idx]=tmp
            jdx+=len(idx)-1
        return q

    """
    n is integer, size of matrix we want nnd
    returns the sparse 'sdp' matrix Q that
    sets up the constraint
    Force the matrix
    [ Y ] >= H
    
    where H is some nxn matrix, (not specified here)
    """
    def sdpmat(n=None):
        m=n*(n+1)/2
        Q =matrix(0., (n**2,m))
        J=0
        for j in range(n):
            for k in range(j,n):
                Q[k+j*n, J]=-1.
                J+=1
        Q=sparse(Q)
        return Q


    """
    Get the objective
    """
    n=P['P0'].size[0]
    I=matrix(0., (n,n))
    I[::n+1]=r
    
    Q0 =augQ(P['P0']+I, P['b0'], P['c0'])

    """
    Equality: force the [0,0] entry of solution to have
    value 1.
    """
    A=matrix(0., (1,Q0.size[0]))
    A[0]=1.
    b=matrix(1., (1,1))
    """
    Build the inequalities
    """
    Gl=matrix(0., (0, Q0.size[0]))
    for j in range( len( G['P'] ) ):
        Qk=augQ(G['P'][j], G['b'][j], G['c'][j])
        Gl=matrix([Gl, Qk.T])
    hl=matrix(0., (Gl.size[0], 1))
    """
    Build the equalities
    """
    for j in range( len( G['Peq'] ) ):
        Qk=augQ(G['Peq'][j], G['beq'][j], G['ceq'][j])
        A=matrix([A, Qk.T])
        b=matrix([b, 0.])
    
    """
    Get the 'semidef' matrix
    """
    G0=sdpmat(n+1)
    h0=matrix(0., (n+1,n+1))
    sol=solvers.sdp(Q0, Gl, hl, [G0],[h0], A, b)
    x=sol['x']
    """
    Build the matrices 'x' and 'X'
    """
    if x is not None:
        X=x[1:n+1]
        Y=x[n+1:]
        YY=matrix(0., (n,n))
        J=0
        for j in range(n):
            for k in range(j,n):
                YY[j,k]=Y[J]
                YY[k,j]=Y[J]
                J+=1
        sol['RQCQPx']=+sol['x'][1:n+1]
        sol['RQCQPX']=+YY
    else:
        sol['RQCQPx']=None
        sol['RQCQPX']=None
    return sol