Algorithms for NoAI (Railroads)

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

Post Reply
lhrios
Engineer
Engineer
Posts: 22
Joined: 28 Dec 2008 00:16

Algorithms for NoAI (Railroads)

Post by lhrios »

Hi. My name is Luis and I am a Computer Science student. Since 06/2008 I have been working in my graduation project that is related to OpenTDD AI. The main goal was the improvement of two activities: railroad station construction and railroad tracks deployment. This improvement consists of implementing in these functions a behavior similar to expert human player’s behavior.

The results were algorithms that can help AI programmers. Four main functions were created: IndustryLandAllocation, CityLandAllocation, IndustryRailroadStationConstruction and RailroadConstruction.

The first two are very similar. Their parameters are:
  • Height
  • Width
  • Validate Land: a function that receives the land position and return a number from 0 to 1 (float) representing the evaluation. If 0 then the land won’t be allocated.
  • Maximum cost: the maximum value that can be expended doing terraforming.
  • Coverage maximum distance: the maximum distance coverage by the station. It is just used on IndustryLandAllocation.
  • Swap Height and Width: indicates that Height and Width can be swapped.
  • Allocated land: output the dimensions and location of allocated land.
  • Cost priority: a number from 0 to 1 (float) representing the importance of the cost during the process of allocation. If 0 then cost has no importance. The cost is used together with the value returned by Validate Land function.
  • Purchase land: say to the function if the land must be purchased.
  • Industry / City: the industry or city near where the land must be allocated.
These functions can be used to create airport and railroad station since they need large flatted land areas. It tries do minimize the cost (of terraforming) and maximize user's evaluation of that position.

The function IndustryRailroadStationConstruction uses IndustryLandAllocation to allocate the land that will be used to construct the station. The stations are always constructed in pairs because the orientation is very important. The idea here is to choose a orientation that will help railroad track deployment. Its parameters are:
  • Source industry
  • Destiny industry
  • Number of tracks
  • Plataform length
  • Railroad track type
The stations are created in such way that trains enter by one side and go away by other side. The signals are always compatible with railroad track’s signals constructed by RailroadConstruction.

RailroadConstruction uses A* algorithm to create a two way railroad tracks. Here was used an abstraction based on railroad parts. These parts were created to incase perfectly and form a singed two way railroad compatible with the stations. The most important result is that the algorithm uses terraform to create better tracks (similar to tracks created by human players).

I know that you’re doing a new framework for AI. I developed all this work using C/C++ OpenTTD (0.61) internal functions but the focus of this work was the algorithms that can be implemented using any language. I think these algorithms could be part of the NoAI so that programmers can easily do some common tasks.

So, i want to know if this work can help OpenTTD project.

I can send the source code if it’s necessary. The main problem is that the code is “in Portuguese”.

Currently, I am trying to apply some techniques of Operation Resarch on the game. In the future, I will need some help of the community (if possibly) to evaluate the quality of tracks created by algorithms. Because I used abstract concepts (similarity to expert player’s tracks) I will need human’s evaluation.

PS: I attached some screenshots.

Patch

Thanks.
Attachments
ss.rar
Screenshots
(3.5 MiB) Downloaded 457 times
Last edited by lhrios on 07 Jan 2009 00:18, edited 2 times in total.
User avatar
Zuu
OpenTTD Developer
OpenTTD Developer
Posts: 4553
Joined: 09 Jun 2003 18:21
Location: /home/sweden

Re: Algorithms for NoAI (Railroads)

Post by Zuu »

I think your work is impressive and interesting that you have managed to get teraforming into the decision making. Though in one screenshot your algorithm changes the hills so that the trains has to climb 3 levels on 3 tiles, where the terrain previously was so that the train will climb on every second tile making it 6 tiles to climb. You might consider climbing 3 tiles in row more esthetically, but that slows down the trains more than having them climb one tile and then have one or more flat tiles, and then climb again.

Regarding the use of your code, speaking of myself (not involved in NoAI except for writing the first public user-AI almost a year ago) I think ýou will need to translate it into English and then it depends if people are willing to convert it to squirrel or if it becomes up to you to do that too. My guess is that it can find its place as a Squirrel library. The devs has decided to make pathfinding and other such codes in Squirrel so that AIs that uses library code doesn't get an adventage of faster code than those who implement pathfinding etc. themself. (Squirrel is the scripting language that OpenTTD uses, until it gets replaced by Nail (which I think stands for Noai AI Language or NOAi Language). Nail is still under development, and will have almost compatible syntax.)

Edit: I should add that Squirrel syntax is quite similar to C++.
My OpenTTD contributions (AIs, Game Scripts, patches, OpenTTD Auto Updater, and some sprites)
Junctioneer (a traffic intersection simulator)
User avatar
SirkoZ
Tycoon
Tycoon
Posts: 1518
Joined: 06 Mar 2004 23:51
Location: The sunny side of Alps

Re: Algorithms for NoAI (Railroads)

Post by SirkoZ »

Beautiful work, Ihrios!

The hill climbing that Zuu mentions is no problem if of course realistic_acceleration is turned ON in patches and trains (of normal length - up to tile length 6) don't even lose much speed if engine's tractive effort is around 500 units. To achive that T.E. and with that top climbing speed sometimes 2 locomotives have to be used - especially with 10 or 12 wagon trains.

Do you have any patch available to try your improved AI some (from screenshots I suspect you fixed the default AI which is really great)?

Oh and BTW - Welcome to the Forum! :mrgreen:
Yexo
Tycoon
Tycoon
Posts: 3663
Joined: 20 Dec 2007 12:49

Re: Algorithms for NoAI (Railroads)

Post by Yexo »

Very interesting lhrios.

Do you plan on releasing your code or a description of how it works? I'm particulary interested in your CityLandAlocation code, as I found it quite hard to choose proper location in a reasonable time. The code will have to be converted to squirrel/NAIL before it can be used by NoAI AIs, but the conversion from c/c++ to squirrel should be pretty easy as long as all necessary api functions are available.
lhrios
Engineer
Engineer
Posts: 22
Joined: 28 Dec 2008 00:16

Re: Algorithms for NoAI (Railroads)

Post by lhrios »

Zuu wrote:I think your work is impressive and interesting that you have managed to get teraforming into the decision making. Though in one screenshot your algorithm changes the hills so that the trains has to climb 3 levels on 3 tiles, where the terrain previously was so that the train will climb on every second tile making it 6 tiles to climb. You might consider climbing 3 tiles in row more esthetically, but that slows down the trains more than having them climb one tile and then have one or more flat tiles, and then climb again.
You are right. I focused on esthetical aspects. The teacher who supervised me during this work said me the same thing. So, one of the future tasks is the improvement of terraforming decision process.
lhrios
Engineer
Engineer
Posts: 22
Joined: 28 Dec 2008 00:16

Re: Algorithms for NoAI (Railroads)

Post by lhrios »

SirkoZ wrote: Do you have any patch available to try your improved AI some (from screenshots I suspect you fixed the default AI which is really great)?
No, I don't. But I am going to create one. I don't know if I fixed default AI. I just studied these two problems and tried to improve their solutions. This new approach is better because it "thinks" before doing the terraforming. Default AI doesn’t pay to do the terraformings and do them randomly.
lhrios
Engineer
Engineer
Posts: 22
Joined: 28 Dec 2008 00:16

Re: Algorithms for NoAI (Railroads)

Post by lhrios »

Yexo wrote:Do you plan on releasing your code or a description of how it works? I'm particulary interested in your CityLandAlocation code, as I found it quite hard to choose proper location in a reasonable time. The code will have to be converted to squirrel/NAIL before it can be used by NoAI AIs, but the conversion from c/c++ to squirrel should be pretty easy as long as all necessary api functions are available.
First, I will do the patch then I will translate the code to English. The CityLandAlocation function is based on an optimized heuristic trial and error approach. The main difficulty is detecting city limits because they are very irregular unlike industry limits.

Another difficulty is terraforming. The command CmdLevelLand sometimes, when called with DC_QUERY_COST flag, returns success when it will fail during the execution. This occurs because the dependence between neighbor tiles when doing terraformings. I avoided this problem creating my own CmdLevelLand. The main difference is that when it fails it does not charge user. Another solution is the simulation of command execution.

This dependence between neighbor tiles is also a problem during railroad creation. If you consider all terraformings to every tile (and also the neighbor tiles affected) the solution space will increase very much. That’s why I first find a path and then use terraforming just on this path.
User avatar
SirkoZ
Tycoon
Tycoon
Posts: 1518
Joined: 06 Mar 2004 23:51
Location: The sunny side of Alps

Re: Algorithms for NoAI (Railroads)

Post by SirkoZ »

Great!
I'm looking forward to your patch.

Happy new year all! :mrgreen:
lhrios
Engineer
Engineer
Posts: 22
Joined: 28 Dec 2008 00:16

Re: Algorithms for NoAI (Railroads)

Post by lhrios »

Here is the patch as I promised. Some important things:
  • This is not a complete AI. So it will only use the functions I created to construct railroad tracks and to allocate lands.
  • It substitutes default AI.
  • The decision algorithms implemented (decision of which industries and cities will be used) are very simple.
  • I used revision 14776.
Attachments
patch.tar.gz
Patch without whitespace changes.
(47.28 KiB) Downloaded 318 times
Last edited by lhrios on 07 Jan 2009 00:17, edited 1 time in total.
Yexo
Tycoon
Tycoon
Posts: 3663
Joined: 20 Dec 2007 12:49

Re: Algorithms for NoAI (Railroads)

Post by Yexo »

Could you please make a new diff without those whitespace changes? Your current patch is littered with

Code: Select all

-[tab]something
+[space][space][space]something
changes, that don't change anything but that makes it very hard to see what you actually changed.
lhrios
Engineer
Engineer
Posts: 22
Joined: 28 Dec 2008 00:16

Re: Algorithms for NoAI (Railroads)

Post by lhrios »

I was trying to rewrite my code to Squirrel language and found a problem. I did not find a function on NoAI equivalent to CmdLevelLand. The OpenTDD CmdLevelLand has a problem for AIs as I explained here. I think the best solution is to make a copy of map affected part and then try to do the terraforming. If it fails just undo the changes using the copy.

Another suggestion is a mechanism to avoid execution suspension. For example:
lock();
// do something knowing that we won't be suspended until the unlock command.
unlock();
Of course this mechanism must be used carefully.

Thanks.
comp615
Engineer
Engineer
Posts: 7
Joined: 11 Jan 2010 19:58

Re: Algorithms for NoAI (Railroads)

Post by comp615 »

lhrios, I am also working on Geographic Transport Networks, and was looking to use the railroad algorithm you described to try to build them. I see the patch file you posted, but not the original code that I can use. Is this available somewhere? Or do I need to pull it out of the AI you wrote? Thanks and let me know.
User avatar
Lord Aro
Tycoon
Tycoon
Posts: 2369
Joined: 25 Jun 2009 16:42
Location: Location, Location
Contact:

Re: Algorithms for NoAI (Railroads)

Post by Lord Aro »

ahem.

year old topic

*cough*grave-digger*cough* :mrgreen:
AroAI - A really feeble attempt at an AI

It is practically impossible to teach good programming to students that have had a prior exposure to BASIC: as potential programmers they are mentally mutilated beyond hope of regeneration. --Edsger Dijkstra
comp615
Engineer
Engineer
Posts: 7
Joined: 11 Jan 2010 19:58

Re: Algorithms for NoAI (Railroads)

Post by comp615 »

Oh my, your right. I saw January and thought it was this year. Hrmm...well I guess that answers my own question. I'll try to pull it from his AI
Post Reply

Return to “OpenTTD AIs and Game Scripts”

Who is online

Users browsing this forum: No registered users and 32 guests