'use "abs" with @ operator in CVXPY
I want to do the absolute operator with CVXPY: abs(A @ x).
From here cvxpy objective function error involving MulExpression I understand that I cannot use np.abs since it is overwritten from CVXPY, so i tried using the abs, as in this documetation: https://www.cvxpy.org/tutorial/functions/index.html,
example taken from
import cvxpy as cp
import numpy
# Problem data.
m = 10
n = 5
numpy.random.seed(1)
A = numpy.random.randn(m, n)
b = numpy.random.randn(m)
# Construct the problem.
x = cp.Variable(n)
objective = cp.Minimize(cp.sum_squares(abs(A @ x) - b)) #here i get the error
constraints = [0 <= x, x <= 1]
prob = cp.Problem(objective, constraints)
print("Optimal value", prob.solve())
print("Optimal var")
print(x.value) # A numpy ndarray.
OUT :
TypeError Traceback (most recent call last)
<ipython-input-26-0483d2aabc95> in <module>()
11 # Construct the problem.
12 x = cp.Variable(n)
---> 13 objective = cp.Minimize(cp.sum_squares(abs(A @ x) - b))
14 constraints = [0 <= x, x <= 1]
15 prob = cp.Problem(objective, constraints)
TypeError: bad operand type for abs(): 'MulExpression'
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|