Example #1
0
def create(**kwargs):
    # m>k
    k = kwargs['k']  #class
    m = kwargs['m']  #instance
    n = kwargs['n']  #dim
    p = 5   #p-largest
    q = 10
    X = problem_util.normalized_data_matrix(m,n,1)
    Y = np.random.randint(0, k-1, (q,m))

    Theta = cp.Variable(n,k)
    t = cp.Variable(q)
    texp = cp.Variable(m)
    f = cp.sum_largest(t, p)+cp.sum_entries(texp) + cp.sum_squares(Theta)
    C = []
    C.append(cp.log_sum_exp(X*Theta, axis=1) <= texp)
    for i in range(q):
        Yi = one_hot(Y[i], k)
        C.append(-cp.sum_entries(cp.mul_elemwise(X.T.dot(Yi), Theta)) == t[i])

    t_eval = lambda: np.array([
        -cp.sum_entries(cp.mul_elemwise(X.T.dot(one_hot(Y[i], k)), Theta)).value for i in range(q)])
    f_eval = lambda: cp.sum_largest(t_eval(), p).value \
        + cp.sum_entries(cp.log_sum_exp(X*Theta, axis=1)).value \
        + cp.sum_squares(Theta).value
    
    return cp.Problem(cp.Minimize(f), C), f_val
Example #2
0
def create(**kwargs):
    # m>k
    k = kwargs['k']  #class
    m = kwargs['m']  #instance
    n = kwargs['n']  #dim
    p = 5  #p-largest
    X = problem_util.normalized_data_matrix(m, n, 1)
    Y = np.random.randint(0, k, m)

    Theta = cp.Variable(n, k)
    t = cp.Variable(1)
    texp = cp.Variable(m)
    f = t + cp.sum_largest(texp, p) + cp.sum_squares(Theta)
    C = []
    C.append(cp.log_sum_exp(X * Theta, axis=1) <= texp)
    Yi = one_hot(Y, k)
    C.append(-cp.sum_entries(cp.mul_elemwise(X.T.dot(Yi), Theta)) == t)

    t_eval = lambda: \
        -cp.sum_entries(cp.mul_elemwise(X.T.dot(one_hot(Y, k)), Theta)).value
    f_eval = lambda: t_eval() \
        + cp.sum_largest(cp.log_sum_exp(X*Theta, axis=1), p).value \
        + cp.sum_squares(Theta).value

    return cp.Problem(cp.Minimize(f), C), f_eval
    def test_validation(self):
        """Test that complex arguments are rejected.
        """
        x = Variable(complex=True)
        with self.assertRaises(Exception) as cm:
            (x >= 0)
        self.assertEqual(str(cm.exception),
                         "Inequality constraints cannot be complex.")

        with self.assertRaises(Exception) as cm:
            cvx.quad_over_lin(x, x)
        self.assertEqual(
            str(cm.exception),
            "The second argument to quad_over_lin cannot be complex.")

        with self.assertRaises(Exception) as cm:
            cvx.sum_largest(x, 2)
        self.assertEqual(str(cm.exception),
                         "Arguments to sum_largest cannot be complex.")

        x = Variable(2, complex=True)
        for atom in [
                cvx.geo_mean, cvx.log_sum_exp, cvx.max, cvx.entr, cvx.exp,
                cvx.huber, cvx.log, cvx.log1p, cvx.logistic
        ]:
            name = atom.__name__
            with self.assertRaises(Exception) as cm:
                print(name)
                atom(x)
            self.assertEqual(str(cm.exception),
                             "Arguments to %s cannot be complex." % name)

        x = Variable(2, complex=True)
        for atom in [cvx.maximum, cvx.kl_div]:
            name = atom.__name__
            with self.assertRaises(Exception) as cm:
                print(name)
                atom(x, x)
            self.assertEqual(str(cm.exception),
                             "Arguments to %s cannot be complex." % name)

        x = Variable(2, complex=True)
        for atom in [cvx.inv_pos, cvx.sqrt, lambda x: cvx.power(x, .2)]:
            with self.assertRaises(Exception) as cm:
                atom(x)
            self.assertEqual(str(cm.exception),
                             "Arguments to power cannot be complex.")

        x = Variable(2, complex=True)
        for atom in [cvx.harmonic_mean, lambda x: cvx.pnorm(x, .2)]:
            with self.assertRaises(Exception) as cm:
                atom(x)
            self.assertEqual(str(cm.exception),
                             "pnorm(x, p) cannot have x complex for p < 1.")
Example #4
0
def maxSoftMaxEpigraphProblem(problemOptions, solverOptions):
    k = problemOptions['k']  #class
    m = problemOptions['m'] #instances
    n = problemOptions['n']  #dim
    p = problemOptions['p']   #p-largest
    
    X = __normalized_data_matrix(m,n,1)
    Y = np.random.randint(0, k, m)


    # Problem construction

    def one_hot(y, k):
        m = len(y)
        return sps.coo_matrix((np.ones(m), (np.arange(m), y)), shape=(m, k)).todense()

    Theta = cp.Variable(n,k)
    beta = cp.Variable(1, k)
    t = cp.Variable(m)
    texp = cp.Variable(m)

    f = cp.sum_largest(t+texp, p) + cp.sum_squares(Theta)
    C = []
    C.append(cp.log_sum_exp(X*Theta + np.ones((m, 1))*beta, axis=1) <= texp)
    Yi = one_hot(Y, k)
    C.append(t == cp.vstack([-(X[i]*Theta + beta)[Y[i]] for i in range(m)]))
    prob = cp.Problem(cp.Minimize(f), C)
    
    prob.solve(**solverOptions)
    return {'Problem':prob, 'name':'maxSoftMaxEpigraphProblem'}
Example #5
0
    def test_eigval_atoms(self):
        """Test eigenvalue atoms.
        """
        P = np.arange(9) - 2j*np.arange(9)
        P = np.reshape(P, (3, 3))
        P1 = np.conj(P.T).dot(P)/10 + np.eye(3)*.1
        P2 = np.array([[10, 1j, 0], [-1j, 10, 0], [0, 0, 1]])
        for P in [P1, P2]:
            value = cvx.lambda_max(P).value
            X = Variable(P.shape, complex=True)
            prob = Problem(cvx.Minimize(cvx.lambda_max(X)), [X == P])
            result = prob.solve(solver=cvx.SCS, eps=1e-6)
            self.assertAlmostEqual(result, value, places=2)

            eigs = np.linalg.eigvals(P).real
            value = cvx.sum_largest(eigs, 2).value
            X = Variable(P.shape, complex=True)
            prob = Problem(cvx.Minimize(cvx.lambda_sum_largest(X, 2)), [X == P])
            result = prob.solve(solver=cvx.SCS, eps=1e-8)
            self.assertAlmostEqual(result, value, places=3)
            self.assertItemsAlmostEqual(X.value, P, places=3)

            value = cvx.sum_smallest(eigs, 2).value
            X = Variable(P.shape, complex=True)
            prob = Problem(cvx.Maximize(cvx.lambda_sum_smallest(X, 2)), [X == P])
            result = prob.solve(solver=cvx.SCS, eps=1e-6)
            self.assertAlmostEqual(result, value, places=3)
Example #6
0
    def expr_intersects(self, other, eps=1e3):
        or_vars = Variable(shape=4,
                           boolean=True,
                           name='overlap_or({},{})'.format(
                               self.name, other.name))
        or_vars2 = Variable(shape=4,
                            boolean=True,
                            name='overlap_or({},{})'.format(
                                self.name, other.name))
        j_vars = Variable(2, boolean=True)
        constraints = [
            # overlaps
            # 0 , 0 -> True
            # 0 , 1 -> False
            # 1 , 0 -> False
            # 1 , 1 -> False
            # Does not overlap main
            self.right <= other.x + eps * or_vars[0],
            other.right <= self.x + eps * or_vars[1],
            self.top <= other.y + eps * or_vars[2],
            other.top <= self.y + eps * or_vars[3],
            sum(or_vars) <= 3,

            # --- or ---
            # overlaps main and overlaps cutout
            self.b2.right <= other.x + eps * or_vars2[0],
            other.right <= self.b2.x + eps * or_vars2[1],
            self.b2.top <= other.y + eps * or_vars2[2],
            other.top <= self.b2.y + eps * or_vars2[3],
            sum(or_vars2) >= 2,

            # if above(1) is met sum largest will
            cvx.sum_largest(or_vars, 3) * j_vars
        ]
        return constraints
Example #7
0
def CVX_Slope(X_train, y_train, llambda, k_Slope, solver):
    start_time = time.time()
    N, P = X_train.shape

    beta = cvx.Variable(P)
    beta0 = cvx.Variable()
    loss = cvx.sum(cvx.pos(1 - cvx.multiply(y_train, X_train * beta + beta0)))
    reg1 = cvx.norm(beta, 1)

    #for j in range(min(P-1, K_max)): reg2  += (lambda_arr[j]-lambda_arr[j+1]) * sum_largest(abs(beta), j+1)
    reg2 = cvx.sum_largest(cvx.abs(beta), k_Slope)

    prob = cvx.Problem(cvx.Minimize(loss + llambda * reg1 + llambda * reg2))
    dict_solver = {'gurobi': cvx.GUROBI, 'ecos': cvx.ECOS}

    prob.solve(solver=dict_solver[solver])

    ### Solution
    support = np.where(np.round(beta.value, 6) != 0)[0]
    print '\nLen support ' + solver + ' = ' + str(len(support))

    ### Obj val
    obj_val = prob.value
    print 'Obj value ' + solver + '  = ' + str(obj_val)

    ### Time stops
    time_cvx = time.time() - start_time
    print 'Time CVX ' + solver + ' = ' + str(round(time_cvx, 2))
Example #8
0
    def test_sum_largest(self) -> None:
        """Test sum_largest.
        """
        expr = cp.sum_largest(self.A, 2)

        self.A.value = [[4, 3], [2, 1]]
        self.assertItemsAlmostEqual(expr.grad[self.A].toarray(), [1, 0, 1, 0])

        self.A.value = [[1, 2], [3, 0.5]]
        self.assertItemsAlmostEqual(expr.grad[self.A].toarray(), [0, 1, 1, 0])
Example #9
0
def create(**kwargs):
    m = kwargs["m"]
    n = kwargs["n"]
    k = kwargs["k"]
    A = np.matrix(np.random.rand(m, n))
    A -= np.mean(A, axis=0)
    K = np.array([(A[i].T * A[i]).flatten() for i in xrange(m)])

    sigma = cp.Variable(n, n)
    t = cp.Variable(m)
    tdet = cp.Variable(1)
    f = cp.sum_largest(t + tdet, k)
    z = K * cp.reshape(sigma, n * n, 1)
    C = [-cp.log_det(sigma) <= tdet, t == z]

    det_eval = lambda: -cp.log_det(sigma).value
    z_eval = lambda: (K * cp.reshape(sigma, n * n, 1)).value
    f_eval = lambda: cp.sum_largest(z_eval() + det_eval(), k).value

    return cp.Problem(cp.Minimize(f), C), f_eval
Example #10
0
    def test_sum_largest(self):
        """Test the sum_largest atom and related atoms.
        """
        with self.assertRaises(Exception) as cm:
            cp.sum_largest(self.x, -1)
        self.assertEqual(str(cm.exception),
                         "Second argument must be a positive integer.")

        with self.assertRaises(Exception) as cm:
            cp.lambda_sum_largest(self.x, 2.4)
        self.assertEqual(str(cm.exception),
                         "First argument must be a square matrix.")

        with self.assertRaises(Exception) as cm:
            cp.lambda_sum_largest(Variable((2, 2)), 2.4)
        self.assertEqual(str(cm.exception),
                         "Second argument must be a positive integer.")

        with self.assertRaises(ValueError) as cm:
            cp.lambda_sum_largest([[1, 2], [3, 4]], 2).value
        self.assertEqual(str(cm.exception),
                         "Input matrix was not Hermitian/symmetric.")

        # Test copy with args=None
        atom = cp.sum_largest(self.x, 2)
        copy = atom.copy()
        self.assertTrue(type(copy) is type(atom))
        # A new object is constructed, so copy.args == atom.args but copy.args
        # is not atom.args.
        self.assertEqual(copy.args, atom.args)
        self.assertFalse(copy.args is atom.args)
        self.assertEqual(copy.get_data(), atom.get_data())
        # Test copy with new args
        copy = atom.copy(args=[self.y])
        self.assertTrue(type(copy) is type(atom))
        self.assertTrue(copy.args[0] is self.y)
        self.assertEqual(copy.get_data(), atom.get_data())
        # Test copy with lambda_sum_largest, which is in fact an AddExpression
        atom = cp.lambda_sum_largest(Variable((2, 2)), 2)
        copy = atom.copy()
        self.assertTrue(type(copy) is type(atom))
Example #11
0
def create(**kwargs):
    m = kwargs["m"]
    n = kwargs["n"]
    k = kwargs["k"]
    A = np.matrix(np.random.rand(m,n))
    A -= np.mean(A, axis=0)
    K = np.array([(A[i].T*A[i]).flatten() for i in xrange(m)])

    sigma = cp.Variable(n,n)
    t = cp.Variable(m)
    tdet = cp.Variable(1)
    f = cp.sum_largest(t+tdet, k)
    z = K*cp.reshape(sigma, n*n, 1)
    C = [-cp.log_det(sigma) <= tdet, t == z]

    det_eval = lambda: -cp.log_det(sigma).value
    z_eval = lambda: (K*cp.reshape(sigma, n*n, 1)).value
    f_eval = lambda: cp.sum_largest(z_eval()+det_eval(), k).value


    return cp.Problem(cp.Minimize(f), C), f_eval
Example #12
0
 def test_paper_example_sum_largest(self):
     self.skipTest("Enable test once sum_largest is implemented.")
     x = cvxpy.Variable((4, ), pos=True)
     x0, x1, x2, x3 = (x[0], x[1], x[2], x[3])
     obj = cvxpy.Minimize(
         cvxpy.sum_largest(
             cvxpy.hstack(
                 [3 * x0**0.5 * x1**0.5, x0 * x1 + 0.5 * x1 * x3**3, x2]),
             2))
     constr = [x0 * x1 * x2 >= 16]
     p = cvxpy.Problem(obj, constr)
     # smoke test.
     p.solve(SOLVER, gp=True)
Example #13
0
def maxGaussianEpigraphProblem(problemOptions, solverOptions):
    m = problemOptions['m']
    n = problemOptions['n']
    k = problemOptions['k']
    
    A = np.matrix(np.random.rand(m,n))
    A -= np.mean(A, axis=0)
    K = np.array([(A[i].T*A[i]).flatten() for i in range(m)])

    sigma_inv = cp.Variable(n, n) # Inverse covariance matrix
    obs = cp.vstack([-cp.log_det(sigma_inv) + cp.trace(A[i].T*A[i]*sigma_inv) for i in range(m)])
    f = cp.sum_largest(obs, k)
    prob = cp.Problem(cp.Minimize(f))
    
    prob.solve(**solverOptions)
    return {'Problem':prob, 'name':'maxGaussianEpigraphProblem'}
Example #14
0
def maxSoftMaxProblem(problemOptions, solverOptions):
    k = problemOptions['k']  #class
    m = problemOptions['m'] #instances
    n = problemOptions['n']  #dim
    p = problemOptions['p']   #p-largest
    X = __normalized_data_matrix(m,n,1)
    Y = np.random.randint(0, k, m)

    # Problem construction
    Theta = cp.Variable(n,k)
    beta = cp.Variable(1,k)
    obs = cp.vstack([-(X[i]*Theta + beta)[Y[i]] + cp.log_sum_exp(X[i]*Theta + beta) for i in range(m)])
    prob = cp.Problem(cp.Minimize(cp.sum_largest(obs, p) + cp.sum_squares(Theta)))
    
    prob.solve(**solverOptions)
    return {'Problem':prob, 'name':'maxSoftMaxProblem'}
Example #15
0
def maxGaussianProblem(problemOptions, solverOptions):
    m = problemOptions['m']
    n = problemOptions['n']
    k = problemOptions['k']
    A = np.matrix(np.random.rand(m,n))
    A -= np.mean(A, axis=0)
    K = np.array([(A[i].T*A[i]).flatten() for i in range(m)])

    sigma_inv1 = cp.Variable(n,n) # Inverse covariance matrix
    t = cp.Variable(m)
    tdet = cp.Variable(1)

    f = cp.sum_largest(t+tdet, k)
    z = K*cp.reshape(sigma_inv1, n*n, 1)
    C = [-cp.log_det(sigma_inv1) <= tdet, t == z]
    prob = cp.Problem(cp.Minimize(f), C)
    
    prob.solve(**solverOptions)
    return {'Problem':prob, 'name':'maxGaussianProblem'}
Example #16
0
# Problem construction


def one_hot(y, k):
    m = len(y)
    return sps.coo_matrix((np.ones(m), (np.arange(m), y)),
                          shape=(m, k)).todense()


Theta = cp.Variable(n, k)
beta = cp.Variable(1, k)
t = cp.Variable(m)
texp = cp.Variable(m)

f = cp.sum_largest(t + texp, p) + cp.sum_squares(Theta)
C = []
C.append(cp.log_sum_exp(X * Theta + np.ones((m, 1)) * beta, axis=1) <= texp)
Yi = one_hot(Y, k)
C.append(t == cp.vstack([-(X[i] * Theta + beta)[Y[i]] for i in range(m)]))
prob = cp.Problem(cp.Minimize(f), C)

# Problem collection

# Single problem collection
problemDict = {"problemID": problemID, "problem": prob, "opt_val": opt_val}
problems = [problemDict]

# For debugging individual problems:
if __name__ == "__main__":
Example #17
0
    def test_sum_largest(self):
        self.skipTest("Enable test once sum_largest is implemented.")
        x = cvxpy.Variable((4, ), pos=True)
        obj = cvxpy.Minimize(cvxpy.sum_largest(x, 3))
        constr = [x[0] * x[1] * x[2] * x[3] >= 16]
        dgp = cvxpy.Problem(obj, constr)
        dgp2dcp = cvxpy.reductions.Dgp2Dcp(dgp)
        dcp = dgp2dcp.reduce()
        dcp.solve(SOLVER)
        dgp.unpack(dgp2dcp.retrieve(dcp.solution))
        opt = 6.0
        self.assertAlmostEqual(dgp.value, opt)
        self.assertAlmostEqual((x[0] * x[1] * x[2] * x[3]).value, 16, places=2)
        dgp._clear_solution()
        dgp.solve(SOLVER, gp=True)
        self.assertAlmostEqual(dgp.value, opt)
        self.assertAlmostEqual((x[0] * x[1] * x[2] * x[3]).value, 16, places=2)

        # An unbounded problem.
        x = cvxpy.Variable((4, ), pos=True)
        y = cvxpy.Variable(pos=True)
        obj = cvxpy.Minimize(cvxpy.sum_largest(x, 3) * y)
        constr = [x[0] * x[1] * x[2] * x[3] >= 16]
        dgp = cvxpy.Problem(obj, constr)
        dgp2dcp = cvxpy.reductions.Dgp2Dcp(dgp)
        dcp = dgp2dcp.reduce()
        opt = dcp.solve(SOLVER)
        self.assertEqual(dcp.value, -float("inf"))
        dgp.unpack(dgp2dcp.retrieve(dcp.solution))
        self.assertAlmostEqual(dgp.value, 0.0)
        self.assertAlmostEqual(dgp.status, "unbounded")
        dgp._clear_solution()
        dgp.solve(SOLVER, gp=True)
        self.assertAlmostEqual(dgp.value, 0.0)
        self.assertAlmostEqual(dgp.status, "unbounded")

        # Another unbounded problem.
        x = cvxpy.Variable(2, pos=True)
        obj = cvxpy.Minimize(cvxpy.sum_largest(x, 1))
        dgp = cvxpy.Problem(obj, [])
        dgp2dcp = cvxpy.reductions.Dgp2Dcp(dgp)
        dcp = dgp2dcp.reduce()
        opt = dcp.solve(SOLVER)
        self.assertEqual(dcp.value, -float("inf"))
        dgp.unpack(dgp2dcp.retrieve(dcp.solution))
        self.assertAlmostEqual(dgp.value, 0.0)
        self.assertAlmostEqual(dgp.status, "unbounded")
        dgp._clear_solution()
        dgp.solve(SOLVER, gp=True)
        self.assertAlmostEqual(dgp.value, 0.0)
        self.assertAlmostEqual(dgp.status, "unbounded")

        # Composition with posynomials.
        x = cvxpy.Variable((4, ), pos=True)
        obj = cvxpy.Minimize(
            cvxpy.sum_largest(
                cvxpy.hstack([
                    3 * x[0]**0.5 * x[1]**0.5,
                    x[0] * x[1] + 0.5 * x[1] * x[3]**3, x[2]
                ]), 2))
        constr = [x[0] * x[1] >= 16]
        dgp = cvxpy.Problem(obj, constr)
        dgp2dcp = cvxpy.reductions.Dgp2Dcp(dgp)
        dcp = dgp2dcp.reduce()
        dcp.solve(SOLVER)
        dgp.unpack(dgp2dcp.retrieve(dcp.solution))
        # opt = 3 * sqrt(4) * sqrt(4) + (4 * 4 + 0.5 * 4 * epsilon) = 28
        opt = 28.0
        self.assertAlmostEqual(dgp.value, opt, places=2)
        self.assertAlmostEqual((x[0] * x[1]).value, 16.0, places=2)
        self.assertAlmostEqual(x[3].value, 0.0, places=2)
        dgp._clear_solution()
        dgp.solve(SOLVER, gp=True)
        self.assertAlmostEqual(dgp.value, opt, places=2)
        self.assertAlmostEqual((x[0] * x[1]).value, 16.0, places=2)
        self.assertAlmostEqual(x[3].value, 0.0, places=2)
Example #18
0
 (lambda x: cp.norm(x, 2), tuple(), [v_np], Constant([3])),
 (lambda x: cp.norm(x, "fro"), tuple(), [[[-1, 2],
                                          [3,
                                           -4]]], Constant([5.47722557])),
 (lambda x: cp.norm(x, 1), tuple(), [v_np], Constant([5])),
 (lambda x: cp.norm(x, 1), tuple(), [[[-1, 2], [3, -4]]], Constant([10])),
 (lambda x: cp.norm(x, "inf"), tuple(), [v_np], Constant([2])),
 (lambda x: cp.norm(x, "inf"), tuple(), [[[-1, 2], [3,
                                                    -4]]], Constant([4])),
 (lambda x: cp.norm(x, "nuc"), tuple(), [[[2, 0], [0, 1]]], Constant([3])),
 (lambda x: cp.norm(x, "nuc"), tuple(), [[[3, 4, 5], [6, 7, 8], [9, 10,
                                                                 11]]],
  Constant([23.173260452512931])),
 (lambda x: cp.norm(x, "nuc"), tuple(), [[[3, 4, 5], [6, 7, 8]]],
  Constant([14.618376738088918])),
 (lambda x: cp.sum_largest(cp.abs(x), 3), tuple(), [[1, 2, 3, -4, -5]],
  Constant([5 + 4 + 3])),
 (lambda x: cp.mixed_norm(x, 1, 1), tuple(), [[[1, 2], [3, 4],
                                               [5, 6]]], Constant([21])),
 (lambda x: cp.mixed_norm(x, 1, 1), tuple(), [[[1, 2, 3],
                                               [4, 5, 6]]], Constant([21])),
 # (lambda x: mixed_norm(x, 2, 1), tuple(), [[[3, 1], [4, math.sqrt(3)]]],
 #     Constant([7])),
 (lambda x: cp.mixed_norm(x, 1, 'inf'), tuple(), [[[1, 4],
                                                   [5,
                                                    6]]], Constant([10])),
 (cp.pnorm, tuple(), [[1, 2, 3]], Constant([3.7416573867739413])),
 (lambda x: cp.pnorm(x, 1), tuple(), [[1.1, 2, -3]], Constant([6.1])),
 (lambda x: cp.pnorm(x, 2), tuple(), [[1.1, 2, -3]],
  Constant([3.7696153649941531])),
 (lambda x: cp.pnorm(x, 2, axis=0), (2, ), [[[1, 2], [3, 4]]],
Example #19
0
k = 20  #class
m = 100  #instance
n = 50  #dim
p = 5  #p-largest
X = normalized_data_matrix(m, n, 1)
Y = np.random.randint(0, k, m)

# Problem construction

Theta = cp.Variable(n, k)
beta = cp.Variable(1, k)
obs = cp.vstack([
    -(X[i] * Theta + beta)[Y[i]] + cp.log_sum_exp(X[i] * Theta + beta)
    for i in range(m)
])
prob = cp.Problem(cp.Minimize(cp.sum_largest(obs, p) + cp.sum_squares(Theta)))

# Problem collection

# Single problem collection
problemDict = {"problemID": problemID, "problem": prob, "opt_val": opt_val}
problems = [problemDict]

# For debugging individual problems:
if __name__ == "__main__":

    def printResults(problemID="", problem=None, opt_val=None):
        print(problemID)
        problem.solve()
        print("\tstatus: {}".format(problem.status))
        print("\toptimal value: {}".format(problem.value))
Example #20
0
 prox("NORM_2", lambda: cp.norm2(x)),
 prox("NORM_NUCLEAR", lambda: cp.norm(X, "nuc")),
 prox("SECOND_ORDER_CONE", None, C_soc_scaled),
 prox("SECOND_ORDER_CONE", None, C_soc_scaled_translated),
 prox("SECOND_ORDER_CONE", None, C_soc_translated),
 prox("SECOND_ORDER_CONE", None, lambda: [cp.norm(X, "fro") <= t]),
 prox("SECOND_ORDER_CONE", None, lambda: [cp.norm2(x) <= t]),
 prox("SEMIDEFINITE", None, lambda: [X >> 0]),
 prox("SUM_DEADZONE", f_dead_zone),
 prox("SUM_EXP", lambda: cp.sum_entries(cp.exp(x))),
 prox("SUM_HINGE", f_hinge),
 prox("SUM_HINGE", lambda: cp.sum_entries(cp.max_elemwise(1-x, 0))),
 prox("SUM_HINGE", lambda: cp.sum_entries(cp.max_elemwise(1-x, 0))),
 prox("SUM_INV_POS", lambda: cp.sum_entries(cp.inv_pos(x))),
 prox("SUM_KL_DIV", lambda: cp.sum_entries(cp.kl_div(p1,q1))),
 prox("SUM_LARGEST", lambda: cp.sum_largest(x, 4)),
 prox("SUM_LOGISTIC", lambda: cp.sum_entries(cp.logistic(x))),
 prox("SUM_NEG_ENTR", lambda: cp.sum_entries(-cp.entr(x))),
 prox("SUM_NEG_LOG", lambda: cp.sum_entries(-cp.log(x))),
 prox("SUM_QUANTILE", f_quantile),
 prox("SUM_QUANTILE", f_quantile_elemwise),
 prox("SUM_SQUARE", f_least_squares_matrix),
 prox("SUM_SQUARE", lambda: f_least_squares(20)),
 prox("SUM_SQUARE", lambda: f_least_squares(5)),
 prox("SUM_SQUARE", f_quad_form),
 prox("TOTAL_VARIATION_1D", lambda: cp.tv(x)),
 prox("ZERO", None, C_linear_equality),
 prox("ZERO", None, C_linear_equality_matrix_lhs),
 prox("ZERO", None, C_linear_equality_matrix_rhs),
 prox("ZERO", None, C_linear_equality_multivariate),
 prox("ZERO", None, C_linear_equality_multivariate2),
Example #21
0
 prox("NORM_NUCLEAR", lambda: cp.norm(X, "nuc")),
 #prox("QUAD_OVER_LIN", lambda: cp.quad_over_lin(p, q1)),
 prox("SECOND_ORDER_CONE", None, C_soc_scaled),
 prox("SECOND_ORDER_CONE", None, C_soc_scaled_translated),
 prox("SECOND_ORDER_CONE", None, C_soc_translated),
 prox("SECOND_ORDER_CONE", None, lambda: [cp.norm(X, "fro") <= t]),
 prox("SECOND_ORDER_CONE", None, lambda: [cp.norm2(x) <= t]),
 prox("SEMIDEFINITE", None, lambda: [X >> 0]),
 prox("SUM_DEADZONE", f_dead_zone),
 prox("SUM_EXP", lambda: cp.sum_entries(cp.exp(x))),
 prox("SUM_HINGE", f_hinge),
 prox("SUM_HINGE", lambda: cp.sum_entries(cp.max_elemwise(1-x, 0))),
 prox("SUM_HINGE", lambda: cp.sum_entries(cp.max_elemwise(1-x, 0))),
 prox("SUM_INV_POS", lambda: cp.sum_entries(cp.inv_pos(x))),
 prox("SUM_KL_DIV", lambda: cp.sum_entries(cp.kl_div(p1,q1))),
 prox("SUM_LARGEST", lambda: cp.sum_largest(x, 4)),
 prox("SUM_LOGISTIC", lambda: cp.sum_entries(cp.logistic(x))),
 prox("SUM_NEG_ENTR", lambda: cp.sum_entries(-cp.entr(x))),
 prox("SUM_NEG_LOG", lambda: cp.sum_entries(-cp.log(x))),
 prox("SUM_QUANTILE", f_quantile),
 prox("SUM_QUANTILE", f_quantile_elemwise),
 prox("SUM_SQUARE", f_least_squares_matrix),
 prox("SUM_SQUARE", lambda: f_least_squares(20)),
 prox("SUM_SQUARE", lambda: f_least_squares(5)),
 prox("SUM_SQUARE", f_quad_form),
 prox("TOTAL_VARIATION_1D", lambda: cp.tv(x)),
 prox("ZERO", None, C_linear_equality),
 prox("ZERO", None, C_linear_equality_matrix_lhs),
 prox("ZERO", None, C_linear_equality_matrix_rhs),
 prox("ZERO", None, C_linear_equality_multivariate),
 prox("ZERO", None, C_linear_equality_multivariate2),
Example #22
0
np.random.seed(0)
m = 10
n = 10
k = 3
A = np.matrix(np.random.rand(m,n))
A -= np.mean(A, axis=0)
K = np.array([(A[i].T*A[i]).flatten() for i in range(m)])


# Problem construction

sigma_inv1 = cp.Variable(n,n) # Inverse covariance matrix
t = cp.Variable(m)
tdet = cp.Variable(1)

f = cp.sum_largest(t+tdet, k)
z = K*cp.reshape(sigma_inv1, n*n, 1)
C = [-cp.log_det(sigma_inv1) <= tdet, t == z]
prob = cp.Problem(cp.Minimize(f), C)


# Problem collection

# Single problem collection
problemDict = {
    "problemID" : problemID,
    "problem"   : prob,
    "opt_val"   : opt_val
}
problems = [problemDict]
Example #23
0
np.random.seed(0)
m = 10
n = 10
k = 3
A = np.matrix(np.random.rand(m, n))
A -= np.mean(A, axis=0)
K = np.array([(A[i].T * A[i]).flatten() for i in range(m)])

# Problem construction

sigma_inv = cp.Variable(n, n)  # Inverse covariance matrix
obs = cp.vstack([
    -cp.log_det(sigma_inv) + cp.trace(A[i].T * A[i] * sigma_inv)
    for i in range(m)
])
f = cp.sum_largest(obs, k)
prob = cp.Problem(cp.Minimize(f))

# Problem collection

# Single problem collection
problemDict = {"problemID": problemID, "problem": prob, "opt_val": opt_val}
problems = [problemDict]

# For debugging individual problems:
if __name__ == "__main__":

    def printResults(problemID="", problem=None, opt_val=None):
        print(problemID)
        problem.solve()
        print("\tstatus: {}".format(problem.status))
Example #24
0
    (lambda x: cp.max(x, axis=1), (2,), [[[-5, 2], [-3, 1]]], Constant([-3, 2])),
    (lambda x: cp.norm(x, 2), tuple(), [v_np], Constant([3])),
    (lambda x: cp.norm(x, "fro"), tuple(), [[[-1, 2], [3, -4]]],
     Constant([5.47722557])),
    (lambda x: cp.norm(x, 1), tuple(), [v_np], Constant([5])),
    (lambda x: cp.norm(x, 1), tuple(), [[[-1, 2], [3, -4]]],
     Constant([7])),
    (lambda x: cp.norm(x, "inf"), tuple(), [v_np], Constant([2])),
    (lambda x: cp.norm(x, "inf"), tuple(), [[[-1, 2], [3, -4]]],
     Constant([6])),
    (lambda x: cp.norm(x, "nuc"), tuple(), [[[2, 0], [0, 1]]], Constant([3])),
    (lambda x: cp.norm(x, "nuc"), tuple(), [[[3, 4, 5], [6, 7, 8], [9, 10, 11]]],
     Constant([23.173260452512931])),
    (lambda x: cp.norm(x, "nuc"), tuple(), [[[3, 4, 5], [6, 7, 8]]],
     Constant([14.618376738088918])),
    (lambda x: cp.sum_largest(cp.abs(x), 3), tuple(), [[1, 2, 3, -4, -5]], Constant([5 + 4 + 3])),
    (lambda x: cp.mixed_norm(x, 1, 1), tuple(), [[[1, 2], [3, 4], [5, 6]]],
     Constant([21])),
    (lambda x: cp.mixed_norm(x, 1, 1), tuple(), [[[1, 2, 3], [4, 5, 6]]],
     Constant([21])),
    # (lambda x: mixed_norm(x, 2, 1), tuple(), [[[3, 1], [4, math.sqrt(3)]]],
    #     Constant([7])),
    (lambda x: cp.mixed_norm(x, 1, 'inf'), tuple(), [[[1, 4], [5, 6]]],
     Constant([10])),

    (cp.pnorm, tuple(), [[1, 2, 3]], Constant([3.7416573867739413])),
    (lambda x: cp.pnorm(x, 1), tuple(), [[1.1, 2, -3]], Constant([6.1])),
    (lambda x: cp.pnorm(x, 2), tuple(), [[1.1, 2, -3]], Constant([3.7696153649941531])),
    (lambda x: cp.pnorm(x, 2, axis=0), (2,),
     [[[1, 2], [3, 4]]], Constant([math.sqrt(5), 5.]).T),
    (lambda x: cp.pnorm(x, 2, axis=1), (2,),