r/algorithms • u/SlayMaster3000 • Nov 28 '21
[Help] Given some input, find the optimal weightings for edges of a graph to maximize some output.
I have a directed graph that describes the relationship between items (nodes) via recipes (edges). Simple example of a recipe: 1 Iron Ore => 1 Iron Ingot.
I want to find a weighting for each recipe (that is to say the number of times each recipe should be applied) such that given some starting number of items, it produces the maximum amount of a specified item.
How can I go about finding this weighting for each recipe?
Note: All weightings must be non-negative (they can be decimal). No weightings can result in more input being required than the amount available.
That's the main problem I'm trying to solve, but following that, the next thing I want to then solve is taking energy usage into account. Each recipe will either use some amount of energy or produce some amount of energy.
How can I ensure that when finding the weightings, the energy production minus the energy consumption is non-negative?
Thanks in advance for any advice :)
1
u/MyOthrUsrnmIsABook Nov 29 '21
Can a single output item have multiple recipes? Will you need to potentially try different recipe choices (like set edge weights to 0 for non-optimal recipes) to maximize your outputs?
1
u/RealJulleNaaiers Nov 29 '21
Trying to solve Satisfactory, eh? I'm not sure it's possible to solve in a reasonable timeframe due to all the alternate recipes and the possibility of producing the same product from multiple recipes.
It's relatively straightforward if you can pre-select all of the recipes in advance such that none produce duplicate products (because recipes may take up to 4 inputs and produce up to 2 products). But if you start having to select between multiple, it gets really messy. And I suspect the problem you're actually trying to solve isn't "how many iron ingots can I theoretically produce?" (which is easy), but is something like "what is the max number of turbomotors I can theoretically produce?"
An optimal answer to that question will definitely involve at least considering cyclical graphs (recycled rubber/plastic, aluminum), and you'll start to run into the limits of scarce resources like sulphur or bauxite, meaning your algorithm will likely need to select multiple recipes that produce the same product in order to make optional usage of your resources.
Curious to hear other people's thoughts on this
1
u/RealJulleNaaiers Nov 29 '21
Oh, something that might help that you might have already found: there's a file in the game files somewhere called Docs.json that contains a json blob describing every item, building, and recipe in the game. You can parse it in an automated fashion to avoid hand entering all of the recipes and everything into your calculation.
1
u/ragusa12 Nov 29 '21 edited Nov 29 '21
I think you can solve the problem with linear programming. I see that you have tried this in this other comment, however, I think you are a bit confused about what you are optimising. You are looking to optimise the amount of some item and your parameters are the number of times a recipe is used. Therefore, your linear program should probably look something like this:
Now, this solution cannot actually solve the problem, as it can only deal with direct recipes, i.e. resulting immediately in the desired item. You may be able to get around this by computing the transitive closure of the recipes, e.g. if you have a recipe1 ore + 1 water -> 1 refined ore + 1 copperand1 refined ore -> 1 ingotthen you that the recipe1 ore + 1 water -> 1 ingot + 1 copper. However, this will definitely explode if there are many different ways to end up making ingots.# Edit Never mind, maybe this is actually not a problem. You can of course just add these recipes to the constraints, as shown here:
Btw, I used the formatting of this online tool.