-1

I am trying to make an algorythm to solve the best combination of "upgrades" in a game based on input. These are the variables:

'imp_total': '30', # This says how many in total the other variables can be combined
'imp_coalpower': '0', 
'imp_oilpower': '0', 
'imp_nuclearpower': '1', 
'imp_windpower': '0', 
'imp_coalmine': '0', 
'imp_oilwell': '0', 
'imp_ironmine': '0', 
'imp_bauxitemine': '0', 
'imp_leadmine': '0', 
'imp_uramine': '1', 
'imp_farm': '0', 
'imp_gasrefinery': '0', 
'imp_steelmill': '0', 
'imp_aluminumrefinery': '0', 
'imp_munitionsfactory': '0', 
'imp_policestation': '1', 
'imp_hospital': '1', 
'imp_recyclingcenter': '1', 
'imp_subway': '1', 
'imp_supermarket': '0', 
'imp_bank': '0', 
'imp_mall': '3', 
'imp_stadium': '3', 

# These variables are chosen by the user:
'imp_barracks': '5', 
'imp_factory': '5', 
'imp_hangar': '5', 
'imp_drydock': '3'

Based on the 'imp_total' and the variables chosen by the user I want the computer to calculate what is the "best" combination of upgrades. In this case best would mean the most income. Each upgrade has a pretty complex direct and indirect impact on revenue. The mines for example gives you resources that you can sell, but they also pollute which decreases the population of your city which can decrease the income. All the algorithms on this system is known.

The variables that affect income is Disease, Commerce, Population, Crime, pollution, etc. Different upgrades have different affects on these numbers.

What I want to know is how should I go about doing this? I have been told the Knapsack Problem is something I should look at, but I don't see how I would implement so many different numbers and inputs that all affect each other. Is that possible? Is there another algorithm i can take a look at to solve my problem?

  • Please repeat [on topic](https://stackoverflow.com/help/on-topic) and [how to ask](https://stackoverflow.com/help/how-to-ask) from the [intro tour](https://stackoverflow.com/tour). “Show me how to solve this coding problem” is not a Stack Overflow issue. We expect you to make an honest attempt, and *then* ask a *specific* question about your algorithm or technique. Stack Overflow is not intended to replace existing documentation and tutorials. Asking for recommendations or references is *specifically* listed as off-topic. – Prune Feb 24 '21 at 21:39
  • This is, indeed, a version of the Knapsack Problem. The classic version presents only three factors: mass, volume, and value. You simply have several factors instead of merely the mass-volume pair. Whatever you find for two factors, you have to generalize to four. You also have a more complex value function. – Prune Feb 24 '21 at 21:42
  • The only part I see that *might* take this beyond a straightforward knapsack solution, is if the upgrade values are *not* independent. In that case, you need to consider the value of a proposed "packing", rather than independent upgrades. You don't specify that detail. – Prune Feb 24 '21 at 21:44

1 Answers1

0

If your "best" combination can be expressed as a set of constraints, you may want to look at linear programming (perhaps the simplex method) so solve it mathematically.

Otherwise you could try to convert the notion of "best" into a number (or tuple) that allows comparison between combinations and use that as the key in a max() call over combinations.

If there are too many combinations, you will probably need to use dynamic programming to optimize the search for a best combination (typically in a recursive traversal, possibly with memoization, i.e the Knapsack problem)

Alain T.
  • 40,517
  • 4
  • 31
  • 51