Pathfinding

Archived discussions related to Transport Empire. Read-only access only.
Locked
Kyaputen Harokku
Engineer
Engineer
Posts: 75
Joined: 12 Sep 2004 09:36
Location: Germany

Pathfinding

Post by Kyaputen Harokku »

For pathfinding, one could think of the tracks and roads as a graph. Every crossing/junction, waypoint and station or depot etc. is a node and the roads/rails between them are the edges. You get a directed, weighted graph with positive edge weights. For the pathfinding, you could then use Dijkstra's algorithm. Pseudocode:

Input:
- G=(V,E) Graph, V is the set of Nodes, E is the set of edges, whereas v in V is one single node and (x,y) in E is an edge between nodes x and y (in V).
- w(x,y) in Q+ the edge weights (ie: a value calculated from the distance of x and y and the max speed), Q+ is the set of positive rational numbers.
- s in V: the start node
- z in V: destination node

variables:
- d(v) for v in V is the distance of v from s (ie the sum of the w(x,y) for every edge you have to go from s to v)
- p(v) for v in V is the predecessor of v in the current shortest path (ie where you come from)
- Q: a priority queue of the nodes, priority function is the distance d(v), enqueuing a node causes it to be sorted into the queue so the node with the smallest d(v) is the first and the one with the biggest d(v) is the last element, dequeuing will give you the v with the lowest d(v), and if d(v) of a node changes, the queue will be re-sorted
- S: a stack

Code: Select all

Set d(s) to 0, Set p(s) to NULL
for all (v in V, v!=s) set d(v)=infinity (or a really big value never appearing in the game) and set p(v) to NULL
enqueue s in Q
while Q is not empty
  dequeue x from Q (is the node with the lowest d)
  for all (y where (x,y) in E)
    if d(x)+w(x,y)<d(y) then
      set p(y) to x
      set d(y) to d(x)+w(x,y)
      if y not in Q then
        enqueue y
      else
        re-sort Q
      end if
    end if
  end for
end while

set y to z

while y not NULL
  push y to S
  set y to p(y)
end while

return S
You then get the stack S back. If you pop one element after another, you will get the path of waypoints and switches the vehicle will have to take.

The edge weights can be given dynamically: every piece of track has a maximum speed depending on bridge type and curve radius. Plus, every train has a max speed. So, if a train has calculated a route with the above algorithm, it could temporarily set the max speed of all track pieces with a faster max speed than the train's to the max speed of the train. You would then achieve the following:

You have two tracks from city A to city B. You let a mail train haul mail from A to B, the mail train has a max speed of 120 km/h. And the same time, a high speed passenger train departs from A heading for B. Let's say, the slow train departs first and takes track 1 which is a bit shorter than track 2. In Locomotion, the fast passenger train would also choose the shorter track 1 although it is actually faster on track 2 due to its faster speed 'cause it doesn't have to wait for the slow train. With this algorithm above the slow train would set the max speed of all tracks to 120km/h and thus increase the edge weights on track 1 so the edge weights on track 2 will be smaller and the high speed train will take the slightly longer track 2.

To optimize it a bit (Dijkstra can get very very long paths while calculating, ie if you want to calculate the shortest track from Halle to Leipzig (it is approx. 30 km) Dijkstra could temporarily get a path going via Paris and Rome and shorten it with every new run), you could first do a modified depth-first-search beginning from node s and with depth 1 and increasing the depth until the "Aufspannender Teilgraph" (don't know this in English) contains the destination node. Then you only have to do a Dijkstra in this part of the graph which would be WAY faster. You could store this graph in every train so you don't have to recalculate it every time. You only need to recalculate it if you change your track layout or the route of the train/bus/etc.
Thanks to the cheaters, Multiplayer in ottd sucks.
User avatar
Zuu
OpenTTD Developer
OpenTTD Developer
Posts: 4553
Joined: 09 Jun 2003 18:21
Location: /home/sweden

Post by Zuu »

I havent read your whole post yet. I think your not the first to think about that topic. In a not to far feature we will probably choose "Destinations & pathfinding" as featured topic.

List of future, past and current topic for featured discussion can be found at:
http://tt2.sourceforge.net/wiki/index.php/Discussion

Its no that I dont want to discuss this, but I'll wait until it becomes a featured discussion. Advanced topic = takes time to get into.
My OpenTTD contributions (AIs, Game Scripts, patches, OpenTTD Auto Updater, and some sprites)
Junctioneer (a traffic intersection simulator)
Kyaputen Harokku
Engineer
Engineer
Posts: 75
Joined: 12 Sep 2004 09:36
Location: Germany

Post by Kyaputen Harokku »

It is no problem :) It was just an idea I had.
Thanks to the cheaters, Multiplayer in ottd sucks.
Hellfire
Transport Empire Developer
Transport Empire Developer
Posts: 699
Joined: 03 Feb 2003 09:30
Location: Back at the office

Post by Hellfire »

Dijkstra's shortest path algorithm is a really nice and correct algorithm, but there are many algorithms that are a lot faster. (Coming form a student in Eindhoven, where Dijkstra was a teacher, this means a lot ;))

For example, the A* algorithm uses heuristics to perform a better selection on the nodes for which the shortest path will be calculated.

Derived from A* are also a lot of other algorithms, which we also should take into consideration:
- IDA*
- RBFS
- MA*
- SMA*

When the time is right, we'll (or maybe you'll?) make some benchmarks, from which we will select the best (fastest, correct!) algorithm.

Oh, and by the way: the abstract version of Dijkstra's algorithm is much easier to understand. ;)

Topic locked, please continu the discussion here (13112006).
Feel free to contact me over Email! My current timezone: Europe/Amsterdam (GMT+1 or GMT+2)

Code: Select all

+------------Oo.------+
| Transport Empire -> |
+---------------------+
[ General TE Discussion ] [ TE Development ] [ TE Coding ]
Under construction...
Locked

Return to “Transport Empire Development Archive”

Who is online

Users browsing this forum: No registered users and 39 guests