-1

I'm working on an optimization problem using Python's CVXPY package. I created a CVXPY variable called x (an array of 10 values). I need to impose a constraint that the 2nd largest value in x is smaller than .03. I tried converting it to a NumPy array to no avail. How do I extract values from the x array?

import cvxpy as cp

x = cp.Variable(10)
cona
  • 169
  • 1
  • 13

1 Answers1

0

No, you have to stay within CVXPY, i.e. using the variable x directly.

The problem "the second-largest x should be <= 0.03" is not completely trivial.

The "largest x should be <= 0.03" is easy. It is the same as "all x should be <= 0.03" or

 x[i] <= 0.03 for all i

or in CVXPY notation:

 x <= 0.03

The second-largest condition means: all x must be <= 0.03 except for one. This can be modeled as (in math notation):

 x[i] <= 0.03 + δ[i]*(XUP-0.03)   for all i
 sum(δ[i]) = 1       (one exception) 
 x[i] <= XUP         (the constant XUP is an upper bound on x)
 δ[i] ∈ {0,1}        (binary variable)

I'll leave it up to you to transcribe this into CVXPY notation.

Erwin Kalvelagen
  • 15,677
  • 2
  • 14
  • 39