5

I want to add many constraint in a optimization problem under cvxpy. In matlab I can do so by adding a line subject to and then use for loop to generate the constraints. How can I do the same work in cvxpy, as there is no 'subject to' concepts in cvxpy. any suggestion please?

Rodrigo de Azevedo
  • 1,097
  • 9
  • 17
Creator
  • 139
  • 1
  • 3
  • 15

1 Answers1

10

In Python constraints is a list. You can use for loop to append/extend it like this (and CVXPY functions make it easier).

import cvxpy as cvx

samples = 10
x = cvx.Variable(samples)
y = range(1, samples+1)
constraints = []

for i in xrange(samples):
    constraints += [
        y[i] * x[i] <= 1000,
        x[i] >= i
    ]

objective = cvx.Maximize(cvx.sum_entries(x))

prob = cvx.Problem(objective, constraints)
prob.solve()
print x.value
dave-cz
  • 413
  • 4
  • 22