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

pnt

Colonel
54 Badges
May 23, 2008
1.134
246
  • Semper Fi
  • Europa Universalis IV: Art of War
  • Europa Universalis IV: Conquest of Paradise
  • Europa Universalis IV: Wealth of Nations
  • For the Motherland
  • Hearts of Iron III
  • Heir to the Throne
  • Europa Universalis III Complete
  • Europa Universalis III Complete
  • Europa Universalis IV: Res Publica
  • Europa Universalis IV: El Dorado
  • Hearts of Iron IV Sign-up
  • Achtung Panzer
  • Crusader Kings II: Horse Lords
  • Europa Universalis IV: Common Sense
  • Crusader Kings II: Way of Life
  • Mount & Blade: With Fire and Sword
  • Victoria 2
  • Hearts of Iron IV: Cadet
  • Hearts of Iron IV: Colonel
  • Crusader Kings II: Reapers Due
  • Hearts of Iron IV: Together for Victory
  • Crusader Kings II: Monks and Mystics
  • Hearts of Iron IV: Death or Dishonor
  • Hearts of Iron IV: Expansion Pass
  • Hearts of Iron IV: Expansion Pass
  • Crusader Kings II: Holy Fury
  • Imperator: Rome
  • Hearts of Iron IV: Expansion Pass
  • Hearts of Iron IV: La Resistance
  • Imperator: Rome - Magna Graecia
  • Battle for Bosporus
  • Europa Universalis IV
  • 500k Club
  • Europa Universalis: Rome
  • Europa Universalis III Complete
  • Hearts of Iron Anthology
  • Crusader Kings II
  • Crusader Kings II: Charlemagne
  • Crusader Kings II: Legacy of Rome
  • Crusader Kings II: The Old Gods
  • Crusader Kings II: Rajas of India
  • Crusader Kings II: The Republic
  • Crusader Kings II: Sons of Abraham
  • Crusader Kings II: Sunset Invasion
  • Crusader Kings II: Sword of Islam
  • Europa Universalis III
  • Darkest Hour
  • Europa Universalis IV: Call to arms event
  • Victoria 2: A House Divided
Just to clarify, I don't know what choice the AI opponents use (not sure how to see that as a player), but it continues large-scale attacks even when the equipment (brown bar) is depleted and there is none in storage. In contrast to morale and supplies, this cannot be addressed at the combat level as it requires production of new hardware. That is why I suggested that it should be a global parameter that the AI could "see." Lowering the aggression overall against human players, or making it a personality trait, could be a good idea - but it would not address the specific issue mentioned above.
 

billcorr

Field Marshal
52 Badges
Feb 5, 2010
8.383
2.354
  • Cities in Motion
  • Stellaris: Apocalypse
  • Age of Wonders: Shadow Magic
  • Stellaris - Path to Destruction bundle
  • Victoria 2: A House Divided
  • Sword of the Stars
  • Ship Simulator Extremes
  • Semper Fi
  • Rome Gold
  • Victoria: Revolutions
  • Majesty 2 Collection
  • Majesty 2
  • Magicka
  • The Kings Crusade
  • Europa Universalis III
  • Darkest Hour
  • Crusader Kings II
  • Arsenal of Democracy
  • Divine Wind
  • Leviathan: Warships
  • Europa Universalis IV: Conquest of Paradise
  • For The Glory
  • For the Motherland
  • Hearts of Iron III
  • Hearts of Iron III: Their Finest Hour
  • Hearts of Iron III Collection
  • Heir to the Throne
  • Stellaris: Federations
  • Battle for Bosporus
  • Hearts of Iron IV: La Resistance
  • Stellaris: Ancient Relics
  • Hearts of Iron IV: No Step Back
  • Hearts of Iron IV: Expansion Pass
  • Age of Wonders III
  • Stellaris: Synthetic Dawn
  • Hearts of Iron IV: Death or Dishonor
  • Knights of Honor
  • Hearts of Iron IV: Together for Victory
  • Stellaris: Leviathans Story Pack
  • Hearts of Iron IV: Cadet
  • Hearts of Iron IV Sign-up
  • Stellaris
  • Pillars of Eternity
  • Pride of Nations
  • Europa Universalis IV: El Dorado
  • 500k Club
  • Victoria 2
  • Europa Universalis IV
  • Stellaris: Megacorp
  • Rome: Vae Victis
That is why I suggested that it should be a global parameter that the AI could "see."

This metric of "seeing" the amount of equipment nations have could be useful in other ways, too.

The advantage of perceiving the amount of equipment for an A.I. nation include:
  • As stated by @pnt and others, the amount of equipment would be a factor for the A.I. to consider. Before attacking and while attacking, the A.I. keeps track of its equipment inventories.
    • Not enough equipment? Hold off on the attack.
    • Enough equipment? Attack!
  • Sharing the equipment inventory information would help both human players and A.I. players know how much Lend Lease to provide to both human and A.I. players.
  • Knowing the equipment inventory information may help the A.I. determine its trade policy.
  • Knowing the equipment inventory information may help the A.I. determine its military factory allocation.
  • Disadvantage: the above features demand more calculations by the A.I. and theoretically would cause more lag. :(
What other advantages or disadvantages can you think of regarding the A.I. keeping track of its equipment inventory information?
 

chris219219

Recruit
6 Badges
Feb 21, 2018
6
0
  • Crusader Kings II
  • Cities: Skylines
  • Hearts of Iron IV: Cadet
  • Hearts of Iron IV: Together for Victory
  • Hearts of Iron IV: Death or Dishonor
  • Hearts of Iron IV: Expansion Pass
All these changes are AMAZING, especially this one, but the other ones may complicate the game a little more than it should, don't you think? They're really useful, sure, but they're a little TOO hard to manage. I'm all for the division namelists, infrastructure and resources, acclimation and army groups/leader traits, but the whole stability and war support thing is a bit too much to handle for a game like this since you already have a lot to do during wartime. Just my opinion though, it won't drive me from the game, it will just make the game slower and I won't be able to play as much.
 

stjern

Major
11 Badges
Mar 28, 2016
758
203
  • Hearts of Iron III
  • Hearts of Iron IV: Cadet
  • Hearts of Iron IV: Colonel
  • Hearts of Iron IV: Field Marshal
  • Hearts of Iron IV: Death or Dishonor
  • Hearts of Iron IV: Expansion Pass
  • Hearts of Iron IV: Expansion Pass
  • Hearts of Iron IV: La Resistance
  • Battle for Bosporus
  • Hearts of Iron IV: By Blood Alone
  • Hearts of Iron IV: No Step Back
Fortunately, "seeing combat" is not really needed in this case. The AI only needs to see the amount of equipment in storage (perhaps comparing it to the average losses and production numbers, the net for which is calculated anyway), and then set a flag about how offensive it should be. The levels for how aggressively plans should be pursued are already pre-set for the player when using the AI, so I assume the AI uses a similar system. The "equipment inventory level," and perhaps remaining manpower, could simply be one of the factors for determining this.

This is exacly the problem, in fact this breaks the AI! It can work in some specific cases, but it will fail fataly. What if the terrain modifier is -100%? What if the ai has a full storage of very bad equipment and very bad templates?

We all know what happens:

Endless suicide attacks.

And FYI it doesn't work at all as you suggested, it uses average strength, average org levels and plan score for an army to determine wether to activate the plan or not. But the result and underlying wrong assumption is the same.
 

billcorr

Field Marshal
52 Badges
Feb 5, 2010
8.383
2.354
  • Cities in Motion
  • Stellaris: Apocalypse
  • Age of Wonders: Shadow Magic
  • Stellaris - Path to Destruction bundle
  • Victoria 2: A House Divided
  • Sword of the Stars
  • Ship Simulator Extremes
  • Semper Fi
  • Rome Gold
  • Victoria: Revolutions
  • Majesty 2 Collection
  • Majesty 2
  • Magicka
  • The Kings Crusade
  • Europa Universalis III
  • Darkest Hour
  • Crusader Kings II
  • Arsenal of Democracy
  • Divine Wind
  • Leviathan: Warships
  • Europa Universalis IV: Conquest of Paradise
  • For The Glory
  • For the Motherland
  • Hearts of Iron III
  • Hearts of Iron III: Their Finest Hour
  • Hearts of Iron III Collection
  • Heir to the Throne
  • Stellaris: Federations
  • Battle for Bosporus
  • Hearts of Iron IV: La Resistance
  • Stellaris: Ancient Relics
  • Hearts of Iron IV: No Step Back
  • Hearts of Iron IV: Expansion Pass
  • Age of Wonders III
  • Stellaris: Synthetic Dawn
  • Hearts of Iron IV: Death or Dishonor
  • Knights of Honor
  • Hearts of Iron IV: Together for Victory
  • Stellaris: Leviathans Story Pack
  • Hearts of Iron IV: Cadet
  • Hearts of Iron IV Sign-up
  • Stellaris
  • Pillars of Eternity
  • Pride of Nations
  • Europa Universalis IV: El Dorado
  • 500k Club
  • Victoria 2
  • Europa Universalis IV
  • Stellaris: Megacorp
  • Rome: Vae Victis
And FYI it doesn't work at all as you suggested, it uses average strength, average org levels and plan score for an army to determine wether to activate the plan or not. But the result and underlying wrong assumption is the same.

Assuming that the A.I. "uses average strength, average org levels" to determine attacks, perhaps that is something to focus on.
  • Instead of using average scores, perhaps the A.I. could be taught to "see" where its enemy is strong and where the enemy is weak.
  • Based on the A.I. understanding of the enemy's disposition, then mass combat power at a weak point.
  • This would allow the A.I. to then defeat its enemy in detail.
(But I'm not convinced that the A.I. "uses average strength, average org levels" to determine attacks...it makes sense, but I've not seen anything to support that statement. On the other hand, I've not seen anything to negate that statement. Kind of like professing, "Four angels can dance on the head of a pin." :) )
 

American Traveler

Second Lieutenant
21 Badges
Apr 4, 2009
137
66
  • Hearts of Iron III: Their Finest Hour
  • Europa Universalis IV: Res Publica
  • Hearts of Iron III
  • Europa Universalis IV: Call to arms event
  • Europa Universalis IV: Wealth of Nations
  • Europa Universalis IV: Conquest of Paradise
  • Hearts of Iron IV: Cadet
  • Hearts of Iron IV: No Step Back
  • Hearts of Iron IV: By Blood Alone
  • Battle for Bosporus
  • Crusader Kings III
  • Hearts of Iron IV: La Resistance
  • Hearts of Iron IV: Expansion Pass
  • Hearts of Iron IV: Expansion Pass
  • Hearts of Iron IV: Death or Dishonor
  • Hearts of Iron IV: Together for Victory
  • Europa Universalis IV: Rights of Man
  • Europa Universalis IV
  • Europa Universalis IV: Mare Nostrum
  • 500k Club
  • Europa Universalis IV: Art of War
Assuming that the A.I. "uses average strength, average org levels" to determine attacks, perhaps that is something to focus on.
  • Instead of using average scores, perhaps the A.I. could be taught to "see" where its enemy is strong and where the enemy is weak.
  • Based on the A.I. understanding of the enemy's disposition, then mass combat power at a weak point.
  • This would allow the A.I. to then defeat its enemy in detail.
(But I'm not convinced that the A.I. "uses average strength, average org levels" to determine attacks...it makes sense, but I've not seen anything to support that statement. On the other hand, I've not seen anything to negate that statement. Kind of like professing, "Four angels can dance on the head of a pin." :) )

I have loaded up games and taken a look at AI germany and italy to try to get a sense for what they are doing. I have taken actions to manually halt their suicide offensives, let the game play 1 hour, then loaded back up my country. The AI immediately resumes the suicide offensives. I haven't manually broken the AI army groups into smaller groups and completed testing. Maybe someone can share if they have. This would help answer whether or not there is currently a formulae.
 

fredinno

First Lieutenant
3 Badges
May 21, 2017
222
0
  • Hearts of Iron IV: Cadet
  • Crusader Kings II
  • Europa Universalis IV
Why is the performance worse now that Waking the Tiger is out? I don't understand. Please explain what might be going on?