[Question] EmpireID vs AI index

Programmers discuss here anything related to FreeOrion programming. Primarily for the developers to discuss.

Moderator: Committer

Post Reply
Message
Author
Morlic
AI Contributor
Posts: 296
Joined: Tue Feb 17, 2015 11:54 am

[Question] EmpireID vs AI index

#1 Post by Morlic »

Is the relation between empire.empireID and the index of the AI client (as in AI1.log, AI2.log, ...) deterministic? If so, how? If not, would it be much work to change it?

For example, I currently use the following construct to compare some different approaches:

Code: Select all

    if empire.empireID % 2 == 0:
        # new stuff
    else:
        # old stuff
I would naively expect that the AIs would alternate. However in my current game AI1 and AI2 have an even empireID and AI3 and AI4 both have an odd empireID.
When playing with multiple AIs it would be nice to know which AI has which empireID (and thus follows which behaviour) to find the interesting logs more quickly without having to look up the empireID first inside each log.
If I provided any code, scripts or other content here, it's released under GPL 2.0 and CC-BY-SA 3.0

User avatar
Geoff the Medio
Programming, Design, Admin
Posts: 13603
Joined: Wed Oct 08, 2003 1:33 am
Location: Munich

Re: [Question] EmpireID vs AI index

#2 Post by Geoff the Medio »

Morlic wrote:Is the relation between empire.empireID and the index of the AI client (as in AI1.log, AI2.log, ...) deterministic?
No. Particularly in multiplayer games, any AI can be assigned to any empire in the loaded game. For single player games, the AI name is set when the AI process is spawned. Which empire ID it gets depends, I think, on what order the AIs contact the server after being spawned.
When playing with multiple AIs it would be nice to know which AI has which empireID (and thus follows which behaviour) to find the interesting logs more quickly without having to look up the empireID first inside each log.
I suggest using a different scheme to assign behaviors than the empire ID. If you really want it to correspond to the log file names, using the AI name might work. That is exposed in the Python API.

Morlic
AI Contributor
Posts: 296
Joined: Tue Feb 17, 2015 11:54 am

Re: [Question] EmpireID vs AI index

#3 Post by Morlic »

Sound advice, thanks. I will try using the AI name and extract the number then.
If I provided any code, scripts or other content here, it's released under GPL 2.0 and CC-BY-SA 3.0

User avatar
Cjkjvfnby
AI Contributor
Posts: 539
Joined: Tue Jun 24, 2014 9:55 pm

Re: [Question] EmpireID vs AI index

#4 Post by Cjkjvfnby »

Morlic wrote:Sound advice, thanks. I will try using the AI name and extract the number then.
Here is code example.
https://github.com/freeorion/freeorion/ ... AI.py#L168

You can hack universe generation scripts to get not random names for empires.
If I provided any code, scripts or other content here, it's released under GPL 2.0 and CC-BY-SA 3.0

User avatar
Dilvish
AI Lead and Programmer Emeritus
Posts: 4768
Joined: Sat Sep 22, 2012 6:25 pm

Re: [Question] EmpireID vs AI index

#5 Post by Dilvish »

Morlic wrote:I will try using the AI name and extract the number then.
I strongly recommend against that; it is only for a newly started game that there is a reliable relationship between AI name, player ID and empire ID; when reloading a saved game they get all jumbled.
Geoff the Medio wrote:I suggest using a different scheme to assign behaviors than the empire ID. If you really want it to correspond to the log file names, using the AI name might work. That is exposed in the Python API.
No, we need behavior to continue matching empire ID even if a game is reloaded; it would not be good for an empire to be subject to potentially radical behavior shifts just because the game was reloaded. It would be convenient if AI names (& the corrsponding log filenames) remained matched to a given empire across game reloads, but that is not the case. That is one of the reasons my charting script includes so much information in the legend, to make it easier for me to match up an empire to its log file after a game reload.

Morlic, take a look at the charting script in default/AI/charting/charts.py

you can count on empire ID and empire name to continue matching across reloads, but player name and player ID can vary.
If I provided any code, scripts or other content here, it's released under GPL 2.0 and CC-BY-SA 3.0

User avatar
Geoff the Medio
Programming, Design, Admin
Posts: 13603
Joined: Wed Oct 08, 2003 1:33 am
Location: Munich

Re: [Question] EmpireID vs AI index

#6 Post by Geoff the Medio »

Dilvish wrote:No, we need behavior to continue matching empire ID even if a game is reloaded...
The purpose, I thought, of the question was to get a reliable correspondance between AI log file name and what behaviour the scripts used, for testing purposes. I don't think this is intended to be a permanent AI behaviour choice system... If it is, then yes, use the empire ID... or something else entirely.

Morlic
AI Contributor
Posts: 296
Joined: Tue Feb 17, 2015 11:54 am

Re: [Question] EmpireID vs AI index

#7 Post by Morlic »

Sadly, for many tests I do need the savegame compatibility, thanks for the warning.

As there appear to be only non-trivial workarounds, I will continue to check the empireID in the logfiles then.
If I provided any code, scripts or other content here, it's released under GPL 2.0 and CC-BY-SA 3.0

User avatar
Dilvish
AI Lead and Programmer Emeritus
Posts: 4768
Joined: Sat Sep 22, 2012 6:25 pm

Re: [Question] EmpireID vs AI index

#8 Post by Dilvish »

Morlic wrote:When playing with multiple AIs it would be nice to know which AI has which empireID (and thus follows which behaviour) to find the interesting logs more quickly without having to look up the empireID first inside each log.
In 79ac524 I just augmented the response from Cj's interactive debugging, quite handy for this purpose-- simply type 'help' as a chat message and one of the AIs will respond with (among other things) a list of the empires that includes player id (which in a game with only one human should always be one greater than the AI_X name)
If I provided any code, scripts or other content here, it's released under GPL 2.0 and CC-BY-SA 3.0

Morlic
AI Contributor
Posts: 296
Joined: Tue Feb 17, 2015 11:54 am

Re: [Question] EmpireID vs AI index

#9 Post by Morlic »

Dilvish wrote:
Morlic wrote:When playing with multiple AIs it would be nice to know which AI has which empireID (and thus follows which behaviour) to find the interesting logs more quickly without having to look up the empireID first inside each log.
In 79ac524 I just augmented the response from Cj's interactive debugging, quite handy for this purpose-- simply type 'help' as a chat message and one of the AIs will respond with (among other things) a list of the empires that includes player id (which in a game with only one human should always be one greater than the AI_X name)
Seems not to be correct.

Started a game with 10 AIs.

For example, AI_1.log corresponds to player id 11. Then again, AI_2.log corresponds to player id 2, AI_6.log to 9 and so on.
AI_1.log wrote: 2015-04-18 00:42:14.760518 [debug] AI : ClientNetworking::SetPlayerID: player id set to: 11

However, playerID and empireID appear to be identical at all game starts.

/Edit:

Code: Select all

            for player in fo.allPlayerIDs():
                if not fo.playerIsHost(player):
                    chat_human('  <rgba {0.colour.r} {0.colour.g} {0.colour.b} {0.colour.a}>id={0.empireID} name={0.name}</rgba> player_id={1}'.format(fo.getEmpire(player), player))
as far as I read the C++ code, fo.getEmpire takes empire_id as argument? If so, of course fo.getEmpire(player).empireID == player.
If I provided any code, scripts or other content here, it's released under GPL 2.0 and CC-BY-SA 3.0

User avatar
Dilvish
AI Lead and Programmer Emeritus
Posts: 4768
Joined: Sat Sep 22, 2012 6:25 pm

Re: [Question] EmpireID vs AI index

#10 Post by Dilvish »

Morlic wrote:Seems not to be correct.
Bah, sorry, I guess I didn't test it enough. Still, that will be the best spot for us to deal with this. Something is quite odd though. I just tried out using this line:

Code: Select all

                    chat_human('  <rgba {0.colour.r} {0.colour.g} {0.colour.b} {0.colour.a}>id={0.empireID} empire_name={0.name}</rgba> player_name={1}'.format(fo.getEmpire(player), fo.playerName(player)))
which really seems to me like it should work properly for our purpose, but it does not seem to do so. I don't have time to test it more right now, perhaps you could take a look at it.
If I provided any code, scripts or other content here, it's released under GPL 2.0 and CC-BY-SA 3.0

Morlic
AI Contributor
Posts: 296
Joined: Tue Feb 17, 2015 11:54 am

Re: [Question] EmpireID vs AI index

#11 Post by Morlic »

Not sure what you mean with "not working".

It seems to me the player_name is corresponding to the correct AI log. So this seems to work for my initial intentions.

Anyway, I propose to change

Code: Select all

fo.getEmpire(player)
to

Code: Select all

fo.getEmpire(fo.playerEmpireID(player))
eventhough it seems that playerID == empireID it feels strange to not look it up anyway.
If I provided any code, scripts or other content here, it's released under GPL 2.0 and CC-BY-SA 3.0

User avatar
Dilvish
AI Lead and Programmer Emeritus
Posts: 4768
Joined: Sat Sep 22, 2012 6:25 pm

Re: [Question] EmpireID vs AI index

#12 Post by Dilvish »

Morlic wrote:Not sure what you mean with "not working".
I was getting different results than I get from my charting script, which I have used a long time and trust. It looks to me like it is due to the bug you have noticed, even if you didn't quite label it as a bug. With that fix, the help output is now giving me the same info as my charting script.
It seems to me the player_name is corresponding to the correct AI log. So this seems to work for my initial intentions.
Ok, great, assuming you mean that it works with the change you proposed. Because that makes all the difference.
Anyway, I propose to change

Code: Select all

fo.getEmpire(player)
to

Code: Select all

fo.getEmpire(fo.playerEmpireID(player))
eventhough it seems that playerID == empireID it feels strange to not look it up anyway.
playerID != empireID, and you are absolutely right about this change being needed. This bug wasn't apparent in the results before because the script was not actually trying to do anything with the playerID at this location other than retrieve an empire, and even it it retrieved the wrong empire it didn't matter so long as it did so in a non-duplicative way. So, this is why the results had been looking wrong to me, because they were.

I just put this fix in, and also added a display of empire ID to the pedia page for empires. Adding player name and ID there would be even more helpful, if no one objects.
If I provided any code, scripts or other content here, it's released under GPL 2.0 and CC-BY-SA 3.0

Post Reply