Posted By

jerryvig on 10/10/10


Tagged

php bin problem cost bottles minimization packing usps


Versions (?)

Cost Minimization Algorithm for USPS Flat Rate Boxes


 / Published in: PHP
 

I have a client who ships fluids in USPS flat rate boxes. If you are unfamiliar with USPS flat rate boxes, they are boxes of a certain volume that ship regardless of weight. Anything that fits in the box, ships for one low price. My client uses two box sizes: the medium flat rate boxes, and the large flat rate boxes. Additionally, my client ships their fluids in the three bottle sizes: 200ml, 375ml, and 750ml. Moreover, due to the shape of the bottles only a certain number of bottles can fit in each box, and due to their shape the cost minimization cannot be detemined using the volume of each box and the bottle volumes. Thus, there are different arrangements of the bottles in each box which can work. For example, a medium box can hold 3 200ml and 2 375ml bottles or it can hold 4 200 ml bottles and 1 375ml bottle, and there are many other possible arrangements depending on the number of each size bottle. The table below, which I will call the allowed_configurations table lists the possible arrangements of each bottle in each size of box. Also, the large box costs $14.50 and the medium box costs $10.70 to ship.\r\n\r\nFor example, the first row in the table says 5,0,0 and indicates that the medium box can hold 5 200 ml bottles, and no other bottles. Using the table of the arrangements above, find an algorithm for computing the optimal arrangement with respect to the shipping cost for shipping a set of x 200ml bottles, y 375 ml bottles, and z 750 ml bottles. Your algorithm should not only compute the minimal cost but it should return the optimal arrangment of bottles among the different size boxes. I would be particularly interested in seeing your algorithm expressed in SQL, but solutions in a procedural language such as Java, PHP, or C will definitely be useful also.

  1. <?php
  2.  
  3. // the values $numberOf200mlBottles, $numberOf375mlBottles, $numberOf750mlBottles are integers
  4. // function to solve the "Bin Packing Problem" for packing 200 ml bottles, 375 ml bottles, and 750 ml bottles into medium and large USPS flat rate boxe\
  5. s.
  6.  
  7. function MinimizeUSPSCost( $numberOf750mlBottles, $numberOf375mlBottles, $numberOf200mlBottles ) {
  8.  
  9. $configCosts = array(array(2, 1, 0, 14.5), array(2, 0, 2, 14.5), array(2, 0, 1, 14.5), array(2, 0, 0, 14.5), array(1, 2, 0, 14.5), array(1, 1, 2, 14.5)\
  10. , array(1, 1, 1, 14.5), array(1, 1, 0, 14.5), array(1, 0, 4, 14.5), array(1, 0, 3, 14.5), array(1, 0, 2, 14.5), array(1, 0, 1, 14.5), array(1, 0, 0, 14.5\
  11. ), array(0, 6, 0, 14.5), array(0, 5, 1, 14.5), array(0, 5, 0, 14.5), array(0, 4, 2, 14.5), array(0, 4, 1, 14.5), array(0, 4, 0, 14.5), array(0, 3, 4, 14.\
  12. 5), array(0, 3, 3, 14.5), array(0, 3, 2, 14.5), array(0, 3, 1, 10.7), array(0, 3, 0, 10.7), array(0, 2, 5, 14.5), array(0, 2, 4, 14.5), array(0, 2, 3, 10\
  13. .7), array(0, 2, 2, 10.7), array(0, 2, 1, 10.7), array(0, 2, 0, 10.7), array(0, 1, 5, 14.5), array(0, 1, 4, 10.7), array(0, 1, 3, 10.7), array(0, 1, 2, 1\
  14. 0.7), array(0, 1, 1, 10.7), array(0, 1, 0, 10.7), array(0, 0, 8, 14.5), array(0, 0, 7, 14.5), array(0, 0, 6, 14.5), array(0, 0, 5, 10.7), array(0, 0, 4, \
  15. 10.7), array(0, 0, 3, 10.7), array(0, 0, 2, 10.7), array(0, 0, 1, 10.7));
  16.  
  17. $allowedConfigurations = array(array(2, 1, 0), array(2, 0, 2), array(2, 0, 1), array(2, 0, 0), array(1, 2, 0), array(1, 1, 2), array(1, 1, 1), array(1,\
  18. 1, 0), array(1, 0, 4), array(1, 0, 3), array(1, 0, 2), array(1, 0, 1), array(1, 0, 0), array(0, 6, 0), array(0, 5, 1), array(0, 5, 0), array(0, 4, 2), a\
  19. rray(0, 4, 1), array(0, 4, 0), array(0, 3, 4), array(0, 3, 3), array(0, 3, 2), array(0, 3, 1), array(0, 3, 0), array(0, 2, 5), array(0, 2, 4), array(0, 2\
  20. , 3), array(0, 2, 2), array(0, 2, 1), array(0, 2, 0), array(0, 1, 5), array(0, 1, 4), array(0, 1, 3), array(0, 1, 2), array(0, 1, 1), array(0, 1, 0), arr\
  21. ay(0, 0, 8), array(0, 0, 7), array(0, 0, 6), array(0, 0, 5), array(0, 0, 4), array(0, 0, 3), array(0, 0, 2), array(0, 0, 1));
  22.  
  23. //initialze set of boxes to ship with one empty box.
  24. $boxes = array( array( 0, 0, 0 ) );
  25.  
  26. //Process the large bottles first.
  27. for ( $i=$numberOf750mlBottles; $i>0; $i-- ) {
  28.  
  29. $configurationIsAllowed = false;
  30.  
  31. for ( $j=0; $j<count($boxes); $j++ ) {
  32.  
  33. $nextBox = array( ($boxes[$j][0]+1), $boxes[$j][1], $boxes[$j][2] );
  34.  
  35. for ( $k=0; $k<count($allowedConfigurations); $k++ ) {
  36. if ( $nextBox == $allowedConfigurations[$k] ) {
  37. //configuration is allowed
  38. $boxes[$j] = $nextBox;
  39. $configurationIsAllowed = true;
  40. break;
  41. }
  42. }
  43. if ( $configurationIsAllowed ) {
  44. break;
  45. }
  46.  
  47. }
  48.  
  49. if ( !$configurationIsAllowed ) {
  50. //configuration is not allowed add to set of boxes.
  51. //open a new box and place 750 ml bottle inside.
  52. array_push( $boxes, array(1, 0, 0) );
  53. }
  54.  
  55. }
  56.  
  57. //Now process medium bottles.
  58. for ( $i=$numberOf375mlBottles; $i>0; $i-- ) {
  59.  
  60. $configurationIsAllowed = false;
  61.  
  62. for ( $j=0; $j<count($boxes); $j++ ) {
  63.  
  64. $nextBox = array( $boxes[$j][0], ($boxes[$j][1]+1), $boxes[$j][2] );
  65.  
  66. for ( $k=0; $k<count($allowedConfigurations); $k++ ) {
  67. if ( $nextBox == $allowedConfigurations[$k] ) {
  68. //configuration is allowed
  69. $boxes[$j] = $nextBox;
  70. $configurationIsAllowed = true;
  71. break;
  72. }
  73. }
  74. if ( $configurationIsAllowed ) {
  75. break;
  76. }
  77.  
  78. }
  79.  
  80. if ( !$configurationIsAllowed ) {
  81. //configuration is not allowed add to set of boxes.
  82. //open a new box and place 375 ml bottle inside.
  83. array_push( $boxes, array(0, 1, 0) );
  84. }
  85.  
  86. }
  87.  
  88. //Now process small bottles.
  89. for ( $i=$numberOf200mlBottles; $i>0; $i-- ) {
  90.  
  91. $configurationIsAllowed = false;
  92.  
  93. for ( $j=0; $j<count($boxes); $j++ ) {
  94.  
  95. $nextBox = array( $boxes[$j][0], $boxes[$j][1], ($boxes[$j][2]+1) );
  96.  
  97. for ( $k=0; $k<count($allowedConfigurations); $k++ ) {
  98. if ( $nextBox == $allowedConfigurations[$k] ) {
  99. //configuration is allowed
  100. $boxes[$j] = $nextBox;
  101. $configurationIsAllowed = true;
  102. break;
  103. }
  104. }
  105. if ( $configurationIsAllowed ) {
  106. break;
  107. }
  108.  
  109. }
  110.  
  111. if ( !$configurationIsAllowed ) {
  112. //configuration is not allowed add to set of boxes.
  113. //open a new box and place 750 ml bottle inside.
  114. array_push( $boxes, array(0, 0, 1) );
  115. }
  116. }
  117.  
  118. //you need to compute the cost of the configurations.
  119. $shippingCost = 0.0;
  120. for ( $i=0; $i<count($boxes); $i++ ) {
  121. for ( $j=0; $j<count($configCosts); $j++ ) {
  122. if ( $boxes[$i] == array( $configCosts[$j][0], $configCosts[$j][1], $configCosts[$j][2] ) ) {
  123. $shippingCost += $configCosts[$j][3];
  124. break;
  125. }
  126.  
  127. }
  128. }
  129.  
  130. $resultArray = array( $boxes, $shippingCost );
  131. return $resultArray;
  132.  
  133. }
  134.  
  135. // now use the function to compute the configuration of a set of z 750 ml, y 375 ml, and x 200 ml bottles.
  136. // e.g. 399 750 ml bottles and 121 200 ml bottles.
  137. $boxSet = MinimizeUSPSCost( 399, 0, 121 );
  138.  
  139. for ( $i=0; $i<count($boxSet[0]); $i++ ) {
  140. echo "Box " . ($i+1) . ": " . $boxSet[0][$i][0] . " 750ml bottles, " . $boxSet[0][$i][1] . " 375 ml bottles, " . $boxSet[0][$i][2] . " 200 ml bottles\
  141. .\n";
  142. }
  143. echo "Total Shipping cost = " . $boxSet[1] . "\n";
  144.  
  145. ?>

Report this snippet  

You need to login to post a comment.