freeorion / freeorion

Source code repository of the FreeOrion project.
https://www.freeorion.org
857 stars 164 forks source link

Monsters are not replicable with same seed #2856

Open Oberlus opened 4 years ago

Oberlus commented 4 years ago

Environment

Description

The location of monsters and monster nests at game start are not replicable using the same seed and galaxy settings (including Experimentors enabled/disabled).

Expected Result

The starting location of monsters and nests should depend on galaxy seed and settings, so that games can be replicated without a save file.

Steps to reproduce

geoffthemedio commented 4 years ago

Do you have random reseeding enabled? Are there AI empires?

Oberlus commented 4 years ago

Do you have random reseeding enabled? Are there AI empires?

Random reseeding enabled, AI empires present.

Edit: for some reason, I thought random reseeding had to be enabled (checked) to get consistent results. After reading the tooltip, I see it should be disabled to get the same sequence of random numbers and more repeatable games.

However, I have started 3 games with random reseeding enabled and another 3 games with it disabled, and I got not once the monsters on the same positions (they are similar sometimes, but there is always some differences, not only names but type of monster).

Vezzra commented 4 years ago

AFAIK that has always been a problem. While we managed to get the same map on all platforms when using the same seed, monster placement can't be reproduced.

The problem is that, when it comes to monster and monster nest placement, not everything is done by the universe generation scripts, but also by FOCS effects on turn 1. As effect execution is multi-threaded, the order in which effects are executed isn't strictly determined, making it impossible to get the same results from the RNG on each run.

We've yet to find a solution to this problem.

geoffthemedio commented 4 years ago

You can set the server options to use just one thread for effect evaluation.

Oberlus commented 4 years ago

You can set the server options to use just one thread for effect evaluation.

Would that affect performance?

We've yet to find a solution to this problem.

Could the effects be executed by a fixed and know number of threads (independent of multithreaded capabilities of the local system) and be instantiated sequentially in a fixed and known order with each of them getting a (different) seed determined by the galaxy seed so that RNG used in each thread produces a known sequence of random numbers that does not depend on the order in which the threads are executed/mixed?

Sorry for the long sentence. It makes me think of David F. Wallace and Thomas Bernhard.

geoffthemedio commented 4 years ago

... each thread produces a known sequence of random numbers ...

Possibly, but the threads aren't operating on independent gamestates, so inconsistent and unpredictable timing issues could occur even if each thread generates random numbers independently from all the other threads' random number generation.

peteroupc commented 4 years ago

I am aware that the problem of thread schedulers and reproducible "randomness" has been tackled by using a different design from traditional PRNGs. Notably, so-called "splittable" PRNGs have been developed for use in "isolated parallel computations that may generate subtasks", such as event processing that could be done by individual threads:

Essentially, splittable PRNGs give each task its own PRNG, rather than using global or thread-local PRNGs. In this case, a unique PRNG could be passed to Python's execute_turn_events method and that method could generate random numbers using that PRNG, rather than rely on a global or thread-local PRNG to do so. In the case of splittable PRNGs, a task does the following:

Vezzra commented 4 years ago

@Oberlus:

You can set the server options to use just one thread for effect evaluation.

Would that affect performance?

It should. If it didn't, having multithreaded effect evaluation would be pointless.