• 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:
The depressing thing is that, exactly like some people predicted, there's apparently a contingent of people who are mad that the AI was made better, but not better enough for their tastes.

C'mon people. The new system is easier to make adjustments to, so if its a little basic NOW that's fine. I don't care if the AI just shoots for some overall quotas scaled to different phases of the game, that's HEAD AND SHOULDERS ABOVE what we have now. This system ALSO allows for future development (either by Paradox or modders) of one of the biggest things I want for the AI: different ethics/personalities to have visibly different building priorities.
 
While the English language was born in Europe and the European way of using it tends to be better, I will die on this hill:

The plural of "Math" is "Math".

No it's maths. Because maths is short for mathematics, which is a plural noun.

You're both right. Mathematics is a proper noun that is plural in form but singular in construction; however, it is always used with a singular verb and because of this, its plural form is least significant. You would never say "Mathematics are my favorite subject," you would instead say "Mathematics is my favorite subject."

Both Math and Maths are correct abbreviations of Mathematics, but the latter is used primarily in British English; although the short-hand Math is about 80 years older than Maths.

The depressing thing is that, exactly like some people predicted, there's apparently a contingent of people who are mad that the AI was made better, but not better enough for their tastes.

C'mon people. The new system is easier to make adjustments to, so if its a little basic NOW that's fine. I don't care if the AI just shoots for some overall quotas scaled to different phases of the game, that's HEAD AND SHOULDERS ABOVE what we have now. This system ALSO allows for future development (either by Paradox or modders) of one of the biggest things I want for the AI: different ethics/personalities to have visibly different building priorities.

Exactly. This update is a huge step in the right direction. The game can always be improved and so long as Stellaris remains in a development cycle it will continue to improve. I have no doubt that the AI will continue to improve, just like it has continued to improve since 1.0. So long as progress is being made, I am satisfied.
 
Last edited:
Just thoughts but it would be cool if I could see some of the military AI's decision making as a player ally of that ai. Like not the entire debug mode but maybe just a "merging this fleet to attack that system."

There's also no function to coordinate between the player and the AI (also the rally button just straight doesn't work lmao). Maybe have a button that tells the AI "hey I want to take this system" or "I think we should ignore this planet for now" to give more weight to the AI taking those actions, combined with just more detailed explanations of what the AI is doing like mentioned above so the player can plan around that?
 
Marvelous! I guess getting into the AI like this is mind-crushing work but I feel this will really pay off in terms of AI competitiveness, which was so far lacking most of the time. Can't wait to get my hands on this and potentially my ass kicked by some AI empire (maybe?)!
 
Thank you very much @MatRopert and @sidestep this was a very interesting read for me and I appreciate all the time you spend here answering question. I hope I am not too late for the party for a question:

Did the last DLCs change the policy on AI development in the stellaris dev team? I really think that the AI was even more of a weakpoint than the performance and I love all the work you put in it. I am hoping you are able to continue to make changes. You definetly got me hyped. I hope you guys will give us an update again when the next DLC comes along.
 
It seems like we now have a framework for the AI to expand on. The system seems easier for the devs to debug and to develop new strategies, and for modders to customize.
It does seem there are also a lot of improvements left on the table so I hope the suits allow the devs to implement at least a significant portion of those.
I am happy and this was a great dev diary to read.
 
Exactly. This update is a huge step in the right direction. The game can always be improved and so long as Stellaris remains in a development cycle it will continue to improve. I have no doubt that the AI will continue to improve, just like it has continued to improve since 1.0. So long as progress is being made, I am satisfied.
I want to elaborate on this bit. One thing we have seen repeatedly with many of the expansions is that AI lags behind because they need to "teach the AI" (read: fiddle with weights) all the new stuff. This system should make it much easier to adapt the AI to new things.
 
You're both right. Mathematics is a proper noun that is plural in form but singular in construction; however, it is always used with a singular verb and because of this, its plural form is least significant. You would never say "Mathematics are my favorite subject," you would instead say "Mathematics is my favorite subject."

Both Math and Maths are correct abbreviations of Mathematics, but the later is used primarily in British English; although the short-hand Math is about 80 years older than Maths.

I bow to the superior grammar n***. You're quite right sir. I was, however, being facetious, of course ;)

edit - Revenge! Latter, not later.
 
Right now there exists no such specific plans, BUT the system is setup so that you can do that very easily. You could for example make a general "high science" plan and weight it higher for technocracy empires. Or make a Technocracy specific plan that is limited to them only via the potential = {} trigger!
Im very exciting about all the good work being done and i look forward to playing the dlc.
But u are saying that empire core values where it is built upon dont matter?
I would like to a empire to behave on the principles it was founded.
That would benefit the emersion and roleplay
 
I bow to the superior grammar n***. You're quite right sir. I was, however, being facetious, of course ;)

As my signature says, "Their are know won who dont make grammar mistake."

Revenge! Latter, not later.

No! I have brought shame upon the Grammar Regime!
 
By making it much easier to see this information, we could fix a number of bugs where e.g. mining bots were not automatically favoured for mining jobs.
I'd like to know if the choice for assembling pops has been looked at. Last I heard, it just chose the highest trait value pop and built that. Seems like it could look at the jobs available and if a bot has a mining trait and there are mining jobs available it should build it once the current pop is done.
With organics there's a whole can of worms with species rights for growing specific pops so I'm going to leave that out and just talk about gestalt consciousness. My understanding is colonies typically grow the pop that's currently the least populous. I have no idea if the AI ever forces growth, taking the penalty. However, divided attention is one of the few things an AI could do better than a human. Seems like the AI could chose a pop suited for an available job after every pop growth on every planet and at least take that advantage over the player who couldn't monitor every planet that closely. Seems like even player controlled hive minds would grow pops for specific jobs by default as it's not that different than robots in terms of reproduction being done with purpose rather than by individual whim.
 
This was actually brought up as one possible improvement when I presented it to some other devs, would definitely be super-nice to have but is not supported right now.
In Operations Research (the science of optimisation) you get around the general issue you describe by using a value which is practically infinite. A cost which is prohibitively high, a value whichis excessively high.

E.g. if all other weights are 1, then 10,000 should do the trick.

In the practical gaming context, a target of 10,000 alloys would encourage the AI to produce as much as it possibly could.

Also have the developers thought about applying control theory to the AI
 
Is every AI "minister" behaving the same whatever their ethics, civics and personality?

It would be interesting to vary weighs based on those, that would provide variability. If you are militaristic a more offensive strategy, or a more foundry based economy could do.

I find ai too predictable. I know what a fleet will do, how will it move.. sometimes, just a more random approach will surprise the human, not just because is better or worst, but because the ai strategy is different. Even mixing stupid strategies and good scripted ones would add a surprise flavour.

For example, I know that ai will not position its fleets on the border before attack, so DOWs give you some time for preparations if their capital is far. I know ai doesn't care about civilian ships, so putting a construction/science ship in the side of a system gives you intel deep in uncontrolled territory. I know ai fleets get stuck on heavily fortified worlds, bombing it into submission. I know ai doesn't repair their fleets on captured stations..... i don't mean this is wrong. Maybe that is the optimal approach, but maybe 10% of the times, ai should do different things to surprise the human. Usually gamers are impressed when an ai does something unexpected and think the ai is being smart... when the thing is that it's just doing something not so very predictable. (Also wrong strategies are fine, an ai should be "smart" enough to make mistakes sometimes)
 
The main issue of the AI is its inability to destroy useless or harmful assets.
even with to old AI if I take control of one random country and destroy some no longer needed districts in 2240 it will defeat all it's neighbours 30 years later.