• 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 #227 - Looking after the AI

KdHFPIxnbfUdecDUWFOFpKrSq3pNeDU6BQdeWWLLCrYe655fAX8gyF_3548erNqCH1DYaMfK8qb7BwsbIZwsd22bEQ3q9jmM7-jKe4dUCd--WO07urWQEkcuT3b-0pJYzWnHB--e=s0


Hello and welcome to a new Dev Diary,

My name is Guido and today I’m here in my role as a Principal Designer on Stellaris to talk about AI in a bit more detail.

You probably have heard about the Custodian Initiative by now which has been created to keep improving the game on a more regular basis and in order to be quicker when reacting to player feedback. A part of this initiative is also to put some more love and attention to the AI of the game going forward - an AI initiative inside the Custodians, basically.

For this, we have set some goals for ourselves going forward:
  1. Always work on AI-related topics, regardless of what else is going on
  2. Move the AI towards being challenging to players in an entertaining way, rather than be optimized to min-max its way to victory
  3. Move the AI towards being more distinctive, so that not all empires feel strong in the same way
  4. Support future DLCs from the get-go
  5. Constantly make small improvements to the AI
  6. React quicker to player feedback
  7. Occasionally make a push for more significant improvements
Speaking of which, for the upcoming patch in November, we have some significant updates in store.

Economic Script Update​


First of all, the biggest change you will notice is how we have changed the economic plans script. This script is the core of the economic behavior of our empires. It defines what resources they strive to get when building districts and buildings. How much population growth they should go for and how much research and unity they want.

The functionality of the script hasn’t changed much, but how we are using it has changed.

Previously the script was divided into early-, mid-and late-game. Depending on the phase of the game, empires would prioritize resources differently. For example, focus on research was lower in the early game than in the later stages of the game. However, this approach didn’t take into account the various situations an empire can find itself in. Especially after a war or when a new empire breaks off an existing one. In those cases, even if the game phase was in the late game, for the respective empires it meant that they were in a much more ‘early game’ position.

uBhcCRICEVI9OQc0U-dQMkvrUuRI9XEsu0KdS3naOfBmz7zMa3jbIVU5q5zwWYshpObGTaTqau-zMAW1SiRcHXYOCNRw1qvq0zbQrPepOEP9jAi1K0kuezFyEsQ0heKhJ84-idbl=s0
LT2BJzggvYTk6h4XYQCGbLlvBzvXZ2PcUyhl3e5myTpzkO2jLqXdCRhIDrs9oUipqiM4uuh89BdjMiW30PUXU2HrXtE3dsHufnJK0TFiAWjZtmRgB2OKmXRzATX-2kNzRJfofA0p=s0


Instead of having 3 different economic plans, we feature 1 base plan instead. In order to get more flexibility and to react to the empire’s situation, we’re relying much more on the ‘subplans’ inside that base plan.

Improved economic subplans​


Subplans can be turned on or off, depending on the situation the empire finds itself in. Our main rationale was to ensure that an empire would be economically stable before it spends resources on ‘bonus’ things like research, population growth, defensive modules on starbases, and unity buildings.

Previously those things were prioritized too early and without enough respect to the basic income of energy and minerals, leading to empires that produced alloys, but had big deficits in energy and mineral production. And this deficit would be the start of an economic death spiral, where the resource debuffs would further reduce production and everything just escalated to the point where an empire was bankrupt on all resources. This became especially problematic after the economic system has been rebalanced to focus resource production more on the districts, rather than the buildings of a planet.

Here’s an example of what the economic situation generally looked for empires in a game that went on for around 80 years:

JkCSTjSHVsY87vJzUb5vNrO_K8aHiUIs6vu-6AQHjeP2sNF_ImdCKwdRaCpSp6mZrqnBSr3eFpfwdu-eUMG-uN16duNFGgaMqw5Ipm4Fd1V4UtwxN5tuTy2X2mEi6_EC_OODtvFb=s0

(These are screenshots from Stellaris version 3.0.3)

Our updated economic script prioritizes basic income first and takes the new economic rebalance into account. Energy and minerals are most important.

The difference between the ‘income’ and ‘focus’ block is that if the monthly income is below what is defined in the ‘focus’ block - districts and buildings which produce those resources get an extra bonus in weight, when deciding what to build.

basic_plan_02.jpg


Then the first subplan kicks in. If a country uses food (therefore, Machine empires will have this subplan turned off) it will prioritize food production.

subplan_1.jpg


The next subplan will check conditions for focusing on consumer goods. Again, checking if the empire actually uses them or not - and then only focus on producing them if the empire has at least a monthly income of minerals of 30.
Based on the fact that in order to create consumer goods you require minerals.
subplan_2.jpg



Further down we activate the plans for prioritizing research and all the higher-level resources

CS9qyuZNW1naD8IG6k1-5GyF-QkYeF_Vw2k9Lugo6rrBtdcFzksDJ2W6X1AHhjHZi44UUkapwAWkaUFBRkvlJ2VxNJp2GQv-6dwBjjCcv86v-sIuInAnfOPr_zYYJzT88QpS51qi=s0


Resulting behavior improvements​


So, the script can check for various situations in AI empires - from the fact if they are a Gestalt Empire, using food to monthly income of specific resources.
This gives the AI a lot more flexibility in managing its economy.

As an example, here we have a 100-year old Galaxy with 13 AIs and every empire is able to manage its economy in a decent enough way. Notice the resource tab at the top - almost all empires have positive income in all resources; the ones with a negative income only have a small deficit:

97IOcqgoSBd3m6c8CQbtqO5z6Yx4XkWopX5HciBdo_4zqVqepRs4QDnnMysgLyute8GQust0nDCT14UMrrSL38p08pcKWHbrvIMbFA8Aj_9WTVpfWVVo8Tw-Qpft6Fi9uJPaSA-c=s0


Apart from this, there were some small, but significant code changes that helped the AI in running the show.

Conclusion​


The code for the AI has been optimized heavily in the past in order to improve performance a lot. However, this has led to some unforeseen and unintended behaviors which have now been corrected. Some of the districts and buildings weren’t considered at all and city districts were weighted way too high. The AI is also now able to build temples and holo theaters, for example.

Finally, the AI has also been given a bit of support in how it will set up its starbases, especially in conjunction with the hydroponics starbase building, which can play a larger role in how you provide food for your empire. The AIs can now use more varied setups when building their starbases, making use of Curator Think Tanks, Nebula Refineries, and other special buildings where it makes sense.

And all of this was built on the foundation of the last major rework of the economic AI, so kudos to @sidestep for making this evolutionary step possible.

With your help, we’re looking forward to giving the AI the attention it deserves and making it even better in the future.

Cheers,
Guido
 
Last edited:
  • 158Like
  • 70Love
  • 14
  • 7
  • 3Haha
  • 1
Reactions:
The final sub-plan that we're currently looking at is set up as follows:

Code:
@consumer_goods_target = 30
@research_target = 9999
@unity_target = 70
@alloy_target = 500
@rare_resource_target = 4

income = {
    physics_research = @research_target
    society_research = @research_target
    engineering_research = @research_target
    unity = @unity_target
    alloys = @alloy_target
    exotic_gases = @rare_resource_target
    volatile_motes = @rare_resource_target
    rare_crystals = @rare_resource_target
}

I am curious how increasing naval capacity is handled. Is it by some different part of the AI not covered in here? As both planetary buildings and starbases can be used to increase the capacity.

Like others have said, the alloy goal also seems very low, especially in the higher difficulties. 500 alloys per month is going to be very little if/when the AI has to replace fleets during a long war or crisis. Same applies for building up megastructures, too.

The other thing which I am somewhat concerned if the fleet building is handled by another part of the AI how the systems avoid situations where the Fleet AI wants to build more fleets but lacks resources or fleet capacity because other parts of the AI has decided that "we have enough of stuff now". Basically if there is any risk where AI ends up doing nothing because all goals are seemingly fullfilled: "yeah we have 10 unemployed pops but since we already have 500 alloys per month we are not gonna do anything".
 
Why are you asking this in the PC forum? The PC devs have nothing to do with the console devs. Ask in the console forum instead.

Probably because the console forum is pretty difficult to find as it is sub-forum of normal Stellaris forum and listed after everything else (tech, bug etc). It really should be it's own standalone forum if PDX wants active discussions there or very least more visible than it is now.
 
  • 4
Reactions:
No more special modes, thanks. It should react just the same as if it lost a resource planet in a war. The AI needs to be made more generalised, not more situational.
I'm not in any way picky about the process but it's silly that choosing to fall under the vassalage of a more powerful empire can immediately send an NPC empire's economy into freefall for no good (in universe) reason. Maybe the exact nature of higher difficulty bonuses needs to be reexamined so that the NPC empires utilize but don't depend on them, or becoming a vassal adds a reverse scaling difficulty bonus that reduces by a bit every year rather than all at once, or something completely different. But the current setup definitely causes Problems.
 
it's silly that choosing to fall under the vassalage of a more powerful empire can immediately send an NPC empire's economy into freefall for no good (in universe) reason.
As soon as you start examining things under a Watsonian lens, difficulty bonuses make no sense at all anyway :)
 
Our updated economic script prioritizes basic income first and takes the new economic rebalance into account. Energy and minerals are most important.

Do you plan also to teach AI to repair buildings and to get rid of those which consume a lot of resources? It is bad when I take enemy planet with mineral production, and he never guess to destroy some of his factories to deal with this situation.
 
I'm a bit late to this party (had some stuff go down over the weekend), but I have some thoughts/concerns on some of what I read. Specifically:

Our updated economic script prioritizes basic income first and takes the new economic rebalance into account. Energy and minerals are most important.
Once you get past the first few decades (and I'm being generous on the timescale here) Minerals stop being important except for their use as input for numerous other more-important resources (Alloys, Research (through CGs), Strategic Resources). While you occasionally need a lot all at once (Acrologies/Ecunopoli), other then that you don't need a huge stockpile.

I understand you want the AI to have enough Minerals on hand to allow it to create the resources that depend on it, but I can't help but think you have it backwards in terms of priority. In terms of priority, there are three resources that are clearly more important than the rest: Energy, Alloys, and Research. The AI should *always* default to maximizing these three resources, and address the others only as necessary. Or in other words, if a Mineral shortage is preventing the construction of a new Industrial district, than focus on propping up Mineral production.

My concern is by making the AI prioritize Minerals you'll end up with AIs that skimp on buildings that consume Minerals (Foundries/Factories, etc.) in order to maximize Mineral production. Have the AI prioritize the resources that are truly important, and make it smart enough to figure out what inputs it needs to maximize in order to reach that point.

Farther, I think the AI needs to have a better idea how to build/specialize it's planets. What I would do is have the AI "grade" each planet for each possible resource output. The highest grade won't automatically be the planet specialization; that does depend somewhat on what other planets are specialized to. But the AI should be smart enough to know, at a glance, how each planet will eventually look like once all possible buildings have been constructed (including things like Acropolis/Ecunopoli). The reason for this is simple: When the time comes to plop down a new district, the AI should know ahead of time what planet it is optimal to put that district down in. Likewise, this solves an existing problem where the AI never seems to replace existing districts; if the AI knows ahead of time what districts are optimal, it should be smart enough to know any non-optimal districts will eventually get replaced with optimal ones once the total Empire surplus is above a certain point.
 
  • 1Like
Reactions:
I am curious how increasing naval capacity is handled. Is it by some different part of the AI not covered in here? As both planetary buildings and starbases can be used to increase the capacity.

Like others have said, the alloy goal also seems very low, especially in the higher difficulties. 500 alloys per month is going to be very little if/when the AI has to replace fleets during a long war or crisis. Same applies for building up megastructures, too.

The other thing which I am somewhat concerned if the fleet building is handled by another part of the AI how the systems avoid situations where the Fleet AI wants to build more fleets but lacks resources or fleet capacity because other parts of the AI has decided that "we have enough of stuff now". Basically if there is any risk where AI ends up doing nothing because all goals are seemingly fullfilled: "yeah we have 10 unemployed pops but since we already have 500 alloys per month we are not gonna do anything".
I would imagine "Naval Capacity" is treated as just another resource, which the AI should attempt to maximize in as optimal a way as possible.
 
Assuming a normal non-gestalt empire without any special civics then answer is pretty much no to both like I described in the original post. No matter if the empire is pacifist ("leave us alone in peace") or militarist or whatever they need a strong enough economy to achieve their goals. Gestalts and empires with special civics also need same thing but obviously some things are added or removed (eg. no food for machine empires, higher food demand for catalytic empire).



True, but that is mostly a balancing issue if some techs are absolutely mandatory (eg. resource techs which I'd like to get removed entirely and moved to the planets itself somehow). Like I said, Startech/Starnet is not a good example when it comes to that level of AI behaviour and I don't think developers should follow it blindly but for actually building up the planets something similar would be nice.

Currently the AI basically picks techs and ascension perks at random regardless of the ethics or civics or AI personality which just makes bunch of similar and inefficient empires. This makes ascension perks especially bad as the AI tends to pick one soon as there is a free slot. Like I said in the original post the AI should be able to make some long term plans for picking ascension perks and related technology ("I want to repair the ruined megastructure so I need to research prequisities for mega-engineering", "I want to pick X ascension so I need to save two perk slots even if I currently lack the tech").

My point, as in the original post, is that the economic behaviour of the AI is mostly invisible to the player unless it fails completely, overwhelms the player or the player is playing as a MegaCorp since they interact with the other empires more. The economic AI (especially planet building) should be not entirely hamstrung just for sake making them different. There of course could be (and should be) slightly changed priorities depending on the situation. For example if the empire has friendly relationships and commercial pact with a megacorp they might be more likely to build a commercial zone if the planet has empty building slot and there isn't any other higher priority building (eg. alloy factory) available.

The focus for the different AI behaviour should be elsewhere than in the pure economy/planet building: In diplomacy, war, ascensions etc. which are far more visible the player. Currently the AIs behave very similarly though obviously a Xenophobic empire is less likely to ally without a common threat for example. This is especially visible in the Galactic Community where you can see many empires just proposing random stuff and never cancelling it. Hopefully future developer diaries expand on the possible changes to other parts of AI besides the economy.
This indeed is a problem. The ascension perks DO have a weighting for the ai on what to pick, but as it fills an ascension slot as soon it opens most slots end up being filled with tier1 perks. As ascension perks which need 2 already unlocked slots or some lategame tech to unlock get unlocked far too late. Until then the ai has already filled most of its perk slot with useless perks, which might have had a very low weighting but were the highest weighting at that time.
This leads to the strange situation that the lower the difficulty and the worse an ai in unity/eco the better the perks it takes.

There are also petks in the weighting for the ai to choose, which it cannot even use. The ecumenopolis perk for example. It annoyed me so much that i modded the ascension file myself so the lategame ascension perks have lower requirements and many useless perks are completely blocked (or have an incredibly low weighting) for the ai to take. I have seen a lot more dyson spheres, machine/gaia/hive worlds and full synthetic/bio/psionic ascensions since.
 
  • 1
Reactions:
Could you give us a way to see how the player empire is doing compared to the AI plans?

When I see a income of 40 Food, it is hard to say if that is barely enough or a massive abundance for my empire at this part of the game.
Shortages in some materials might "sneak up" on you, as +40 Minerals with a large storage still looks solid.
A simple yellow color + the target value shown in tooltip could be enough.

This would also be a assistance for AI developers, as they can quickly spot where the AI is falling behind the plan in resources.
 
  • 1
Reactions:
What perks can the AI not use? Are they actually blocked from creating ecumenopoli or are they just realistically incapable of doing so?
Blocked. You can find the script in common/decisions/02_special_decisions.txt. Relic world version of the decision is also weighted to 0.

Code:
# Arcology Project
decision_arcology_project = {

<snip>

    ai_weight = {
        weight = 0
    }
}

The AI also can't repair the shattered ring.
 
  • 3
Reactions:
That would be so weird, if true.
It has always been true.

The Console Devs are literally a different company. Doing similar work, in totally different order. And no crossplay is planned.
 
  • 5
  • 1Like
Reactions:
I'm a bit late to this party (had some stuff go down over the weekend), but I have some thoughts/concerns on some of what I read. Specifically:


Once you get past the first few decades (and I'm being generous on the timescale here) Minerals stop being important except for their use as input for numerous other more-important resources (Alloys, Research (through CGs), Strategic Resources). While you occasionally need a lot all at once (Acrologies/Ecunopoli), other then that you don't need a huge stockpile.

I understand you want the AI to have enough Minerals on hand to allow it to create the resources that depend on it, but I can't help but think you have it backwards in terms of priority. In terms of priority, there are three resources that are clearly more important than the rest: Energy, Alloys, and Research. The AI should *always* default to maximizing these three resources, and address the others only as necessary. Or in other words, if a Mineral shortage is preventing the construction of a new Industrial district, than focus on propping up Mineral production.

My concern is by making the AI prioritize Minerals you'll end up with AIs that skimp on buildings that consume Minerals (Foundries/Factories, etc.) in order to maximize Mineral production. Have the AI prioritize the resources that are truly important, and make it smart enough to figure out what inputs it needs to maximize in order to reach that point.

Farther, I think the AI needs to have a better idea how to build/specialize it's planets. What I would do is have the AI "grade" each planet for each possible resource output. The highest grade won't automatically be the planet specialization; that does depend somewhat on what other planets are specialized to. But the AI should be smart enough to know, at a glance, how each planet will eventually look like once all possible buildings have been constructed (including things like Acropolis/Ecunopoli). The reason for this is simple: When the time comes to plop down a new district, the AI should know ahead of time what planet it is optimal to put that district down in. Likewise, this solves an existing problem where the AI never seems to replace existing districts; if the AI knows ahead of time what districts are optimal, it should be smart enough to know any non-optimal districts will eventually get replaced with optimal ones once the total Empire surplus is above a certain point.
The DD explains that prioritizing basic resources has resulted in a higher production of advanced resources compared to when they tried the other way around.

"Previously those things were prioritized too early and without enough respect to the basic income of energy and minerals, leading to empires that produced alloys, but had big deficits in energy and mineral production. And this deficit would be the start of an economic death spiral, where the resource debuffs would further reduce production and everything just escalated to the point where an empire was bankrupt on all resources. This became especially problematic after the economic system has been rebalanced to focus resource production more on the districts, rather than the buildings of a planet."
 
  • 1
Reactions:
Energy, Alloys, and Research. The AI should *always* default to maximizing these three resources, and address the others only as necessary.
The the entire economy crashes, because it has not enough minerals to run Alloy or CG production and it is in a death spiral.
Or becuase food becomes a shortage, and there is massive loss of Happiness. Wich ends in a daeth spiral.
The issue was a lack of the most basic resources, which collapsed all building and maintenance ability.

The best your idea would to is build more things it can not support and then have even less population and stored minerals to fix the shortages that will bring it down.
 
  • 1
Reactions:
I think we need to abandon the term "AI" for this game, it's clearly just scripts. If this, then that. A finite list of conditions.

To achieve what you're saying, maybe they should just make specific build lists, based on categorizing planets.

Elaborate build lists with hundreds of conditions.

Make the system do 1 check during colonization, to see which category and associated build list to apply.

Make it re-check and adjust the planet category every 10 or 20 years. If the category changes, make the "AI" demolish stuff that does not conform to its current planet-category-applied build list.

I mean this is a super primitive approach but what else can be done, considering the AI approach in this game?
Everyone seemed to hate when this was in place for Masters of Orion 3 but I rather liked it.

Meant that I essentially one and doned my entire empire economy after a few games of tinkering with the automation scripts but it was fairly fun.
 
Everyone seemed to hate when this was in place for Masters of Orion 3 but I rather liked it.

Meant that I essentially one and doned my entire empire economy after a few games of tinkering with the automation scripts but it was fairly fun.
And the AI side would break beyond recognition after 1-2 mods or patches.
It was also a turn based game, where the math was leagues simpler and you had more time to do the math.

This has no hope of working for Stellaris.
 
  • 1Like
  • 1
Reactions: