Page 1 of 7

Strategic AI Mod

Posted: Fri Apr 13, 2018 12:59 am
by harpy eagle
Hi everyone, I put together a collection of tweaks to how the empire AI manages it's economy.

When I started out my goal was just to patch a few egregious issues I was noticing (more on that below), but it ended up growing to include a few other improvements as well. Making the AI run a perfectly optimal economy was never really the goal, so it will still do some suboptimal things. That said, suggestions on how to improve it are welcome.

What started this mod was that, at a certain point in vanilla games, pretty much every AI empire crashes their mineral economy by attempting to build fleet bases on all their systems, even rockballs with maybe 1 or 2 population. This mod attempts to address that and also introduces a few other improvements:
  • When deciding what ship to build, the AI now considers the number of turns it will take to build, and recognizes that if a ship takes e.g. 10 turns to build, setting a planet to build that ship will only produce at most 1/10th it's value every turn. It also factors in an opportunity cost related to the fact that getting a ship later is inherently less valuable.
  • The AI estimates how long a ship will take to build not only on it's production cost (hammers), but also projects it's current metal income into the future and includes possible future metal shortages in it's build time estimation. Together with the first item, this enables the AI to account for metal costs when deciding what to build.
  • The AI incorporates the already completed progress on a ship when estimating it's build time. Together with the first item, this greatly reduces the tendency of the AI to switch production constantly. In fact I find it usually lets ships build until completion, unless e.g. more metal became available, so it can afford to switch to omething better, or the desire for a colony ship or transport took priority, etc.
  • The AI now calculates the value of potential ships to build using things like the ship's potental damage output, shields, armour, etc.
  • When deciding to build defences on a planet, it includes ships in the possible designs to pick from. After all, ships are just as capable of defending as bases are, and so the decision between the two is based on combat value vs build time instead of being limited to bases. When the AI considers to build a base, the opportunity cost is harsher than for ships though since static defences are strategically inflexible.
  • The AI also now considers build time when deciding what planetary improvements to build - to an extent. Since improvement slots are limited, as they fill up the AI considers build time to be less important. Once half the improvement slots are full, the AI chooses what to build based only upon what it would like that planet to have, regardless of how long it will take.
  • Also adjusted the vanilla improvement build spread. For example, it will now build labs on plants that can have 10+ population, depending on how strongly it desires more research. Also reduced the AI's desire to build farms on poor fertility worlds, though it is still possible.
  • The AI estimates roughly how much ship production it can sustain given it's current metal production, and will try to build less factories if it doesn't have enough metal income to sustain them. If it has an excess of metal, it will try to build more factories.
  • If the AI doesn't know what to build on a planet, it will try to set either mining, research, or trade focus, instead of setting the planet to auto build.
  • The AI now calculates a desire for metal, and may set fully developed planets to focus on mining if wants more (e.g. to meet the estimated metal income needed to sustain production). Non-fully developed plants may also be set to mining focus if the AI is starved of metal (i.e. no metal left and net metal income is negative).
  • Removed some limits on AI ship production that seemed unjustified. Now as the AI gets more ships, it's desire for ships will become less urgent, but it only stops producing ships if it won't be able to afford the upkeep.
  • If the AI sees that it will run out of coins in two turns, it attempts to set planets to trade focus until its coin income is positive again.
  • I've tried to cut down on the scout and colony ship spam. It still builds a lot of scouts though.
  • The AI adjusts it's desire for research based on the research of known other empires, instead of being fixed. This desire now affects the amount of labs it builds, instead of only affecting it's decision to focus on research.
  • The AI now decides to city plan or manufacture population if doing so would produce more resources than building an improvement.
Update v1.0
  • Bugfixes
Update v1.1
  • The AI now compares it's naval power with that of other empires it knows about when determining the priority to assign to shipbuilding.
  • Reduced scout spam.
  • AI now accounts for ship industrial cost modifiers when determining it's desired industrial output to metal income ratio.
Update (13edc0a):
  • The AI will now move population between worlds if doing so would increase the population cap of the destination world and doing so would leave at least 1 pop of that species left on the source world.
Update (097f859):
  • Reworked planetary improvement selection to encourage the AI to build more factories. The AI no longer cares about the build time of improvements, I don't think it was helping. The AI no longer cares about max sustainable hammer production - if it has more production capacity than it can supply with minerals, it just tries extra hard to get more mines.
  • When determining it's desired mineral income, the AI only looks at planets that have at least one factory.
  • Overhauled ship construction desire, to accommodate some important bugfixes in vanilla. I've tried to strike a balance between building up naval power while also allowing for construction of colony ships/outposts/etc.
  • If the AI doesn't have enough transports to meet it's food import needs, it assigns a higher priority to civilian transport production.
  • The AI will no longer attempt to build ground units on planets that are not fully developed when revolt chance is 0.
  • Removed pop transfer code in favour of the vanilla version.
Update (4e97698):
  • Bugfixes
  • Reverted some changes related to AI resource pooling, as it was causing the AI to favour cheaper ships excessively. The AI still uses resource pooling, but makes build decisions as if it doesn't.
  • Made the AI less afraid of metal shortages
Update (4f4d022):
  • A fairly critical fix to the strategic ship combat power calculation, that was causing the AI to act somewhat erratically compared to vanilla.
  • More tweaks to how the AI handles metal shortages
  • Fixed the AI setting mining focus on undeveloped worlds.
NOTE that the latest indev version requires r22220 or later.

LAST UPDATE: 01/05/2018

NOTE: the earliest versions of this mod came in a folder called "StrategyAI", but it has since been renamed to "EconomyAI" (as it does not deal with fleet AI). If you still have the old "StrategyAI" folder in your Mod directory, please make sure to delete it when installing newer versions of this mod.

Re: Strategic AI Tweaks

Posted: Sun Apr 15, 2018 2:03 am
by harpy eagle
Update (7c7c850):
  • When deciding to build a planetary improvement, the AI now considers whether it would get a bigger benefit from city planning (due to staffing levels), and chooses city planning if so.
  • Disabled hammer to metal exchange for the AI.

Re: Strategic AI Tweaks

Posted: Sun Apr 15, 2018 6:47 am
by zolobolo
harpy eagle wrote: [*] Disabled hammer to metal exchange for the AI.[/list]
Qurious: This is do that the AI does not gradually put all of its planets into Mining project instead of building new mines or lowering ship production?
In this case the only safe case when to use this project for the AI is if all the improvement slot of a colony have been filled up, and the focus of the colony output wise is metal

Re: Strategic AI Tweaks

Posted: Sun Apr 15, 2018 2:36 pm
by harpy eagle
zolobolo wrote: Qurious: This is do that the AI does not gradually put all of its planets into Mining project instead of building new mines or lowering ship production?
The hammer to metal exchange was an AI-only feature that allowed the AI to produce metal without any mines (although I can see potential for it becoming accessible to both AI or human players as a technology, that's another discussion).

Basically it helped the AI deal with the fact that it was chronically short on metal. Now that the AI is aware of metal costs, has an idea of how much metal it needs to sustain it's production, and knows how to use mining focus, I found that metal shortages for the AI were rare instead of inevitable as they were previously. And when they did occur, I found the AI got out of them quickly, instead of being crippled by them for dozens of turns. So I felt that it was no longer a necessary advantage for the AI to have.
zolobolo wrote:In this case the only safe case when to use this project for the AI is if all the improvement slot of a colony have been filled up, and the focus of the colony output wise is metal
If you are referring to mining focus, yes it only uses mining focus on non-fully-developed colonies unless it has a metal emergency (zero metal left and negative income). And in such a case it will only use it temporarily since once the AI is so low on metal it will cut it's ship production until it's income is positive again. At which point it's mining desire will be sky high and the AI will be sure to build more mines.

Re: Strategic AI Tweaks

Posted: Sun Apr 15, 2018 8:12 pm
by zolobolo
Another endless AI turn but this time in early game and triggered by the Strategic AI mod

Re: Strategic AI Tweaks

Posted: Sun Apr 15, 2018 9:38 pm
by harpy eagle
I think I've identified the cause of this, hang on...

EDIT: Now fixed (8226e9f).

Thanks for spotting that and sending the test case, not sure if I would have found it on my own.

EDIT: removed outdated attachment.

Re: Strategic AI Tweaks

Posted: Mon Apr 16, 2018 4:37 am
by harpy eagle
I've improved the city planning decision used by the AI, so that it now considers not only staffing levels, but the extra resources you would gain from the population's natural production.

This seems to really help the AI understand that making the population grow faster on a 1-pop colony is more useful than waiting 50 turns for a factory or lab to build. While also being able to tell when the latter is in fact the correct decision.

Image

In the above example the AI wanted to build a lab on Rastaban III, with only 1 population, the AI determined that it would get more beakers by growing the population rather than waiting 50 turns for a lab.

It didn't take long until the population grew to ~3.5, at which point the 10 hammers/wrenches from the population made it possible to build a lab in a more reasonable time frame.

Unfortunately implementing this was pretty ugly code wise since in order to determine exactly the population growth benefit of city planning, I had to rip out a bunch of tables and constants from pop_growth.lua (basically pure copypasta) since they were only accessible locally, or buried inside functions.

But I guess this is just a typical thing that mods have to deal with.

I'm going to try to update the OP with new versions from now on, instead of attaching to posts.

Re: Strategic AI Tweaks

Posted: Mon Apr 16, 2018 12:35 pm
by CapnDarwin
Harpy Eagle, thanks for your mod work. I have been playing with them and it's noticeably better.

One suggestion, I like the latest in the original post, but can you include the date of the update as well so I know when things have changed. Thanks.

Strat AI question. Does the AI weigh starting another war based on number of wars already being fought and the splitting of warships and loss of firepower versus all opponents?

My current game I was pretty immediately attacked by the Imperials who considered me weak (bad guess) but they were also fighting one or two others at the same time. I lost two undefended outposts before my ships arrived, pounded his new colony and wiped out his small fleet. From there with more warships leaving my production world I went on a run through a handful of his systems wiping them clean.

Is there a way to make the AI look at the big picture and frown on multiple front wars unless they have fleets strong enough to give it a shot (with some random fuzz to have some crazy stuff happen)?

Thanks and keep up the great work!

Re: Strategic AI Tweaks

Posted: Mon Apr 16, 2018 4:37 pm
by harpy eagle
CapnDarwin wrote:Strat AI question. Does the AI weigh starting another war based on number of wars already being fought and the splitting of warships and loss of firepower versus all opponents?
CapnDarwin, I haven't really looked at how the AI handles diplomacy, but now's probably not a bad time to start...

Okay, so right now in vanilla it looks like the AI decides to declare war if EITHER:

A. Their total fleet power is greater than twice your fleet power - so far so good.

OR

B. Their total fleet power is greater than some minimum power value that is proportional to 8 * (their total industrial output i.e. wrench production, or "hammers" as it is called in the code)

The second one is a little strange. I imagine the developers intended it as a fuzz factor like you mentioned, to ensure that the AI isn't overly cautious and interesting things still happen. The problem is that it is poorly chosen - since it triggers when their fleet power is greater than their industrial output it means that empires with worse economies are more likely to declare war(?!)

I think having a condition like that which shortcuts the "overpowering navy" condition is a good approach though, probably better than adding randomness. I think it would be enough to really improve the AI here if we chose a better short-circuit condition.

EDIT: It also looks like the AI is prevented from declaring war if it is already at war, so I'm not sure how the imperials decided to declare war on you. Seems like a bug.

Re: Strategic AI Tweaks

Posted: Mon Apr 16, 2018 5:16 pm
by CapnDarwin
I think the second condition emulates a faction with a strong force, but limited resources to grow with. Like Japan in WW2. They need to strike outward to grow before their warship power is overrun by others.

Getting the AI out of multi-front wars in the early and mid-game part of the game should setup better diplomacy and trade before the big wars start.

Re: Strategic AI Tweaks

Posted: Mon Apr 16, 2018 5:42 pm
by harpy eagle
CapnDarwin wrote:I think the second condition emulates a faction with a strong force, but limited resources to grow with. Like Japan in WW2. They need to strike outward to grow before their warship power is overrun by others
Right, I hadn't considered that. Although, since it reflects current production instead of potential production, the way it's set up makes it easy for the AI to make a mistake.

I think it should factor in the total number of unfilled improvement slots, available room for population growth, and # of unclaimed colonies to make that decision.

Re: Strategic AI Tweaks

Posted: Mon Apr 16, 2018 6:10 pm
by zolobolo
Sounds logical: If expected industrial growth of an empire is expected to be capped, it needs to target the next best target to expand to
In my experience, this logic often cannot shine due to range considerations: a faction situated on one side of my empire declaring war on another one on the other side because I have just cut them down to size. In these cases they can obviously not engage the enemy and have thus used the war declaration slot for nothing

So the basic idea is solid, would just benefit from considering a couple of additional parameters besides the already mentioned capacity like:
- Range: Can I effectively attack some of the valuable targets (and not just outposts or a single colony with 1 pop)
- Diplomatic status of the target: the more empires the potential target is currently in war in, the more weight it becomes. This would enable the AI to be opportunistic in their selection and pray on weakened ones
- Existence of effective invasion force: if the AI does not have transports and ground troops, there is no sense in declaring war and giving the target to strike on production worlds or defense up before the invasion finally arrives

@harpy eagle: do you have a console command that reveals the whole map like above? Would make the whole strategic AI testing much more fluid instead of rush-declaring alliance with all empires

The other topic I asked you about on the other thread was if we could see the rest of the resource values of the other empires like food, total production, and trade root usage/capacity - we can already see some of them when selecting them, but not all of them. This makes it more difficult to see why and under which conditions the AI has made a decision. Generally I also think there is no reason not to know these values of the AI as they know that of the player and each other, the player could calculate them if taken the time, and when making deals like trying to buy food it would basically be a must to see who has capacity to begin with before initiating the discussion

Re: Strategic AI Tweaks

Posted: Mon Apr 16, 2018 6:16 pm
by sven
harpy eagle wrote:
CapnDarwin wrote:I think the second condition emulates a faction with a strong force, but limited resources to grow with. Like Japan in WW2. They need to strike outward to grow before their warship power is overrun by others
Right, I hadn't considered that. Although, since it reflects current production instead of potential production, the way it's set up makes it easy for the AI to make a mistake.

I think it should factor in the total number of unfilled improvement slots, available room for population growth, and # of unclaimed colonies to make that decision.
I think a simpler fix might be to just look at the percentage of their income that they're spending on fleet upkeep. In theory, if a) most of your planets are well developed and b) you're spending more than you can afford on fleet upkeeps, then it's time for a war of expansion :)

I think that's more or less what I had intended the industry check to achieve, but, as harpy eagle points out, the current logic can result in 'false positives' where an empire with an undeveloped economy will mistakenly think it's ready for a war.

Re: Strategic AI Tweaks

Posted: Mon Apr 16, 2018 6:22 pm
by sven
zolobolo wrote:The other topic I asked you about on the other thread was if we could see the rest of the resource values of the other empires like food, total production, and trade root usage/capacity - we can already see some of them when selecting them, but not all of them. This makes it more difficult to see why and under which conditions the AI has made a decision. Generally I also think there is no reason not to know these values of the AI as they know that of the player and each other, the player could calculate them if taken the time, and when making deals like trying to buy food it would basically be a must to see who has capacity to begin with before initiating the discussion
If you turn on the 'developer' options by creating a 'show_hidden_options.bool' (as described here), you'll be able to turn on and off 'omniscient developer' mode. You can also turn on 'ai dev', which will let you switch to controlling any other faction by alt-clicking on one of their planets (and that of course will let you see all their resources, etc). 'ai dev' can also be useful in setting up mock battles of whatever configuration, because you can just create and the sorts of fleets you want and then move them into position. (however, to use that trick, you'll need to turn the AI -off- for whatever factions you want to control, which you do by clicking the white faction name at the top of the screen so that it turns grey.)

Re: Strategic AI Tweaks

Posted: Mon Apr 16, 2018 6:58 pm
by zolobolo
sven wrote: If you turn on the 'developer' options by creating a 'show_hidden_options.bool' (as described here)
Thanks for the tip, and for supporting the modding community so fully - the DEV options menu is a real treasure trove
Finally I can display metal production and also: so this is how we can change our faction color scheme :)!!!

Generally would still suggest implementing all the resource values into the diplomacy menu for the above reason for everyone to see

Considering the genre, players would surely enjoy more stats - heck if you just visualize these values historically in a graph when the game ends most people would probably take that as a proper victory screen ;)