It depends a bit on what exactly "top 3 brands" is. If you mean the sum of the three largest x[i]
should be less than 50% of the total, then this can be linearized as follows:
y1 >= x[i] for all i
y2 >= x[i]-M*delta1[i] for all i
y3 >= x[i]-M*delta2[i] for all i
y1+y2+y3 <= sum(i,x[i])/2
sum(i, delta1[i]) = 1
sum(i, delta2[i]) = 2
delta1[i],delta2[i] ∈ {0,1}
This can be solved with CBC. Here M
is a large enough constant (called big-M
) and delta1
,delta2
are binary variables. Note that y1,y2,y3 are bounds on the largest three values, so interpreting these values may not always be obvious. Basically, the definition of these values is:
y1 : variable at least as large as the largest x[i]
y2 : variable at least as large as the second largest x[i]
y3 : variable at least as large as the third largest x[i]
Other and better formulations exist (but this one is a bit more intuitive [for me that is]).
A simpler approach would be to use sum_largest(x,k)
in CVXPY, e.g.
sum_largest(x,3) <= sum(x)/2