I need to fill an array which may contain an indeterminate number of subarrays (pallets) — each with a maximum size of 265cm.
I have a flat array of integers (packs) which need to be optimally arranged in pallets (for example 50cm, 45cm, 30cm, …).
How can I dynamically create a system that creates the multidimensional array that represents pallets with the best space optimization?
This is my code:
for ($i=0; $i < $mix_cc; $i++) {
foreach ($sh_array as $key => $row) {
$cm_remaining = $default_cc_height_fa - $sh_size;
$sh_size = $sh_size + $row['size'];
if ($row['size'] < $cm_remaining) {
$mix_cc_array[$cc_number][$key] = $sh_array[$key];
} else {
$mix_cc_array[$cc_number + 1][$key] = $sh_array[$key];
}
unset($sh_array[$key]);
}
$cc_number++;
}
2
Answers
To optimize the space in the pallets, you can try the following First Fit Decreasing (FFD) approach:
Sort the array of packs by size in descending order. This way, you can start by adding the largest packs first and try to fit as many of them as possible in the pallet.
Iterate through the sorted array of packs and try to fit each pack in the current pallet. If the pack fits, add it to the pallet; If the pack does not fit, create a new pallet and add the pack to it.
Here’s some sample code that demonstrates how you can implement this approach:
Sandbox Example: https://onlinephp.io/c/45ca2
This should give you an array of pallets that are optimally packed in terms of space utilization using the First Fit Decreasing (FFD) method. You can also look into the Next Fit Decreasing (NFD) method if this one doesn’t work for you.
Here are two succinct snippets to implement the First Fit Decreasing and Next Fit Decreasing algorithms. I suspect you will lean toward the FFD algorithm because it attempts to fully pack early pallets before bothering to open a new one. My understanding is that NDD is optimized for performance, but FFD is optimized for minimal pallets.
If my understanding of these algorithms is not entirely accurate, I am happy to be corrected.
Codes: (Demo)
And