Fix NaN when province admin cost is exactly zero - #2314
Merged
Merged
Conversation
For control_ratio < 0.01 the factor 1/(1.01 - control) - 1 in the admin cost is negative, so (pop * conc + area * 100) * factor + 100 can land on exactly 0. update_administrative_efficiency then computes desire = prize / 0 = inf and control_scale * desire = 0 * inf = NaN, so control_ratio becomes NaN. The NaN reaches tax income, the owner's treasury and then every market price, and the game later aborts. Seen in a headless AI-only campaign (seed 97): province 593 (25,234 people, 3,322.6 km2, control 0.00972) hit mass == 0 on 1869-10-13; the abort followed in 1871. With this change control is left unchanged on such a day; every other case computes exactly the same values (other seeds stay bit-identical over 100 years).
Contributor
|
I would recommend adjusting calculation of admin cost instead to avoid negative numbers by definition. |
…rol update For control below 0.01 the factor 1/(1.01-control)-1 is negative, so the admin cost could reach exactly 0 and produce inf/NaN. Clamping keeps the cost >= 100.
Contributor
Author
|
Done, thanks: the skip is gone and the factor is clamped with Checked it on headless AI campaigns: the seed that originally went NaN (1869) and crashed (1871) now runs through 1872 with no NaN in prices or treasuries. 20 seeds x 100 years with vs without the change show no difference in world population, industrial score, treasury, prices, nations alive/civilized or wars started (all Mann-Whitney p > 0.06), and none of the 40 campaigns has a NaN. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
update_administrative_efficiencycan turn a province'scontrol_ratiointo NaN, and that NaN later spreads to tax income, treasuries and market prices.Cause. When control_ratio is below 0.01, the factor
1/(1.01 - control) - 1in the admin cost is negative, so(pop * conc + area * 100) * factor + 100can come out exactly 0. Thendesire = prize / massis inf, andcontrol_scale * desireis 0 * inf = NaN.Where we hit it. A headless AI-only campaign (seed 97). On 1869-10-13, province 593 (25,234 people, 3,322.6 km², control 0.00972) had mass == 0. The NaN reached the owner's treasury and every market, and the game aborted in 1871.
Fix. On a tick where mass is exactly 0, leave control unchanged. Every other case computes exactly the same values as before.
Testing. On our Linux headless build (single-threaded, fixed seeds):