**Title:** DCPError in CVXPY: Division by Norm Constraint Violates DCP Rules in MPC for Rendezvous Problem

Question:

I’m trying to implement a model predictive control (MPC) for a rendezvous problem using CVXPY. The code calculates the control u to drive the state x towards a reference state x_ref over a specified horizon N. However, I’m encountering a DCPError, which states that the problem does not follow DCP (Disciplined Convex Programming) rules due to a division by a norm inside a constraint. Here’s a truncated version of the error:

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,))
...

The problematic part of my code seems to be this line:

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

The code snippet for the mpc_rendezvous function is below:

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]  # initial condition constraint

    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

# Calling the function
u = mpc_rendezvous(x_current, N_rendezvous, A_rendezvous, B_rendezvous, u_max_rendezvous, Q_rendezvous, R_rendezvous, Qf_rendezvous, lambda_rendezvous, r1)

Additional Information:

  • A and B are discretized matrices.
  • r1 is an integer.
  • The objective includes a terminal penalty terminal_penalty(x[:, N], H, lambda_rendezvous).

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  # degrees
u_max_rendezvous = 0.1786  # m/s

How can I rewrite or relax the constraints to conform to DCP rules and avoid this error? I tried following this thread: How to handle nonlinear equality constraints? - #4 by mcg
Thank you!

This forum is for CVX, which runs under MATLAB, not CVXPY

https://www.cvxpy.org/faq/index.html#where-can-i-get-help-with-cvxpy

I have been facing exactly the same error while implementing a particular constraint for a well defined objective function.
It is as follows:

Warning: A non-empty cvx problem already exists in this scope.
   It is being overwritten. 
> In cvxprob (line 28)
In cvx_begin (line 41)
In mpc_rendezvous (line 2)
In mpc_initial (line 76) 
Error using  .*  (line 173)
Disciplined convex programming error:
    Cannot perform the operation: {real affine} ./ {convex}

Error in  ./  (line 19)
z = times( x, y, './' );

Error in  *  (line 36)
    z = feval( oper, x, y );

Error in  /  (line 15)
z = mtimes( x, y, 'rdivide' );

Error in mpc_rendezvous (line 16)
            nhat_k = x(1:3, k) / (norm_x_k + 1e-6);  % Adding a small epsilon to avoid division by zero

Error in mpc_initial (line 76)
        u = mpc_rendezvous(x_current, N_rendezvous, A_rendezvous, B_rendezvous, u_max_rendezvous, Q_rendezvous, R_rendezvous, Qf_rendezvous, lambda_rendezvous, r1, r2, cone_angle_rendezvous);

Now, this particular line mpc_rendezvous (line 16) is one of the constraint for the optimisation problem. Code for which is as follows:

function u = mpc_rendezvous(x0, N, A, B, u_max, Q, R, Qf, lambda_rendezvous, r1, r2, cone_angle)
    cvx_begin quiet
        cvx_precision high
        variable x(size(A,1), N+1)
        variable u(size(B,2), N)
        cost = 0;
        
        % Reference position
        x_ref = [r1; 0; 0; 0; 0; 0];
        H = generate_H_matrix(cone_angle, 4);

        % MPC cost and constraints
        for k = 1:N
            % Calculate Euclidean norm and unit vector of x(:, k)
            norm_x_k = norm(x(1:3, k), 2);
            nhat_k = x(1:3, k) / (norm_x_k + 1e-6); 
            
            cost = cost + quad_form(x(:, k) - x_ref, Q) + quad_form(u(:, k), R);
            
            % System dynamics constraint
            x(:, k+1) == A * x(:, k) + B * u(:, k);
            
            % Control input constraint
            norm(u(:, k), Inf) <= u_max;
            
            % Keep-out constraint: ensuring the state remains outside r2
            (nhat_k')* (x(1:3, k)) >= r2;
        end

        % Terminal cost with cone constraint penalty
        cost = cost + quad_form(x(:, N+1) - x_ref, Qf) + terminal_penalty(x(:, N+1), H, lambda_rendezvous);
        
        % Initial condition constraint
        x(:, 1) == x0;
    cvx_end
    
    u = u(:, 1); % return the first control input
end

All the hyper-parameters and weights are well defined. This is what the constraint looks like in its mathematical form.

I need to implement that particular constraint without violating the DCP rules. Any tips in this direction would be much appreciated.
Thank You.

Image Credits:
Fear, Andrew & Lightsey, E… (2024). Autonomous Rendezvous and Docking Implementation for Small Satellites Using Model Predictive Control. Journal of Guidance, Control, and Dynamics. 1-9. 10.2514/1.G007523.

The text says to use an outer approximation of linear inequalities in (10) instead of using (9). The linear inequalities can be straightforwardly entered into CVX once you determine what they should be. how to determine what they should be is referenced, and is out of scope of this forum.