Skip to content

Latest commit

 

History

History
162 lines (119 loc) · 8.17 KB

readme.md

File metadata and controls

162 lines (119 loc) · 8.17 KB

Factorio optimizer

See also: factorio_optimizer_v2, a separate project that generates belt-based production blocks: a linear program picks recipes and machine counts, simulated annealing places the machines, a router lays belts, pipes and poles between them, with experimental Space Age support.

Plans a Factorio 2.0 bot mall: you name the products and rates, it works out how many assembling machines each recipe needs and places them on a grid so that items travel as little as possible. Each improvement is written as a blueprint string you can paste into the game.

The placement is a constraint program solved with Google OR-Tools' CP-SAT. Every machine gets one grid cell, and the objective is the total Manhattan distance between each machine and its suppliers (other machines or fixed input points), weighted by how many items per second flow along it. Supply is split greedily: a consumer draws from the first producers of its ingredient until its demand is met.

Example

Example layout, built in game

A mall for utility science at 1/s, as built from the blueprint. Each cell is an assembling machine 3 with:

  • a requester chest asking for the recipe's ingredients,
  • a filtered storage chest and an active provider chest for the product,
  • three fast inserters: one feeds the machine, and two take the product out, normal quality to storage and anything else to the active provider,
  • a medium electric pole.

The input points (plates, plastic, steel, batteries, sulfur) are along the top. final_video_1000w3.mp4 shows the layouts found one after another (solutions 1–305, stopped early).

Results

Six jobs searched for up to an hour each on 10 threads (M5), with the raw inputs entering on the row above the grid, and one bigger run of six science at 7/s each. Times leave out the start-up (about 8 s of loading the recipes and OR-Tools).

Job Products (per second) Machines Size in tiles Model variables Search Best found after Peak RAM
military military 1 13 20 × 16 441 < 1 s, optimal < 1 s 0.2 GB
red_green red 1, green 1 17 25 × 16 682 < 1 s, optimal < 1 s 0.2 GB
red_green_2x red 2, green 2 30 30 × 20 1,927 < 1 s, optimal < 1 s 0.2 GB
utility utility 1 78 45 × 36 12,986 1 h, stopped 18 min 1.3 GB
utility_production utility 1, production 1 108 55 × 40 24,371 1 h, stopped 44 min 2.0 GB
six_science all six at 1 174 70 × 52 62,072 1 h, stopped 56 min 3.8 GB
six_science_7 (cloud machine) all six at 7 1,698 230 × 148 about 5.8 million 2 h, stopped 1 h 32 min (first) 152 GB

The model grows with the square of the machine count (two variables per pair of machines), and from about 80 machines CP-SAT can no longer prove a layout optimal within an hour.

Six science at 7/s each, built in game

Six science at 7/s each: 1,698 machines on a 46 × 37 grid.

To see what the search is worth, each of the six jobs was also scored with 30 random placements of the same machines and searched for its worst layout for 15 minutes. Transport cost is the sum of items per second times distance in grid cells, so lower is better. The lower bound is what CP-SAT proves while searching: no layout can cost less. Where it equals the best layout, that layout is optimal.

Job Best found Proven lower bound First layout found Random (mean) Worst found Random ÷ best
military 4.5 4.5 10.0 11.4 22.8 2.5×
red_green 16.0 16.0 32.0 41.2 81.0 2.6×
red_green_2x 20.0 20.0 52.0 70.3 158.0 3.5×
utility 145.4 92.0 439.6 530.0 1,130.7 3.6×
utility_production 214.3 136.0 926.3 869.7 2,019.9 4.1×
six_science 288.4 156.0 1,362.7 1,470.0 3,465.3 5.1×

Every layout kind relative to the best one

The first layout CP-SAT finds is about as bad as a random one, and the gap to the best grows with the size of the mall: 2.5× for 13 machines, 5× for 174. It should keep growing for bigger malls, since a random layout's links get longer as the grid grows while a good one keeps suppliers next to their consumers. For the three big jobs the best layout isn't proven optimal; the lower bound says the optimum is at most 36–46% lower still.

Search progress of the three big jobs

Most of the gain comes in the first minute, which takes the big jobs to about 0.3 of a random layout's cost; the rest of the hour brings them to 0.2–0.27.

Why it matters

This grid is very close to a city-block base: fixed-size cells served by bots. With minor changes, placing the blocks this way could greatly lower bot congestion and raise efficiency. How much depends on what it's compared with: a random layout (about what the search finds first), the worst one, or anything between. A person planning a base will strategise, so a hand-made layout lands somewhere between random and optimal; in these runs that gap is 2.5× for small malls and about 5× for a full six-science one, and it grows with size.

How to run

Java 25 (LTS) with Gradle 9.8.2 (the wrapper downloads it); it runs on macOS, Linux and Windows. Point JAVA_HOME at a JDK 25, for example on macOS:

export JAVA_HOME=$(/usr/libexec/java_home -v 25)

on Linux, at the JDK that java runs from:

export JAVA_HOME="$(dirname "$(dirname "$(readlink -f "$(command -v java)")")")"

On Windows use gradlew.bat instead of ./gradlew. Or open the folder in a GitHub Codespace or in VS Code with the Dev Containers extension: .devcontainer/ uses the Java 25 image (JAVA_HOME is already set) and compiles everything on creation.

Then run it:

./gradlew run

Pass -Pram=8g to give it more memory (the default is 6 GB). It searches for up to an hour, writing every improvement as it goes, and stops then. Stop it earlier whenever the layout is good enough.

Configuration

src/main/resources/config.json (see example_config.json):

{
  "quality": "normal",
  "forbidden_recipes": ["productivity-module"],
  "forbidden_places": [{ "x": 1, "y": 1 }],
  "targets": [{ "name": "utility-science-pack", "amount": 1.0 }],
  "sources": [{ "name": "iron-plate", "x": 1, "y": 20, "force": 1.0 }]
}
Field Meaning
quality Quality set on the recipes and chest requests.
targets Products and their rates, in items per second at crafting speed 1.
forbidden_recipes Recipes never used; their products then have to come from a source.
forbidden_places Grid cells left empty.
sources Where raw items come in, in grid cells (may lie outside the grid). An item can have several sources and each consumer uses the nearest. force multiplies the weight of distances to that source.

Without sources the distances to raw items aren't counted at all. Only assembling-machine categories are used (crafting, crafting with fluid, intermediates, electronics); recipes come from the game's recipes.json.

Output

Each run writes to src/main/resources/solves/<time> - solve/:

  • best_output_string.txt: the latest blueprint string.
  • allSolves/: every improvement's blueprint.
  • simpleTableVisual/: the grid as text, one file per improvement.
  • objectiveValuation.txt and score_vs_time.svg: the score over time.

Limits

  • One machine per cell, no beacons and no belts: everything moves by bots. Fluids aren't supplied, so recipes that need them must be fed by hand or forbidden.
  • The grid is as close to square as fits the machines.
  • The model grows with the square of the machine count, so large malls get slow and need a lot of memory.

License

Copyright © 2026 Filip Hostinský. Licensed under the GNU General Public License v3.0. recipes.json is extracted from Factorio's game data, which belongs to Wube Software and is not covered by this license.