用矩阵乘法解决异星工厂品质升级概率计算
Solving Factorio Quality
给硬核策略游戏玩家提供了一套完整的数学建模方案,矩阵推导清晰可直接复用,适合喜欢深度优化的读者收藏。
I play Factorio the normal way: by writing matrix math code to plan the factory.
我以常规方式游玩《异星工厂》:通过编写矩阵数学代码来规划工厂。
But we’ll get to that. Or, TL;DR, go to my new online calculator tool.
但我们会讲到那部分。或者,简而言之(TL;DR),请访问我的新在线计算器工具。
Intro to Factorio and Quality
《异星工厂》与品质简介
Factorio pretty much founded the factory video game genre: you play a character who harvests resources and combines them to craft increasingly complex items, which in turn enable more sophisticated crafting. So far this sounds a lot like Minecraft and many other survival games, but what sets factory games apart is the focus on automation: soon enough, most of the crafting is done not “by hand” by the character but by increasingly many machines, with various forms of logistics like conveyor belts to move items between machines or wherever they need to go. Some factory game go further and remove the player character altogether.
《异星工厂》几乎开创了工厂类电子游戏这一流派:你扮演一个角色,采集资源并组合它们以制作日益复杂的物品,而这些物品反过来又能支持更精细的制作工艺。到目前为止,这听起来很像《我的世界》和许多其他生存游戏,但工厂类游戏与众不同的地方在于对自动化的侧重:很快,大多数制作不再由角色“手动”完成,而是由越来越多的机器完成,并通过传送带等各种物流形式在机器之间或所需目的地之间移动物品。有些工厂类游戏更进一步,完全移除了玩家角色。
As “technologies” are unlocked in-game, Factorio offers many mechanisms to improve production. One of them is modules: crafting machines have a (limited) number of slots to accept different kinds of modules that affect their stats: speed modules make the machine run faster at the cost of more energy consumption, productivity modules increase yield from the same ingredients at the cost of speed and energy, etc.
随着游戏中“科技”的解锁,《异星工厂》提供了许多提升生产的机制。其中之一是模块:制造机器拥有有限数量的插槽,可以接受影响其属性的各类模块:加速模块使机器运行更快,但代价是能耗增加;产量模块在牺牲速度和能耗的情况下,从相同的原料中提高产出率,等等。
Released in 2024, the Space Age extension adds new game mechanics including Quality: every item and recipe now come in five quality tiers: ⚀ normal, ⚁ uncommon, ⚂ rare, ⚃ epic, and ⚄ legendary. Depending on the item, each tier improves stats such as making crafting machines faster or making productivity modules more productive. High-quality items can be crafted directly from ingredients of the same quality, but the only way to increase quality is through the new quality modules.
2024年发布的“太空时代”扩展包增加了新的游戏机制,包括品质系统:每个物品和配方现在都有五个品质等级:⚀ 普通、⚁ 稀有、⚂ 罕见、⚃ 史诗和⚄ 传说。根据物品的不同,每个等级会提升某些属性,例如使制造机器更快或使产量模块更具生产力。高品质物品可以直接由同品质的原料制作而成,但提升品质的唯一途径是通过新的品质模块。
Quality modules can have quality too. Source: Factorio wiki
品质模块本身也可以具有品质。来源:《异星工厂》维基
Modules affect the probability QQ of any quality increase. For a given craft, each quality tier increase after the first is another 10% chance. We can build a table of the probabilities of output quality depending on input quality:
模块会影响任何品质提升的概率 QQ。对于某次制作,第一次之后的每个品质等级提升都有额外的 10% 几率。我们可以根据输入品质构建输出品质的概率表:
Source: Factorio wiki
来源:《异星工厂》维基
For example, the maximum possible quality chance in a machine with four module slots is 24.8%:
例如,拥有四个模块槽位的机器中,最大可能的品质几率为 24.8%:
Jumping from normal to legendary in one step is only a 0.0248% chance. Source: Factorio wiki
一步从普通跃升至传说的几率仅为 0.0248%。来源:《异星工厂》维基
Some players dislike the introduction of randomness to a game that was mostly deterministic, but with enough repetitions probabilities become ratios.
一些玩家不喜欢将随机性引入一款主要是确定性玩法的游戏,但只要重复次数足够多,概率就会变成比率。
The probabilities are balanced so that even with multiple crafting steps (each a potential quality jumps), getting high-quality items unavoidably involves also crafting many unwanted low-quality ones.
概率经过平衡,使得即使经过多次制作步骤(每一步都可能导致品质跃升),获得高品质物品不可避免地也会同时制作大量不需要的低品质物品。
To avoid the factory grinding to a halt when storage eventually gets full, Space Age also introduces the recycler: a new machine that destroys any item and (usually) returns 25% of its ingredients. This enables players to design “upcycling” contraptions that craft and recycle in a loop with quality modules until items reach the desired quality, at the cost of consuming many more ingredients:
为了避免工厂在存储空间最终被填满时陷入停滞,《太空时代》还引入了回收机:一种可以销毁任何物品并(通常)返还其25%原材料的新机器。这使得玩家能够设计“升级再造”装置,通过品质模块循环进行制作和回收,直到物品达到所需品质,代价是消耗更多的原材料:
Source: Factorio blog
来源:Factorio 博客
Factory planning tools
工厂规划工具
Some video games are partly “played” outside of the game itself. Blue Prince fully expects its players to keep extensive notes of everything they see, but doesn’t provide an in-game notepad or similar tool. Factory games lend themselves to building large spreadsheets for resource accounting, but a select few players decide that spreadsheets are not powerful enough for factory planning and spent countless hours programming dedicated tools that reproduce much of the game’s math to accurately model a production chain.
有些电子游戏部分是在游戏之外“游玩”的。《Blue Prince》完全期望玩家记录所看到的一切详细信息,但并未提供游戏内的记事本或类似工具。工厂类游戏适合建立大型电子表格以进行资源核算,但少数玩家认为电子表格对于工厂规划来说不够强大,于是花费无数小时编程开发专用工具,重现游戏中的大部分数学逻辑,以准确模拟生产链。
Example production chain in Factoriolab
Factoriolab 中的示例生产链
This is all optional in factory games, it’s perfectly viable to play it by ear and just build more when seeing something lacking.
在工厂类游戏中,这一切都是可选的;完全可以凭直觉游玩,看到缺什么就建什么。
But I do like to plan in advance: how many machines of each kind do I need? How much yield can I expect? Where are the bottlenecks? The looping nature of quality upcycling makes this particularly challenging either to guess, or to calculate with existing tools.
但我喜欢提前规划:我需要多少台各类机器?预期产量是多少?瓶颈在哪里?品质升级再造的循环特性使得无论是猜测还是使用现有工具计算,都极具挑战性。
Matrix math
矩阵数学
Let’s imagine:
让我们设想一下:
- Some ingredients (for example iron plates) that come from an arbitrary production chain that may involve quality modules. Any given ingredient has a probability to be in each quality tier: ⚀ normal, ⚁ uncommon, ⚂ rare, ⚃ epic, and ⚄ legendary.
- Enough assembling machines with each recipe tier to craft all ingredients into some product (for example pipes). These machines have quality modules so that the a quality chance is 10%.
- 一些来自任意生产链的原材料(例如铁板),该生产链可能涉及品质模块。任何给定的原材料都有概率处于每个品质等级:⚀ 普通、⚁ 稀有、⚂ 罕见、⚃ 史诗和 ⚄ 传说。
- 拥有足够多的装配机,每种配方等级各一台,用于将所有原材料制成某种产品(例如管道)。这些机器装有品质模块,使得品质提升的概率为10%。
Let’s track the possible fates of one item:
让我们追踪一个物品的可能命运:
A product of a given tier can come from ingredients of the same tier or lower. The total probability for this outcome is the sum of (independent) probabilites of different ways to get it. In turn, those are the product of the percentage chance of a specific quality jump times the probability of having the corresponding ingredient tier in the first place:
特定等级的产品可以来自相同或更低等级的原材料。该结果的总概率是不同方式获得它的(独立)概率之和。而这些概率则是特定品质跃升百分比概率与最初拥有相应原材料等级概率的乘积:
p⚀=i⚀⋅90%p⚁=i⚀⋅9%+i⚁⋅90%p⚂=i⚀⋅0.9%+i⚁⋅9%+i⚂⋅90%p⚃=i⚀⋅0.09%+i⚁⋅0.9%+i⚂⋅9%+i⚃⋅90%p⚄=i⚀⋅0.01%+i⚁⋅0.1%+i⚂⋅1%+i⚃⋅10%+i⚄ \begin{align*} p_⚀ &= i_⚀ ⋅ 90\% \\ p_⚁ &= i_⚀ ⋅ 9\% &+ &i_⚁ ⋅ 90\% \\ p_⚂ &= i_⚀ ⋅ 0.9\% &+ &i_⚁ ⋅ 9\% &+ &i_⚂ ⋅ 90\% \\ p_⚃ &= i_⚀ ⋅ 0.09\% &+ &i_⚁ ⋅ 0.9\% &+ &i_⚂ ⋅ 9\% &+ &i_⚃ ⋅ 90\% \\ p_⚄ &= i_⚀ ⋅ 0.01\% &+ &i_⚁ ⋅ 0.1\% &+ &i_⚂ ⋅ 1\% &+ &i_⚃ ⋅ 10\% &+ i_⚄ \end{align*}
(The percent sign can be thought of as implicit division by 100, so that “percentage of” is the same as multiplication.)
(百分号可视为隐式的除以 100,因此“百分比”等同于乘法。)
Here the percentage coefficients look transposed across the diagonal compared to the quality jump probability table from the wiki, but that’s only because we’ve arranged product tiers vertically. Instead let’s group the probabilities of different tiers of the same item into row vectors:
与维基百科上的品质跃迁概率表相比,这里的百分比系数看起来是沿对角线转置的,但这只是因为我们把产品等级垂直排列了。不如将同一物品不同等级的概率组合成行向量:
product=(p⚀p⚁p⚂p⚃p⚄)ingredient=(i⚀i⚁i⚂i⚃i⚄) \begin{align*} product &= \begin{pmatrix*} p_⚀ & p_⚁ & p_⚂ & p_⚃ & p_⚄ \end{pmatrix*} \\ ingredient &= \begin{pmatrix*} i_⚀ & i_⚁ & i_⚂ & i_⚃ & i_⚄ \end{pmatrix*} \\ \end{align*}
Now our system of linear equations can be written as a single equation where a vector is multiplied by a transition matrix that matches the wiki’s table:
现在,我们的线性方程组可以写成一个单一方程,其中向量乘以与维基百科表格相匹配的转移矩阵:
products=ingredients⋅Tquality(10%)Tquality(q)=(1−q9q109q1009q1000q100001−q9q109q100q100001−q9q10q100001−qq00001) \begin{align*} {products} &= {ingredients} ⋅ T_{quality}(10\%) \\[1em] T_{quality}(q) &= \begin{pmatrix*} 1-q & \frac{9q}{10} & \frac{9q}{100} & \frac{9q}{1000} & \frac{q}{1000} \\[0.3em] 0 & 1-q & \frac{9q}{10} & \frac{9q}{100} & \frac{q}{100} \\[0.3em] 0 & 0 & 1-q & \frac{9q}{10} & \frac{q}{10} \\[0.3em] 0 & 0 & 0 & 1-q & q \\[0.3em] 0 & 0 & 0 & 0 & 1 \end{pmatrix*} \end{align*}
products=ingredients⋅Tquality(10%)Tquality(q)=(1−q9q109q1009q10009q10000q100001−q9q109q100q100001−q9q10q100001−qq00001) \begin{align*} {products} &= {ingredients} ⋅ T_{quality}(10\%) \\[1em] T_{quality}(q) &= \begin{pmatrix*} 1-q & \frac{9q}{10} & \frac{9q}{100} & \frac{9q}{1000} & \frac{q}{1000} \\[0.3em] 0 & 1-q & \frac{9q}{10} & \frac{9q}{100} & \frac{q}{100} \\[0.3em] 0 & 0 & 1-q & \frac{9q}{10} & \frac{q}{10} \\[0.3em] 0 & 0 & 0 & 1-q & q \\[0.3em] 0 & 0 & 0 & 0 & 1 \end{pmatrix*} \end{align*}
As an edge case, zero quality chance means no tier transformation. The corresponding transition matrix is the identity matrix: Tquality(0%)=I5T_{quality}(0\%) = I_5
作为边界情况,零品质几率意味着没有等级转换。对应的转移矩阵是单位矩阵:Tquality(0%)=I5T_{quality}(0\%) = I_5
This may not seem like much progress, but now a multi-step process can be computed through successive matrix multiplication. For example mining iron ore with 7.5% quality chance, then smelting it into iron plates with 5% quality chance, then crafting pipes with 10% chance. With no productivity bonus, we get these probabilities of end-products:
这看起来似乎进展不大,但现在可以通过连续的矩阵乘法来计算多步骤过程。例如,以 7.5% 的品质几率开采铁矿石,然后以 5% 的品质几率将其冶炼成铁板,最后以 10% 的几率制作管道。在没有生产力加成的情况下,我们得到最终产品的这些概率:
pipe=(10000)⋅Tquality(7.5%)⋅Tquality(5%)⋅Tquality(10%)=(0.9250.06750.006750.0006750.000075)⋅Tquality(5%)⋅Tquality(10%)≈(0.878750.105750.0136130.0016650.000223)⋅Tquality(10%)≈(0.7908750.1742630.0296780.0044670.000719) \begin{align*} pipe &= \begin{pmatrix*} 1 & 0 & 0 & 0 & 0 \end{pmatrix*} ⋅ T_{quality}(7.5\%) ⋅ T_{quality}(5\%) ⋅ T_{quality}(10\%) \\ &= \begin{pmatrix*} 0.925 & 0.0675 & 0.00675 & 0.000675 & 0.000075 \end{pmatrix*} ⋅ T_{quality}(5\%) ⋅ T_{quality}(10\%) \\ &≈ \begin{pmatrix*} 0.87875 & 0.10575 & 0.013613 & 0.001665 & 0.000223 \end{pmatrix*} ⋅ T_{quality}(10\%) \\ &≈ \begin{pmatrix*} 0.790875 & 0.174263 & 0.029678 & 0.004467 & 0.000719 \end{pmatrix*} \end{align*}
pipe=(10000)⋅Tquality(7.5%)⋅Tquality(5%)⋅Tquality(10%)=(0.925 0.0675 0.00675 0.000675 0.000075)⋅Tquality(5%)⋅Tquality(10%)≈(0.87875 0.10575 0.013613 0.001665 0.000223)⋅Tquality(10%)≈(0.790875 0.174263 0.029678 0.004467 0.000719) \begin{align*} pipe &= \begin{pmatrix*} 1 & 0 & 0 & 0 & 0 \end{pmatrix*} ⋅ T_{quality}(7.5\%) ⋅ T_{quality}(5\%) ⋅ T_{quality}(10\%) \\ &= \begin{pmatrix*} 0.925 & 0.0675 & 0.00675 & 0.000675 & 0.000075 \end{pmatrix*} ⋅ T_{quality}(5\%) ⋅ T_{quality}(10\%) \\ &≈ \begin{pmatrix*} 0.87875 & 0.10575 & 0.013613 & 0.001665 & 0.000223 \end{pmatrix*} ⋅ T_{quality}(10\%) \\ &≈ \begin{pmatrix*} 0.790875 & 0.174263 & 0.029678 & 0.004467 & 0.000719 \end{pmatrix*} \end{align*}
Quality strategies
质量策略
While it is possible to use quality modules as much as possible and deal with Every tier Everywhere All at Once, here we’ll focus on smaller self-contained systems.
虽然可以尽可能多地使用质量模块,并做到处处、每层、一次性处理(Every tier Everywhere All at Once),但这里我们将专注于更小的自包含系统。
“Gambling”: opportunistic quality without recycling
“赌博”:不回收的投机性质量
更进一步:量化金融体系
看懂新闻只是起点——沿量化金融路径,把它变成能交付的工程能力