I have widgets and a single widget will have attributes of:
Name
Weight (decimal from 0-1)
Group (letter A-F)
Price (an integer from 1 - 100)
I must pick one widget from each Group (A-F) for a total of 6 widgets. How do I write an algorithm to find the 6 widgets that give me the aggregate highest Price while at the same time keeping the aggregate Weight less-than or equal to 1? Obviously, a lot of combinations will have an aggregate weight less than 1, but I need to come up with a way to get the combination that ALSO results in the highest Price. I suppose I could solve this with a Monte Carlo simulation, but I'm hoping there is a better way.
This is somewhat reminiscent of the "Knapsack problem". As we know, it is NP-complete, so no algorithm exists to solve it fast. I am not going to try to prove that this problem is NP-complete, but it certainly looks that way.
You can however solve it in exponential time like this:
Generate all combinations (this will be $n_A * n_B * \cdots * n_F$ combinations where $n_A$ i s no. of elements in $A$ and so on)
sort combinations by aggregate price
pick the first combination with weight $\leq 1$
Edit: This is in fact the equivalent of the so-called "Multiple choice Knapsack problem". There seems to exist $O(n)$-time algorithms for this using linear programming. You can read about some of their implementations in this paper by David Pisinger: link