3.19 AI Tech Choice Code

Wreck

Prince
Joined
Jan 4, 2006
Messages
331
Since it came up in the Pro/Agg argument, I got interested in how the AI chooses its next research item. The code for this is in the file CvPlayerAI.cpp, in the function CvPlayerAI::AI_bestTech(). It is a very long function, and rather hard to understand in terms of what it does in the game. But we can try! A lot of the work has already been done. I found a fairly accurate decription in this thread:
http://forums.civfanatics.com/showthread.php?t=205759&page=3
and there are probably others, too. Note that the thread above is a Fall from Heaven mod thread, but it appears to describe the same code I am looking at quite well.

Anyway, here is a somewhat adapted version of the description linked above.

The BTS 3.19 AI Tech Choice Code

All techs within a pathlength of one to three techs up the tree are evaluated. The tech with the best value is chosen.

(1) The base value of a tech is largely random:
  • value = 1 + rand(0-1999) + any research progress

(2) To that, a great many small bonuses (in the range of 100-200 points) may be added. I am ignoring all of the small ones, and rare ones (i.e. there are a number of them for AIs alone on a continent). I will try to list all bonuses of 500+, and in the order they are defined in the code. Note that when I list a particular tech, that is not what the code is looking at: it only uses the features of techs, not the techs themselves.
  • for Alphabet: +500, and +500 more for each known civ
  • for Currency: +600 (just +200 if have not met anyone)
  • for Writing and have met someone: +500, or +900 with a coastal city
  • for Biology: +500
  • for Fishing: +600 per coastal city
  • for ability to trade across coast/ocean: +100 per foreign city on other continents. If AI has coastal city: +400, or +1000 if alone on continent
  • for ability to trade on river: +1000
  • for unlocking military units. This is based on an attribute units have called their UNITAI, which seems to be basically a hint to AIs as to how to use them. Most fall below the 500 point threshold for noting here, particularly naval units. Land units, especially when the AI is planning a war, get values of 800 or more.
  • for unlocking a unique unit: +600 in addition to the military unit bonus
  • for unlocking buildings. Typically, they're worth 150 each. A unique building is worth 750. Lots of other small or rare modifiers.
  • the highest-value building gets its value added again
  • +500-1300, if the pathlength is 1, and the tech enables building at least one wonder
  • +1000 for Projects (Apollo, spaceship parts)
  • if the pathlength is 1 or 2, and nobody has the tech yet
    • +1-3600 for a unfounded religion (only 2/3 that if pathlength is 2). AIs with a favorite religion want it bad, adding +1200; otherwise they weight it 2/3 as much. As religions are founded their value declines: divide amount by (1 + #holycities), although this factor does not decline for AIs with AI_STRATEGY_CULTURE1, who instead add +500. Then, if AI has no religion in any city, 2x the value, +500.
    • +100 + rand(2400) for unfounded corps
    • +200 + rand(3200) for a tech with a free great person still available
    • +200-3400 for tech granting a free tech (Liberalism)

(3) Then some addends coming from values defined in XML files, that I am not sure of the magnitude of:
  • the tech's AI weight is added (from the CIV4TechInfos.xml, field <iAIWeight>).
  • (if non-human), any flavors of the leader (from their XML file) are multiplied by 20 times the flavor weight of that tech (from their XML file).

(4) Finally, there are some multiplicative factors. These will tend to carry a lot of weight. The value #turns_left is the number of turns left to research the tech for that AI; since the AI has the same science output for all evals, in essence all it does is build in the cost of the tech.
  • if it is a repeatable tech (namely, Future Tech), the value is divided by 10.
  • if evaluating a free tech (i.e. the AI won Liberalism), the value is multiplied by #turns_left/10
  • if this isn't a free tech, the value is multiplied by 1000/(#turns_left + 1)
  • multiply value for would-be monopoly techs, but just 10% of the time. A tech that is unknown to all known civs gets its value increased 25%*(#teams - 2), up to max of 150%.
  • the minimum value for a tech is always 1.
 
A few myths can be immediately dispatched using the info above.

First AIs do look ahead. However, unfortunately for them, it does them little good. This is because the time to research techs up the tree is large, but the AIs do not add any value for the intermediate techs.

Second, AIs do evaluate for tech-trade value. However, unfortunately for them, it does them little good, because they only do it 1/10 of the time.

Third, AIs essentially ignore what tech the human and other AIs have, other than prioritizing religions, free techs, and free GPs.
 
Doesn't this essentially mean the following: Let's say Cyrus has a corn next to him, and nothing else. He has the potential to research Mining!?
I never knew that.
 
Persians start with agri...

Excellent! I've been very curious about this for a long time!

Edits:
since the AI has the same science output for all evals, in essence all it does is build in the cost of the tech.

Don't forget about pre-req discounts...

Then some addends coming from values defined in XML files, that I am not sure of the magnitude of:

The highest flavor for a leader is 10, and likewise for a tech. Thus, the max value (which would be rare) here is (10*10*20) = 2000.
 
oops cyrus wasn't the best choice.
imagine another leader, then :P
 
Interesting. You can't rely on AI getting alphabet as much if there are few other civs. This is pretty good to know! I also find it surprising that there is no parameter for what resources are laying around, much as MrDoe noted.

The latter isn't so useful, but interesting.
 
There are bonuses for techs unlocking resources and improvements. The bonuses just are not particularly large, at least by my initial reading. But I can see that I might not have read that part well; it is pretty opaque. Further looking at it, it seems like there is a fairly large weight on techs unlocking resources, including code that looks at the number of improvable resources tiles.
 
I am a software engineer so I love to see how games work behind the scenes. Thanks!
 
What about AI's the start with Mysticism and the choice of Mediation/Polytheism? Do they ever go for Polytheism as their first tech?
 
What about AI's the start with Mysticism and the choice of Mediation/Polytheism? Do they ever go for Polytheism as their first tech?

Yes, but not often, was the answer when I last investigated.

On v313, I hacked the random number generator so that it would always return the middle number for a tech decision dice roll, and added code that would log the results. What I remember is that Gandhi would consistently break for Poly (a cultural tech) where the other leaders would break toward Meditation.
 
Yes, but not often, was the answer when I last investigated.

On v313, I hacked the random number generator so that it would always return the middle number for a tech decision dice roll, and added code that would log the results. What I remember is that Gandhi would consistently break for Poly (a cultural tech) where the other leaders would break toward Meditation.

Great info - thanks!:goodjob:
 
Let's try to use these rules to make a guess as to what tech an AI may choose for their FIRST tech:
Spain: Starts with Fishing and Mysticism

Possible techs/tech cost/Unlocks (let&#8217;s restrict it to single step path length, since research time to 2nd and 3rd steps would probably outweigh things ).

Sailing/100/Galley/Lighthouse/GL/Moai
Wheel/60/nothing
Agriculture/60/Improve farm resources
Hunting/40/scout/spearman
Meditation/80/Monastery/Buddhism
Polytheism/100/Parthenon/Temple of Artemis/Hinduism
Mining/50/improve mine resources

Potential buildings/hammer cost:
Lighthouse/60, Monastery/60 How do you decide which building is the highest value? Let&#8217;s stick to 150 for each.
For units, lets just assign a value of 250 for each. AI will not be planning war on turn 0.

Bonuses:
Sailing: 250 Galley, 150 Lighthouse, +500-1300 wonders (RAND or assigned?)
Wheel: none
Agriculture: none
Hunting: 250 scout, 250 spearman
Meditation: 150 monastery, +1-3600 religion (is a RAND value?)
Polytheism: +500-1300 wonders, +1-3600 religion
Mining: none


If we apply average values for items with ranges and add 1000 for the RAND value for each tech:

Total additive value:
Sailing 2300
Wheel 1000
Agriculture 1000
Hunting 1500
Meditation 2950
Polytheism 2700
Mining 1000

Now let&#8217;s give her a tech rate of 9/turn to calculate the multiplicative value of 1000/(#turns left+1) for tech costs, and do the math:

Sailing 2300 * 76.9 = 176,923
Wheel 1000 * 125 = 125,000
Agriculture 1000 * 125 = 125,000
Hunting 1500 * 166.7 = 250.050
Meditation 2950 *100 = 295,000
Polytheism 2700 * 76.9 = 207,630
Mining 1000 *166.7 = 166,666

So, on average Meditation wins! Sounds like Isabella to me!

We can see now why Civs that start with Mysticism are inclined to go for early religion. Based on VoU's experience, we might guess that Ghandi has a high flavor value skewing him towards Polytheism. I imagine we can do a similar analysis to come up with a best guess of what each AI Civ will choose as their first tech. You can see that the starting tech combo may have a big influence on first tech choices!
 
Additional point: You can see that cheapo Hunting gives a very high value, perhaps this skews Civs towards Archery? At least on levels where they do not already have this tech...
 
Does this mean that if, for reasons unknown, the human player wants to found a religion, he is much wiser to go for Polytheism than Meditation?
 
Does this mean that if, for reasons unknown, the human player wants to found a religion, he is much wiser to go for Polytheism than Meditation?

Well, usually if the human wants a holy city of a religion, you're better to go for Bronze working than either :hammer: but yes, if I decide early to try for a religion, I will never try for meditation.
 
Does this mean that if, for reasons unknown, the human player wants to found a religion, he is much wiser to go for Polytheism than Meditation?

Sure, but you should know that already

Review the games in your Hall of Fame, and take careful note of which turn each religion gets founded.
 
^^Actually, Meditation / Polytheism will be even more heavily weighted over the others by 2000 points each because of Isabella's 10 religion flavor.

vv Rather than another post... Gandhi has a 10 culture flavor, and Polytheism has a 9 culture flavor, so its weighted by an extra 1800. Meditiation (7) only gets 1400.
 
Gandhi would consistently break for Poly (a cultural tech) where the other leaders would break toward Meditation.
I would guess that this is because Gandhi has Hinduism as his favorite religion, no? If he does, then there will be a large bonus to Poly that will easily outweigh the 25% higher techcost.
 
Using a similar analysis to obtain values for each available starting tech:

Fishing/40/work boat
Sailing/100/Galley/Lighthouse/GL/Moai
Wheel/60/build roads
Pottery/80/Granary, improve cottages
Agriculture/60/Improve farm resources
Animal Husbandry/100/reveal horse, improve pasture
Hunting/40/scout/spearman
Archery/60/Archer
Mysticism/50/Monument, wonder
Meditation/80/Monastery/Buddhism
Polytheism/100/Parthenon/Temple of Artemis/Hinduism
Mining/50/improve mine resources
Masonry/80/walls, wonders, improve quarry
Bronze working/120/axeman, reveal copper


Fishing 1250 * 166.7 = 208,375
Sailing 2300 * 76.9 = 176,923
Wheel 1000 * 125 = 125,000
Pottery 1150 * 100 = 115,000
Agriculture 1000 * 125 = 125,000
Animal Husbandry 1000 * 76.9 = 76,900
Hunting 1500 * 166.7 = 250,050
Archery 1250 * 125 = 156,250
Mysticism 2050 * 166.7 = 341,666
Meditation 2950 *100 = 295,000
Polytheism 2700 * 76.9 = 207,630
Mining 1000 *166.7 = 166,666
Masonry 2050 * 100 = 205,000
Bronze working 1250 * 71.4 = 89,285

Rank order:
Mysticism 341,666
Meditation 295,000
Hunting 250,050
Fishing 208,375
Polytheism 207,630
Masonry 205,000
Sailing 176,923
Mining 166,666
Archery 156,250
Agriculture 125,000
Wheel 125,000
Pottery 115,000
Bronze working 89,285
Animal Husbandry 76,900

So this would indicate that AIs would be very biased towards Mysticism first! Perhaps the coastal bonus to Fishing will bump it up for some, but still...

Also, look where BW and AH rank: At the very bottom. Perhaps AH gets a bonus for giving the potential for Chariot (maybe not until wheel?)? I think it basically says most AIs will go hunting/archery before bothering with the rush options.

On the other hand, I often see AIs going into slavery early, so perhaps there are other bonuses for BW at play.
 
LuCiver: it is a great idea to get an listing of what is likely early for which leader. But I think for most AIs it is going to be a real tossup. Having Mysticism does look like it should heavily induce religion attempts. And a unique building or (less so) unique unit will also tend to skew things a lot. (I just noticed the UU bonus was missing and added it.)

The Wheel, AH, BW, Agriculture and Mining should all get bonuses based on unlocking improvments and/or resources, but I am not really clear on those as yet.

Also, don't forget the tech flavors. I am not sure how big the numbers are there (would have to look at XML for that). But if they are in the range of say 10 each for leader and for tech, then that would be around 2000 points -- enough to matter a lot.
 
Back
Top Bottom