• We have updated our Community Code of Conduct. Please read through the new rules for the forum that are an integral part of Paradox Interactive’s User Agreement.

CK2 Dev Diary #71 - AI and Optimization

Good afternoon, everyone. I’m Magne “Meneth” Skjæran, back again for another dev diary. The list of previous topics I’ve covered is getting too long to include in full, so I’ll just include some of my favorites: modding, optimization, and quality of life.
Today I’ll be covering one of those topics once more: optimization. I’ll also talk a bit about what we’ve done to improve the AI in the upcoming patch; I’ll even share the whole AI changelog, which is by far the biggest in CK2’s history.

Let’s start out with optimization. Optimization hasn’t been a focus on anywhere near the level it was in The Reaper’s Due, but with the sheer number of new provinces we’ve added in Tibet, the Tarim Basin, and the Middle East, we wanted to ensure the game wasn’t slowed down. So during development we spent some time profiling the game to identify parts that might be made faster, and have kept track during development of the average speed of the game in our overnight hands-off campaigns.

The result of this was roughly 50 changes across a variety of different systems, ranging from tiny optimizations to a few large ones. The largest, sadly, was simply fixing a performance issue introduced earlier in JD’s development, but still this resulted in a noticeable improvement over 2.7.

Here’s a small selection of the changes we made:
  • Ported HoI4's optimization to GUI focus processing
  • A few optimizations to unit map icons
  • Courtiers now only check events every 50 days instead of 20
  • Checking if minor titles need an update is now done in parallel
  • Informed the AI that we changed how decadence works years ago and it shouldn't give titles to its family members just for the hell of it
  • Unlanded characters will no longer take the "become council member" ambition
  • Added "ai_check_interval" parameter for decisions. This determines how rarely the AI checks a given decision, and can be useful for performance-heavy decisions that don't need to be checked often
  • Plot and ambition selection is now done in parallel, and applied in series
  • Raid armies now only re-evaluate where to go if they have a compelling reason, or haven't done so in a while, like regular armies
Essentially, a bunch of small changes, including some increased parallelization.
On top of this, we also made a few changes to avoid unnecessary characters getting generated:
  • Restricted a few events/decisions that would generate characters to playable characters, and one to non-mercenaries only
  • Improved PopulateCourt so that more characters than necessary aren't generated, by better counting councilors, counting existing concubines, and ensuring only AI characters that would want to marry generate marriage candidates
  • Characters currently in hiding or prison can no longer impregnate women, who were already excluded from impregnation if they were in hiding or prison
  • Made AI baron generation try to find an appropriate character in court first; only if that fails is a new character generated
  • Fixed naval mercs generating sub-unit commanders
As a result, despite the significant number of provinces 2.8 adds, and the extra characters arriving from China, the game now plateaus at a similar level to 2.7:
upload_2017-10-16_12-0-37.png

For comparison, here’s a similar graph from Reaper’s Due:
vMnckpc.png

Sadly I don’t have a similar chart from MnM. To the best of my knowledge, in MnM it would look very similar to JD’s chart.

There’s been a bunch of other minor changes between what I’ve listed above, but they’re too many and too technical to be especially interesting.
Overall, in our tests, these changes combined have resulted in the first decade of the game being 8 to 16% faster than 2.7. As you can tell, this varies some by PC, but every single machine we tested it on ran faster in 2.8 than 2.7. Note that as our main goal was simply to ensure that no slowdown had occurred, we only have precise metrics for the first decade, though our overnight tests indicate that roughly the same result holds through for long-term performance as well.
We’ve also taken a few steps to reduce memory use a little, though we don’t expect this to be noticeable except perhaps for those on machines with very little RAM.

Now, let’s move onto the AI. The CK2 AI has always had a few idiosyncrasies, though the character-based nature of the game means that this doesn’t hurt the game experience as much as it might in other games, since human beings doing silly stuff is quite normal. However, some areas can be frustrating even with that taken into account, such as troop movements, and the AI having little regard for border gore when handing out titles.
In 2.8 we’ve therefore spent quite a bit of time on various parts of the AI. Without further ado, here’s the full AI changelog for 2.8:
- Fixed one way AI characters that should have AI could end up with no AI
- The AI now actually considers the idea of sending its army to a province if its friends are there or adjacent, even if the province is otherwise a bit boring
- The AI now actually realizes that friendly armies standing near a potential target is a good thing even if it doesn't personally own those armies
- Once the AI gets to its allies it won't decide that if it can't attach due to supply, it is better for everyone if it just packs its bags and goes home. Instead it will now try to stick close
- The AI is now vaguely aware of the existence of tribal vassals when evaluating whether a faction has a real chance of succeeding
- The AI now checks to see exactly how many liege levies it'll deny its liege by revolting, rather than going "eh, half my levies I guess?"
- The AI now takes tribal vassals into account in most decisions where it considers how strong a potential enemy or ally is, rather than simply pretending they don't exist
- Fixed aggression from ambitions having 20 times as big an effect as intended
- AI will now also call allies to arms if the estimated answer is a "maybe," to force them into making a decision.
- Raiding adventurers now realize once they've been settled that maybe, just maybe, they should consider their new realm their base of army operations, and return armies there when they're not up to anything
- AI: Should be snappier to evaluate settlement decisions when needed
- If defensive pacts are disabled, the AI will no longer think no one could possibly be a threat to it
- AI: Made the Pope less reluctant to accept requests for excommunication
- AI (Conclave): Will never pick Heritage or Faith Focuses for children of the right culture and religion
- Tributaries are now always aware that their tributary relationship benefits their suzerain more than them, and that they could potentially break free
- The AI no longer gets increasingly reckless the more "rational" it is; being enlightened by your intelligence turns out not to make your troops any better
- The army AI no longer thinks that just because two destinations are pretty much equally good, it should switch back and forth between the two and thus never get anywhere
- The army AI now understands that if it has bothered to start a siege, maybe there was a good reason to do so, so we shouldn't just run off right after just because another destination looks very slightly better
- Taught the AI that more loot means a province is a better target for looting
- The army looter AI now actually realizes that if all the loot in a province is protected by holdings and the army isn't big enough to siege them, it isn't actually a good loot target
- The AI now considers any strong realm nearby a potential threat
- Taught vassals how to send alliance offers to other vassals
- Informed the AI that we changed how decadence works years ago and it shouldn't give titles to its family members just for the hell of it
- Told the AI that if it is the attacker in a war, it *might* be a good idea to actually go attack the defender even if you're targeting their whole realm rather than a specific title within it, and it is a bit of a long walk
- The AI has learned how to spell, and no longer look for "soceities" missions when they're actually called "societies" missions. With this newfound ability to spell, the AI is better at identifying mission targets
- Taught nomads that if they rise in revolt in response to an attempt to split their clan, maybe they should accept it next time rather than rise again and again
- The AI is no longer deathly afraid of going overseas to help its allies just because the enemy is a bit far away
- The AI no longer thinks that just because a province is *someone*'s capital, it's a good target; it is now more discerning and only cares if it is the capital of an enemy
- The AI is now a bit more aware of just how superior heavy cavalry is to light infantry
- The AI now no longer thinks that enemy armies adjacent to other enemy armies are just on vacation, and won't help out if it were to attack
- Gave the AI a crash course in how to hand out titles
- Gave the AI a crash course in how to transfer vassals in a way that won't make you want to claw your eyes out
- The AI now realizes that just because it has occupied everything it borders, it shouldn't skip out on occupying the non-bordering stuff
- Taught the AI a few things about how supply works, and why a doomstack isn't always the best way to deal with it while travelling to its destination. The AI is hard of hearing, so it only partially understood this lesson, but partial understanding beats no understanding, right?
- Taught the Pope Geography 101, with an emphasis on what "distance from Catholicism" and "size of kingdom" means in the context of Crusades, and why this means that invading India when Egypt is right there might be a bad idea
- Informed the AI that just because their ideal commander can't lead troops right now, doesn't mean they should just skip assigning a commander to the flank they wanted to assign that commander to
- The AI now realizes that once it is landed, it should base its army in its capital rather than have it stand in the middle of nowhere, taking attrition, just because it happened to spawn in the middle of nowhere many years ago
- Betrothed men no longer go "nah, can't fulfill this betrothal" after sending a marriage offer which you accidentally reject. They will also no longer decide to not bother to resend the offer while mumbling "let's see how *they* like being ignored"
- Told just-spawned raiding adventurers "hey, your fleet is right over there" so that they wouldn't disband instantly when spawning on an island with no good raid targets, due to thinking that getting across the sea is simply impossible
- Members of small reformed religions are now a bit more fond of joining Great Holy Wars and the like
- The AI has finally come to the realization that it is allowed to invite its own vassals and courtiers to plots, not just the vassals and courtiers of the target

Shortly put, this fixes some of the more blatant mistakes the military AI would make, though I’m sure there’s still some left; I’ve personally still observed a few AI loops where the AI will start and stop moving every day, but sadly that wasn’t reproducible from save and thus couldn’t be fixed. It should be a bit more committed to its actions now, and better at figuring out where it should actually be going.
Beyond that, the AI is now much better at handing out titles and transferring vassals. For the hand-out part, it tries to avoid making existing vassals stronger, instead preferring to create new ones, and tries to maintain its culture and religion if reasonably possible, as well as strengthening its dynasty. For vassal transfers, it now tries to give it to adjacent vassals so that sub-vassals don’t end up scattered all over their realm. Inheritance will still lead to that kind of thing, but that’s historical, so not something we consider much of an issue.
There’s been a number of other tweaks, but one I think people will be especially glad to hear of: the Pope should no longer be calling random crusades for India when he’s got targets much closer at hand.

That’s all for today. If you’ve got any questions, feel free to ask them in the thread.
 
Last edited:
Not sure about the others, but there's a Define for frequency of character events, so you could easily change that with a mod.
I don't think it would really be noticeable though - you can't really tell when an unlanded character is getting an event, and the game calculates there's chances so that the yearly average is still correct.
 
The game does generate women for unmarried rulers if there's no decent target available in their court. However, it might currently be slightly too lenient about what's a "decent target", leading to no one getting generated even though they don't want to marry that "decent target".
Haven't noticed any massive issues with this though.

What event creates young females?
 
@Meneth i really appreciate all the various AI changes you mentioned as CK2 can have a pretty bad AI at times.


There’s a few obversations I keep seeing in my unmodded games that I hope you address. I’ll name a few off the top of my head, these are the ones I see most often that don’t make sense to me.




The first is anytime a ruler acquires land and is over their demense limit. Either through inhereting it or through holy wars. When this happens, they give away titles that are adjacent or close to their capital, or baronies in their capital, and keep distant, weak, penalized, counties. A good AI would give away these distant titles for others to manage and keep their strong, centralized locations. Hopefully you can look into this.


Another big one is that AI Kings and Emperors, when owning a single top title, like the kingdom of England, will create the kingdom of Scotland if they can. Why? I’ve also seen the emperor of the HRE create the empire of Germanía. Why? This move makes no sense. And the top titles were primogeniture, not gavelkind. So what purpose can it serve to hold 2 kingdom primogeniture titles when the Duchies of the second kingdom were being drifted into the main one? I saw the king of England almost completely integrate the duchies in Scotland, but then he made the kingdom of Scotland! And thus negated all that progress which would have made the title of England much more powerful. This absolutely ruins so many games because it just makes no sense and fractures realms. Something the AI should try its best not to do.


Another is that AIs are so bad at managing their realm. They hold counties and duchies resulting in many vassals hating them and then revolting, when a good AI would give them these titles to keep them happy and try to reduce their power in other ways such as valid imprisonment and revocation. Too often I see the AI with good traits, genius and such, incur a lot of tyranny. Supposedly these traits increase their “rational” decision making but too often I see horrible decisions made.


The AI also doesn’t gift gold enough, which as a player, is such an easy way to maintain a happy realm. It gives a huge 5 year boost, and you more than makeup the gold through taxation after a short while. For example, when a new ruler comes to power because the parent died, if they inherit a lot of money, they shouldn’t spend it on holdings but rather increase the opinions of their vassals to avoid revolts post-succession. As a player I do this with remarkable success. A smart AI would do likewise. So I’d like to see the AI manage their realms a lot smarter. It would result in better games with less random fractures for dumb reasons.


There’s also constant surge of revolts everywhere, attempting to increase council power, and they always fail. Why do factions lose so often? They clearly aren’t taking into account proper strength of the liege and themselves. Can you look into this and make sure the right numbers are being used? Every game I’ve played I’ve seen revolts in dozens of places and they all constantly have less men than their liege. This is just dumb. A faction shouldn’t fire unless they have at least 120% of the lieges potential troops post-revolt. Currently it’s just done wrongly.


I saw you addressed the call to arms mechanic for tribal nations. I suspect we won’t see tiny nations attack large tribal ones anymore just because they miscalculated how many of their vassals would answer the call to arms. Likewise you answered that this mechanic, if modded into feudal realms, would likewise work. I hope this is true, because it’s such a neat mechanic that would fit perfectly with feudal realms calling their vassals to arms. Hopefully the war AI works well and stays close to the war leader’s troops.


There’s a lot more but I typed enough from memory. If I got a notebook and wrote down things I saw as I played i know I’d have a lot more. The game is great, I love the idea of it, but the AI is just so faulty..
 
What event creates young females?
It happens in code, not by event.

The first is anytime a ruler acquires land and is over their demense limit. Either through inhereting it or through holy wars. When this happens, they give away titles that are adjacent or close to their capital, or baronies in their capital, and keep distant, weak, penalized, counties. A good AI would give away these distant titles for others to manage and keep their strong, centralized locations. Hopefully you can look into this.
Part of the title grant overhaul improved this a lot. We iterate over titles backwards now, so it checks the newest titles first. It also tries to avoid giving away stuff in its capital duchy, and also any other duchy it holds personally (prioritizing keeping capital duchy stuff over secondary duchy stuff).
So combined, that makes it in most cases much more sensible than it is right now, and at worst equally stupid.
Though it still prioritizes getting rid of baronies, even if they're in the capital, above getting rid of distant counties.

There’s also constant surge of revolts everywhere, attempting to increase council power, and they always fail. Why do factions lose so often? They clearly aren’t taking into account proper strength of the liege and themselves. Can you look into this and make sure the right numbers are being used? Every game I’ve played I’ve seen revolts in dozens of places and they all constantly have less men than their liege. This is just dumb. A faction shouldn’t fire unless they have at least 120% of the lieges potential troops post-revolt. Currently it’s just done wrongly.
We improved the strength calculation some in 2.8; it didn't properly understand how many troops it'd deny to its liege by revolting, leading it to in many cases overestimate its strength.
So now it's a script issue if anything. Should be better in 2.8 than in 2.7, anyway.
 
Its good to hear that some of these issues are being fixed. I eagerly look forward to this update. CK2 is by far one of the most immersive and interesting games to me. I love seeing kingdoms rise and fall against each other. I just wish characters didn't sometimes act like they had all three traits at once, imbecile + lunatic + rabies, when they don't :p


Though it still prioritizes getting rid of baronies, even if they're in the capital, above getting rid of distant counties.
I wonder why you maintain this function? Baronies in the capital county provide +50% additional troops and receive bonuses from councilors in the same county. This stacking outweighs any benefits from owning a separate county, even if you do receive some more tax revenue from the new baron vassals.

Do you think the choice is still right to abandon baronies in the capital?


And besides this one point. Do you know why the AI creates additional top titles when it need not to? Like my example of the King of England creating the Kingdom of Scotland when he has the primogeniture law? What purposes does it serve besides giving people a chance to revolt to claim the title and thus fracture the realm?

Anyways, I think you guys are making a lot of progress with this patch. I may be too eager to see everything fixed all at once :)
 
Do you think the choice is still right to abandon baronies in the capital?
Probably not, but it doesn't make a big enough difference for us to care all that much. With the exception of your capital, it generally makes sense to give away baronies first, so it makes the logic more straight-forward.

And besides this one point. Do you know why the AI creates additional top titles when it need not to?
I'm not sure what the original reason for this is, but there's a define somewhere to turn it off.
 
Why not add an option where you can join a rebellion later? That way you have time to decide who to support? Winning key battles would let you take advantage of the momentum and get undecided lords to fight on your side.

Rebel lords should also expect to be given rewards. They should ask for X county or Y duchy in exchange for their troops fighting on your side.
 
Why not add an option where you can join a rebellion later? That way you have time to decide who to support? Winning key battles would let you take advantage of the momentum and get undecided lords to fight on your side.

Rebel lords should also expect to be given rewards. They should ask for X county or Y duchy in exchange for their troops fighting on your side.
I agree, although I think this should be taken a step further: the ability to leave a war at any given time and rejoin if desired. This could be penalized by a Prestige hit and opinion malus from the members of the alliance (more from the main attacker/defender). Maybe a requirement to stay a determined amount of time before doing so (say, 1 or 2 years) in order to not abuse the feature.
 
I agree, although I think this should be taken a step further: the ability to leave a war at any given time and rejoin if desired. This could be penalized by a Prestige hit and opinion malus from the members of the alliance (more from the main attacker/defender). Maybe a requirement to stay a determined amount of time before doing so (say, 1 or 2 years) in order to not abuse the feature.
Accompanying the prestige hit and opinion malus that would inevitably arise from such an action, the ability to leave a conflict during its duration should require characters to make a separate peace with the entity they are opposing. It would be a bit nonsensical for players or the AI to suddenly hang up their gloves and start acting as if everything was back to normal without facing any tangible political consequences (other than prestige hit and opinion malus which you have ascribed).
 
Accompanying the prestige hit and opinion malus that would inevitably arise from such an action, the ability to leave a conflict during its duration should require characters to make a separate peace with the entity they are opposing. It would be a bit nonsensical for players or the AI to suddenly hang up their gloves and start acting as if everything was back to normal without facing any tangible political consequences (other than prestige hit and opinion malus which you have ascribed).
Why should there be consequences from the (former) enemy side? As of now, there aren't any after the war in which you joined as ally. The primary attacker and defender have a truce, but not any of the other participants. And as stated, you would have Prestige and opinion hit from allies.
But I get where you come from. You should probably have a small-duration truce with the primary attacker/defender if you leave the alliance or stay hostile for a while (like when you get raided).
Although I can see right now all the rageposts for when the AI leaves you hanging when you need them and join your adversary, since AI is being optimized.
 
Tears of happiness. But does muslim AI understand now that he can appoint a good son (or grandson) by giving out titles, not only first born?
Their own unlanded kids is the AI's first priority when handing out titles.

I think he wanted to know if muslim AI realises that handing out titles to their sons and grandsons will influence their succession and they could actively change their heir by handing out titles.

There's another issue I observed regarding AI marriage:
Sometimes, my vassals simply won't marry. It doesn't happen often but it's not rare either.
They turn 16 and don't marry for 20 years despite having the ambition to get married. This affects counts and kings alike.
 
What would you do?
Well in the morning you'd wake up, beat a servant during breakfast, go to court and chat up Ermengarde and Adalbert, complain about the liege being brave, go look for the liege who's hiding pathetically to avoid you, have an argument with a stablemaster. etc. repeat. Definately the cat's meow if you ask me.

Jokes aside, it would be nice if they would be more involved in a more interesting way other than being in it for the opinon modifiers.
 
What would you do?

What wouldn't I do ?

Play as the leader of a Holy Order or dynamic mercenary company, play as as baron and work my way up from nothing, create some wicked modded events and decisions and play as a true landless character running a Guild, an Adventurer's company or a religious or cultural revolt. Travel the entire world, tagging each capital I visited and then compiling a magnum opus on my travels throughout the known world, create my own religion or society, visit all the Universities and centers of high Learning on the map and then write down all my knowledge in a grand treatise, conquer an unclaimed province in sub-saharan Africa and become Prester John.

You know, the usual.