In other languages: 简体中文

Tutorial:Quality upcycling math

From Official Factorio Wiki
Revision as of 01:08, 13 January 2026 by 707090 (talk | contribs) (The original article is very high level and low on explanation for the way the math works. I have attempted to make the article easier to follow along for those who are rusty on their matrix algebra)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

How do we get the most amount of legendary items out of an upcycling plant?

The answer is not quite as straight forward as we'd like it to be, because it depends on a number of factors, luckily there is a finite number of possibilities of what the modules can be, and for the sake of simplicity this tutorial will ignore the productivity gain from infinite technologies. But first, a presentation of the results.

Best ratios

The table below shows the best ratio for quality to productivity modules in the crafting machines, while the recyclers always take only quality modules. The values are not given in whole numbers because often it is not just a single crafting machine per tier that will be used, then the ratios can change between different crafting machines in the same tier. e.g. "3.67 quality / 1.33 productivity" could have 4 machines where 3 have a ratio 4 to 1, and one a ratio 3 to 2.

The columns "X products" denote modules need to install into the machines that are set to produce items of X quality.

Use normal modules
Crafting machine Normal products Uncommon products Rare products Epic products Legendary products Percentage yield Items recycled*
Chemical plant
3
+
0
3
+
0
3
+
0
3
+
0
0
+
3
0.034014% 2940
Assembling machine 3
4
+
0
4
+
0
4
+
0
4
+
0
0
+
4
0.046275% 2161
Foundry
4
+
0
4
+
0
4
+
0
4
+
0
0
+
4
0.133814% 747
Electromagnetic plant
5
+
0
5
+
0
5
+
0
5
+
0
0
+
5
0.176712% 566
Cryogenic plant
6
+
2
6
+
2
6
+
2
6.5
+
1.5
0
+
8
0.119134% 840
Use uncommon modules
Crafting machine Normal products Uncommon products Rare products Epic products Legendary products Percentage yield Items recycled*
Chemical plant
3
+
0
3
+
0
3
+
0
3
+
0
0
+
3
0.059498% 1681
Assembling machine 3
3.75
+
0.25
3.75
+
0.25
3.8
+
0.2
3.9
+
0.1
0
+
4
0.082296% 1216
Foundry
4
+
0
4
+
0
4
+
0
4
+
0
0
+
4
0.243699% 410
Electromagnetic plant
4.7
+
0.3
4.67
+
0.33
4.75
+
0.25
4.9
+
0.1
0
+
5
0.324189% 309
Cryogenic plant
4.6
+
3.4
4.6
+
3.4
4.67
+
3.33
5
+
3
0
+
8
0.257621% 389
Use rare modules
Crafting machine Normal products Uncommon products Rare products Epic products Legendary products Percentage yield Items recycled*
Chemical plant
2.8
+
0.2
2.8
+
0.2
2.9
+
0.1
2.9
+
0.1
0
+
3
0.100660% 994
Assembling machine 3
3
+
1
3.1
+
0.9
3.2
+
0.8
3.33
+
0.67
0
+
4
0.145220% 689
Foundry
3.5
+
0.5
3.5
+
0.5
3.6
+
0.4
3.9
+
0.1
0
+
4
0.424039% 236
Electromagnetic plant
3.6
+
1.4
3.6
+
1.4
3.6
+
1.4
3.9
+
1.1
0
+
5
0.588510% 170
Cryogenic plant
3.6
+
4.4
3.6
+
4.4
3.6
+
4.4
3.9
+
4.1
0
+
8
0.565030% 177
Use epic modules
Crafting machine Normal products Uncommon products Rare products Epic products Legendary products Percentage yield Items recycled*
Chemical plant
2.33
+
0.67
2.4
+
0.6
2.4
+
0.6
2.4
+
0.6
0
+
3
0.152486% 656
Assembling machine 3
2.5
+
1.5
2.5
+
1.5
2.6
+
1.4
2.8
+
1.2
0
+
4
0.232966% 430
Foundry
2.7
+
1.3
2.7
+
1.3
2.75
+
1.25
3
+
1
0
+
4
0.664130% 151
Electromagnetic plant
2.6
+
2.4
2.6
+
2.4
2.67
+
2.33
2.9
+
2.1
0
+
5
0.974700% 103
Cryogenic plant
2.6
+
5.4
2.6
+
5.4
2.6
+
5.4
2.8
+
5.2
0
+
8
1.122444% 90
Use legendary modules
Crafting machine Normal products Uncommon products Rare products Epic products Legendary products Percentage yield Items recycled*
Chemical plant
1.67
+
1.33
1.67
+
1.33
1.67
+
1.33
1.8
+
1.2
0
+
3
0.344061% 291
Assembling machine 3
1.67
+
2.33
1.67
+
2.33
1.67
+
2.33
1.8
+
2.2
0
+
4
0.586191% 171
Foundry
1.4
+
2.6
1.4
+
2.6
1.4
+
2.6
1.5
+
2.5
0
+
4
1.624266% 62
Electromagnetic plant
1
+
4
1
+
4
1
+
4
1
+
4
0
+
5
2.722332% 37
Cryogenic plant
0
+
8
0
+
8
0
+
8
0
+
8
0
+
8
4.835199% 21

* "items recycled" quantifies how many items need to be crafted and put into the entire upcycler to yield a single legendary item, it doesn't account for those which get recycled multiple times.


Number of crafting machines

If we assume a constant input stream of uncommon items, which will always fill back up, we can additionally figure out what ratio of items will be inside the system at once, and with that we can figure out how many crafting machines we need per tier of quality. This is done by setting m1,k+1=100%m2,k+1m3,k+1m4,k+1 after each iteration and further adjusting for the crafting machines change in speed dependant on the modules (assuming the machines which only house productivity modules are not additionally boosted by speed moduled beacons). See the calculations further below for a full explanation of the calculations.

Per recycler
Crafting machine Recyclers Machines for
normal products
Machines for
uncommon products
Machines for
rare products
Machines for
epic products
Machines for
legendary products
Chemical plant 1 4.418130519 0.037216657 1.294369664 0.009779 0.431955
Assembling machine 3 1 4.733868899 0.052316655 1.527502978 0.015336 0.52576
Foundry 1 4.314446 0.082198 1.683088 0.03016 0.58448
Electromagnetic plant 1 4.494435 0.116985 1.93479 0.049665 0.60795
Cryogenic plant 1 5.23556 0.21384 2.53374 0.1034 0.594
Per legendary crafter (exact)
Crafting machine Recyclers Machines for
normal products
Machines for
uncommon products
Machines for
rare products
Machines for
epic products
Machines for
legendary products
Chemical plant 90.1223 331.8198 38.5325 11.2893 2.9653 1
Assembling machine 3 52.8432 208.4632 30.3992 9.8094 2.8783 1
Foundry 24.7668 89.0483 19.9206 7.7712 2.8592 1
Electromagnetic plant 16.5379 61.9418 17.4010 7.4908 3.1822 1
Cryogenic plant 10.8718 47.4350 18.2124 8.8138 4.2654 1
Per legendary crafter (conservative)
Crafting machine Recyclers Machines for
normal products
Machines for
uncommon products
Machines for
rare products
Machines for
epic products
Machines for
legendary products
Chemical plant 53 198 23 7 2 1
Assembling machine 3 31 123 18 6 2 1
Foundry 14 53 12 5 2 1
Electromagnetic plant 14 56 16 7 3 1
Cryogenic plant 9 41 16 8 4 1

The crafting machines

Crafting machine Module slots N Base productivity bonus p0
Chemical plant 3 +0%
Assembling machine 3 4 +0%
Foundry 4 +50%
Electromagnetic plant 5 +50%
Cryogenic plant 8 +0%

Quality probability

When an item gets produced and the initial roll decides that the quality of the item will increase, there is a 90% chance it will rise one tier, a 9% chance it will rise two, a 0.9% chance it will rise three, and a 0.1% chance it will rise four. This is of course capped if the item already started out at a higher tier.

Mathematical model

The mathematical model is time discrete. As opposed to dealing with derivatives in respect to time, the next state is a direct function of the previous state.

  • mi,k ... Number of materials of tier i (whereas 1 is normal, 2 is uncommon, 3 is rare, 4 is epic, and 5 is legendary) after the k-th iteration before being crafted. (this doesn't mean that an item only needs one type of ingredient, but that "1 materials" can be crafted into 1 item from them)
  • ni,k ... Number of items of tier i after the k-th iteration, after being crafted together.
  • p0 ... the crafting machines inherent productivity bonus
  • N ... number of modules the crafting machine can hold
  • qr=46.2%=0.248 ... quality probability of the recyclers with 4 quality module 3's (6.2% is the chance of a legendary tier quality module 3)
  • pi=p0+xi25% ... productivity due to xi legendary productivity module 3's in the crafting machine which takes i-tier materials (25% is the productivity boost of a legendary tier productivity module 3)
  • qi=(Nxi)6.2% ... quality probability due to (Nxi) legendary quality module 3's in the crafting machine which takes i-tier materials

Quality Matrix

When any machine with a quality chance operates on its ingredients it will have a chance to increase its quality with the probability mentioned above. We can model this quality increase chance as a transformation matrix. Common outputs only have one term because they are only created from common inputs which fail to increase in quality since Uncommon items and above can only increase in quality. Uncommon outputs are created from uncommon inputs which failed to increase in quality, as well as all common inputs which got a single rarity upgrade. This equation repeats for the higher tiers, summing up the chances of a jump from lower tiers as well inputs which failed to upgrade from its own teir. Additionally, the number of outputs can be increased with machine productivity. The quantity of outputs from each tier machine can be multiplied by the productivity of the machine.

Inputs starting at each tier have different potential upgrade upside since quality is capped at legendary. This means that it is optimal to vary the ratio of productivity to quality modules used in each tier of machine (pi will not be the same for all tiers).

normal outputs:n1,k+1=m1,k+1(1+p1)(1q1)uncommon outputs:n2,k+1=m1,k+1(1+p1)q10.9+m2,k+1(1+p2)(1q2)rare outputs:n3,k+1=m1,k+1(1+p1)q10.09+m2,k+1(1+p2)q20.9+m3,k+1(1+p3)(1q3)epic outputs:n4,k+1=m1,k+1(1+p1)q10.009+m2,k+1(1+p2)q20.09+m3,k+1(1+p3)q30.9+m4,k+1(1+p4)(1q4)legendary outputs:n5,k+1=m1,k+1(1+p1)q10.001+m2,k+1(1+p2)q20.01+m3,k+1(1+p3)q30.1+m4,k+1(1+p4)q4+m5,k+1(1+p5)+n5,k

Something to note is that the equation for legendary items does not use q5. This is because the number of legendary outputs made from legendary inputs is unaffected by quality modules since they are already the maximum quality.

This set of equations can be written as a matrix multiplication.

[n1,k+1n2,k+1n3,k+1n4,k+1n5,k+1]nk+1=[(1+p1)(1q1)0000(1+p1)q10.9(1+p2)(1q2)000(1+p1)q10.09(1+p2)q20.9(1+p3)(1q3)00(1+p1)q10.009(1+p2)q20.09(1+p3)q30.9(1+p4)(1q4)0(1+p1)q10.001(1+p2)q20.01(1+p3)q30.1(1+p4)q4(1+p5)]QualityandProductivityMatrix[m1,k+1m2,k+1m3,k+1m4,k+1m5,k+1]mk+1

This Quality and Productivity Matrix can be built with a quality and productivity vector (q15 and p15). It allows us to simply matrix multiply our vector of inputs mk with our transformation matrix to get the number of outputs in each tier nk. In order to further simplify, we can factor the productivity matrix out as well since it is simply a scaling matrix.

[n1,k+1n2,k+1n3,k+1n4,k+1n5,k+1]nk+1=[(1q1)0000q10.9(1q2)000q10.09q20.9(1q3)00q10.009q20.09q30.9(1q4)0q10.001q20.01q30.1q41]QualityMatrix[1+p1000001+p2000001+p3000001+p4000001+p5]ProductivityMatrix[m1,k+1m2,k+1m3,k+1m4,k+1m5,k+1]mk+1

Now that we have a general framework modelling how machines interact with quality, we need to combine to the two parts of the upcycling cycle: recycling and recrafting.

Recycled materials

A recycler is a machine just like any other with a few nuances:

  • Its inputs are fully built items (nk), and its outputs are the materials for those items (mk).
  • It cannot accept productivity modules so it is clearly optimal to just fill it with quality modules. Since the amount of productivity cannot be varied, we will fill every tier of recycler with the same quality modules, thus making q15 all the same value qr.
  • The recycler comes with a built-in (anti-)productivity of -75% which gets rid of most of what is input to it.
  • In an upcycling factory, we do not want to recycle legendary products since they are the final output. So we will manually set the 1 in the bottom right corner to 0 indicating that we will not be putting any legendary products into the recycler regardless of how many we have (this will be accounted for later in the final model).

If we plug those into our matrix model we get this:

[m1,k+1m2,k+1m3,k+1m4,k+1m5,k+1]mk+1=[(1qr)0000qr0.9(1qr)000qr0.09qr0.9(1qr)00qr0.009qr0.09qr0.9(1qr)0qr0.001qr0.01qr0.1qr0]QualityMatrix[1+0.75000001+0.75000001+0.75000001+0.75000001+0.75]ProductivityMatrix[n1,k+1n2,k+1n3,k+1n4,k+1n5,k+1]nk+1

This new set of Quality and Productivity Matrices has only a single input variable qr. If we assume the recycler has 4 legendary quality module 3s, that produces a total quality chance of qr=0.248, we could for instance calculate the recycler's transformation matrix by plugging that in:

R=[0.18800000.05580.1880000.005580.05580.188000.0005580.005580.05580.18800.0000620.000620.00620.0620]

At this point in the tutorial I want to review what this all means for those of you who are new or rusty on your matrix algebra. This transformation matrix represents the ratio of outputs of each rarity that will be produced for every input. Each column represents the input rarity. Remember in our equations that we derived this matrix from, common items were always the first term. Each row represents the output rarity of the items after the machine has processed it. So for example the at 0.005 at A3,1 means that for every common input, we will get 0.005 rare (because it is in row 3) outputs. The beautiful thing about this transformation matrix is that it captures every transition chance from any rarity to any other rarity all at once when something is processed by the machine (a recycler with 4 legendary quality module 3s in this case).

Combined model

We can combine the transforms for a machine using the quality chance matrix above, along with the special transform matrix for the recycling part of the loop to form a larger equation representing the transition through the full recycle+build cycle. We will call the assembly combined Quality and Productivity matrix A and the recycling Quality and Productivity matrix R:

The final outputs for a given iteration are formed from the assembler transformation applied to the materials we have in that same iteration.

nk+1=Amk+1

We can substitute the recycler matrix for mk+1 since that is where the materials come from.

nk+1=ARnk

This is where we need to remember our modification to the recycling transformation matrix. We set the value which represented the number of recycled legendary items to 0 because we dont want to recycle items which are already legendary. We need to account for those items which have already made it to legenedary in our combined cycle equation. To do that we will multiply our current products matrix nk by a matrix unit to carry over legendary products from previous iterations. We will call this carryover from iteration k Ck

Ck=[0000000000000000000000001]C[n1,kn2,kn3,kn4,kn5,k]nk

Lets add that to the products created through reassembly above:

nk+1=ARnk+Ck=ARnk+Cnk=(AR+C)nk

We can simplify this by multiplying out matrices A and R and adding C to create a combined transformation matrix that represents all of the transitions between product rarity for a full cycle of the upcycler. We can call this combined loop matrix L. This compressed matrix that contains the transformation of all rarities for the full cycle allows us to quickly calculate the output ratios of cycling products through our upcycler multiple times. To do this we can simply multiply our input vector by the loop transformation matrix multiple times.

nk+1=Lnknk+2=Lnk+1=LLnk=L2nknk=Lkn0

Because products can only get higher quality, or be destroyed by the recycler's negative productivity, all products will eventually either become legendary or be destroyed by the recycler. We can see what the ratio of our inputs become legendary be assessing the ratio of items in different tiers after many cycles though the loop. If we choose a sufficiently high iteration k=100 the ratio of items left in the common-epic rows will be nearly zero.

Example

To demonstrate, lets walk through an example. Lets assume we are using an Electromagnetic Plant with only quality modules at every stage, and that we use legendary quality module 3s. This is of course clearly not optimal since the machines assembling legendary outputs have no use for quality modules, but I will do it anyway for the sake of keeping the example simple. I will start by trying to find the loop transformation matrix and substitute in values from there:

L=AR+C=[(1q1)0000q10.9(1q2)000q10.09q20.9(1q3)00q10.009q20.09q30.9(1q4)0q10.001q20.01q30.1q41][1+p1000001+p2000001+p3000001+p4000001+p5]ReassemblyMatrix[(1qr)0000qr0.9(1qr)000qr0.09qr0.9(1qr)00qr0.009qr0.09qr0.9(1qr)0qr0.001qr0.01qr0.1qr0][1+0.75000001+0.75000001+0.75000001+0.75000001+0.75]RecyclingMatrix+[0000000000000000000000001]LegendaryCarryoverMatrix

Substituting q15=0.31 (5 quality modules) and p15=0.5 (EM built-in productivity with no additional productivity modules) for the reassembly machines and qr=0.248 for the recycler we get a final loop matrix:

L=[0.194600000.13640.19460000.0370.13640.1946000.0060.0370.13640.194600.0010.0070.0440.18041]

When we raise this loop matrix to the 100th power to see what would happen to each common item after putting the outputs back into the upcycler 100 times, we get this:

L=[000000000000000000000.0130.03470.09260.2241]

This this shows us what would happen to inputs of each rarity after 100 cycles. The number in the bottom left corner (0.013 in our example), which comes from the far left column (common input) and bottom row (legendary output), shows how many legendary outputs will be created for every common input. We can use an program to try out different ratios of productivity to quality for each stage to try to maximize this value.