GEM Advisor Behavior

hulkster

Chieftain
Joined
Aug 5, 2010
Messages
15
Hi Guys

When I play Civilization V I use advisers' advises to spice the things up for me and introduce random elements into my game play. I don't like to follow a victory route every time but rather play like "why don't I follow a route this guy is suggesting for the moment"

This includes both the Advisor screen and the little icons on build and research screens "your advisor is recommending you build/research this"

This works very good for me in unmodded Civilization, however in GEM there's a slight problem.

Whenever I reach the naval techs the advisers start suggesting I build Liburna and later ships (there's at least 2 of them, sometimes 3). I can be at negative gold and they'd still recommend building ships.

Do you have any idea why is this happening?
 
I would prefer that our modders concentrate on more critical matters.
There are probably more pressing matters.

Why don't you figure out the AI's logic?

Sorry if I was rude.
 
Hi

The matter didn't let me sleep so I jumped into Civilization V SDK DLL Source Code and I've thought that I'd share my findings with you.

Most functionality of the Advisor mechanism is covered in the CvAdvisorRecommender.cpp

The algorithm is quite simple actually.

First - the UpdateCityRecommendations function (I have not checked but I assume it is updated every turn)

1) Create an empty list
2) Fill it with all elements a city can build (buildings, units, projects). Each element consists of 2 elements:
buildable - contains the type (unit, building, project), the index and the turns needed to build
iWeight - this is the "weight" of the element - important value, more on it later
3) Reweight all the elements by including the Turns to complete. This is done in CityStrategyAIHelpers::ReweightByTurnsLeft
4) Once we have all the weights calculated sort the list descending
5) Take x top elements from the list where x is the number of advisors available (4 unless a mod adds more)
6) Now for each of those x elements (starting at the top one) review which advisor(s) would recommend it. This is done by reviewing the flavors assigned to the particular buildable element and comparing those against the advisor preferences.


This was actually a surprise for me because I've thought that the mechanism would be different. If I have coded it, I would make each of the advisers go through the list of buildables and find the one that matches his/hers flavor the most. Instead we have a universal method of calculating Weight (or Score as the source also calls it) and then just assigning the Advisor if the particular buildable fits his style in some way.


Getting back at our mysterious Liburna the conclusion at this point is it's stats must make it very strong for the weight calculation and also flavors assigned to it must match Military, Foreign and Science advisers.


To get to the bottom of this we must investigate the Weight calculation algorithm. This a little troublesome when done in Notepad++ and not Visual Studio. If I wasn't lost on the way I tracked the function calls to the CvUnitProductionAI.cpp, CvBuildingProductionAI.cpp and CvProjectProductionAI.cpp.

Looking at the big picture what these functions do is basically add flavor weights (they sum up). There is also a function to recommend the best unit/building/project to build.


Recollecting my previous GEM experiences naval units get recommended the most however, the Vanguard units also get recommended a lot.

Reviewing GEM GEAI_End_Flavors.sql few things stand out

Liburna:
1) All UNITCOMBAT_NAVALMELEE get FLAVOR_NAVAL
2) Additionaly Liburna gets FLAVOR_NAVAL_RECON
3) 'FLAVOR_NAVAL', 'FLAVOR_NAVAL_RECON' get doubled

Scouts:
1) All UNITCOMBAT_RECON get 'FLAVOR_HEALING', 'FLAVOR_PILLAGE', 'FLAVOR_VANGUARD'
2) Then again all those 4 get: 'FLAVOR_RECON' 'FLAVOR_DEFENSE', 'FLAVOR_CONQUEST', 'FLAVOR_CITY_DEFENSE'
3) Flavor for Vanguards get's doubled


In conclusion I believe Vanguards and Melee Naval units have too much flavor added which in turn causes the AI to want to build more of them than other units/buildings and breaks the advisor feature.

Can this be treated as a bug?

@Thalassicus, could you include the Flavor totals into your GEM_Info_ArmiesCities.xls? That way we could see the totals of Flavors for all units
 
Now this sounds like useful research if we can fix the behavior of the AI. I've seen that most AI build vast armadas of liburnas and armies of sentinels.
I believe he did a fix to reduce sentinels in this patch.

This AI flavor is surely not in the Advisor. Better to turn him off unless you want to hear what the AI is doing.
What are the areas in the code that is getting the AI to overproduce to the point of uselessness?
 
Well, the CvCityStrategyAI::ChooseProduction function is used bor each city to determine what it should build (unit, building or wonder). The function is huge so I'll roll out the investigation results in turns. The first step is:

1) AI operates using operations - tasks it decides to do. These tasks are:

AI_OPERATION_CITY_CLOSE_DEFENSE,
AI_OPERATION_RAPID_RESPONSE,
AI_OPERATION_BASIC_CITY_ATTACK,
AI_OPERATION_DESTROY_BARBARIAN_CAMP,
AI_OPERATION_FOUND_CITY,
AI_OPERATION_PILLAGE_ENEMY,
AI_OPERATION_SNEAK_CITY_ATTACK,
AI_OPERATION_SMALL_CITY_ATTACK,
AI_OPERATION_MERCHANT_DELEGATION,
AI_OPERATION_NAVAL_BOMBARDMENT,
AI_OPERATION_NAVAL_SUPERIORITY,
AI_OPERATION_COLONIZE,
AI_OPERATION_QUICK_COLONIZE,
AI_OPERATION_NAVAL_ATTACK,
AI_OPERATION_NAVAL_SNEAK_ATTACK,
AI_OPERATION_CITY_STATE_ATTACK,
AI_OPERATION_CITY_STATE_NAVAL_ATTACK,
AI_OPERATION_NUKE_ATTACK,
AI_OPERATION_PURE_NAVAL_CITY_ATTACK,


This is the 1st thing AI checks when looking what to build. It checks what type of unit it needs from the following list:

UNITAI_UNKNOWN, // we don't know what to do with these units
UNITAI_SETTLE, // these are Settlers
UNITAI_WORKER, // these are Builders
UNITAI_ATTACK, // use these to attack other units
UNITAI_CITY_BOMBARD, // use these to attack cities
UNITAI_FAST_ATTACK, // use these to pillage enemy improvements and attack barbarians
UNITAI_DEFENSE, // these are units that are mainly in the floating defense force
UNITAI_COUNTER, // these are counter-units to specific other units - these will likely need more logic in building and using
UNITAI_RANGED, // units with ranged attacks
UNITAI_CITY_SPECIAL, // more AA???
UNITAI_EXPLORE, // scouts, etc.
UNITAI_ARTIST, // great person
UNITAI_SCIENTIST, // great person
UNITAI_GENERAL, // great person
UNITAI_MERCHANT, // great person
UNITAI_ENGINEER, // great person
UNITAI_ICBM, // nuke
UNITAI_WORKER_SEA, // work boats
UNITAI_ATTACK_SEA, // naval melee units
UNITAI_RESERVE_SEA, // naval units used defensively
UNITAI_ESCORT_SEA, // naval units tasked to defend embarked units
UNITAI_EXPLORE_SEA, // naval units used for scouting
UNITAI_ASSAULT_SEA, // naval ranged units
UNITAI_SETTLER_SEA, // UNUSED in Civ 5
UNITAI_CARRIER_SEA, // aircraft carrier
UNITAI_MISSILE_CARRIER_SEA, // missile carrier
UNITAI_PIRATE_SEA, // avast, ye
UNITAI_ATTACK_AIR, // bombers
UNITAI_DEFENSE_AIR, // fighters
UNITAI_CARRIER_AIR, // planes on boats
UNITAI_MISSILE_AIR, // cruise missiles
UNITAI_PARADROP, // paratrooper
UNITAI_SPACESHIP_PART, // spaceship part that needs to be taken to capital
UNITAI_TREASURE, // treasure to return to your capital
UNITAI_PROPHET, // great person
UNITAI_MISSIONARY, // missionary
UNITAI_INQUISITOR, // inquisitor
UNITAI_ADMIRAL, // admiral


It browses through all units that the city can build that match the type and add it to the list. At this point each unit that the city can build has a Weight value which from my understanding is a combination of Leader flavors, Unit flavors and the City Strategy Flavor (so LEADER_FLAVOR_RECON*UNIT_FLAVOR_RECON*CITY_STRATEGY_RECON + LEADER_FLAVOR_OFFENSE*UNIT_FLAVOR_OFFENSE*CITY_STRATEGY_RECON...) <- I might be wrong about this as this part of code is not that clear but that is my understanding.


Now the weight vector is reweighted to include the turn cost. After that it's sorted descending and the first unit from the list is taken.

Ok, enough for now. I will continue the explaining some other day. Finished at line 792.
 
That's fascinating.
I play Panagea maps and notice that most civs with a coast build ships non-stop.
Particularly the Liburna, then it's descendant, the Galleon.
I once assaulted Rome and had to fight off no less than 40 Galleons, losing Rome 9 times until there was nothing left in the city.

This is an important matter.
A capital with so much production should be building Wonders, buildings and useful units.
I just walked up to Rome with my little army.
Where were the legions?
They were all out at sea.

Rome refused to make peace fooled by the size of its huge, useless Navy.
 
I still believe this is caused by the Flavor changes introduced into the AI system.

When you review the original Flavor values for Frigate for example you get:

FLAVOR_NAVAL - 12
FLAVOR_NAVAL_RECON - 3

15 in total

Now when you review the GEAI__End_Flavors.sql:

FLAVOR_NAVAL - 16
FLAVOR_NAVAL_RECON - 16

32 in total, more than twice the original value.
 
Back
Top Bottom