Page 1 of 1

Any math wizards out there?

Posted: Fri Aug 25, 2017 7:34 pm
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. :)

Re: Any math wizards out there?

Posted: Fri Aug 25, 2017 7:50 pm
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

Re: Any math wizards out there?

Posted: Fri Aug 25, 2017 7:58 pm
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.

Re: Any math wizards out there?

Posted: Sat Aug 26, 2017 8:51 pm
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.

Re: Any math wizards out there?

Posted: Sun Aug 27, 2017 2:35 am
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.

Re: Any math wizards out there?

Posted: Sun Aug 27, 2017 7:02 am
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).