示例#1
0
    def solve_sparse(self, y, w, x_mask=None):
        assert y.ndim == 1 and w.ndim == 1 and y.shape == w.shape
        assert w.shape[0] == self.n

        x = cp.Variable(np.count_nonzero(x_mask))
        inv_mask = np.logical_not(x_mask)

        term1 = cp.square(cp.norm(x-y[x_mask]))
        term2 = self.c * cp.norm1(cp.diag(w[x_mask]) * x)

        objective = cp.Minimize(term1 + term2)
        constraints = [cp.quad_form(x, self.B[np.ix_(x_mask, x_mask)]) <= 1]
        problem = cp.Problem(objective, constraints)

        result = problem.solve(solver=cp.SCS)
        if problem.status != cp.OPTIMAL:
            warnings.warn(problem.status)
        if problem.status not in (cp.OPTIMAL, cp.OPTIMAL_INACCURATE,
                                  cp.UNBOUNDED_INACCURATE):
            raise ValueError(problem.status)

        out = np.zeros(self.n)
        x = np.asarray(x.value).flatten()
        out[x_mask] = x
        return out
def solve_sdp(G):
    """ Solves the SDP problem:
    
    maximize    1' * s
    subject to  0 <= s <= 1
                [G, G - diag(s); G - diag(s), G] >= 0
    stolen directly from knockoff R package - solve_sdp.py
    """
    assert G.ndim == 2 and G.shape[0] == G.shape[1]
    p = G.shape[0]
    
    # Define the problem.
    s = cvx.Variable(p)
    objective = cvx.Maximize(cvx.sum_entries(s))
    constraints = [
        0 <= s, s <= 1,
        2*G - cvx.diag(s) == cvx.Semidef(p),
    ]
    prob = cvx.Problem(objective, constraints)
    
    # Solve the problem.
    prob.solve()
    assert prob.status == cvx.OPTIMAL
    
    # Return as array, not as matrix.
    return np.asarray(s.value).flatten()
示例#3
0
 def test_paper_example_eye_minus_inv(self):
     X = cvxpy.Variable((2, 2), pos=True)
     obj = cvxpy.Minimize(cvxpy.trace(cvxpy.eye_minus_inv(X)))
     constr = [cvxpy.geo_mean(cvxpy.diag(X)) == 0.1]
     problem = cvxpy.Problem(obj, constr)
     # smoke test.
     problem.solve(gp=True, solver="SCS")
示例#4
0
def get_nldi_controller_gain(x_in, A, B, G, C, D, Q_sqrt, R_sqrt):
    n = A.shape[1]
    m = B.shape[1]
    w = G.shape[1]
    wq = C.shape[0]
    assert (w <= wq), "wp must equal wq to use this method"
    if w < wq:
        G = np.concatenate([G, np.zeros([G.shape[0], wq - w])], axis=1)
        w = wq
    x = np.expand_dims(x_in, 1)

    S = cp.Variable((n, n), symmetric=True)
    Y = cp.Variable((m, n))
    gam = cp.Variable()
    lam = cp.Variable(w)

    m1 = cp.bmat(((np.expand_dims(np.array([1]), 0), x.T), (x, S)))

    m2 = cp.bmat(
        ((S, Y.T @ R_sqrt, S @ Q_sqrt, S @ C.T + Y.T @ D.T,
          S @ A.T + Y.T @ B.T), (R_sqrt @ Y, gam * np.eye(m), np.zeros(
              (m, n)), np.zeros((m, w)), np.zeros((m, n))),
         (Q_sqrt @ S, np.zeros((n, m)), gam * np.eye(n), np.zeros(
             (n, w)), np.zeros((n, n))), (C @ S + D @ Y, np.zeros(
                 (w, m)), np.zeros((w, n)), cp.diag(lam), np.zeros(
                     (w, n))), (A @ S + B @ Y, np.zeros(
                         (n, m)), np.zeros((n, n)), np.zeros(
                             (n, w)), S - G @ cp.diag(lam) @ G.T)))

    cons = [S >> 0, lam >= 1e-2, m1 >> 0, m2 >> 0]

    try:
        prob = cp.Problem(cp.Minimize(gam), cons)
        prob.solve(solver=cp.MOSEK)
        if prob.status in ["infeasible", "unbounded"]:
            warnings.warn(
                'Infeasible or unbounded SDP for some x. Falling back to K_init for that x.'
            )
            K = np.nan * np.ones((m, n))
        else:
            K = np.linalg.solve(S.value, Y.value.T).T
    except cp.SolverError:
        warnings.warn(
            'Solver error for some x. Falling back to K_init for that x.')
        K = np.nan * np.ones((m, n))

    return K
示例#5
0
    def make_constraints(self, budget):
        constraints = []
        T = self.T
        ram_costs = self.g.cost_ram
        ram_cost_vec = np.asarray([ram_costs[i] for i in range(T)])

        with Timer("Var bounds"):
            constraints.extend([self.R >= 0, self.R <= 1])
            constraints.extend([self.S >= 0, self.S <= 1])
            constraints.extend([self.Free_E >= 0, self.Free_E <= 1])
            constraints.extend([self.U >= 0, self.U <= budget])
            constraints.append(cp.diag(self.R) == 1)
            constraints.append(cp.diag(self.S) == 0)
            constraints.append(cp.upper_tri(self.R) == 0)
            constraints.append(cp.upper_tri(self.S) == 0)

        with Timer("Correctness constraints"):
            # ensure all checkpoints are in memory
            constraints.append(self.S[1:, :] <= self.S[:-1, :] + self.R[:-1, :])

            # ensure all computations are possible
            for (u, v) in self.g.edge_list:
                constraints.append(self.R[:, v] <= self.R[:, u] + self.S[:, u])

        with Timer("Free_E constraints"):
            # Constraint: sum_k Free_{t,i,k} <= 1
            for i in range(T):
                frees = [self.Free_E[:, eidx] for eidx, (j, _) in enumerate(self.g.edge_list) if i == j]
                if frees:
                    constraints.append(cp.sum(frees, axis=0) <= 1)

            # Constraint: Free_{t,i,k} <= 1 - S_{t+1, i}
            for eidx, (i, k) in enumerate(self.g.edge_list):
                constraints.append(self.Free_E[:-1, eidx] + self.S[1:, i] <= 1)

            # Constraint: Free_{t,i,k} <= 1 - R_{t, j}
            for eidx, (i, k) in enumerate(self.g.edge_list):
                for j in self.g.successors(i):
                    if j > k:
                        constraints.append(self.Free_E[:, eidx] + self.R[:, j] <= 1)

        with Timer("U constraints"):
            constraints.append(self.U[:, 0] == self.R[:, 0] * ram_costs[0] + ram_cost_vec @ self.S.T)
            for k in range(T - 1):
                mem_freed = cp.sum([ram_costs[i] * self.Free_E[:, eidx] for (eidx, i) in self.g.predecessors_indexed(k)])
                constraints.append(self.U[:, k + 1] == self.U[:, k] + self.R[:, k + 1] * ram_costs[k + 1] - mem_freed)
        return constraints
示例#6
0
 def test_matrix_constraint(self) -> None:
     X = cp.Variable((2, 2), pos=True)
     a = cp.Parameter(pos=True, value=0.1)
     obj = cp.Minimize(cp.geo_mean(cp.vec(X)))
     constr = [cp.diag(X) == a, cp.hstack([X[0, 1], X[1, 0]]) == 2 * a]
     problem = cp.Problem(obj, constr)
     gradcheck(problem, gp=True)
     perturbcheck(problem, gp=True)
示例#7
0
def solveProblem(Laplacian,numNodes,solver):
    eye = np.ones(numNodes)
    X = cp.Variable((numNodes, numNodes), PSD=True)
    cost = (0.25) * cp.trace(Laplacian @ X)
    constr = [X >> 0, cp.diag(X) == eye]
    prob = cp.Problem(cp.Maximize(cost), constr)
    prob.solve(solver=solver)
    return X,prob.value
示例#8
0
def gram2edm(G, N, mode):
    if mode:
        dG = cvx.vstack( cvx.diag(G) )
        D = cvx.matmul(dG ,np.ones((1,N)))-2*G + cvx.matmul(np.ones((N,1)),dG.T)
    else:
        dG = np.diag(G)[:,None]
        D = dG-2*G+dG.T
    return D
示例#9
0
    def test_diag(self) -> None:
        """Test diag of mat, and of vector.
        """
        X = cvx.Variable((2, 2), complex=True)
        obj = cvx.Maximize(cvx.trace(cvx.real(X)))
        cons = [cvx.diag(X) == 1]
        prob = cvx.Problem(obj, cons)
        result = prob.solve(solver="SCS")
        self.assertAlmostEqual(result, 2)

        x = cvx.Variable(2, complex=True)
        X = cvx.diag(x)
        obj = cvx.Maximize(cvx.trace(cvx.real(X)))
        cons = [cvx.diag(X) == 1]
        prob = cvx.Problem(obj, cons)
        result = prob.solve(solver="SCS")
        self.assertAlmostEqual(result, 2)
示例#10
0
def gram2edm(G, N, mode):#Generating the EDM from the gramian matrix,using the formula
    if mode:
        dG = cvx.vstack( cvx.diag(G) )
        D = cvx.matmul(dG ,np.ones((1,N)))-2*G + cvx.matmul(np.ones((N,1)),dG.T)
    else:
        dG = np.diag(G)[:,None]
        D = dG-2*G+dG.T
    return D
示例#11
0
    def build_domain_constraints(self):
        row_upper = self.row_upper_bounds()

        global_L1_upper_th = self.global_L1_sum_upper_bound()
        row_L1_upper_bounds = self.row_L1_upper_bound()

        An_th_nodiag = (self.An_th_th -
                        np.diag(np.diag(self.An_th_th)))[self.onehop_ixs_th]
        An_nodiag_sort = np.sort(An_th_nodiag, axis=1)

        # assume that for each 2h neighbor we delete the largest entries and keep the diagonals the same
        # this indicates the maximum negative change per row
        max_neg_change_per_row = self.row_max_neg_change(An_nodiag_sort)

        An_sum_lower_per_node_th = np.array(
            [self.row_lower_bound(ix) for ix in range(len(self.twohop_nbs))])

        # assume that we set the globally largest entries to zero and keep everything else fixed.
        # this gives a lower bound on the sum of An_oh_th
        max_neg_change_global = self.max_neg_change_global()

        sum_lower_bound_constraint = cvx.sum(self.epsilon_minus,
                                             axis=1) <= max_neg_change_per_row

        domain_constraints = [
            # basic constraints
            cvx.diag(self.epsilon_minus_var) == 0,
            self.epsilon_minus_var >= 0,
            self.epsilon_plus_var >= 0,

            # only entries with an edge are allowed to be changed.
            cvx.multiply(1 - self.A_mask_th, self.epsilon_plus_var) == 0,
            cvx.multiply(1 - self.A_mask_th, self.epsilon_minus_var) == 0,

            # tie together the row of node i with the matrix variable.
            self.An_i_val == self.An_cvx[self.i_oh, self.onehop_ixs_th],
            # element-wise lower/upper
            self.An_cvx_orig >= self.A_lower_th,
            self.An_cvx_orig <= self.A_upper_th,
            self.epsilon_minus_var <= self.An_th_th,
            self.epsilon_plus_var <= self.A_upper_th - self.An_th_th,
            self.epsilon_plus_var[self.An_th_th.nonzero()] / (self.A_upper_th - self.An_th_th)[self.An_th_th.nonzero()] + \
            self.epsilon_minus_var[self.An_th_th.nonzero()] / (self.An_th_th)[self.An_th_th.nonzero()] <= 1,

            # L1 change per row upper bound
            cvx.sum(cvx.abs(self.An_th_th - self.An_cvx_orig), axis=1) <= row_L1_upper_bounds,
            # row sum upper bound
            cvx.sum(self.An_cvx, axis=1) <= row_upper[self.onehop_ixs_th],
            # negative change upper bound
            sum_lower_bound_constraint,
            cvx.sum(self.An_cvx_orig, axis=1) >= An_sum_lower_per_node_th,

            cvx.sum(cvx.abs(self.An_cvx_orig - self.An_th_th)) <= global_L1_upper_th,

            cvx.sum(self.epsilon_minus_var) <= max_neg_change_global,

        ]
        return domain_constraints
示例#12
0
def HDM_order_sensitivity(param, D, index):
    N = param.N
    d = param.d
    #######################################
    output = HDM_OUT()
    EPS = 0.01
    #######################################
    Gp = cvx.Variable((N, N), PSD=True)
    Gn = cvx.Variable((N, N), PSD=True)
    G = Gp - Gn
    #######################################
    con = []
    con.append(cvx.diag(G) == -1)
    con.append(G <= -1)

    M = np.shape(index)[0]
    for m in range(M):
        i1 = int(index[m, 0])
        i2 = int(index[m, 1])
        i3 = int(index[m, 2])
        i4 = int(index[m, 3])
        if D[i1, i2] < D[i3, i4]:
            con.append(G[i1, i2] - G[i3, i4] >= EPS)
        if D[i1, i2] > D[i3, i4]:
            con.append(G[i3, i4] - G[i1, i2] >= EPS)
    #######################################
    min_dist = 1
    np.fill_diagonal(D, 1000)
    k, l = np.unravel_index((-D).argmax(), D.shape)
    for i in range(N):
        for j in range(i + 1, N):
            if i == k and j == l:
                con.append(G[i, j] == htools._cosh(min_dist))
                continue
            if j == k and i == l:
                con.append(G[i, j] == htools._cosh(min_dist))
                continue
            con.append(G[i, j] <= htools._cosh(min_dist))
    #######################################
    cost = cvx.trace(Gn) + cvx.trace(Gp)
    #######################################
    obj = cvx.Minimize(cost)
    prob = cvx.Problem(obj, con)
    try:
        prob.solve(
            solver=cvx.CVXOPT,
            verbose=False,
            normalize=False,  #max_iter= 500, 
            kktsolver='robust')  #, refinement = 2)
    except Exception as message:
        print(message)
    output.status = str(prob.status)
    if str(prob.status) == 'optimal':
        G = htools.h_rankPrj(G.value, param)
        output.G = G
    else:
        print('fail')
    return output
示例#13
0
    def create_opt_vars(self, large_gamma=False, large_t=False):
        # init gamma from abs(N(0,1))
        if large_gamma:
            gamma = cp.Variable((self.in_numel, self.in_numel), name='gamma')
        else:
            gamma = cp.Variable((self.in_numel,), name='gamma')
        self.constraints.append((gamma >= 0.))

        s = cp.Variable(name='s')

        if large_t:
            lbda_n = sum(self.shapes[1:-1])
            lbda_vec = cp.Variable((lbda_n,), name='lbda_vec')
            lbda_mat = cp.Variable((lbda_n, lbda_n), name='lbda_mat', symmetric=True)
            self.constraints.append((lbda_mat >= 0.))
            lbda_mat_masked = cp.multiply((- np.ones((lbda_n, lbda_n)) + np.eye(lbda_n)), lbda_mat)
            lbda_mat_diag = cp.hstack([- cp.sum(lbda_mat_masked[i, i+1:]) for i in range(lbda_n)])
            lbdas = cp.diag(lbda_vec) + lbda_mat_masked + cp.diag(lbda_mat_diag)
        else:
            lbdas = list()

        nus = list()
        etas = list()

        for i in range(1, len(self.shapes) - 1):
            if not large_t:
                now_lbda = cp.Variable((self.shapes[i],), name=f'lbda_{i}')
            now_nu = cp.Variable((self.shapes[i],), name=f'nu_{i}')
            now_eta = cp.Variable((self.shapes[i],), name=f'eta_{i}')

            if not large_t:
                self.constraints.append((now_lbda >= 0.))
            self.constraints.append((now_nu >= 0.))
            self.constraints.append((now_eta >= 0.))

            if not large_t:
                lbdas.append(now_lbda)
            nus.append(now_nu)
            etas.append(now_eta)

        self.gamma = gamma
        self.lbdas = lbdas
        self.nus = nus
        self.etas = etas
        self.s = s
示例#14
0
 def test_simpler_eye_minus_inv(self):
     X = cvxpy.Variable((2, 2), pos=True)
     obj = cvxpy.Minimize(cvxpy.trace(cvxpy.eye_minus_inv(X)))
     constr = [cvxpy.diag(X) == 0.1,
               cvxpy.hstack([X[0, 1], X[1, 0]]) == 0.1]
     problem = cvxpy.Problem(obj, constr)
     problem.solve(gp=True, solver="ECOS")
     np.testing.assert_almost_equal(X.value, 0.1*np.ones((2, 2)), decimal=3)
     self.assertAlmostEqual(problem.value, 2.25)
示例#15
0
def get_svec_sdp(G, minEV = None):
    p = G.shape[0]
    s = cp.Variable(p)
    objective = cp.Minimize(cp.sum(1 - s))
    sdcon = (2 * G - cp.diag(s) >> 0)
    constraints = [0 <= s, s <= 1, sdcon]
    prob = cp.Problem(objective, constraints)
    prob.solve(solver=cp.CVXOPT)
    return s.value
示例#16
0
def solve_svm_dual(K, y, lbd):
    n = len(K)
    mu, nu = cp.Variable(n), cp.Variable(n)
    constraints = [mu >= 0, nu >= 0, mu + nu == 1 / n]
    objective = cp.Maximize(
        cp.sum(mu) - cp.quad_form(cp.diag(y) @ mu, K) / (4 * lbd))
    prob = cp.Problem(objective, constraints)
    res = prob.solve(verbose=False)
    return mu.value, nu.value, res
示例#17
0
def BoydHeuristic(U, r):
    X = cp.Variable((n, n), symmetric=True)
    v = cp.Variable(m)
    Z = cp.bmat([[X, np.eye(n)], [np.eye(n), U.T @ cp.diag(v) @ U]])
    constraints = [Z >> 0, 0 <= v, v <= 1, sum(v) == r]
    prob = cp.Problem(cp.Minimize(cp.trace(X)), constraints)
    prob.solve(solver=cp.CVXOPT)
    #prob.solve()
    return v.value
示例#18
0
 def solve_with_trick_and_constants(self):
     self.p = self.generate_p()
     x1 = cp.Variable(self.students)
     x2 = cp.Variable(self.students)
     y1 = cp.Variable(self.classes)
     original_func = cp.sum(cp.log(cp.diag(self.p @ self.values)))
     objective_func = cp.sum(cp.log(cp.diag([email protected]))) \
                      - 10e10 * cp.sum(cp.square(self.max_per_student - cp.sum(self.p, axis=1) - x1))\
                      - 10e10 * cp.sum(cp.square(self.max_per_student - cp.sum(self.p, axis=1) + x2))\
                      - 10e10 * cp.sum(cp.square(self.C - cp.sum(self.p, axis=0) - y1))
     prob = cp.Problem(cp.Maximize(objective_func), [
         self.p <= 1, x1 <= self.max_per_student, 0 <= x1,
         x2 <= self.max_per_student, 0 <= x2, y1 <= self.C, 0 <= y1
     ])
     prob.solve(solver="SCS", max_iters=5000)
     # print(self.values.T)
     print(self.p.value)
     print(original_func.value)
    def matchLift(self):
        """
        Apply Match Lift algorithm
        """

        X_in_trimmed = self.trim_X_in()

        if self.m_estimated is None:
            self.m_estimated = self.estimate_m(X_in_trimmed)
        else:
            print(
                'postprocessing.py: Using oracle assignment or fix value for number of faces m'
            )
        print('estimated m: ' + str(self.m_estimated))

        dim = X_in_trimmed.shape[0]
        n = dim
        #cardinality_edges = np.count_nonzero(np.around(X_in_trimmed, decimals=1))    #rounding necessary, if soft matrix is used
        #lambda_regularizer = np.sqrt(cardinality_edges)/(2*n)
        #print('Lambda Regularizer = ' + str(lambda_regularizer))
        lambda_regularizer = 0.5
        ones_matrix = np.ones(X_in_trimmed.shape)
        'prototype for semidefinite constraint'
        prototype_matrix = np.zeros((dim + 1, dim + 1))
        prototype_matrix[0, :] = np.ones(dim + 1)
        prototype_matrix[:, 0] = np.ones(dim + 1)
        prototype_matrix[0, 0] = self.m_estimated

        X = cp.Variable(shape=(dim, dim), symmetric=True)
        prototype_matrix = cp.Variable(shape=(dim + 1, dim + 1))

        'Convex optimization problem'
        objective = cp.Minimize(-cp.trace(cp.matmul(X.T, X_in_trimmed)) +
                                lambda_regularizer *
                                cp.trace(cp.matmul(ones_matrix, X)))

        constraints = [
            prototype_matrix[0, 1:] == 1,
            prototype_matrix[1:, 0] == 1,
            prototype_matrix[0, 0] == self.m_estimated,
            prototype_matrix[1:, 1:] == X,
            prototype_matrix >> 0,
            X >= 0,
            X <= 1,
            cp.diag(X) == [1] * dim,
        ]  # >>0 means positive semidefinite
        prob = cp.Problem(objective, constraints)

        result = prob.solve()
        #print(result)
        #print(X.value)
        print('Found solution via MatchLift is ' + prob.status)

        relation_matrix_after_matchLift = X.value

        return relation_matrix_after_matchLift, self.m_estimated
示例#20
0
    def test_linearize(self) -> None:
        """Test linearize method.
        """
        # Affine.
        expr = (2 * self.x - 5)[0]
        self.x.value = [1, 2]
        lin_expr = linearize(expr)
        self.x.value = [55, 22]
        self.assertAlmostEqual(lin_expr.value, expr.value)
        self.x.value = [-1, -5]
        self.assertAlmostEqual(lin_expr.value, expr.value)

        # Convex.
        expr = self.A**2 + 5

        with self.assertRaises(Exception) as cm:
            linearize(expr)
        self.assertEqual(
            str(cm.exception),
            "Cannot linearize non-affine expression with missing variable values."
        )

        self.A.value = [[1, 2], [3, 4]]
        lin_expr = linearize(expr)
        manual = expr.value + 2 * cp.reshape(
            cp.diag(cp.vec(self.A)).value @ cp.vec(self.A - self.A.value),
            (2, 2))
        self.assertItemsAlmostEqual(lin_expr.value, expr.value)
        self.A.value = [[-5, -5], [8.2, 4.4]]
        assert (lin_expr.value <= expr.value).all()
        self.assertItemsAlmostEqual(lin_expr.value, manual.value)

        # Concave.
        expr = cp.log(self.x) / 2
        self.x.value = [1, 2]
        lin_expr = linearize(expr)
        manual = expr.value + cp.diag(
            0.5 * self.x**-1).value @ (self.x - self.x.value)
        self.assertItemsAlmostEqual(lin_expr.value, expr.value)
        self.x.value = [3, 4.4]
        assert (lin_expr.value >= expr.value).all()
        self.assertItemsAlmostEqual(lin_expr.value, manual.value)
示例#21
0
 def GetClassicalUB(self):
     import cvxpy as cvx
     #solve cvxpy sdp
     n = len(self.Cost_Matrix)
     X = cvx.Variable((n, n), symmetric=True)  #PSD=True
     obj = cvx.Maximize(cvx.sum(cvx.sum(
         self.Cost_Matrix * X)))  #np.trace(np.matmul(self.Cost_Matrix,X))
     cons = [cvx.diag(X) == np.ones(()), X >= np.zeros((n, n))]
     prob = cvx.Problem(obj, cons)
     prob.solve()
     print(prob.value)
示例#22
0
 def solve_with_trick(self):
     self.p = cp.Variable((self.students, self.classes), nonneg=True)
     upper_bound = np.array([[
         1 if self.values.T[i][j] != 0 else 0 for j in range(self.classes)
     ] for i in range(self.students)])
     x1 = cp.Variable(self.students)
     x2 = cp.Variable(self.students)
     y1 = cp.Variable(self.classes)
     original_func = cp.sum(cp.log(cp.diag(self.p @ self.values)))
     objective_func = cp.sum(cp.log(cp.diag([email protected]))) \
                      - 10e10 * cp.sum(cp.square(self.max_per_student - cp.sum(self.p, axis=1) - x1))\
                      - 10e10 * cp.sum(cp.square(self.max_per_student - cp.sum(self.p, axis=1) + x2))\
                      - 10e10 * cp.sum(cp.square(self.C - cp.sum(self.p, axis=0) - y1))
     prob = cp.Problem(cp.Maximize(objective_func), [
         self.p <= upper_bound, x1 <= self.max_per_student, 0 <= x1,
         x2 <= self.max_per_student, 0 <= x2, y1 <= self.C, 0 <= y1
     ])
     prob.solve(solver="SCS", max_iters=5000)
     print(self.p.value)
     print(original_func.value)
示例#23
0
 def relax(self):
     """Convex relaxation.
     """
     constr = super().relax()
     # Ensure it's a tour.
     n = self.shape[0]
     # Diagonal == 0 constraint.
     constr += [cp.diag(self) == 0]
     # Spectral constraint.
     mat_val = np.cos(2 * np.pi / n) * np.eye(n) + 4 * np.ones((n, n)) / n
     return constr + [mat_val >= (self + self.T) / 2]
示例#24
0
文件: tour.py 项目: cvxgrp/ncvx
 def relax(self):
     """Convex relaxation.
     """
     constr = super(Tour, self).relax()
     # Ensure it's a tour.
     n = self.size[0]
     # Diagonal == 0 constraint.
     constr += [cvx.diag(self) == 0]
     # Spectral constraint.
     mat_val = np.cos(2*np.pi/n)*np.eye(n) + 4*np.ones((n,n))/n
     return constr + [mat_val >> (self + self.T)/2]
def gram2edm(G, param, mode):
    N = param.N
    if mode:
        dG = cvx.vstack(cvx.diag(G))
        D = cvx.matmul(dG, np.ones(
            (1, N))) - 2 * G + cvx.matmul(np.ones((N, 1)), dG.T)
    else:
        dG = np.diag(G)[:, None]
        D = dG - 2 * G + dG.T
        D[D <= 0] = 0
    return D
示例#26
0
    def run(self, bl, bu, y0, yp):
        constraints = [self.P >> 0, self.P[0][0] == 1.0]

        p = 1 + self.shapes[0]
        for i in range(1, len(self.shapes) - 1):
            constraints.append(self.P[p:(p + self.shapes[i]), 0] >= 0)
            constraints.append(
                self.P[p:(p + self.shapes[i]),
                       0] >= cp.matmul(self.Ws[i - 1], self.P[
                           (p - self.shapes[i - 1]):p, 0]) + self.bs[i - 1])
            constraints.append(
                cp.diag(self.P)[p:(p + self.shapes[i])] == cp.diag(
                    cp.matmul(
                        self.Ws[i - 1], self.P[(p - self.shapes[i - 1]):p,
                                               p:(p + self.shapes[i])])) +
                cp.multiply(self.bs[i - 1], self.P[p:(p + self.shapes[i]), 0]))
            p += self.shapes[i]

        p = 1
        for i in range(0, len(self.shapes) - 1):
            constraints.append(
                cp.diag(self.P)[p:(p + self.shapes[i])] <= cp.multiply(
                    (bl[i] + bu[i]), self.P[p:(p + self.shapes[i]), 0]) -
                np.multiply(bl[i], bu[i]))
            p += self.shapes[i]

        obj = (self.Ws[-1][yp] - self.Ws[-1][y0]) * self.P[
            -self.shapes[-2]:, 0] + self.bs[-1][yp] - self.bs[-1][y0]

        self.prob = cp.Problem(cp.Maximize(obj), constraints)
        # self.prob.solve(solver=cp.MOSEK, verbose=True, mosek_params={
        #     # 'optimizerMaxTime': self.timeout,
        #     'MSK_DPAR_OPTIMIZER_MAX_TIME': self.timeout,
        #     # 'numThreads': self.threads,
        #     'MSK_IPAR_NUM_THREADS': self.threads,
        #     # 'lowerObjCut': 0.,
        #     'MSK_DPAR_LOWER_OBJ_CUT': 0.,
        # })
        self.prob.solve(solver=cp.SCS, verbose=True, warm_start=True, eps=1e-2)
        print('status:', self.prob.status)
        print('optimal value:', self.prob.value)
示例#27
0
    def cvx_opt(self, cp_p, cp_Tx_real, cp_Tx_imag, cp_Rx_real, cp_Rx_imag, cp_beta_real, cp_beta_imag, cp_mu):
        K, L, Ml, M, N = self.Ksize
        cp_Theta_real = cp.Variable(L*Ml)
        cp_Theta_imag = cp.Variable(L*Ml)


        cp_Rx_Theta_real = [email protected](cp_Theta_real) - [email protected](cp_Theta_imag)
        cp_Rx_Theta_imag = [email protected](cp_Theta_imag) + [email protected](cp_Theta_real)

        cp_h_real = cp_Rx_Theta_real@cp_Tx_real - cp_Rx_Theta_imag@cp_Tx_imag
        cp_h_imag = cp_Rx_Theta_real@cp_Tx_imag + cp_Rx_Theta_imag@cp_Tx_real

        cp_numerator_real = cp.multiply( cp.diag(cp_h_real),  cp.sqrt(2*cp.multiply(cp_p, 1 + cp_mu)) )
        cp_numerator_imag = cp.multiply( cp.diag(cp_h_imag), cp.sqrt(2*cp.multiply(cp_p, 1 + cp_mu)) )
        cp_h_square = cp.square(cp_h_real) + cp.square(cp_h_imag)
        cp_h_gain = cp.multiply(cp_h_square, cp.vstack([cp_p]*K))
        cp_denominator = cp.sum(cp_h_gain, axis=1) + self.sigma2

        cp_beta_square = cp.square(cp_beta_real) + cp.square(cp_beta_imag)
        objective = cp.Maximize( cp_beta_real@cp_numerator_real + cp_beta_imag@cp_numerator_imag - cp_beta_square@cp_denominator )
        constrains = [cp.square(cp_Theta_real) + cp.square(cp_Theta_imag) <= 1]
        prob = cp.Problem(objective, constrains)
        assert prob.is_dcp(dpp=False)
        result = prob.solve()
        theta_out = torch.stack((torch.from_numpy(cp_Theta_real.value.astype(np.float32)), torch.from_numpy(cp_Theta_imag.value.astype(np.float32))), dim = 1).view(L, Ml, 2)
        return theta_out
示例#28
0
    def SolveSDP(self):
        import cvxpy as cvx
        #solve cvxpy sdp
        n = len(self.Cost_Matrix)
        X = cvx.Variable((n, n), symmetric=True)  #PSD=True
        obj = cvx.Maximize(cvx.trace(self.Cost_Matrix * X))
        cons = [cvx.diag(X) == np.ones((n))]
        cons += [X >> np.zeros((n, n))]
        prob = cvx.Problem(obj, cons)
        prob.solve(solver=cvx.SCS, eps=1e-5)

        self.V = np.linalg.cholesky(X.value + 0.001 * np.diag(np.ones(n))).T
        self.Allsteps = -1
示例#29
0
    def test_diag(self):
        """Test the diag atom.
        """
        expr = cp.diag(self.x)
        self.assertEqual(expr.sign, s.UNKNOWN)
        self.assertEqual(expr.curvature, s.AFFINE)
        self.assertEqual(expr.shape, (2, 2))

        expr = cp.diag(self.A)
        self.assertEqual(expr.sign, s.UNKNOWN)
        self.assertEqual(expr.curvature, s.AFFINE)
        self.assertEqual(expr.shape, (2,))

        expr = cp.diag(self.x.T)
        self.assertEqual(expr.sign, s.UNKNOWN)
        self.assertEqual(expr.curvature, s.AFFINE)
        self.assertEqual(expr.shape, (2, 2))

        with self.assertRaises(Exception) as cm:
            cp.diag(self.C)
        self.assertEqual(str(cm.exception),
                         "Argument to diag must be a vector or square matrix.")
示例#30
0
def solve_rand(W):
    #dual of original:
    print("dual of original:")
    dim = W.shape[0]
    v = cp.Variable((dim, 1))
    constraints = [W + cp.diag(v) >> 0]
    prob = cp.Problem(cp.Maximize(-cp.sum(v)), constraints)
    prob.solve()

    lower_bound = 0
    if prob.status not in ["infeasible", "unbounded"]:
        print("Optimal value: %s" % prob.value)
        lower_bound = prob.value

    print("lower_bound: ", lower_bound)

    #dual of relaxed:

    #restrict to PSD for randomized sampling
    #on proper covariance matrix later
    X = cp.Variable((dim, dim), PSD=True)

    constraints = [X >> 0, cp.diag(X) == np.ones((dim, ))]
    prob = cp.Problem(cp.Minimize(cp.trace(cp.matmul(W, X))), constraints)
    prob.solve(solver=cp.SCS, max_iters=4000, eps=1e-11, warm_start=True)

    print("prob.status:", prob.status)

    if prob.status not in ["infeasible", "unbounded"]:
        print("Optimal value: %s" % prob.value)

    ret = prob.variables()[0].value

    K = 100  #number of samples
    xs_approx = np.random.multivariate_normal(np.zeros((dim, )), ret, size=(K))
    xs_approx = np.sign(xs_approx)  #shape: (K,dim)

    return xs_approx
示例#31
0
文件: SVM.py 项目: Sai-Ashish/AI_ML
def convex_solve(X, Y, var):

    R = cp.norm(cp.matmul(X.T, cp.matmul(cp.diag(var), Y)))**2
    R.shape

    P = cp.sum(var)
    Const1 = P - 0.5 * R

    Const2 = [0 <= var, cp.matmul(var.T, Y) == 0]
    obj = cp.Maximize(Const1)
    prob = cp.Problem(obj, Const2)
    prob.solve(verbose=True)

    return var.value
示例#32
0
    def solve_with_trick_and_reduced_variables(self):
        self.p = cp.Variable((self.students, self.max_declared_class),
                             nonneg=True)
        values_without_zero = self.values_without_zero()
        students_per_class = self.generate_constraints()

        x1 = cp.Variable(self.students)
        x2 = cp.Variable(self.students)
        y1 = cp.Variable(self.classes)
        original_func = cp.sum(cp.log(cp.diag(self.p @ values_without_zero)))
        objective_func = cp.sum(cp.log(cp.diag(self.p@values_without_zero))) \
                         - 10e10 * cp.sum(cp.square(self.max_per_student - cp.sum(self.p, axis=1) - x1))\
                         - 10e10 * cp.sum(cp.square(self.max_per_student - cp.sum(self.p, axis=1) + x2))\
                         - 10e10 * cp.sum(cp.square(self.C - students_per_class - y1))
        prob = cp.Problem(cp.Maximize(objective_func), [
            self.p <= 1, x1 <= self.max_per_student, 0 <= x1,
            x2 <= self.max_per_student, 0 <= x2, y1 <= self.C, 0 <= y1
        ])
        prob.solve(solver="SCS", max_iters=5000)
        print(values_without_zero.T)
        print(self.p.shape)
        print(self.p.value)
        print(original_func.value)
示例#33
0
 def test_psd_constraint(self):
     """Test PSD constraint.
     """
     s = cp.Variable((2, 2))
     obj = cp.Maximize(cp.minimum(s[0, 1], 10))
     const = [s >> 0, cp.diag(s) == np.ones(2)]
     prob = cp.Problem(obj, const)
     r = prob.solve(solver=cp.SCS)
     s = s.value
     print(const[0].residual)
     print("value", r)
     print("s", s)
     print("eigs", np.linalg.eig(s + s.T)[0])
     eigs = np.linalg.eig(s + s.T)[0]
     self.assertEqual(np.all(eigs >= 0), True)
示例#34
0
    def solve(self, y, w):
        assert y.ndim == 1 and w.ndim == 1 and y.shape == w.shape
        assert w.shape[0] == self.n

        x = cp.Variable(self.n)

        term1 = x.T*y - self.c*cp.norm1(cp.diag(w) * x)
        constraints = [cp.quad_form(x, self.B) <= 1]
        problem = cp.Problem(cp.Maximize(term1), constraints)

        result = problem.solve(solver=cp.SCS)
        if problem.status != cp.OPTIMAL:
            warnings.warn(problem.status)
        if problem.status not in (cp.OPTIMAL, cp.OPTIMAL_INACCURATE):
            raise ValueError(problem.status)
        return np.asarray(x.value).flatten()
    def train(self, data):
        assert data.is_regression
        y_s, y_true = self.get_predictions(data)
        I = data.is_target & data.is_labeled
        #y_s = y_s[I]
        y_s = data.y[data.is_source]
        y_true = data.true_y[I]

        x_s = data.x[data.is_source]
        x_s = array_functions.append_column(x_s, data.y[data.is_source])
        x_s = array_functions.standardize(x_s)
        x_t = data.x[I]
        x_t = array_functions.append_column(x_t, data.y[I])
        x_t = array_functions.standardize(x_t)
        Wrbf = array_functions.make_rbf(x_t, self.sigma, self.metric, x2=x_s)
        S = array_functions.make_smoothing_matrix(Wrbf)
        w = cvx.Variable(x_s.shape[0])
        constraints = [w >= 0]
        reg = cvx.norm(w)**2
        loss = cvx.sum_entries(
            cvx.power(
                S*cvx.diag(w)*y_s - y_true,2
            )
        )
        obj = cvx.Minimize(loss + self.C*reg)
        prob = cvx.Problem(obj,constraints)
        assert prob.is_dcp()
        try:
            prob.solve()
            #g_value = np.reshape(np.asarray(g.value),n_labeled)
            w_value = w.value
        except:
            k = 0
            #assert prob.status is None
            print 'CVX problem: setting g = ' + str(k)
            print '\tsigma=' + str(self.sigma)
            print '\tC=' + str(self.C)
            w_value = k*np.ones(x_s.shape[0])

        all_data = data.get_transfer_subset(self.configs.labels_to_keep,include_unlabeled=True)
        all_data.instance_weights = np.ones(all_data.n)
        all_data.instance_weights[all_data.is_source] = w.value
        self.instance_weights = all_data.instance_weights
        self.target_learner.train_and_test(all_data)

        self.x = all_data.x[all_data.is_source]
        self.w = all_data.instance_weights[all_data.is_source]
    def test_problem_expdesign(self):

        # test problem needs review
        print 'skipping, needs review'
        return
        
        from cvxpy import (matrix, variable, program, minimize,
                           log, det_rootn, diag, sum, geq, eq, zeros)
        tol_exp = 6
        V = matrix(self.V)
        n = V.shape[1]
        x = variable(n)

        # Use cvxpy to solve the problem above
        p = program(minimize(-log(det_rootn(V*diag(x)*V.T))),
                    [geq(x, 0.0), eq(sum(x), 1.0)])
        p.solve(True)
        np.testing.assert_array_almost_equal(
            x.value, self.xd, tol_exp)
示例#37
0
    def solve_sparse(self, y, w, x_mask):
        assert y.ndim == 1 and w.ndim == 1 and y.shape == w.shape
        assert w.shape[0] == self.n

        x = cp.Variable(np.count_nonzero(x_mask))

        term1 = x.T*y[x_mask] - self.c*cp.norm1(cp.diag(w[x_mask]) * x)
        constraints = [cp.quad_form(x, self.B[np.ix_(x_mask, x_mask)]) <= 1]
        problem = cp.Problem(cp.Maximize(term1), constraints)

        result = problem.solve(solver=cp.SCS)
        if problem.status != cp.OPTIMAL:
            warnings.warn(problem.status)
        if problem.status not in (cp.OPTIMAL, cp.OPTIMAL_INACCURATE):
            raise ValueError(problem.status)

        out = np.zeros(self.n)
        x = np.asarray(x.value).flatten()
        out[x_mask] = x
        return out
示例#38
0
    constraints.append(
    cvx.sum_entries(N[:,j]) == I[j] )
prob = cvx.Problem(obj, constraints)
prob.solve(solver=cvx.SCS)

s1 = cvx.pos(q-cvx.diag(Acontr.T*N*Tcontr));
optimal_net_profit = prob.value
optimal_penalty = p.T*s1
optimal_revenue = optimal_penalty + optimal_net_profit
print "status:", prob.status
print "optimal net profit:", optimal_net_profit
print "optimal penalty:", prob.value
print "optimal revenue:", prob+p.T*prob.value
'''
# Greedy Aproach displaying without contracts
Nignore = cvx.Variable(n,T)
obj2 = cvx.Maximize(cvx.sum_entries(cvx.mul_elemwise(R, Nignore))) #np.reshape(R,n*T).T *Nignore[:,:])
constraints2 = [Nignore >= 0]
for j in range(T):
    constraints2.append(
    cvx.sum_entries(Nignore[:,j]) == I[j] )
prob2 = cvx.Problem(obj2, constraints2)
prob2.solve()

s2 = cvx.pos(q-cvx.diag(Acontr.T*Nignore*Tcontr))
greedy_net_profit = prob2.value - p.T*s2
greedy_penalty =  p.T*s2
print "greedy status:", prob2.status
print "greedy net_profit:", greedy_net_profit.value
print "greedy penalty:", greedy_penalty.value
print "greedy revenue:", prob2.value
示例#39
0
文件: 123.py 项目: Lolik111/MyRepo
n = 20
N = 1000

C = np.random.random_sample((n, n))
C = - C.dot(C.T) + 1000 * np.eye(n)
# print(np.linalg.eigvals(C))

import cvxpy as cp

mX = cp.Symmetric(n, n)
constraints = [cp.lambda_min(mX) >= 0, cp.diag(mX) == 1]

obj = cp.Maximize(cp.trace(C * mX))
maxcut = cp.Problem(obj, constraints).solve()

print(maxcut)
# print(mX.value)

data = np.zeros(N)
for i in range(N):
    X = np.matrix(np.random.randint(0, 2, n) * 2 - 1).T
    data[i] = X.T.dot(C).dot(X)[0][0]

fig = plt.figure(figsize=[10, 5])

plt.plot(data, 'bo', markersize=1)
plt.plot([maxcut], 'ro')
plt.plot([maxcut for i in range(N)], 'ro', markersize=0.2)


plt.show()
示例#40
0
import cvxpy as cp
import numpy as np

I = np.random.randn(10,10) > .4
I[np.diag_indices_from(I)] = 0
K = np.shape(I)[0]

X = cp.variable(K,K, name='X')
const = []
for i in range(K):
	for j in range(K):
		if I[i,j] > 0:
			c = cp.equals(X[i,j],0)
			const.append(c)
c = cp.equals(cp.diag(X),1)
const.append(c)

p = cp.program(cp.minimize(cp.nuclear_norm(X)), const)
p.solve(quiet=False)
print X.value
示例#41
0
tau_1 = cvx.Variable(N)
#tau_2 = cvx.Variable(N)
tau_2 = np.zeros(N)
print tau_2, b
print b.shape, tau_2.shape
#inv_alpha_1 = cvx.Variable(1)
inv_alpha_1 = 0.5

# specify objective function
#obj = cvx.Minimize(-cvx.sum_entries(cvx.log(tau_1)) - cvx.log_det(A - cvx.diag(tau_1)))
#obj = cvx.Minimize(-cvx.sum_entries(cvx.log(tau_1)) - cvx.log_det(A - cvx.diag(tau_1)))
# original
#obj = cvx.Minimize( 0.5*N*(inv_alpha_1-1)*log_2_pi - 0.5*inv_alpha_1*(N*cvx.log(1/inv_alpha_1) + cvx.sum_entries(cvx.log(tau_1))) + 0.5*cvx.sum_entries(cvx.square(tau_2)/tau_1) + inv_alpha_1*cvx.sum_entries(log_normcdf(tau_2*cvx.sqrt(1/(inv_alpha_1*tau_1)))) +0.5*N*(1-inv_alpha_1)*cvx.log(1-inv_alpha_1) -0.5*(1-inv_alpha_1)*cvx.log_det(A-cvx.diag(tau_1)) + 0.5*cvx.matrix_frac(b-tau_2, A-cvx.diag(tau_1)) )
# modifications
#obj = cvx.Minimize( 0.5*N*(inv_alpha_1-1)*log_2_pi - 0.5*inv_alpha_1*(-N*cvx.log(inv_alpha_1) + cvx.sum_entries(cvx.log(tau_1))) + 0.5*cvx.matrix_frac(tau_2, cvx.diag(tau_1)) + inv_alpha_1*cvx.sum_entries(log_normcdf(tau_2.T*cvx.inv_pos(cvx.sqrt(inv_alpha_1*tau_1)))) +0.5*N*(1-inv_alpha_1)*cvx.log(1-inv_alpha_1) -0.5*(1-inv_alpha_1)*cvx.log_det(A-cvx.diag(tau_1)) + 0.5*cvx.matrix_frac(b-tau_2, A-cvx.diag(tau_1)) )
obj = cvx.Minimize( 0.5*N*(inv_alpha_1-1)*log_2_pi - 0.5*inv_alpha_1*(-N*cvx.log(inv_alpha_1) + cvx.sum_entries(cvx.log(tau_1))) + 0.5*cvx.matrix_frac(tau_2, cvx.diag(tau_1)) + cvx.sum_entries(inv_alpha_1*log_normcdf(cvx.inv_pos(cvx.sqrt(inv_alpha_1*tau_1)))) )# +0.5*N*(1-inv_alpha_1)*cvx.log(1-inv_alpha_1) -0.5*(1-inv_alpha_1)*cvx.log_det(A-cvx.diag(tau_1)) + 0.5*cvx.matrix_frac(b-tau_2, A-cvx.diag(tau_1)) )


#def upper_bound_logpartition(tau, inv_alpha_1):
#    tau_1, tau_2 = tau[:D+N], tau[D+N:]
#    tau_1_N, tau_2_N = tau_1[D:], tau_2[D:]     # first D values correspond to w
#    alpha_1 = 1.0 / inv_alpha_1
#    inv_alpha_2 = 1 - inv_alpha_1
#    if np.any(tau_1 <= 0):
#        integral_1 = INF2
#    else:
#        integral_1 = inv_alpha_1 * (-0.5 * ((D+N)*np.log(alpha_1) + np.sum(np.log(tau_1)) ) \
#                        + np.sum(norm.logcdf(np.sqrt(alpha_1)*tau_2_N/np.sqrt(tau_1_N)))) \
#                        + 0.5 * np.sum(np.power(tau_2, 2) / tau_1)
#    mat = A - np.diag(tau_1)
#    sign, logdet = np.linalg.slogdet(mat)