Пример #1
0
def create(m, n):
    mu = 1
    rho = 1
    sigma = 0.1

    A = problem_util.normalized_data_matrix(m, n, mu)
    x0 = sp.rand(n, 1, rho)
    x0.data = np.random.randn(x0.nnz)
    x0 = x0.toarray().ravel()

    b = np.sign(A.dot(x0) + sigma * np.random.randn(m))
    A[b > 0, :] += 0.7 * np.tile([x0], (np.sum(b > 0), 1))
    A[b < 0, :] -= 0.7 * np.tile([x0], (np.sum(b < 0), 1))

    P = la.block_diag(np.random.randn(n - 1, n - 1), 0)

    lam = 1
    x = cp.Variable(A.shape[1])

    # Straightforward formulation w/ no constraints
    # TODO(mwytock): Fix compiler so this works
    z0 = 1 - sp.diags([b], [0]) * A * x + cp.norm1(P.T * x)
    f_eval = lambda: (lam * cp.sum_squares(x) + cp.sum_entries(
        cp.max_elemwise(z0, 0))).value

    # Explicit epigraph constraint
    t = cp.Variable(1)
    z = 1 - sp.diags([b], [0]) * A * x + t
    f = lam * cp.sum_squares(x) + cp.sum_entries(cp.max_elemwise(z, 0))
    C = [cp.norm1(P.T * x) <= t]
    return cp.Problem(cp.Minimize(f), C), f_eval
Пример #2
0
def create(m, n):
    mu = 1
    rho = 1
    sigma = 0.1

    A = problem_util.normalized_data_matrix(m, n, mu)
    x0 = sp.rand(n, 1, rho)
    x0.data = np.random.randn(x0.nnz)
    x0 = x0.toarray().ravel()

    b = np.sign(A.dot(x0) + sigma*np.random.randn(m))
    A[b>0,:] += 0.7*np.tile([x0], (np.sum(b>0),1))
    A[b<0,:] -= 0.7*np.tile([x0], (np.sum(b<0),1))

    P = la.block_diag(np.random.randn(n-1,n-1), 0)

    lam = 1
    x = cp.Variable(A.shape[1])

    # Straightforward formulation w/ no constraints
    # TODO(mwytock): Fix compiler so this works
    z0 = 1 - sp.diags([b],[0])*A*x + cp.norm1(P.T*x)
    f_eval = lambda: (lam*cp.sum_squares(x) + cp.sum_entries(cp.max_elemwise(z0, 0))).value

    # Explicit epigraph constraint
    t = cp.Variable(1)
    z = 1 - sp.diags([b],[0])*A*x + t
    f = lam*cp.sum_squares(x) + cp.sum_entries(cp.max_elemwise(z, 0))
    C = [cp.norm1(P.T*x) <= t]
    return cp.Problem(cp.Minimize(f), C), f_eval
Пример #3
0
def oneClassSVMProblem(problemOptions, solverOptions):
    n = problemOptions['n']
    m = problemOptions['m']

    # Generate random points uniform over hypersphere
    A = np.random.randn(m, n)
    A /= np.sqrt(np.sum(A**2, axis=1))[:,np.newaxis] # Normalize each row of A
    A *= (np.random.rand(m)**(1./n))[:,np.newaxis]

    # Shift points and add some outliers
    # NOTE(mwytock): causes problems for operator splitting, should fix
    #x0 = np.random.randn(n)
    x0 = np.zeros(n)
    A += x0

    k = max(m//50, 1)
    idx = np.random.randint(0, m, k)
    A[idx, :] += np.random.randn(k, n)
    lam = 1


    # Problem construction

    x = cp.Variable(n)
    rho = cp.Variable(1)

    # Straightforward expression
    z = np.sum(A**2, axis=1) - 2*A*x + cp.sum_squares(x)  # z_i = ||a_i - x||^2
    f = cp.sum_entries(cp.max_elemwise(z - rho, 0)) + lam*cp.sum_entries(cp.max_elemwise(0, rho))
    prob = cp.Problem(cp.Minimize(f))
                                  
    prob.solve(**solverOptions)
    return {'Problem':prob, 'name':'oneClassSVMProblem'}
Пример #4
0
def create(m, n):
    # Generate random points uniform over hypersphere
    A = np.random.randn(m, n)
    A /= np.sqrt(np.sum(A**2, axis=1))[:,np.newaxis]
    A *= (np.random.rand(m)**(1./n))[:,np.newaxis]

    # Shift points and add some outliers
    # NOTE(mwytock): causes problems for operator splitting, should fix
    #x0 = np.random.randn(n)
    x0 = np.zeros(n)
    A += x0

    k = max(m/50, 1)
    idx = np.random.randint(0, m, k)
    A[idx, :] += np.random.randn(k, n)
    lam = 1
    x = cp.Variable(n)
    rho = cp.Variable(1)

    # Straightforward expression
    #z = np.sum(A**2, axis=1) - 2*A*x + cp.sum_squares(x)  # z_i = ||a_i - x||^2
    #f = cp.sum_entries(cp.max_elemwise(z - rho, 0)) + lam*cp.max_elemwise(0, rho)

    # Explicit epigraph form
    t = cp.Variable(1)
    z = np.sum(A**2, axis=1) - 2*A*x + t  # z_i = ||a_i - x||^2
    z0 = np.sum(A**2, axis=1) - 2*A*x + cp.sum_squares(x)
    f_eval = lambda: ((1./n)*cp.sum_entries(cp.max_elemwise(z0 - rho, 0)) + cp.max_elemwise(0, rho)).value
    f = (1./n)*cp.sum_entries(cp.max_elemwise(z-rho, 0)) + lam*cp.sum_entries(cp.max_elemwise(rho, 0))
    C = [cp.sum_squares(x) <= t]

    return cp.Problem(cp.Minimize(f), C), f_eval
Пример #5
0
def infinite_push(theta, Xp, Xn):
    m, d = Xp.shape
    n = Xn.shape[0]
    Z = cp.max_elemwise(
        1 - (Xp * theta * np.ones((1, n)) - (Xn * theta * np.ones((1, m))).T),
        0)
    return cp.max_entries(cp.sum_entries(Z, axis=0))
Пример #6
0
def create(**kwargs):
    n = kwargs["n"]

    x = cp.Variable(n)
    u = np.random.rand(n)
    f =  cp.sum_squares(x-u)+cp.sum_entries(cp.max_elemwise(x, 0))
    return cp.Problem(cp.Minimize(f))
Пример #7
0
    def cost(self):
        noload = cvx.Constant(self.no_load_cost)
        if len(self.bid_curve) == 0: return noload
        p = -self.terminals[0].power_var
        offset = self.no_load_cost
        segments = [offset + p * self.bid_curve[0][1]]
        if len(self.bid_curve) == 1:
            return segments[0]
        offset += self.bid_curve[0][1] * self.bid_curve[0][0]
        for i in range(len(self.bid_curve) - 1):
            b = self.bid_curve[i + 1][1]
            segments.append(b * (p - self.bid_curve[i][0]) + offset)
            offset += b * (self.bid_curve[i + 1][0] - self.bid_curve[i][0])
        return cvx.max_elemwise(*segments)


#gen = GeneratorWithBidCurve(
#    no_load_cost=290.42,
#    bid_curve=[
#        (30, 21.21),
#        (33.1, 21.43),
#        (36.2, 21.66),
#        (39.4, 21.93),
#        (42.5, 22.22),
#        (45.6, 22.53),
#        (48.8, 22.87),
#        (51.9, 23.22),
#        (55, 23.6)])
#load = FixedLoad(power=43)
#net = Net([gen.terminals[0], load.terminals[0]])

#network = Group([gen, load], [net])
#print(network.optimize())
Пример #8
0
def envelope_fit(signal, mu, eta, kind='upper', period=None):
    '''
    Perform an envelope fit of a signal. See: https://en.wikipedia.org/wiki/Envelope_(waves)
    :param signal: The signal to be fit
    :param mu: A parameter to control the overall stiffness of the fit
    :param eta: A parameter to control the permeability of the envelope. A large value result in
    no data points outside the fitted envelope
    :param kind: 'upper' or 'lower'
    :return: An envelope signal of the same length as the input
    '''
    if kind == 'lower':
        signal *= -1
    n_samples = len(signal)
    envelope = cvx.Variable(len(signal))
    mu = cvx.Parameter(sign='positive', value=mu)
    eta = cvx.Parameter(sign='positive', value=eta)
    cost = (cvx.sum_entries(cvx.huber(envelope - signal)) +
            mu * cvx.norm2(envelope[2:] - 2 * envelope[1:-1] + envelope[:-2]) +
            eta * cvx.norm1(cvx.max_elemwise(signal - envelope, 0)))
    objective = cvx.Minimize(cost)
    if period is not None:
        constraints = [envelope[:n_samples - period] == envelope[period:]]
    problem = cvx.Problem(objective, constraints)
    try:
        problem.solve(solver='MOSEK')
    except Exception as e:
        print(e)
        print('Trying ECOS solver')
        problem.solve(solver='ECOS')
    if kind == 'upper':
        return envelope.value.A1
    elif kind == 'lower':
        signal *= -1
        return -envelope.value.A1
Пример #9
0
def robustSVMProblem(problemOptions, solverOptions):
    m = problemOptions['m']
    n = problemOptions['n']
    mu = problemOptions['mu']
    rho = problemOptions['rho']
    sigma = problemOptions['sigma']
    gamma = problemOptions['gamma']
    lam = problemOptions['lam']

    A = __normalized_data_matrix(m, n, mu)
    x0 = sps.rand(n, 1, rho)
    x0.data = np.random.randn(x0.nnz)
    x0 = x0.toarray().ravel()

    b = np.sign(A.dot(x0) + sigma*np.random.randn(m))
    A[b>0,:] += gamma*np.tile([x0], (np.sum(b>0),1))
    A[b<0,:] -= gamma*np.tile([x0], (np.sum(b<0),1))

    P = la.block_diag(np.random.randn(n-1,n-1), 0)

    x = cp.Variable(n)
    z = 1 - sps.diags([b],[0])*A*x + cp.norm1(P.T*x) 
    f = lam*cp.sum_squares(x) + cp.sum_entries(cp.max_elemwise(z, 0))
    prob = cp.Problem(cp.Minimize(f))
    
    prob.solve(**solverOptions)
    return {'Problem':prob, 'name':'robustSVMProblem'}
Пример #10
0
def f_quantile_elemwise():
    m = 4
    k = 2
    alphas = rand(k)
    A = np.tile(alphas, (m, 1))
    X = cp.Variable(m, k)
    return cp.sum_entries(
        cp.max_elemwise(cp.mul_elemwise(-A, X), cp.mul_elemwise(1 - A, X)))
Пример #11
0
def f_quantile_elemwise():
    m = 4
    k = 2
    alphas = rand(k)
    A = np.tile(alphas, (m, 1))
    X = cp.Variable(m, k)
    return cp.sum_entries(cp.max_elemwise(
        cp.mul_elemwise( -A, X),
        cp.mul_elemwise(1-A, X)))
Пример #12
0
def quantile_loss(alphas, Theta, X, y):
    m, n = X.shape
    k = len(alphas)
    Y = np.tile(y, (k, 1)).T
    A = np.tile(alphas, (m, 1))
    Z = X*Theta - Y
    return cp.sum_entries(
        cp.max_elemwise(
            cp.mul_elemwise( -A, Z),
            cp.mul_elemwise(1-A, Z)))
Пример #13
0
def quantile_loss(alphas, Theta, X, y):
    m, n = X.shape
    k = len(alphas)
    Y = np.tile(y.flatten(), (k, 1)).T
    A = np.tile(alphas, (m, 1))
    Z = X*Theta - Y
    return cp.sum_entries(
        cp.max_elemwise(
            cp.mul_elemwise( -A, Z),
            cp.mul_elemwise(1-A, Z)))
Пример #14
0
 def cvx_loss_piecewise(self, d, a, b):
     a_is_none = a is None
     b_is_none = b is None
     assert a_is_none or b_is_none
     assert not (
         a_is_none and b_is_none
     ), 'Constraint with both upper and lower bounds not implemented yet'
     if not a_is_none:
         v = [0, d]
     elif not b_is_none:
         v = [0, d]
     return cvx.max_elemwise(*v)
Пример #15
0
def chebyshevProblem(problemOptions, solverOptions):
    n = problemOptions['n']
    m = problemOptions['m']
    k = problemOptions['k']
    A = [__normalized_data_matrix(m,n,1) for i in range(k)]
    B = __normalized_data_matrix(k,n,1)
    c = np.random.rand(k)
    # Problem construction
    x = cp.Variable(n)
    obj_list = [cp.pnorm(A[i]*x, 2) + cp.abs(B[i,:]*x - c[i]) for i in range(k)]
    f = cp.max_elemwise(obj_list)
    prob = cp.Problem(cp.Minimize(f))
    prob.solve(**solverOptions)
    return {'Problem':prob, 'name':'chebyshevProblem'}
    def cost(self):
        p = -self.terminals[0].power_var

        segments = [cvx.Constant(self.no_load_cost)]
        prev_power = None
        for power, price in self.bid_curve[1:]:
            if prev_power is None:
                offset = self.no_load_cost
            else:
                offset += (power - prev_power) * prev_price
            segments.append(price * (p - power) + offset)
            prev_power = power
            prev_price = price

        return cvx.max_elemwise(*segments)
Пример #17
0
 def generate_cvx(cls, constraints, f, transform=None, scale=1.0):
     x = np.zeros((len(constraints), constraints[0].x[0].size))
     b = np.zeros(len(constraints))
     for i, c in enumerate(constraints):
         x_curr = c.x[0]
         if transform is not None:
             x_curr = transform.transform(x_curr)
         x[i, :] = x_curr
         b[i] = c.c[0]
         if c.bound_type == BoundConstraint.BOUND_LOWER:
             x[i, :] *= -1
             b[i] *= -1
     d = f(x) - b
     #d = np.asarray(d)
     #z = np.zeros((len(constraints)))
     vals = cvx.max_elemwise(d, 0)
     return cvx.sum_entries(vals)
Пример #18
0
def envelope_fit_with_deg(signal, period, mu, eta, kind='upper', eps=1e-4):
    '''
    Perform an approximately periodic envelope fit of a signal.
    See: https://en.wikipedia.org/wiki/Envelope_(waves)
    :param signal: The signal to be fit
    :param mu: A parameter to control the overall stiffness of the fit
    :param eta: A parameter to control the permeability of the envelope. A large value result in
    no data points outside the fitted envelope
    :param kind: 'upper' or 'lower'
    :return: An envelope signal of the same length as the input
    '''
    offset_eps = False
    if kind == 'lower':
        signal *= -1
    if np.min(signal) < 0:
        print('function only works for signals with all values >= 0')
    elif np.min(signal) == 0:
        offset_eps = True
        signal += eps
    n_samples = len(signal)
    beta = cvx.Variable()
    envelope = cvx.Variable(len(signal))
    mu = cvx.Parameter(sign='positive', value=mu)
    eta = cvx.Parameter(sign='positive', value=eta)
    cost = (cvx.sum_entries(cvx.huber(envelope - np.log(signal))) +
            mu * cvx.norm2(envelope[2:] - 2 * envelope[1:-1] + envelope[:-2]) +
            eta * cvx.norm1(cvx.max_elemwise(np.log(signal) - envelope, 0)))
    objective = cvx.Minimize(cost)
    constraints = [envelope[:n_samples - period] == envelope[period:] + beta]
    problem = cvx.Problem(objective, constraints)
    try:
        problem.solve(solver='MOSEK')
        #problem.solve()
    except Exception as e:
        print(e)
        print('Trying ECOS solver')
        problem.solve(solver='ECOS')
    if offset_eps:
        signal -= eps
    if kind == 'upper':
        return np.exp(envelope.value.A1), np.exp(beta.value)
    elif kind == 'lower':
        signal *= -1
        return -np.exp(envelope.value.A1), np.exp(beta.value)
Пример #19
0
 def to_cvx_dccp(constraints, f, logistic=False):
     objective = 0
     n = len(constraints)
     t = cvx.Variable(n)
     t_constraints = [0] * n
     #d_far - d_close
     for i, c in enumerate(constraints):
         d_close, d_far = c.get_convex_terms(f)
         '''
         t_constraints[i] = t[i] == d_close
         assert False, 'Is this correct?'
         objective += cvx.max_elemwise(d_far - t[i], 0)
         '''
         t_constraints[i] = t[i] == d_far
         if logistic:
             objective += cvx.logistic(d_close - t[i])
         else:
             objective += cvx.max_elemwise(d_close - t[i], 0)
     return objective, t, t_constraints
Пример #20
0
    def cost(self):
        if len(self.bid_curve) == 0: return cvx.Constant(0)
        p = self.terminals[0].power_var
        segments = [p * -self.bid_curve[0][1]]
        if len(self.bid_curve) == 1:
            return segments[0]
        offset = -self.bid_curve[0][1] * self.bid_curve[0][0]
        for i in range(len(self.bid_curve) - 1):
            b = -self.bid_curve[i + 1][1]
            segments.append(b * (p - self.bid_curve[i][0]) + offset)
            offset += b * (self.bid_curve[i + 1][0] - self.bid_curve[i][0])
        return cvx.max_elemwise(*segments)


#gen = GeneratorWithBidCurve(
#    no_load_cost=290.42,
#    bid_curve=[
#        (30, 21.21),
#        (33.1, 21.43),
#        (36.2, 21.66),
#        (39.4, 21.93),
#        (42.5, 22.22),
#        (45.6, 22.53),
#        (48.8, 22.87),
#        (51.9, 23.22),
#        (55, 23.6)])
#fload = FixedLoad(32)
#load = LoadWithBidCurve(
#    bid_curve=[
#        (1, 33),
#        (2, 32),
#        (3, 30),
#        (4, 28),
#        (5, 25),
#        (6, 24),
#        (7, 23),
#        (8, 22),
#        (10, 20.9)], power_max=10)
#net = Net([gen.terminals[0], load.terminals[0], fload.terminals[0]])

#network = Group([gen, load, fload], [net])

#print(network.optimize())
Пример #21
0
def SVM_soft_L1(samples, labels, C):
    """
    Input:
    sample: np array in the format [[x1],[x2],....,[xn]] xi as col vectors
    labels: np array [y1,y2,....,yn]
    """
    t_in = time.time()
    dim, Nsamples = samples.shape
    theta = cvx.Variable(dim)  # declaring dimension of variable
    lam = 1 / C
    Jump_term = cvx.max_elemwise(
        0, 1 - cvx.mul_elemwise(labels, (theta.T @ samples).T))
    Jump_term = cvx.sum_entries(Jump_term)
    obj_expr = Jump_term + (lam / 2) * (cvx.sum_squares(theta[:-1]))
    obj = cvx.Minimize(obj_expr)
    prob = cvx.Problem(obj)
    prob.solve(solver=cvx.ECOS)
    theta_star = theta.value
    theta_star = theta_star.tolist()
    theta_star = [x[0] for x in theta_star]
    print('SVM soft time ' + str(time.time() - t_in) + '\n')
    return (theta_star)
Пример #22
0
def hinge_loss(theta, X, y):
    if not all(np.unique(y) == [-1, 1]):
        raise ValueError("y must have binary labels in {-1,1}")
    return cp.sum_entries(
        cp.max_elemwise(1 - sps.diags([y], [0]) * X * theta, 0))
Пример #23
0
def f_hinge():
    return cp.sum_entries(cp.max_elemwise(x,0))
Пример #24
0
x0 = x0.toarray().ravel()

b = np.sign(A.dot(x0) + sigma * np.random.randn(m))
A[b > 0, :] += 0.7 * np.tile([x0], (np.sum(b > 0), 1))
A[b < 0, :] -= 0.7 * np.tile([x0], (np.sum(b < 0), 1))

P = la.block_diag(np.random.randn(n - 1, n - 1), 0)

lam = 1

# Problem construction

# Unconstrained formulation
x = cp.Variable(n)
z = 1 - sps.diags([b], [0]) * A * x + cp.norm1(P.T * x)
f = lam * cp.sum_squares(x) + cp.sum_entries(cp.max_elemwise(z, 0))
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))
Пример #25
0
 prox("NON_NEGATIVE", None, lambda: [x >= 0]),
 prox("NORM_1", f_norm1_weighted),
 prox("NORM_1", lambda: cp.norm1(x)),
 prox("NORM_2", lambda: cp.norm(X, "fro")),
 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),
Пример #26
0
def hinge(x):
    return cp.sum_entries(cp.max_elemwise(x,0))
Пример #27
0
def f_hinge_axis1():
    return cp.sum_entries(cp.max_elemwise(X,0), axis=1)
Пример #28
0
def f_dead_zone():
    eps = np.abs(randn())
    return cp.sum_entries(cp.max_elemwise(cp.abs(x)-eps, 0))
Пример #29
0
import cvxpy as cp

x1 = cp.Variable(1)
x2 = cp.Variable(1)

constraints = [2*x1+x2 >= 1, x1+3*x2 >= 1, x1 >= 0, x2 >= 0]

objectives = [
    cp.Minimize(x1 + x2),
    cp.Minimize(-x1 - x2),
    cp.Minimize(x1),
    cp.Minimize(cp.max_elemwise(x1, x2)),
    cp.Minimize(cp.power(x1, 2) + 9*cp.power(x2, 2))
]

for o_id, objective in enumerate(objectives):
    prob = cp.Problem(objective, constraints)
    print('< Objective', o_id, '>')
    print('Optimal objective value    :', prob.solve())
    print('optimal value for variables: x1={}, x2={}'.format(x1.value, x2.value))
    print('-------------------------------------------------------------------------\n')
Пример #30
0
 def _estimate(self, t, wplus, z, value):
     self.risks = [
         risk.weight_expr(t, wplus, z, value) for risk in self.riskmodels
     ]
     return cvx.max_elemwise(*self.risks)
Пример #31
0
def hinge_loss(theta, X, y):
    return cp.sum_entries(cp.max_elemwise(1 - sp.diags([y],[0])*X*theta, 0))
Пример #32
0
def infinite_push(theta, Xp, Xn):
    m, d = Xp.shape
    n = Xn.shape[0]
    Z = cp.max_elemwise(
        1 - (Xp*theta*np.ones((1,n)) - (Xn*theta*np.ones((1,m))).T), 0)
    return cp.max_entries(cp.sum_entries(Z, axis=0))
Пример #33
0
def f_dead_zone():
    eps = np.abs(randn())
    return cp.sum_entries(cp.max_elemwise(cp.abs(x)-eps, 0))
Пример #34
0
import cvxpy as cp, numpy as np, cvxopt, matplotlib.pyplot as plt
import ncvx

np.random.seed(1)

N = 41 # number of circles
r = 0.4 + 0.6 * np.random.rand(N) # radii

#define variables.
x_vals = [cp.Variable() for i in range(N)]
y_vals = [cp.Variable() for i in range(N)]

objective = cp.Minimize(cp.max_elemwise( *(x_vals + y_vals) ) + 0.5)
constraints = []
for i in range(N):
    constraints  += [0.5*r[i] <= x_vals[i],
                     0.5*r[i] <= y_vals[i]]
diff_vars = []
for i in range(N-1):
    for j in range(i+1,N):
        t = cp.Variable()
        diff_vars.append(ncvx.Annulus(2, 0.5 * (r[i]+r[j]), N))
        constraints += [
            cp.vstack(x_vals[i] - x_vals[j],
                      y_vals[i] - y_vals[j]) == diff_vars[-1]]

prob = cp.Problem(objective, constraints)
result = prob.solve(method="relax-round-polish", polish_depth=100)

#plot the circles
circ = np.linspace(0,2 * np.pi)
Пример #35
0
np.random.seed(0)
m = 100
n = 200
k = 10
A = [normalized_data_matrix(m, n, 1) for i in range(k)]
B = normalized_data_matrix(k, n, 1)
c = np.random.rand(k)

# Problem construction

x = cp.Variable(n)
obj_list = [
    cp.pnorm(A[i] * x, 2) + cp.abs(B[i, :] * x - c[i]) for i in range(k)
]
f = cp.max_elemwise(obj_list)

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()
Пример #36
0
def f_hinge_axis1():
    return cp.sum_entries(cp.max_elemwise(X,0), axis=1)
Пример #37
0
x0 = np.zeros(n)
A += x0

k = max(m // 50, 1)
idx = np.random.randint(0, m, k)
A[idx, :] += np.random.randn(k, n)
lam = 1

# Problem construction

x = cp.Variable(n)
rho = cp.Variable(1)

# Straightforward expression
z = np.sum(A**2, axis=1) - 2 * A * x + cp.sum_squares(x)  # z_i = ||a_i - x||^2
f = cp.sum_entries(cp.max_elemwise(
    z - rho, 0)) + lam * cp.sum_entries(cp.max_elemwise(0, rho))
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))
han = []

mu = cvx.Parameter(sign='positive')
x = cvx.Variable(n, 1)
exp = pbar.T * x
var = cvx.quad_form(x, S)
obj = cvx.Minimize(-exp + mu * var)
cs1 = [npy.ones((1, n)) * x == 1]
prb = cvx.Problem(obj, [])
for idx in range(2):
    if idx == 0:
        prb.constraints = cs1 + [x >= 0]
        lab = 'long only'
    else:
        prb.constraints = cs1 + \
                [npy.ones((1, n)) * cvx.max_elemwise(-x, 0) <= .5]
        lab = 'total short <= .5'
    for jdx in range(nn):
        mu.value = mus[jdx]
        prb.solve()
        #print(prb.status)
        avg[jdx] = exp.value
        std[jdx] = npy.sqrt(var.value)
    #plot
    hantemp = plt.plot(std, avg, label=lab)
    han = han + hantemp
#plt.gca().set_aspect('equal')
ylimi = plt.gca().get_ylim()
plt.gca().set_ylim((0, ylimi[1]))
plt.ylabel('mean')
plt.xlabel('standard deviation')
Пример #39
0
 prox("NORM_1", f_norm1_weighted),
 prox("NORM_1", lambda: cp.norm1(x)),
 prox("NORM_2", lambda: cp.norm(X, "fro")),
 prox("NORM_2", lambda: cp.norm2(x)),
 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),
Пример #40
0
def f_quantile():
    alpha = rand()
    return cp.sum_entries(cp.max_elemwise(alpha*x,(alpha-1)*x))
Пример #41
0
def hinge_loss(theta, X, y):
    if not all(np.unique(y) == [-1, 1]):
        raise ValueError("y must have binary labels in {-1,1}")
    return cp.sum_entries(cp.max_elemwise(1 - sp.diags([y],[0])*X*theta, 0))
Пример #42
0
def f_quantile():
    alpha = rand()
    return cp.sum_entries(cp.max_elemwise(alpha*x,(alpha-1)*x))