• 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.

HOI4 Dev Diary - Performance and AI

Hello! I’ve been drafted from EU4 to fight in the trenches together with the HOI4 team for a month and give some support on AI and performance. So I’m here to talk about those.

AI

I’ve been mostly devoting my time into improving garrison orders to solve some really wonky cases that could happen. Like it placing two units on Magdeburg instead of Berlin letting the capital be completely exposed. But also focused on making the garrison work a lot better when you don’t have enough troops to cover every point in the order that needs it.

First I implemented a new tool for HOI4 to let us programmers visually inspect what the unit controller is up to.

capture(95).png

Province Weights

capture(96).png

State Weights


After that came the big task, rewriting garrison order and how it assigns units. There’s been a lot of issues here and you’ve probably experienced them from time to time. Shuffling, units prioritizing the wrong states or provinces, the list goes on.

So what did I do that is new. Instead of having the old logic setting up rules for where units should go I went with a thing I hate. Mathematics. The main difference is that each state will sort of calculate how “painful” it is to remove a unit from it now. This means as each state goes through this, they will be trying to find the least painful way to perform the garrison order and will eventually settle on a sort of equilibrium.

It’s given us a lot easier way to control where units should go in a garrison and where they end up will match a lot more in style of the weights you can see on the pictures above.


A thing that we have been planning to do for a long time and finally got around to is to allow a more flexible way to control how the AI plays. We use a system we call “Ai Strategic Plans” for this. Its essentially telling Ai to act a certain way when it comes to national focuses and priorities based on scripted conditions. To put that in english I am going to hand over to @Bratyn for some examples:

Some situations in which the new Strategy Plans have been useful is to ensure ‘compatible’ AI behaviour in the case of an ahistorical Germany. A lot of the AI behaviours for the major European powers has been based on the assumption that Germany remains fascist and causes mayhem in Europe. This means that AI Italy would usually see Germany as an ally, and that AI France would either always go with the United Kingdom (if historic mode was turned on), or be so unpredictable that it was problematic for planning out and predicting alternate history scenarios.

Using strategy plans, we have made these AI nations partly behave more logical to the changing world scene, and partly ensure that the player will always have a challenging playthrough. Going democratic Germany, only to get your only enemy (Soviet Union) end up at war with both the Allies and yourself, is not a very fulfilling playthrough. Instead, a new strategy plan will now load for France upon German completion of the “Oppose Hitler” focus. This delays their pick of which political direction to head into until we have more information from the German player, essentially a ‘holding pattern’.

  • If Germany revives the Kaiserreich and chooses “Focus on the True Enemy”, France is released from the holding pattern and will go with Britain, forming a counter-reaction to rising German colonialism. Essentially, in this playthrough not much will change other than Germany being unaligned rather than fascist. The war will likely develop in a similar matter (though not on a similar timeline), with one war with the Allies, and one war with the Soviet Union.

  • If Germany revives the Kaiserreich and chooses “Expatriate the Communists”, these communists will head to France and give them a communist drift national spirit. France is released from the holding pattern and will go communist, choosing to side closer with the USSR so that both powers can stand against the new Anglo-German alliance.
  • If Germany goes democratic, France is released from this holding pattern and will choose to Revise Versailles. Which direction they pick next is up to the AI, ensuring some unpredictability. France may go Communist and start their own faction, or join the Soviet Union, ensuring a two-front war if Germany goes to war with the USSR. Alternatively, the French may go fascist. They then will go for “Woo Italy”, creating a new semi-powerful Fascist faction in south-west Europe. Similar strategy plans for Italy (and liberal application of AI strategy values…) ensure this alliance is stable. A new focus for france “Dismantle the Democracies” becomes available if Germany is democratic, and the AI has a high likelihood of taking this when the Germans are already at war with the USSR. This makes gameplay more dynamic, and ensures Germany will have ample enemies to fight against even if they elected to go democratic.
We also used AI strategy plans to teach the Chinese AI how to handle the Inflation mechanic and do things like making the PRC AI keep the peace in China when they are going for the peaceful takeover through the political struggle. All in all, to myself and @Archangel85, this has been (and will continue to be) an invaluable tool for ensuring believable and, most importantly, fun playthroughs for each ahistorical path that one can take. We hope to continue building upon this basis and make nations ‘smarter’ in dealing with alternate history situations, or just general oddities that arise in the state of the world over the course of a playthrough.

Modders I am sure are going to like this a lot, so for you guys check out the example below for how it can be set up (regular humans without modding superpowers can safely ignore ;) ).

upload_2018-2-21_16-49-6.png


upload_2018-2-21_16-49-24.png

PERFORMANCE

Late game performance have always been a tough nut to crack for us on our games. For Cornflakes we rolled up our sleeves and got elbow deep into the code to improve it the best we could.

In order to measure performance and see where it goes bad to better pinpoint what causes the slowdown I write a little script that generates this super sexy graph for us.

capture(93).png


We did tons of neat little tricks to improve our performance. AI when it micromanages does a lot less pathfinding now. The game will automatically balance workload between cores depending on how long the hourly update took for a country, The fronts system has been a lot more streamlined to make it not hog performance

And we’ve done quite A good job at it as well. Here’s the result of our hard work just this week

capture(94).png


Between finding ways to make things more efficient and utilizing the cores on the CPU better we’ve gotten an overall improvement late game. We won’t rest there though, there’s tons more that we can do to make late game an even more enjoyable experience!



To conclude this Dev Diary, we have a reminder for you. Tune in for the first day of the Three Day War later tonight, 20:00 until 22:00 CET. Since the release of Waking the Tiger is approaching we have invited some of the grandest streamers to battle it out in multiplayer and show off the expansion as well as put the build to the test.


Quill18, Alex the Rambler, Marbozir, Enter Elysium, Solar Gamer, Spiffing Brit, Drew Durnil, The Prussian Prince, Bratyn, Da9L, Shen Plays and Jay’s Gaming will all be streaming their point of view in their own channels, and we’ll be hosting a main observer stream at the Paradox Twitch channel: https://www.twitch.tv/paradoxinteractive

Next week we are going to try and do a big recap of all the features and changes and also talked about a bunch of small stuff that hasn't fit into diaries so far. See you then!


Rejected Titles:
Droogies Quest For The Missing Core
Hoi4 Dev diary: Reverse the polarity of the deflector dish
Graphs are hot!
 
Last edited by a moderator:
Excellent idea for France reacting to Germany. Does this French behavior only apply to player Germany, or to AI Germany as well? Will France delay choosing a political direction (Revise/Status Quo) if Germany doesn't go Rhineland/Oppose straight away? If so, for how long will they linger? Regardless, this is a great way to make Single-player more interesting.
 
U should watch stream, AI in battle doesn't look to much improved...
Err, thanks :( Used to watch the streams, but find their rush tactics increasingly banal.

I am open enough to still have a wait and see to the whole package of AI improvements. But they drop the ball if the basics are not significantly improved.
 
Yea. I think it maybe worth looking into. Timing wise its a bit risky to mess around with the map more now in asia tho
I am aware of that and an argument can certainly be made that it should be part of Tibet. However, this was a case where I would have to investigate further why this particular border was chosen when the game was first released (that was before my time here), and that was more effort than this would likely be worth (it's usually a 50/50 chance between "that's how it was in HoI 3, because that was how it was in HoI 2" and "there are AI issues"). I've made a note about it and will revisit when we have a release out and are working on follow-up patch stuff.


I am very thankful for your answer, knowing it is on your radar is a relief :D
Thank you for the great work on what is a 2.0 patch in all but name ;)
 
Awesome looking update! Any chance we could change the historical/ahistorical option at game start to more of a slider? With all the alternate history stuff the new patch adds I think it would be neat to improve the odds of the AI making non-standard choices beyond the slight encouragement they get in ahistorical.
 
(Keyboard screwy atm)

Grooogy I can see there are major improvements. But i have major concerns. There is no reason why reviving kaiserreich should be incompatible with peace with france. exterminating communists should also not guaranty France goes communist. Further, I'm concerned no improvements have been made to automatic repairing of naval ships. Currently it is completely broken, as the entire fleet will go to a single port, and will not repair because there are too many in one port.
Would a dev please address my concerns if they are able to?
 
Mathematics, the most beautiful thing ever concieved my mankind...
Also, a good algorithm is a very powerful thing.

Also, it is very satisfying to see the performance improvement in the graphs, I can't wait to see this patch out.

Keep up the great work.
 
Cheers for the DD Groogy/Bratyn, and the extra info Podcat/Archangel :D. More great improvements in this weeks' DD - AI and performance are two things that are well worth some quality elbow grease, and sounds like they've had some quality attention :). Those AI strategy plans enabling greater control over AI behaviour also sound tops both for now and down the track :cool:.

Speaking of performance, here's a pic of a Shimakaze class destroyer - at a designed speed of 39 knots, its performance was blistering :D

1920px-Shimakaze.jpg


First I implemented a new tool for HOI4 to let us programmers visually inspect what the unit controller is up to.

Not trying to be greedy but:

Would changing the defines values for the various garrison priorities change how the tool shaded the map? If so, this tool be pretty handy to modders tinkering with how the various garrison priorities worked. Would making this kind of thing to modders be feasible/appropriate, if it was easy to do? License granted to roll eyes at silly forumite asking for something that's inappropriate and hella hard because they're clueless :).

If Germany goes ahistorical and historical mode is turned on, the strategy plans will overwrite the historical focus list anyway, resulting in the exact same behaviour as ahistorical mode. The rationale is that it is not necessary (and not possible) to continue on historical mode when the player him/herself goes ahistorical.

This is particularly cool - historical mode becoming ever more of a 'plausibly historical' mode, rather than HoI3 "we're doing this now because the code made us, regardless of international situation" is a good thing (well, at least imo, but it's the only o I have) :D.

It's be really cool if those AI strategy plans could be more exposed into the UI. For example you could set up a disloyal fascist commonwealth strategy plans for Canada, South Africa, British Raj, British Malaya, Australia, and New Zealand that would tell them to pursue an independent and fascist agenda. And then when starting a campaign instead of historical/ahistorical check box, you could enable those plans for those countries and have a more challenging UK game. If that selection were also moddable, I could see that being especially valuable for some of the more chaotic mods like Kaiserreich (for example playing Ireland expecting a showdown with England but that never comes because they elected the wrong leader).

From the look of the script in the spoiler, the enable = {} block looks like it'd take a range of conditions. We'd need to wait until after release to test and find out for sure, but if that block would take a flag, then it'd be possible at the start of a mod to have options for setting strategies (or collections of strategies - and using flags it needn't be particularly strict, so an event choice could lead to one of possibly 3-4 strategies, keeping players from knowing what was going to happen). Given the pretty specific nature of how these things would be, it's probably something for mods rather than the base game (although it wouldn't be the first time I've guessed wrong on this).

I wonder how many actually plays this game on i7 and will be able to benefit from these performance improvements...?

Totally guessing here, but I'd imagine performance improvements generally involve reducing the number of CPU cycles required to play through a particular amount of time in a particular situation - so any CPU would benefit, as it'd have less work to do, and wouldn't be i7 specific. That's not to say that different CPU architectures might not have proportionally different benefits, but I can't imagine that general performance improvements wouldn't be generally applicable to most PCs.

Of course tons of improvement for the ahistorical path and 0 for those who would like to avoid fantasy WW2.

Just talking about this DD, the performance and garrison improvements (2/3rds of the features showcased in the DD) are both likely to be very helpful for people playing historical games. Looking at 1.5/WtT more generally, more historically plausible Chinese set-up and behaviour, the border wars in China, chain of command, and air supply are all things that make historical playthroughs better (and that's just off the top of my sleep-deprived, illness-addled head, I'm quite sure there's other things that have slipped my mind - like Decisions! There's a biggy I forgot about ).
 
Hello! I’ve been drafted from EU4 to fight in the trenches together with the HOI4 team for a month and give some support on AI and performance. So I’m here to talk about those.

AI

I’ve been mostly devoting my time into improving garrison orders to solve some really wonky cases that could happen. Like it placing two units on Magdeburg instead of Berlin letting the capital be completely exposed. But also focused on making the garrison work a lot better when you don’t have enough troops to cover every point in the order that needs it.

First I implemented a new tool for HOI4 to let us programmers visually inspect what the unit controller is up to.

View attachment 337117
Province Weights

View attachment 337118
State Weights


After that came the big task, rewriting garrison order and how it assigns units. There’s been a lot of issues here and you’ve probably experienced them from time to time. Shuffling, units prioritizing the wrong states or provinces, the list goes on.

So what did I do that is new. Instead of having the old logic setting up rules for where units should go I went with a thing I hate. Mathematics. The main difference is that each state will sort of calculate how “painful” it is to remove a unit from it now. This means as each state goes through this, they will be trying to find the least painful way to perform the garrison order and will eventually settle on a sort of equilibrium.

It’s given us a lot easier way to control where units should go in a garrison and where they end up will match a lot more in style of the weights you can see on the pictures above.


A thing that we have been planning to do for a long time and finally got around to is to allow a more flexible way to control how the AI plays. We use a system we call “Ai Strategic Plans” for this. Its essentially telling Ai to act a certain way when it comes to national focuses and priorities based on scripted conditions. To put that in english I am going to hand over to @Bratyn for some examples:

Some situations in which the new Strategy Plans have been useful is to ensure ‘compatible’ AI behaviour in the case of an ahistorical Germany. A lot of the AI behaviours for the major European powers has been based on the assumption that Germany remains fascist and causes mayhem in Europe. This means that AI Italy would usually see Germany as an ally, and that AI France would either always go with the United Kingdom (if historic mode was turned on), or be so unpredictable that it was problematic for planning out and predicting alternate history scenarios.

Using strategy plans, we have made these AI nations partly behave more logical to the changing world scene, and partly ensure that the player will always have a challenging playthrough. Going democratic Germany, only to get your only enemy (Soviet Union) end up at war with both the Allies and yourself, is not a very fulfilling playthrough. Instead, a new strategy plan will now load for France upon German completion of the “Oppose Hitler” focus. This delays their pick of which political direction to head into until we have more information from the German player, essentially a ‘holding pattern’.

  • If Germany revives the Kaiserreich and chooses “Focus on the True Enemy”, France is released from the holding pattern and will go with Britain, forming a counter-reaction to rising German colonialism. Essentially, in this playthrough not much will change other than Germany being unaligned rather than fascist. The war will likely develop in a similar matter (though not on a similar timeline), with one war with the Allies, and one war with the Soviet Union.

  • If Germany revives the Kaiserreich and chooses “Expatriate the Communists”, these communists will head to France and give them a communist drift national spirit. France is released from the holding pattern and will go communist, choosing to side closer with the USSR so that both powers can stand against the new Anglo-German alliance.
  • If Germany goes democratic, France is released from this holding pattern and will choose to Revise Versailles. Which direction they pick next is up to the AI, ensuring some unpredictability. France may go Communist and start their own faction, or join the Soviet Union, ensuring a two-front war if Germany goes to war with the USSR. Alternatively, the French may go fascist. They then will go for “Woo Italy”, creating a new semi-powerful Fascist faction in south-west Europe. Similar strategy plans for Italy (and liberal application of AI strategy values…) ensure this alliance is stable. A new focus for france “Dismantle the Democracies” becomes available if Germany is democratic, and the AI has a high likelihood of taking this when the Germans are already at war with the USSR. This makes gameplay more dynamic, and ensures Germany will have ample enemies to fight against even if they elected to go democratic.
We also used AI strategy plans to teach the Chinese AI how to handle the Inflation mechanic and do things like making the PRC AI keep the peace in China when they are going for the peaceful takeover through the political struggle. All in all, to myself and @Archangel85, this has been (and will continue to be) an invaluable tool for ensuring believable and, most importantly, fun playthroughs for each ahistorical path that one can take. We hope to continue building upon this basis and make nations ‘smarter’ in dealing with alternate history situations, or just general oddities that arise in the state of the world over the course of a playthrough.

Modders I am sure are going to like this a lot, so for you guys check out the example below for how it can be set up (regular humans without modding superpowers can safely ignore ;) ).


PERFORMANCE

Late game performance have always been a tough nut to crack for us on our games. For Cornflakes we rolled up our sleeves and got elbow deep into the code to improve it the best we could.

In order to measure performance and see where it goes bad to better pinpoint what causes the slowdown I write a little script that generates this super sexy graph for us.

View attachment 337115

We did tons of neat little tricks to improve our performance. AI when it micromanages does a lot less pathfinding now. The game will automatically balance workload between cores depending on how long the hourly update took for a country, The fronts system has been a lot more streamlined to make it not hog performance

And we’ve done quite A good job at it as well. Here’s the result of our hard work just this week

View attachment 337114

Between finding ways to make things more efficient and utilizing the cores on the CPU better we’ve gotten an overall improvement late game. We won’t rest there though, there’s tons more that we can do to make late game an even more enjoyable experience!



To conclude this Dev Diary, we have a reminder for you. Tune in for the first day of the Three Day War later tonight, 20:00 until 22:00 CET. Since the release of Waking the Tiger is approaching we have invited some of the grandest streamers to battle it out in multiplayer and show off the expansion as well as put the build to the test.


Quill18, Alex the Rambler, Marbozir, Enter Elysium, Solar Gamer, Spiffing Brit, Drew Durnil, The Prussian Prince, Bratyn, Da9L, Shen Plays and Jay’s Gaming will all be streaming their point of view in their own channels, and we’ll be hosting a main observer stream at the Paradox Twitch channel: https://www.twitch.tv/paradoxinteractive

Next week we are going to try and do a big recap of all the features and changes and also talked about a bunch of small stuff that hasn't fit into diaries so far. See you then!


Rejected Titles:
Droogies Quest For The Missing Core
Hoi4 Dev diary: Reverse the polarity of the deflector dish
Graphs are hot!


At last... some true love to AI...

I'm honestly surprised that maths were not used up front for otpimising AI behavior. I can only hope you will rely more on them in a near future.
 
WOW! I did´t excepted any Dev diary and even not some thing like this! Amazing, "Performance and AI" is most needed
 
Would changing the defines values for the various garrison priorities change how the tool shaded the map? If so, this tool be pretty handy to modders tinkering with how the various garrison priorities worked. Would making this kind of thing to modders be feasible/appropriate, if it was easy to do? License granted to roll eyes at silly forumite asking for something that's inappropriate and hella hard because they're clueless :).

Yes you can, just trigger `track_unit_controller_weights` in the console for the country you want to track.
To enable the mapmode you write `debug_unit_controller_mapmode`

You can also specify different mapmodes, though right now there is only province_weight, state_weight and state_min_units or something like that don't remember exactly.
 
At last... some true love to AI...

I'm honestly surprised that maths were not used up front for otpimising AI behavior. I can only hope you will rely more on them in a near future.

I'd say that's exactly what they're doing, right? This sets up a system where the AI can respond to a variety of geopolitical situations with different approaches. They also laid the groundwork of province and state weighting for strategic deployment.
 
Would really like to hear about the fixes for more of the longstanding issues that have been mentioned.

for example:
suicide fleets.
suicide transports.
Suicide attacks.
unoptimized economic planning

Front shuffling and bad templates was mentioned.