In this exercise, you'll be asked to implement several variants of the famous Knapsack problem in knapsack.py.
The most popular variant of this problem is the
Sometimes the key to solving a real-world problem is to identify the well-known classic problem that represents them.
Here are some examples where knapsack could be applied:
- Loading cargo in airplanes given their cost and weight
- Choosing which ads to display on a webpage, given revenue and size constraints.
- Maximizing portfolio revenue given risk and diversification constraints
Let us start with a simplified version of the classical knapsack, where instead of choosing which items to pick, we must choose which amount to pick.
Mathematically, the variable bounds go from
Your task: Formulate the problem described above with continuous variables in knapsack.py, method linear_knapsack().
Hint 1
The variable bounds can be set during variable creation with the optionslb and ub, for lower bound and upper bound, respectively.
In many scenarios, we are more interested in the binary variant of the knapsack problem.
Your task: Enforce integrality on the variables, in the method binary_knapsack().
Hint 1
You can set the variable type during variable creation with thevtype option.
Your task: Allow the possibility for choosing multiple copies of the same item, in the method integer_knapsack().
Hint 1
Instead of the variables being binary, they can just be integer.Your task: Force the solution from integer knapsack to have at most n items, in the method limited_knapsack().
