The Big Question - How Does The AI Choose Which Units To Build?

hmm, things got more complicated.

1 thing of which I am sure, the Ai needs some kind of balance. I disbanded all units except the defensive infantry, and sure enough, the ai responded by building exclusively offensive units for quite a while.

There is no methodology becaus eI have yet to set up a scenario, I was just giving my first impressions from a test I was doing.

A more baffling, and surprising observation. It seemed the ai was only building two type of land units, an offensive and a defensive, this seemed to go on for 20 builds... ( I changed govs, and now was cash rushing) So I tired to "nudge" it, into building something else..... and it did. I changed two cities to build a more balanced 8/8/2 80shunit as opposed to 1/3/1 20sh or 16/16/1 120sh unit, (I know.... the numbers are just there to give you an idea)... and amazingly, the Ai then started to build a lot more of these 8/8/2 units...... very strange.
 
http://www.civfanatics.net/uploads5/AI_TEST.zip


Starts at 10 AD, 1 year a turn, so 100 turns would be 110 AD

Directions: unzip to your scenarios folder.

This has to be the most balanced possible "scenario". It is recommended you play as Germany, they are on an island away from the continent.

All build often/never flags are turned off.

Aggression for the four test civs is set to high (5), and alll special abilities have been removed.

Every city is identical to every other city, there are 4 civs, with cities equidistant from eachother. Each city produces 2 shields a turn. This is to test build time. turn up grassland's shield bonus to 4 to get 10 shields a turn.

It is recommeended for first tests only the first two opponents are chosen, and the other two set to none.

the government s minimal corruption, but that doesn't matter because every city has a Forbidden Palace or palace. No support, No rushing and 30 Military Police limit.

Each civ has four cities, who do not share any territory or square.

Grassland is the only terrain, and is set to impassable, for your ai build crunching pleasure. Raise grassland shield production to 5, and hit enter 25 times, go to military advisor, and see any percentage.

Alot of king units are left for customization, if you want/need a cannon to represent a bombard unit, you will need to add it and put an entry in the civilopedia box in the editor... ie PRTO_cannon.

My first crunch was archers, 20/10/1 10sh and and spearmen, 10/20/1 10 sh, flagged O and D, respectively.

Archers/spearmen
monarch
Egypt 41/62 Rome 42/61
 
First test: does difficulty have any bearing on how the Ai chooses it's units? NO

regent (archer 20/10/1 10 sh)/(spearman 10/20/1 10 sh)

Egypt 48/52 Rome 51/49

Monarch

Egypt 41/62 Rome 42/61

Emperor

Egypt 63/67 Rome 53/77
Egypt 59/71 Rome 60/70
Egypt 55/69 Rome 58/66
Egypt 47/53 Rome 51/49

Conclusion: regent seemed to be 50/50, Monarch 40/60, so one would expect Emperor to follow the pattern and be 30/70, this did not happens. Also once the bonus units for difficulty and bonus production was removed, it seemed to be near 50/50 for emperor. My conclusion is no. All tests from now on will be conducted at regent
 
Does aggression have any influence on AI build? No

Rome bumped down from agression of 5 to agression of 1, egypt stays aggression of 5

Rome 43/57 Egypt 49/51
Rome 42/58 Egypt 44/56

This does not seem staistically significant, especially since it seems the ai tends to favor spearmen. There must be another reason, my guess is when war occurs, spearmen will get built at a greater ratio or the ai is hardcoded to build about 54% spearmen or it depends on the number of opponents, which will be tested later.

all tests from now on will be regent, aggression 5 all civs.

of 884 trials, spearmen clearly are favored

Rome 400/484 Egypt 406/478

next question: Does war affect the ai's build decisions?
 
Does war affect the ai's build decison? LIKELY 7%-9% difference in offensive units produced

I am 95% sure it affects whether the ai builds improvements or military, but which military unit, I can't say yet.
METHOD:
turn 1, I built an embassy with both Rome and Egypt.
nither decalre war on eachother, AFAIK, I do place 1 archer next to eachother, so they can have contact, and declare war on eachother. (However, they do not make contact, perhaps I messed up on that front, however, since it was another variable, I am glad I messed up, therefore, the only way they could be at war would be if they were at war with me.

pcts mark archers, archer/spearman

turn 53 egypt declares war on me.
Egypt 16/25 40% archers
Rome 12/29 29% archers
100 builds after egypt declares war on me
E- 63/78 45% overall, 47% since war declared
R-61/80 43% overall, 49% since war declared by Egypt*
*Rome declares war on me turn 68
E- 21/32 40%
R-19/34 36%
100 builds after Rome declares war on me
E-68/85 44% overall, 47% since war declared
R-68/85 44% overall, 49% since war declared
I make peace with both, results after 100 builds
E- 108/125 46% overall, 40/60 40% for peace
R- 108/125 46% overall, 40/60 40% for peace
In 153 turns of peace with Egypt
E- 56/85 40%
In 168 turns of peace with Rome
R- 61/153 40%

These numbers are fairly compelling I think. However, earlier results found 45% archers, after 848 runs. Does it seek an equilibrium? A magic percentage? Overall, the Ai still hovers around 46% but in times of peace, archers are only built 40% of the time overall, and war, 47%-49% of the time. Perhaps it's formula is 45-5-45, with 5% dedicated to "other" units... ony more testing will tell. For now, I am finished with the overall questions of aggression, difficulty, and war/peace.
 
Does the one civ's army affect the building of the others? NO

In this test, Rome can only build archers, will Egypt respond by building more archers? Perhaps more spearmen?

Rome 100/0 100%
Egypt 53/47 53% whoa is this a fluke, only once out of 11 sets of 100 builds has a civ built more archers than spearmen.

try again:

Rome 100/0
Egypt 44/56 it certainly appears to be a fluke.

one more time:

Rome 100/0
Egypt 37/63

hmm... try again

Rome 100/0
Egypt 43/57

last time

Rome 100/0
Egypt 44/56

Conclusion: No, overall average is 44%. This seems to be the average, even over longer trials.
 
Does the ai want equilibrium? For A while I will only test Rome.

First test is a control to make sure even if a civ is alone, it will still act the same.

40/60 40%
add 100 more builds
57/43 57%
Total:
97/103 48%


More data crunching:
If I were to subtract 8 spearman from every peacetime test, (since I started) that started at build 1 and ended at build 100, would it be closer to 50%

Another test result: Rome by itself, what is it's build order the first 12 builds? This is done in 3 turns with 4 builds a turn. (1 unit per city) I observed the 10 of 10 times the first 2 turns were spearmen exclusively, the next turn was 2 archers 2 spearmen 9 out of ten times.

9 ot of ten times it is 2 archers, 10 spearmen once it is 3 archers 9 spearmen. This was to strengthen my hypothesis before continuing forward with a large number crunch: back to the original question: If I were to subtract 8 spearman from every peacetime test, (since I started) that started at build 1 and ended at build 100, would it be closer to 50/50 archers/spearmen?

results:
11 runs of 100 builds in peace done
495/606 44.9% archers
Subtract 88 of those spearmen as "initial defense" units
495/518 48.8% archers
 
Experiment 6 a mod. on experiment 1:

A.I. had 30 infantry and 5 Heavy Artillery 0 Attack
10 Defence 250 Bombard 3 Range ROF 6
This time I had placed AI-cities on another continent.
Thus AI could not make the automatic retreat to cities move.

Human defender had 30 infantry.

To begin with the experiment followed the pattern
from experiment 1 : AI attacking with infantry, heavy artillery
doing nothing. Then when I counterattacked the change come:
(in experiment 1 the heavy artillery did nothing when I
advanced on its cities) When it had my infantry in range
it started to shoot. Soon my remaining infantry were
down to 1HP each and I ended the experiment.


Rocoteh
 
OK, the pictures of a real AI navy now show in my previous post.
I admit I have not done the calculations to check what strength percentage of the global armed forces it represnts.
 
10 first 2 rounds (8 builds) tests. This time with all 4 civs present
(in one round, every city builds 1 unit, for a total of 4 units a round per civ)

Rome=R Egypt=E Greece=G Babylon=B

R archers/spearmen #occurances (out of ten)
1/7 7
2/6 3
TOTAL: 33/67 33%
E
0-8 1
1/7 5
2/6 3
3/5 1
TOTAL: 34/66 34%
G
1/7 6
2/6 4
TOTAL: 36/64 36%
B
1/7 8
2/6 2
TOTAL: 28/72 28%
GRAND TOTAL: 32.75%

This surprised me. I was expecting all spearmen for the first two rounds.

Now, first 2 rounds, Rome only: (no neighbors)
0/8 9
1/7 1
TOTAL: 1% :cool: I will now edit a previous post, having neighbors does effect ai build queues. Why 1%, why did one archer appear like that?

I'll try this: 5/16 = 32% Hmm... 5 civs, but according to this hypothesis, if there were 16 civs, the initial build would be almost exclusively archers.... Also according to this hypothesis, Rome should have built 2/16 or 12.5% archers, because I count Germany as a neighborin the 5/16. This cannot be it, but that one anomoly helped greatly. Whatever the alogorithm is, it does not start with 2 spearmen, but uses a probability.

First two rounds: Egypt and Rome (pct archers)
R
1/7 8
2/6 2
Total: 32%
E
1/7 6
2/6 4
Total: 34%
Grand Total: 33%

there goes the 5/16 theory. Is it because Egypt and Rome could see eachother's borders that they immediately began to build a handful of archers?

First two rounds: Rome and Greece (cross map, cannot see eachother at onset)

azfter two rounds it was obvious they were acting the same as Rome and Greece did, 1/7's and 2/6's.

That is all for me tonight, I haev to make more sci-fi forbidden palaces for my mod.... I'll mull this over for a while....
 
Originally posted by Kryten

A good test Iztvan, but I think you may have forgotten something.....

It is my understanding that units will only use 'defensive bombardment' if they ARE stacked.
One unit on it's own cannot use DB....but others stacked with it can.
So in your experiment, NO unit was actually using DB.

(Could someone please confirm this, as I have been wrong before in this thread! :lol: )

OMG! :eek: All that testing for nothing! Hard work and nothing to show for it! LOL! :lol:

Ok, if you are right it does show one thing - even if I did many tests on the same circumstance (attackers attack value = defender defense value) there were still significant differences in the results. Statistics & probability is tricky business, and a lot of testing is required to verify even a rather simple hypothesis.

By the way: if I'm threadjacking or spamming with these "defensive bombardment" tests, please tell me so, and I'll start a new thread (or classify the results and keep them to myself! :lol: ).

Ok, I tried out a new testing model last night, lets see if I've forgotten something crusial this time:

Terrain: Desert with defencive bonus changed to 0%, i.e. A 1 vs D 1 should result in exactly 50-50 results.
Units: All experience levels set to 2 hp (i.e. regular, veteran, elite all have 2 hp) This is so unit promotions won't screw up battle statistics.
Romans (active player)
100 Warriors (1/1/1)
100 Spearman (1/2/1)
Egyptians:
100 Warriors (1/1/1)
100 Archers (2/1/1) with def bomb (1/1/1)

They are all in stacks with unit animations turned of, so testing goes fairly quick.

The base case will be warriors (or spearmen) vs warriors. Since randomness is involved, it'll be intresting to see how far "1 on 1" odds deviate from 50-50. Even if the odds are even, the results ccan take a long time to even out. Sometimes I'd get 7 or 8 victories or losses in a row with 50% odds! No wonder some people think the game is cheating! (including me when I'm losing, but never when I'm winning :p )

I'll also do a lot of tests with spearmen vs archers. Why? I want to find out weather the attackers defense value protects him from defensive bombardment.
Another thing I'll try is putting 100 warrionrs on a hill attacking the archers? Why? To test if terrain effects defencive bombardment!


A thought on AI build priorities:
We all agree that a (experienced) human is better at determining which unit is better. And Civ3 was made with the standard game in mind, not modding with hordes of different units available at the same time. So am I crazy to asume that, in a large part, the AI has been programmed to asume that more expensive = better, since the unit costs have been set by the human developers for the human players to choose from?
I mean, the AI assumes " Expensive units are better, since the programmers made them expensive, and they know what theye are doing, which I don't"
 
Originally posted by Iztvan


A thought on AI build priorities:
We all agree that a (experienced) human is better at determining which unit is better. And Civ3 was made with the standard game in mind, not modding with hordes of different units available at the same time. So am I crazy to asume that, in a large part, the AI has been programmed to asume that more expensive = better, since the unit costs have been set by the human developers for the human players to choose from?
I mean, the AI assumes " Expensive units are better, since the programmers made them expensive, and they know what theye are doing, which I don't"

I have read this alot lately, and I know it is simply not true. The ai does not simply just pick the most expensive unit. When testing Pentagenesis last night, it clearly was not the case.
 
I am of the theory that the AI takes the best available unit, available to all civs, and uses this as a standard. Then it determines what units are best to attack or defend against this unit and makes the appropriate choice. I am currently testing this option and will let you know... Can you tell I am running out of things to test :D

Seriously, though. I am basing this on a test Ozymandias did with regards to a 3.2.1 2hp defender being chosen over a 6.4.1 1 hp defender (the 3.2.1 is the better defender and attacker, statistically). So the AI is aware of the importance of hp (nearly as important as AF/DF values)... I am going to create units that solely based on their AF/DF values appear inferior, but are in fact superior to other units.

Honestly I am out of ideas, and this test and the one of increasing the bombard defense of buildings and improvements to facilitate land unit bombarding by artillery is my last resort. We seem to have shown that the AI doesn't follow any 'logic' in its build choice, and I simply refuse to believe this is true...
 
I find LouLong's experience with huge AI navies highly interesting. Now we only need figure out how to provoke the AI into building a big navy - I cannot recall anything the the Iroquois's situation in that old game that should have made naval power exceptionally important to them. Well, the did have a high percentage of coastal cities, but I've seen many a game where AIs with long coasts have not built unusually large navies.

I also find Neomega's tests, suggesting the AI is aiming for a specific ratio of attackers to defenders highly interesting. It agrees perfectly with my unscientific impression over many games.

Re: defensive bombardment, I ran a little test in which 10 AI Modern Armor attacked 10 human-controled Warriors, each accompanied by a Catapult, modded in the first test to have Bombardment Strength=1, in the second =10.

Result: One defensive bombardment hit in the first test, nine in the the second. If defensive bombardment works like normal combat (except only one round), and assuming the grassland +10% defense bonus applies, the expected #s of hits would be .56 and 8.51 (IIRC - I'm not having my notes with me). Pretty close. The samples are to small really, of course, but it was enough to convince me that the efficasy of defensive bombardment is indeed dependent on the bombardment strength (which always have been my impression).
 
Originally posted by Neomega


I have read this alot lately, and I know it is simply not true. The ai does not simply just pick the most expensive unit. When testing Pentagenesis last night, it clearly was not the case.

I have the impression that AI doesn't care about A/D/M, only about cost, though it will still build some numbers of cheaper crap if it's available. In my "anectodal" experience, AI builds very little number of 1.1.1 C4 units (they're crap for him, but that suits me, since they're "wizards" in the mod), still building older cavalry that is much better. On the other hand, I've never seen AI building a single skeleton unit that has 0(zero) cost and 3.2.1 stats.
 
I go away for a couple of days and yuo guys run circles around me! :D :goodjob:

I ran one last "benchmark" test. I d/l-ed Neomegas map and ran 20 turns with the following units (all units = 1HP, and no special flags except where indicated):

1/1/1 "worthless" unit, flagged O/D
5/5/1 "famous" benchmark unit, flagged O/D
6/4/1 "O"
7/3/1 "O"
4/6/1 "D"
3/7/1 "D"
2/3/1 "D" WITH ZOC
0/0/1 - 8/1/1 artillery

Pre-test notes:
1. I chose the 2/3/1 ZOC jsut as a starting point to see what ZOC is worth to the AI -- BTW, at 50% A+D of the other units, none were built.
2. I chose the standard artillery values for the Civ era wherein A+D ~= 10
3. I discounted the Player Gov builds, as there were the usual, noteable diescrepancies from the non-Player builds.

Test results:
Combined, the 3 AI Civs built (out of a total of 80/Civ 240/total; NB: % includes rounding):

60 5/5/1s = 25% O/D
06 1/1/1s = 3% O/D

53 3/7/1s = 22% D
44 4/6/1s = 18% D
0 2/3/1 ZOC = 0% D

39 7/3/1s = 16% O
35 6/4/1s = 15% O

artillery = 3 = 1%



Test Observations
1. Neomega is quite correct, the AI preserves some sort of balance -- IIRC my tests generally support a statement that this ratio is ~= 4 : 3 D : O.
2. Unshown in the above #s is that each Civ AI, INCLUDING the Player's, built 1 artillery unit ONLY (again, ~1% total builds).
3. Also unshown -- EVERY city built a 3/7/1 on turn 1 and all built either a 3/7/1 or 4/6/1 on turn 2 -- obviously, the AI sensibly looks to its immediate defense first.
4. The 3% (actually 2.5%) build of 1/1/1 indicates a very small "wild card" factor does exist for AI builds. NB: (i) adding in the artillery with the 1/1/1 and (ii) assuming the 5/5/1 builds were split 50/50 O/D (sorry, I wasn't counting ...) then, at peace, this suggests a peacetime D : O : other ~= 53% : 43% : 4% ... doesn't look good for artillery ... :mad:

Other Observations

1. I concur with Neomega that neither aggresion nor difficulty level nor neighboring Civ builds effects the AI build choices.
2. I'm intrigued by Neomega's observation that warfare increased the number of O units produced 7-9%; this would mean a wartime footing of approximately 50% : 50% O : D units.
3. In my own modding, I think the 2: 1 : 2 = O : O/D : D unit types ratio is "stable" -- i.e., the AI can handle it well, while doing abysmally with artillery and maintaining a certain "wild card" % of builds.

-- Have we exhausted all meaningful tests until Conquests come out, and the "Bombardment rules are improved" (no idea if that's re: AI builds or not)? -- In short, is further testing of flags warranted?

All The Best,

Oz
 
It occured to me that if my theory is correct -- that the AI is most capable of building within a limited range of units -- then it was worth running one more (A+D)*HP = 10 experiment, using literally every combination except (1AD+1DF)*5HP ... :( which I just realized should have been in for completeness.

-- Anyway, I've included the Player Gov AI build results this time around, as this wider "choice" of units to build put the PGAI within acceptable variance of the other Civs.

Unless otherwise remarked, all units are 1 HP. Artillery was same as last test (0/0/1 - 8/1/1)

Overall result of 320 builds --

01 1/1/1 O/D "Red Herring" = <1%

02 3/2/1 O 2HP = <1%
47 9/1/1 O = 15%
36 8/2/1 O = 11%
17 7/3/1 O = 5%
11 6/4/1 O = 3%

17 5/5/1 O/D "benchmark" = 5%

26 4/6/1 D = 8%
44 3/7/1 D = 14%
41 2/8/1 D = 13%
64 1/9/1 D = 20%
09 2/3/1 D 2HP = 3%

5 artillery = 1%

NB: Rounding yields IIRC 106% for this test.

Overall ratio of O : D units = 130 : 184 = ~40% : ~57% Note that this is within reasonable range of the prior test. -- So I would now truly conclude that the AI, at peace, "likes" between a 3 : 2 and 4 : 3 ratio of D : A units.

Observations, Questions & Curiosities

(1) Notice how skewed these results are at the extreme ends of the AF/DF ranges. As gamers, how useful do you think a 9/1/1 1HP unit actually is?

(2) Within the range of units above, (i) which do you think is the single most efficient and (ii) which would be the single most efficient DUO of 1 O and 1 D units? (iii) if the "best" "D" unit is 1/9/1 -- which the AI certainly seems to "think"! -- does this necessitate building 9/1/1s simply for the AF?

(3) With more choices, the "red herring" factor seems to go down -- well under 1% in this test for 1/1/1 builds.

(4) Artillery builds are still SOL :(

-- Any reaction, thoughts, etc.?

@The Mathematically Inclined (you know who you are ;) ) any view on what the "best" mixture from this force pool would actually be?

Abraxas,

Oz
 
Next I played a trick on the AI, I created mao, 15/15/1 10sh flagged o and d

compared to archer 20/10/1 10sh flagged o and spearman 10/20/1 flagged d, the results were surprising:

100 builds
Archers/spearman/mao
Rome
18/30/52
Egypt
28/30/42

next 100 builds
R-26/29/45 total: 44/59/97
E-23/26/49 total: 53/56/91

total pcts:
R-22% archers, 29.5% spearmen, 48.5% Mao
E- 26.5% archers, 28% spearmen, 45.5% Mao

is it the O/D flags? NO, A/D/M does matter
bump Mao down to 1/1/1 o/d
(for some reason, it appears the Ai may have disbanded some units in the seonc round?)
R- 40/55/3
E- 41/51/4
next 100 builds
R-48/45/2 total: 88/100/5
E-45/50/2 total: 86/101/6
pcts:
R- 45.5% 51.8% 2.5%
E- 44.5% 52.3% 3.1%

So.... If Mao were half the original stats, would he be made half as often?
Mao set to 8/8/1 10Sh
Target set at 48.5% - 2.5% =46%/2 = around 23%
R- 26/54/14
E- 36/49/12
next 100 builds (again the ai seems to be disbanding units, which is frustrating)
R- 36/52/10 total:62/106/24
E- 43/42/12 total:79/91/24
pcts%
R- 32.3% 55.2% 12.5%
E - 40.7% 46.9% 12.4%

edit: most interesting, almost directly half of what I was expecting.

About half of target....... I think my next experiments will bump up the A/D one point at a time, and see how the percentages play out.

I think already as you can se though, the groundwork is being laid to get a well balanced build of 3 units..... perhaps even more, it may just be the key is balance.
 
Before I continue, I want to make the methodology more pure.

1) the ai always builds a Defensive unit first in an empty city, therefore, when analyzing my stats, I will run 104 builds, and subtract 4 spearmen from the fial result.... not that my 55% spearman conlclusion could have a lot to do that after 100 builds, 4 spearmen not determined by RNG should have contaminated the RNG generated unit pool around 4%

2) to build a base known, a fact, I ran some tests, with archers 10/10/1 10sh and spearmen also 10/10/1 10sh. This is to determine the weight the RNG places on these two flags.
Results archer/spearman four trials (Without removing 4 spear)
R-45/55 50/50 46/54 44/56
E-49/51 45/55 48/52 50/50
total pct: archer 47% Spear 53%
Subtracting four spear archers/spearmen four trials
r-45/55 63/37 51/49 54/46
e-40/60 51/49 51/49 55/45
total pct: archer 51.3% Spear 48.7%

3) then I set about to see if there was any difference between 2 civs and 4 civs, from now on all trials will have four spear subtracted, unless specified otherwise.
archers/spear r=Rome, e=Egyprt, g=Greece, b=Babylon (3 trials)
r-52/48 56/42 51/49 g-48/52 50/50 49/51
e-52/48 36/54 48/52 b-54/46 49/51 48/52
total pct: archer 49.4% Spear 51.6%

Conclusion:
I am convinced I can now crunch 4 civs at once. and that the AI actually places a 1:1 value on the O and D flags
 
Next. what will a 10/10/1 10sh O/D do to the balance
Archer 10/10/1 10sh O, Spearman 10/10/1 10sh D, Mao 10/10/1 10sh O/D
Hypothesis:
From some past observation, I believe the ai will create 50% maos, and 25% spearmen, 25% archers: this will retain the 1:1 balance of offensive units to defnsive units, I also believe the Ai will "mark"50% of Maos OFF and the other 50% DEF

104 builds per civ, first 4 spearmen deducted: 3 trials
archers/spearmen/Maos
r-24/24/52 27/18/55 14/27/59
e-21/25/54 19/29/52 21/24/55
g-25/23/52 30/19/51 21/25/54
b-29/24/57 23/17/60 29/29/42
total pcts: archer: 23.6% Spearman: 23.7% Mao: 53.6%

Pretty close... is it possible the 5% "other flags" is partitioned along the same ratio if no "other" is available, giving Mao a 2.5% boost?

My hypothesis was close, so now, the real experimenting with the O and D flags can begin.
 
Top Bottom