title: How to solve a facility location allocation (IP) problem in CVXPY
body: <p>I am learning to solve optimization problems using CVXPY, so I started with the following simple facility location allocation problem.</p>
  
  <p><a href="https://i.stack.imgur.com/lxkOK.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/lxkOK.png" alt="enter image description here"></a></p>
  
  <p>The Code in CVXPY is given as:</p>
  
  <pre><code>Fi  =   np.array([1,1,1]) # Fixed cost of each facility
  Ci  =   np.array([15, 10, 10]) # Capacity of each facility
  Dj  =   np.array([5, 5, 5, 3, 3, 4]) # Demand of each facility
  Cij =   np.ones(m,n)
  
  
  n   = len(Dj)
  m   = len(Fi)
  
  
  # Decision Variables
  Xij = cvx.Bool(m,n)   # (m,n) vector
  Yi = cvx.Bool(m)     # column vector of length (m,1)
  
  # Objective
  fixed_cost   = cvx.sum_entries(Fi*Yi)
  var_cost     = cvx.sum_entries(Cij.T * Dj *Xij)
  total_cost   = fixed_cost + var_cost
  objective    = cvx.Minimize(total_cost)
  
  # Maximum facility locations to be selected?
  constraints.append(cvx.sum_entries(Yi)==2)
  
  # Sum of demands allocated to a facility shall be &lt;= facility capacity - 
  # Capacity Fixed Cost
  constraints.append(cvx.sum_entries(Dj * Xij.T, axis=0) &lt;= Ci*Yi)
  
  # Every demand point shall be supplied by only one facility.
  constraints.append(cvx.sum_entries(Xij, axis=1) == 1) 
  
  
  # Solve the problem
  prob = cvx.Problem(objective, constraints)
  prob.solve(solver=cvx.GLPK_MI)
  
  # Print the values
  #print("status:", prob.status)
  print("optimal value", prob.value)
  print("Selected Facility Locations", Yi.value)
  print("Assigned Nodes", Xij.value, )
  </code></pre>
  
  <p>As per the last constraint, a demand location should be supplied by only one facility, however the output of <strong>Xij.value</strong> shows wrong results. </p>
  
  <pre><code>Using CVXPY version: 0.4.10
  status: optimal
  optimal value 91.0
  Selected Facility Locations [[1.]
  [0.]
  [1.]]
  Assigned Nodes to Facility 1) [[1. 0. 0. 0. 0. 0.]]
  Assigned Nodes to Facility 2) [[1. 0. 0. 0. 0. 0.]]
  Assigned Nodes to Facility 3) [[1. 0. 0. 0. 0. 0.]]
  </code></pre>
  
  <p>The Xij.value should be something like this:</p>
  
  <pre><code>Using CVXPY version: 0.4.10
  status: optimal
  optimal value 91.0
  Selected Facility Locations [[1.]
   [1.]
   [0.]]
  Assigned Nodes to Facility 1) [[1. 1. 1. 0. 0. 0.]]
  Assigned Nodes to Facility 2) [[0. 0. 0. 1. 1. 1.]]
  Assigned Nodes to Facility 3) [[0. 0. 0. 0. 0. 0.]]
  </code></pre>
  
  <p>Which means, facility 1 and 2 are selected.
  The first three points are allocated to facility 1 and the next three to facility 2.</p>