[AI behavior] Improvement decisions

Chalid said:
But there are some other factors as well. One is the additional gain by the improvement is added with weights:
food*4+production*3+commerce*2 and
Sorry, let me restate what you're saying I'm sure I understand: this is the ADDITIONAL gain by the improvement, e.g. a decision by the AI to change from an existing improvement?

So the steps are:
1. Calculate tile's current value for each yield with Improvement A
2. Calculate tile's potential value for each yield with Improvement B
3. Calculate B-A for each yield
4. Weight the difference (4 / 3 / 2 for food/prod/comm) between yield A and B to get a sum
5. Repeat 1-4 for Improvements C...X
6. Pick the best Improvement for this tile
7. Repeat 1-5 for all tiles
8. Pick the highest value and go!

Am I reading you right? Or does the weighting (4/3/2) happen somewhere else (at steps 1 and 2, or at step 6).

Chalid said:
and when more food is generated then needed 21 or 12 points are further added.
21 or 12 points are further added to food, or to other yields?

Chalid said:
No those numbers are strictly hard coded and no XML is involved. To change them you'll have to modify the sourcecode ~ SDK usage.
So what is the purpose of the "weights" node in the Yields XML file?

Chalid said:
The leaderhead weights are multiplied on that with (100+Weight)/100
If steps 1-8 above are accurate, then when does this happen, at step 6.1 presumably?

Chalid said:
I gladly dig into this as it helps understand the way the game and the ai is working and possibly shows where it can be improved. :)
When our team was young and naive we had proposed that one choice for the civ would be to mutate from a terrain/yield-driven mechanism into a building/yield-driven one (a Borg city, as it were). The idea is that this could best be done by building terrain-destroying improvements that gave huge production boosts to build these massive buildings. A human would figure this out pretty easily, but the AI...?

Anyway, would be interesting if the AI could be made to understand present discounted value of a cottage, though presumably only if it understood what that value could buy. I assume the AI has the best thinking that powergamers have brought to bear on the Civ series built into it...?
 
Hmm it seems i havent been absolutly clear on that, but befor discussing further read the post 3 above.. there i solved your initial problem http://forums.civfanatics.com/showpost.php?p=3956011&postcount=19 ;)

its actually:
1. Calculate Weighting factors for Plot (EmphasiseFood, EmphasiseProduction, Emphaissie Commerse, EmphasiseIrrigation, and LowFood)
2. Repeat for all Improvments do:
2a) Can I build this improvement
2b) Calculate Gain by improvment (in contrast to unimproved Tile) and weight this by 4/3/2
2c) Calculate Total Yields and weight those
2d) Add 19 if Spread irrigation and Irrigation emphasised
2e) Add 21 od 12 for each (total food - food per person)
2f) Add weight by Leader
3) Select best improvement
4) Do this for all plots within the city to find the plot that should be improved

If you want the AI to understand a System like you propose it can be done, but some parts of the City Managing AI will have to be changed, and it should actually not be as hard. So your dream is not yet gone ;)

Edit: About the Yeild Weight i haven't found out yet but i will ;)
 
Chalid said:
Hmm it seems i havent been absolutly clear on that, but befor discussing further read the post 3 above.. there i solved your initial problem
Yes, you did, and it's a brilliant solution -- thank you.

But this is so much fun!


1. Calculate Weighting factors for Plot (EmphasiseFood, EmphasiseProduction, Emphasise Commerse, EmphasiseIrrigation, and LowFood) -- this is where the 19-48 Food, 15-35 Prod, 7-8 Comm weight you mentioned earlier plays in? Or ar those applied at step 2c?

2b) Calculate Gain by improvment (in contrast to unimproved Tile) and weight this by 4/3/2 - OK, so this step considers the DIFFERENCE in yield (marginal change).

2c) Calculate Total Yields and weight those - This step considers the FINAL yield (absolute value). Is this, or step 1, or both, where the 19-48 / 15-35 / 7-8 weights come in?

Also, what is the relationship between 2b and 2c? Are the values totaled?

2e) Add 21 od 12 for each (total food - food per person) -- I don't understand this at all. :confused:


So very interesting -- assuming that 2b and 2c are totaled or averaged or something, the AI has a system for caculating MARGINAL change as well as final ABSOLUTE value. This would lead the AI to slightly prefer improving naked tiles rather than changing improvements. However, because the weight is 4/3/2, depending on the interplay between these two steps, there could be a double-weighting in favor food and against commerce? I guess I will wait for the explanation of how steps 2b and 2c interact...

THANKS Chalid.
 
Ist simply added up everything in step 2. In step 1 there is no weighting but a series of yes no descision.
Something like.. oh there nor river near us so we should try to get some irrigation here (Emphasise Irrigation) or oh this tile produces a lot even without an improvement, so lets build something here that even increases the production (emphasise Production)

To give another example: grassland with river that belogs to a city that has low food will will get values like this:
Farm:
2a) +1 food => value = 4
2b) total 3 food (3*48) + 1 Commerce (1*7) + 2a (4) = 155
2d) 1 food surplus (3 Produced 2 needed per population) (+1*21) => Value 176
If the need for irrigation exists another 19 would be added.

Workshop_
2a) -1 food +1production => value -1
2b) total 1 Food(48) 1Production(15) 1 commerce (7) +2a=> Value 69
2d) No food surplus so it statys at 69

Town
2a) +5 Commerce => value 10
2b) total 2 food(2*48) 6 commerce (6*7) + 2a => Value 148
2d) No food surplus


When food is abundand:
Farm:
2a) +1 food => value = 4
2b) total 3 food (3*19) + 1 Commerce (1*7) + 2a (4) = 68
2d) 1 food surplus (+12) => Value 80
If the need for irrigation exists another 19 would be added.

Workshop_
2a) -1 food +1production => value -1
2b) total 1 Food(19) 1Production(15) 1 commerce (7) +2a=> Value 40
2d) No surplus so it statys at 40

Town
2a) +5 Commerce => value 10
2b) total 2 food(2*19) 6 commerce (6*7) + 2a => Value 90
2d) No food surplus

So it depends stongli on the emphasisis calculated in step 1 which is the best improvement for a plot.
 
Thanks for the clarification and example; this really helps. I would point out, though, that all but your last calculation does not include 2a; so the first set should be Farm = 180, Workshop = 68; Town = 158, etc...

Then is this score MULTIPLIED by the leader weight, e.g. if the leader weight is 40, it would be multiplied by 1.4 (100+40/100)? There must be some cap to the weight...

Anyway, the moral of this story seems to be not to stray too far from the core game mechanics or you'll get smashed. :crazyeye: Even with all this explanation we still need to know when the AI decides that "food is abundant" and moves on. But now I know why the AI does what it does...
 
You'll need a weekend of deep meditation (or medication) to clear your head now Gene. I'm sure it hurts by now!
 
Padmewan said:
Then is this score MULTIPLIED by the leader weight, e.g. if the leader weight is 40, it would be multiplied by 1.4 (100+40/100)? There must be some cap to the weight...

Only the Interger cap at 65535...

Anyway, the moral of this story seems to be not to stray too far from the core game mechanics or you'll get smashed. :crazyeye: Even with all this explanation we still need to know when the AI decides that "food is abundant" and moves on. But now I know why the AI does what it does...

Or hire someone who changes the CoreGame via the SDK for you ;)
 
woodelf said:
:wow:

War machines and SDK coding! We need to lure you away from FfH somehow....

HEY!!! Hands off elf boy!
 
woodelf said:
:p

We call him Mr. Elf Boy so he likes us more now. ;)

I meant you! :crazyeye:
 
One more thing to add:

I also found that if two units are able to build the same improvement, the AI will never use the slower unit. Presumably this is related to the fact that the AI never deploys more than one worker to the same tile to do a task (which I also presume to be because every turn the AI could change its mind on what to do with a tile and have workers all undoing each others' work every turn).

In this mod we had an unbuildable Agribot that could instantly build a farm but be consumed in the process (it is coded as a replacement for the worker and warrior in goody huts). That kept the AI from deploying workers to do that same job, and therefore farms never got built.
 
I've done some tests and am fairly confident that the AI does not consider civic effects when calculating improvement values and decision-making.
==========
Parameters
Unmodified:
Improvement A: +1 food
Improvement B: +2 energy

Civic 1:
Improvement A: +2 food
Improvement B: +2 energy

Civic 2:
Improvement A: +1 food
Improvement B: +2 food, +2 energy
==========

Naturally, under Civic 1 the AI spams Improvement A (particularly as our terrain is nutrient-poor).

However, switching to Civic 2 the AI continues to spam Improvement A, even though Improvement B is clearly better. I have done this from scratch using WorldBuilder, to make sure that the AI is not doing this because of the lower marginal value of changing an existing improvement, but even with a blank slate it continues to behave the same.

I suspect this is a bug, perhaps somewhat corrected for in Vanilla by giving the leaderheads who like State Property a predeliction for Workshops.

Can anyone who has looked at the code and spotted the civic adjustment show me where my mistake in the setup or in my reasoning is? Improvement A does not upgrade, etc.
 
Hmm does Improvement A change somewhere later in the tech tree.

I investigated this and found one more thing that i had overlocked. When deciding what to build the flag bOptimal is set, what means all technologies and all Civics that change the Improvement are already taken into consideration. And it takes into consideration that the tile will be irrigated sometime in the future.

So actually the Ai should built Improvement B even before it changes its civic when Improvement A is not changed later in the tech tree.

Quite a mess this improvement placing AI. :(
 
Chalid said:
When deciding what to build the flag bOptimal is set, what means all technologies and all Civics that change the Improvement are already taken into consideration. And it takes into consideration that the tile will be irrigated sometime in the future.
That would make the AI incredibly stupid -- an early-era civ would not refuse to build cottages on unirrigated grass just because it could later build a farm much further down the road (because it needs food NOW). If it's not already there, the AI needs some kind of "present discounted value" analysis and/or at least some sense of how long until said improvement becomes available.

Chalid said:
So actually the Ai should built Improvement B even before it changes its civic when Improvement A is not changed later in the tech tree.
But it doesn't, and so that gives me hope that bOptimal is not controling but rather contributing to the final decision. Didn't you share earlier that sticking in a fake era would cause the AI to not "see" an improvement that arrives in the next era and therefore be willing to build its inferior predecessor?

btw, thanks to some code adapted from Kael, Civics A and B are attached to religions, (e.g. you need Religion B to adopt Civic B) and so when you have no religion (Civic A) you don't have access to Civic B. Neither do you have access to Civic A while you are in Civic B. Don't know how that factors into this analysis, if at all -- it seems to me from your description that bOptimal doesn't care whether the option is available or forthcoming, just possible...

???
 
:bump:
HEEEELLLLPPP!

:D

hate to pile it on, but if anyone has any insight into how the AI chooses civics, that would also be illuminating!
 
Open up CvPlayerAI.ccp, and go to line 5722 - that is where it is determined. It is based on rather a lot of things though. If you want explaination of a little bit of it post it up here, and I'll do a translation of it. I think you could probably read quite a bit of it without knowing much about it.
 
Can someone tell me how to encourage the AI to GUARD a particular resource?

I've noticed that the AI will fortify units on particular Resources...but I haven't been able to figure out what flags will cause it to choose those resources.
 
Top Bottom