| 1 comments ]

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