當前位置: 首頁>>代碼示例>>Python>>正文


Python cvxpy.norm方法代碼示例

本文整理匯總了Python中cvxpy.norm方法的典型用法代碼示例。如果您正苦於以下問題:Python cvxpy.norm方法的具體用法?Python cvxpy.norm怎麽用?Python cvxpy.norm使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在cvxpy的用法示例。


在下文中一共展示了cvxpy.norm方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: __init__

# 需要導入模塊: import cvxpy [as 別名]
# 或者: from cvxpy import norm [as 別名]
def __init__(self):
        """
        A large r_scale for a small scale problem will
        ead to numerical problems as parameters become excessively small
        and (it seems) precision is lost in the dynamics.
        """

        self.set_random_initial_state()

        self.x_init = np.concatenate(((self.m_wet,), self.r_I_init, self.v_I_init, self.q_B_I_init, self.w_B_init))
        self.x_final = np.concatenate(((self.m_dry,), self.r_I_final, self.v_I_final, self.q_B_I_final, self.w_B_final))

        self.r_scale = np.linalg.norm(self.r_I_init)
        self.m_scale = self.m_wet

        # slack variable for linear constraint relaxation
        self.s_prime = cvx.Variable((K, 1), nonneg=True)

        # slack variable for lossless convexification
        # self.gamma = cvx.Variable(K, nonneg=True) 
開發者ID:EmbersArc,項目名稱:SCvx,代碼行數:22,代碼來源:rocket_landing_3d.py

示例2: __init__

# 需要導入模塊: import cvxpy [as 別名]
# 或者: from cvxpy import norm [as 別名]
def __init__(self):
        """
        A large r_scale for a small scale problem will
        ead to numerical problems as parameters become excessively small
        and (it seems) precision is lost in the dynamics.
        """

        # self.set_random_initial_state()

        self.x_init = np.concatenate(((self.m_wet,), self.r_I_init, self.v_I_init, self.q_B_I_init, self.w_B_init))
        self.x_final = np.concatenate(((self.m_dry,), self.r_I_final, self.v_I_final, self.q_B_I_final, self.w_B_final))

        self.r_scale = np.linalg.norm(self.r_I_init)
        self.m_scale = self.m_wet

        # self.nondimensionalize() 
開發者ID:EmbersArc,項目名稱:SuccessiveConvexificationFreeFinalTime,代碼行數:18,代碼來源:model_6dof.py

示例3: running_example

# 需要導入模塊: import cvxpy [as 別名]
# 或者: from cvxpy import norm [as 別名]
def running_example():
    print("running example")
    m = 20
    n = 10
    x = cp.Variable((n, 1))
    F = cp.Parameter((m, n))
    g = cp.Parameter((m, 1))
    lambd = cp.Parameter((1, 1), nonneg=True)
    objective_fn = cp.norm(F @ x - g) + lambd * cp.norm(x)
    constraints = [x >= 0]
    problem = cp.Problem(cp.Minimize(objective_fn), constraints)
    assert problem.is_dcp()
    assert problem.is_dpp()
    print("is_dpp: ", problem.is_dpp())

    F_t = torch.randn(m, n, requires_grad=True)
    g_t = torch.randn(m, 1, requires_grad=True)
    lambd_t = torch.rand(1, 1, requires_grad=True)
    layer = CvxpyLayer(problem, parameters=[F, g, lambd], variables=[x])
    x_star, = layer(F_t, g_t, lambd_t)
    x_star.sum().backward()
    print("F_t grad: ", F_t.grad)
    print("g_t grad: ", g_t.grad) 
開發者ID:cvxgrp,項目名稱:cvxpylayers,代碼行數:25,代碼來源:cvxpy_examples.py

示例4: test_simple_batch_socp

# 需要導入模塊: import cvxpy [as 別名]
# 或者: from cvxpy import norm [as 別名]
def test_simple_batch_socp(self):
        set_seed(243)
        n = 5
        m = 1
        batch_size = 4

        P_sqrt = cp.Parameter((n, n), name='P_sqrt')
        q = cp.Parameter((n, 1), name='q')
        A = cp.Parameter((m, n), name='A')
        b = cp.Parameter((m, 1), name='b')

        x = cp.Variable((n, 1), name='x')

        objective = 0.5 * cp.sum_squares(P_sqrt @ x) + q.T @ x
        constraints = [A@x == b, cp.norm(x) <= 1]
        prob = cp.Problem(cp.Minimize(objective), constraints)

        prob_tch = CvxpyLayer(prob, [P_sqrt, q, A, b], [x])

        P_sqrt_tch = torch.randn(batch_size, n, n, requires_grad=True)
        q_tch = torch.randn(batch_size, n, 1, requires_grad=True)
        A_tch = torch.randn(batch_size, m, n, requires_grad=True)
        b_tch = torch.randn(batch_size, m, 1, requires_grad=True)

        torch.autograd.gradcheck(prob_tch, (P_sqrt_tch, q_tch, A_tch, b_tch)) 
開發者ID:cvxgrp,項目名稱:cvxpylayers,代碼行數:27,代碼來源:test_cvxpylayer.py

示例5: __init__

# 需要導入模塊: import cvxpy [as 別名]
# 或者: from cvxpy import norm [as 別名]
def __init__(self, m, k, n, complex=False):
        if not cvx_available:
            raise RuntimeError('Cannot initialize when cvxpy is not available.')
        # Initialize parameters and variables
        A = cvx.Parameter((m, k), complex=complex)
        B = cvx.Parameter((m, n), complex=complex)
        l = cvx.Parameter(nonneg=True)
        X = cvx.Variable((k, n), complex=complex)
        # Create the problem
        # CVXPY issue:
        #   cvx.norm does not work if axis is not 0.
        # Workaround:
        #   use cvx.norm(X.T, 2, axis=0) instead of cvx.norm(X, 2, axis=1)
        obj_func = 0.5 * cvx.norm(cvx.matmul(A, X) - B, 'fro')**2 + \
                   l * cvx.sum(cvx.norm(X.T, 2, axis=0))
        self._problem = cvx.Problem(cvx.Minimize(obj_func))
        self._A = A
        self._B = B
        self._l = l
        self._X = X 
開發者ID:morriswmz,項目名稱:doatools.py,代碼行數:22,代碼來源:l1lsq.py

示例6: test_proj_soc

# 需要導入模塊: import cvxpy [as 別名]
# 或者: from cvxpy import norm [as 別名]
def test_proj_soc(self):
        import cvxpy as cp
        np.random.seed(0)
        n = 100
        for _ in range(15):
            x = np.random.randn(n)
            z = cp.Variable(n)
            objective = cp.Minimize(cp.sum_squares(z - x))
            constraints = [cp.norm(z[1:], 2) <= z[0]]
            prob = cp.Problem(objective, constraints)
            prob.solve(solver="SCS", eps=1e-10)
            p = cone_lib._proj(x, cone_lib.SOC, dual=False)
            np.testing.assert_allclose(
                p, np.array(z.value))
            np.testing.assert_allclose(
                p, cone_lib._proj(x, cone_lib.SOC, dual=True)) 
開發者ID:cvxgrp,項目名稱:diffcp,代碼行數:18,代碼來源:tests.py

示例7: relax

# 需要導入模塊: import cvxpy [as 別名]
# 或者: from cvxpy import norm [as 別名]
def relax(self):
        """The convex relaxation.
        """
        constr = super(Card, self).relax()
        return constr + [cvx.norm(self, 1) <= self.k*self.M,
                         cvx.norm(self, 'inf') <= self.M] 
開發者ID:cvxgrp,項目名稱:ncvx,代碼行數:8,代碼來源:card.py

示例8: _project

# 需要導入模塊: import cvxpy [as 別名]
# 或者: from cvxpy import norm [as 別名]
def _project(self, matrix):
        if self.R >= cvx.norm(matrix, 2).value >= self.r:
            return matrix
        elif cvx.norm(matrix, 2).value == 0:
            result = np.ones(self.shape)
            return self.r*result/cvx.norm(result, 2).value
        elif cvx.norm(matrix, 2).value < self.r:
            return self.r*matrix/cvx.norm(matrix, 2).value
        else:
            return self.R*matrix/cvx.norm(matrix, 2).value 
開發者ID:cvxgrp,項目名稱:ncvx,代碼行數:12,代碼來源:annulus.py

示例9: _restrict

# 需要導入模塊: import cvxpy [as 別名]
# 或者: from cvxpy import norm [as 別名]
def _restrict(self, matrix):
        # Add restriction that beyond hyperplane at projection onto
        # n-sphere of radius r.
        return [matrix.T*self >= self.r*cvx.norm(matrix, 2).value] 
開發者ID:cvxgrp,項目名稱:ncvx,代碼行數:6,代碼來源:annulus.py

示例10: relax

# 需要導入模塊: import cvxpy [as 別名]
# 或者: from cvxpy import norm [as 別名]
def relax(self):
        """The convex relaxation.
        """
        constr = super(Annulus, self).relax()
        return constr + [cvx.norm(self, 2) <= self.R] 
開發者ID:cvxgrp,項目名稱:ncvx,代碼行數:7,代碼來源:annulus.py

示例11: dist

# 需要導入模塊: import cvxpy [as 別名]
# 或者: from cvxpy import norm [as 別名]
def dist(self, matrix):
        """Distance from matrix to projection.
        """
        proj_mat = self.project(matrix)
        return cvxpy.norm(cvxpy.vec(matrix - proj_mat), 2).value

    # Project the matrix into the space defined by the non-convex constraint.
    # Returns the updated matrix. 
開發者ID:cvxgrp,項目名稱:ncvx,代碼行數:10,代碼來源:noncvx_variable.py

示例12: get_error

# 需要導入模塊: import cvxpy [as 別名]
# 或者: from cvxpy import norm [as 別名]
def get_error(noncvx_vars, eps, rel_eps):
    """The error bound for comparing infeasibility.
    """
    error = sum([cvx.norm(cvx.vec(var)) for var in noncvx_vars]).value
    return eps + rel_eps*error 
開發者ID:cvxgrp,項目名稱:ncvx,代碼行數:7,代碼來源:admm_problem.py

示例13: init_z

# 需要導入模塊: import cvxpy [as 別名]
# 或者: from cvxpy import norm [as 別名]
def init_z(self, random):
        """Initializes the value of the replicant variable.
        """
        if random:
            length = np.random.uniform()
            direction = np.random.randn(self.shape)
            self.z.value = length*direction/norm(direction, 2).value
        else:
            self.z.value = np.zeros(self.shape) 
開發者ID:cvxgrp,項目名稱:ncvx,代碼行數:11,代碼來源:sphere.py

示例14: _project

# 需要導入模塊: import cvxpy [as 別名]
# 或者: from cvxpy import norm [as 別名]
def _project(self, matrix):
        if np.all(matrix == 0):
            result = np.ones(self.shape)
            return result/cvx.norm(result, 2).value
        else:
            return matrix/cvx.norm(matrix, 2).value

    # Constrain all entries to be the value in the matrix. 
開發者ID:cvxgrp,項目名稱:ncvx,代碼行數:10,代碼來源:sphere.py

示例15: relax

# 需要導入模塊: import cvxpy [as 別名]
# 或者: from cvxpy import norm [as 別名]
def relax(self):
        constr = super(Sphere, self).relax()
        return constr + [cvx.norm(self, 2) <= 1] 
開發者ID:cvxgrp,項目名稱:ncvx,代碼行數:5,代碼來源:sphere.py


注:本文中的cvxpy.norm方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。