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