DCP error Objective function model predictive control

119 views
Skip to first unread message

Sandeep hegde

unread,
Jan 17, 2021, 7:00:05 AM1/17/21
to cvxpy
Hello members,
I am new user of cvxpy. I want to solve optimization problem of the form
Screenshot from 2021-01-17 12-37-01.png
where xN, xr, xk, uk are vectors (numpy arrays) and Q, R are matrices. When I program it as shown below, I get the error:

Ad = np.random.rand(12,12)
Bd = np.random.rand(12,4)
[nx, nu] = Bd.shape

# limits
umin = np.array([9.6, 9.6, 9.6, 9.6]
umax = np.array([13., 13., 13., 13.])
xmin = np.array([-np.pi/6,-np.pi/6,-np.inf,-np.inf,-np.inf,-1.,
-np.inf,-np.inf,-np.inf,-np.inf,-np.inf,-np.inf])
xmax = np.array([ np.pi/6, np.pi/6, np.inf, np.inf, np.inf, np.inf,
np.inf, np.inf, np.inf, np.inf, np.inf, np.inf])

# Penalties
Q = np.diag([0., 0., 10., 10., 10., 10., 0., 0., 0., 5., 5., 5.])
QN = Q
R = 0.1*np.eye(4)

# Initial and reference states
x0 = np.zeros(12)
xr = np.array([0.,0.,1.,0.,0.,0.,0.,0.,0.,0.,0.,0.])

# Prediction horizon
N = 10

x = cp.Variable((nx,N+1))
u = cp.Variable((nu,N))

cost = 0
constraints = []

for t in range(N):
  cost+= (x[:,t+1].T - xr) @ Q @ (x[:,t+1] - xr.T) + u[:,t].T @ R @ u[:,t]
  constraints+= [x[:,t+1] == Ad@x[:,t] + Bd@u[:,t],u[:,t]<=umax,u[:,t]>=umin,x[:,t]>=xmin,x[:,t]<=xmax]

constraints+= [x[:,N]==xr, x[:,0]==x0]

problem = cp.Problem(cp.Minimize(cost),constraints)
problem.solve()

cvxpy.error.DCPError: Problem does not follow DCP rules. Specifically:
The objective is not DCP. Its following subexpressions are not:
(var0[0:12, 1] + -[0. 0. 1. 0. 0. 0. 0. 0. 0. 0. 0. 0.]) @ [[ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
 [ 0.  0. 10.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
 [ 0.  0.  0. 10.  0.  0.  0.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0. 10.  0.  0.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0. 10.  0.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  5.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  5.  0.]
 [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  5.]] @ (var0[0:12, 1] + -[0. 0. 1. 0. 0. 0. 0. 0. 0. 0. 0. 0.])
var1[0:4, 0] @ [[0.1 0.  0.  0. ]
 [0.  0.1 0.  0. ]
 [0.  0.  0.1 0. ]
 [0.  0.  0.  0.1]] @ var1[0:4, 0]
.
.
.
It continues like this. I understand that my cost term is incorrect. How can I write the expression X'QX and U'RU?
Thanks in advance.
Regards
Sandeep

Sandeep hegde

unread,
Jan 17, 2021, 7:27:16 AM1/17/21
to cvxpy
Hi,
 I think I found the solution by using the atom function quad_form. So I restructured my cost term like this:
cost+= cp.quad_form((x[:,t+1] - xr),Q) + cp.quad_form(u[:,t], R)

Sorry for the inconveniance. Please do reply if you have more to tell about this. CVXPY is awesome :)

Best regards
Sandeep
Reply all
Reply to author
Forward
0 new messages