python-3.xmathematical-optimizationcvxpycvxoptcvx

Absolute value function not recognized as Disciplined Convex Program (CVXPY)


I am trying to run the following optimization using CVXPY:

import cvxpy as cp
import numpy as np

weights_vec = cp.Variable(10)

er_vec = cp.Parameter(10, value=np.random.randn(10))
prev_h_vec = cp.Parameter(10, value=np.ones(10))
tcost_vec = cp.Parameter(10, value=[0.03]*10)


objective = cp.Maximize(weights_vec @ er_vec - tcost_vec @ cp.abs(weights_vec - prev_h_vec))
prob = cp.Problem(objective)
prob.solve()

However, I get the following error:

cvxpy.error.DCPError: Problem does not follow DCP rules. Specifically:
The objective is not DCP. Its following subexpressions are not:
param516 @ abs(var513 + -param515)

The absolute function is convex. Hence, I am not quite sure why CVX is throwing an error for the absolute value function in the objective.


Solution

  • DCP-ness depends on the sign of tcost_vec.

    As this is a (unconstrained) parameter it's not okay.

    Both of the following will work:

    # we promise it's nonnegative
    tcost_vec = cp.Parameter(10, value=[0.03]*10, nonneg=True)
    
    # it's fixed and can by analyzed
    tcost_vec = np.array([-0.03]*10)
    

    Given the code as posted, there is not reason to use parameters (yet).