Closed EricB1 closed 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.
Here is an older link: Diplomacy by numbers
I couldn't find the list I was using a couple months 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.
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?
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.
If you have to adjust all the flavors manually, then wouldn't it be better to use the vanilla flavor values then?
I agree.
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.
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.
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.
Thal already uses sql to auto-generate flavor values, but there are some problems with that method.
Here's the spreadsheet with the four leader types: https://docs.google.com/spreadsheet/ccc?key=0Ap8Ehya83q19dHlBVGdUV0doVGlVU3dGLWt3LS1YRHc&usp=drive_web#gid=1
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.
Are these the same values as used by the Good for tooltips?
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
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."
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
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.
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?
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.
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.
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."
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.
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.
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.
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....
Good luck. I've reinstated the vanilla flavor values for units and buildings in hopes of improving AI performance for now.
@EricB1 I thought we may have lost you for a while there!
@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?
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?
@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?
@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.
Finished getting flavor values in for every building/wonder/unit. Starting the testing phase now.
@EricB1 How many tests would you need and in what sort of setups to provide ample evidence for your conclusion phase?
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.
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.
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.
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.
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:
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.
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
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.
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
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:
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.
@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.
One thing I saw missing in those priorities was gold buildings. The AI really needs to prioritize Caravansary and Market to improve their GPT
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.
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.
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
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.