I am trying to create an LP problem which is like the knapsack problem but where there is a fixed bonus/penalty based on the number of items chosen.
- There are 20 items to choose from with some weight between 1 to 20.
- Knapsack can hold a total weight W=40
- If only 1 item is chosen, there is a penalty of -3
- If 2 items are chosen, there is a bonus of 2
- If 3 items are chosen, there is a bonus of 1.5
- If 4 items are chosen, there is a bonus of 6
- If 5 or more items are chosen, there is a penalty of -4
While it seems like a simple problem, the bonus/penalties are constantly changing, and there are times that the bonus for 1 item is so high that it's the solution. My objective function is to maximize the total weight and the bonus/penalty associated with the number of items chosen.
I tried creating a Dictionary (call it dict) for my bonus/penalties and then having my objective function be sum(items[i]*weights[i] for i in 1:20) + dict[sum(items[i] for i in 1:20)]
. The second part, the dictionary lookup, does not work and gives me an error so I must be doing something wrong.
Are dictionary lookups valid in an LP problem? Do I need to convert this to something else?