NoAI Branch - An AI Framework

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

TrueBrain
OpenTTD Developer
OpenTTD Developer
Posts: 1370
Joined: 31 May 2004 09:21

Re: NoAI Branch - An AI Framework

Post by TrueBrain »

reylas wrote:Another question about IsWithinTownInfluence. If I use AIStation.IsWithinTownInfluence, it works. But If I use AITile.IsWithinTownInfluence, I get an index IsWithinTownInfluence does not exist.

Is that a bug or am I using it wrong.

Area.Validate(AITile.IsWithinTownInfluence,current_town);

Thanks,
MarkS
Well, it should work, so the most logic reason I can think up: are you using the latest binaries? As that function is added not too long ago, so please make sure you have the latest binary (r13418, http://nightly.openttd.org/noai/files/).
The only thing necessary for the triumph of evil is for good men to do nothing.
Finaldeath
Engineer
Engineer
Posts: 72
Joined: 09 Apr 2006 23:49
Location: UK
Contact:

Re: NoAI Branch - An AI Framework

Post by Finaldeath »

Worked on my AI, I suspect outdated binaries - it's assuming it's a variable name (with the dot, it looks like a list identifier of sorts).
Finaldeath
User avatar
paullb
Traffic Manager
Traffic Manager
Posts: 129
Joined: 19 May 2008 13:11

Re: NoAI Branch - An AI Framework

Post by paullb »

Question about AIOrder.AppendOrder. I want to have two order flags like below (which has the wrong syntax;

What is the correct syntax for more than one order flag? (like below, non-stop and full-load


AIOrder.AppendOrder(new_bus, this.stationOne.tile, [ AIOrder.AIOF_FULL_LOAD , AIOrder.AIOF_NON_STOP_INTERMEDIATE ] );
Roujin
Tycoon
Tycoon
Posts: 1884
Joined: 08 Apr 2007 04:07

Re: NoAI Branch - An AI Framework

Post by Roujin »

Well, if it works how flags normally work, you should be able to combine them with a binary or.

Code: Select all

FLAG1 | FLAG2
edit: yes, confirmed. so in your case

Code: Select all

AIOrder.AppendOrder(new_bus, this.stationOne.tile, AIOrder.AIOF_FULL_LOAD | AIOrder.AIOF_NON_STOP_INTERMEDIATE );
* @Belugas wonders what is worst... a mom or a wife...
<Lakie> Well, they do the same thing but the code is different.

______________
My patches
check my wiki page (sticky button) for a complete list

ImageImage
ImageImageImageImageImageImageImage
User avatar
paullb
Traffic Manager
Traffic Manager
Posts: 129
Joined: 19 May 2008 13:11

Re: NoAI Branch - An AI Framework

Post by paullb »

Roujin wrote:Well, if it works how flags normally work, you should be able to combine them with a binary or.

Code: Select all

FLAG1 | FLAG2
edit: yes, confirmed. so in your case

Code: Select all

AIOrder.AppendOrder(new_bus, this.stationOne.tile, AIOrder.AIOF_FULL_LOAD | AIOrder.AIOF_NON_STOP_INTERMEDIATE );
I thought I had tried that, but I guess I must have made a mistake, thanks for confirming.
reylas
Engineer
Engineer
Posts: 52
Joined: 22 Dec 2007 01:04

Re: NoAI Branch - An AI Framework

Post by reylas »

TrueLight wrote:
reylas wrote:Another question about IsWithinTownInfluence. If I use AIStation.IsWithinTownInfluence, it works. But If I use AITile.IsWithinTownInfluence, I get an index IsWithinTownInfluence does not exist.

Is that a bug or am I using it wrong.

Area.Validate(AITile.IsWithinTownInfluence,current_town);

Thanks,
MarkS
Well, it should work, so the most logic reason I can think up: are you using the latest binaries? As that function is added not too long ago, so please make sure you have the latest binary (r13418, http://nightly.openttd.org/noai/files/).
Ok, that was it. I was using an old binary. I know where to check now, so I will check that first.

Next question. Is it possible to copy a List from one variable to another? I am building two TownLists then running some Valuators on them. I noticed that except for one Valuator, they are the same. In order not to run my custom function more than once, I would like to run it on one list, then copy that list to another variable like this:

local Town1 = AITownList();
local Town2 = AITownList();

(some Valuators on Town1)

Town2 = Town1;

This fails with no error (openTTD just goes into la la land). I have tried both with and without the declaration of Town2. What am I doing wrong?

Thanks,
MarkS.
User avatar
Ralph
Engineer
Engineer
Posts: 87
Joined: 21 Jun 2004 15:25

Re: NoAI Branch - An AI Framework

Post by Ralph »

Ok, here she is, RalphAI v1.0. At the moment it builds a bus network between towns, then sits there and rakes in the cash when they are all connected. Developed with NoAI r13326.

I am releasing this now mostly to force me to tidy up some bugs, and give a nice starting point for building further. I don't think there are any bugs that will crash the script, but you never know.

All code here is GPL, so feel free to poke/prod or add it to the wiki (though my binary heap/pathfinder are slooooow).

Known bugs:
-Will sometimes not complete a route when it encounters its own road, not sure why.

Todo:
-Think of an awesome name for my computerised transport overlord .
-Improve road pathfinding to favour existing roads and avoid unnecessary slopes/corners, add bridge/tunnel support.
-Adjust route selection to get a faster start, also play better with itself/other AIs/humans.
-Check for vehicles not making any money.
-Check for lots of passengers waiting at a station.
-Repay loan.
Attachments
RalphsAI.zip
RalphsAI v1.0
(16.57 KiB) Downloaded 132 times
User avatar
Ralph
Engineer
Engineer
Posts: 87
Joined: 21 Jun 2004 15:25

Re: NoAI Branch - An AI Framework

Post by Ralph »

reylas wrote:
Next question. Is it possible to copy a List from one variable to another? I am building two TownLists then running some Valuators on them. I noticed that except for one Valuator, they are the same. In order not to run my custom function more than once, I would like to run it on one list, then copy that list to another variable like this:

local Town1 = AITownList();
local Town2 = AITownList();

(some Valuators on Town1)

Town2 = Town1;

This fails with no error (openTTD just goes into la la land). I have tried both with and without the declaration of Town2. What am I doing wrong?

Thanks,
MarkS.
This may work (not tested)

Code: Select all


local oldlist = AITownList();

oldlist.Valuate(blah blah blah);
oldlist.keepValue(blah);

local newlist = AIList();
newlist.AddList(oldlist);
wilco_moerman
Engineer
Engineer
Posts: 70
Joined: 05 Jun 2008 15:51

Re: NoAI Branch - An AI Framework

Post by wilco_moerman »

Hi, I am trying to create an AI for the TJIP contest, and I was wondering whether the NOAI framework allows writing to/reading from files? And if so, if someone could give me a short example?
Nunc dimittis servum tuum Domine secundum verbum tuum in pace
Yexo
Tycoon
Tycoon
Posts: 3663
Joined: 20 Dec 2007 12:49

Re: NoAI Branch - An AI Framework

Post by Yexo »

It doesn't allow that. The only thing you can write text to is de console (and the ingame ai debug panel). You can do that by using AILog.Info/Warning/Error.
wilco_moerman
Engineer
Engineer
Posts: 70
Joined: 05 Jun 2008 15:51

Re: NoAI Branch - An AI Framework

Post by wilco_moerman »

Yexo wrote:It doesn't allow that. The only thing you can write text to is de console (and the ingame ai debug panel). You can do that by using AILog.Info/Warning/Error.
thanks for the reply. I guess I'll just have to use the script mechanism to output the console to a file, and copy-paste from there to my code.

In case you were wondering: I want to tune my AI by learning values for some of the decisions, and it would have been nice if I could just write them to some file and load them when needed.
Nunc dimittis servum tuum Domine secundum verbum tuum in pace
TrueBrain
OpenTTD Developer
OpenTTD Developer
Posts: 1370
Joined: 31 May 2004 09:21

Re: NoAI Branch - An AI Framework

Post by TrueBrain »

As of today (and in the nightly binary which is compiled in 2 hours), we proudly introduce to you: a library system. To give a bit insight information:

2 days ago we had a long talk how we should introduce a pathfinder in an 'extra' package, as I described in a few posts earlier. After some debating we came to the conclusion that there is absolutely no fair way to do that in C++. Although a pathfinder would operate faster in C++, the difference isn't big enough. So we decided to make a library framework in Squirrel, where people can put their general libraries, and an easy way to access them. I just committed exactly that. In the coming days / weeks / months, we will be adding some things we think is useful, under which some queues, but also a basic pathfinder, and more of such tools all AIs will want to use.

Some positive things about this library system: it doesn't force you to use any of them. You can always write your own. It allows multiple implementations of the same thing, like more than one pathfinder. It is portable. It is secure.

Now how does it work? I will write a wiki page about it soon. Basically:

in bin/ai/library there are dirs, 'categories'. In a category there is a dir, the library itself. In that dir is a file called 'library.nut', similar to 'info.nut'. It gives some basic information about the library, and is loaded on OpenTTD startup. The idea is equal to 'info.nut' in all ways, but you don't do: RegisterAI, you do: RegisterLibrary. Easy as pie ;)

Now from your AI you can run:

Code: Select all

import("categorie.name", "LocalName", version);
Mind that it is 'import', not 'require'. You use 'require' to load your own local files.

The LocalName is how you want to class to be called in your script. This is useful, as there will be multiple implementation of the same thing. By changing one line, you can switch between those implementations, without changing everything in your AI. Also, it avoids a lot of name collision, and other problems of that order.

The version part makes sure you talk to library you expect. Say you made your AI for sets.priority_queue version 1, but in the library-dir of a user is version 2. Your AI will now fail to load on his machine, as there might be a mismatch in operations. This is an early problem detection, which hopefully avoids a lot of problems. So, make sure to fill in the right version ;)

I think that sums it up pretty well.. any questions, feel free to ask. Sorry that I had to post this in this topic again, as it is becoming unreadable, but we are trying to get our own NoAI subforum, which hopefully makes this easier on all of us ;) (http://www.tt-forums.net/viewtopic.php?f=21&t=37891)

Ps: if any of you have a library you think should be included as official library (in SVN), please send it to me. Sending in any library for inclusion means you give up the rights of the library, and it becomes GPL copyrighted to OpenTTD.
The only thing necessary for the triumph of evil is for good men to do nothing.
User avatar
Zutty
Director
Director
Posts: 565
Joined: 22 Jan 2008 16:33

Re: NoAI Branch - An AI Framework

Post by Zutty »

TrueLight wrote:As of today (and in the nightly binary which is compiled in 2 hours), we proudly introduce to you: a library system. To give a bit insight information:

2 days ago we had a long talk how we should introduce a pathfinder in an 'extra' package, as I described in a few posts earlier. After some debating we came to the conclusion that there is absolutely no fair way to do that in C++. Although a pathfinder would operate faster in C++, the difference isn't big enough. So we decided to make a library framework in Squirrel, where people can put their general libraries, and an easy way to access them. I just committed exactly that. In the coming days / weeks / months, we will be adding some things we think is useful, under which some queues, but also a basic pathfinder, and more of such tools all AIs will want to use.

Some positive things about this library system: it doesn't force you to use any of them. You can always write your own. It allows multiple implementations of the same thing, like more than one pathfinder. It is portable. It is secure.

Now how does it work? I will write a wiki page about it soon. Basically:

in bin/ai/library there are dirs, 'categories'. In a category there is a dir, the library itself. In that dir is a file called 'library.nut', similar to 'info.nut'. It gives some basic information about the library, and is loaded on OpenTTD startup. The idea is equal to 'info.nut' in all ways, but you don't do: RegisterAI, you do: RegisterLibrary. Easy as pie ;)

Now from your AI you can run:

Code: Select all

import("categorie.name", "LocalName", version);
Mind that it is 'import', not 'require'. You use 'require' to load your own local files.

The LocalName is how you want to class to be called in your script. This is useful, as there will be multiple implementation of the same thing. By changing one line, you can switch between those implementations, without changing everything in your AI. Also, it avoids a lot of name collision, and other problems of that order.

The version part makes sure you talk to library you expect. Say you made your AI for sets.priority_queue version 1, but in the library-dir of a user is version 2. Your AI will now fail to load on his machine, as there might be a mismatch in operations. This is an early problem detection, which hopefully avoids a lot of problems. So, make sure to fill in the right version ;)

I think that sums it up pretty well.. any questions, feel free to ask. Sorry that I had to post this in this topic again, as it is becoming unreadable, but we are trying to get our own NoAI subforum, which hopefully makes this easier on all of us ;) (http://www.tt-forums.net/viewtopic.php?f=21&t=37891)

Ps: if any of you have a library you think should be included as official library (in SVN), please send it to me. Sending in any library for inclusion means you give up the rights of the library, and it becomes GPL copyrighted to OpenTTD.
Nice! I'm glad I never got around to actully posting the code now!! I'm going to need a little time to re-work the stuff I had planned to out in there then.
PathZilla - A networking AI - Now with tram support.
User avatar
Zutty
Director
Director
Posts: 565
Joined: 22 Jan 2008 16:33

Re: NoAI Branch - An AI Framework

Post by Zutty »

Just a quick one for the devs... I'm getting inconsistent results from AITile.IsBuildable() on coast tiles. This really feels like a bug to me, as the result is unpredictable. Mostly it returns false for any land tile that borders with water, but sometimes it returns true for no apparent reason. There can be a long stretch of coast tiles in a line, and one out of twenty will be buildable at random!

Ideally I'd like IsBuildable() to return true for coast tiles with nothing on them (it would be helpful for bridge building), but just having consistent results will be a start!

Is this a bug? Can we have it fixed? Is it already fixed?! (I'm still using r13326)

Thanks.


As for libraries, I've only just noticed that there are standard libs already. To be perfectly honest I don't know what the value will be in having 20 separate binary heap implementations made available, so I probably won't bother posting my own code anymore! Plus I'm not going to re-engineer my pathfinder to use the library code at this stage, so there wouldn't be much point in me posting my pathfinder either (and frankly, A* is A* regardless of who wrote it!!). I might post some other parts of my AI in the library format (maybe some of the graph stuff I'm working on) if people are interested, but that'll suit me for now. You'd all probably be horrified by my Java-esque style of coding anyway!! ;) I use camelCase for variables and ExtremelyExplicitMethodNames().
PathZilla - A networking AI - Now with tram support.
wilco_moerman
Engineer
Engineer
Posts: 70
Joined: 05 Jun 2008 15:51

Re: NoAI Branch - An AI Framework

Post by wilco_moerman »

I noticed that the game directly crashes if you try to reload your AI in r13442.

The same happened in r13355 but only after a few "reloads" (in the same game). Also if the "newgame" command was issued on the console then the game crashed after "start_ai ....".

(Is this the correct place to post this kind of stuff?)
Nunc dimittis servum tuum Domine secundum verbum tuum in pace
User avatar
Zutty
Director
Director
Posts: 565
Joined: 22 Jan 2008 16:33

Re: NoAI Branch - An AI Framework

Post by Zutty »

wilco_moerman wrote:I noticed that the game directly crashes if you try to reload your AI in r13442.

The same happened in r13355 but only after a few "reloads" (in the same game). If the "newgame" command was issued on the console then the game crashed when "start_ai ...." was used.

(Is this the correct place to post this kind of stuff?)
You can report bugs here. I'd report my issue above, but I'm not sure if its expected behaviour or not!
PathZilla - A networking AI - Now with tram support.
Yexo
Tycoon
Tycoon
Posts: 3663
Joined: 20 Dec 2007 12:49

Re: NoAI Branch - An AI Framework

Post by Yexo »

wilco_moerman wrote:I noticed that the game directly crashes if you try to reload your AI in r13442.

The same happened in r13355 but only after a few "reloads" (in the same game). Also if the "newgame" command was issued on the console then the game crashed after "start_ai ....".

(Is this the correct place to post this kind of stuff?)
As Zutty pointed out, you can report it on flyspray. However, if you post it here it'll get read. About the first bug: get the lastest version (can you compile yourself?) and try if the game still crashes.

The issue about start_ai after a new_game command is known and was fixed in r13454. Thanks for reporting and please keep posting any bugs here or on flyspray. If you want a fast reaction or follow latest development, irc is a good place (#openttd.noai @OFTC).
wilco_moerman
Engineer
Engineer
Posts: 70
Joined: 05 Jun 2008 15:51

Re: NoAI Branch - An AI Framework

Post by wilco_moerman »

Yexo wrote:
wilco_moerman wrote:I noticed that the game directly crashes if you try to reload your AI in r13442.

The same happened in r13355 but only after a few "reloads" (in the same game). Also if the "newgame" command was issued on the console then the game crashed after "start_ai ....".

(Is this the correct place to post this kind of stuff?)
As Zutty pointed out, you can report it on flyspray. However, if you post it here it'll get read. About the first bug: get the lastest version (can you compile yourself?) and try if the game still crashes.
I can't compile here, but I thought I was already using the latest NOAI-binaries. I have tried it with r13442 (latest) and two older ones. All of them have the same error.
The issue about start_ai after a new_game command is known and was fixed in r13454.
OK, any idea when it will apear on http://nightly.openttd.org/noai/scoreboard.php? Because that is still r13442 instead of r13454
Thanks for reporting and please keep posting any bugs here or on flyspray. If you want a fast reaction or follow latest development, irc is a good place (#openttd.noai @OFTC).
maybe I'll do that.


I wanted to start a server with restart_game_year = 2007 and starting in 1998 (for the TJIP challenge) and just let it run all night for testing. But if the game crashes after each "newgame" that won't work.

I am trying to implement an AI using a neural network, but networks need a lot of runs to train. That is why I need to start new games and save and load data. The network needs to be stored and loaded between each run. The saving works by using the console to output a script. I'll try to do the loading by modifying the .nut-files automatically using some Java-programming. I'll just get the parts I need from the console-output and substitute them into the .nut-file.

It might be ugly, but as far as I can see it's the only way to transfer information between successive AI's.
Nunc dimittis servum tuum Domine secundum verbum tuum in pace
TrueBrain
OpenTTD Developer
OpenTTD Developer
Posts: 1370
Joined: 31 May 2004 09:21

Re: NoAI Branch - An AI Framework

Post by TrueBrain »

wilco_moerman wrote: (..)

OK, any idea when it will apear on http://nightly.openttd.org/noai/scoreboard.php? Because that is still r13442 instead of r13454
Nightlies run every night at 1800 CEST ... and the commits are made after that ;) So check back in ... 10 minutes :p
wilco_moerman wrote:
Thanks for reporting and please keep posting any bugs here or on flyspray. If you want a fast reaction or follow latest development, irc is a good place (#openttd.noai @OFTC).
maybe I'll do that.
Avoid FlySpray, I don't read that :p I hope to have a subforum soon, then this will get a bit more organized ;)
wilco_moerman wrote: I wanted to start a server with restart_game_year = 2007 and starting in 1998 (for the TJIP challenge) and just let it run all night for testing. But if the game crashes after each "newgame" that won't work.

I am trying to implement an AI using a neural network, but networks need a lot of runs to train. That is why I need to start new games and save and load data. The network needs to be stored and loaded between each run. The saving works by using the console to output a script. I'll try to do the loading by modifying the .nut-files automatically using some Java-programming. I'll just get the parts I need from the console-output and substitute them into the .nut-file.

It might be ugly, but as far as I can see it's the only way to transfer information between successive AI's.
It most likely is :) Good luck with using a NN for this problem ... I wonder if it ever leads to anything :) Can't wait to see if it does :)
The only thing necessary for the triumph of evil is for good men to do nothing.
wilco_moerman
Engineer
Engineer
Posts: 70
Joined: 05 Jun 2008 15:51

Re: NoAI Branch - An AI Framework

Post by wilco_moerman »

TrueLight wrote:
wilco_moerman wrote: (..)

OK, any idea when it will apear on http://nightly.openttd.org/noai/scoreboard.php? Because that is still r13442 instead of r13454
Nightlies run every night at 1800 CEST ... and the commits are made after that ;) So check back in ... 10 minutes :p
I'm counting the seconds :)

TrueLight wrote: Avoid FlySpray, I don't read that :p I hope to have a subforum soon, then this will get a bit more organized ;)
yeah I read the topic on that. Seems only logical.

TrueLight wrote: It most likely is :)
at least as long as NOAI doesn't implement any file IO :?
TrueLight wrote: Good luck with using a NN for this problem ... I wonder if it ever leads to anything :) Can't wait to see if it does :)
I'll let you know. I have successfully used a NN together with reinforcement learning for my master's thesis, so I thought I would give it a try.
Nunc dimittis servum tuum Domine secundum verbum tuum in pace
Locked

Return to “OpenTTD AIs and Game Scripts”

Who is online

Users browsing this forum: No registered users and 6 guests