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.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.
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.