The problem
I have $1000 of money. With it I can buy 3 items:
- Item 1 which costs 45 dollars.
- Item 2 which costs 75 dollars.
- Item 3 which costs 25 dollars.
I want to know the optimal amount of each item to buy to come as close as possible to the following statements :
75% of the money I spend has been allocated to Item 1
20% of the money I spend has been allocated to Item 2
5% of the money I spend has been allocated to Item 3
There is one strong rule: As long as there is enough money left to buy one of the items, I have to continue buying.
Example :
What I mean with 'as close as possible to the following statements'
Solution 1:
80% Item 1
20% Item 2
0% Item 3
sum of percentage differences = (80-75) + (20-20) + (5-0) = 10%
Solution 2:
- 90% Item 1
- 5% Item 2
- 5% Item 3
sum of percentage differences = (90-75) + (20-5) + (5-5) = 30%
The better solution is Solution 1 (because 10% is a smaller percentage difference than 30%)
If you only bought Item 1, then you could buy $\lfloor \frac{1000}{45} \rfloor = 22$ of them. Similarly, you could buy at most $\lfloor \frac{1000}{75} \rfloor = 13$ of Item 2, or $\lfloor \frac{1000}{25} \rfloor = 40$ of Item 3. This gives us a maximum search space of $23 \times 14 \times 41 = 13202$ combinations (adding 1 to each factor because 0 is a valid quantity), amenable to brute-force search by computer. So let's just do that.
Running the program shows a unique optimal solution of $(16, 3, 2)$, i.e.: