I am using cvxpy
to solve a convex optimization problem, and here's my constraint:
So how can I express this constraint in cvxpy
? The sum_entries
function in cvxpy
can only sum the whole matrix/vector, but not part of a vector.
Just select a subset by using indexing (in the following example: classic python-based slicing; but more complex indexing / numpy-style is possible):
Example:
from cvxpy import *
x = Variable(5)
constraints = []
constraints.append(x >= 0) # all vars
constraints.append(x <= 10) # all vars
constraints.append(sum_entries(x[:3]) <= 3) # only part of vector; sum(first-three) <=3
objective = Maximize(sum_entries(x))
problem = Problem(objective, constraints)
problem.solve()
print(problem.status)
print(x.value.T)
Output:
optimal
[[ 1. 1. 1. 10. 10.]]
Note: as of cvxpy 1.0, sum_entries
has been changed to sum
I also suspect you are misunderstanding the problem here, but that formula-image is of course incomplete to be implemented.