コード例 #1
0
ファイル: huber.py プロジェクト: yfamy123/subgradient-py
def huber(x,M):
    """
    Huber penalty function with parameter M.
    Convex.

    :param x: Number or scalar object.
    :param M: Positive constant.
    :rtype: Number or :class:`cvxpy_tree`.
    """

    # x must be scalar
    if((not np.isscalar(x)) and
       type(x).__name__ not in SCALAR_OBJS):
        raise ValueError('Invalid first argument')

    # M must be a positive constant
    if((not np.isscalar(M)) or M <= 0):
        raise ValueError('Invalid second argument')

    # Construct and return program
    v = var('v')
    w = var('w')
    a = param('a')
    p = prog(minimize(2*v+square(w)),
             [less(abs(a),w+v),
              greater(v,0),
              greater(w,0),
              less(w,1)],
             [a],None,'huber') 
    return square(M)*p((1.0/M)*x)
コード例 #2
0
def huber(x, M):
    """
    Huber penalty function with parameter M.
    Convex.

    :param x: Number or scalar object.
    :param M: Positive constant.
    :rtype: Number or :class:`cvxpy_tree`.
    """

    # x must be scalar
    if ((not np.isscalar(x)) and type(x).__name__ not in SCALAR_OBJS):
        raise ValueError('Invalid first argument')

    # M must be a positive constant
    if ((not np.isscalar(M)) or M <= 0):
        raise ValueError('Invalid second argument')

    # Construct and return program
    v = var('v')
    w = var('w')
    a = param('a')
    p = prog(minimize(2 * v + square(w)),
             [less(abs(a), w + v),
              greater(v, 0),
              greater(w, 0),
              less(w, 1)], [a], None, 'huber')
    return square(M) * p((1.0 / M) * x)
コード例 #3
0
def quad_form(x, P):
    """
    Quadratic form :math:`x^TPx`.
    Convex.

    :param x: Column vector.
    :type x: :class:`cvxpy_matrix` or array object
    :param P: Positive semidefinite matrix.
    :type P: :class:`cvxpy_matrix`
    :rtype: number or :class:`cvxpy_tree`.
    """

    # P must be symmetric
    if (not np.allclose(P, P.T)):
        raise ValueError('Invalid second argument')

    # P must be positive semidefinite
    min_eig = np.min(np.linalg.eig(P)[0])
    if (min_eig < 0.0):
        raise ValueError('Invalid second argument')
    P_half = sqrtm(P)
    (m, n) = x.shape

    # x must be a column vector
    if (n != 1):
        raise ValueError('Invalid first argument')

    # Construct and return program
    a = param('a', m, 1)
    t = var('t')
    p = prog(minimize(t), [less(sum(square(sqrtm(P) * a)), t)], [a], None,
             'quad_form')
    return p(x)
コード例 #4
0
ファイル: quad_form.py プロジェクト: yfamy123/subgradient-py
def quad_form(x,P):
    """
    Quadratic form :math:`x^TPx`.
    Convex.

    :param x: Column vector.
    :type x: :class:`cvxpy_matrix` or array object
    :param P: Positive semidefinite matrix.
    :type P: :class:`cvxpy_matrix`
    :rtype: number or :class:`cvxpy_tree`.
    """

    # P must be symmetric
    if(not np.allclose(P,P.T)):
        raise ValueError('Invalid second argument')

    # P must be positive semidefinite
    min_eig = np.min(np.linalg.eig(P)[0])
    if(min_eig < 0.0):
        raise ValueError('Invalid second argument')
    P_half = sqrtm(P)
    (m,n) = x.shape

    # x must be a column vector
    if(n!=1):
        raise ValueError('Invalid first argument')

    # Construct and return program
    a = param('a',m,1)
    t = var('t')
    p = prog(minimize(t),
             [less(sum(square(sqrtm(P)*a)),t)],
             [a],
             None,
             'quad_form')
    return p(x)