Tuesday, October 12, 2010

solving the packaging problem in PHP

When, I was developing a shipping software based on Fedex ans UPS APIs, I got an issue with packagaging. It was, the packing the products in an optimised way. there are few methods to solve this issues in mathematics. Here's a program which can be used to solve the packaging problem.



   // setting up Max value for each contatiner

    DEFINE('MAX_VALUE',40);

   //   different set of array values for testing

    //$input = array(18,13,33,7,26,39,16,5);
    //$input = array(15,28,19,21,4,36,12,15);

    $input = array(1,2,3,4,5,30,6,7,8,9,10,1,2,3,4,5,30,6,7,8,9,10);

    //$input = array(15,15,15,15,15,10,10,25);

    $expected_containers = ceil(array_sum($input)/MAX_VALUE);
    $no_of_items = count($input);
    rsort($input);

    for($i = 0; $i < $no_of_items; $i++)
   {
        if(isset($input[$i]))
       {
            $remainder = MAX_VALUE - $input[$i];
            for($j = $i + 1; $j <= $no_of_items; $j++)
           {
                if(isset($input[$j]) && $input[$j] <= $remainder)
                {
                    if(!isset($container[$i]))
                   {
                        $container[$i] = array($input[$i],$input[$j]);
                    }
                     else
                    {
                        array_push($container[$i],$input[$j]);
                    }
                    $remainder = $remainder - $input[$j];
                    unset($input[$j]);
                }
                if(!isset($container[$i]))
                {
                    $container[$i] = array($input[$i]);
                }
            }
        }
    }

    echo "Expected containers ".$expected_containers."
";
    echo "Actual containers ".count($container)."
";
    echo '
';

    print_r($container);

    foreach($container as $val)
   {
 $productsum = array_sum($val);
 echo  "
".$productsum;
   } 

   echo '
';
?>




seems helpful someone, who is developing a shipping software.

@credits Praveesh

1 comment:

  1. Hi,

    I have a container. In that container i have to
    fit different size boxes. Container will have (length X height X width).
    as same for box also. Now my problem is once fitting the first canvas.
    it should go for second canvas...like wise until filling the container
    according to container height. This is like shipping container.

    Now i am able fill the container with multiple canvas irrespective of container height. My problem is i have to fit the boxes in container with respective of container height. Kindly help me on this. Please..

    ReplyDelete

Please put your comments here. your questions, your suggestions, also what went wrong with me.