Road Pathfinder Modifications

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

Yexo
Tycoon
Tycoon
Posts: 3663
Joined: 20 Dec 2007 12:49

Re: Road Pathfinder Modifications

Post by Yexo »

Rubidium wrote:Building bridges can be cheaper than actually building road there. 'newai' got some code to prevent building unnecessary bridges even though it's more expensive.
Only the road pathfinder doesn't check the actual cost, so that can't be the problem here.
Zutty wrote:*sob* :( Sometimes bug fixing drives me to the very brink of insanity.

Yexo can you tell me what max_length_multiplier means?...

Code: Select all

function Road::InitializePath(sources, goals, max_length_multiplier = 0, max_length_offset = 10000)
Is it something to do with making the pathfinder sub-admissable? i.e. that a value > 1 means the RPF prefers faster completion to cheaper cost.
No, _estimate_multiplier is for making it non-perfect. max_length_multiplier is for setting the maximum route length (in tiles) the pathfinder will find. The maximum route the pathfinder will find

Code: Select all

max_route_length (in tiles) = DistanceManhattan(source, goal) * max_length_multiplier + max_length_offset;
Any path longer than max_route_length will not be considered by the pathfinder. This is to prevent searching the complete map if a route is blocked (either the source or the destination is on an unreachable island for example). You can use the defaults (0 and 10000) to not use this feature at all, it'll then behave the same as in the previous version.
User avatar
Zutty
Director
Director
Posts: 565
Joined: 22 Jan 2008 16:33

Re: Road Pathfinder Modifications

Post by Zutty »

Thanks for the replies. :)

Hmmmm.... what on earth am I doing wrong.

As you know I added a feature that forces roads to "snap" to town road layouts by adding cost to any tile not on the grid. The RPF can get around this though by simply bridging over the more expensive tiles.

I have tried to fix this by calculating how many tiles under a bridge would be in violation of the grid but it just wasn't working. So I then gave a penalty for EVERY tile under a bridge, meaning the bridge was more expensive than the equivalent road and it still didn't work. This suggested to me that something else was wrong, but I've no idea what.

Damn.

On the bright side, I really like v4. I'll post my updated modifications soon.
PathZilla - A networking AI - Now with tram support.
User avatar
Zutty
Director
Director
Posts: 565
Joined: 22 Jan 2008 16:33

Re: Road Pathfinder Modifications

Post by Zutty »

Ok here are my changes to the new RPF v4.

Edit: See my later post...
I re-added the cost callbacks, which you seemed to forget AGAIN! ;) I also seems like you forgot to change self to this, which I also fixed.

In order to allow cost callbacks to modify bridge costs I had to re-work the _Cost() method so that there is only one return statement.

For now I'm sticking with your method of getting neighbors 'cos its faster, but I might fall back into my nasty old ways after Ive done some more thorough testing.

Hope this all makes sense, and that someone can perhaps benefit from this. Enjoy :D

Also, I'll be releasing this in v6 of PathZilla, so can aystar v5 go onto BaNaNas if/when you are finished with it Yexo?
Last edited by Zutty on 29 Mar 2009 20:09, edited 1 time in total.
PathZilla - A networking AI - Now with tram support.
Yexo
Tycoon
Tycoon
Posts: 3663
Joined: 20 Dec 2007 12:49

Re: Road Pathfinder Modifications

Post by Yexo »

Zutty wrote:Ok here are my changes to the new RPF v4.
Road_v4.diff
I re-added the cost callbacks, which you seemed to forget AGAIN! ;) I also seems like you forgot to change self to this, which I also fixed.

In order to allow cost callbacks to modify bridge costs I had to re-work the _Cost() method so that there is only one return statement.

For now I'm sticking with your method of getting neighbors 'cos its faster, but I might fall back into my nasty old ways after Ive done some more thorough testing.

Hope this all makes sense, and that someone can perhaps benefit from this. Enjoy :D

Also, I'll be releasing this in v6 of PathZilla, so can aystar v5 go onto BaNaNas if/when you are finished with it Yexo?
You seem to have an older version of pathfinder.road.4. The latest version (which I uploaded in this post: http://www.tt-forums.net/viewtopic.php?p=767157#p767157) already contains the self->this cahnges and also the cost callbacks (though the code is slightly different from yours). I didn't forget them. I'll try to finish it up later this week,if you have no further comments to AyStar.5 I will already upload that one.
User avatar
Zutty
Director
Director
Posts: 565
Joined: 22 Jan 2008 16:33

Re: Road Pathfinder Modifications

Post by Zutty »

Awww damn. How did I miss that. Sorry Yexo I thought I had the latest version. Hang on...

I have no changes to aystar v5.
PathZilla - A networking AI - Now with tram support.
Yexo
Tycoon
Tycoon
Posts: 3663
Joined: 20 Dec 2007 12:49

Re: Road Pathfinder Modifications

Post by Yexo »

Zutty wrote:I have no changes to aystar v5.
Graph.AyStar.5 is now available from bananas.
User avatar
Zutty
Director
Director
Posts: 565
Joined: 22 Jan 2008 16:33

Re: Road Pathfinder Modifications

Post by Zutty »

Yexo wrote:
Zutty wrote:I have no changes to aystar v5.
Graph.AyStar.5 is now available from bananas.
Brilliant thanks :)

Here are my changes to your latest v4 pathfinder.
Road_v4.diff
(4.52 KiB) Downloaded 128 times
Now its just a change to the _Cost() method as I described before, and a slight code style change (which I thought was more inline with the rest of it).

Sorry about the mixup Yexo. I hope I got it right this time! :oops:

Comments?
PathZilla - A networking AI - Now with tram support.
Yexo
Tycoon
Tycoon
Posts: 3663
Joined: 20 Dec 2007 12:49

Re: Road Pathfinder Modifications

Post by Yexo »

Zutty wrote:Comments?
Only that I agree 100% with that diff :). If there are no other comments (and I assume not, since it's been quite some time ago since I uploaded v4 then I'll apply your diff tomorrow and upload the new version then.
User avatar
Zutty
Director
Director
Posts: 565
Joined: 22 Jan 2008 16:33

Re: Road Pathfinder Modifications

Post by Zutty »

Hooray! Thanks Yexo. :)
PathZilla - A networking AI - Now with tram support.
User avatar
fanioz
Transport Coordinator
Transport Coordinator
Posts: 320
Joined: 19 Dec 2008 05:03
Location: Indonesia
Contact:

Re: Road Pathfinder Modifications

Post by fanioz »

Finally...
You've all done a good job. its now give much flexibility how the rpf would find it path. And uploading to bananas is good news too. :bow:
Correct me If I am wrong - PM me if my English was bad :D

**[OpenTTD AI]** Image
***[NewGRF] *** Image
User avatar
fanioz
Transport Coordinator
Transport Coordinator
Posts: 320
Joined: 19 Dec 2008 05:03
Location: Indonesia
Contact:

Re: Road Pathfinder Modifications

Post by fanioz »

Rubidium wrote:Building bridges can be cheaper than actually building road there. 'newai' got some code to prevent building unnecessary bridges even though it's more expensive.
Could not understand with the meaning of 'newai'.
And ever found that highest bridge price is 6 x road tile. CMIIW
Correct me If I am wrong - PM me if my English was bad :D

**[OpenTTD AI]** Image
***[NewGRF] *** Image
User avatar
Zuu
OpenTTD Developer
OpenTTD Developer
Posts: 4553
Joined: 09 Jun 2003 18:21
Location: /home/sweden

Re: Road Pathfinder Modifications

Post by Zuu »

fanioz wrote:Could not understand with the meaning of 'newai'.
Before NoAI, there was a previous new AI attempt in C++ that aimed at creating one new improved AI.
My OpenTTD contributions (AIs, Game Scripts, patches, OpenTTD Auto Updater, and some sprites)
Junctioneer (a traffic intersection simulator)
User avatar
fanioz
Transport Coordinator
Transport Coordinator
Posts: 320
Joined: 19 Dec 2008 05:03
Location: Indonesia
Contact:

Re: Road Pathfinder Modifications

Post by fanioz »

I see.. that mean there are :
- Old AI (native)
- New AI (attempt to rewrite in C++) <-- this what I've missed one
- NoAI ( current AI framework )
---------------
Yexo, would you update http://devs.openttd.org/~yexo/ai_libraries/ too ?
Correct me If I am wrong - PM me if my English was bad :D

**[OpenTTD AI]** Image
***[NewGRF] *** Image
User avatar
Zutty
Director
Director
Posts: 565
Joined: 22 Jan 2008 16:33

Re: Road Pathfinder Modifications

Post by Zutty »

There was also Trolly, which was an attempt to create a tram AI before NoAI.

http://hg.openttd.org/openttd/branches/ ... ai/trolly/

In fact there was even another AI framework written by TrueBrain before NoAI that used some other langauge (not Squirrel), but it never took off! I can't remember what it was called though.

They really want better AI for OpenTTD!
PathZilla - A networking AI - Now with tram support.
Yexo
Tycoon
Tycoon
Posts: 3663
Joined: 20 Dec 2007 12:49

Re: Road Pathfinder Modifications

Post by Yexo »

Zutty wrote:In fact there was even another AI framework written by TrueBrain
See http://libgpmi.org/
User avatar
Zutty
Director
Director
Posts: 565
Joined: 22 Jan 2008 16:33

Re: Road Pathfinder Modifications

Post by Zutty »

Hi again Yexo,

I have a couple of bugs that I wanted to address by modifying the pathfinder again. These aren't so much new features as hacks to get my AI working!

The first problem I had was that the AI cannot find a path through traffic. My old pathfindaer was able to do this, as was my version of the library RPF I released in PathZilla v5, however the curret RPF uses AIRoad.BuildRoad() and so fails with a VEHICLE_IN_THE_WAY error. I get around this with the following hack...
traffic_hack.diff
Apply to the RPF v4 cumulatively with my previous patch
(1.06 KiB) Downloaded 122 times
If a vehicle is in the way it just falls back to CanBuildConnectedRoadPartsHere(), which works since a vehicle in the way implies that a road already exists on at least ONE of the two tiles. This useful mainly for building tram lines over road.

The second issue I am having is that the pathfinder sometimes builds bridges over the goal tile. My AI assumes that the goal tile is buildable (i.e. to put a station on it), so this behaviour messes up that assumption.

I haven't written a fix for this yet, but I'm planning on adding a test to _GetTunnelsBridges() in the for loop to skip an iteration if the goal tile is between cur_node and target. I haven't thought up an efficient way of doing this yet.

I didn't post these to have them considered for the new RPF and to make your life difficult, but just to ask your opinion on these hacks and if you have any ideas. Do you think there are better ways of fixing these issues?

Thanks. :)
PathZilla - A networking AI - Now with tram support.
Yexo
Tycoon
Tycoon
Posts: 3663
Joined: 20 Dec 2007 12:49

Re: Road Pathfinder Modifications

Post by Yexo »

Zutty wrote:Hi again Yexo,

I have a couple of bugs that I wanted to address by modifying the pathfinder again. These aren't so much new features as hacks to get my AI working!
I'm happy you post it now, before I uploaded it to bananas, otherwise we would need another version again :p
The first problem I had was that the AI cannot find a path through traffic. My old pathfindaer was able to do this, as was my version of the library RPF I released in PathZilla v5, however the curret RPF uses AIRoad.BuildRoad() and so fails with a VEHICLE_IN_THE_WAY error. I get around this with the following hack...
traffic_hack.diff
If a vehicle is in the way it just falls back to CanBuildConnectedRoadPartsHere(), which works since a vehicle in the way implies that a road already exists on at least ONE of the two tiles. This useful mainly for building tram lines over road.
Are you sure it's ok to remove can_demolish from there?
The second issue I am having is that the pathfinder sometimes builds bridges over the goal tile. My AI assumes that the goal tile is buildable (i.e. to put a station on it), so this behaviour messes up that assumption.

I haven't written a fix for this yet, but I'm planning on adding a test to _GetTunnelsBridges() in the for loop to skip an iteration if the goal tile is between cur_node and target. I haven't thought up an efficient way of doing this yet.
This is pretty easy to check as the end tile of the bridge is extended starting from the begin tile, so just break out of the loop as soon as you hit a goal tile. We might want to add a seperate array 'no_brigde_above' next to ignored tiles, just so it can also work if a not-yet-build station consists of multiple tiles.
User avatar
Zutty
Director
Director
Posts: 565
Joined: 22 Jan 2008 16:33

Re: Road Pathfinder Modifications

Post by Zutty »

Yexo wrote:Are you sure it's ok to remove can_demolish from there?
I haven't removed anything (have I?). The line is very long ATM, so maybe you didn't see it. I'll break it up to make it more readable.
Yexo wrote:This is pretty easy to check as the end tile of the bridge is extended starting from the begin tile, so just break out of the loop as soon as you hit a goal tile. We might want to add a seperate array 'no_brigde_above' next to ignored tiles, just so it can also work if a not-yet-build station consists of multiple tiles.
Thats a brilliant idea! I'll have a go at that later on.
PathZilla - A networking AI - Now with tram support.
User avatar
fanioz
Transport Coordinator
Transport Coordinator
Posts: 320
Joined: 19 Dec 2008 05:03
Location: Indonesia
Contact:

Re: Road Pathfinder Modifications

Post by fanioz »

I was thinking, that this line is necessary. I'm sorry I can't make a diff file right now. But this line is inside Road::_Cost function.

Reason :: After changed the code style by zutty for cost callback, this function should not add un-necessary cost. :?:

Code: Select all

    if (!AITile.IsBuildable(new_tile) && !AIRoad.IsRoadTile(new_tile) {
        cost += this._cost_demolition;
    }
Proposed revision is :

Code: Select all

    /* Check if need to demolish
     *  1: not demolish road tile
     *  2: not demolish own property
     *  3: the pathfinder is set to allow demolition
     *  4: the tile is demolish-able
     */
    if (!AITile.IsBuildable(new_tile) && !AIRoad.IsRoadTile(new_tile) && this._allow_demolition &&
        !AICompany.IsMine(AITile.GetOwner(new_tile)) && AITile.DemolishTile(new_tile)) {
        cost += this._cost_demolition;
    }
OR, the Road::_Neighbours function handled this well ? :?:
Correct me If I am wrong - PM me if my English was bad :D

**[OpenTTD AI]** Image
***[NewGRF] *** Image
User avatar
Bilbo
Tycoon
Tycoon
Posts: 1710
Joined: 06 Jun 2007 21:07
Location: Czech Republic

Re: Road Pathfinder Modifications

Post by Bilbo »

Zutty wrote: As you know I added a feature that forces roads to "snap" to town road layouts by adding cost to any tile not on the grid. The RPF can get around this though by simply bridging over the more expensive tiles.
Well, there can be problem - if there are two towns close to each other, their road grid can be not aligned. Especially as the towns grow, their grids will clash, causing a little biut of extra road around the line where the growth of the town touches.

How do you solve this situatiuon?
If you need something, do it yourself or it will be never done.

My patches: Extra large maps (1048576 high, 1048576 wide) (FS#1059), Vehicle + Town + Industry console commands (FS#1060), few minor patches (FS#2820, FS#1521, FS#2837, FS#2843), AI debugging facility

Other: Very large ships NewGRF, Bilbo's multiplayer patch pack v5 (for OpenTTD 0.7.3)
Post Reply

Return to “OpenTTD AIs and Game Scripts”

Who is online

Users browsing this forum: Bing [Bot] and 10 guests