0

Hi I am trying to solve the following problem:

I have multiple category sets of food A,B,..., each with nutritional information encoded in a vector x_a1,x_a2,...,x_b1,x_b2,.... I also have a vector, y, which encodes the target nutrition values. I want to select a combination of foods from the food sets such that the difference between the target nutrition y and the sum of the selected food nutrition vectors is minimized:

min_x ||x−y||_2

where x is the element-wise sum of all selected foods

x = ∑_i(x_{ij})

for i∈{A,B,C,...} and j∈{1,2,3,...}.

To solve this with linear programming I formulated it in the following way - Let Z be a m by n matrix for n foods and m nutrients and w_j is a vector of binary values. I am trying to minimize the L1 norm:

sum_i | sum_j(Z_{ij}w_j) - y |

linearly by introducing slack and nonnegative surplus variables t_i and s_i, respectively:

min sum_i(s_i + t_i)
s.t. sum_j(Z_{ij}w_j) - s_i + t_i = y for all i
     sum_j(w_j) >= 1 for j∈{A,B,C,...}

Can someone point me in the direction for how I would formulate this in scipy?

Theory94
  • 149
  • 1
  • 3
  • 12

1 Answers1

1

scipy uses as interface:

     min c'x
     Ax=b
     Dx<=e
     l <= x <= u

You have to shoehorn your problem into this format. This step is not always easy (not too difficult for your problem, however). If you want to use something that is closer to your equations, use a tool like PuLP or Pyomo. CVXPY is also a possibility.

When using SCIPY it is a good idea to get a piece of paper and draw the layout of the matrices. The columns are the variables and the rows are the equations. Something like:

        w(j)     s(i)     t(i)     rhs

 obj     0        1        1

 A       Z       -I        +I       y

 D      -1        0        0       -1 


 lo     -inf      0        0
 up     inf       inf      inf   
Erwin Kalvelagen
  • 15,677
  • 2
  • 14
  • 39