Any math wizards out there?

Place to get help with not working mods / modding interface.
Post Reply
User avatar
withers
Fast Inserter
Fast Inserter
Posts: 125
Joined: Fri Apr 08, 2016 4:54 pm
Contact:

Any math wizards out there?

Post by withers »

I'm trying to figure out a formula to use to get the TOTAL pollution produced at any given time.

We can get the current evolution factor, and we can get the total game time, and we can (let,s just say for now).... ignore how many enemies bases have been destroyed. (assume zero for my purposes). So this function would be:

pollution_produced = f(time_played, current_evolution_level)

The tricky part is that on each tick the formula used by the game to increment evolution uses (1-current_evolution) as a variable. So it's some kind of exponential decay formula I'd guess.....

Alternatively, if anyone knows of an api call that can just tell me total pollution produced, that'd work too. :)

keyboardhack
Filter Inserter
Filter Inserter
Posts: 478
Joined: Sat Aug 23, 2014 11:43 pm
Contact:

Re: Any math wizards out there?

Post by keyboardhack »

http://lua-api.factorio.com/latest/LuaS ... _pollution

Pollution is the same per chunk so the below code should give the total amount of pollution for the default surface(nauvis)

Code: Select all

/c
local totalPollution = 0
for chunk in game.surfaces.nauvis.get_chunks() do
    totalPollution = totalPollution + game.surfaces.nauvis.get_pollution({chunk.x * 32, chunk.y * 32})
end
Waste of bytes : P

User avatar
withers
Fast Inserter
Fast Inserter
Posts: 125
Joined: Fri Apr 08, 2016 4:54 pm
Contact:

Re: Any math wizards out there?

Post by withers »

Thanks! I'll try that.
Pollution is the same per chunk so the below code should give the total amount of pollution for the default surface(nauvis)
Brilliant. Hopefully that will not eat too many UPI's.

Doesn't get total pollution produced for the whole game since pollution dissipates but this could still work for what I'm trying to do.

sillyfly
Smart Inserter
Smart Inserter
Posts: 1099
Joined: Sun May 04, 2014 11:29 am
Contact:

Re: Any math wizards out there?

Post by sillyfly »

So you are asking (assuming no spawners are/were destroyed), what is the function f(t) such that

Code: Select all

f(t+1) = f(t) + (1-f(t))*(tA + pB) 
where t is the current tick, A is the time evolution increase factor (default 0.000004 according to the wiki), p is the total pollution produced and B is the pollution evolution increase factor (0.000015 according to the wiki).

Assuming there is an smooth function satisfying this relation, we can use a first-order approximation to get a differential equation:

Code: Select all

f'(t) = (1-f(t)) * (tA + pB)  
After some "massaging" you realize it is probably a decaying exponential, and since the second term has the argument t we can assume it has up to second order in the exponent, so "guess" a solution in the form:

Code: Select all

f(t) = 1 - exp(Lt + Qt²)
and calculate the derivative:

Code: Select all

f'(t) = -(L + 2Qt)*exp(Lt + Qt^2)
So it comes down to finding L and Q, such that for every t:

Code: Select all

-(L + 2Qt) = (tA + pB)
and since this should hold true for every t you can separate this to two equations - one with t and one without:

Code: Select all

-L = pB  ==>  L=-pB
-2Q = A ==> Q = -A/2
And so the original function is:

Code: Select all

f(t) = 1 - exp(-pBt - At²/2)

Note that this assumes the pollution produced (p) does not change with time, but since you can calculate this every tick and you are interested in the current pollution production I think this should work.

User avatar
withers
Fast Inserter
Fast Inserter
Posts: 125
Joined: Fri Apr 08, 2016 4:54 pm
Contact:

Re: Any math wizards out there?

Post by withers »

Wow! I knew there were smart people on this forum! So given....

Code: Select all

f(t) = 1 - exp(-pBt - At²/2)
How do I solve for the p? I think it has something to do with natural logs. But all my logs are fake and the cobwebs in my head just won't let me do math like in the old days.

sillyfly
Smart Inserter
Smart Inserter
Posts: 1099
Joined: Sun May 04, 2014 11:29 am
Contact:

Re: Any math wizards out there?

Post by sillyfly »

Assuming I made no silly mistakes, it should be:

Code: Select all

p = - [log(1-E) + At²/2] / Bt 
Where E is the current evolution (i.e. f(t)), and by `log` I mean the natural logarithm, of course (not the base 10 engineers often use, or base 2 computer scientists like).

Post Reply

Return to “Modding help”