Alright lets start with safe cuts. To make this work we're going to add the detail that the housing supports 200 pop. We only need 100 so this will make houses safe to reduce.
This makes our supplies/costs look like this: 100 pop. Oil rigs trying to draw 50 electricity, 25 pop, produce 100 oil. Oil refineries trying to draw 50 electricity, 25 pop, 50 oil, produce 50 fuel. Oil powerplants trying to draw 25 pop, 50 fuel, produce 200 electricity. Houses trying to draw 150 electricity, produce 200 pop support. (Note: houses produce population support, not population itself.)
Total supplies: 100 pop, 100 oil, 50 fuel, 200 electricity, 200 pop support.
Total costs: 75 pop, 50 oil, 50 fuel, 250 electricity.
We see that electricity cost is over supply by 50 so it needs to go onto the limited resource list. but we need an algorithm to identify resources we can get rid off without any issues. At this point it's easier to try to work with the numbers then make the algorithm to fit than to make an algorithm So we're going to do this in reverse, first the "do this do that" and later the pseudo code.
Total draw 250, supply 200 = 50 unit overage
Oil rigs, Oil refineries, Housing using electricity
Oil rigs are using 50 to produce 100 oil, only 50 oil needed can be cut up to 50%
Oil refineries are using 50 to produce 50 fuel, 50 needed cannot be cut.
Housing are using 150 to produce 200 pop support, only 100 needed, can be cut up to 50%.
At this point we have our safe cut list (Oil Rigs, 50%, Housing 50%) in this case both can be cut 50% but that won't always be true, we'll have to examine that case later with any algorithm we make to ensure it works properly. Also we have to examine the case where we need to cut 50%, one can cut 75% and one can only cut 25%. This is where the algorithm will get -really- interesting. Algorithm design like this is essentially taking a task (distribute resources) and breaking it down into sub tasks, then doing the same thing over and over again until you're sub tasks are simple enough to convert to code easily.
Our safe cut list is drawing 200, we need to cut 50 which is 25%.
Oil rigs can be cut by 25% do so (electricity cost reduced by 12.5 to 237.5, oil production reduced by 25 to 75)
Housing can be cut by 25% do so (electricity cost reduced by 37.5 to 200, pop support reduced by 50 to 150)
Electricity cost is within supply. Cave Johnson, we're done here.
So we've solved this case, we need to examine others before we know that our algorithm will work, or even exactly what it is for that matter. Lets start with the uneven unneeded %'s. Lets say housing only produces 150 pop support. Now the above looks like this (NOTE: both code sections above combined here):
Total draw 250, supply 200 = 50 unit overage
Oil rigs, Oil refineries, Housing using electricity
Oil rigs are using 50 to produce 100 oil, only 50 oil needed can be cut up to 50%
Oil refineries are using 50 to produce 50 fuel, 50 needed cannot be cut.
Housing are using 150 to produce 150 pop support, only 100 needed, can be cut up to 33.3...% (NOTE: because of how number are handled in computing you probably don't want to store this % and retrieve it later as it may be slightly wrong. Look up or ask me about floating point computing for details.)
Our safe cut list is drawing 200, we need to cut 50 which is 25%.
Oil rigs can be cut by 25% do so (electricity cost reduced by 12.5 to 237.5, oil production reduced by 25 to 75)
Housing can be cut by 25% do so (electricity cost reduced by 37.5 to 200, pop support reduced by 37.5 to 112.5)
Electricity cost is within supply. Cave Johnson, we're done here.
Alright that works fine, now lets change it up a little more. Our goal is to cause the plan to fail so lets modify housing again. If we reduce housing pop support to 125. That should be just low enough to screw up the algorithm.
Alright lets start with safe cuts. To make this work we're going to add the detail that the housing supports 200 pop. We only need 100 so this will make houses safe to reduce.
This makes our supplies/costs look like this: 100 pop. Oil rigs trying to draw 50 electricity, 25 pop, produce 100 oil. Oil refineries trying to draw 50 electricity, 25 pop, 50 oil, produce 50 fuel. Oil powerplants trying to draw 25 pop, 50 fuel, produce 200 electricity. Houses trying to draw 150 electricity, produce 200 pop support. (Not houses produce population support, not population itself.)
Total supplies: 100 pop, 100 oil, 50 fuel, 200 electricity, 200 pop support.
Total costs: 75 pop, 50 oil, 50 fuel, 250 electricity.
We see that electricity cost is over supply by 50 so it needs to go onto the limited resource list. but we need an algorithm to identify resources we can get rid off without any issues. At this point it's easier to try to work with the numbers then make the algorithm to fit than to make an algorithm So we're going to do this in reverse, first the "do this do that" and later the pseudo code.
Total draw 250, supply 200 = 50 unit overage
Oil rigs, Oil refineries, Housing using electricity
Oil rigs are using 50 to produce 100 oil, only 50 oil needed can be cut up to 50%
Oil refineries are using 50 to produce 50 fuel, 50 needed cannot be cut.
Housing are using 150 to produce 200 pop support, only 100 needed, can be cut up to 50%.
At this point we have our safe cut list (Oil Rigs, 50%, Housing 50%) in this case both can be cut 50% but that won't always be true, we'll have to examine that case later with any algorithm we make to ensure it works properly. Also we have to examine the case where we need to cut 50%, one can cut 75% and one can only cut 25%. This is where the algorithm will get -really- interesting. Algorithm design like this is essentially taking a task (distribute resources) and breaking it down into sub tasks, then doing the same thing over and over again until you're sub tasks are simple enough to convert to code easily.
Our safe cut list is drawing 200, we need to cut 50 which is 25%.
Oil rigs can be cut by 25% do so (electricity cost reduced by 12.5 to 237.5, oil production reduced by 25 to 75)
Housing can be cut by 25% do so (electricity cost reduced by 37.5 to 200, pop support reduced by 50 to 150)
Electricity cost is within supply. Cave Johnson, we're done here.
So we've solved this case, we need to examine others before we know that our algorithm will work, or even exactly what it is for that matter. Lets start with the uneven unneeded %'s. Lets say housing only produces 150 pop support. Now the above looks like this (NOTE: both code sections above combined here):
Total draw 250, supply 200 = 50 unit overage
Oil rigs, Oil refineries, Housing using electricity
Oil rigs are using 50 to produce 100 oil, only 50 oil needed can be cut up to 50%
Oil refineries are using 50 to produce 50 fuel, 50 needed cannot be cut.
Housing are using 150 to produce 125 pop support, only 100 needed, can be cut up to 20%
Our safe cut list is drawing 200, we need to cut 50 which is 25%.
Oil rigs can be cut by 25% do so (electricity cost reduced by 12.5 to 237.5, oil production reduced by 25 to 75)
Housing cannot be cut by 25%, only 20% do so (electricity cost reduced by 30 to 207.5, pop support reduced by 25 to 100)
Alright we've broken it. It's not solved the problem and we know the problem is solvable (we need to cut 7.5, there is 12.5 left to cut from the rigs. Two possible solutions here: A. Cycle the algorithm as long as there is uncut unneeded resources and excess draw, or B. Change the algorithm so it never reaches that point. A. is easier. B. is more fun. Another thing to keep in mind is execution time. In our case it is irrelevant but in the real world you have to compare your options and see which is faster. In a case like this sometimes A will be faster, Sometimes B will be faster. I can tell you from looking and experience that B will be faster in any case that would cause A to loop and A will be faster in any case that it completes in 1 pass. To determine the "best" of the two you would have to analyze which case occurred more often and the performance profiles of each. Lots of work. Sometimes you can actually end up with a option C. Calculate if A or B is faster, run faster one. That only works if the savings of choosing the best case is more than the cost of figuring out which is best.
In this case, we're going to go with option B. Because I enjoy solving problems and B is more difficult than A.
A potential solution is to take our list of things we're cutting to meet supply and sort it by how much we can cut before we go through, and then (potentially) recalculate the cut percentage at each step which would then look like this:
Total draw 250, supply 200 = 50 unit overage
Oil rigs, Oil refineries, Housing using electricity
Oil rigs are using 50 to produce 100 oil, only 50 oil needed can be cut up to 50%
Oil refineries are using 50 to produce 50 fuel, 50 needed cannot be cut.
Housing are using 150 to produce 125 pop support, only 100 needed, can be cut up to 20%
Our safe cut list is drawing 200, we need to cut 50 which is 25%.
Sort cut order by percentage( housing, then oil)
Housing cannot be cut by 25%, only 20% do so (electricity cost reduced by 30 to 220, pop support reduced by 25 to 100)
As this didn't meet cut percentage, recalculate cut percentage for remaining items.
current safe cut list is drawing 50, we need to cut 20 which is 40%.
Oil rigs can be cut by 40% do so (electricity cost reduced by 20 to 200, oil production reduced by 40 to 60)
Electricity draw is within supply, we're done here.
To confirm that our system produced the correct result lets examine our final numbers: 100 pop. Oil rigs trying to draw 30 electricity, 15 pop, produce 60 oil. Oil refineries trying to draw 50 electricity, 25 pop, 50 oil, produce 50 fuel. Oil powerplants trying to draw 25 pop, 50 fuel, produce 200 electricity. Houses trying to draw 120 electricity, produce 100 pop support. (Not houses produce population support, not population itself.)
Total supplies: 100 pop, 60 oil, 50 fuel, 200 electricity, 100 pop support.
Total costs: 65 pop, 50 oil, 50 fuel, 200 electricity.
We've done it!
Now people are dragging me to dinner so... Next post: Pseudo coding that, examining situation where cuts are not do able, identifying the situation and moving to forgone conclusion (total shutdown).