Find the closest stack total weight where number of n values should be the same in all stack

27 Views Asked by At

Lets say I have potatoes with weight 7,5,4,8,12,10,4,8,12,12,13,12
I want to stack them in the way that all the stacks weight should be closest possible
condition each stack should have equal amount of potatoes.
Number of stacks and number of potatoes are predefine can't be changed
Is there any formula to achieve this. or any other way

1

There are 1 best solutions below

0
On

You can solve the problem via mixed integer linear programming as follows. Let binary decision variable $x_{p,s}$ indicate whether potato $p$ is assigned to stack $s$. Let $y$ and $z$ represent the smallest and largest stack weights, respectively. The problem is to minimize $z-y$ subject to \begin{align} \sum_s x_{p,s} &= 1 &&\text{for all $p$} \tag1 \\ \sum_p x_{p,s} &= m &&\text{for all $s$} \tag2 \\ y \le \sum_p a_p x_{p,s} &\le z &&\text{for all $s$} \tag3 \end{align} Constraint $(1)$ assigns each potato to exactly one stack. Constraint $(2)$ assigns exactly $m$ potatoes to each stack. Constraint $(3)$ enforces $y \le \min_s \sum_p a_p x_{p,s}$ and $z \ge \max_s \sum_p a_p x_{p,s}$.