Recipe output probabilities - how does it work?

Place to post guides, observations, things related to modding that are not mods themselves.
User avatar
Trblz
Long Handed Inserter
Long Handed Inserter
Posts: 63
Joined: Thu Sep 21, 2017 1:23 am
Contact:

Recipe output probabilities - how does it work?

Post by Trblz »

I want the output of my recipe products to work as followed using probabilities. Here's an example where the main product has 80% probability ("chance to succeed") and 20% probability to fail.

Code: Select all

results = 
{
	{type="item", name="main-product", probability=0.8, amount=1}, --this is the main product with 80% probability to succeed
	{type="item", name="by-product1", probability=0.2, amount=1}
	}
this works as expected.

Now here's an more complex example. The principle is still 80% for main-product and 20% for by-products.

Code: Select all

results = 
{
	{type="item", name="main-product", probability=1.6, amount=1}, --this is the main product with 80% probability to succeed
	{type="item", name="by-product1", probability=0.2, amount=1},
	{type="item", name="by-product3", probability=0.2, amount=1},
	{type="fluid", name="by-product4", probability=0.2, amount=1}
	}
If i do this using Amator Phasma's Recycling, i get
Untitled.png
Untitled.png (267.84 KiB) Viewed 3622 times
What i don't get is that the main product needs a probability of 1.6 (160%) in order to get the 80%/20%/20%/20%?
Is the probability set per recipe or item category?
My Mod list: Necormant co-author
User avatar
eradicator
Smart Inserter
Smart Inserter
Posts: 5207
Joined: Tue Jul 12, 2016 9:03 am
Contact:

Re: Recipe output probabilities - how does it work?

Post by eradicator »

https://wiki.factorio.com/Types/ItemProductPrototype

Product probabilities are calculated individually per cycle for each product. I.e. having one product with 80% and one with 20% means there's still a 0.2*0.8 = 16% chance of getting both in the same cycle. Probability has to be a value between 0 and 1 (see link above).

I can't help you with the recycling mod or it's bugs because i don't know it, but...
Trblz wrote: Mon Sep 02, 2019 5:14 pm If i do this using Amator Phasma's Recycling,
What do you even mean by "using" it? Are you using functions from the mod to generate your recipe?
Author of: Belt Planner, Hand Crank Generator, Screenshot Maker, /sudo and more.
Mod support languages: 日本語, Deutsch, English
My code in the post above is dedicated to the public domain under CC0.
User avatar
Trblz
Long Handed Inserter
Long Handed Inserter
Posts: 63
Joined: Thu Sep 21, 2017 1:23 am
Contact:

Re: Recipe output probabilities - how does it work?

Post by Trblz »

I am using the mod library as a bases for my own changes. The good thing of this lib is that i don't have to create a full library of new items.

The recipes are added through the following procedure (in recipe.lua)

Code: Select all

function apm.lib.utils.recipe.result.add_with_probability(recipe_name, result_name, result_amount_min, result_amount_max, probability)

    local recipe = data.raw.recipe[recipe_name]
    local type_name = apm.lib.utils.item.get_type(result_name)

    -- convert result
    apm.lib.utils.recipe.convert_simple_result_to_results(recipe_name)

    -- simple recipe (results)
    if recipe.results then
        table.insert(recipe.results,{type=type_name, name=result_name, amount_min=result_amount_min, amount_max=result_amount_max, probability=probability})
    end

    apm.lib.utils.recipe.add_mainproduct_if_needed(recipe_name)
end
So the mod is only doing a table insert using it's own library function set.
Product probabilities are calculated individually per cycle for each product. I.e. having one product with 80% and one with 20% means there's still a 0.2*0.8 = 16% chance of getting both in the same cycle. Probability has to be a value between 0 and 1 (see link above).
This part i get and i am not challenging it. At it works in my simple example.

What i am challenging is that i need to put 1.6 (160%) for the red circuit to get an actual 80% chance as reported in the screenshot.
My Mod list: Necormant co-author
User avatar
Deadlock989
Smart Inserter
Smart Inserter
Posts: 2529
Joined: Fri Nov 06, 2015 7:41 pm

Re: Recipe output probabilities - how does it work?

Post by Deadlock989 »

Trblz wrote: Mon Sep 02, 2019 6:39 pm What i am challenging is that i need to put 1.6 (160%) for the red circuit to get an actual 80% chance as reported in the screenshot.
I have a bunch of recipes which produce a big mixed bag of results, some probabilistic, some not, and I don't observe that behaviour (and have counted results). If it says probability = 0.75, I get it 75% of the time. I'm not sure what 160% even means for an ItemProductPrototype. The wiki currently suggests it is supposed to be a 0-1 value.
User avatar
Trblz
Long Handed Inserter
Long Handed Inserter
Posts: 63
Joined: Thu Sep 21, 2017 1:23 am
Contact:

Re: Recipe output probabilities - how does it work?

Post by Trblz »

Following your comment about probability in the 0 - 1 range

If i use:

Code: Select all

results = 
{
	{type="item", name="main-product", probability=0.8, amount=1}, --this is the main product with 80% probability to succeed
	{type="item", name="by-product1", probability=0.2, amount=1},
	{type="item", name="by-product3", probability=0.2, amount=1},
	{type="fluid", name="by-product4", probability=0.2, amount=1}
	}
then it displays as 40%/20%/20%/20%
My Mod list: Necormant co-author
User avatar
Deadlock989
Smart Inserter
Smart Inserter
Posts: 2529
Joined: Fri Nov 06, 2015 7:41 pm

Re: Recipe output probabilities - how does it work?

Post by Deadlock989 »

Not for me. Sounds like some other mod is dicking with your recipes.
User avatar
darkfrei
Smart Inserter
Smart Inserter
Posts: 2905
Joined: Thu Nov 20, 2014 11:11 pm
Contact:

Re: Recipe output probabilities - how does it work?

Post by darkfrei »

Add Amator Phasma's Recycling to dependencies or shift the code to the one data file later. From data.lua to data-updates.lua or from data-updates.lua to data-final-fixes.lua
User avatar
eradicator
Smart Inserter
Smart Inserter
Posts: 5207
Joined: Tue Jul 12, 2016 9:03 am
Contact:

Re: Recipe output probabilities - how does it work?

Post by eradicator »

Trblz wrote: Mon Sep 02, 2019 7:22 pm then it displays as 40%/20%/20%/20%
Certainly sounds like a problem with the library your using and not with the game itself. Try using data:extend{} for comparison, if that works you can be sure it's a problem with the library.
darkfrei wrote: Mon Sep 02, 2019 7:43 pm Add Amator Phasma's Recycling to dependencies or shift the code to the one data file later. From data.lua to data-updates.lua or from data-updates.lua to data-final-fixes.lua
Or try that. I have no knowledge about how to use that particular library.
Author of: Belt Planner, Hand Crank Generator, Screenshot Maker, /sudo and more.
Mod support languages: 日本語, Deutsch, English
My code in the post above is dedicated to the public domain under CC0.
User avatar
darkfrei
Smart Inserter
Smart Inserter
Posts: 2905
Joined: Thu Nov 20, 2014 11:11 pm
Contact:

Re: Recipe output probabilities - how does it work?

Post by darkfrei »

I use for such situation the Info Mod viewtopic.php?f=135&t=45107, that writes the whole data.raw to log file and you can see all parameters after all applying code of all other mods.
Post Reply

Return to “Modding discussion”