AI Code Organization

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

Moderators: Committer, Committer

Post Reply
Message
Author
User avatar
Cjkjvfnby
AI Contributor
Posts: 471
Joined: Tue Jun 24, 2014 9:55 pm

AI Code Organization

#1 Post by Cjkjvfnby » Tue Feb 03, 2015 12:00 am

I will try to explain my ideas about code organisation.

Current cashes:
  • cache - variables stored during turn to have easy access to data. Cleared before turn.
  • permanent cache - data stored in AIState. Same reasons as cache but stored across game. Validated by code.
  • session cache - optimization to avoid calculating some values too often (not implemented as I know)
Current code has mix of filling cache, reading cache and making decisions in functions for hundred lines of code.

What I want:
split code for two layers:
- information layer
- decision layer

Information layer will produce information. How many planets I have?, What can be colonized?, What enemy fleet are near? Give rate of current designs?. All work with filling and reading caches should be done here. There will be a lot of simple function that answer single question.

Decision layer This is most important part. It should be as small as possible. Get info in couple of lines, make decisions.

https://github.com/freeorion/freeorion/ ... AI.py#L661
This function calculates a lot o useful information (>500 LOC). But it should be splitted for many small functions.

settle_suitability: can we live on this planet?
species_feature: what strength of defense? did we need this species in our empire? why it is in colonization code?
distance and supply: is it in our supply range? which way it can be colonized? is it in war zone?
...

Decision layer will ask this question and rate planet by its own rules. Code like

Code: Select all

if "COMPUTRONIUM_SPECIAL" in planet.specials:
should be placed in info layer and never be on decision layer.

This implementation will enlarge codebase but will make it more simple and structured.
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, Programmer
Posts: 4768
Joined: Sat Sep 22, 2012 6:25 pm

Re: The State of the AI

#2 Post by Dilvish » Tue Feb 03, 2015 1:17 am

Cjkjvfnby wrote:I will try to explain my ideas about code organisation....
That will warrant a fair bit of discussion, and this isn't really the place for that...
Dilvish wrote:...Anyone wanting to discuss some aspects of this should probably post in the Design forums rather than reply here...
As a reminder, if you wish to discuss any of this please do so in the Design Forum.
Geoff, it appears I don't have moderator rights here -- would you please split off this and Cjkjvfnby's post above into a new thread in the Programming forum (or wherever you think best)? Thanks.
If I provided any code, scripts or other content here, it's released under GPL 2.0 and CC-BY-SA 3.0

Mitten.O
Programmer
Posts: 255
Joined: Sun Apr 06, 2014 4:15 pm

Re: AI Code Organization

#3 Post by Mitten.O » Tue Feb 03, 2015 8:58 pm

I haven't really even looked at the AI code, so I cannot comment on the specifics, but I am very much in support of modularization in general. The set up you describe sounds like the information layer could then be very useful for potential entirely different AI implementations.
Any code by me in this post is released under GPL 2.0 or later.

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

Re: AI Code Organization

#4 Post by Dilvish » Tue Feb 03, 2015 11:21 pm

Cjkjvfnby wrote:What I want:
split code for two layers:
- information layer
- decision layer
Further increasing the separation between these tasks sounds like a *generally* fine idea to me. The AI does generally start each turn with a large information gathering step and then alternates between decisions and some smaller information gathering steps. Most of the existing individual functions do already primarily serve either one or the other of these purposes, although they are spread out among the different modules dependent on the subject matter being dealt with, and many of the "decision" functions still do at least some small amount of direct information gathering.

So, it sounds like this proposal is primarily one of grouping the information gathering functions together into a single module, refactoring many of the larger functions into smaller, more focused pieces, and then refining the various decision functions so that they are more purely decision-only.

Certainly, a number of the functions have grown quite large, and refactoring them into smaller pieces would surely be helpful. Regrouping everything according to 'layer', well, might be helpful. The current set of modules is one I mostly inherited; I'm not particularly wedded to the way things are grouped if there is a better organization to be had. Did you have any more specifics to offer regarding your ideas for the layers?
Decision layer This is most important part. It should be as small as possible. Get info in couple of lines, make decisions....Decision layer will ask this question and rate planet by its own rules. Code like

Code: Select all

if "COMPUTRONIUM_SPECIAL" in planet.specials:
should be placed in info layer and never be on decision layer.
Hmm, well if you expect your 'decision' layer to be making any nontrivial decisions with only a couple of lines of information, and that it would not be making queries like whether a planet had a COMPUTRONIUM_SPECIAL, then it seems to me like the real guts of the decision code would actually be embedded in your 'information' layer, and your 'decision' layer would really just be primarily converting previous decisions into game Orders. But perhaps I'm misunderstanding what you are really proposing.

In conclusion,
(1) It sounds like something we both definitely agree on is that some of the larger functions would be better off refactored into smaller chunks.

(2) Also, regrouping the existing functions based on whether they are primarily collecting and compiling information, versus making decisions based on that information, sounds like a generally ok idea to me as well. Do you mean to propose just two modules, or two higher level modules with submodules, or what?

As for what I understand to be a vision for an ultra-streamlined decision layer, I think I may not be really understanding you right. You could try to clarify this aspect of the proposal right now, or perhaps we could try to first proceed with (1) and (2) and then it would probably be easier to have a more focused discussion on streamlining the 'decision' layer and perhaps moving more code into the 'information' layer.
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: 471
Joined: Tue Jun 24, 2014 9:55 pm

Re: AI Code Organization

#5 Post by Cjkjvfnby » Thu Feb 05, 2015 10:01 pm

Dilvish wrote: In conclusion,
(1) It sounds like something we both definitely agree on is that some of the larger functions would be better off refactored into smaller chunks.
Yes
(2) Also, regrouping the existing functions based on whether they are primarily collecting and compiling information, versus making decisions based on that information, sounds like a generally ok idea to me as well. Do you mean to propose just two modules, or two higher level modules with submodules, or what?
each python file is module :) Each layer is group of modules.
As for what I understand to be a vision for an ultra-streamlined decision layer, I think I may not be really understanding you right. You could try to clarify this aspect of the proposal right now, or perhaps we could try to first proceed with (1) and (2) and then it would probably be easier to have a more focused discussion on streamlining the 'decision' layer and perhaps moving more code into the 'information' layer.
I have some feelings but I am not ready to convert it to text. Lets do 1) and 2)
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: 471
Joined: Tue Jun 24, 2014 9:55 pm

Re: AI Code Organization

#6 Post by Cjkjvfnby » Thu Feb 05, 2015 10:09 pm

Right now I am thinking about one more feature.

Update c++ api from python.

add __str__ function to children of UniverseObject and other classes.
add some properties/function to have code more readable.

Implementing this in c++ is waste of time. Adding it through python is easy and fast.
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, Programmer
Posts: 4768
Joined: Sat Sep 22, 2012 6:25 pm

Re: AI Code Organization

#7 Post by Dilvish » Fri Feb 06, 2015 12:20 am

Cjkjvfnby wrote:Right now I am thinking about one more feature. Update c++ api from python.
add __str__ function to children of UniverseObject and other classes.
add some properties/function to have code more readable.
Implementing this in c++ is waste of time. Adding it through python is easy and fast.
I'm a bit wary of adding extra layers of python calls that might slow things down, but the AI is not a huge CPU hog right now so we don't have to be totally averse to that. If you expect it to be fast and easy perhaps you could give some examples.
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: 471
Joined: Tue Jun 24, 2014 9:55 pm

Re: AI Code Organization

#8 Post by Cjkjvfnby » Sat Feb 07, 2015 7:06 am

Dilvish wrote:
Cjkjvfnby wrote:Right now I am thinking about one more feature. Update c++ api from python.
add __str__ function to children of UniverseObject and other classes.
add some properties/function to have code more readable.
Implementing this in c++ is waste of time. Adding it through python is easy and fast.
I'm a bit wary of adding extra layers of python calls that might slow things down, but the AI is not a huge CPU hog right now so we don't have to be totally averse to that. If you expect it to be fast and easy perhaps you could give some examples.
This slowdowns will be less then measure spread.

https://github.com/Cjkjvfnby/freeorion/ ... 96fc9397b9
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, Programmer
Posts: 4768
Joined: Sat Sep 22, 2012 6:25 pm

Re: AI Code Organization

#9 Post by Dilvish » Wed Feb 11, 2015 4:33 am

Looks fine to me so far. Have you tested it yet to be sure it's working out as you expect?
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: 471
Joined: Tue Jun 24, 2014 9:55 pm

Re: AI Code Organization

#10 Post by Cjkjvfnby » Wed Feb 11, 2015 11:48 pm

Dilvish wrote:
Looks fine to me so far. Have you tested it yet to be sure it's working out as you expect?
Yes it works fine.

Here is log example image with slightly changed format (for highlighting in SublimeText2)
https://drive.google.com/file/d/0B12sy1 ... F1SzA/view
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: 471
Joined: Tue Jun 24, 2014 9:55 pm

Re: AI Code Organization

#11 Post by Cjkjvfnby » Thu Feb 19, 2015 11:42 pm

extender as patch. It will help to simplify debug prints.
Attachments

[The extension patch has been deactivated and can no longer be displayed.]

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, Programmer
Posts: 4768
Joined: Sat Sep 22, 2012 6:25 pm

Re: AI Code Organization

#12 Post by Dilvish » Sat Feb 21, 2015 12:58 am

Cjkjvfnby wrote:extender as patch. It will help to simplify debug prints.
Committed, with slight adjustment to some comments
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