Solver bug with catalysts
I haven't investigated this bug too much yet, but it's related to catalysts. They are probably handled a bit weirdly by the mod, so I'll see if I can come up with a clearer system for them.
Relavent issues: https://github.com/ClaudeMetz/FactoryPlanner/issues/255 https://github.com/ClaudeMetz/FactoryPlanner/issues/257
I just ran into this issue again with vitamelange nuggets, and I think the issue is in this line https://github.com/ClaudeMetz/FactoryPlanner/blob/15be116ec27aeec0b1d2c3c1c3c0f2c41bdda6db/modfiles/backend/handlers/generator_util.lua#L30 which causes prodded_amount to become negative when it subtracts the catalyst amount of stone (4) from the average value of the range (2). The analysis shows an 80% productivity debuff to the stone output when using 4 P9 modules (+20% each).
Barring a solver rewrite, a possible quick fix may be to clamp (base_amount - (base_item.catalyst_amount or 0)) to be non-negative.
Thank you for your time!
It seems like this issue appears when a catalyst item with a range of possible numbers is used as an output but doesn't appear as an input. Iirc, the "product" type item entries for the recipes already have the corresponding inputs subtracted from them before, so it only can become negative in this very case (or if a mod author set the catalyst item too high). In the SE case outlined above, xane256's solution would work. If however an even weirder recipe pops up where for instance 0-4 items are output but the catalyst amount is set to 2 (why would anyone do that? Idk) then I think (from how I understand the definition of catalysts at the moment) that any items beyond 2 would be eligible for productivity, i.e. could pop out after a productivity proc. Not sure how exactly it's done in engine unfortunately. If this is the case, then the catalyst calculations would need to be done before the expected value is calculated.
@Scarabytes it appears that your guess is correct - prod only applies to the random quantity above the catalyst amount.
I created a mod that adds some test recipes. Each recipe converts 1 iron ore into 1 iron plate on average, but with different random output amounts and catalyst amounts. For each recipe I fed 100k items into an assembler using that recipe with +80% productivity (4x SE prod 9s). The test results and derived formula are in this spreadsheet.
The formula as implemented in the sheet is:
chance * (
AVERAGE(lo, hi) +
AVERAGE(MAX(lo - cat, 0), MAX(hi - cat, 0)) *
(prod - 1) *
IF(cat < lo, 1, MAX((hi - cat + 1) / (hi - lo + 1), 0)))
where lo is the low end of the random range (amount_min in the recipe output definition), hi is the high end (amount_max), cat is catalyst amount, chance is the probability of output, and prod is the prod factor.
See PR #323 for fix.
I finally had a look at the PR and merged it for the next release, thanks a lot for the PR and the sleuthing to figure it out!