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

Locked
User avatar
Ralph
Engineer
Engineer
Posts: 87
Joined: 21 Jun 2004 15:25

Re: NoAI Branch - An AI Framework

Post by Ralph »

paullb wrote:In order to prevent doubling up of roads and whatnot (and for checking if a route has been broken) it would be really cool to have a function to check if any two tiles are already connected by roads. This would allow one to use a competitiors roads if they have already buillt it.

The current function, AIRoad::AreRoadTilesConnected, only checks if two adjactent tiles are connected.
I designed my pathfinder such that it can be extended with different methods for returning adjacent nodes and their respective costs, so I can create two:

A) one that only traverses roads that are already there using AreRoadTilesConnected.

B) one that returns a route to build a road on. (this can assign a lower cost to existing roads if you want, to prevent excessive road building)

If A finds a path, I can get the length and compare it to the result of B, if A is not much larger than B, we use the existing road, if its much larger, it would be worth building a new road.
User avatar
paullb
Traffic Manager
Traffic Manager
Posts: 129
Joined: 19 May 2008 13:11

Re: NoAI Branch - An AI Framework

Post by paullb »

TrueLight wrote: The NoAI API is a low-level API, which allows simplified access to OpenTTD internals,

...
The only valuable option left for such functions, and also for a pathfinder for that matter, is a so called '3rdparty' / 'contrib' / 'external'. We are not sure about the name yet. This will be a set of functions on top of the NoAI API which gives you a possible way to do things all AI makers will do. Like pathfinding. You can't live without it. So we supply one, or maybe several, via a clear interface, not part of the NoAI API, but part of something 'extra' around it. This way you are free to choice; you can either use it, or not at all and create your own. But never this will be part of the NoAI API, as it doesn't belong in any API (under the terms of being ambiguous). We are supplying functions to create an AI, not functions to be an AI.
Thank you for the considered reply.

When looking at it in the way that you explain, it makes sense and I would agree with that assessment.

What sort of timeframe till thise "3rd party" code snippits become available (or a framework for them)?

In the interim, it would be cool if there was some place where we could exchange code so that we all don't wind up re-inventing the wheel for those same functions that we all need. (I'll leave it up to you for where the best place is)
User avatar
Zephyris
Tycoon
Tycoon
Posts: 2890
Joined: 16 May 2007 16:59

Re: NoAI Branch - An AI Framework

Post by Zephyris »

So your saying the noAI API will stay as is, but a "noAI API API" will be created? Sounds good to me :D
TrueBrain
OpenTTD Developer
OpenTTD Developer
Posts: 1370
Joined: 31 May 2004 09:21

Re: NoAI Branch - An AI Framework

Post by TrueBrain »

paullb wrote:
In the interim, it would be cool if there was some place where we could exchange code so that we all don't wind up re-inventing the wheel for those same functions that we all need. (I'll leave it up to you for where the best place is)
Well, I leave that up to you guys ;) The wiki is a nice place, this forum a bit less nice place, and if required/need-for, we can always make some http place somewhere.. you are the AI programmers here, we just supply tools ;)
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:
paullb wrote:
In the interim, it would be cool if there was some place where we could exchange code so that we all don't wind up re-inventing the wheel for those same functions that we all need. (I'll leave it up to you for where the best place is)
Well, I leave that up to you guys ;) The wiki is a nice place, this forum a bit less nice place, and if required/need-for, we can always make some http place somewhere.. you are the AI programmers here, we just supply tools ;)
The Wiki seems like as good a place as any to me, since we have that setup and ready to use right now!

I did actually thought it might be a good idea to share code on the Wiki for some of the more difficult stuff a while ago, but there might be some issues...
  • Licensing (Must it all be GPL'd?)
  • Coding standards and style (not just agreeing on a style but sticking to it!)
  • Drift from the HEAD of the branch (e.g. API changes, Squirrel language updates, etc...)
  • Bugs and/or flaws - If a piece of code has a bug, it means lots of people's AIs could be broken
  • Pre-requisites - People can't be expected to make their code 100% modular, and complex bits of code are bound to rely on other stuff from the same author
  • People might not want to share ideas until they are "ready"
Still though, a good idea.

What do you think of a "AI:Library" page for the AI section? There could be a different section for each type of algorithm or code snippet, and then each developer could add their own take on the problem, in a standard format. Im thinking something like, authors name, a short description, maybe some stats, and a link to a page with the full details and the actual code.

Thoughts?
PathZilla - A networking AI - Now with tram support.
User avatar
YukonRob
Transport Coordinator
Transport Coordinator
Posts: 290
Joined: 31 Jul 2007 00:58
Location: North of 63

Re: NoAI Branch - An AI Framework

Post by YukonRob »

Again with the questions! Can someone please explain the uses of AIList() vs AIAbstractList() preferably with examples.
Cheers,
Rob

@TrueLight or TrueBrain - does Doxygen get updated immediately or after next nightly compile?
User avatar
Ralph
Engineer
Engineer
Posts: 87
Joined: 21 Jun 2004 15:25

Re: NoAI Branch - An AI Framework

Post by Ralph »

YukonRob wrote:Again with the questions! Can someone please explain the uses of AIList() vs AIAbstractList() preferably with examples.
Cheers,
Rob
Just quicky, I have to run, code below from memory but should work.

They are a list of key/value pairs that can be sorted and used with valuators tonarrow down a list, or select specific items. Some of the extended classes come pre-populated. e.g

Code: Select all

local townlist = AITownList(); //townlist now contains all the towns on the map, and the key to each item is the townindex

townlist.Valuate(AITown.GetPopulation);//set the value of each list item to its population.

townlist.Sort(townlist.SORT_BY_VALUE,false) //Sort so the max population is at the start of the list. 

local max_pop_town = townlist.Begin(); // set max_pop_town to the key of the first item in the list.

townlist.RemoveTop(1); // Remove this town from the list.

townlist.Valuate(AITown.GetDistanceManhattanToTile, AITown.GetLocation(max_pop_town)); // set the value of each town to the distance between it and the max population town.

townlist.Sort(townlist.SORT_BY_VALUE,false); //Sort so the closest town is at the top.

local nearest_town = townlist.Begin(); // get the closest town.

So now you have the townID of the highest population town on the map, and the town closest to it.
TrueBrain
OpenTTD Developer
OpenTTD Developer
Posts: 1370
Joined: 31 May 2004 09:21

Re: NoAI Branch - An AI Framework

Post by TrueBrain »

YukonRob wrote:Again with the questions! Can someone please explain the uses of AIList() vs AIAbstractList() preferably with examples.
Cheers,
Rob

@TrueLight or TrueBrain - does Doxygen get updated immediately or after next nightly compile?
AIList is what you use inside your code, AIAbstractList is the underlying code for all NNNList. So some things you find in the documentation under AIAbstractList, some under AIList, but you either use AIList or any other list, but never AIAbstractList in your code :)

Doxygen is updated when ever I feel like it, mostly directly after a commit.
The only thing necessary for the triumph of evil is for good men to do nothing.
User avatar
YukonRob
Transport Coordinator
Transport Coordinator
Posts: 290
Joined: 31 Jul 2007 00:58
Location: North of 63

Re: NoAI Branch - An AI Framework

Post by YukonRob »

Thanks Ralph and TrueLight - I needed examples. I am still trying to get the hang of this language style (OO?) it's a far cry from my Fortran days.
I was already having problems with the following and your example seems to tell me I'm on the right track but it doesn't work:

Code: Select all

local industryList = AIIndustryList_CargoProducing(Index); //Index is 1 in this case for coal
	industryList.Valuate(AIIndustry.GetProduction,Index); //Sorts by coal production
	industryList.KeepTop(1); //Get top coal producer
	industryList.Valuate(AIIndustry.GetLocation); //Should revaluate list with tileindex instead of production 
	AILog.Info("Location: " + AIMap.GetTileX(industryList.Begin())+","+AIMap.GetTileY(industryList.Begin())); //Should print out location of industry
When I run this I get a location like 23,0 vs (in this case) the real location of 567,109.

I don't want to clutter this thread with newbie problems (although hopefully I will be able to put a 'Newbie examples of Squirrel programming ' page on the wiki) - are there any mentors out there I can PM?
User avatar
GeekToo
Tycoon
Tycoon
Posts: 961
Joined: 03 Jun 2007 22:22

Re: NoAI Branch - An AI Framework

Post by GeekToo »

industryList.Begin() returns the industry id, not the tileindex I think

leave this away : industryList.Valuate(AIIndustry.GetLocation);

and change industryList.Begin() in the last line to
AIIndustry.GetLocation(industryList.Begin() )

Should work better I think. Did not try it though.
User avatar
YukonRob
Transport Coordinator
Transport Coordinator
Posts: 290
Joined: 31 Jul 2007 00:58
Location: North of 63

Re: NoAI Branch - An AI Framework

Post by YukonRob »

Thanks GeekToo. Your solution works. Based on my new found knowledge I also found an alternative if you need the 2nd valuate, which I didn't:

Code: Select all

local industryList = AIIndustryList_CargoProducing(Index);
	industryList.Valuate(AIIndustry.GetProduction,Index);
	industryList.KeepTop(1);
	industryList.Valuate(AIIndustry.GetLocation);
	AILog.Info("Location: " + AIMap.GetTileX(industryList.GetValue(industryList.Begin()))+","+AIMap.GetTileY(industryList.GetValue(industryList.Begin())));
User avatar
AndersI
Tycoon
Tycoon
Posts: 1732
Joined: 19 Apr 2004 20:09
Location: Sweden
Contact:

Re: NoAI Branch - An AI Framework

Post by AndersI »

TrueLight wrote:So we supply one, or maybe several, via a clear interface, not part of the NoAI API, but part of something 'extra' around it.
This can be both good and bad. The availability of the routines is good, the non-API:ness is bad. How will the users see/realize the difference? Where is the responsibility?

If you want many people trying their hands on an AI, you should supply 'official' routines that help them getting started. They will be able to get *something* going, and have more time left for the tactics. When they realize they need something 'better' they have a working framework to see the 'betterness' in. Of course, if you want every AI creator to spend lots and lots of time reinventing existing wheels, you shouldn't supply any routines at all...

My own experience from cEvo is that trying to micro-manage every detail by yourself in an AI when the main game is a moving target just scares everyone away from AI development. For each new version some of the AI developers give up, and sooner or later everything stops and there is only the game-supplied AI left that knows all the current rules.

Let the kernel supply usable (but not optimal) routines for many of the AI chores, and you get more AI:s, you get more variation for the game players.

Have routines for IsConnected, ConnectedDistance, ConnectionCost, CreateConnection etc. and it's up to the AI developer which granularity they will use in the arguments sent to these functions.

All IMO, of course.
TrueBrain
OpenTTD Developer
OpenTTD Developer
Posts: 1370
Joined: 31 May 2004 09:21

Re: NoAI Branch - An AI Framework

Post by TrueBrain »

AndersI wrote:
TrueLight wrote:So we supply one, or maybe several, via a clear interface, not part of the NoAI API, but part of something 'extra' around it.
(..)

If you want many people trying their hands on an AI, you should supply 'official' routines that help them getting started.
Either you cannot read, or you just don't get it. If you read carefully: _we_ supply one. So WE, as DEVELOPER of the NoAI framework. I think that makes it pretty official. But you should not forget the NoAI framework is very much work in progress. We are just getting done with creating a basic API, which allows the things needed the most. Now it is up to you guys to create things around that, feed that back to us, so we can feed it back into the framework as a whole, but not as part of the API. But I spend enough words explaining that.
AndersI wrote: when the main game is a moving target
OpenTTD is not a moving target. Else any attempt to make a AI framework would have been useless from the start. Any API that changes ever N days is useless. An AI created 1 year ago should run (give or take) just as good as one just created. If not, it is the wrong game to create an AI API for. OpenTTD lucky enough allows to create such a stable API, and besides some initial badness, we believe there is now an API that is stable enough to last for a few months.
The only thing necessary for the triumph of evil is for good men to do nothing.
TrueBrain
OpenTTD Developer
OpenTTD Developer
Posts: 1370
Joined: 31 May 2004 09:21

Re: NoAI Branch - An AI Framework

Post by TrueBrain »

Zutty wrote: The Wiki seems like as good a place as any to me, since we have that setup and ready to use right now!
So go upload some nice libs ;)
Zutty wrote: I did actually thought it might be a good idea to share code on the Wiki for some of the more difficult stuff a while ago, but there might be some issues...
  • Licensing (Must it all be GPL'd?)
In my opinion, all code that is published should be GPL, as the game itself is GPL. For sure any code you want inside the NoAI API you should give up as GPL.
Zutty wrote: [*]Coding standards and style (not just agreeing on a style but sticking to it!)
Make a nice wiki page out of it :)
Zutty wrote: [*]Drift from the HEAD of the branch (e.g. API changes, Squirrel language updates, etc...)
I don't get thisone?
Zutty wrote: [*]Bugs and/or flaws - If a piece of code has a bug, it means lots of people's AIs could be broken
The page the lib is posted give a Talk page, which allows bug-reporting and stuff :)
Zutty wrote: [*]Pre-requisites - People can't be expected to make their code 100% modular, and complex bits of code are bound to rely on other stuff from the same author
Well, you can make a simple system to check if a given lib is loaded .. and we might be able to generalize that inside the framework. Although Squirrel allows enough room to solve that within Squirrel :)
Zutty wrote: [*]People might not want to share ideas until they are "ready"[/list]
Personally, I dislike people who think like that. It is more often useful to share your ideas beforehand, for 2 good reasons:
1) You might not finish it. Other people might continue where you couldn't (or didn't want to)
2) People spot mistakes sooner, which might avoid a complete rewrite or what ever
Zutty wrote:
Still though, a good idea.

What do you think of a "AI:Library" page for the AI section? There could be a different section for each type of algorithm or code snippet, and then each developer could add their own take on the problem, in a standard format. Im thinking something like, authors name, a short description, maybe some stats, and a link to a page with the full details and the actual code.

Thoughts?
Sounds like a plan! Make a draft, and we see how it goes :)
The only thing necessary for the triumph of evil is for good men to do nothing.
User avatar
AndersI
Tycoon
Tycoon
Posts: 1732
Joined: 19 Apr 2004 20:09
Location: Sweden
Contact:

Re: NoAI Branch - An AI Framework

Post by AndersI »

TrueLight wrote:Either you cannot read, or you just don't get it.
Thank you for the nice words! My interest in trying my hands on a NoAI AI just dropped quite a bit.
TrueBrain
OpenTTD Developer
OpenTTD Developer
Posts: 1370
Joined: 31 May 2004 09:21

Re: NoAI Branch - An AI Framework

Post by TrueBrain »

AndersI wrote:
TrueLight wrote:Either you cannot read, or you just don't get it.
Thank you for the nice words! My interest in trying my hands on a NoAI AI just dropped quite a bit.
Feeling insulted is more easy than reading the positive in words :) (the problem of written words, with the lack of non-verbal communication) It was written down more abrupt than meant. I was merely making my point. So please forgive my rudeness, and try to find the positive in the words :) As I had no intention in insulting you in any way.
The only thing necessary for the triumph of evil is for good men to do nothing.
reylas
Engineer
Engineer
Posts: 52
Joined: 22 Dec 2007 01:04

Re: NoAI Branch - An AI Framework

Post by reylas »

Ok, so I have tried my hands at an AI and was more than happy to watch my little AI build a functioning bus network. I tried to implement an inner-city bus route and have been decently successful. My AI makes money over the long haul. I am trying to emulate the way *I* play which is very different from the AI's I have seen so far. Next I will try my hand at connecting two cities.

So back to the point, first congratulations on a very easy to use API. I have only done asp.net programming since college, and was able to make an AI with very little effort. It is not as intelligent as other AI's, but it does make money. It has made playing OpenTTD much more fun.

Second, a question. Is it just me or is GetBankBalance() returning half of what is the actual bank balance? I did not see it explained in the Wiki, so I found out by trial and error. If so, is there a reason why in case it matters?

Third, another question. When I check a tile for IsWithinTownInfluence (TownID town_id, TileIndex tile), if two towns are really close together, my busdepot builder will build a depot in the other town. I know why the tile is being checked, (I check so many tiles in the area around a city) but I would think that IsWithinTownInfluence (TownID town_id, TileIndex tile) would return false if I am closer to another city. The reason why I am asking is in case I am not understanding what IsWithinTownInfluence actually does.

Thanks again for a great little API. I have *wasted* several nights at home building my little innercity bus builder.. It is ugly as can be and probably needs A LOT of work, but I will gladly show it if anyone wants.

Thanks,
MarkS.
Rubidium
OpenTTD Developer
OpenTTD Developer
Posts: 3815
Joined: 09 Feb 2006 19:15

Re: NoAI Branch - An AI Framework

Post by Rubidium »

That's because you are using a currency that is not the English Pound when you play the game. The AI will always get the costs and such in English Pounds as that is the least work for everybody. What use is it to have all money related values multiplied by 2?
reylas
Engineer
Engineer
Posts: 52
Joined: 22 Dec 2007 01:04

Re: NoAI Branch - An AI Framework

Post by reylas »

Oh, cool. I understand completely. It totally escaped me that it would be Pounds. I use Dollars.

So, the reason being was that I was trying to do something like - If I have more than 50,000 dollars, then look to build. I showed 70,000 dollars in the game screen, but the AI function showed ~35000. So my function would not look to build even though the company info screen showed way more.

May I ask what the conversion rate is the game uses? Is it exactly double?

Sorry for such basic questions.

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

Re: NoAI Branch - An AI Framework

Post by TrueBrain »

reylas wrote: Third, another question. When I check a tile for IsWithinTownInfluence (TownID town_id, TileIndex tile), if two towns are really close together, my busdepot builder will build a depot in the other town. I know why the tile is being checked, (I check so many tiles in the area around a city) but I would think that IsWithinTownInfluence (TownID town_id, TileIndex tile) would return false if I am closer to another city. The reason why I am asking is in case I am not understanding what IsWithinTownInfluence actually does.
Yeah, this is tricky/hard/annoying. This function just checks if the tile is within the influence of the town. Basically, if you remove a tree on the tile, you will loose rating in this town. But ... it doesn't guarantee that the road you are on is the town you checked for.

And the worst part of it all, there is no function that does what you expected this function to do. Game-wise, it is not possible. A human sees which tile belongs to which town, but game-wise, nothing indicates which tile 'belongs' to which town. Even more if 2 towns grown completely into each other, it is very impossible to distinguish between them, and any distinguish is a human interpretation of the facts :)

So what you need, is some kind of pathfinder, who checks if the places you want to build stuff on, are connected to each other.. road-wise that is. Much more you can't do. Sorry :)
The only thing necessary for the triumph of evil is for good men to do nothing.
Locked

Return to “OpenTTD AIs and Game Scripts”

Who is online

Users browsing this forum: No registered users and 26 guests