s25client icon indicating copy to clipboard operation
s25client copied to clipboard

Make AIJH Actions Reproducible by Using the Games Random Seed

Open wichern opened this issue 1 year ago • 2 comments

Currently, the actions of AIJH are not reproducible because it uses a different random seed for each execution, resulting in different outcomes every time. I'm unsure whether this behavior is intentional.

This change ensures reproducibility by switching from the standard rand() function to RANDOM.Rand(), which allows for the use of a fixed random seed. With this update, users can replicate results consistently if desired.

If the lack of reproducibility was intentional, feel free to disregard this pull request.

wichern avatar Jan 25 '25 16:01 wichern

Hi @Flamefire

You are right, it only matters for AI battle. I am using them to analyze performance. It would be easier to do if the AI behaviour is reproducible. If you are ok with it, I would update RunProgram() to support a new command line parameter. Only in case someone wants to use a fixed value.

wichern avatar Feb 02 '25 15:02 wichern

For the AI-Battle RunProgram is not used, so that wouldn't help there.

And for regular playing rand() is not only used for AI but also for drawing, which was the initial intention:

// Zufallsgenerator initialisieren (Achtung: nur für Animations-Offsets interessant, für alles andere
    // (spielentscheidende) wird unser Generator verwendet)

i.e.

Init RNG (Attention: Only used for animation offsets...)

Which became outdated as it is also used for sounds and AI.

I suggest to add a global RNG for AI, and maybe even the functions from helpers/random.hpp directly in the AI namespace to forward them using the AI RNG. I.e. something like `T randomValue(...) { return helpers::randomValue(getAiRng(), ...); }

This could be std::minstd_rand or even std::mt19937_64

This can the be seeded by the AI battle CLI and probably also in StartGame

The AI code then needs to be updated to use those new functions.

It is still possible that the results are not fully deterministic as the time when the AI acts might depend on e.g. network packet order. It wasn't written to be deterministic. But I guess it will still work, at least for the AI battle.

Flamefire avatar Feb 03 '25 10:02 Flamefire