title: multi-capacities Knapsack in CPLEX
body: <p>I came cross Knapsack problem, where the maximum number of multiple items from a set of items need to be placed into one bin by minimizing the cost. I am able to solve the optimization problem in CPLEX. </p>
  
  <p>However, I am finding difficulties in implementing in CPLEX, when the problem consists of two bins (with different capacities).</p>
  
  <p>The problem:</p>
  
  <pre><code>Bin = [B1, B2] 
  Capacity = [7,5]
  
  Item = [I1, I2, I3, I4]
  Weight = [6,3,1,4]
  Price = [2,8,2,4]
  </code></pre>
  
  <p>The objective is to place the maximum number of items and to minimize the total price. </p>
  
  <p>How can I implement this objective problem in CPLEX?</p>
  
  <p>Below is my code snippet:</p>
  
  <pre><code>// ITEMS
  int n=4; // no of items
  range items = 1..n; // range of items
  int p[items] = [2,8,2,6]; //price
  int w[items] = [6,3,1,4]; //weight
  
  // BINS
  int m=2; // no of bins
  range bins=1..m; // range of bin
  int capacity[bins] = [7,5]; // capacity of each bin
  
  dvar boolean x[items][bins]; 
  
  // model ; max the profit
  maximize sum(i in items, j in bins) p[i]*x[i][j];
  
  subject to {
      forall (j in bins)
          cons1 : sum(i in items) w[i]*x[i][j] &lt;= capacity[j];
      forall (i in items)
          cons2 : sum(j in bins) x[i][j] == 1;    
  } 
  </code></pre>
  
  <p>-Thanks</p>