Get all boxes combinations? #266
-
Hi! Thanks for this grat library, really :) I want to get all the possible box combinations for a cart on ecommerce, because I want to code by myself the best option (volume vs shipping rate). What is the best performant way? Thanks, |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
Hi @CarlesRever The answer depends a little bit on how much you mean by "all", because as soon as you have an order than needs multiple boxes, things become interdependent, as what's best for the second box depend on what you picked for the first one... There are 2 main classes that are relevant here The first approach I'd suggest is just extending However, there is a performance trick at (https://github.com/dvdoug/BoxPacker/blob/3.x/src/Packer.php#L182) you might want to be aware of that stops evaluating boxes as soon as one is found that can contain all of the items. If you actually do want to consider all boxes and want to remove that early-exit, then I'd recommend copy/pasting the existing |
Beta Was this translation helpful? Give feedback.
-
Hi Doug! Thank you very much for your reply, so useful and appointed me in the right direction (I think) :) What I'm doing (for now) it's call packer in a loop. I load on each time the items again, limit the boxes and call packer(). I think all permutations can't be analised (for timeouts, memory or simply performance). I have a counter and stop after 20 iterations. I tried to exclude the first result box for the next iteration. This works fine for most of the cases, but, for example: what about if the first solution is two mid-boxes? Then the mid + small/great/whatelse, never occurs! I think the enforcing single box is right for 99% of the cases, and won't to override too much the code :) So, I think my best approach is keep my loop without box limits, and override the findBestBoxFromIteration() function as you suggested, with a mix of random, choosen strategy and permutation... thank you very much!!! ...do you allow me a suggestion? This isn't enough for my case, but maybe useful for most of your users: would be great if the compare() function works in distinct ways due an user strategy parameter (volume/max weight/maybe price?!) The best, |
Beta Was this translation helpful? Give feedback.
Hi @CarlesRever
The answer depends a little bit on how much you mean by "all", because as soon as you have an order than needs multiple boxes, things become interdependent, as what's best for the second box depend on what you picked for the first one...
There are 2 main classes that are relevant here
VolumePacker
(packs items in a box), andPacker
which wraps around that and handles the the logic relating to permutations.The first approach I'd suggest is just extending
Packer
and overriding thefindBestBoxFromIteration
method (https://github.com/dvdoug/BoxPacker/blob/3.x/src/Packer.php#L237) so that the comparison does what you need. That way you'll keep the benefit from any future enhan…