-1

I am working on a package selection problem. I have to put a constraint on the final result, like ‘top3 brands among selected products should account for less than 50%’.

I try to implement that on Pulp. But it seems CBC solver do not support such constraint. Please help me. How can I put such constraint? Or should I switch to another solver?

Cino
  • 83
  • 1
  • 7

1 Answers1

1

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 
  
Erwin Kalvelagen
  • 15,677
  • 2
  • 14
  • 39
  • “Top3” means: the top3 brands with largest quantity in selected package. Eg. there are 1000 items. Each belongs to a certain brand. We will select some items and top3 brands should account for less than 50%(in the final combination). I am not an expert in linear programming. I guess it is not a MILP problem? And could u give more detailed explanation on your code? I am confused with the meaning of delta and M. Tks a lot! – Cino Nov 01 '22 at 13:23
  • Tks for ur answer. BTW can u give a brief introduction why y2 denotes “at least as large as the second largest x[i]”. For me, the inequalities above are equivalent to ‘x[i] <= 50%/3’ – Cino Nov 03 '22 at 07:32