cvxpy icon indicating copy to clipboard operation
cvxpy copied to clipboard

DCPError in CVXPY: Division by Norm Constraint Violates DCP Rules

Open varun-sappa opened this issue 1 year ago • 0 comments

Describe the bug
I'm encountering a DCPError in CVXPY when trying to implement a model predictive control (MPC) for a rendezvous problem. The error indicates that my problem does not follow DCP (Disciplined Convex Programming) rules, specifically due to a division by a norm inside a constraint.

To Reproduce
Here’s the minimal code example that reproduces the bug:

import cvxpy as cp
import numpy as np

def mpc_rendezvous(x0, N, A, B, u_max, Q, R, Qf, lambda_rendezvous, r1):
    n = A.shape[0]
    m = B.shape[1]
    x = cp.Variable((n, N+1))
    u = cp.Variable((m, N))
    
    x_ref = [r1, 0, 0, 0, 0, 0]
    H = generate_H_matrix(cone_angle_rendezvous, 4)
    
    cost = 0
    constraints = [x[:, 0] == x0]

    for k in range(N):
        cost += cp.quad_form(x[:, k] - x_ref, Q) + cp.quad_form(u[:, k], R)
        constraints += [x[:, k+1] == A @ x[:, k] + B @ u[:, k]]
        constraints += [cp.norm_inf(u[:, k]) <= u_max]
        q = (x[:,k]) / cp.norm((x[:,k]), 2)
        constraints += [q.T @ x[:, k] >= r1]
    
    cost += cp.quad_form(x[:, N] - x_ref, Qf)
    cost += terminal_penalty(x[:, N], H, lambda_rendezvous)

    prob = cp.Problem(cp.Minimize(cost), constraints)
    prob.solve(solver=cp.SCS, warm_start=False)

    print("Rendezvous phase problem status:", prob.status)
    return u[:, 0].value

# Parameters
N_rendezvous = 30
Q_rendezvous = np.diag([1, 1, 100, 1, 1, 10])
R_rendezvous = 1e3 * np.eye(3)
Qf_rendezvous = Q_rendezvous.copy()
lambda_rendezvous = 10
cone_angle_rendezvous = 20
u_max_rendezvous = 0.1786

The code snippet causing the error:

q = (x[:,k]) / cp.norm((x[:,k]), 2)
constraints += [q.T @ x[:, k] >= r1]

Expected behavior
The expected behavior is for the MPC optimization to proceed without violating DCP rules, allowing the state x to approach the reference state x_ref while respecting the constraints.

Output
Here is the truncated error output:

DCPError: Problem does not follow DCP rules. Specifically:
The following constraints are not DCP:
500.0 <= (var4917985[0:6, 0] / Promote(Pnorm(var4917985[0:6, 0], 2), (6,))) @ var4917985[0:6, 0] , because the following subexpressions are not:
|-- var4917985[0:6, 0] / Promote(Pnorm(var4917985[0:6, 0], 2), (6,))
...

Version

  • OS: Windows 11
  • CVXPY Version: 1.5.3

Additional context
I suspect that the division by cp.norm((x[:,k]), 2) is causing the issue since it does not conform to DCP rules. Any suggestions on how to rewrite or relax this constraint to avoid the DCP error would be helpful. I tried following this thread: https://ask.cvxr.com/t/how-to-handle-nonlinear-equality-constraints/22/4 Thank you!

varun-sappa avatar Nov 02 '24 04:11 varun-sappa