## Another approach to Cable Resistance (with fluid network)

Post your ideas and suggestions how to improve the game.

Moderator: ickputzdirwech

ratchetfreak
Filter Inserter
Posts: 950
Joined: Sat May 23, 2015 12:10 pm
Contact:

### Re: Another approach to Cable Resistance (with fluid network)

Sean Mirrsen wrote:I don't know if it's been suggested or thought of already - I can't read through all the different posts on the matter right now - but have you ever considered treating the system as a pathfinding solution, rather than a flow one? Because, really, electricity is not a flow. If you have a power consumer on the end of a very long line that only gets a fraction of the maximum power, then if you plug it in you'll get a reduction of available power across the entire network - the far-away consumer will have a cap on how much it can draw from the grid, but it will draw any available amount it needs up to that cap, even if there are other consumers closer to the powerplants.

With that in mind, something like a good pathfinding algorithm could suit your needs far better than a flow simulation. It might not be perfectly realistic, but then hardly anything in Factorio actually is. You'll end up with a dynamic calculation of paths by nodes, from each source to each destination, with assigned weights for calculating the cheapest path. Nodes are the power poles, the length of each cable can be easily gotten at, and the weight for getting from one node to the next is the cable length, maaaaaybe modified by the type of power pole. Each node will end up with a table of path cost values for each of the power providers in the grid, and the total power available at the pole will be the sum of those modified values. Still probably not a very performance-cheap solution, but since recalculations will only be required when the grid layout changes (i.e. not all that often), it could probably be a better solution than some.
A typical factory has a ton of both power producers and constumers, that results in ton^2 paths to calculate multiplied by the number of alternative paths (when there are redundant connections). Completely infeasible in a typical factory, let alone a megabase.

Flow networks are the existing solution to this kind of problem where calculating all those paths are amortized into operations on the graph itself. In a flow network you have a graph with costs on each link, a set of source node and a set of sink nodes. This is exactly what the electricity grind in factorio is.

Sean Mirrsen
Long Handed Inserter
Posts: 86
Joined: Wed Apr 27, 2016 6:30 pm
Contact:

### Re: Another approach to Cable Resistance (with fluid network)

ratchetfreak wrote:A typical factory has a ton of both power producers and constumers, that results in ton^2 paths to calculate multiplied by the number of alternative paths (when there are redundant connections). Completely infeasible in a typical factory, let alone a megabase.
How does pathfinding work in some games, then? Like Dwarf Fortress, for instance? In the bigger games, hundreds of dwarves and creatures, constantly trying to find new paths to thousands of potential locations, across a 3-dimensional grid with different path weights per cell, all in realtime? Compared to the pace that a power grid in Factorio expands at, it, at least on the surface, seems like a much more intensive task. A good pathing algorithm doesn't completely rebuild all paths every time a single node is added or removed, just the affected ones (and only optimal paths are stored, since they are the only ones that matter - alternative paths only come into play when optimal paths are destroyed, which triggers a recalculation for the broken path). And unlike a flow (at least from what it sounds like to me), it won't need constant recalculation - as long as the grid stays intact and unchanged, a megabase will run its power grid just as quickly as a smaller factory, if with a larger memory footprint for the stored paths.

Again though, this is just my impression of how it could work, so pardon me if you do have experience with both and know for a fact which one works better.

Rseding91
Factorio Staff
Posts: 11923
Joined: Wed Jun 11, 2014 5:23 am
Contact:

### Re: Another approach to Cable Resistance (with fluid network)

Sean Mirrsen wrote:
ratchetfreak wrote:A typical factory has a ton of both power producers and constumers, that results in ton^2 paths to calculate multiplied by the number of alternative paths (when there are redundant connections). Completely infeasible in a typical factory, let alone a megabase.
How does pathfinding work in some games, then? Like Dwarf Fortress, for instance? In the bigger games, hundreds of dwarves and creatures, constantly trying to find new paths to thousands of potential locations, across a 3-dimensional grid with different path weights per cell, all in realtime? Compared to the pace that a power grid in Factorio expands at, it, at least on the surface, seems like a much more intensive task. A good pathing algorithm doesn't completely rebuild all paths every time a single node is added or removed, just the affected ones (and only optimal paths are stored, since they are the only ones that matter - alternative paths only come into play when optimal paths are destroyed, which triggers a recalculation for the broken path). And unlike a flow (at least from what it sounds like to me), it won't need constant recalculation - as long as the grid stays intact and unchanged, a megabase will run its power grid just as quickly as a smaller factory, if with a larger memory footprint for the stored paths.

Again though, this is just my impression of how it could work, so pardon me if you do have experience with both and know for a fact which one works better.
100s path finding vs 100,000 things trying to path find across *much larger* paths. It's a massive difference.

The way the electric network works now is:

For each producer transfer power to consumers

That's it. There's no path finding - there's no distance - nothing but producer -> consumer transfer. In fact the electric network doesn't even know what poles are currently connected to the electric network. It just knows that it has things that produce energy and things that consume energy and it transfers energy between them each game tick.
If you want to get ahold of me I'm almost always on Discord.

ske
Filter Inserter
Posts: 409
Joined: Sat Oct 17, 2015 8:00 am
Contact:

### Re: Another approach to Cable Resistance (with fluid network)

Rseding91 wrote:The way the electric network works now is:

For each producer transfer power to consumers

That's it. There's no path finding - there's no distance - nothing but producer -> consumer transfer. In fact the electric network doesn't even know what poles are currently connected to the electric network. It just knows that it has things that produce energy and things that consume energy and it transfers energy between them each game tick.
You cannot be any more computing efficient than that.

Can you give us a rough estimate how much processing time (percentage of time each frame) a large fluid network needs?

(I'm aware that the fluid network cannot be used for electricity without major adjustments and even then it might have a few showstoppers. I'm asking to get a general understanding how detailed the simulation could be without hurting the game.)

Darloth
Fast Inserter
Posts: 116
Joined: Sun Jun 08, 2014 3:57 pm
Contact:

### Re: Another approach to Cable Resistance (with fluid network)

Someone has modded this sort of approach by the way - doesn't seem to have a forum topic so for the benefit of anyone still watching this thread I'll just leave this link here:

https://mods.factorio.com/mods/Simdezimon/flownetwork

Inserter
Posts: 43
Joined: Mon Jul 25, 2016 11:12 pm
Contact:

### Re: Another approach to Cable Resistance (with fluid network)

ske's original idea of "capacitance" for power really, really has merit, more than most players may think. Yes, you can imagine it like a fluid network, too. The main change is that the flow of power is constrained by the power pole type. You can't just string everything together with small power poles and have gigawatts going through one bottleneck!

But there are three competing design issues. First, would it be fun for players? Second, would it be efficient in programmer time? And third, would it be efficient for CPU processing time per tick?

Player fun would be GREAT I think. right now electric power distribution is a purely local layout subtask. It's about "can I put a pole near my assembers" or "if I fit a substation here, I can cover all of these at once." That's it. It's part of the fiddly fun of Factorio design, but that's all. There's no need to think about the STRING of power poles that connects your solar fields to your current assembler. It's all local.
But designing routing is fun too. MUCH of the deepest fun of Factorio is designing a bus of materials on banks of transport belts and routing products where they need to go. Power is a potential new routing challenge which would add to the richness of Factorio factory design.
How would it be fun? Well, the limitation is that power cables only can "flow" certain amounts of power per tick. You need "wider" wires (higher voltage, thicker cables) to move more current over a long distance, but then you need to "transform" them down to lower usable voltages with small wires for assemblers to use. The crucial game change would be user FEEDBACK of insufficient power delivery everywhere. Without this it'd just be frustrating. I suggest a simple toggle mode.. hit one key, and every power pole and wire is shown in color. Green for "have plenty of power to share to anything connected to me" and red for "need power!" Then there's a SECOND mode, just as important. Here it's FLOW related. Green for "I can move more even power between my neighbors if I have to" and red for "I'm transferring as much as I can! I can't move any more!" You need both feedback modes or it'll be frustrating. The FLOW related view is to visually identify bottlenecks. You find the red areas and add extra power lines, or upgrade the ones you have already. These may be far from your steam engines or assemblers, since they're bottlenecks, not over-consumers. But the first feedback mode will identify regions which has too much local consumption, and you'll see the (multiple) input power routes are likely all bottlenecked. So this tells you the rough regions of where to optimize, and if there's a problem at all. The second mode is for actually fixing bottlenecks. Note that the SAME SYSTEM would be fun and interesting for LIQUID FLOWS too. Look at all the confusion about flow rates and trying to measure them.

Programming difficulty: maybe not too bad. The actual flow between nodes is pretty similar to liquids now. The main weirdness is that power can't flow instantly, it will be updated in the game engine per tick, so it takes time for power to get from one place to another. That's weird and even unwanted, but it's workable, and keeps the game engine simpler since every update is local between connected entities. The annoying part is the GUI design for the color overlays.

CPU efficincy:. Factorio's engine is so fast and clean now, so it's important not to overload it with a deep new requirement. Currently, the power grid takes no CPU, it's hard work is done by just identifying network blocks, and within a block there's just three variables of power production, power use, and power stored. Changing to a distribution system would really affect computation effort. But I suspect NOT THAT MUCH, since it's all local between entities. It's basically a small accumulator per item, and drawing from your neighbors. Ideally you'd have an idea of a voltage AND charge quantity (unlike liquids) since a voltage drop would tend to bring in more charge where it's needed, giving a gradient for power to flow as a whole. Currently liquids work entirely by equalizing buffers which is a softer, slower, relaxation flow algorithm. Both work fine for steady state, but when there's a peak load (say when lasers start firing), we want power to max out the flow rate along the path to the lasers ASAP, not take 1000 game ticks for a wave of needed joules to be gently accelerated to make it from a distant steam engine bank. The right answer is to solve the Kirchoff flow matrix every tick, but that's impractical, compute wise. Having a voltage "hint" modeling the gradient of power flow will let each power node independently make a faster adjustment to the global answer by delivering power from its "easiest to supply you" neighbors to the "really really needs it" neighbors instead of wasting power routing capacity. Think of a critical power bottleneck going east to west. It can't supply enough power, so quite reasonably the player makes a SECOND line from east to west for double the capacity. The code needs to prioritize the flow capacity to be used east-to-west power flow and NOT use its flow to try to match up exact potentials between the two lines north-to-south.

aubergine18
Smart Inserter
Posts: 1264
Joined: Fri Jul 22, 2016 8:51 pm
Contact:

### Re: Another approach to Cable Resistance (with fluid network)

A mod implemented something like this: https://mods.factorio.com/mods/Simdezimon/flownetwork
Better forum search for modders: Enclose your search term in quotes, eg. "font_color" or "custom-input" - it prevents the forum search from splitting on hypens and underscores, resulting in much more accurate results.

steinio
Smart Inserter
Posts: 2595
Joined: Sat Mar 12, 2016 4:19 pm
Contact:

### Re: Another approach to Cable Resistance (with fluid network)

Fun fact: For that we don't need thicker cables the voltage of long distant lines is transformed up. With that the current stays low because power is voltage times current.

Transport Belt Repair Man