# How do I implement this convex problem in CVXPY?

I am looking to implement the following optimization problem in CVXPY.

$$\max _{x_t} x_t' \mu - \frac{\gamma}{2} x'_t \Sigma x_t - x'_t\Lambda \Delta x_t$$

where $$\Delta x_t := x_t - x_{t-1}$$ and $$\Sigma$$ and $$\Lambda$$ are positive definite symmetric matrices.

Here is my attempt:

import cvxpy as cp
import numpy as np

N = 2
gamma = 0.01
xprev = [0.5, 0.5]
mu = [0.01, 0.01]
Sigma = np.diag([0.1 ** 2] * 2)
Lambda = Sigma * 0.01
x = cp.Variable(N)
obj_terms = []
constraints = []
xdiff = x - xprev

term1 = cp.sum(cp.multiply(mu, x))
term2 = gamma * 0.5 * cp.quad_form(x, Sigma)
term3 = x.T @ Lambda @ xdiff

term = (term1 - term2 - term3)
obj_terms.append(term)
objective = cp.Maximize(cp.sum(obj_terms))
problem = cp.Problem(objective)
problem.solve()
x_value = x.value
print(x_value)


However, this implementation always results in a DCP error. I believe this problem is a convex one. The problem child is the implementation x.T @ Lambda @ xtdiff. Can someone please help me figure out how to implement the term $$x'_t\Lambda \Delta x_t$$ in CVXPY?

• How did you establish this is convex? I don't think it is. (Last term is spoiling things). Jun 26, 2023 at 11:14
• Please include the DCP error message Jun 26, 2023 at 11:16
• Where does this QP come from? Jun 26, 2023 at 11:17
• @ErwinKalvelagen It is convex given that $\Gamma$ and $\Lambda$ are symmetric matrices (see updated problem statement). One can establish by taking the derivative twice. Jun 26, 2023 at 13:51
• @RodrigodeAzevedo Here is the error. cvxpy.error.DCPError: Problem does not follow DCP rules. Specifically: The objective is not DCP. Jun 26, 2023 at 13:53