Trying to calculate accurate % out of variables

Don't know how to use a machine? Looking for efficient setups? Stuck in a mission?
Post Reply
Crankenstein
Burner Inserter
Burner Inserter
Posts: 5
Joined: Sat Jan 13, 2024 10:25 pm
Contact:

Trying to calculate accurate % out of variables

Post by Crankenstein »

Hello math majors! I'm playing with combinators and suddenly, I had an idea. If I could measure the percentage of my storage at all times, hook up combinators to my storage, and always have the circuit network display an accurate % no matter how many times I place more storages, it will take the variables and convert it into a %.

Setting the stage to make this easier to understand, let's say I plan on making a huge storage array of petroleum, well, to start, you need a storage tank, and the tank stores 25,000 units of fluid. A simple calculation can be explained as such: 25,000/250=100%, because 25,000 fills up the entire tank. Makes sense so far. With this simple calculation, I could also do 8000/250=32%. So 8000 is 32% of 25,000. Great!

Here's where it starts to become convoluted (at least for me anyways). I have installed now 3 more tanks, and have gotten a lot more petroleum to fill them up. Let's say I have 54,000 petroleum. 54,000/250=216%, which is another way of saying 216% of 25,000, even though my total storage is now 100,000. You could argue that I could change my equation to reflect my max storage as such: 54,000/1000=54% to keep the % accurate, but that requires manual work, which is not what I want to do. I want to automate the algorithm.

After some thinking, I tried x/y=P, the problem I have is, how do I figure out what the divisor(y) should be if x is the current amount of petroleum(not the total) and P is the percentage? If the factory must grow, then so must my brain. So in this example, I will always be building more storage tanks and always have more petroleum(x). I can't really use the number of storage tanks as a constant because it's not a constant, it's a variable.

So I changed up the formula. I got rid of x and now I have A/B/y*100=P, where A represents the current storage amount and B represents the total storage amount. The game knows by default how much petroleum(A) you have if you connect it to a circuit network, and calculating B is 25,000*B, now the equation becomes A/(25,000*B)/y*100=P. So if I do 25,000/(25,000*B)/y*100=P, I'll find out that y=1, great!

But how do I calculate this for chests? So far this algorithm works with liquids, but items in chests use different stacks. Iron ore for example is a 50 stack and red wire is a 200 stack. What algorithm would work for both liquid and solid storage? I'm currently trying to solve this, since this is an interesting puzzle to solve and would introduce a universal QoL combinator setup. If you have no idea what's going on, don't worry, neither do I. Just thinking about how I'm going to break down this formula into simple equations and putting it into combinators is giving me a headache.

Thank you for reading this info dump, and don't worry, I'm having fun doing this. I'm not suffering, I promise. :) Okay, maybe I'm suffering a little bit.

User avatar
DaveMcW
Smart Inserter
Smart Inserter
Posts: 3700
Joined: Tue May 13, 2014 11:06 am
Contact:

Re: Trying to calculate accurate % out of variables

Post by DaveMcW »

Fluid automatically balances between tanks. So you just need to calculate the percentage in one tank to measure your total fluid percentage.

Crankenstein
Burner Inserter
Burner Inserter
Posts: 5
Joined: Sat Jan 13, 2024 10:25 pm
Contact:

Re: Trying to calculate accurate % out of variables

Post by Crankenstein »

DaveMcW wrote:
Sun Jan 14, 2024 3:16 am
Fluid automatically balances between tanks. So you just need to calculate the percentage in one tank to measure your total fluid percentage.
Oh of course I forget about such an important detail about this game! :oops: So yeah that just means x/250=% is all you need to do for tanks. Now to figure out stack sizes...

Crankenstein
Burner Inserter
Burner Inserter
Posts: 5
Joined: Sat Jan 13, 2024 10:25 pm
Contact:

Re: Trying to calculate accurate % out of variables

Post by Crankenstein »

So I decided to focus on just items that can be stored in chests, I made a % calculator for every item in the game. There's probably a better way to do this but I'm content with this for now. The only thing left I want to do is to have it account for x storage chests instead of just 1, I have no idea how to do that though.
Attachments
CCs on the left are grouped by stack size and act as a catalyst for the filter. CCs on the right simulate your logistics network.
CCs on the left are grouped by stack size and act as a catalyst for the filter. CCs on the right simulate your logistics network.
Calc.png (1.33 MiB) Viewed 612 times
Clac.png
Clac.png (975.88 KiB) Viewed 612 times

Tertius
Filter Inserter
Filter Inserter
Posts: 671
Joined: Fri Mar 19, 2021 5:58 pm
Contact:

Re: Trying to calculate accurate % out of variables

Post by Tertius »

Without blueprint it's difficult to deduct what you actually built. Screenshots don't show the combinator settings.

You can probably remove 7/8 of your combinators by simply wiring every combinator on the left with each other and keep only one "signal filtering" and one "% calculation" block. Since every material has its own signal channel independent from each other, everything can be computed by just one computing unit.

Crankenstein
Burner Inserter
Burner Inserter
Posts: 5
Joined: Sat Jan 13, 2024 10:25 pm
Contact:

Re: Trying to calculate accurate % out of variables

Post by Crankenstein »

Tertius wrote:
Fri Jan 19, 2024 2:29 pm
Without blueprint it's difficult to deduct what you actually built. Screenshots don't show the combinator settings.

You can probably remove 7/8 of your combinators by simply wiring every combinator on the left with each other and keep only one "signal filtering" and one "% calculation" block. Since every material has its own signal channel independent from each other, everything can be computed by just one computing unit.


If I'm understanding you correctly, I don't think that will work. I'm sorting each block by stack size, white science pack has 2000 stack size, batteries have 200 stack size and belts have 100 stack size. So for each '% calculation' block has a different calculation. White science needs to be divided by 96,000, while batteries by 9,600, and then yellow belts need to be divided by 4,800, so on and so forth. Each 'signal filtering' block takes the signals from the CCs(the CCs on the left decide what signals should stay) and multiplies that by -2 billion, then the DC takes EACH < 0 -> outputs the input, effectively filtering the signals by only allowing negative valued signals, then you add 2 billion to get your normal values back. If I combine any of the green wires, it will mess up the calculations. Feel free to try the blueprint though.

Tertius
Filter Inserter
Filter Inserter
Posts: 671
Joined: Fri Mar 19, 2021 5:58 pm
Contact:

Re: Trying to calculate accurate % out of variables

Post by Tertius »

I understand. You hardcode the corresponding stack sizes in the combinator that performs the division.
The challenge is that an universal EACH / EACH operation isn't easy with current combinators. I always try to create setups with not more than about 4 combinators per task, and as composite not more than about 12. Otherwise I never know how it works when I get back to it a few days later. Your setup asks for generalization - it has a core that is duplicated 8 times. There must be a different approach to have only 1 core. At least that's something that I would try to create.

At the left side, a collection of signals with their corresponding stack sizes. You set all 1, I would set the stack sizes here. At the right side, a collection of actual signals. Then divide the right side by the left side. Again, that's not directly possible, since universal EACH / EACH isn't available, and a combinator combination I know for this has 125 combinators - definitely too large.

My approach would be to iterate over each signal, lookup its stack size and perform the division. You can use the ANYTHING wildcard for grabbing one signal to work with, then after processing store this signal in a memory cell and remove all signals that are stored in the memory cell from the input of the ANYTHING decider combinator, so it pulls the next signal. I did this blacklist approach with memory cell for train unloading before to avoid filter inserters stalling on non existent items, however since I don't see any practical use for computing the fill state of one chest in percent (sorry), I will not create a similar setup for this thread.

I will revisit this kind of challenge with Factorio 2.0, since the decider combinator is vastly improved, so the number of combinators for any complex setup will probably be halved. May be we even get EACH op EACH functionality with the arithmetic combinator. What we will definitely get, is the functionality to read the stack size for any given signal (the new selector combinator), so we don't need to store that explicitly. An EACH op EACH wasn't presented yet in a FFF, but I keep up my hope.

Crankenstein
Burner Inserter
Burner Inserter
Posts: 5
Joined: Sat Jan 13, 2024 10:25 pm
Contact:

Re: Trying to calculate accurate % out of variables

Post by Crankenstein »

Tertius wrote:
Sat Jan 20, 2024 12:41 pm
I understand. You hardcode the corresponding stack sizes in the combinator that performs the division.
The challenge is that an universal EACH / EACH operation isn't easy with current combinators. I always try to create setups with not more than about 4 combinators per task, and as composite not more than about 12. Otherwise I never know how it works when I get back to it a few days later. Your setup asks for generalization - it has a core that is duplicated 8 times. There must be a different approach to have only 1 core. At least that's something that I would try to create.
I started playing with combinators a couple of weeks ago, I have no programming knowledge, and my math is OK at best. Learning how the signals interact with each other, learning existing combinator concepts and tricks(EACH, ANYTHING, EVERYTHING, SR latch, memory cell, clock, etc.), making sure your wires are connected properly and bringing all that information together to make a functional setup, I was not prepared for how difficult this turned out to be.

I was able to grasp non-combinator setups easily, but the moment combinators was introduced to the mix, I started to struggle immensely. YouTube tutorials, the wiki cookbook, and conversations about combinators didn't help me understand at a higher level no matter how many times I've tried to. Imagine me just consuming a bunch of information but not knowing what to do with it. So I started simple, light indicators, this was way harder than I expected, but the progress was still steady. Asking questions, that helps too, but trying to make sense out of information from someone who is better than you at circuits is a whole different beast.

Since calculating % from liquids was much easier than I expected(thank you DaveMcW), and after realizing that having a calculator that accounts for liquids and chest items is absolutely insane and not achievable with my current skillset, I shifted my focus to just items in chests. I couldn't find a formula that accounts for stack size, so I went the tedious route. I'm happy it works though.
Tertius wrote:
Sat Jan 20, 2024 12:41 pm
At the left side, a collection of signals with their corresponding stack sizes. You set all 1, I would set the stack sizes here. At the right side, a collection of actual signals. Then divide the right side by the left side. Again, that's not directly possible, since universal EACH / EACH isn't available, and a combinator combination I know for this has 125 combinators - definitely too large.


My approach would be to iterate over each signal, lookup its stack size and perform the division. You can use the ANYTHING wildcard for grabbing one signal to work with, then after processing store this signal in a memory cell and remove all signals that are stored in the memory cell from the input of the ANYTHING decider combinator, so it pulls the next signal. I did this blacklist approach with memory cell for train unloading before to avoid filter inserters stalling on non existent items, however since I don't see any practical use for computing the fill state of one chest in percent (sorry), I will not create a similar setup for this thread.
So there's still some nuanced mechanics about memory cells that I don't know about, because even though I understand it's a memory cell, I don't know what to do with that information besides link it up to the fact that it's useful in SR latches, and hooking up an SR latch properly to other combinators is something I still don't fully understand. So unfortunately, I have no idea what you're talking about.

But damn, 125 combinators. That's spicy.

You're right, it's not necessary for regular Factorio play to calculate percentages for one chest. It's something that I decided to do to compound on the knowledge I know from learning how to do light indicators, since % calculators work well with light indicators. It was a grueling experience, but a fun side project nonetheless. One chest % calculator was something I could do, so I did it, the learning continues, which is the goal of all this. The next challenge for me is to have the calculator account for an increasing amount of chests, which in theory is taking the variable 'X' and implementing it into the calculator, where X is the amount of chests you have in total. Except I have no idea how to do that with combinators.

Other projects I want to tackle are: count up and countdown display clocks (using bitshift and modulo), smart trains (omni pickup and drop off, train schedule is the same among all trains, circuit does the rest), efficient nuclear reactor (probably the easiest one compared to all the other projects).
Tertius wrote:
Sat Jan 20, 2024 12:41 pm
I will revisit this kind of challenge with Factorio 2.0, since the decider combinator is vastly improved, so the number of combinators for any complex setup will probably be halved. May be we even get EACH op EACH functionality with the arithmetic combinator. What we will definitely get, is the functionality to read the stack size for any given signal (the new selector combinator), so we don't need to store that explicitly. An EACH op EACH wasn't presented yet in a FFF, but I keep up my hope.
Holy. That's huge. My monstrosity of a calculator will shrink for sure! :lol:

Have fun with the challenge by the way. :)

Tertius
Filter Inserter
Filter Inserter
Posts: 671
Joined: Fri Mar 19, 2021 5:58 pm
Contact:

Re: Trying to calculate accurate % out of variables

Post by Tertius »

Crankenstein wrote:
Sun Jan 21, 2024 1:06 am

I started playing with combinators a couple of weeks ago, I have no programming knowledge, and my math is OK at best. Learning how the signals interact with each other, learning existing combinator concepts and tricks(EACH, ANYTHING, EVERYTHING, SR latch, memory cell, clock, etc.), making sure your wires are connected properly and bringing all that information together to make a functional setup, I was not prepared for how difficult this turned out to be.
If you're looking for inspiration for what you might do with combinators, these are examples for what I use combinators in my factories. May be that's some challenge with a practical use for you:
  • balanced loading of chests in loading/unloading stations
  • train limit control for loading/unloading stations
  • mixed loading/unloading of various items for supply trains
  • trash recycling for supply trains
  • nuclear reactor fuel control based on steam buffer fill state (includes a meter built with colored lamps to visualize the buffer fill state in 10% steps)
  • measuring and visualizing throughput of belts (combinators plus nixie tube mod for number display)
  • control which items your mall should produce
  • limit the amount of items your mall should produce
  • traffic control for congested train lines (that's really a only a megabase issue)
I could add "controlling U-235 production", but I always try to create foolproof, purely mechanical kovarex enrichment centrifuge setups. This is so important, it has to just work without ever need to look at it. So I have only minimal circuit control with that, mostly for unattended startup until the first 40 U-235 has been produced to store the surplus U-238 away and make it not congest the belt in the refining centrifuge area.

The most sophisticated setup I did was probably the supply train handling. With "sophisticated" I mean the amount of combinator techniques used for combinator, inserter and logistic network interaction.

I was very active in almost every combinator related forum thread of the last 2 years, however I practiced only theoretical knowledge with that. I built everything in a sandbox. But actual use in my factory is much less.

I prefer the KISS principle in production: keep it simple, stupid. I don't use even one SR latch or memory cell, and the only counter I use is a 200 second timer for nuclear reactor fuel control. You don't need to micromanage everything through combinators. Often, it works on its own if you create the proper mechanical setup, and if something works on a purely mechanical basis, it's a sign that you've done something right.
But if there is just a tiny detail that requires only one logic component to make that factory part perfect, do use a combinator.

Another example for theory versus practice: I developed a system to connect a power plant only, if the loading state of the accumulator field gets low and the solar panels are not sufficient. However, I never used this in my factory, because it's not simple enough. I cannot stand stuff that fails, and stuff that is not simple might fail more often.
Or I created a defense wall with laser turrets that switches its power off if there is no enemy in range. But I don't use it. I simply build more power supply to fuel the increasing base drain - much easier than the hassle with all that circuit and switch components.

Nuclear power is simply too cheap to justify all that micromanagement. May be in the upcoming Factorio 2.0 electric power is more limited on remote planets, so all these techniques might become more important, but with the current Factorio 1.1, you simply add another power plant tile.

Post Reply

Return to “Gameplay Help”