City Specialisation

Yakk

Cheftan
Joined
Mar 6, 2006
Messages
1,288
I'm thinking about ways I kick the crap out of the AI. And one big one is city specialisation.

Approach to fix this.

Step 1: Think about what kinds of city specialisation there are.
Step 2: Identify which cities should be specialised which way.
Step 3: Influence worker plot improvements that is effective for the identification.
Step 4: Influence city build decisions to bias towards specialisation.
Step 5: Feedback civilisation needs to create demand for various kinds of city.

Step 1:
The basic specialisations are "factory town", economic "cash+research" town, "culture factory", and GPP factory.

Factory towns can be specialised into "wonder towns" and "unit factories".

Economic towns can be specialised into "research towns", "flexible towns" and "cash cow towns".

GPP factories can be narrowly aimed at one type of GPP.

GPP settlement targets (where you aim to build oxford university, etc) also kick ass.

There are a number of other niche towns, like the national park town, that might have to be hard coded?

With slavery, the "slave town" also exists. It tends to be a short term phase, however.

Step 2 thoughts:
Build a "multiplier profile" based on current and/or future technologies, and a plot return profile. This includes a happiness/healthyness budget. Use a greedy binary search to maximise the return on a linear utility function, then do a post-process check for things like "I want zero or slightly positive food return at happy cap for my factory cities".

Tweak the utility function to search for an option that generates the most production (for a factory town), or most cash+research (for an economics town), or most GPP (for a GPP factory), or most culture, etc.

This is aimed at being a fast and efficient way of generating a good idea on how good we can make a given city at a given role. Because we are using a linear utility function, the greedy algorithm works, and we get O(n lg n) on the number of tiles/size of the city work to figure out what the ideal result of the city is -- we avoid combinatorics hell.

The post-processing pass then tells us if we are valuing the non-linear components (like food for a production city -- we want enough food to support the city, but we don't care much about excess food) too high. We then adjust their (relative) value down, and repeat. A binary search can then rapidly close in on the ideal price of food (for example) in a logarithmic number of steps.

This isn't perfectly ideal, but it will be pretty fast (the slow part will be setting up the plot/building profiles, which requires searching the tech tree and the like. These profiles are relatively constant over an empire, so can be built and cached and not saved).

Step 3 thoughts:
I think the AI already has workers bias their auto build decisions based on the preferences recorded in the city. An interesting side effect of step 2's greedy algorithm is that we might be able to use it for worker AI improvements.

Step 4 thoughts:
There is a difference between "A Unit Factory" and "The Unit Factory", with regard to things like the heroic epic. Building national wonders is a "no-going-back" choice. Making that decision (and factoring it into what to do with a city decisions) is tricky. There is also the question of "should I change my mind".

Step 5 thoughts:
A civilisation who is falling behind in power/production needs more factory/unit factory cities. Everyone needs at least a few. Economic problems can be solved by building economic cities.

Deciding if a production city, or an economic city, is a better choice is a tricky problem that needs information about the civilisation's global state.

Should the AI seek to find a good spot to build each national wonder as they get the national wonder? (as opposed to the current system, where cities decide if they feel like building a wonder ad-hoc)
 
I agree that some central planning in how to specialize cities would benefit the AI greatly. Placement of national wonders and placement of settled great people is all done ad-hoc now, the multiplicative effects of strategizing it properly are huge. If the AI does learn to do this better it might make the highest difficulty levels nearly impossible ... a human player recoups some of the bonuses the AI gets by specializing cities.

Often I find I can just keep up and the game is exciting through the middle ages, but then as my city specialization really starts to kick in I pull away in tech and the later in production ... getting the AI to specialize cities better will have an increasing effect as the turns roll by.

Another piece which should be centralized is picking a new capital, but that's a slightly different exercise.
 
I agree that some central planning in how to specialize cities would benefit the AI greatly. Placement of national wonders and placement of settled great people is all done ad-hoc now, the multiplicative effects of strategizing it properly are huge. If the AI does learn to do this better it might make the highest difficulty levels nearly impossible ... a human player recoups some of the bonuses the AI gets by specializing cities.
Yes, and that is the point. :)

I notice that if I don't build a unit factory, I fall behind in military power. I build a single unit factory (Heroic Epic, some high-food tiles, some high production tiles, forge, barracks, then spam units), and it can almost single handedly keep up with the cheap upgrades that the AI gets until armor shows up. And that city can get gross when armor and aircraft show up, because the AIs discounted upgrades no longer let it keep up...

This doesn't cover empire-wide strategies. Many of the high level strategies of players involve mixing an empire-wide strategy (build a city with lots of scientists to pop great scientists, settle them, build multiple science multipliers in the city (including oxford), use rest of empire to fund itself using cottages and a higher-than-normal tax rate) that have the additional advantage that you only need to build one side of the cash/science building boosters.

...

So implementation thoughts.

Leave the "what improvements are allowed" code outside of the optimisation engine. The step 2 engine simply takes a collection of "plot options", a collection of "specialist options", a single "multiplier" structure (that covers all resources), a resource-weight function, and a happy cap (which will be considered hard) and a health cap (after which, each additional plot costs -1 food). Then give it a "relative value" structure that assigns linear utility values to outputs (research, commerce, culture, unit production, non-unit production, food, great people points).

Greedily take plots until you hit the happy cap by the ones that evaluate best. If you break the health cap, extra plots are worth -1 food. Pay no attention to "can I actually feed these people".

Return the result.

Outside of the function, add in the base plot and it's value. Check for things like "can we actually feed these people". If you cannot feed the people, up the value of food relative to what you are actually working for until you can feed the people.

This gives you a relatively quick O n lgn way to evaluate "how good would this city be as a unit factory". What is better, the results are relatively consistent over time (with new technologies updating the results). It also works for "economy pump", and a check on how big/much food the city can pump out can be used to determine how fast the city is able to grow. It also works to build, say, a specialist city by pumping out food.

Even better than this is that it can be set up to decide without prejudice if a specialist city, or a cottage city, will generate more return... (There are complications here -- cottage cities have to grow their cottages, and specialist cities GPP are worth less over time, and specialist cities need buildings or caste system...)

However, even before that point, this lets a city determine relatively quickly if it is a factory or economic city. Then it can make its decisions about what buildings and national wonders to build based off that knowledge. And workers can relatively quickly query the city they are improving to get a list of the ideal long-term tile mix as far as that city is concerned.

None of this requires any kind of civilization wide coordination beyond a quick glance at the civilisations current balance of cash and research.

The 'ideal placement' of national wonders, making a balance between production and economic cities, ideal placement of a GPP factory, ideal placement of "the unit factory", ideal placement of the "wonder factory" -- these will require a second pass. I'm thinking that these "unique city plans" will end up being a visitor pattern. You evaluate your empire, find a location that is both a sufficiently good spot and the best available spot. Once you find a candidate, you then override the standard city AI for that city.

And as you are able to find these cities using the visitor in a relatively stable way, you can also use this for great person settlement AI (military instructors are wanted by the "unit factory" unique city plan).
 
On Noble / Huge / Marathon with Mehmed, if I can survive until around 1500 AD, staying competitive in terms of land owned, power graph, production -- my power will explode once I get factories online. Heaven help the AI once I get Mining Inc and Sid's Sushi online.

This last 0.60 game, I had a 4.0 power ratio with Mao and he was in 2nd place. The Dutch were in 3rd place in terms of military power with a 9.6 ratio (I had 9.6x more power).

City specialization is a large part of that.

Then there's Justinian, who settled *5* Great Generals in one of his cities, giving him +10 XP units. I'm not sure that was a good move for the AI - I prefer to have lots of +2 and +4 XP cities. Montezuma had a +6 XP city that I captured.

But I could see some roleplay coming into play there. Some leaders should prefer to use GGs for units, some should focus promote, some should seed lots of +2 cities, some should shoot for +4 cities.
 
One of the problems with city specialisation for the AI is that we need the AI to stick to its choices. At present, there is some link between the city emphasize buttons and the terrain improvements around a city which can have disastrous effects when the AI changes the emphasize settings of a city repeatedly and thus repeatedly changes the terrain improvements around a city. The idea behind linking the improvements to the emphasize buttons was city specialisation but it isn't working.

In the above mentioned model something similar could occur, when it is designed with assumptions around constant elements in an empire. For instance, the multipliers for food and hammers and commerce which are talked about above are likely to be not absolutely constant as not every empire has similar terrain but still every empire needs commerce cities and production cities. So likely there should be some empire dependent factor in these multipliers (if we already have 50% hammer cities, then hammers are valued less). But in that case, changes in the empire (caused by war or natural expansion), can change the multipliers which changes the values of existing cities and causes empire wide changes in the types of cities. Such potential disastrous effects need to considered directly in the beginning development stage of this algorithm. These should absolutely be avoided.

Personally, I would like to see the type of a city to be fixed once a choice has been made and only newly acquired cities can get a new city specialisation type which is dependent on what type of city is needed.

Another tough problem is dealing with cities which are under cultural pressure. Should you consider them in their perfect end-state where they have (militarily or culturally) acquired their fat cross of tiles or should you look at them as they are now or should you reevaluate them when the number of tiles available to them changes (culture borders move). As explained above reevaluation is dangerous. You don't want a city with 10 towns to reevaluate it's best specialisation purpose when it loses just one of those towns to culture pressure.
A similar but lesser problem is city overlap. How does the algorithm assign the tiles to the overlapping cities?

A third problem is the availability of resources. The availability of happiness and health resources can quickly change. This should not effect city specialisation.

I'm not mentioning these tough issues because I don't want a city specialisation algorithm for the AI, but because a bad specialisation algorithm is far worse then no specialisation algorithm at all. One of the worst elements of the present AI is that it regularly changes towns (and lesser forms of cottages) in other tile improvements because it can't make up its mind about the end state of the city.

Note also that the biggest gain can be achieved by creating specialised national wonder cities. The effect of the other specialised cities (for a mainly cottage economy) is often exaggerated on this forum.
 
The main problem with city specialization appears to be in the lack of persistent memory. The AI can't remember what a city is supposed to be doing, and so keeps re-arranging its improvements and its focus. I'll leave aside the argument for a non-save game compatible version of Better AI, but there are a few places where memory can be tucked.

Roland Johansen made a lot of good points about how to determine city specialization, so I'm kinda going to glide over that for now. But I agree that a city should have its future specialization determined when it is placed (or conquered, perhaps- leave that to a second order effect for now). Now, specialization has to take into account what you've already got- if this city could be a GPP or a commerce city, you need to know how many of each you already have. One thing you could do would be write an algorithm that has no randomness to it and so will always come up with the same result for its available BFC (or however much of it is used in calculation, for the purposes of a culturally oppressed city), but then to check what it actually ends up as, you'd need to iterate through every previously founded city to get the weights for what was "needed" when you founded this one, and you'd have to do that every turn, and it would still get messed up if you lost a city due to war.

So my proposal is that the AI names its cities based on the results of the specialization calculation. I don't mean naming the city "Commerce 3", but come up with an algorithm whereby city names that start or end with certain letters, or where the ASCII value mod N is a certain value, or whatever, just something consistent.

Now, the AI never renames cities that I know of (and it would be mildly confusing to human players if it did, I suspect), but so long as all AIs used the same algorithm (ie, it isn't civ specific), a city conquered from another AI would already have its name representing its specialization. Its new owner just needs to continue in that direction.

It breaks down with cities taken from a human player, and would add some confusion in that capital cities wouldn't necessarily be what we're used to.

An alternate solution that takes advantage of the same type of "save space"- as I understand it, when the AI decides what troops to send where during wars and such, it considers a couple of troops to be purely defenders that it won't send to shore up another city. The names of such units are another storage space- name a unit, and modify the algorithms for moving troops around to make it extremely hesitant to move a unit with names in a certain pattern. The problem with this, of course, is that you can erase a civ's city's memory by attacking the city enough.

In any case, I dunno if any of these specific implementation ideas are any good, but I wanted to toss out there the idea of using names as memory, it being one of the only things I know of that we can shove information into without breaking saved games.
 
In any case, I dunno if any of these specific implementation ideas are any good, but I wanted to toss out there the idea of using names as memory, it being one of the only things I know of that we can shove information into without breaking saved games.

Interesting idea. You mean that the AI might found its cities in a different order and that for instance (in the normal german city list Berlin, Hamburg, Munich, Cologne, Frankfurt, Essen, etc.),
Berlin will always be a commerce city, Hamburg always a production city, Munich always a great person city, Cologne always a commerce city, Frankfurt always a production city, Essen always a commerce city, etc.

If the German civilization were to decide that its first city would be best suited to be a production city, it will name it Hamburg. If the second city will be another production city, it will name it Frankfurt. If the third city will be best suited for a commerce city, it will name it Berlin. And so on.

I guess that might work.

However, the only way the AI might change the type of a captured city would be by renaming the city. I don't have a problem with that, but I just mention this consequence so that it is known. Before a civilisation starts renaming cities, the new type of city would have to be a lot better to avoid all kinds of changing of the terrain.

Is this what you meant? In case you do, I at least like it. :)
 
Yeah, that's exactly what I meant. I am hoping that most of the time the conquering Civ will agree with the decision of the previous owner as to what a city should be specialized as, and so won't have to change the name (assuming the rules are the same for all civs so it isn't just that Berlin is always a commerce city, but that a city whose name ends in N is always a commerce city). That way the renaming would only have to happen if (a) the conquerer has a ton of that kind of city and wants to change it, or (b) the previous owner was human.

Though it occurs to me that some of the Civ city names might be disproportionately weighted towards certain letters (names ending in "berg" or "ville" or "shire", kind of thing), but that's an implementation detail. ;)
 
Though it occurs to me that some of the Civ city names might be disproportionately weighted towards certain letters (names ending in "berg" or "ville" or "shire", kind of thing), but that's an implementation detail. ;)

I'd just do something like every first city in the city list (Berlin, Washington, London, Paris, ...) would be a commerce city. Every second city in the city list would be a production city, every third would be a great person city, every...

This way every civilisation will have a similar number of cities of a certain type (for instance 20 commerce cities, 10 production cities, 5 great person cities, etc.). This way if the algorithm of choosing the type of city is balanced well and the numbers of cities of certain types are approximately right, then the city names will be used in approximately the normal order.

You'd just need a file with a list of all the names of commerce cities, a list of all the names of production cities, a list of all the great person cities, etc. Then the AI can always look up what type of city a certain city is and treat it accordingly.
 
This suggestion of using city names to store information regarding city specialization made me wonder of what other places in the game could be put to such a use. Signs came to mind. Can the AI place signs? Could the signs then be used by the AI to store information about city specialization, or anything else? This way, the human player wouldn't know what specialization an AI city has by looking at the name. It could also be used to let the AI do a kind of "dot map" for settlement ahead of time.

Likewise, units can be named. Perhaps this can somehow also be used to overcome the limitations of the Civ4 save file format. Maybe the AI could use this to remember what kind of a role a unit is supposed to have, etc.
 
Dude, if the AI can add signs, that would rock so hard they'd feel it on the other side of the world. Even if the signs added were visible to the players, honestly- they could be gibberish that gets translated and have all the signs be placed on ice pole tiles or something, who cares. Unfortunately I have my doubts, but I really dunno.
 
If the German civilization were to decide that its first city would be best suited to be a production city, it will name it Hamburg. If the second city will be another production city, it will name it Frankfurt. If the third city will be best suited for a commerce city, it will name it Berlin. And so on.

I guess that might work.

Well, some people would want to keep the order of cities, or, at least, have the capital of an AI civilization named like its capital in real life.

However, the only way the AI might change the type of a captured city would be by renaming the city.

And not only that - what about historical scenarios like Earth1000AD?
 
I don't see the need of renaming cities. Sure it can be done in a more subtle way? You know, behind the scenes.

Also, if they change cities names to each kind of cities, the human would end up exploiting it somehow.
 
I know that the BUG mod is currently using several layers on the map to store the dot map information that you can use to plan your cities. The BUG mod also doesn't want to change the savegame format, so apparently that information can be stored normally in the savegame. The information can be made visible and invisible to the player by pressing a key combination.

Could such a layer be used to store city specialisation information invisible to the player? I guess the main questions are whether the AI can write on the these layers (it does this in the BUG mod to write signs to designate events that change the map) and whether it can read the information that it would store in these layers on the map.
 
I don't see the need of renaming cities. Sure it can be done in a more subtle way? You know, behind the scenes.

If it could I'd love to see it done, but if it can't, the naming of cities and units is just someplace where data can be stored without modifying the saved game format.

Also, if they change cities names to each kind of cities, the human would end up exploiting it somehow.

Sure, the human could figure out which city names mean which specialization. Or they could just look at the improvements around the city and make an educated guess. ;) (One possibility would be to add an obfuscating element- I know that there are certain seeds based on the x,y location of the capital city or whatever- you could use that to pick between various naming schemes, to cause them to change from game to game. We'll worry about obfuscating information from the player after we see if any of it is viable.)

Lone Wolf makes a really good point, though, about this breaking some mods and scenarios, like the Earth maps. If a player is willing to sacrifice a little bit of game immersion there is always the possibility of just _adding_ a single letter to a city name. ("Washington" becomes "Washington A".) That's ugly and very obviously a hack, but I put it on the table.

The idea of reading the information on signs might wind up obviating all of this, though. An invisible sign over each city to say its specialization would greatly increase the AIs flexibility. If the signs are invisible the sky's the limit- declare that the sign at 10,10 is used for military plans, and the sign at 11,42 is the next tech he wants to research, and...
 
You know how the player can mark terrain with notes/signs/whatever it's called? Maybe the AI can mark city sites with one of these (invisible to the human of course) which would tell it what the city was specialized for. Then we wouldn't have to worry about specific cities always being specialized a certain way (which I think was an excellent idea), although I'm not sure that the AI would be able to draw information from the signs? If not, maybe it's possible to code something like that in?

Then, when founding a city, the ai can run some quick heuristics to determine what the city would best be optimized for. It could place a marker on the city tile so it remembers this, and appropriate markers in the fat cross corresponding to what improvements should be built there. Workers would then draw their information from the markers around the city when improving tiles.
 
I don't want to let this fall off the universe entirely- how could we find out if the AI can make use of the strategy layer? Does anyone know?
 
Back
Top Bottom