superbob / clicker-heroes-1.0-hsoptimizer

Clicker Heroes 1.0 HS Optimizer
https://superbob.github.io/clicker-heroes-1.0-hsoptimizer
5 stars 1 forks source link

#17: Fix Numeric Imprecision in Large Numbers Causing Optimizer Infinite Loop #18

Closed alainbryden closed 7 years ago

alainbryden commented 7 years ago

Summary

Details It's a pretty trivial fix, thanks to the algorithm already being fairly elegant.

My understanding is that the Optimizer will start increasing levels in powers of 2 until a further doubling would exceed the number of available HS. It then begins a new iteration, adding further precision to the level increase (increasing it by a smaller number each time). Finally, when a new iteration finds that it can't even increase by 1 more level without over-spending, it completes.

The bug here is that due to numeric imprecision, as we try to zero in on the number of levels to increase, getting more and more precise, there comes a time where we are increasing the levels by a small incremental amount (small power of 2), but not impacting the number of remaining hero souls, which results in an infinite loop, because the exit condition requires at least some HS to be 'spent' with the level increase.

This fix simply keeps track of the last base offset (level increase) calculated - if there has been no increase from one iteration to the next (because we could not not increase the base level by a number high enough to decrease the remaining hero souls without them going negative) then we can safely exit - because we have already found the most accurate level increase that numerical precision will allow.

alainbryden commented 7 years ago

Tested the fix and it let me allocate upwards of 1e36 HS without any problems - which was previously causing an infinite loop.

superbob commented 7 years ago

That's the quick fix I was thinking about. I'm doing some test at my side and I'll merge it quickly. Thanks !