• 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:
You mentioned that The Plan does not take difficulty into account. Wouldn't it then make sense to scrap the concept of free resources at higher difficulty alltogether in favour of just lowering prices? Functionally there should be no difference in just giving the AI alloys and making sure it can buy more battleships for the same price. That way the AI following the plan would make sure that the economy could always support itself. It would also scale better into the lategame.
That sounds like a nice workaround. I wonder what the answer to this could be.
 
All right, just pre-ordered the next expansion due entirely to this plus the performance improvement diary. Please keep working on the rest of the AI and the various crises. I may not be typical, but I suspect it'll move a fair bit of sales.
 
in the last video ( on the shoulder of giant) the AI early game seems to just send small fleets around ... is it still an old version of the AI ? or is because they are still too early and have some other problems ?
 
I have a few interesting things to say to you guys ... I always play Stellaris in grand admiral difficulty with advanced start chosen for all AI s. I have seen weird stuff. If I know correctly AI will get +%100 resource production for all of its jobs for that difficulty. However, I believe making them advanced start with this difficulty get them stupider.

In-game, I see worthless planets with almost no districts, meaningless level 1 unity production buildings on planets with 10 pops in the late 2300s or 2400s. They build habitats and then decide to not colonize it. I have seen systems with 7-8 habitats and none of them are colonized. They are not building robots or clone vats. ( I know they have personality things but if they won't build robots, they should go biological ascension or psionic )

So, even when I play with these huge bonuses for them, they do not have level 3 buildings in their capitals at the late 2300s... ( they are better at tech than me: they have no way not having the required tech ) Also, they seem to have troubling time managing special resources ( exotic gases, volatile motes ...etc ) Sure, they have huge production even they have level 2 buildings in their planets but this is only because they have huge production bonuses. If I would play normal difficulty they would perform poorly which is why I am playing like that.

There is a lot to say but I think they should use the galactic market more for the special resources, even go deficit on exotic gases for level 3 tech buildings and buy them monthly. Because I always see alloys at its max price but special resources are always untouched which makes my life easier but not satisfying.
 
Last edited:
@Dev is there going to be some late game performance optimization? All the new features is really nice but in the late game multiplayer the game becomes practically unplayable because it runs so slow and laggy due to all the stuff going on really affecting the quality of the game , Please if you can seriously work on late game performance that would be a lot more valuable for those of us who play multiplayer but end up not really playing due to the late game performance issues.
 
@Dev is there going to be some late game performance optimization? All the new features is really nice but in the late game multiplayer the game becomes practically unplayable because it runs so slow and laggy due to all the stuff going on really affecting the quality of the game , Please if you can seriously work on late game performance that would be a lot more valuable for those of us who play multiplayer but end up not really playing due to the late game performance issues.
They addressed this last Dev Diary IIRC.
 
I have a few interesting things to say to you guys ... I always play Stellaris in grand admiral difficulty with advanced start chosen for all AI s. I have seen weird stuff. If I know correctly AI will get +%100 resource production for all of its jobs for that difficulty. However, I believe making them advanced start with this difficulty makes them stupider. I see worthless planets with almost no districts, meaningless level 1 unity production buildings on planets with 10 pops in the late 2300s or 2400s. They build habitats and then decide to not colonize it. I have seen systems with 7-8 habitats and none of them are colonized. They are not building robots or clone vats. (I know they have personality things but if they won't build robots, they should go biological ascension or psionic) Even when I play with these huge bonuses for them, they do not have level 3 buildings in their capitals at the late 2300s... (even they are better at tech than me: they have no way not having the required tech) They seem to have troubling time managing special resources (exotic gases, volatile motes ...etc) Sure they have huge production even they have maximum level 2 buildings in their planets but this is only because they have huge production bonuses. If I would play normal difficulty they would perform poorly which is why I am playing like that. There is a lot to say but I think they should use the galactic market more for the special resources, even go deficit on exotic gases for level 3 tech buildings and buy them monthly. Because I always see alloys at its max price but special resources always are untouched which makes my life easier but not satisfying.
Paragraphs, they're your friend.
 
The things @Paladinn mentioned are very valid. The current ai doesnt upgrade buildings, is not using build habitats (and only builds energy ones) and is not able to develop planets further. The current ai is "okay" in the early game but falls behind in the midgame because it doesnt upgrade buidlings. Planets with more tahn 100 working pops are a rarity. Unemployment is tanking their whole ecomony while the endgame upgrades arent used. This stacks up so hard that the ai is absolutely pathetic after 150 years, even on grand admiral.

I am really interested in the new ai and how it performs. I will start an observer game and watch how it is doing.

I am always stunned when i go back to 2.1 and see the ai being "equivalent" to me all the game because it was able to build up working economies and planets... on commodore. Not even tested A or GA.
 
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!
Good and bad at the same time, IMHO,

Good because of the moddability (does such a word even exist? :D )

Bad because:
- not everyone of us is good at modding, even if it's a good thing that PDX games are so modding-friendly
- and most of all, because if I myself mod the AI's behaviours, where are the surprise, the sense of discovery, the sense of wonder in encountering a new empire that I myself shaped? This sense of discovery is what I play, and pay, the game for

And since we're on it, I'd really love to have more depth in first contacts, that's a fundamental trope of SF, in Stellaris it's quite standard, no matters who you meet.

Anyway, don't get me wrong, great DD that issues a topic I'm very fond of, keep up the good work!
 
I have a question.

In multiple games I have been observing that AI being left alone ramp up the price for the alloys on the market as high as 12 or even 15-16 credits over prolonged periods of time, which indicates that they are, en masse, selling off stockpiles of whatever they have produced to buy more alloys, regardless of price. That effectively means that even at max difficulty they just sacrifice all bonuses, buying more stuff at even higher price.

Naturally, having a stronger economy overall would help the AI immensely, but as long as they just end up dumping all profits into buying alloys at ridiculous prices, the efficiency would still suffer after a certain point.

Are there plans to make AI behaviour more intelligent as far as buying and selling resources is concerned? Something as simple as making "current market price" or more like "average price over the last 10/20/50 years" to be an additional multiplier in the AI considerations could help massively. It would be less inclined to produce, say, goods if they always somehow end up being sold at half price (since most of the galaxy does the same thing), and they would prioritize alloys even more at times when it is possible to fetch a good price on those.
 
What? No, they certainly upgrade building, I still remember my fustration having to tear down a lot of upgraded civilian factory from conquered planets.

They do, but mostly only to tier2. When the player can build the tier3 upgrades the game reaches the point where the current ai falls apart imo. In my last 2.5.1 iron man play it looked like the ai stopped building their planets completely. It sometimes feels like taht it is incapable of using the tactical resources and only build to the point when the natural ones are used.... where we players start building atificial plants to produce them.

I really hope that this is fixed with the new one, together with habitat spam (which would be fine when used to exploit more resources and growth, not only energy). In my last observed ai game i saw other stuff why the vanilla ai falls behind so fast. It it closing specialist jopbs from the beginning, opens them back but the pops dont join in them, from the very beginning. This repeats all game long. Unemployment, no housing... you see a complete inability to manage the economy, empire and planetwide. How it it possible that an ai has 8k fleetpower with 9 "colonized" planets in the year 2450? (I had 600k fleetpower at that time and wasnt even maxing, i just fought fallen empires...)
 
I saw a few comments from the dev about planet specialisation and I don't see why the AI can't kick in a specialising AI minister that work along side the normal AI that replace districts/buildings on planets to optimise the production of resources... it should not be hard for the AI if they have enough resources to replace one mining with energy on one planet and an energy with a mining on another to gain a net gain on both resources... or even do it on three planets and replace it on all of them. The same for buildings...

It would be an AI that are allowed to use resources at certain levels of resource abundance.
 
@sidestep I wonder if we might be able to get a little more access to attitudes for modding? Right now we can mod the behaviors for each attitude, but we have little control over when an empire adopts an attitude. It would add a lot of modding value for 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.

View attachment 550747
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:

View attachment 550749
“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:

View attachment 550750
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:

View attachment 550748
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.
 
@sidestep These improvements sound great! Very thoughtful!

I'm wondering how the AI responds once it reaches its goals? Seems like this is an important consideration since it obviously wouldn't be good for empires to stagnate just because they reached some number. Is the solution just to make the goals unreasonably high for each game period, such as the ideal composition for late game being based on when you've conquered the whole galaxy?

What is an economy AI and where do I buy one?
Hey all spacefarers, Zack aka. sidestep here. While The French Paradox has been hard at work on the military side of the AI I’ve been occupied with the AI economy, which is important as we all know that credits make the worlds go round.

View attachment 550754

So what is an economy AI and how does it work in Stellaris? Well, that could be an entire dev diary in itself and differs from game to game. But in short an economy AI decides when and how the AI should spend its resources as well as how it should work to get more of those resources. In Stellaris specifically the AI handles things such as trade, mining bases, starbase modules, policies, edicts, job favoriting and more. But perhaps most importantly the economy AI handles what is constructed on your planets!

Construction is at the core of the Stellaris economy, buildings and districts fuel your economy and help grow more pops, which in turn means more resources. Previously the economy AI did construction on a planet-by-planet basis and with no long term goal in mind. At regular intervals it would go over all planets it owned and through scripted weights it would find something “good” to build, queue it, and then move on and do the same for the next planet and so on. There exists some fundamental problems with doing construction this way however.

The Problems
Firstly it’s very script intensive and requires lots of script maintenance and balancing. If you change how a building works or basically anything in-game which changes the economic balance then you would end up having to rewrite a lot of script. “Oh, if we change this then we would need more alloys but if I increase the weighting for foundries then that would mean that this other building was not built often enough and then if we change that then…”, you see the issue?

What we ended up with was huge, complex, and very explicit and static scripts for each building with a lot of hard to follow ( and expensive to compute ) if/else statements with different weights that nobody could keep track of in the end. Below is an example AI weight script for a single building.

Code:
ai_weight = {
        weight = 0
        modifier = {
            weight = 10
            OR = {
                planet_crime < 15
                has_building = building_hall_judgment
            }
            is_capital = no
            free_amenities > 2
            owner = {
                has_monthly_income = {
                    resource = minerals
                    value > 100
                }
                has_monthly_income = {
                    resource = alloys
                    value < 100
                }
                has_monthly_income = {
                    resource = alloys
                    value > 6
                }#Check for alloy shutdown
            }
        }
        modifier = {
            weight = 200
            planet = {
                is_capital = yes
                NOR = {
                    has_building = building_foundry_2
                    has_building = building_foundry_3
                }
                num_buildings = { type = building_foundry_1 value < 3 }
                has_building_construction = no
            }
        }
        modifier = {
            weight = 400
            OR = {
                AND = {
                    owner = {
                        OR = {
                            has_monthly_income = {
                                resource = consumer_goods
                                value > 0
                            }
                            country_uses_consumer_goods = no
                        }
                    }
                    NOR = {
                        num_buildings = { type = building_foundry_1 value > 1 }
                        has_building = building_foundry_2
                        has_building = building_foundry_3
                    }
                    owner = {
                        has_ai_personality_behaviour = conqueror
                        OR = {
                            has_ai_personality_behaviour = opportunist
                            has_ai_personality_behaviour = purger
                            has_ai_personality_behaviour = propagator
                        }
                    }#Agressive empires but not robot liberators as they tend to be erudite explorers and the like.
                }#Give agressive empires 2 alloy factories on planets early on.
                NOR = {
                    has_building = building_foundry_1
                    has_building = building_foundry_2
                    has_building = building_foundry_3
                }
            }
        }
        modifier = {
            factor = 0.5
            free_jobs > 3
        }
        modifier = {
            weight = 500
            has_building = building_foundry_1
            free_building_slots = 0
        }#Repairs
    }

How does weight 200, 400 and 500 compare to other weights of buildings in other files? I have no idea.

Another problem is that an AI working this way is extremely reactive, and not very proactive. It has low income of something which leads to a script evaluating a certain building very high and it then builds that, that’s fine. But what if you’re doing quite alright? How do you then determine what to build? You would have to add even more script that says something along the line of “if you’re doing okay in all these resources then this should be built, but don’t value it too high because maybe we are in need of some auxiliary resource, hmmmm…”, you can see how this would be hard to script and would easily conflict with other script. It’s also a problem because this would all have to be manually maintained and updated as well, in each script for each building.

Yet another issue is that by doing this on a planet-by-planet basis in script we have no real idea of what has been built on any other planet or for what reason. This means that if we on one planet evaluate that an energy producing district is needed because we are running low on energy and then queue that, we will most likely queue the exact same district on the next planet we check since the situation looks exactly the same to the evaluating script even though when construction is done of the first district we might be fine and the second district would then just add to an unnecessary surplus. This might sound like a small issue but imagine if this happened on 12 different planets, it could swing an AI economy completely in the wrong direction and have massive long term consequences. The AI could even end up having to destroy some of those districts to make room for something else, making the entire effort a wasted one.

There were other smaller issues as well, but these were the main ones I wanted to address:
  • Less complex and hard to maintain script
  • More proactive AI planning
  • No isolated planet-by-planet construction
  • Overall better economic AI performance
The Solution aka. The Plan™
The wonderful Stellaris QA had put together a list of economic metrics that they thought the AI should be able to achieve at a certain point in time. They had a set for the early game and one for the late game. I started designing and planning how I would do this rework and came to the conclusion that the AI needed to actually plan long term, and not just react to whatever happened to them economically. This tied nicely into these performance metrics that QA had compiled so I just thought “hey, why not make these actual goals in the game!?”, and so the AI Economic Plan was born. The economic plan is at the core of the new economic AI, from the plan the AI derives exactly what it needs to reach the plan goals and decides how to do it in the long term. Plans are fully scriptable objects that simply set goals that the AI should aim for and when. Plans can either be for the early, middle or late game. They also have potential triggers and AI weights so that you can script exactly who, when and why an AI should pick a certain plan ( plans are only replaced when they are fulfilled or no longer valid ).

Code:
early_default_plan = {
    type = early

    income = {
        energy = 50
        minerals = 200
        food = 50
        consumer_goods = 50
        alloys = 100
        unity = 50

        physics_research = 200
        society_research = 200
        engineering_research = 200

        exotic_gases = 1
        volatile_motes = 1
        rare_crystals = 1
        sr_living_metal = 1
        sr_zro = 1
        sr_dark_matter = 1
    }

    focus = {
        energy = 10
        minerals = 30
        alloys = 50
        food = 10
        consumer_goods = 20
    }

    pops = 500
    empire_size = 1.25

    potential = {
        country_uses_consumer_goods = yes
        country_uses_food = yes
    }

    ai_weight = {
        weight = 1
    }
}

Above is the default early economic plan used by regular empires. As you can see the potential trigger ensures that no hiveminds for example use this plan since that would mean that they started producing lots of consumer goods that they don’t need.

Below I will go through each scriptable field of the example plan, so if you’re not interested in scripting your own or more in-depth how the system works you can skip this part

‘type = early’
The type field determines during which part of the game this plan is considered valid, this particular one is valid for the early game so it will only be considered until you reach whatever year has been set as the mid-game start year in the game settings.

‘income = {}’
The income field contains the main economic plan goals that the AI will try to reach when having this plan. Here you can script any available strategic resource followed by how much net income the AI needs to have before it considers that resource goal fulfilled. NOTE: If a resource is not scripted here then that means that the AI will not actively try to build anything that produces that resource, it might do that because a certain building produces something else it needs though.

‘focus = {}’
The focus field can be used to set special focus goals ( net income ) for resources that the AI should specifically try to reach before anything else. This field is great to use to ensure that the AI never dips below a certain net income, and that it makes sure to produce certain resources early on even if there is no deficit of said resource. Minerals and alloys are good example of early game focus resources because the AI needs to produce a considerable amount of them to be competitive, just staying above 0 net income is not enough!

‘pops = 500’
The pops field is the goal number of pops that the AI will try to reach. It will not stop producing pops when this number is reached, however pop growth and assembly buildings will be weighted much lower in favor of more resource production when the goal has been reached.

‘empire_size = 1.25’
The empire_size field denotes, in percentage, a threshold under which the AI should try to keep its Empire Sprawl. In this case 1.25 means that the AI should try to keep its early game Empire Sprawl under 125% of capacity, so that it does not incur too many penalties but still can tolerate some.

‘potential = {}’
The potential field is a standard trigger field, scoped to the AI country, where you can script which type of countries are allowed to use this plan. The base plan potentials are just to ensure that different empire types produce only the resources they need, but you could also script things like special economic plans for when you are at war or if you are in a federation etc.

‘ai_weight = {}’
The ai_weight field is a standard weighting field that you can use to script how much the AI wants to use a certain plan. Since there is only one possible plan per country type and era right now all weights are just set to 1, but you could script up lots of different plans and weigh them differently for different empires based on for example personality, ethics or any other AI property.

Executing The Plan™
So how does the AI turn these plans into anything actionable? Good question! At regular intervals the AI will look at its current economic situation ( income, deficits, budgets etc. ) and compare it to the goals set in the currently active plan. From this it will derive exactly what it needs to reach those goals and create a build plan from that. The build plan is basically a prioritized list of buildings ( or districts ) that the AI wants to build on different planets.


This build plan can be viewed in-game while hovering an empty or locked building slot with ‘debugtooltip’ enabled and can look something like the above image ( the number is the score that the AI has calculated for that building ). The AI will recalculate this plan at regular intervals as well, so that it does not stick to old plans that are no longer relevant.

The AI constructs this build plan by finding out exactly what it can build, what those buildings actually produce when built ( including resources from jobs ) and then try to mix and match buildings to as effectively as possible fulfill the goals identified from the economic plan. This of course takes into account any building upkeep costs or tech limitations present. A benefit of this system is that you no longer have to explicitly script when the AI should build a certain building, instead it will dynamically figure out what each building produces in code. This also means that it is way quicker to add new buildings that produce different resources since you don’t have to manually script how the AI should handle them. Moreover the build plan also tries to take into account on which planet a building would produce the most and build that building there, making the AI more likely to create specialized resource-production planets like “foundry worlds”. It will also not build anything on planets that already have several free jobs. Since this is now done on an empire-wide basis and not planet-by-planet the AI can keep track of and work towards an overarching goal instead of looking at planets in relative isolation. The AI will still go through all your planets to see if any of them are missing housing, amenities or any other planet-specific resources. These buildings are however still part of the economic plan and so if you REALLY need another resource then one of your planets might have to live with an amenities deficit for a while.

So how does the AI actually prioritize the different buildings in the build plan? Another great question! To answer that we need to look at how the AI scores different buildings and districts. The scoring function takes several parameters into account: income needed to reach plan goals, income needed to reach focus goals, current income and deficits, resources produced, miscellaneous resources ( amenities, crime, housing etc. ) produced and lastly if it contributes to pop growth or assembly. The given building gets a score for each of these parameters that is factored by how much we are currently currently missing in that category and how much this building would help to rectify that, along with a defined multiplier. We then add even more score to it if it produces a resource in which we currently have a deficit or if it brings us closer to our focus goal. NOTE: The system remembers all queued buildings so if we have queued 4 mineral districts and that would fix our current mineral deficit then the 5th mineral district would not get the extra deficit score!

Code:
AI_DEFICIT_SCORE_MULT = 50                    # AI will score buildings producing resources in deficit this much more
        AI_FOCUS_SCORE_MULT = 10                    # AI will score buildings producing focus resources this much more
        AI_AMENITIES_SCORE_MULT = 2                    # AI will score amenity buildings this much more than other misc resources
        AI_HOUSING_SCORE_MULT = 5                    # AI will score housing buildings this much more than other misc resources
        AI_CRIME_REDUCTION_SCORE_MULT = 2            # AI will score crime fighting buildings this much more than other misc resource
        AI_ADMIN_CAP_SCORE_MULT = 2                    # AI will score admin cap buildings this much more than other misc resource
        AI_POPS_SCORE_MULT = 5                        # AI will score pop growth and assembly buildings this much more ( already fairly high weighted  in code )
        AI_UPGRADE_SCORE_MULT = 40                    # AI will score building upgrades this much more ( since they don't take up a new building slot )

Above you can see the multipliers applied to each of these scoring parameters, these are all defines and can therefore be modded.

This all sounds very complicated and “maths-y” so let’s do a quick example to illustrate. If Blorg currently has a steady income in all resources except for minerals where net income is -1, and is using the default plan as outlined above with a mineral income goal of 200 and a mineral focus goal of 30, then when the algorithm is scoring a mineral district it would…
  1. See that Blorg is currently missing ~200 net mineral income to reach is goal and would give this a very high base score since the relative amount of income missing is huge
  2. See that the mining district produces resources needed for a focus goal and multiply the score by a defined factor
  3. See that Blorg currently has a mineral deficit and yet again multiply the score by a defined factor
The algorithm would then give a score for the build plan to use when prioritizing buildings and a mining district ( or several ) would soon be constructed by the Blorg. This would work similarly no matter which building we’re looking at, the important thing is that it scores it relatively to how much you are missing from your goal, and deducts any planned building resources from said goals. Meaning that if you’re only missing 2 mineral income the score wouldn’t be that high and if you have queued a mineral producing building then the AI would no longer look at those buildings because the ones you have planned account for all you need to reach that goal.

Evaluating The Plan™
So, does this system manage to address all the issues that I set out to? Yes, it does! :D

You no longer need to do complex scripts for each building, instead you can tweak the overall plan to affect how important the AI thinks a resource is. The plans also ensure that the AI is always looking forward, being proactive, instead of just reacting to deficits and certain happenings. The AI no longer looks at planets in isolation but as part of a whole, all tied together by The Plan™.

This all sounds very fancy-like and good, but now for the real question; does the economic AI perform better overall? Well...yeah!

In our tests we have seen much more robust AI economies and a better handling of the economy overall. And I’m really happy with it! Of course this wasn’t all me, I had lots of help from QA and other devs, and am very thankful for that because without them this wouldn’t have been nearly as good. Now of course, with the release of Federations closing in I would love to see what you ( the community ) manage to do with this and how you will manage to mod this into oblivion ;)

Lastly, I of course did not forget all of the hard work you put into your mods when writing this. The old style of writing building scripts and weights still work IF there are no economic plans for the AI to use. This means that if you remove all the plans or if an AI fulfills all the plans that it can have, then it will fall back to the old system of building planet-by-planet and use the old building weights.

Thanks for reading, and good luck amongst the stars!
 
I know not everyone agrees on this, but I'd rather have small details like this that aren't balanced than not have them at all. Not every empire is destined to own the universe. Sometimes some fall to cruel fates. I like that. I get that it's more important in multiplayer where people aren't there to roleplay a tragic fate, but in single player it adds a lot of flavor to the universe.

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.

I would assume bypasses means wormholes and such?
 
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.

View attachment 550747
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.


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:

View attachment 550749
“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:

So a few questions about the AI in general:

First, one of the big differences between most strategy game AIs and human players in my opinion is that the human players can adjust their strategies and tactics in relation to what the AI does, but AIs usually don´t seem to have this ability, and instead they just do the same mistakes over and over again. (And once you learn what those mistakes are it becomes easy to exploit them.) So any chance of giving the AI at least some ability to adjust to what ever the human player is doing against it? By for example comparing some of it`s stats to the player`s? (Like this critical number on the player`s side is way bigger than what the AI has on it`s side, so thing X should be done more?)

Also in relation to that, one thing I have always hoped the military AI´s could do in these space 4X strategy games is build fleets that have different kinds of ship designs compared to the other AIs (So that one AI empire could use more beam based weapons for example and another more missiles etc.) So that the various empires would feel different in combat as well. And if the ship building strategy the AI is using would proof very ineffective againts the player the AI could "take a look" at the combat performance stats of various components against the player and make adjustments to its fleets accordingly.

And about the Military AI:

-Does the AI have some points (star systems etc.) in it`s own territory as well that it considers crucial or at least important to defend? Since although I have mostly being playing modded Stellaris lately and I realize the AI behaviour there can be a bit different, what I see the AI do a lot is just attack my systems aggressively, but at the same time I can send my own fleet to attack and take many of its systems and it doesn´t seem to mind. Also one behaviour I have seen quite a lot as well is that the AI often seems to go back and forth from system to system like a headless chicken which makes trying to get to some point to face its fleets quite annoying. (Although granted, when avoiding and all out battle with a larger and more powerful fleet it isn´t a bad strategy, but it often seems like this isn´t what the AI is really doing.)

-Can the AI be made more intelligent in using and protecting its troop transports? As quite often I have seen behaviour where the AI just leaves its big fleet of troop transports to be destroyed by my ships after a battle when they could either A. land those ground troops to defend their colony in the system or B. just flee with them immediately so it doesn´t take so heavy and unneccessary losses. And also in general I think it would be nice if the AI would offer its troop transport fleets protection when they are moving from system to system during wars, so that you can`t just take them out easily.

-Have you considered adding things like supply lines that have to be open to the players/AIs own empire to keep the fleets at peak operating efficiency to add more strategic targets to the map and also help against one mega fleet tactics?

And finally, do you have all the necessary processing power there at Paradox to test and make the AI as smart as possible? Since if no perhaps we members of the community could give you some more processing power to run small tests on our systems to make the AI as smart as possible. @grekulf (I think there was some news recently about SETI home project closing soon, so if we can´t use our home computers to find intelligent life from the universe, perhaps we can create some inside Stellaris instead. ;) )
 
Last edited: