Advertisement
Guest User

Untitled

a guest
Jan 16th, 2012
473
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.25 KB | None | 0 0
  1. <?php
  2.  
  3. // KNAPSACK BALANCER!
  4.  
  5. class Balancer
  6. {
  7.     public static function balance($items, $key)
  8.     {
  9.         $result = array();
  10.         $maxWeight = floor(self::sum($items, $key) / 2);
  11.         $numItems = count($items);
  12.        
  13.         $sack = self::buildSack($numItems, $maxWeight);
  14.        
  15.         for ($n = 1; $n <= $numItems; $n++)
  16.         {
  17.             // loop all items
  18.             for ($weight = 1; $weight <= $maxWeight; $weight++)
  19.             {
  20.                 $a = $sack[$n - 1][$weight]['value'];
  21.                 $b = null;
  22.                 $value = $items[$n - 1][$key];
  23.                 if ($value <= $weight)
  24.                 {
  25.                     $b = $value + $sack[$n - 1][$weight - $value]['value'];
  26.                 }
  27.                 $sack[$n][$weight]['value'] = ($b === null ? $a : max($a, $b));
  28.                 $sack[$n][$weight]['take'] = ($b === null ? false : $b > $a);
  29.             }
  30.         }
  31.        
  32.         $setA = array();
  33.         $setB = array();
  34.        
  35.         for ($n = $numItems, $weight = $maxWeight; $n > 0; $n--)
  36.         {
  37.             $item = $items[$n - 1];
  38.             $value = $item[$key];
  39.             if ($sack[$n][$weight]['take'])
  40.             {
  41.                 $setA[] = $item;
  42.                 $weight = $weight - $value;
  43.             }
  44.             else
  45.             {
  46.                 $setB[] = $item;
  47.             }
  48.         }
  49.        
  50.         return array($setA, $setB);
  51.     }
  52.    
  53.     protected static function sum($items, $key)
  54.     {
  55.         $sum = 0;
  56.         foreach ($items as $item)
  57.         {
  58.             $sum += $item[$key];
  59.         }
  60.         return $sum;
  61.     }
  62.    
  63.     protected static function buildSack($width, $height)
  64.     {
  65.         $sack = array();
  66.         for ($x = 0; $x <= $width; $x++)
  67.         {
  68.             $sack[$x] = array();
  69.             for ($y = 0; $y <= $height; $y++) {
  70.                 $sack[$x][$y] = array(
  71.                     'value' => 0,
  72.                     'take' => false
  73.                 );
  74.             }
  75.         }
  76.         return $sack;
  77.     }
  78. }
  79.  
  80. $players = array(
  81.     array('id' => 1, 'skill' => 1000),
  82.     array('id' => 2, 'skill' => 1010),
  83.     array('id' => 3, 'skill' => 1035),
  84.     array('id' => 4, 'skill' => 1142),
  85.     array('id' => 5, 'skill' => 1300),
  86.     array('id' => 6, 'skill' => 900),
  87.     array('id' => 7, 'skill' => 850),
  88.     array('id' => 8, 'skill' => 1231),
  89.     array('id' => 9, 'skill' => 1135),
  90.     array('id' => 10, 'skill' => 1592)
  91. );
  92.  
  93. $t = microtime(true);
  94. $sets = Balancer::balance($players, 'skill');
  95.  
  96. $setA = $sets[0];
  97. $setB = $sets[1];
  98.  
  99. $sum = 0;
  100. foreach ($setA as $player)
  101. {
  102.     $sum += $player['skill'];
  103. }
  104. echo 't: '.$sum."\r\n";
  105.  
  106. $sum = 0;
  107. foreach ($setB as $player)
  108. {
  109.     $sum += $player['skill'];
  110. }
  111. echo 'ct: '.$sum."\r\n";
  112.  
  113. //print_r($sets);
  114. var_dump(microtime(true) - $t);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement