title: Solve Multiple Choice Knapsack (MCKP) With Dynamic Programming?
body: <h1>Example Data</h1>
  
  <p>For this question, let's assume the following items:</p>
  
  <ul>
  <li>Items: Apple, Banana, Carrot, Steak, Onion</li>
  <li>Values: 2, 2, 4, 5, 3</li>
  <li>Weights: 3, 1, 3, 4, 2</li>
  <li>Max Weight: 7</li>
  </ul>
  
  <h1>Objective:</h1>
  
  <p>The MCKP is a type of <a href="https://en.wikipedia.org/wiki/Knapsack_problem" rel="nofollow noreferrer">Knapsack Problem</a> with the additional constraint that "[T]he items are subdivided into <strong>k classes</strong>... <strong>and exactly one item must be taken from each class</strong>"</p>
  
  <p>I have written the code to solve the 0/1 KS problem with dynamic programming using recursive calls and memoization.  My question is whether it is possible to add this constraint to my current solution? Say my classes are Fruit, Vegetables, Meat (from the example), I would need to include 1 of each type.  The classes could just as well be type 1, 2, 3.</p>
  
  <p>Also, I think this can be solved with linear programming and a solver, but if possible, I'd like to understand the answer here.</p>
  
  <h1>Current Code:</h1>
  
  <pre><code>&lt;?php
  $value = array(2, 2, 4, 5, 3);
  $weight = array(3, 1, 3, 4, 2);
  
  $maxWeight = 7;
  $maxItems = 5;
  
  $seen = array(array()); //2D array for memoization
  $picked = array();
  
  //Put a dummy zero at the front to make things easier later.
  array_unshift($value, 0);
  array_unshift($weight, 0);
  
  //Call our Knapsack Solver and return the sum value of optimal set
  $KSResult = KSTest($maxItems, $maxWeight, $value, $weight);
  $maxValue = $KSResult; //copy the result so we can recreate the table
  
  //Recreate the decision table from our memo array to determine what items were picked
  //Here I am building the table backwards because I know the optimal value will be at the end
  for($i=$maxItems; $i &gt; 0; $i--) {
          for($j=$maxWeight; $j &gt; 0; $j--) {
              if($seen[$i][$j] != $seen[$i-1][$j]
                  &amp;&amp; $maxValue == $seen[$i][$j]) {
                  array_push($picked, $i);
                  $maxValue -= $value[$i];
                  break;
              }
          }
  }
  
  //Print out picked items and max value
  print("&lt;pre&gt;".print_r($picked,true)."&lt;/pre&gt;");
  echo $KSResult;
  
  
  //  Recursive formula to solve the KS Problem
  //  $n = number of items to check
  //  $c = total capacity of bag
  function KSTest($n, $c, &amp;$value, &amp;$weight) {
      global $seen;
  
      if(isset($seen[$n][$c])) {
          //We've seen this subproblem before
          return $seen[$n][$c];
      }
      if($n === 0 || $c === 0){
          //No more items to check or no more capacity
          $result = 0;
      }
      elseif($weight[$n] &gt; $c) {
          //This item is too heavy, check next item without this one
          $result = KSTest($n-1, $c, $value, $weight);
      }
      else {
          //Take the higher result of keeping or not keeping the item
          $tempVal1 = KSTest($n-1, $c, $value, $weight);
          $tempVal2 = $value[$n] + KSTest($n-1, $c-$weight[$n], $value, $weight);
  
          if($tempVal2 &gt;= $tempVal1) {
              $result = $tempVal2;
              //some conditions could go here? otherwise use max()
          }
          else {
              $result = $tempVal1;
          }
      }
      //memo the results and return
      $seen[$n][$c] = $result;
      return $result;
  }
  ?&gt;
  </code></pre>
  
  <h1>What I've Tried:</h1>
  
  <ol>
  <li>My first thought was to add a class (k) array, sort the items via class (k), and when we choose to select an item that is the same as the next item, check if it's better to keep the current item or the item without the next item.  Seemed promising, but fell apart after a couple of items being checked. Something like this: 
  $tempVal3 = $value[$n] + KSTest($n-2, $c-$weight[$n]);
  max( $tempVal2, $tempVal3);</li>
  <li>Another thought is that at the function call, I could call a loop for each class type and solve the KS with only 1 item at a time of that type + the rest of the values.  This will definitely be making some assumptions thought because the results of set 1 might still be assuming multiples of set 2, for example.</li>
  </ol>
  
  <p><a href="https://nickgavalas.com/solving-the-multiple-choice-knapsack-problem/" rel="nofollow noreferrer">This looks to be the equation</a> (If you are good at reading all those symbols?) :) and a C++ implementation? but I can't really see where the class constraint is happening?</p>