Page 1 of 1

Questions about the library road pathfinder (v3)

Posted: 10 Aug 2009 11:57
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.

Re: Questions about the library road pathfinder (v3)

Posted: 10 Aug 2009 12:12
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 :)

Re: Questions about the library road pathfinder (v3)

Posted: 10 Aug 2009 12:15
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
}

Re: Questions about the library road pathfinder (v3)

Posted: 10 Aug 2009 12:19
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.

Re: Questions about the library road pathfinder (v3)

Posted: 10 Aug 2009 12:20
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

Re: Questions about the library road pathfinder (v3)

Posted: 10 Aug 2009 12:29
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).

Re: Questions about the library road pathfinder (v3)

Posted: 10 Aug 2009 12:34
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)?

Re: Questions about the library road pathfinder (v3)

Posted: 10 Aug 2009 12:49
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.

Re: Questions about the library road pathfinder (v3)

Posted: 10 Aug 2009 12:57
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 :)

Re: Questions about the library road pathfinder (v3)

Posted: 10 Aug 2009 16:05
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:

Re: Questions about the library road pathfinder (v3)

Posted: 11 Aug 2009 04:20
by Roujin
hurray for inheritance :)

Re: Questions about the library road pathfinder (v3)

Posted: 11 Aug 2009 09:01
by Brumi
Thank you! :) I've never thought that it is that easy. I'll try it when I have time.

Re: Questions about the library road pathfinder (v3)

Posted: 17 Sep 2009 13:18
by Brumi
I have a new issue with the road pathfinder:
pathfinder problem.png
pathfinder problem.png (36.07 KiB) Viewed 1033 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.

Re: Questions about the library road pathfinder (v3)

Posted: 17 Sep 2009 16:33
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()

Re: Questions about the library road pathfinder (v3)

Posted: 19 Sep 2009 17:30
by Brumi
Thank you. :)
The only thing I can say that I agree with Roujin:
Roujin wrote:hurray for inheritance :)

Re: Questions about the library road pathfinder (v3)

Posted: 30 Oct 2009 10:37
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!