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

Stellaris Dev Diary #172 - Reworking the AI

Bonjour everyone, it’s the French Paradox speaking! For those who don’t know me, I’ve joined the Stellaris team this December after a year and a half as a programmer on Europa Universalis IV.

Today, we are gonna talk about AI.

pasted image 0.png

A good introduction for those new to the field

Fifty Shades of AI
There are several AI modules in Stellaris. For historical reasons we call them “ministers” as each one is supposed to handle a specific role in an AI empire.

There are 3 broad kinds:
  • The AI foreign minister handles diplomacy, federations, galactic community, peace deals and the like
  • The AI interior minister is in charge of the economy. He keeps budgets and order constructions, both civil and military.
  • The AI military minister is in command of all troops and military fleets, and also responsible for laying out strategic plans when at war.
For each of those ministries there are different “ministers” there are several options that can be selected for every empire in the game. All of those have generic one which behaves more or less like we’d expect a player to and is used for most AI empires. Then we have a bunch of specialized ones for special tags such as space monsters, fallen empires, crisis, marauders and the like.

As almost everything in our games, AI is configurable in script for our modders, although I’m not exactly sure what would happen if you assigned a space monster military AI to the caravaneers ;)

In guise of a welcoming gift when I joined the team, I was tasked with reworking the military one...

The Military AI
To give you a little bit of background, there were several generations of military AIs in Stellaris. The generic one (used by most “classic” empires) was redone by the great @sidestep last year, while the more specialized ones (crisis, space monsters) have kept close to what they were on release. In the midst of the sad and dark swedish winter, I managed to bring some improvements that I’ll showcase today.

First of all, I worked on visualization to help us debug how the AI “thinks”. Funny thing is, it already made it look “better” to audiences even if it didn’t actually change any behaviour. It’s actually something that’s been observed in video games: a good AI tells you what it does, which makes it look smarter. One of my favourite examples of that would be the enemies in FEAR.

So by typing 'debug_ai' in the console and observing an AI empire, you can see what it has in mind:

pasted image 0 (1).png

“I don't even see the code. All I see is blonde, brunette, redhead. Hey uh, you want a drink?”

As a simple analogy, imagine that the AI has a war minister that looks at the big picture and rates every potential target, a general staff who assign fleets to some of those objectives, and then admirals who try to lead those fleets on a tactical level to achieve those objectives.

The skulls on top of each system shows military objectives that the AI is considering (the war minister). Red ones are the ones they selected and committed some fleets to, while green ones are other options they haven’t retained for now. Finally for each individual fleet, in those task forces, you see what they are doing at present.

In our screenshot example, the AI decided that taking Tiralam was the most important objective with a score of 4500, and that they estimated that at least 11.2k fleet power was needed to accomplish this. They committed the Kilik Armada, the Jinki-Ki-Ti Armada and the the Grekki Armada to this. Since it makes little tactical sense to attack in a dispersed formation, the AI issued orders to regroup in Broon’s Singularity before proceeding on the attack (something we improved in this patch).

For convenience, the summary is also visible in the outliner:

pasted image 0 (2).png

As seen from the other side of that war

That change alone allowed us to see where the AI was a bit weak and also made evident a few bugs in the production AI that we promptly fixed. A funny one was that in some cases a fleet would end up assigned to two different fleet groups, nicely simulating two admirals fighting over command of a fleet and issuing contradictory orders every day.

Crisis AI
The next step was to rewrite the various crisis to use the generic AI, so that any effort spent on making better would benefit all. In patch 2.6 the specific AI of the Khan, the Prethoryn, the Unbidden and the Contingency will use the same AI as the “standard” empires, with a few twists to still retain their personality.

Without spoiling every secret, here’s a few ideas:
  • The Khan doesn’t really believe in defense and will try to beat the closest systems into submission
  • The Prethoryn will swarm in every direction they can
  • The Contingency will systematically try to stop the biggest threat to the galaxy, until nothing remains
  • The Unbidden will be harder to predict, but there’s reason behind their alien way of acting.
One of the biggest challenges we faced was assigning fleets to objectives. Matching X fleets with Y out of Z objectives is not an easy task. Do we try to accomplish as many objectives as we can at the risk of spreading too thin or accomplishing nothing of value? Should we instead focus on the most valuable target and possibly end up in a big fight that we could have avoided? How often should we reconsider our options?

The current version solves this by putting a fleet power value on every target, then grabbing fleets by order of priority until it either has enough to accomplish the objective, or go over the next one. This approach showed its limits when we plugged the crisis AI into it, as it relies a lot on the size of available fleets (it doesn’t know how to split them, it can only merge them).

Teaching the AI how to split fleets proved quite interesting:

pasted image 0 (3).png

What shall we do with this knowledge?

It took several tries to find a good balance, as the AI tended to split too much (most objectives don’t call for that much fleet power, unless you’re fighting your enemy main fleet). In the end, after trying some complex strategies such as keeping statistics on accomplished objectives and deriving a good target number from that, a simpler approach turned out more efficient: put all the nation’s offensive fleet power into one stack, and then consider splitting in 2,3 or more depending on how confident the AI feels about its military power versus its foes.

Knowing some of you like to mod our AI, here’s some new defines you may want to play with once all that hits the shelves.

Code:
# Objective values
HORDE_INVASION_PLANNING_DEPTH = 5    # How far out does the Horde AI looks for invasion targets (in system hops)
SWARM_INVASION_PLANNING_DEPTH = 5    # How far out does the Swarm AI looks for invasion targets (in system hops)
SWARM_POP_TARGET_MULT = 1.0            # Extra target scoring for swarm (multiplied by number of edible pop on the planet)
CONTINGENCY_MEGASTRUCTURE_EXTRA_VALUE = 4    # How attractive are megastructures to the Contingency (added to the base value of 1)
UNBIDDEN_PORTAL_EXTRA_VALUE = 20            # How much does the Unbidden want to defend their portal (compared to base value of 1)
UNBIDDEN_BYPASSES_EXTRA_VALUE = 4            # How attractive are bypasses to the Unbidden (added to the base value of 1)
UNBIDDEN_RIVALS_EXTRA_VALUE = 10            # Extra target scoring for rival invaders (Aberrant and Vehement)
UNBIDDEN_TARGET_EXTRA_VALUE = 10            # Extra target scoring for randomly chosen nemesis
UNBIDDEN_PSIONIC_CONQUER_DESIRE = 20        # Extra weight added to psionic empires when rolling a nemesis (base 1 + number of owned bypasses)
UNBIDDEN_CHOSEN_ONE_CONQUER_DESIRE = 50        # Extra weight added to empire lead by the chosen one when rolling a nemesis (base 1 + number of owned bypasses)

# Fleet sizing
OFFENSE_VS_DEFENSE_STRATEGY_ALLOTMENT = 0.75 # How much of its fleet power should a country with 1.0 aggressiveness should try to commit to offensive missions
AVERAGE_FLEET_SIZE_FACTOR    = 0.05            # Ballpark estimate of the minimum size a fleet should be in relation to total fleet power
OWN_FLEET_POWER_FACTOR = 1.0                # How much does AI count its own fleet power when evaluating forces
ALLY_FLEET_POWER_FACTOR = 0.5                # How much does AI count ally fleet power when evaluating forces
ENEMY_FLEET_POWER_FACTOR = 1.0                # How much does AI count enemy fleet power when evaluating forces
FLEET_SUPERIORITY_FACTOR = 1.5                # How stronger should the AI be before it starts considering splitting fleets (fleet count = relative strength / this factor)
CRISIS_FLEET_SUPERIORITY_FACTOR = 1.0        # Same as previous but will be compared to the strongest foe in the universe

Most of those changes will be delivered in the patch coming alongside the Federations release (2.6.0), but not all. As you may imagine, changes to the military AI are quite impactful and we don’t want to release the changes without enough testing, so some of them will be delivered in the first support patch (2.6.1).

And with that, I shall leave you with @sidestep one last time.
 
Last edited by a moderator:
  • 2Like
Reactions:
Well, I was going to ask about sectors, but someone's already done that, so I'll just say that this certainly looks promising. Obviously, we'll have to see how it plays out to be sure, but this certainly has me a lot more optimistic about the Patch/Expansion than I was a few weeks ago
 
Yes! An AI focused Dev Diary!

Does the AI still build a vast array of Habitats that it doesn't colonise?

Does the AI know how to demolish stuff, e.g. to build city districts for a Ecumenopolis?

Is this Goal Oriented Action Planning?
 
Thanks! Do you think about the possibility of introducing a peacekeeping contingent under the auspices of the galactic community and collective projects such as the galactic olympics and so on?
 
Last edited:
This looks very good so far!

You mention 3 types of AI ministers, but only outlined 2. Will the foreign minister AI get its own dev diary, outlining the diplomatic changes between pre-2.6 and post-2.6?
 
You said the economic performance is noticeably better. Is this evident at the rate at which other empires become inferior to you? As it is you don't need even need to look at AI planets to see how bad it is doing. In fact it can be better to spare oneself that horror. But once you make it out of the early game, you just need to look at the diplomacy screen to see that everyone else is soon "inferior" and "pathetic". Even without trying much. Sure, a good human player will always outperform the AI in the long run, but what I expect to see here is that it takes longer and that the AI sometimes can be a serious opponent at least into the mid game. Things like wars, federations and galactic communities are pointless when you are effortlessly dominating everyone.
 
Last edited:
I'm very curious how much of a performance increase we can see from cutting down on the if/then statements that the AI uses for its decision making. We had the dev diary speaking about performance improvements not long ago; was that before or after this new AI was implemented?

Also hopefully the AI will be 'smart' enough not to put down multiple police stations on their planets. Playing crime syndicates was very underwhelming with that behaviour.
 
This is the dev diary I've been waiting for for a long time now. Thank you very much. At a first glance it looks like there have been some huge improvements on the economic side I am very much looking forward to see in action. It seems that a lot of work has gone into the AI plans and I have no doubt that they are a sound basis to develop actually working AI empires.

However I can already see a couple problems coming:

1. What happens to a lithoid empire if it aquires a non-lithoid minority? Do the normal pops starve because the AI remains in a non-food-producing plan? Or does the AI switch to a plan that does produce a food surplus but is completely inadequate to produce the mineral surplus the lithoid population requires? Similar problems I can see for synthetically ascended empires while assimilating new pops, normal empires conquering lithoid planets and any machine empire with organic pops -pampered, enslaved or otherwise- .

2. I hate that there still are no planetary plans. Sure the AI can see a need for an alloy foundry and order one one the planet most suited, but the human player is still stuck with dozens of planets that the AI can't be trusted to take care of when we have no ability to assign an AI-plan specifically to a single world.

3. How well does the AI handle big numbers of pops switching jobs? If it upgrades an Alloy foundry and then all the former mining workers flock to the new improved jobs there is a sudden demand in minerals and CGs as well as a sudden drop in mineral production.

4.Does the AI take buildings under construction into account? Otherwise a mineral deficit will cause minign districts to be ordered en masse until the first ones are actually finished.
 
I'm excited to see the military AI changes in action. Modders have done a great job patching over the economic AI, but as I understand it there's not much they could do with the military AI.

The military AI changes made last year cleared up some very bad AI behaviour (thanks @sidestep). But it's great to see the work continuing with this depth. A competent military AI (along with the other AIs) will revitalize Stellaris. I hope that performance and AI remain a high priority going forwards.

While my 'no pre-order' rule holds, these diaries have left me very tempted to buy on release day.
 
Great stuff, the potential to simplify stuff is tremendous.

Will the plans tie in with specific Authority Types, i.e, Imperial plans differ from Authoritarian and Egalitarian ones? Or even at the Government Type level, i.e, Representative Democracy plans differ from Communal Parity ones?
 
Interesting, and a good idea to give them distinct personalities, but is there anything about fixing the 'crisis is very slow and gets slower as their territory grows' issue? I had the Unbidden spawn and, despite 50 years of being unchallenged, they took 60 systems out of 1000!
I have a couple questions regarding this. Lets start with the crisis. One of its biggest issues that prevents the crisis from ever conquering the galaxy was apparently the fact that they expand slower and slower, because most of their time is spend sending construction ships all across their territory. Did you adress this? How long does it take for each Crisis to full conquer, lets say a medium galaxy now? If the Crisis can't conquer territory, then at worst you end up in a stalemate of both the crisis and the player throwing ships at each other.
As I mentioned the new military AI will be a continuous focus for this patch and probably the next one. So far we focused mostly on the local scale, and now we plan to sit back and try to gather some data to see how it goes in the long run, and adjust accordingly. I have lots of ideas on how to improve it further, but we felt it'd be better to let things crystallize a bit before changing it even more.

So if I'm reading this right psionic ascension puts a big bullseye on your forehead when facing the interdimensional invaders. This is cool narratively, but makes the already weakest ascension path a tad worse than it already was.
Being a psionic empire gives you a higher chance to be selected as a target, but it's not guaranteed. While the Contigency is quite deterministic in the way it plays (it's a machine, after all ;)), the Unbidden are a bit more random.

I would assume bypasses means wormholes and such?
Yes, gates, l-gates and wormholes.

1. Can the military AI target enemy fleets or just systems? As a player my highest priority is enemy fleets as after they have been neutralized I can focus on other objectives.
They have 3 target categories: fleets, systems and planets. Each as its owned scoring system and they pick what they think is more important.

As someone who plays both Stellaris and Imperator:Rome, this is the best Dev Diary I have ever read. It has enough "meat". That's what I expect: How to do something in the technical perspectives and why we need it done.
Thank you, I don't write DD often (last one was on EU4 tech debt) so it's nice to see you guys like it.
 
The AI has needed a lot of attention (like performance) and this dev diary shows a lot of promise for the future, and is one of the most exciting bits of news for 2.6.

Excellent dev diary!!
 
You mention 3 types of AI ministers, but only outlined 2. Will the foreign minister AI get its own dev diary, outlining the diplomatic changes between pre-2.6 and post-2.6?
The Foreign Minister was not in the scope of this DD (or patch). It got some additions to be able to handle Galactic Community and the other new diplomatic features of course, but no major changes.
Is anyone else unable to see the pictures ?
Yes, I foolishly copy-pasted from google drive without rehosting the pictures on Plaza. We're fixing it :oops:
 
Last edited: