Power Grid Comb Optimizer

by craig_g

Selecting a messy power-pole area, rebuilding only the internal copper links into a cleaner row/column comb, and doing it safely and efficiently without disturbing unrelated wiring.

Content
2 hours ago
2.1
61
Power
Owner:
craig_g
Source:
N/A
Homepage:
N/A
License:
MIT
Created:
15 days ago
Latest Version:
0.1.40 (2 hours ago)
Factorio version:
2.1
Downloaded by:
61 users

Power Grid Comb Optimizer

Power Grid Comb Optimizer is a performance-focused reimplementation inspired by the original Power Grid Comb by shanemadden, whose original Mod Portal page credits /u/BeanBayFrijoles for the concept. Credit also goes to chasemuss for the Factorio 2.1 compatibility fork that kept the original mod usable on Factorio 2.1.

The original idea is simple and extremely useful: take an area of power poles with messy copper wiring and clean it up.

This mod keeps that idea, but uses a new implementation focused on performance, predictable results, and protecting an established factory while the wiring is reorganized.

What does "clean up the power grid" mean?

Factorio automatically creates copper connections as power poles are placed.

Over time, especially when poles are:

  • built in different orders;
  • placed manually and later expanded;
  • constructed by robots;
  • replaced or upgraded;
  • added gradually as a factory grows;

the resulting electrical network can be completely functional while the copper wires become visually inconsistent.

You may end up with some poles connected neatly along a row while others have diagonal wires, uneven connections, unexpected crossings, or different wiring patterns simply because they were built at a different time.

There may be nothing electrically wrong with the network.

It just no longer looks like the clean, regular power grid you originally designed.

Before & After
&

Power Grid Comb Optimizer lets you select an area and reorganizes those internal copper connections into a cleaner and more predictable row-and-column "comb" pattern wherever the pole layout allows.

It changes the wiring, not the placement of your power poles.

Measured performance comparison

To get a more useful comparison than an estimate, profiling was added to the Factorio 2.1 compatibility fork of the legacy mod without intentionally optimizing its original algorithm.

Both versions were then tested in-game using the same 2,459-pole workload, with three runs of each implementation.

Instrumented legacy Power Grid Comb fork:

  • Average: 7,293.1 ms

Power Grid Comb Optimizer 0.1.39:

  • Average: 55.1 ms

For that particular benchmark, Power Grid Comb Optimizer used approximately 99.24% less CPU time, or was approximately 132x faster.

The individual runs produced speedups of roughly 129x to 135x, so the result was also reasonably consistent across the repeated tests.

This is a measured benchmark rather than a promise that every factory will see exactly the same result.

Actual performance will depend on the number and type of poles selected, their layout, existing wiring, other installed mods, and the computer running Factorio.

The goal is simply to make the algorithm scale much better as selections become larger.

Designed to protect an existing factory

Cleaning up the appearance of a power grid should not mean blindly disconnecting everything and hoping it reconnects correctly.

The optimizer therefore treats the existing electrical network as something that should be preserved.

It is designed to:

  • keep originally separate electrical networks separate;
  • leave copper connections leading outside the selected area untouched;
  • leave red and green circuit wires untouched;
  • preserve protected or script-owned copper connections;
  • preserve existing connections that cannot safely be recreated using the current wire-reach rules;
  • check actual Factorio wire reach before creating new connections.

The result is intended to be a cosmetic and structural cleanup of the selected power grid, rather than an uncontrolled rebuild of the factory's electrical system.

Built for large factories

Performance was one of the main reasons for creating this version.

The legacy implementation can perform an enormous amount of work as the number of selected poles increases, including comparing very large numbers of pole pairs and constructing a full distance table.

Power Grid Comb Optimizer instead tries to avoid unnecessary work.

It uses the structure of the factory itself to find useful connections, searches rows and columns directly, limits fallback searches to spatially relevant poles, caches repeated information, and processes large operations across game ticks.

The aim is not simply to make small selections slightly faster.

The aim is to make the Power Grid Comb idea practical on large established factories containing thousands or tens of thousands of power poles without unnecessarily stalling the game.

Large selections without one huge operation

Planning and wire changes are processed across game ticks using configurable work limits.

This means a very large selection does not need to perform every calculation and every wire change in one enormous script execution.

The optimizer only registers its processing while work exists, so there is no permanent polling loop running after the job has completed.

Plan first, change second

The optimizer plans the desired wiring before modifying the factory.

Immediately before applying the result, it checks the selected poles and electrical network again to make sure the factory has not changed underneath the operation.

When changes are required, new copper connections are created before obsolete internal connections are removed.

If an unexpected connection cannot be created, newly created wires from that operation can be rolled back rather than continuing with the removal stage.

Factorio 2.1

Power Grid Comb Optimizer is designed for Factorio 2.1.

Space Age is not required.

The optimizer can still work with Factorio 2.1 features such as quality-aware pole wire reach when they are available, but the mod itself does not depend on the Space Age expansion.

Credits

This project exists because of the work that came before it.
I have for years used the their mods and have always gone back to get their newer vesions.
I have really appreciated what it tries to achieve. With Space Age now out and factorio not seeming to get the same love and attention. I thought I would add my contribution to a game that I have spent thousands of hours on, and a nod back to their pioneers of such a great set mod maintainers.

The intention of Power Grid Comb Optimizer is not to present their idea as something new.

It is an attempt to preserve a very useful Factorio tool while exploring a different implementation that is safer to apply to established factories and considerably more scalable for large power grids.