Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

This is a tough question and even tougher to explain.

USPS has a 70lb shipping limit per box and people can purchase more than that weight from my shopping cart. I can't price check weights that surpass 70lbs.

I need a way to determine the BEST weight to box ratio if they have too much weight. Like if they had 80lbs, a 50lb box and a 30lb box would work but I'd have to send two search queries to USPS which would be inefficient. If say they had 100lbs, I could price check 50lbs on USPS and double the price myself.

I need a way to automate this to determine if 1) the weight is too much 2) how many boxes are needed 3) finding the price per box (hoping to use just one search query unless it's really unreasonable).

This is making my brain hurt. Can anyone think of a way to do this?

  • Comment on determine numbers of boxes needed per weight

Replies are listed 'Best First'.
Re: determine numbers of boxes needed per weight
by BrowserUk (Patriarch) on Nov 24, 2005 at 05:00 UTC

    You mean something like this?

    perl> sub distribute{ my $weight = shift; my $n = int( $weight / 70 ) +1; return ($weight /$n) x $n; } perl> print "$_ as ", distribute $_ for map{ int rand 250 } 1 .. 20;; 65 as 65 130 as 65 65 91 as 45.5 45.5 93 as 46.5 46.5 243 as 60.75 60.75 60.75 60.75 15 as 15 79 as 39.5 39.5 235 as 58.75 58.75 58.75 58.75 119 as 59.5 59.5 105 as 52.5 52.5 37 as 37 42 as 42 75 as 37.5 37.5 119 as 59.5 59.5 79 as 39.5 39.5 225 as 56.25 56.25 56.25 56.25 180 as 60 60 60 104 as 52 52 183 as 61 61 61 118 as 59 59

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: determine numbers of boxes needed per weight
by perl_lover (Chaplain) on Nov 24, 2005 at 05:28 UTC

    I think your Problem is a Knapsack problem . There is a cpan module for Knapsack Algorithm (Algorithm::Knapsack)

    Algorithm::Knapsack
    use Algorithm::Knapsack; my $knapsack = Algorithm::Knapsack->new( capacity => $capacity, weights => \@weights, ); $knapsack->compute(); foreach my $solution ($knapsack->solutions()) { foreach my $index (@{$solution}) { # do something with $weights[$index] } }


    -perl_lover-

    Edit: g0n - reparented to root node

Re: determine numbers of boxes needed per weight
by GrandFather (Saint) on Nov 24, 2005 at 06:26 UTC

    There are a number of things you probably need to factor into your solution like:

    • how does the price change with box weight - is there a sweet spot?
    • is there a minimum box weight?
    • does it matter if the box weights are wildly different?
    • does the merchandise impose restrictions on box weight?
    • do you really need the best solution, or is a 95% solution good enough?

    It is hard to provide much help beyond "it's a knapsack problem" without a bit more detail about the constraints, and impossible to provide anything approaching an optimim solution.

    On the other hand, with a little more information, this sort of problem is the sort of thing that monks like to find solutions for and your particular variant doesn't sound particuarly intractable.


    DWIM is Perl's answer to Gödel
      But it's not a knapsack problem. It would be a knapsack problem if he has a set of fixed weights, and a set of fixed size boxes. But he doesn't - he can divide the weights anyway he wants to. He wants to minimize the number of different size boxes, with a limit on the size of an individual box. BrowserUK's solution will do. If you throw in an additional constraint on the minimum size of a box, there will be situations where you need two different size boxes.
      Perl --((8:>*

        Fair enough. However without knowing the box weight (and possibly size) constraints imposed by USPS and the nature of the items being packed, it was not at all clear that OP's problem wasn't a knapsack problem. Especially if there were other criteria such as weights and/or box sizes that were better economy than others.


        DWIM is Perl's answer to Gödel
      Hi. Here is some more information.

      For the sweet spot, I am not sure. But I would like to keep things between 50 and 70 pounds to make best use of the boxes.

      I don't want to go less than 50 lbs per box.

      The problem with different sizes boxes is I'd have to send a bot to the USPS site for each box. If they are all the same, I only need to parse their site once.

      This is for small products (candles, incense, soaps, etc) so no one item will take up a box, if that's what you mean.

      95% is definitely good enough.

      I really appreciate the help!

        Using the distribute function above, the minimum box weight would be 37.5 lbs, except where the total weight is less than that figure; and you will only ever need to look up one price as they will always be the same.

        If you need to limit your weight to greater than 50 lbs, then you will inevitably have to do 2 lookups more often than not.


        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.

        But you must go to a 10 lbs box for small orders surely? Or is there a minimum charge that equates to say, a 20 lbs box? For your 80 lbs shipment, why not two 40 lbs boxes?


        DWIM is Perl's answer to Gödel
Re: determine numbers of boxes needed per weight
by monarch (Priest) on Nov 24, 2005 at 23:14 UTC
    If you are making hundreds of queries per day, then perhaps a caching algorithm may really assist you. In otherwords, at the beginning of the day, send queries to USPS for the price of every sized box. Store this data locally to assist you with making a more accurate estimation.