ChooChoo, a train network AI

Discuss the new AI features ("NoAI") introduced into OpenTTD 0.7, allowing you to implement custom AIs, and the new Game Scripts available in OpenTTD 1.2 and higher.

Moderator: OpenTTD Developers

User avatar
Michiel
Transport Coordinator
Transport Coordinator
Posts: 338
Joined: 13 Jul 2008 00:57
Contact:

Re: ChooChoo, a train network AI

Post by Michiel »

Kogut wrote:From "newgrf hates ChooChoo" saga
:lol:

Alas, I've been rather busy on a new project - which also includes an AI framework, inspired by OpenTTD NoAI! It's our turn based strategy game, Powargrid, and I'm shamelessly going to take this opportunity to plug it :)

Game website: www.powargrid.com
AI dev forum: https://forum.powargrid.com/phpbb/viewforum.php?f=9
NsNidPL
Engineer
Engineer
Posts: 1
Joined: 15 May 2017 12:32

Re: ChooChoo, a train network AI

Post by NsNidPL »

Hi can you make a version that doesnt choose newest aviable track because the newest aviable at me is metro and crashes
User avatar
Michiel
Transport Coordinator
Transport Coordinator
Posts: 338
Joined: 13 Jul 2008 00:57
Contact:

Re: ChooChoo, a train network AI

Post by Michiel »

I'd love to, but I have too many projects in the works (including a kid, these days!) to continue work on ChooChoo :(
User avatar
Redirect Left
Tycoon
Tycoon
Posts: 7238
Joined: 22 Jan 2005 19:31
Location: Wakefield, West Yorkshire

Re: ChooChoo, a train network AI

Post by Redirect Left »

I'm having this issue, seems to occur in all maps.
The one this occured on was a randomly generated 2048 x 2048 map, generated by TerraGenesis. The AI had been left to run by itself (so i could check if it errored when playing by itself and with me just idle)
It's also occured on smaller maps, down to 256 x 256 is what i've tried, below that it doesn't work correctly anyway from not having enough places to use.

Any ideas for what's wrong? If I know what even the basics of what is going wrong, I'll have a go at fixing it myself.
Unnamed, 1975-11-27.png
(186.46 KiB) Not downloaded yet
Image
Need some good tested AI? - Unofficial AI Tester, list of good stuff & thread is here.
Alberth
OpenTTD Developer
OpenTTD Developer
Posts: 4763
Joined: 09 Sep 2007 05:03
Location: home

Re: ChooChoo, a train network AI

Post by Alberth »

src/script/api/script_list.cpp, line 619-ish wrote:

Code: Select all

		/* Kill the script when the valuator call takes way too long.
		 * Triggered by nesting valuators, which then take billions of iterations. */
		if (ScriptController::GetOpsTillSuspend() < -1000000) {
			/* See below for explanation. The extra pop is the return value. */
			sq_pop(vm, nparam + 4);

			ScriptObject::SetAllowDoCommand(backup_allow);
			return sq_throwerror(vm, "excessive CPU usage in valuator function");
		}
In short, that function runs a function over a list, but the amount of time to run that code is too large.

Solutions can be anything to reduce the execution time. Move some code out to a second iteration, prepare some data before hand so it runs faster, or break the list in smaller pieces are all options.
Being a retired OpenTTD developer does not mean I know what I am doing.
User avatar
Redirect Left
Tycoon
Tycoon
Posts: 7238
Joined: 22 Jan 2005 19:31
Location: Wakefield, West Yorkshire

Re: ChooChoo, a train network AI

Post by Redirect Left »

So in the screenshot, it's listing the calls (noting the file + exact line that took it there) that led up to the actual section of code that caused the error? In this case IsBuildableRectangle is the function that had an issue? (Line 97 is 'area.Valuate(AITile.IsBuildable);')

Sounds like the entire thing is a wild goose chase until you find the end thing through trial and error until you get the hang of the language and setup of AIs, need to go through the files to find out where area.Value(AITul[..] is defined and find ot what is wrong with that.

The AI worked fine <many versions ago> so I wonder if something in openTTD has changed.
Image
Need some good tested AI? - Unofficial AI Tester, list of good stuff & thread is here.
Alberth
OpenTTD Developer
OpenTTD Developer
Posts: 4763
Joined: 09 Sep 2007 05:03
Location: home

Re: ChooChoo, a train network AI

Post by Alberth »

Yes the list of functions + line numbers is a call-stack, it starts at the bottom where main.nut, line 75 did a function call, that led eventually to executing builder_network.nut, line 442. That line did a function call, which eventually led to executing the same file, but at line 601. There is the (long) native call, which called a function in Squirrel again, which triggered the error in builder.nut, line 97.

[ie just like you say]

You get 2 error messages for some reason. Most likely is because the native function noticed that the squirrel subroutine failed, and it passed the error + stack down for it caller to deal with it.

The other red things look like variables and their values, eg "flat" is probably a variable, and it has value "true".
In this case IsBuildableRectangle is the function that had an issue? (Line 97 is 'area.Valuate(AITile.IsBuildable);')
Normally, yes.

However "excessive CPU usage" means the native function takes too long, and that needs some explanation first.

What you want is that the AI is not eating all processor time, or the normal game would suffer. To get that, an AI gets some number of opcodes it can execute, and then its turn has passed, and the script has to wait for its next turn.
The problem is that you can't stop halfway a native function. It's either not do it this turn, or do it fully. Since it's impossible to guess beforehand how long it will take, there is only one solution, do the native call. This is what was implemented first (probably, I never looked at the squirrel code in detail). However, as AI authors got more experienced the code got bigger and more complicated, which is fine in itself (since squirrel code can be stopped at any statement), UNTIL you run such code through a native function. At that moment, the AI can't be stopped halfway, and it can eat all CPU it wants.
Some time ago, this became too much of a problem apparently, and an additional check was added that the code is not wildly beyond its allowed number of opcodes. If it is, the interpreter now throws an error "excessive cpu usage", which mostly translates to "don't make long and complicated computations while using a native function".
So while your error triggered at "isBuildableRectangle", but it is not an error at that point, it's an arbitrary "enough, now stop it!" point.

The real culprit here is either in the function that is called from the native function (which would be too long and complicated), or it is in the list items that it received (assuming the native function does a loop of some kind), which is too long.
Being a retired OpenTTD developer does not mean I know what I am doing.
User avatar
Michiel
Transport Coordinator
Transport Coordinator
Posts: 338
Joined: 13 Jul 2008 00:57
Contact:

Re: ChooChoo, a train network AI

Post by Michiel »

Hi Redirect Left, thanks for giving my AI a go :) And thanks to Alberth for responding in detail.

I don't immediately see what's wrong - it should only look at a small number of tiles, and the valuator function is not trivial, but not that expensive either:

Code: Select all

function IsBuildableRectangle(location, rotation, from, to, mustBeFlat) {
	// check if the area is clear and flat
	// TODO: don't require it to be flat, check if it can be leveled
	local coords = RelativeCoordinates(location, rotation);
	local height = AITile.GetMaxHeight(location);
	
	for (local x = from[0]; x < to[0]; x++) {
		for (local y = from[1]; y < to[1]; y++) {
			local tile = coords.GetTile([x, y]);
			local flat = AITile.GetMaxHeight(tile) == height && AITile.GetMinHeight(tile) == height && AITile.GetMaxHeight(tile) == height;
			if (!AITile.IsBuildable(tile) || (mustBeFlat && !flat)) {
				return false;
			}
			
			local area = AITileList();
			SafeAddRectangle(area, tile, 1);
			area.Valuate(AITile.GetMinHeight);
			area.KeepAboveValue(height - 2);
			area.Valuate(AITile.GetMaxHeight);
			area.KeepBelowValue(height + 2);
			area.Valuate(AITile.IsBuildable);
			area.KeepValue(1);
			
			local flattenable = (
				area.Count() == 9 &&
				abs(AITile.GetMinHeight(tile) - height) <= 1 &&
				abs(AITile.GetMaxHeight(tile) - height) <= 1);
			
			if (!AITile.IsBuildable(tile) || !flattenable || (mustBeFlat && !flat)) {
				return false;
			}
		}
	}
	
	return true;
}
It's been years since I worked on this, so I'm afraid the chance of a fix is low...
User avatar
Michiel
Transport Coordinator
Transport Coordinator
Posts: 338
Joined: 13 Jul 2008 00:57
Contact:

Re: ChooChoo, a train network AI

Post by Michiel »

Quick fix if you want to hack the source code: replace the valuator with a for loop so it can be interrupted after every call.

In builder_network.nut, replace line 601 with the following:

Code: Select all

		// tiles.Valuate(IsBuildableRectangle, Rotation.ROT_0, [-2, -2], [Crossing.WIDTH + 2, Crossing.WIDTH + 2], false);
		for (local tile = tiles.Begin(); tiles.HasNext(); tile = tiles.Next()) {
			tiles.SetValue(tile, IsBuildableRectangle(tile, Rotation.ROT_0, [-2, -2], [Crossing.WIDTH + 2, Crossing.WIDTH + 2], false) ? 1 : 0);
		}
User avatar
Michiel
Transport Coordinator
Transport Coordinator
Posts: 338
Joined: 13 Jul 2008 00:57
Contact:

Re: ChooChoo, a train network AI

Post by Michiel »

I think I fixed it! Even managed to upload a new version to BaNaNas :)
User avatar
Redirect Left
Tycoon
Tycoon
Posts: 7238
Joined: 22 Jan 2005 19:31
Location: Wakefield, West Yorkshire

Re: ChooChoo, a train network AI

Post by Redirect Left »

You indeed appear to have fixed it. I've run a game on fast forward, i just did a few minor routes to keep me afloat during the period and not go bankrupt. Your AI built a proper network, instead of doing a junction, then crashing trying to find a path.
Huge thanks! I had a look but couldn't really find the issue, or find any reason why it suddenly stopped working as it worked flawlessly several versions of openTTD ago.
Attachments
Nelson & Co., 2004-09-09.png
success!
(389.95 KiB) Not downloaded yet
Image
Need some good tested AI? - Unofficial AI Tester, list of good stuff & thread is here.
User avatar
Michiel
Transport Coordinator
Transport Coordinator
Posts: 338
Joined: 13 Jul 2008 00:57
Contact:

Re: ChooChoo, a train network AI

Post by Michiel »

Thanks for the test run!
User avatar
Redirect Left
Tycoon
Tycoon
Posts: 7238
Joined: 22 Jan 2005 19:31
Location: Wakefield, West Yorkshire

Re: ChooChoo, a train network AI

Post by Redirect Left »

As it turns out, it does still error, just takes a lot longer to reach that place.
Attachments
Nelson & Co., 2051-01-06.png
(357.17 KiB) Not downloaded yet
Image
Need some good tested AI? - Unofficial AI Tester, list of good stuff & thread is here.
User avatar
Michiel
Transport Coordinator
Transport Coordinator
Posts: 338
Joined: 13 Jul 2008 00:57
Contact:

Re: ChooChoo, a train network AI

Post by Michiel »

Bugger! OK, lemme check... Looks like I was using a similar "too expensive" Valuate call in three more places. I've applied the same fix there. Updated in BaNaNas!
User avatar
Michiel
Transport Coordinator
Transport Coordinator
Posts: 338
Joined: 13 Jul 2008 00:57
Contact:

Re: ChooChoo, a train network AI

Post by Michiel »

Gonna let it run for a while to see if anything else pops up. Thanks for the bug report!
User avatar
Michiel
Transport Coordinator
Transport Coordinator
Posts: 338
Joined: 13 Jul 2008 00:57
Contact:

Re: ChooChoo, a train network AI

Post by Michiel »

Looks like it's back in business!

Image
Attachments
choochoo-412.png
Map after 30 years
(110.06 KiB) Not downloaded yet
xarick
Transport Coordinator
Transport Coordinator
Posts: 336
Joined: 26 Feb 2015 00:52

Re: ChooChoo, a train network AI

Post by xarick »

ChooChoo crash.

I post the log text file, a save before crash and a save after crash.
dbg: [script] [0] [W] 2081-05-18: Selling: XT0D39065
dbg: [script] [0] [W] 2081-05-18: Selling: XT0D75452
dbg: [script] [0] [W] 2081-05-18: Selling: XT0D134797
dbg: [script] [0] Your script made an error: the index 'find' does not exist
dbg: [script] [0]
dbg: [script] [0] *FUNCTION [Cull()] choochoo-412\manager.nut line [51]
dbg: [script] [0] *FUNCTION [HandleEvents()] choochoo-412\main.nut line [146]
dbg: [script] [0] *FUNCTION [Start()] choochoo-412\main.nut line [62]
dbg: [script] [0]
dbg: [script] [0] [name] NULL
dbg: [script] [0] [vehicle] 11
dbg: [script] [0] [this] INSTANCE
dbg: [script] [0] [vehicle] 11
dbg: [script] [0] [S] [converted] INSTANCE
dbg: [script] [0] [S] [e] INSTANCE
dbg: [script] [0] [S] [this] INSTANCE
dbg: [script] [0] [S] [year] 2080
dbg: [script] [0] [S] [minMoney] 0
dbg: [script] [0] [S] [this] INSTANCE
dbg: [script] The script died unexpectedly.
One of the running scripts crashed. Please report this to the script author with a screenshot of the AI/Game Script Debug Window
Attachments
openttdCore3log.txt
(20.79 KiB) Downloaded 260 times
ChooChoo, 1st May 2081.sav
(376.7 KiB) Downloaded 265 times
ChooChoo, 1st Jun 2081.sav
(376.24 KiB) Downloaded 254 times
Formerly known as Samu
xarick
Transport Coordinator
Transport Coordinator
Posts: 336
Joined: 26 Feb 2015 00:52

Re: ChooChoo, a train network AI

Post by xarick »

the index 'find' does not exist. OpenTTD 1.8.0
Attachments
Unnamed, 1981-08-18.sav
(4.76 MiB) Downloaded 223 times
Unnamed, 1981-08-20.png
(165.02 KiB) Not downloaded yet
Formerly known as Samu
Baldy's Boss
Tycoon
Tycoon
Posts: 1396
Joined: 23 Feb 2014 22:02

Re: ChooChoo, a train network AI

Post by Baldy's Boss »

The Trudhattan & Frinford R.R. has just been offered a chance to purchase a ChooChoo (at times its most formidable competitor) in bankruptcy...and on looking it over,it appears that your manager Mr. Gordon has invested heavily into monorails in the early 1950s,which may seem forward-looking to him but has plunged the company deeply into debt.These track tiles are useless to the company when its only tracked vehicles are profitable steam engines.Perhaps my NewGRF choices caused this but if it can't use tracks maybe there's a way to make it not buy them?
Attachments
Trudhattan&FrinfordR.R.Co.,19thDec1952.sav
(4.13 MiB) Downloaded 227 times
_Steffl_
Engineer
Engineer
Posts: 7
Joined: 07 Oct 2016 04:14

Re: ChooChoo, a train network AI

Post by _Steffl_ »

Half the rail=half the cost. Every artifical intelligent manager knows this. 8)

ChooChoo would have built the second rail soon, but unfortunately aliens kidnapped a train full of valuables and it had not enough money to pay the bank for the lost value. That's why the bank sold the company to you. I analysed the savegame, it's true.
Post Reply

Return to “OpenTTD AIs and Game Scripts”

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot] and 8 guests