Solutions

Solve the exercises about linear and integer programming.

Exercise 1: Fractional knapsack

In the fractional knapsack problem, we don’t have to choose the entire item, but we can get a part of it. That way, we aren’t solving an integer problem anymore. The constraints are the same, but the solution is different from the original knapsack problem. In the original version, a solution is a zero or a one for each item, representing whether we take the item or not. Now a solution is a number between zero and one for each item, representing the fraction of that item we take. So the problem can be written as follows:

maxxvxs.t.:wxw0xi1\max_{\mathbf{x}} \mathbf{v}\mathbf{x}^\top\\ s.t.: \mathbf{w}\mathbf{x}^\top \leq w\\ 0 \leq x_i \leq 1

Let’s see the code:

Get hands-on with 1400+ tech skills courses.