Advertisement
tnewhook

Knapsack Wrapper

Mar 22nd, 2014
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.  
  3.  $w=array(2,4,6);
  4.     $w = array_map("round", $w);//$w needs to be integers, $v can be floats
  5.     $v=array(2.2,4.6,6.8);
  6.     $C=10;//max weight limit of knapsack
  7.     $itemCount=array();
  8.    
  9.     KnapsackWrapper($v,$w,$C);
  10.  
  11. function KnapsackWrapper($v,$w,$C){
  12. /*
  13. in - arrays of item weight & value, knapsack capacity
  14. do - call Knapsack & knapsackContents
  15. return knapsackContents result
  16. */
  17.  
  18.    
  19.     for($i=0;$i<count($C);$i++){
  20.                     $GLOBALS["itemCount"][$i]=0;
  21.     };
  22.      
  23.     $knapsackResult=Knapsack($v,$w,$C);
  24.     echo 'Knapsack is: '.$knapsackResult;
  25.     echo '<br />ITEMCOUNT<pre>';
  26.     var_dump($GLOBALS["itemCount"]);
  27.     echo '</pre>';
  28.      
  29.     echo 'elements are: <pre>';
  30.     $knapsackContentsResult=KnapsackContents($knapsackResult,$GLOBALS["itemCount"],$w,$v,$C);
  31.     var_dump($knapsackContentsResult);
  32.     echo '</pre>';
  33.    
  34.     //return array('knapsackResult'=>$knapsackResult,'knapsackContentsResult'=>$knapsackContentsResult);
  35. }
  36. //-----------------------------------
  37.                              
  38.                                     //(N = # different items)
  39.                      
  40.     function Knapsack($v,$w,$C){                        
  41.        $sol=array();
  42.        $mySol=array();
  43.        $myFinalSol=array();
  44.        $N=count($v);
  45.             /****************************************
  46.                     Base case
  47.             *************************************** */
  48.             if ($C == 0 )
  49.             return 0;
  50.        /* ==============================================
  51.                       Divide and conquer procedure
  52.        ============================================== */
  53.      
  54.             /* ---------------------------------------
  55.             Solve the appropriate smaller problems
  56.             --------------------------------------- */
  57.             for ($i=0;$i<$N;$i++){
  58.                     if($C>=$w[$i]){
  59.                             $sol[$i] = Knapsack($v,$w,$C-$w[$i]);// Knapsack capacity reduced by w[i]    
  60.                     }                                                    // because it has item i packed in
  61.                     else{$sol[$i] = 0;}        // Not enough space to  pack item i
  62.             }
  63.             /* ---------------------------------------------
  64.             Use the solutions to the smaller problems
  65.             to solve original problem
  66.             --------------------------------------------- */
  67.             for($i=0;$i<$N;$i++){
  68.                     if($C>=$w[$i]){
  69.                             $mySol[$i]=$sol[$i]+$v[$i]; // Value is increased by v[i]
  70.                     }                               // because it has item i packed in
  71.                                                                                     // it already
  72.                     else{$mySol[$i]=0;}                     // Not enough space to  pack item i
  73.             }
  74.      
  75.        /* *************************
  76.                       Find the best (maximum)
  77.                       ************************* */
  78.                     $myFinalSol = $mySol[1];
  79.                     for($i=0;$i<$N;$i++){
  80.                             if($mySol[$i]>$myFinalSol){
  81.                                     $myFinalSol = $mySol[$i];
  82.                             }
  83.                     }
  84.                     $GLOBALS["itemCount"][$C]=$myFinalSol;
  85.             return $myFinalSol;                                
  86.     };
  87.      
  88.     function KnapsackContents($total,$itemCount,$w,$v,$C){
  89.             //$total is the total value that the knapsack function found
  90.             //$itemCount is the value at each size of the knapsack
  91.             $knapsackHolds=array();
  92.             $knapsackHoldsValue=array();
  93.             end($itemCount);
  94.             $placeholder=key($itemCount);//get final key in array
  95.            
  96.             $v=array_reverse($v);//set value & weight to prefer largest
  97.             $w=array_reverse($w);
  98.             while($placeholder>0){
  99.                     for($j=0;$j<count($v);$j++){
  100.                             if($itemCount[$placeholder]-$v[$j]==$itemCount[$placeholder-$w[$j]]&&$itemCount[$placeholder-$w[$j]]>=0){//if placeholder-weight of j equals placeholder - value of j, j is part of array
  101.                                     $knapsackHolds[]=array('w'=>$w[$j],'v'=>$v[$j]);
  102.                                     $placeholder-=$w[$j];
  103.                                     break;
  104.                             };
  105.                     };
  106.             };
  107.             echo 'KnapsackContents dump: <pre>';
  108.             var_dump($knapsackHolds);
  109.             echo '</pre>';
  110.             return $knapsackHolds;
  111.     };
  112.     ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement