Thalassicus / cep-bnw

Civ V Communitas Expansion Pack
32 stars 22 forks source link

AI Flavors #200

Closed EricB1 closed 10 years ago

EricB1 commented 10 years ago

Hi Guys,

I'm starting a major undertaking of trying to improve the AI behavior. I've tried some test games with CEP and the AIs make very poor decisions on what to build. They can overrun a human player early in the game due to just sheer numbers, but they really neglect their economies. It's routine that they have so many units that they are running massive gold deficits, which severely impacts their science rates, letting the human player pull ahead in the tech race. Sometimes they have such a poor economy that they have a zero science rate. I want to try to modify the flavor values in a way that more accurately reflects the values of certain buildings/units/etc and see if it'll work. I can test it separately from CEP, and if successful and everyone likes it then we can discuss merging it with CEP.

stackpoint commented 10 years ago

I was looking into this problem a while back before I ended up being sidetracked by the more insidious lua bugs. I note down my observations below.

So there are two sources of possible errors here, the Leader Flavors (unlikely) and the Units/Building Flavors (likely). Since you are familiar with C++, you can probably look into the DLL source code and become familiar with how exactly selection process occurs. I don't know if building times or how early the game is affects the build decision either.

I had a theory that the flavor problems might just be a problem of scale. That the 2-4-8-16-32 scale might be too low for code to make distinguishing choices (since the vanilla BNW game operates at a larger and more arbitrary scale) and that a 4-8-16-32-64 or a 8-16-32-64-128 scale would work better. But I hadn't taken the time to look through the source or test it to be sure.

It also might be prudent to just remove the custom flavors we have for our non-modded vanillaBNW units/buildings until we get the build order bug under control. I believe we only have a couple modded custom buildings/units that would need flavors manually added.

I'll make another post once I find the references on civfanatics I have been working off of.

stackpoint commented 10 years ago

Here is an older link: Diplomacy by numbers

I couldn't find the list I was using a couple months ago.

EricB1 commented 10 years ago

From scanning through the core C++ code I have a little bit of an idea what it does.

First, it sets some sort of strategy goal. What this does is establish a weight threshold, which is a number. It reads these numbers from an XML file. Then, the code adds together the leader flavors in addition to flavors from units/buildings/wonders/etc. If the 2 numbers added together exceed the threshold, then it executes some action. This makes it more likely that a leader such as Gandhi will be peaceful or Montezuma will attack you.

We don't need to touch the leader flavors at all. The behavior that needs to be adjusted is really universal to all AIs. It's just getting them to make wiser choices on what to build. Looking through Thal's code for buildings, it's really a pretty simple across the board rule that he has. It would take a ton of time to really fine-tune it, which is probably why he took a simple approach (to save time and effort).

What someone would have to do is set flavors for every building/wonder/unit in the game. Just about all of them have multiple flavors. Basically, what you have to do is take a building such as the Shrine. What aspects of the game does this help you with and how much for each of them? Shrines help you get faith, which helps with a ton of different things depending on what beliefs you've selected. So it helps with more than just FLAVOR_RELIGION. Something like a monument helps with more than just FLAVOR_CULTURE. It helps you unlock policies, which is very beneficial to a number of game aspects.

So, I'm going through trying to set flavors for buildings/units/wonders. I'm starting with ancient era buildings/wonders/units, then running simulations with AIs and just observing their behavior. If it's pretty intelligent decisions then good. If it's stupid choices, then time to re-adjust the flavors and try again.

GrantSP commented 10 years ago

May I ask, if at some point in the future the unit or building's stats are changed, does that mean the flavors also need adjusting? So now if the shrine is faith type building and the flavors should reflect that, what would happen if we made it a dual purpose building with with AND culture?

EricB1 commented 10 years ago

Yeah if you adjust the stats of a building you'd need to adjust the flavors too. Not all the time though. Only if it changes its purpose or the way that a human player would use it.

Say for example you upped the culture on a theater by +1 culture. You wouldn't need to adjust the flavors for that. If you changed a Smith to give faith all of a sudden, then you'd need to adjust the flavors for that.

stackpoint commented 10 years ago

If you have to adjust all the flavors manually, then wouldn't it be better to use the vanilla flavor values then?

GrantSP commented 10 years ago

I agree.

EricB1 commented 10 years ago

The vanilla values are a good starting point but flawed. I don't mind the tediousness. It's what I do for fun. Doesn't feel like work at all.

GrantSP commented 10 years ago

Then perhaps we could look into devising some spreadsheet to formulate them based on their stats? If we envisage changes being made every now and then.

EricB1 commented 10 years ago

It's rare actually to change the purpose of a building or unit. Normally it's a minor tweak to it adjusting some stats to be more balanced. Not that hard to adjust a couple of flavors if needed. It's hard to automate too. The goal is to get an AI player to make decisions like a human player, and it's difficult to come up with a formula for that.

stackpoint commented 10 years ago

Thal already uses sql to auto-generate flavor values, but there are some problems with that method.

stackpoint commented 10 years ago

Here's the spreadsheet with the four leader types: https://docs.google.com/spreadsheet/ccc?key=0Ap8Ehya83q19dHlBVGdUV0doVGlVU3dGLWt3LS1YRHc&usp=drive_web#gid=1

EricB1 commented 10 years ago

I looked at the SQL for buildings and units that Thal has. It assigns the same flavor values to buildings for each flavor type. For example, a granary is good for science and for growth, so it gets FLAVOR_GROWTH=8 and FLAVOR_SCIENCE=8. In reality, a granary is better for growth than for science, so the growth value should be higher.

In addition, a building like a granary is good for more things than just those 2 things. It's also good for production because it allows for a city to use production tiles instead of food tiles because it's getting food from the granary. Ever notice that production typically increases when you build a granary? It's a small factor, but it's important to include them as flavors too so the AI knows about it. Also small flavors for gold for the same reason. Plus, great people (due to being able to use more specialists), and offense and defense. Your military is stronger when you have better cities and more advanced technology.

GrantSP commented 10 years ago

Are these the same values as used by the Good for tooltips?

skodkim commented 10 years ago

I definately agree that AI flavors is something that needs a makeover. One example is the FLAVOR_CULTURE for monuments vs. theatres. Even though the former has lower culture output than the latter FLAVOR_CULTURE is 8 for both of them.

\Skodkim

GrantSP commented 10 years ago

I'm glad you guys are taking a look at this, I have been asking for clarification on these things for months and have yet to get a satisfactory response. http://forums.civfanatics.com/showthread.php?t=513595&highlight=flavors That was just one time where I asked a broad question on this matter. Curiously stackpointer and EricB both answered that post. Maybe this is a good omen.

On 26 February 2014 18:32, skodkim notifications@github.com wrote:

I definately agree that AI flavors is something that needs a makeover. One example is the FLAVOR_CULTURE for monuments vs. theatres. Even though the former has lower culture output than the latter FLAVOR_CULTURE is 8 for both of them.

\Skodkim

Reply to this email directly or view it on GitHubhttps://github.com/Thalassicus/cep-bnw/issues/200#issuecomment-36098553 .

"There are 10 types of people in the world: those who understand binary, and those who don't."

stackpoint commented 10 years ago

I don't know how the vanilla BNW flavor values were chosen (some values seem arbitrarily small or large) but the goal of Thal's flavor work was to automate the flavor values based on certain triggers and then assign them on a 2-4-8-16-32 scale. Unfortunately, it doesn't seem like it worked out and something else needs to be figured out

GrantSP commented 10 years ago

Agreed. I am confident that you can devise a system that will. I'm able to devote as much time as is needed to test any changes that are made. BTW, I don't expect you to be the only one that will work on this. If I can get a handle on this system I'll try and do what I can.

GrantSP commented 10 years ago

Is it possible to at least have the formula from the DLL code isolated so that we can see what/when the selection takes place? I was thinking if we can see what/when the AI chooses its build queue for both buildings and units and also what/when it chooses to purchase units/buildings, and the criteria it uses to make those decisions.

I would like to do a simple simulator in a spreadsheet to at least see what to expect with any changes made. We could input vanilla and CEP values and compare them. Closely related to this is the Personality of each leader. Just how much does that new descriptor add to the gaming experience?

jma2286 commented 10 years ago

I did some foolin around with flavors a long time ago, where I made every AI value what I consider to be fundamentals above all the other flavors. The ones I emphasized were Tile Improvement (water too), Water Connection, Happiness, and Infrastructure (I think that does handles roads).

That way, every Civ would first make sure they had enough happiness and handle the Worker actions as the highest priority. Tile improvement expands economy immensely and internal trade routes crank up GPT so it makes a lot more sense that way IMO.

I also think, in the case of aggressive civs, that Gold should be valued just as much as units if not more so that they see Caravansaries and Markets just as importantly as their units. Warmonger AIs seem to be broke all the time when I play. They either go Alexander the Great on everyone or get regulated lol.

stackpoint commented 10 years ago

From Eric's post it seems like it goes AI_Strategy (determined by leader flavors and circumstances?) and from the strategy determines what Flavors to build for.

GrantSP commented 10 years ago

If the vanilla code adds the flavors for units/buildings/etc. to the leader flavors and executes the process if it is greater than some threshold then I can see a problem already with our values.

Like @stackpoint I have some reticence about the values assigned to the units/buildings/etc.. e.g. The Forbidden Palace is an excellent wonder to build to reduce Unhappiness. In vanilla this wonder has a FLAVOR value of 250 in CEP it is 32 However this threshold is determined, comparing it to either 250 or 32 is a vastly different outcome. Although I have long felt the simplistic progressive scale used in CEP wasn't up to the task I have never had any evidence to support my belief. Since the values in vanilla aren't of this type but appear to be more thought out and applied specifically to each item, I feel it really needs to be shown WHY the change introduced in CEP was made. Otherwise there is no valid reason not to use the vanilla values. "if it aint broke, don't fix it."

Like @stackpoint said in an earlier comment: "It also might be prudent to just remove the custom flavors we have for our non-modded vanillaBNW units/buildings until we get the build order bug under control. I believe we only have a couple modded custom buildings/units that would need flavors manually added."

stackpoint commented 10 years ago

Actually, I would argue the opposite with your Forbidden Palace example. If the Leader_Flavors are added to the Unit_Flavors/Building_Flavors then the difference between 250 and 32 is relatively insignificant. For example:

Leader_Flavor + Building_Flavor = Total
0             + 32              = 32
10            + 16              = 26

From here you can see that a flavor value of 32 will be greater no matter what the Leader_Flavor value will be, which would be similar to how the Forbidden Palace works currently. The more relevant problem would be the more discrete values between 1-16 and how the AI strategies are chosen in the first place.

GrantSP commented 10 years ago

Yeah but what isn't shown here is how/what the threshold is.

Leader_Flavor + Building_Flavor = Total (using Napoleon as an example)
8             + 32              = 40
4            + 250              = 254

Just what are those 2 figures compared to? @EricB1 stated there is some sort of weighting read from a XML file. What file? What figures? How are they used? Like I said, I'm not worried about the HOW so much as the WHY the vanilla values were changed.

stackpoint commented 10 years ago

I've already commented on Thal's possible thought process here: https://github.com/Thalassicus/cep-bnw/issues/200#issuecomment-36112655

If you want to browse the AI strategies tables then SQLiteSpy should have them listed as the first dozen or so tables. There's a lot of information to go through and the math that the DLL uses needs to be more clearly defined before we can come to definite conclusions.

EricB1 commented 10 years ago

I've still been working on this. It's just a slow process going through every unit/building/wonder in the game and assigning flavors for everything. I've made it though the ancient/classical/medieval things so far. A bit more and I can start testing....

stackpoint commented 10 years ago

Good luck. I've reinstated the vanilla flavor values for units and buildings in hopes of improving AI performance for now.

GrantSP commented 10 years ago

@EricB1 I thought we may have lost you for a while there!

skodkim commented 10 years ago

@EricB1 Great that you're giving it a go at it!

Are you doing all the flavors manually?

I know the old way of assigning flavors automatically seemed to give bad results but isn't there some kind of logic in how vanilla flavors are assigned that we can use to assign them automatically?

EricB1 commented 10 years ago

Yeah I've been doing it manually by just what feels about it. I figure that I can go with that and then test it to see how the AI reacts. I'm trying to get them to build fewer units and focus on their economy more so they are more competitive.

Through the Industrial era now. Just modern, atomic, and information eras left along with a ton of testing.

BTW, I've been working with version 3.14. Are there any changes to 3.15 that would affect this?

EricB1 commented 10 years ago

@stackpoint Are the vanilla values re-instated in the latest stable release (3.15)? Looking through the code, I don't see that happening as they appear to be automatically filled with new values.

I've been building a separate mod for modifying the AI flavors so that I can easily add the flavors or remove them while testing it. I've been trying to get the AI to have smarter build orders, but it isn't easy. They seem to want to be dumb a lot, but there have been some results. Takes a lot of trial and error. Also, I've been making a spreadsheet to keep track of what values are in there for each flavor so that it's easy to see what's going on.

Whenever I get it done, how would I go about getting the AI mod to you guys for testing?

GrantSP commented 10 years ago

@EricB1 the stable branch is still 3.14 so only the new-features branch is the latest. If you want to you can make a new branch off new-features and put your adjustments in there. It will be easier to merge into that branch than the stable.

Our priority at the moment is to get 3.15 as bug free as possible so that IT becomes the next stable branch.

EricB1 commented 10 years ago

Finished getting flavor values in for every building/wonder/unit. Starting the testing phase now.

GrantSP commented 10 years ago

@EricB1 How many tests would you need and in what sort of setups to provide ample evidence for your conclusion phase?

EricB1 commented 10 years ago

Tests under standard settings would be a good start. I've been doing some of this. Just start a game under normal settings (communitas map, standard size, standard speed). Run some turns using the tuner. Check to see what the AI is building in their cities. If you see them building a bunch of stupid units repeatedly or ignoring really obvious things then that's what I'm looking for. Ideal builds is probably impossible to achieve.

Early things I found that I corrected were: AI was building way too many workers, so I decreased some flavor values for workers.

Look for things like certain buildings being completely ignored even though they would be valuable to build, units never being built, or units being built to excess. I tested some of the early game builds, but didn't get into the mid or late game builds.

Thalassicus commented 10 years ago

Is the AI building too many units, or are they not correctly using those units to conquer rivals? The second one has usually been the problem in the past. When they build a big army, do they declare war on their neighbors and conquer lots of territory?

If one building is significantly more valuable than another, we can either balance the buildings, or adjust the flavors to accurately show the imbalance. I like balancing the game.

We also need to ensure everything is easy to understand for humans who have never played the mod before. Humans need to see a simple flavor progression like this:

Color Building Type Examples
Red superpower Apollo Program, Manhattan Project
Purple 1 per game world wonders
Blue 1 per civ national wonders, or 1 civ per game (uniques)
Green 1 per city regular buildings
White many per city Great Person flavor
Grey situational

Shrines help you get faith, which helps with a ton of different things depending on what beliefs you've selected. So it helps with more than just FLAVOR_RELIGION.

This is not practical because everything leads to everything. If we use a shrine to get a science belief, that belief gets us better technologies, which might improve our military, which helps us conquer cities, which gives us new yields... a multi-layered approach means all buildings have all flavors. When every building has everything, that information is identical, and can't be used to make decisions. I found that giving too many flavors to buildings causes the AI to make worse choices than even the bad vanilla values.

It's really important building flavors are created automatically based on the stats of the building. This is vital for three reasons:

1) Automatic assignment makes this mod compatible with other mods which add new buildings. It's impossible to balance flavor values with manual assignment, because we don't know what other mods players may be using, how those other mods assign flavors, or if other authors are aware of our flavor reassignment. 2) Humans can make mistakes, while the flavor-assigning AI can't. It also sees things human miss. It recognized things about technologies and other things I never considered, even after years of playing and analyzing the game. 3) Manually setting flavors takes time away from more important priorities, like interface enhancements, leader balance, policy improvements, or even fixing problems in the game core directly with a new c++ core part of the project. If Firaxis had used an automatic system from the beginning, they could have saved a huge amount of development time, spending that time on things that might have ensured Civ 5 was better received at release than the controversial game they initially put out. 4) Manual assignment follows no logical patterns. Firaxis' scale for one flavor rarely matches the scale for other flavors. Without any rational organization system, flavors are nearly impossible to balance.

I carefully adjusted the automatic assignment method to ensure it's accurate for the AI and helpful for humans. I put a lot of thought into this, with hundreds of hours of development and testing over more than a year, with in-depth analysis of how AIs should function in games. If we need to improve it, it's better to do so directly, instead of re-introducing the problems of the old system.

If the build-order AI does not understand how to use flavors, the best solution is to fix that AI directly. I did this for gold in the SpendAIGold function of CEAI_Events.lua, and if we want to fix the production decisions too, we just need to translate that code to C++ for use in the game core. Patching the issue with an approach that causes new problems gives us one step forward, three steps back.

The AI should not use a threshold system to decide what to build. It should use weighted probabilities like in SpendAIGold. If one building is twice as valuable as another, it should have twice the likelihood of construction. Threshold systems create weird problems and extreme case scenarios. This is why you see things like "certain buildings being completely ignored even though they would be valuable to build". That's caused by the threshold system.

Thalassicus commented 10 years ago

A quick inspection of the code revealed these problems were caused by a mistake with AIUnitCostPercent in the new-features version of CEAI_Handicaps.xml. A 400% cost increase two months ago led to the issues you encountered in this thread. The increased cost drained AI economies, and the reduced gold supply prevented them from using the improved AI spending algorithms in CEAI_Events.

These and other changes to the handicap file were an experiment someone was doing (not intended to be used in a release). I thought I'd fixed the problems there two months ago, but overlooked this cost change.

I'll restore the original settings to fix the issue. I don't know what else I may have overlooked that might be causing other problems. We'll need to re-evaluate balance changes that may have been made as a result of this mistake.

If this doesn't solve the AI problems to your satisfaction, we can start a branch of the project to fix the city-construction AI directly. The real issue is a poorly-designed core algorithm, and no amount of patching around the periphery can solve that root problem. It should be a straightforward matter of replacing Firaxis' old algorithms with the improved ones from CEAI_Events. I've wanted to do this for two, but haven't had enough free time, and other priorities keep coming up. It sounds like you have the interest, time, and dedication to accomplish this. I can help guide you through the process.

GrantSP commented 10 years ago

Wow, what a difference a trained eye can make on this problem. Simply knowing the starting point of the discussion and seeing the steps taken by others to solve it and the result can be boiled down to this. Amazing.

I'm curious though, you say in this thread that: "Automatic assignment makes this mod compatible with other mods ..." Am I to conclude that the FLAVORS assigned in Communitas are automatically assigned? Or is this the path that you want to take?

Either way, I would really like to see how this is accomplished. It has been a concern of mine for some time just how the values are defined.

Thalassicus commented 10 years ago

Am I to conclude that the FLAVORS assigned in Communitas are automatically assigned?

It's more than that. I taught the AI to learn like humans, studying the effects of things to figure out how to use them. It uses what it learns to decide what to research or build. It does this without human interaction. Firaxis uses a human for the first step, while I trained the AI to think for itself.

I also taught the AI how to spend gold. Firaxis didn't teach it how to purchase things, and simply told the AI to never buy anything! That's one reason we can't have lots of gold in the unmodded game. Their AI is clueless how to use it.

Sadly, the AI's decision-making process for production is more difficult to modify than gold or research, since it's in the c++ game core. It's best to modify that directly in the core, because I'm unsure whose training would take precedence in that situation, mine or that of Firaxis.

A learning AI is better in the long run than one hand-held by humans. Computers are faster and more accurate at analyzing vast amounts of data than us. Humans work slow and make mistakes.

There's always ways to improve things. I recently realized the AIs decision process for spending gold didn't consider trade units or buildings, so I'm working on that today. I suspect it's a big reason for low gold income.


Here's the thread where I started training the AI two years ago: AI Gold Spending

I'll give an example. Consider a situation where we suddenly lose a luxury resource trade deal, and need to purchase emergency happiness. Humans basically follow these steps:

  1. Look at our happiness. It's negative!
  2. Look at what buildings give happiness.
  3. Purchase one of those buildings.

I taught the AI to think the same way. Most people don't realize the human decision-making process is incredibly complex, and what looks like 3 simple steps actually involves hundreds of tiny decisions.

That's the basics of how things work. Give me a few hours, and I'll write up the human / AI thought process in more detail.

Thalassicus commented 10 years ago

Here's how the AI decides to buy a happiness building if happiness is -4, when no critical priorities exist (like negative gold income). This is based on everything involved in a human making the same choice. Each step has a file line number before it for reference. The bold-faced lines follow the decision path.

The AI reports its decisions in lua.log. It stops thinking about what to buy if it runs out of budget. It will spend all its money for a critical-emergency priority, but tries to keep enough money to pay for a research agreement before purchasing moderate or low priority items.

CAT function SpendAIGold

  1. 67-79: Check if I'm human or not (actually a deep philosophical question!)
  2. 80-116: Figure out my war status, what research agreements cost, and how much gold I have.
  3. 117-145: Figure out the order I should check my cities. The capital always goes first, then other cities follow in either population order, or reverse pop order.
  4. 146-167: Figure out how many workers, healers, military land units, total military units, and other unit flavors I have.
  5. 168-218: Check my critical priorities.
    1. 172-180: Negative income?
    2. 181-189: Severely negative happiness below -10?
    3. 190-202: Am I religious and can found a pantheon?
    4. 203-208: At least 2 workers in the early eras?
    5. 209-217: At least 3 cities in the early eras?
  6. 219-318: Check my moderate priorities.
    1. 224-238: Am I defending my cities?
    2. 239-247: Negative happiness? Attempt to purchase Happiness buildings in all my cities.
    3. 248-256: Upgrade units I'm militaristic or at war.
    4. 257-262: Bribe citystates if the game is close to diplomatic victory, .
    5. 263-266: Buy workers if I have less than 1 per city.
    6. 267-270: Purchase a science building.
    7. 271-276: Purchase a Monument.
    8. 277-284: Purchase healing units if I have too few for my army size.
    9. 285-308: Purchase military units if I have less than 1 per city (more if militaristic, or at war with a human)
    10. 309-313: Purchase Recon units if I have less than 3 in the early game.
  7. 219-318: Check my low priorities.
    1. 323-338: Consider the ingame situation when deciding what to purchase.
      1. 329: Prioritize citystate bribes if there's lots of nearby unallied citystates.
      2. 330: Consider ships after Sailing.
      3. 331: Consider aircraft after Flight.
      4. 332: Consider expanding if I'm not at war, and have less than 5 cities.
      5. 333: Don't get workers (taken care of in Critical and Moderate priorities).
      6. 334: Prioritize food buildings if my smallest city has low population.
      7. 335: Prioritize happiness if it is low.
      8. 338: Prioritize religion if I can found a religion.
    2. 339-344: Increase the value of military units if I'm at war with a human, or I like military units.
    3. 345-361: What stuff do I like to focus on?
    4. 362-392: Attempt to purchase things until I run out of budget, or I've tried "MAX_ATTEMPTS" times.
      1. 366-381: Pick a flavor based on my preferences. I'm twice as likely to get something I like twice as much.
      2. 375-376: If my chosen flavor is growth or production, check cities from smallest to largest.
      3. 377-384: Other flavors check cities from largest to smallest.
      4. 385-390: Ignore this flavor for future steps if I attempted to bribe a citystate, or I was unable to buy anything of that flavor.
  8. 395: Tell lua.log I was unable to spend all my budget.

Something important to point out is the AI first decides what flavor to get, then picks something from that flavor.

Increasing the flavor of something like the Monument doesn't have an effect if that's the only culture building the AI can choose from. Raising that won't alter the AIs decision about what flavor to pursue, either. If it's low on happiness, it's going to go for a happiness building, regardless of how good the Monument may be for culture. This is because a low-happiness situation takes priority over raising our culture in a balanced game.

Thalassicus commented 10 years ago

The next step is to check the purpose of things.

The AI figures out every mod we have installed, and how those mods affect the purpose of all units, buildings, resources, improvements, and technologies. It does all this in just 0.115 seconds at the start of each game. AIs are incredibly fast and efficient at analyzing vast amounts of information. It took me a decade of every civ game (and Alpha Centauri) to learn this stuff. With just a few hours' training from me, the AI figures this out in a fraction of a second.

I told the AI to re-learn this every game, since it's so fast, and the information changes depending on what mods the human uses. An AI using only CAT will learn how the vanilla game works, while an AI using CEG will figure out changes made in that mod. It also figures out how any other installed mods affect the game.

A similar part of the AI reports the effects of all units/buildings/techs to humans on tooltips. This AI tells humans what buildings are good for, what effects they have, and what we need to build them.


The AI is looking for a building that gives happiness. It learns which buildings are good for happiness on line 197. The most common effect is city-happiness ("Happiness"). It picks a purchasable building with the best Happiness rating to buy. It considers regular buildings in a line identical (Arena/Zoo/Cinema) since only one of these is ever available at a time.

CEAI_Buildings.sql

  1. 14-52: Learn which buildings have limited availability (like the Watermill or World Wonders).
  2. 53-69: Figure out what buildings unlock superpowers that lead to victory (unlocking Spaceships or Nukes).
  3. 70-385: Figure out the purpose of buildings, by looking at the effects of the building. This step learns about effects that are not standardized yield bonuses.
    1. 83: Worker bonuses
    2. 89: Golden ages
    3. 97: Food
    4. 106: Production
    5. 114: Gold
    6. 133: Science
    7. 143: Culture
    8. 161: Tourism
    9. 171: Expansion to new cities (high value)
    10. 188: Expansion to new cities (low value)
    11. 197: Happiness The AI looks for happiness buildings by checking if the building has one of these effects:
      • Happiness <> 0
      • UnmoddedHappiness <> 0
      • UnhappinessModifier <> 0
      • HappinessPerCity <> 0
      • HappinessPerXPolicies <> 0
      • CityCountUnhappinessMod <> 0
      • NoOccupiedUnhappiness <> 0
    12. 210: Religion
    13. 237: Great People (high value)
    14. 246: Great People (low value)
    15. 254: Diplomacy
    16. 263: Espionage
    17. 276: Water connections
    18. 282: Military training
    19. 291: Conquest
    20. 305: Defense
    21. 315: City defense
    22. 328: Spaceships
    23. 335: Constructing wonders (not wonders themselves)
    24. 341-373: Trade
    25. 348: World Wonders
  4. 386-423: The AI next learns which buildings give standardized yield bonuses.
  5. 432-639: Adjust the value of some unusual effects.
    1. 432-452: Change "growth" flavor to "naval growth" for port buildings.
    2. 453-503: Increase the value of unique buildings and special national wonder effects.
    3. 504-548: Increase the value of special world wonder effects.
    4. 549-564: Add the value of specialists to the building.
    5. 565-584: Increase the value of wonder-quality cultural effects.
    6. 585-588: Increase the value of overpowered buildings like the Great Library that are impossible to balance.
    7. 589-607: Manually add value to some building effects that are very difficult to analyze. (I'd like to eventually replace this section with proper analysis.)
    8. 623-639: Add the value of free units or buildings the building provides.
  6. 640-657: Learn which effect of a type of flavor on a building has the highest value.
  7. 662: Show human players the AI's analysis so far. This is the "Good For" section of tooltips.
  8. 663-686: Adjust AI preferences to improve its decision-making for several buildings that are difficult to analyze. This is where we could increase the value of specific effects people think the AI should use more.
  9. 664-673: Peaceful AIs do not prioritize military wonders.
  10. 674-679: Stonehenge is extremely useful for religious games.
  11. 680-686: Firaxis' production-choosing AI is horrible at picking the right city to build situational wonders like the Terracotta Army, so give them a low priority.
  12. 689: Ignore buildings that no longer exist in the Buildings table.
  13. 692: Ignore dummy buildings that are automatically assigned to cities by lua code.
Thalassicus commented 10 years ago

I think that's the main stuff. I usually give summaries about topics like this because going into detail takes a long time. I recognize this is an important question yall have had for a while, however, and knew I needed to go more in-depth about it. It's exhausting to show everything though. The information I posted in this thread took a total of seven hours of thinking, research, and writing.

Here's where you can find more information if you'd like to learn more details:

Thalassicus commented 10 years ago

If you give me a list of buildings you feel the AI should prioritize differently, I'll look over it, and figure out how to adjust the AI.

For example, if you think the AI should place more emphasis on Courthouses, we can increase the value of the NoOccupiedUnhappiness effect around line 663 of CEAI_Buildings.sql.

GrantSP commented 10 years ago

@Thalassicus that would have to be THE most helpful piece of code commenting I have EVER seen. Thank you. If I can now understand this more clearly then you have succeeded. Excellent work. PS. This could quite easily be included in the files themselves.

Pardon me for using this as a lesson. Is it because the sql commands are assigning FLAVORs to ANY buildings that have the specific attribute on them that the hugely differing values as compared to vanilla occur? ie. line 192 203 of CEAI_Buildings is setting the FLAVOR_HAPPINESS to 8 for any building that has those listed attributes. So Courthouse changes from 150 to 8, not because of some whim of the programmer, but because it meets the criteria of this code block. Please tell me this is correct because I feel a lightbulb moment has just happened.

jma2286 commented 10 years ago

One thing I saw missing in those priorities was gold buildings. The AI really needs to prioritize Caravansary and Market to improve their GPT

Thalassicus commented 10 years ago

The unusually low income was because of an erroneously high cost to maintain armies. Once I fixed that AIs are now getting high gold per turn.

Low income is the first critical priority (line 172). I'll increase the threshold for detecting critically low income from 0 to 7, the typical cost to trade luxury resources. Purchasing gold buildings/units also appears in the low-level priorities which depend on the leader's preferences.

Thalassicus commented 10 years ago

I discovered today I introduced an obscure bug sometime last fall, probably after the fall patch. This messed up the purchasing AI. Fixing this should greatly help the situation. I'm testing a solution now.

In more detail, in the function PurchaseOneUnitOfFlavor and its building counterpart, I inadvertently used "#units > 0" for a units table that is sparse (it does not have consecutive integer ID keys so #units doesn't work). In this situation it's necessary to use a different method to count the number of items in a table. I'd done this before then, but changed the code there while trying to debug an unrelated issue, and accidentally broke this part of the code.

Thalassicus commented 10 years ago

I discovered the problem after noticing abnormally large gold reserves on all the AIs. After taking a few hours to find the reason and implement the fix, the spending AI kicked in next turn. It looks like it's solved:

DEBUG  goldStored=5043 goldHigh=500 goldMin=125
INFO   Turn 176 AIPurchase Askia 130/5043 PAID for FLAVOR_GOLD         BUILDING_MARKET
INFO   Turn 176 AIPurchase Askia 130/4913 PAID for FLAVOR_EXPANSION    BUILDING_CIRCUS
INFO   Turn 176 AIPurchase Askia 100/4783 PAID for FLAVOR_GOLD         BUILDING_CARAVANSARY
INFO   Turn 176 AIPurchase Askia 180/4683 PAID for FLAVOR_RELIGION     BUILDING_TEMPLE
INFO   Turn 176 AIPurchase Askia 180/4503 PAID for FLAVOR_HAPPINESS    BUILDING_COLOSSEUM
INFO   Turn 176 AIPurchase Askia 130/4323 PAID for FLAVOR_SCIENCE      BUILDING_LIBRARY
INFO   Turn 176 AIPurchase Askia 130/4193 PAID for FLAVOR_NAVAL_GROWTH BUILDING_LIGHTHOUSE
INFO   Turn 176 AIPurchase Askia 630/4063 PAID for FLAVOR_SIEGE        UNIT_TREBUCHET
INFO   Turn 176 AIPurchase Askia 710/3433 PAID for FLAVOR_DEFENSE      UNIT_LONGSWORDSMAN
INFO   Turn 176 AIPurchase Askia 630/2723 PAID for FLAVOR_OFFENSE      UNIT_TREBUCHET
INFO   Turn 176 AIPurchase Askia 350/2093 PAID for FLAVOR_EXPANSION    BUILDING_THEATRE
INFO   Turn 176            Askia 1618 of 1743 (with +46 gold/turn) saved