Building AI for RTS
AI Fields
The AI is not able to see game by human's eyes. In fact the AI is just an algorithm which can work with numbers and thus it is necessary to transform game data through special interface into data fields with exact meaning (AI field). Minimal unit in the KaM game is 1 tile (= 1 texture in game map). On the tile can be placed for example object, resource, obstacle or warrior. The AI does not know anything about any tile until it check the tile - for example the AI ask a question whether there is unit then save the information and keep asking another tiles. Decision-making process based on such questions is not too effective because it must handle each game tick around 255*255 tiles + not all tiles are walkable / reachable. Therefore, in computer games is created higher structure (often called Navigation Mesh) which merge multiple useful tiles into one unit (polygon). For KaM are needed only 2-dimensional polygons which can be obtained for example by Delaunay triangulation.
The map is then divided into polygons (you can see that polygons are NOT created in mountains):

Every polygon can contain multiple informations - for the city building AI is the most important information player's influence = detection whether are player's buildings in surrounding tiles (or the whole base). My AI also creates polygon routes between mines and decrease chance that in those polygons will be build forest (it is expected that there will be other houses).
Standard game (what the player sees):

AI influence "look":

Another example of the AI field can be array which corresponds with game map and allow or denny building of houses in specific areas (coal mines, forests, protected areas etc.)
City-building AI
Core of city-building AI is divided into 4 main classes:
- TKMCityManagement = main class which controls city interface = unit training (in schools), weapons ordering, ware distribution, blocking wares in storehouse, house repairing, demolishing of exhausted houses and trading (+ simple trading algorithm = secure wood and gold production so AI does not get stuck).
- TKMCityPredictor = this class computes city production, predicts derivation of ware flow and tries to create list of needed houses in dependence on peace time, count of mines, available free space and actual game tick.
- TKMCityBuilder = selection of optimal houses which should be build + organization of roads / fields / shortcuts construction. AI should be able to build city quite fast because it also check free workers and give them work immediately after notification.
- TKMCityPlanner = planning of new houses position: at the input is house type and at the output the "best" possible loc for the house. The "best" position is selected by weighted function which considers surrounding houses, terrain, player's influences etc. This class keeps list of all reserved / placed / destroyed houses and by requirements it sends them to city builder (AI can plan houses without placing house plan, destroyed houses are constructed at the same loc). It is one of more complicated classes which uses Eye class.
This class provides map informations to the AI - gold / coal / iron / stone mine locs + list of forests, procedures which will check whether is possible to place house at specific location etc. It is universal for all AI players and it is located in AI fields.
Example of Eye view:

Genetic algorithm
Not every calculation is straightforward. Sometimes is required approximation or parameter which will change weight of certain element in equation. For example when the AI wants to decide if it place gold mine in loc 1 or loc 2 there is following function:
Actual problems
The AI is now able to produce quite good city. There is still quite large potential to improve but it is question whether it is worth it - as an example I will show you the impact of Random Seed in two games with identical AI, initial conditions, resources and time for production / construction:


You can see that difference between weapons production is almost 60% (I simulated just 10 simulations with identical conditions and pick the best / worst). Producing of an aditional code which could suppress the influence of random noise is too much time consuming. It will also make actual code even more unclear and add another computation requirements.
Do you have any ideas? Do you need to explain something more detailed?