Manipulating AI Unit Builds

Ozymandias

In Terra Fantasia
Supporter
Joined
Nov 5, 2001
Messages
10,829
Location
The lone and level sands
Long ago, I started looking into how the AI chooses which units to build. (https://forums.civfanatics.com/thre...oes-the-ai-choose-which-units-to-build.62138/) Ultimately, that thread went on for 5-1/2 years and 530 posts, with many people participating. Scores of different test cases were devised, and thousands of individual iterations run. Many others contributed ("Names Of Yore:) BomberEscort; neomega; Rocoteh. Then tom2050 picked up the ball, and really ran with it, rigorously testing the AI’s preference in flags.

Spoiler HERE'S HOW IT BEGAN :

It all began when I stumbled across a post by a fellow named Oystein, who - back in PTW days - suggested that the AI would be equal amounts of different units, as long as the combined values, in this formula, were equal:

(((3*A+2*D)*HP)+Bombard)

The “sandbox” .biq I built to test this was on a small map, with 3 Civs (Player + 2 AIs) arranged in an equilateral triangle.

The “bench line” parameters for the units, in every test, were:
• Every unit would have the values: (AF + DF = 10.) (I’m trusting that the advantages of this are obvious.)
• Strategy flags for all units were set to both "Offensive,” and "Defensive.”
• I decided to omit Bombard, as there were already too many variables in play. Also, not much was known about Artillery in Days of Yore, plus I decided that simple math should allow me to eliminate Bombard on both sides of the equation. (As later testing showed, for non-Artillery Units with a D Bombard, the AI was shown not to take Bombard into account, anyway.)

For the Civs:
• AlI Civ values (Aggression, etc.) were identical (“3.")
• Each Civ belonged to a different Culture.
• Each Civ was also limited to one Gov type: “Despotism“ modified to " Communal Corruption” with no tile penalties. All units were free; no builds could be rushed; and “War Weariness” was set to, “None.”
• No Civs were given any bonus traits (Commercial, Expansionist, etc.)
• Each city began at size 1, and all were limited to a maximum size of 4.
• Each City had access to one bonus resource, which provided enough Gold to build one unit per turn.
• Number of citizens born content = 3; number of citizens quelled by military = 1.
• All 3 Civs started at war with each other.
• Standard victory conditions are in use.

Other:
• All Terrain Tiles were “Plains,” producing 1 food, and nothing else, and were set to a Defense Bonus of "0."
• Standard victory conditions were used.
• One (1) Victory Location was placed in each Capitol, and three others were also added, roughly equidistant to both each other and each Capitol.
• Standard victory conditions were used.
• Level of play = Regent.
• Build Rounds were either 10 or 20, depending upon circumstances.
• All 3 Civs started at war with each other (this was one reason I chose to use 3 Civs, as that's the max number of Alliances.)

I chose Rome as my “Player” Civ, and let the Governor make all of Rome’s build choices.

NOTE: In my original set of tests, I had equal strength Units of both MF=1 and MF=2, and the AI simply ignored the difference. Much later, tom2050 came to the following conclusion: "The AI does increase the percentages of it's builds to a higher amount based on the units speed factor, but in reality units speeds hardly ever surpass 2, so if all else is equal, it will build a faster unit 65% of the time."

FIRST & SECOND BUILD ROUNDS:

I had my doubts about directly equating 1 additional HP alongside a simple (1 AF + 1 DF), so I tested that first. Combing Oystein’s formula with my unit parameters of “AF + DF = 10,” there was only one possible combination:

A (6/4/1/1HP) and a (3/2/1/2HP) BOTH flagged O/D. (The astute among you will already have realized that the (3/2/1/2HP) is the superior Unit. In a battle with a 0% defensive modifier, with the 6/4/1 unit attacking, the chance for victory is only 56.25% and this would go down significantly as the defender bonus increased from 0%, e.g., if the (3/2/1/2HP) had a bonus of 25% - Forests - or greater, then the victory chance of the 6/4/1 1hp unit falls below 50%.)

Here are the resulting builds by Civ, over 10 Rounds:
  • Romans (player Governor) = 30 6/4/1/1HP = 100%
  • Greeks = 28 6/4/1/1HP = 93% and 2 3/2/1/2HP = 7%
  • Egyptians = 24 6/4/1/1HP = 80% and 6 3/2/1/2HP = 20%
Now, Round 2 immediately had me suspecting how challenging a task this was going to be. I kept the same Units, with the difference that 6/4/1/1HP was flagged "Offensive" and the 3/2/1/2HP "Defensive." This now, which produced a HUGE difference in builds:
  • Romans (player Governor) = 9 6/4/1/1HP = 30% and 21 3/2/1/2HP = 70%
  • Greeks = 18 6/4/1/1HP = 60% and 12 3/2/1/2HP = 40%
  • Egyptians = 9 6/4/1/1HP = 30% and 21 3/2/1/2HP = 70%
The identical Civ and unit settings for the Greeks and Egyptians, as well as their essentially identical strategic starting positions, indicates that there is some randomness in the choice the AI makes to build units. :think:


Spoiler "Build Often" / "Build Never :

These Flags ...
1. Don't affect Unit Builds by more than ~5%.
2. Selecting more than 3 (maybe 4) of these options will "confuse" the AI, causing it to ignore all of your selections entirely.


Spoiler The "Rome Question" :
@AnthonyBoscia and I have had a half-joking back-and-forth about Rome, as some monkeying around in the code had to have been done to have Roman Legions capable of performing worker tasks. Over the years, it has seemed to me that whatever Civ is assigned the Rome slot plays more aggressively than other Civs.

Because I began these tests in prehistoric times, I simply used the first 3 Civ slots - Rome; Greece; Egypt - with Rome being the Player Civ. I let the "Governor" make all decisions - and you saw the results, above. (Of course, a simple answer here might be, "Never let your Governor do your Unit Builds.)


Next up: first test results, still using (A+D=10)
 
Last edited:
Spoiler "Summary Of The A+D=10" Rounds Of Testing, With Some Weirdness Revealed" :

I'm not going to belabor the many rounds of A/D testing which followed, as these can be found in the original thread.

That being said, truly thousands of iterations, with varying Units of "A+D=10" were run. Many of us around then contributed greatly to this effort, with neomega getting a Gold Star, and tom2050 Five Gold Stars, for his detailed, process-of-elimination investigations into Flags.

neomega found some true oddities when he ran 20 turns with the following units (all units = 1HP, and no special flags except where indicated), withe one Artillery Unit thrown in for good measure. The Units he used were:

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

(A minor note: my own initial tests deliberately did not include either artillery or Flags.)


neomega's Pre-test notes:
1. I chose the 2/3/1 ZOC just 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, notable discrepancies 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%

Not shown here is that 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. The 3% (actually 2.5%) build of 1/1/1 indicates a very small "wild card" (e.g. - an "RNG" -"Random Number Generator") 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 ... (Indeed, later testing showed that the AI was proven not to take bombard into account.)


Spoiler A Belated "Completeness Test" :

It occurred 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 belatedly realized should have been in for completeness. Here are the results of a 320 build test, utilizing every possible combination of "A+D=10." Unless otherwise remarked, all units are 1 HP. Artillery was same as last test (0/0/1 - 8/1/1) -

01 1/1/1 AF/DF "Red Herring" = <1%
02 3/2/1 AF 2HP = <1%
47 9/1/1 AF = 15%
36 8/2/1 AF = 11%
17 7/3/1 AF = 5%
11 6/4/1 AF = 3%
17 5/5/1 AF/DF "benchmark" = 5%
26 4/6/1 DF = 8%
44 3/7/1 DF = 14%
41 2/8/1 DF = 13%
64 1/9/1 DF = 20%
09 2/3/1 DF / 2HP = 3%
5 Artillery = 1%

(Note that rounding yields ~106% for this test.)

Overall ratio of A-D units = 130:184 = ~40%:~57% Note that this is within reasonable range of the prior tests; my conclusion here is that the AI "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) QUESTION: Within the range of units above, which do you think are the most efficient DUO of 1 AF and 1 DF units?

AttackerDefenderAttacker WinDefender Win
9.1.11.9.150.0%50.0%
8.2.11.9.147.0%53.0%
3.2.1 2hp1.9.143.8%56.2%
7.3.11.9.143.8%56.2%
6.4.11.9.140.0%60.0%
5.5.11.9.135.7%64.3%

It appears, based on the information above, that the AI does not factor which is 'truly' the best defender or attack available. If it did, it would build roughly the same amount of 7.3.1 1hp units as 3.2.1 2hp units and this is not the case - THE AI CAN'T DO MATH!



Spoiler Beyond that, here's a re-cap of some other info, discovered along the way: :

1. Does difficulty have any bearing on how the AI chooses it's units? NO - http://forums.civfanatics.com/showpost.php?p=1269761&postcount=243

2. Does aggression have any influence on AI build? NO - http://forums.civfanatics.com/showpost.php?p=1269779&postcount=244

3. Does war affect the AI's build decison? LIKELY - http://forums.civfanatics.com/showpost.php?p=1269872&postcount=245

4. Does one civ's military affect the building decisions of other civs? NO - http://forums.civfanatics.com/showpost.php?p=1269897&postcount=246

5. Does a higher cost of a unit make the AI build that unit more often? YES - http://forums.civfanatics.com/showpost.php?p=6832643&postcount=476

6. Do units requiring strategic resources affect AI build decision? YES - http://forums.civfanatics.com/showpost.php?p=6832760&postcount=477

7. Does the ai seek equilibrium in it's offensive/defensive units? YES - http://forums.civfanatics.com/showpost.php?p=1269925&postcount=247

8. Do all available units to the AI have a 'chance' to be built? YES - http://forums.civfanatics.com/showpost.php?p=1276578&postcount=261

9. If able to, the AI will always build a defender in an empty city, apparently choosing the cheapest available defender. tom2050 speculated that this was simply the first D Unit in the order of the list of Units available to the AI, i.e., usually the Warrior. Further note that this might be VERY good news when contemplating "Territorial / Reserve / Landwehr / Whatever" units in a mod.

10. Strategic Resources do not appear to affect how frequently the AI builds a unit.

11. The AI will "Re-Flag" a Unit from A to D or D to A as it sees fit.

12. Dropping the cost of the worse unit made the AI produce LESS of them! So as a general rule of thumb, higher cost will make the AI produce that unit more often.


 
Last edited:
Spoiler Notes on the Flag Tests :
To begin, 99.99% of the credit for this goes to tom2050, who easily conducted 1,000+ "round-robin" tests to arrive at these results - and they are going to be controversial. As I mentioned at the very beginning, the "AI" isn't programmed to decide which is its best Attacker or Defender, even where simple math is concerned. It's a hard and simple truth: this was a game, set within 1990s parameters, and was never even approximately a simulation. That we are still playing it 1/4 of a century later is a tribute to us, this community.


First off, please note that 100% complete testing was impossible, e.g., if a Unit is given the Radar Flag, how many AF/DF would the AI be willing to give up to have the Amphibious Flag as well?? - That being said:
Spoiler Here Are tom2050's Test Results For AI Flag Preferences: :

First, a quick note: tom2050 ran RIGOROUS tests, and discovered that his best results were not "absolute:" there's some degree of randomness thrown in here, as well.,

There was one perplexing test which tom2050 chose to run: extra hit points as a Flag, with the startling note that, "The AI absolutely loves and adores extra hit points ..." which is a straightforward way to raise basic AF+DF; I have omitted this from his results.

There are also unanswered questions about the "+ Fort" Flag. This Flag could not be tested at the time (@Quintillus hadn't devised his magnificent editor yet) so the only possible tests which could be done involved entailed modifying the already plainly heavily "engineered" Roman Legion. Some tests showed a Unit with this flag to be a prime choice for a first Unit build, and there is the possibility that adding this Flag will tilt an AI build, between two otherwise equal Units, to the one with this Flag.

Indeed, tom2050 ultimately concluded that an absolute order could not be ascertained: "Yes, folks, it's all relative; although preferences seem established, ABSOLUTE preferences are not. There's a randomness factor in here somewhere."0

In short, this list is a best possible list. If anyone has any non-anecdotal info, I'm happy to change it -


AI FLAG PREFERENCES, IN ORDER:

1. Stealth *
2. Enslave **
3. All Terrain As Roads
4. Radar
5. ZOC
6. Amphibious
7. Hidden Nationality
8. Starts Golden Age
9. Blitz ***
10. Pillage
11. Detect Invisible
12. Invisible
13. Paradrop

* Stealth ABILITY, and not what Stealth targets it can act upon - even none, at all.
** Similar to Stealth, it does not matter what unit the Enslave ability produces, the AI will build them.
*** From these results, it appears that the AI does not place a high value on the Blitz ability. From a human player's standpoint, Blitz is an incredibly powerful tool, but it is primarily because human players know how to use bombardment to lower an enemy's hit points, and then use Blitz forces to knock out many enemies while minimizing causalities; the AI does not know how to do this, and the programmers probably realized this, and thus programmed the AI to not place a high value on Blitz. Giving a unit the Blitz ability, would only increase the AI building them (with all other things being equal) by around 4%. (!!!) Knowing this can be a powerful tool for "up-gunning" modded AI Units, irrespective of the Flag Itself.

NB: For both Stealth and Enslave, thorough testing also revealed that the AI will not build Stealth and Enslave units more often if the there is not at least 1 unit picked to be a Stealth/Enslave target.
 
Last edited:
Moderator Action: Sorry, everyone. hold yer horses...
On Ozymandias' request, I have for now locked this thread and deleted the posts so far.
Please let Oz first finish what he has to say, and then -- let the discussions begin... :)

Lanzelot
 
For stealth attack and enslave, dummy units can be created to trick the AI into increasing its value.

How much does the number of stealth attack targets affect things? A submarine in a mod with tons of flavor units could have a vastly inflated AI value?
 
For stealth attack and enslave, dummy units can be created to trick the AI into increasing its value.
Yup :D
How much does the number of stealth attack targets affect things? A submarine in a mod with tons of flavor units could have a vastly inflated AI value?
Sorry, never tested.
 
Thanks a lot for summarising all of this. Disappointing that the AI can flip the flag between attacking and defensive. Completely consistent with my experience of making some Ancient Era unique units defensive only and seeing no change in numbers produced or attacks launched.

I am slightly confused by the entry that the AI will pick a unit more often if it costs more. I guess working on the assumption that more expensive = better. Definitely something I need to think about as I make Ancient Cavalry and Crusader available to all (at a later tech) but make them very expensive to discourage usage.
 
Thanks a lot for summarising all of this. Disappointing that the AI can flip the flag between attacking and defensive. Completely consistent with my experience of making some Ancient Era unique units defensive only and seeing no change in numbers produced or attacks launched.

I am slightly confused by the entry that the AI will pick a unit more often if it costs more. I guess working on the assumption that more expensive = better. Definitely something I need to think about as I make Ancient Cavalry and Crusader available to all (at a later tech) but make them very expensive to discourage usage.
It took me a long time to wrap my head around the fact that the game hadn't been designed/built around simple "AI" rules. Having been a programmer coder until about 1985 or so, I was surprised by this, as it would have been easy enough to do this for a game released in 1999. Nonetheless, Civ IV already in the works, and no one could possibly image that there would be so much modding and astonishing creations still coming along 25 years later - and with, despite its many flaws, Civ III still considered by many as "Best of Breed" - with Civ VII in the works!
 
Any knowledge how much the AI values workers? What's its mechanism for deciding if it needs to build more? Is it the total worker strength of all of its terraform AI strat units? Or is it just the number of them? I'm looking to make some anti air units terraform AI strat but with zero worker strength so that they would just attempt to repair bombed tiles but end up staying there to protect them. But if the AI thinks it has enough workers and stops building real workers then it won't work well.
 
Any knowledge how much the AI values workers? What's its mechanism for deciding if it needs to build more? Is it the total worker strength of all of its terraform AI strat units? Or is it just the number of them? I'm looking to make some anti air units terraform AI strat but with zero worker strength so that they would just attempt to repair bombed tiles but end up staying there to protect them. But if the AI thinks it has enough workers and stops building real workers then it won't work well.
Sadly, I haven't a clue about this one (or Settlers, for that matter.) I'm following in @Civinator's steps insofar as turning these both over to auto-production:
  • The Capital auto-producing 1 Settler every 20 turns, to prevent (amongst other ills) ICS.
  • I'm still experimenting with how to handle Workers - almost certainly through some form "Advanced Auto-production" - as, in my opinion, the AI never seems to have enough of them, and often makes sub-optimal builds:
 
If you:

1) curtail tech trading for the first two columns in the ancient era.
2) auto-produce settlers every 20 or so turns (to kill the expansion obsession)
3) 'build often' workers for all Civs

then the AI will be slow in the early game to learn techs that allow them to build improvements. As they don't have many options they'll build a lot more workers to the extent it clears up the issue of them not hooking resources (and then committing suicide by declaring war on someone whilst having no iron or horses). If you do the above you will see the AI do things like build colonies and curraghs if they remain in peacetime. I personally massively prefer this early game situation compared to the default. I'm unable to advise if it helps them much with railroading, as I can't remember how good they are at doing that in the default settings.
 
If you:

1) curtail tech trading for the first two columns in the ancient era.
There's decidedly a split opinion on this. Personally, I tend to hold off on it a bit, typically until Map Trading, although I think even that might be a bit early
2) auto-produce settlers every 20 or so turns (to kill the expansion obsession)
:yup:
3) 'build often' workers for all Civs
Sadly, the "Build Often" etc. Flags don't seem to change the AI's decisions more than +/- ~5%.
 
I have good success with creating another identical worker upgrade that can only be trained when a city has a granary. That combined with a slight perfume for regular workers and the build often flag has the AI complete its entire terrain infrastructure by mid-late medieval times. The AI, once out of worker jobs to do will merge those back into the cities. This does enhance the AI's mid game performance greatly. But be careful not to overperfume the regular workers. They'd build too many of those during the expansion phase.
 
I see the AI disband units it cannot afford (including workers). Has anyone ever seen the AI add a worker to a city instead to increase population? I can easily test this in debug by giving them several workers at the start I suppose.
 
Last edited:
I see the AI disband units it cannot afford (including workers). Has anyone ever seen the AI add a worker to a city instead to increase population? I can easily test this in debug by giving them several workers at the start I suppose.
They do join workers into cities when the terrain is all finished. But I'm not sure if they would intentionally do that in response to going bankrupt.
 
Workers

It is always interesting that many humans tend to look at a problem only from one side. Here: The AI isn´t producing enough workers, what must be changed, so the AI is building more workers? Please allow the question: Couldn´t it be, that the human player in Civ 3 can build too many workers compared to the AI?

For me I answered that question more than a decade ago with "yes, it is much better for the game play in C3C, if the human player receives the same number of workers as it is for the AI civs". Therefore in the mod CCM workers up to the late era 2 are produced for the human and the AI players only by a SW in constant intervals. Additionally workers can be enslaved and a powerful new unit was added to that mod for those purposes without triggering a war: The enslaver.

Less workers even from the early phases of the game mean less micromanagement of units, tougher decisions for the human player what to do with that smaller number of workers and the, in my eyes nice effect, that the map is not filled up with every tile "reworked" as soon as it is in standard Civ 3. Now a road can longer stay a road and not become as early a new "standard" land terrain in the game as it is in normal Civ 3. The task for military units to protect the precious workers against the nasty enslavers becomes more important even in the times of peace. The problem, that more workers can build a connection for trade by roads much faster, is balanced in CCM by setting the palace to have an air trade connection to the palace of every other discovered civ. So trade is not limited by lesser workers, but speeded up by that additonal function of the palace. Additionally the player now can focus the production in cities on the, in my eyes, more interesting and fun parts of the game: Construct buildings and great and small wonders, discover the map and build up the military. Of course the most important aspect of this setting is, that now the human player no longer can simply out-produce the AI in constructing workers, but must use other tactics to be successful in better improving the landscape of a map.

That these settings are working very well, can be seen in many games with the mod CCM in the succession games forum of CFC, where the games with the mod CCM mostly hold the letters CCM in their titles. In these succession games the players meticulously report every turn in their games, what was and is a great help in improving that mod.

Even with limiting the production of workers with the methods described above, there comes the point, when the human player feels, that the micromanagement of the hordes of workers becomes annoying in the game. Therefore in CCM at the end of era 2 (when that mod different to standard Civ 3 is in its industrial phase) "superworkers" can been built normally, that have many times the power of normal workers and therefore massively reduce the time of micromanagement by the human player. Now the question arises, what to do with the obsolete normal workers. In standard Civ3 many players concentrate them on a tile to use them only when needed (mostly to clean up pollution), but in CCM normal pollution is mostly abolished and if there would be pollution by volcanoes or nukes, this can be handled easily by the "superworkers". These workers can be used to build up radar towers, air fields or to decorate coast lines by outposts (as it can be seen in the video some posts above). In the current version of CCM these normal workers have an additional upgrade option to partisans, but so these partisans in CCM 2.5 are far too overpowered, many players in the succession games still use their small worker power and suffer the additional pain in micromanagement.

In the upcoming next version of CCM the partisans are dimmed down in power, the upgrade of normal and enslaved workers to partisans is for free and these partisans - other than workers - have no maintenance costs. Additionally, with the options possible by the Flintlock mod, normal and enslaved workers are no longer able to build railroads - this is only possible for the generations of superworkers (in CCM steamrollers and bulldozers). I hope with these new settings the problem of micromanaging workers and what to do with no longer needed workers in the later phases of the game at least can be massively reduced.
 
Last edited:
Top Bottom