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?
g[1:10]which identifies the special group, thena[5row, 10col]which tells me how many items were taken from each group. I tried creating a new variableb[1:5](max # of items) to try[i=1:5], b[i] <= sum(g[j] * a[i,j] for j in 1:10)– Eddie Nov 19 '21 at 02:00aby whether it's the special group ingand storing that inbto calculate the bonus. This introduces a quadratic constraint which is not allowed in my solver. How would you go about this problem? I'm new to all this so sorry for the poor syntax. – Eddie Nov 19 '21 at 02:00