Questions about the library road pathfinder (v3)

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
Brumi
President
President
Posts: 920
Joined: 18 Jul 2009 17:54

Questions about the library road pathfinder (v3)

Post by Brumi »

Hi all,
Recently I've been trying to make an AI on my own. My (far-to-achieve) ambition is to make some sort of remake of the old AI, I think that should be quite easy compared to building a really intelligent AI.
I'm a real amateur in programming, I use the library pathfinder according to the guidelines at http://wiki.openttd.org/AI:RoadPathfinder. In fact, I just copied the code. :)
So, my questions:
1) Is there some way to add a penalty for road-rail level crossings without modifying the code of the pathfinder?
2) I noticed that my pathfinder is slow compared to other AIs that use the library pathfinder (for example, Convoy and TransAI). So, is there some way to make the pathfinder faster? (again, without modifying the library code) I don't mind if the pathfinder doesn't find the optimal route.
3) How do you know that the pathinder failed to find a path? Is it OK to add a time limit?
I hope I will be able develop the AI for some alpha-release. Currently it chooses a service completely randomly, builds the two truck stations and then connects them todether. The next step for me is to build and start the vehicles. Of course my biggest problem will be exception handling, currently there is almost none.
Yexo
Tycoon
Tycoon
Posts: 3663
Joined: 20 Dec 2007 12:49

Re: Questions about the library road pathfinder (v3)

Post by Yexo »

Brumi wrote:Hi all,
Recently I've been trying to make an AI on my own. My (far-to-achieve) ambition is to make some sort of remake of the old AI, I think that should be quite easy compared to building a really intelligent AI.
I'm a real amateur in programming, I use the library pathfinder according to the guidelines at http://wiki.openttd.org/AI:RoadPathfinder. In fact, I just copied the code. :)
So, my questions:
1) Is there some way to add a penalty for road-rail level crossings without modifying the code of the pathfinder?
Not yet, but a future version of the road pathfinder library will have such a penalty.
3) How do you know that the pathinder failed to find a path? Is it OK to add a time limit?
The pathfinder returns false if there is more to search and null if there is no path.
I hope I will be able develop the AI for some alpha-release. Currently it chooses a service completely randomly, builds the two truck stations and then connects them todether. The next step for me is to build and start the vehicles. Of course my biggest problem will be exception handling, currently there is almost none.
Good luck with your AI :)
User avatar
Xander
Route Supervisor
Route Supervisor
Posts: 485
Joined: 18 May 2007 12:47
Location: Oxford
Contact:

Re: Questions about the library road pathfinder (v3)

Post by Xander »

Hi Brumi and good to see another AI developer.
Brumi wrote:1) Is there some way to add a penalty for road-rail level crossings without modifying the code of the pathfinder?
Nothing I'm aware of. One thing you could do is ammend your road/rail builder so that when it encounters and existing road or rail it builds a bridge until it's past the offending item. It's not slick, nor very easy, but it's an option.
Brumi wrote:2) I noticed that my pathfinder is slow compared to other AIs that use the library pathfinder (for example, Convoy and TransAI). So, is there some way to make the pathfinder faster? (again, without modifying the library code) I don't mind if the pathfinder doesn't find the optimal route.
If your and other peoples AI are all using the same library your results will be the same. The only way to speed it up is to run shorter distances with it. The other thought is the while loop you enter into whilst path finding. If it's got any extra tasks that would affect the speed of things.
Brumi wrote:3) How do you know that the pathinder failed to find a path? Is it OK to add a time limit?
Wiki wrote:FindPath returns false if it isn't finished with pathfinding. When it's done, it returns either the path it found or null to indicate no route exists.
It's perfectly fine to add a timer. I do the following:

Code: Select all

local counter = 0;
local path = false;
//MAXIMUM_PATH_TRIES is a constant declared in main.nut
while (path == false && counter < MAXIMUM_PATH_TRIES) {
  path = pathfinder.FindPath(100);
  counter++;
  AIController.Sleep(1);
}

if(path == false){
    //Did not find path in time
}
if(path == null){
    //Could not find a path
}
Last edited by Xander on 10 Aug 2009 12:19, edited 1 time in total.
Real Tycoons do it on Trains!

JAMI: Just Another Moronic Intelligence
Yexo
Tycoon
Tycoon
Posts: 3663
Joined: 20 Dec 2007 12:49

Re: Questions about the library road pathfinder (v3)

Post by Yexo »

Xander wrote:It's perfectly fine to add a timer. I do the following:
I hope that's not a copy/paste of your code, since you never update counter.
User avatar
Xander
Route Supervisor
Route Supervisor
Posts: 485
Joined: 18 May 2007 12:47
Location: Oxford
Contact:

Re: Questions about the library road pathfinder (v3)

Post by Xander »

Yexo wrote:
Xander wrote:It's perfectly fine to add a timer. I do the following:
I hope that's not a copy/paste of your code, since you never update counter.
fixed :P
Real Tycoons do it on Trains!

JAMI: Just Another Moronic Intelligence
Brumi
President
President
Posts: 920
Joined: 18 Jul 2009 17:54

Re: Questions about the library road pathfinder (v3)

Post by Brumi »

Thank you both.
Currently I can't promise anything, but I will try my best :) I'll be studying IT from September and I hope that won't take away all my time, instead it should help a lot (I hope we will learn about A*, I'm really interested in that, but the page on Wikipedia is still very high-level for me).
User avatar
Xander
Route Supervisor
Route Supervisor
Posts: 485
Joined: 18 May 2007 12:47
Location: Oxford
Contact:

Re: Questions about the library road pathfinder (v3)

Post by Xander »

Brumi wrote:Thank you both.
Currently I can't promise anything, but I will try my best :) I'll be studying IT from September and I hope that won't take away all my time, instead it should help a lot (I hope we will learn about A*, I'm really interested in that, but the page on Wikipedia is still very high-level for me).
ICT or Computer Science? What level (university or high school)?
Real Tycoons do it on Trains!

JAMI: Just Another Moronic Intelligence
Brumi
President
President
Posts: 920
Joined: 18 Jul 2009 17:54

Re: Questions about the library road pathfinder (v3)

Post by Brumi »

Xander wrote:ICT or Computer Science? What level (university or high school)?
er... I don't know how it translates properly into English, something like 'Information Specialist Engineer', but I'm not sure about the branches. I'll be studying it at university.
User avatar
Xander
Route Supervisor
Route Supervisor
Posts: 485
Joined: 18 May 2007 12:47
Location: Oxford
Contact:

Re: Questions about the library road pathfinder (v3)

Post by Xander »

Ah that's alright then. Over here we have Computer Science - which is programming and such and Information Communication Technology which is stupid.

You'll probably not cover anything as specialised as A* in your first semester but what you will learn are important theories and practices to use when programming :)
Real Tycoons do it on Trains!

JAMI: Just Another Moronic Intelligence
User avatar
fanioz
Transport Coordinator
Transport Coordinator
Posts: 320
Joined: 19 Dec 2008 05:03
Location: Indonesia
Contact:

Re: Questions about the library road pathfinder (v3)

Post by fanioz »

Brumi wrote:1) Is there some way to add a penalty for road-rail level crossings without modifying the code of the pathfinder?

Code: Select all

import("pathfinder.road", "RoadPathFinder", 3);

class MyRoadPF extends RoadPathFinder {}
function MyRoadPF ::_Cost(path, new_tile, new_direction, self)
{
   local cost = ::RoadPathFinder._Cost(path, new_tile, new_direction, self);
   if (AITile.HasTransportType(new_tile, AITile.TRANSPORT_RAIL)) cost += your_cost;
   return cost;
}
then in your old :

Code: Select all

local pathfinder = RoadPathFinder();
replace with :

Code: Select all

local pathfinder = MyRoadPF();
That is what I've used in Trans :lol:
Correct me If I am wrong - PM me if my English was bad :D

**[OpenTTD AI]** Image
***[NewGRF] *** Image
Roujin
Tycoon
Tycoon
Posts: 1884
Joined: 08 Apr 2007 04:07

Re: Questions about the library road pathfinder (v3)

Post by Roujin »

hurray for inheritance :)
* @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
Brumi
President
President
Posts: 920
Joined: 18 Jul 2009 17:54

Re: Questions about the library road pathfinder (v3)

Post by Brumi »

Thank you! :) I've never thought that it is that easy. I'll try it when I have time.
Brumi
President
President
Posts: 920
Joined: 18 Jul 2009 17:54

Re: Questions about the library road pathfinder (v3)

Post by Brumi »

I have a new issue with the road pathfinder:
pathfinder problem.png
pathfinder problem.png (36.07 KiB) Viewed 934 times
I made a 64x64 scenario with a town, a coal mine and a power station. Then I placed that rail line between the industries. AIs using the library pathfinder (e. g. SimpleAI, TransAI, MogulAI) fail to build a road here, with or without Fanioz's little modification to add a penalty for level crossings. It also failed if I didn't modify the default costs.
Any of you has an idea what causes this? ?(

EDIT: If I raise two little hills by the side of the railway, the pathfinder can find a path.
User avatar
fanioz
Transport Coordinator
Transport Coordinator
Posts: 320
Joined: 19 Dec 2008 05:03
Location: Indonesia
Contact:

Re: Questions about the library road pathfinder (v3)

Post by fanioz »

It seems, that is because AITile.IsBuildable return false on each rail tile.
AFAIK, road pf3 has been optimized to build bridge on non flat tiles only. Thats why you got a bridge when you made 'hill'.
And with inheritance too, I beleive you can fix this for your AI firstly :D
Edit:
You need to change the behaviour of Road.Neighbours() and Road.GetTunnelsBridges()
Correct me If I am wrong - PM me if my English was bad :D

**[OpenTTD AI]** Image
***[NewGRF] *** Image
Brumi
President
President
Posts: 920
Joined: 18 Jul 2009 17:54

Re: Questions about the library road pathfinder (v3)

Post by Brumi »

Thank you. :)
The only thing I can say that I agree with Roujin:
Roujin wrote:hurray for inheritance :)
Hephi
Engineer
Engineer
Posts: 72
Joined: 25 Oct 2009 09:56
Location: Belgium

Re: Questions about the library road pathfinder (v3)

Post by Hephi »

I changed the pathfinder library to add an extra cost for building over farm tiles and made a new tar file for it.

But reading this thread I notice I should have used inheritance instead. :) Thanks for the tip!
--------------------------------------------------
MailAI, a casual postal service for openTTD.
--------------------------------------------------
Post Reply

Return to “OpenTTD AIs and Game Scripts”

Who is online

Users browsing this forum: No registered users and 7 guests