comparative analysis of weighted pathfinding in realistic ...1111201/fulltext01.pdf · pathfinding...

30
INOM EXAMENSARBETE TEKNIK, GRUNDNIVÅ, 15 HP , STOCKHOLM SVERIGE 2017 Comparative Analysis of Weighted Pathfinding in Realistic Environments RICHARD UGGELBERG ANDREAS LUNDBLOM KTH SKOLAN FÖR DATAVETENSKAP OCH KOMMUNIKATION

Upload: others

Post on 25-Aug-2020

23 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Comparative Analysis of Weighted Pathfinding in Realistic ...1111201/FULLTEXT01.pdf · Pathfinding in Realistic Environments RICHARD UGGELBERG ANDREAS LUNDBLOM KTH SKOLAN FÖR DATAVETENSKAP

INOM EXAMENSARBETE TEKNIK,GRUNDNIVÅ, 15 HP

, STOCKHOLM SVERIGE 2017

Comparative Analysis of Weighted Pathfinding in Realistic Environments

RICHARD UGGELBERG

ANDREAS LUNDBLOM

KTHSKOLAN FÖR DATAVETENSKAP OCH KOMMUNIKATION

Page 2: Comparative Analysis of Weighted Pathfinding in Realistic ...1111201/FULLTEXT01.pdf · Pathfinding in Realistic Environments RICHARD UGGELBERG ANDREAS LUNDBLOM KTH SKOLAN FÖR DATAVETENSKAP

Comparative Analysis ofWeighted Pathfinding inRealistic Environments

ANDREAS LUNDBLOM & RICHARD UGGELBERG

Degree Project in Computer Science, DD142XSupervisor: Dilian GurovExaminer: Örjan Ekeberg

CSC, June 17, 2017

Page 3: Comparative Analysis of Weighted Pathfinding in Realistic ...1111201/FULLTEXT01.pdf · Pathfinding in Realistic Environments RICHARD UGGELBERG ANDREAS LUNDBLOM KTH SKOLAN FÖR DATAVETENSKAP

Abstract

The general subject of this thesis concerns different weightings ap-plied to Dijkstra’s pathfinding algorithm to balance performanceversus accuracy in a simulated realistic environment. The algo-rithms used in this study are Dijkstra’s pathfinding algorithm andA*. When evaluating paths in the opposite direction of the goalthe path length is multiplied by a heuristic coefficient. Thus giv-ing preference to the path in the general direction of the goal. Theresults derived from this testing show that for each usage case thatdoes not disregard either path length or computation time thereis a heuristic coefficient for which the pathfinding calculations willrequire the least amount of computation power in relation to theaccuracy of the resulting paths.

Page 4: Comparative Analysis of Weighted Pathfinding in Realistic ...1111201/FULLTEXT01.pdf · Pathfinding in Realistic Environments RICHARD UGGELBERG ANDREAS LUNDBLOM KTH SKOLAN FÖR DATAVETENSKAP

Sammanfattning

Denna avhandling avser påvisa hur olika viktningar applicerad påDijkstras Pathfindingalgoritm balanserar prestanta och precisioni en simulerad realistisk miljö. Algortimerna som används är Di-jkstras och A*. När vägar i motsatt riktning mot målet övervägsmultipliceras dessa med en heuristisk koefficient. Därmed pre-mieras vägar som generellt går rakt mot målet. De härledda re-sultaten påvisar att för varje användningsfall som inte ignorerarvarken prestanta eller precision finns det en heuristisk viktningsko-efficient där minsta möjliga mängd prestanta relativt precisionkrävs.

Page 5: Comparative Analysis of Weighted Pathfinding in Realistic ...1111201/FULLTEXT01.pdf · Pathfinding in Realistic Environments RICHARD UGGELBERG ANDREAS LUNDBLOM KTH SKOLAN FÖR DATAVETENSKAP

Contents1 Introduction 1

1.1 Problem statement . . . . . . . . . . . . . . . . . . . . . 11.2 Scope . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1

2 Background 22.1 Pathfinding . . . . . . . . . . . . . . . . . . . . . . . . . 22.2 Heuristics . . . . . . . . . . . . . . . . . . . . . . . . . . 32.3 Dijkstra’s algorithm . . . . . . . . . . . . . . . . . . . . . 32.4 The A* Algorithm . . . . . . . . . . . . . . . . . . . . . 62.5 Other algorithms . . . . . . . . . . . . . . . . . . . . . . 72.6 Complexity of algorithms using heuristics . . . . . . . . . 8

3 Method 103.1 Realism . . . . . . . . . . . . . . . . . . . . . . . . . . . 103.2 Approach . . . . . . . . . . . . . . . . . . . . . . . . . . 103.3 Environment . . . . . . . . . . . . . . . . . . . . . . . . . 113.4 Initialization . . . . . . . . . . . . . . . . . . . . . . . . . 123.5 Pathfinding phase . . . . . . . . . . . . . . . . . . . . . . 143.6 Traveling phase . . . . . . . . . . . . . . . . . . . . . . . 143.7 Evaluation . . . . . . . . . . . . . . . . . . . . . . . . . . 15

4 Results 164.1 Computation time . . . . . . . . . . . . . . . . . . . . . 164.2 Path length . . . . . . . . . . . . . . . . . . . . . . . . . 164.3 Relation . . . . . . . . . . . . . . . . . . . . . . . . . . . 17

5 Discussion 195.1 Calculation time . . . . . . . . . . . . . . . . . . . . . . 195.2 Accuracy . . . . . . . . . . . . . . . . . . . . . . . . . . . 205.3 Relative importance . . . . . . . . . . . . . . . . . . . . 205.4 Limitations . . . . . . . . . . . . . . . . . . . . . . . . . 205.5 Future research . . . . . . . . . . . . . . . . . . . . . . . 21

6 Conclusion 22

7 References 23

Page 6: Comparative Analysis of Weighted Pathfinding in Realistic ...1111201/FULLTEXT01.pdf · Pathfinding in Realistic Environments RICHARD UGGELBERG ANDREAS LUNDBLOM KTH SKOLAN FÖR DATAVETENSKAP

1 IntroductionPathfinding is a vital component in many ares of IT today. Applicationsare found in video games, where characters have to traverse maps filledwith obstacles, finding the shortest path when routing in networking orsimply finding your way around the real world. There are many areaswhere knowing the shortest distance between two points can be beneficial.

There are many different algorithms that are used for solving pathfindingproblems. Almost all pathfinding algorithms for solving static problemsare based on Dijkstra’s algorithm, especially in games. [4] This algorithmwas created by Dijkstra in 1959 and holds the test of time. [3]

However, calculations in pathfinding can be very demanding to even themost sophisticated computer systems. This is where heuristics becomeuseful. Heuristics supply the algorithm with extra information, that in-tends to speed up the execution. While heuristics might prevent yourpathfinding algorithm from finding the actual shortest path, they willoften provide a dramatic decrease in calculation time. Choosing howmuch the algorithm takes the heuristic into account (how it is weighted)is a very interesting topic, since it can cause an incredible difference inpathfinding performance.

1.1 Problem statement

This report aims to investigate how different weightings of the A* algo-rithm affect the time required to calculate paths, how accurate the pathsbecome and if there is a weighting that always performs the best. Thisis done by answering the following question:

How does weighting affect calculation time and accuracy?

1.2 Scope

The algorithm used will be A*, with heuristic coefficients ranging fromzero (functionally the same as Dijkstra’s Algorithm) to “large” (function-ally the same as greedy best-first search). [8] The heuristic used will benon-admissible, leading to sub-optimal paths but faster execution time.Our simulation will be written in C++.Since our simulation is not a completely general testing environment, allthe results will be specific to our domain. However this does not meanthat any conclusions will be inapplicable to other scenarios.

1

Page 7: Comparative Analysis of Weighted Pathfinding in Realistic ...1111201/FULLTEXT01.pdf · Pathfinding in Realistic Environments RICHARD UGGELBERG ANDREAS LUNDBLOM KTH SKOLAN FÖR DATAVETENSKAP

2 Background

2.1 Pathfinding

Pathfinding is the process of finding a path from point A to point B, usu-ally through some algorithm. This is very simple if there are no obsta-cles or branching pathways, but it quickly increases in complexity whenobstacles are added; obstacles like untraversable locations or divergingpaths. If an algorithm would examine all possible paths or inspect everypossible location in the graph network, the execution would be too slow.Therefore, we would like our algorithm to inspect as few nodes as possi-ble, while still finding the fastest path to the goal.

When finding paths, the input is structured as a graph. Graphs in graphtheory are ordered pairs consisting of a set of vertices, connected by a setof edges (see figure 1). This is usually notated as G = (V,E,ΨG), whereV is the set of vertices , E is the set of edges and ΨG is an incidencefunction that connects the edges to the vertices. [1] When traversing thegraph, you traverse the edges. You can only move through the edgesthat are connected to the vertex where you are currently residing. Thegraphs used and referred to in this paper will always be undirected (bi-directional).

6

4

3

5

2

1

Figure 1: A simple graph, consisting of six vertices and sevenedges.

2

Page 8: Comparative Analysis of Weighted Pathfinding in Realistic ...1111201/FULLTEXT01.pdf · Pathfinding in Realistic Environments RICHARD UGGELBERG ANDREAS LUNDBLOM KTH SKOLAN FÖR DATAVETENSKAP

2.2 Heuristics

Definition of heuristicInvolving or serving as an aid to learning, discovery, or problem-solving by experimental and especially trial-and-error meth-ods.

- Merriam-Webster [7]

Even though this a more general definition of heuristics it is still re-lated to our usage case. When an algorithm uses heuristics it acts oninformation that is either assumed or otherwise irrational. In this con-text, irrational does not mean necessarily bad or false in some way butrather that the assumption is made without data backing it up. Thisconcept is also applicable to other areas of research such as behavioraleconomics as people do not always make rational and informed decisions.

In computer science the definition is a bit more precise and narroweddown. There it refers to a way of circumventing strict algorithm-basedcomputing and thus cut down on calculation time. The easiest way toexplain it is to say that while algorithms operate in known environmentsand on known logical principles heuristics utilize "intelligent guesses" orother operations that are not based on hard numbers or hard data. [12] Inour case this refers to the fact that the shortest path between two pointsgenerally will tend towards a straight line. Thus making the examinationof nodes in the general direction of the goal a more valuable use of timeand resources.

2.3 Dijkstra’s algorithm

Dijkstra’s is the algorithm of choice when it comes to obtaining the short-est path between two points. The algorithm always finds the shortestpath from a start vertex to a goal vertex in a weighted graph. Weightedgraphs have a cost associated with every edge. This can be interpreted assome edges being longer than others, or requiring more time for traversalin some other way.

The algorithm is usually implemented with a priority queue, where ver-tices are pushed to and ordered based on the cost of the connecting edge,or a Fibonacci heap. Fibonacci heaps offer better performance in general,but are far more complex [5].Starting at the vertex S, the algorithm inspects all the vertices connectedto that vertex and pushes them onto the queue. In addition to storing

3

Page 9: Comparative Analysis of Weighted Pathfinding in Realistic ...1111201/FULLTEXT01.pdf · Pathfinding in Realistic Environments RICHARD UGGELBERG ANDREAS LUNDBLOM KTH SKOLAN FÖR DATAVETENSKAP

the cost of the path, we also store how we got to the current vertex.Since we start at S, we add this data alongside our path cost. The al-gorithm continues in this fashion, until we reach the goal. When we findthe goal, we can follow our vertices backwards from the goal and retrievethe path. [3]

Algorithm 1: Dijkstra’s algorithm for finding the shortest pathfrom S to T1 function Dijkstra (G,S, T );Input : A graph G = (V,E), start vertex S, goal vertex TOutput: The shortest path from S to T

2 set Q to vertex set3 for each vertex v in G do4 if v 6= S then5 set v.cost =∞6 set v.previous = null

7 end8 Q.AddWithWeight(v, v.distance)9 end

10 while Q is not empty do11 set u to Q.LowestVertex()12 for each neighbor v of u do13 set a to u.cost + Cost(u,v)14 if v.cost > a then15 set v.previous to u16 set v.cost to a17 Q.DecreaseCost(v, a)18 end19 end20 end21 return Backtrack(Q)

4

Page 10: Comparative Analysis of Weighted Pathfinding in Realistic ...1111201/FULLTEXT01.pdf · Pathfinding in Realistic Environments RICHARD UGGELBERG ANDREAS LUNDBLOM KTH SKOLAN FÖR DATAVETENSKAP

Note that Dijkstra’s algorithm has no sense of direction. The only waythe algorithm chooses its vertices is based on the currently lowest pathcost. This will cause the algorithm to inspect an unnecessary largeamount of edges, and reduce the performance. The solution to thisproblem is to extend the algorithm with a sense of direction, throughheuristics.

Figure 2: First steps of finding a path from a to l. The queue isset up with all distances set to infinity. Node a is expanded andthe distances to d and c is set to 3 and 4.

Figure 3: Since the distance to d is the shortest, it is expanded. ais not chosen since it has already been expanded. The distancesto h and e is set to the length of the edges to the nodes plus thelength to d.

5

Page 11: Comparative Analysis of Weighted Pathfinding in Realistic ...1111201/FULLTEXT01.pdf · Pathfinding in Realistic Environments RICHARD UGGELBERG ANDREAS LUNDBLOM KTH SKOLAN FÖR DATAVETENSKAP

Figure 4: The algorithm keeps expanding to the lowest nodes inthe queue.

Figure 5: The algorithm finally starts moving towards the goal.k is checked twice but it set to the same the distance (the pathsare of equal length). i is expanded and the goal is reached.

2.4 The A* Algorithm

A* (pronounced A-Star) is an extended version of Dijkstra’s algorithm.It is a, so-called, informed search algorithm or best-first search. Thismeans that A* will prioritize the evaluation of possible routes that ap-pear to be the better. This assumption is based on heuristics. A commonheuristic among A* implementations is to prioritize nodes that are in thegeneral direction of the end goal.

An example where this is a clear benefit is when it comes to pathfind-ing is in areas where there very few or no obstacles in the way. In thiscase A* will head straight towards the goal without checking alternativeroutes whereas Dijkstra’s will not reach the goal before it has gone anequal distance down every available path. If the area is relatively clear

6

Page 12: Comparative Analysis of Weighted Pathfinding in Realistic ...1111201/FULLTEXT01.pdf · Pathfinding in Realistic Environments RICHARD UGGELBERG ANDREAS LUNDBLOM KTH SKOLAN FÖR DATAVETENSKAP

then Dijkstra’s will evaluate an entire circle around the starting pointwith a radius equal to the length of the distance from the start vertex tothe goal vertex.

Figure 6: A comparison between the amount of tiles inspected inan A* and a Dijkstra implementation when finding the shortestpath between the green dot (start) and the red dot (goal). Thecolored area represents searched path while the yellow representsthe resulting shortest path. Note the difference in nodes searchedand also the different resulting paths. [13]

The A*-algorithm is almost completely identical to Dijkstra’s. The dif-ference is the heuristic value associated with each vertex. The algorithmselects the best candidate from the priority queue based on the cost ofthe path plus the heuristic value of the vertex. The heuristic value can bebased on anything, but it has to be admissible if it is to be guaranteed tofind the shortest path. This means that the function h(v) that calculatesthe heuristic h of the vertex v cannot overestimate the cost of getting tothe nearest goal node. If the heuristic is not admissible, it will becomean approximation of the shortest path in almost every situation. [11] Onevery common heuristic is the length of the straight line from the currentvertex to the goal vertex. [8]

2.5 Other algorithms

Naturally, Dijkstra’s algorithm and A* are far from the only pathfindingalgorithms out there. However most other tend to find themselves inmore niche areas that have very special requirements. Among the morenotable ones there is D* (pronounced D-Star) and its derivatives. D*

7

Page 13: Comparative Analysis of Weighted Pathfinding in Realistic ...1111201/FULLTEXT01.pdf · Pathfinding in Realistic Environments RICHARD UGGELBERG ANDREAS LUNDBLOM KTH SKOLAN FÖR DATAVETENSKAP

is an incremental search algorithm that differs relatively little from A*.The difference is that D* employs an incremental search strategy. This isused in unknown environments where the entire path from the startingposition to the goal cannot initially be found. Hence an incrementalsearch is used to evaluate adjacent paths as the we go along. This ismore efficient than using A* to calculate new paths all the way to thegoal every time new information about the environment is discovered. [2]

2.6 Complexity of algorithms using heuristics

The asymptotic complexity of Dijkstra’s algorithm may vary between im-plementations. Since A* is based on Dijkstra’s algorithm it too will vary.Furthermore the heuristics applied to A* will add the to the deviation.

In Dijkstra’s paper from 1959 where he described his algorithm the pri-ority queue was implemented with the running time O(|E|+ |V |2) where|E| is the number of edges and |V | is the number of vertices. [3] Imple-mentations invented since then have greatly improved the performancethough. The two main ones are the binary heap and the Fibonacci heap.Their running times are O((|E|+ |V |)log|V |) and O(|E|+ |V |log|V |) re-spectively. [6]

While it might be hard to say something about the average case of arealistic heuristic, the worst case of a perfect heuristic is a lot easier toanalyze. The perfect heuristic is the heuristic that forces the algorithmto disregard all nodes that are not part of the shortest path. Althoughit can be said that we’ll generally have a constant relative error. Thismeans that the total error will deviate from the shortest path by thesame factor as any relative error. [10] The complexity of A* can thus bewritten as such

|h(x)− h∗(x)| = O(log h∗(x)) (1)

where h∗ is the perfect heuristic. [9].

If the heuristic forces the algorithm to move away move the goal in-stead of towards it, we will achieve a worst case. This will cause thealgorithm to expand an exponential amount of nodes which will lead toa time complexity O(bd), where d is the length of the optimal solutionand b is the average amount of children per node. In most cases, however,A* will never perform worse than Dijkstra. [11]

The asymptotic complexity of algorithms utilizing heuristics may be quite

8

Page 14: Comparative Analysis of Weighted Pathfinding in Realistic ...1111201/FULLTEXT01.pdf · Pathfinding in Realistic Environments RICHARD UGGELBERG ANDREAS LUNDBLOM KTH SKOLAN FÖR DATAVETENSKAP

difficult to determine. This is because heuristics as a concept substitutehard cut data and information with guesswork and irrationality. Therunning time is then mainly dependent on the accuracy of the heuristicfunction.

The conclusion of this is that there has to be a balance between ac-curacy and calculation time. It is all dependent on the limiting factor ofthe usage case. Sometimes a small error can be tolerated and sometimesthe minimization of calculation is of greater importance.

9

Page 15: Comparative Analysis of Weighted Pathfinding in Realistic ...1111201/FULLTEXT01.pdf · Pathfinding in Realistic Environments RICHARD UGGELBERG ANDREAS LUNDBLOM KTH SKOLAN FÖR DATAVETENSKAP

3 Method

3.1 Realism

This analysis is mostly focused on the types of pathfinding used in gamedevelopment, where the environments often feature realistic characteris-tics like forests, lakes, cities, and so on. Usually, these maps feature alot of obstacles that the characters on the map have to find their wayaround when traversing the map. This will cause the paths to becomemore complex and thus require more calculation time.

We will try to generate environments that match these conditions, sincetests that match the intended use cases are more useful when discussingheuristics and weighting. Pathfinding is most interesting when the pathfrom A to B is complex.

3.2 Approach

In a realistic environment, the answer to our question is not all that ob-vious. Hence our simulation will incorporate many different aspects thatwill affect the decision making process of the weighted heuristic imple-mentations. These aspects include areas or routes that are easier for ourtravelers to traverse, such as paved roads, which could mean that a longerroute might actually be faster. The environments will also contain areasthat are more difficult to traverse, such as forests and lakes. This furthercomplicates calculations and will make way for more varied heuristic so-lutions. As the shortest path will probably not be a straight line it will beharder to find. And thus a heuristic based on euclidean distance will ig-nore shorter routes that might avoid obstacles in a more efficient manner.

To show the various drawbacks and benefits between different weightingswe will create an application that simulates travelers traveling betweenkey locations. These key locations will simulate cities or towns. Theroutes to the locations will be calculated after each stop. Each travelerwill use different weightings to calculate their routes. Route calculationwill be made more realistic (and therefore more complex) by weightingthe nodes of the graph. Traveling by road is of course faster, and willhave a lower weight associated with the vertices. The weight of the pathwill be the weighted value of the vertex one is traveling to.

10

Page 16: Comparative Analysis of Weighted Pathfinding in Realistic ...1111201/FULLTEXT01.pdf · Pathfinding in Realistic Environments RICHARD UGGELBERG ANDREAS LUNDBLOM KTH SKOLAN FÖR DATAVETENSKAP

Figure 7: This small scale example shows how the different areasmight be implemented on top of a map. The colored squaresrepresent different types areas. For example, drawing a straightline through the woods might be a lot slower than following thepaved road the entire way. But that kind of route might be ignoredwith certain heuristics.

3.3 Environment

For our calculations we’re using a randomly generated environment (map)with certain constraints. This follows a template which shall resemblea realistic environment. These environments are the simulated physicalmaps where we will perform our tests.

The environments that we will generate will feature obstacles that forcethe travelers to move in more complex patterns. Lakes will completelyobstruct their paths, forests will slow them down, and a road networkwill guide them to their goals. These ”random” maps will of course affectthe result, but all environments that are not completely plain have toaffect the result in some way. We strive to achieve the highest level ofrealism possible (and therefore results that are the most relevant) withthe properties of our maps.

11

Page 17: Comparative Analysis of Weighted Pathfinding in Realistic ...1111201/FULLTEXT01.pdf · Pathfinding in Realistic Environments RICHARD UGGELBERG ANDREAS LUNDBLOM KTH SKOLAN FÖR DATAVETENSKAP

Figure 8: An example of a generated world. Forests are green andlakes are blue. All untraversable nodes are bronze colored. Theblacks paths are the road network between the cities.

3.4 Initialization

First of all, the basis of the environment is generated as a grid of nodes.Intuitively, a grid might not seem like the most fitting structure, althoughthis will ensure a more general environment than using a graph or an-other shape as we’re not only interested in the usage of roads and soforth. Instead, roads and other easily traversable areas are representedby a lower difficulty value. The difficulty value is how differences in ter-rain are represented. For example, a road will have a lower value anda dense forest or cliffs will have a higher value. A value of 0 representsuntraversable nodes. Each node is connected to all of its adjacent nodes(making that 8 for all nodes not on the edge of the map). Thus thetravelers are not restricted to the usage of roads or other kinds of prede-termined paths and may choose their own.

Certain nodes represent cities. Between the cities there is a network ofroads. These roads are represented as lines of nodes with low difficultyvalues. To limit the number or roads and thus make the environmentmore varied the roads are generated using an algorithm that creates atree-shaped network. The "tree-shape" here references the trees found ingraph theory, which is an acyclic graph, and not actual trees. This is also

12

Page 18: Comparative Analysis of Weighted Pathfinding in Realistic ...1111201/FULLTEXT01.pdf · Pathfinding in Realistic Environments RICHARD UGGELBERG ANDREAS LUNDBLOM KTH SKOLAN FÖR DATAVETENSKAP

another aspect that renders the environment more realistic or represen-tative of the actual usage cases since usually not all cities in a world areinterconnected. Such a thing would make the simulation uninteresting.

Algorithm 2: Road generation algorithm1 function RoadGen (C);Input : A set of coordinates C of cities

2 while Cities >= 2 do3 set roadStart = ChooseRandomCity(C)4 C.remove(roadStart)5 set roadEnd = ChooseRandomCity(C)6 set Road = AStar(roadStart, roadEnd)7 while Road is not empty do8 set v = Road.pop()9 v.SetToRoad()

10 end11 end

Obstacles and features include the following:

• Forests, represented by an intermediate difficulty value surroundedby similar nodes. (Green in figure 8)

• Lakes, represented by an area of untraversable nodes. (Blue infigure 8)

• Random obstacles, represented by single untraversable nodes. (Bronzein figure 8)

Forest and lakes are generated by randomly choosing locations where anelement of that type is to be generated. The element then grows outwardfrom that point using a recursive, non-deterministic algorithm. Everystep that the element grows outward reduces the chance of it growingbigger. The chance of a vertex being chosen is:

C(s) = e−sk (2)

where s is the current iteration (step) and k is a constant divider. Whenthe first point is chosen, s is 0, which means that the chance is 100%. Alarger k yields larger environments. We have chosen to use larger valuesof k for lakes, and smaller for the forests.Obstacles are simply a dynamic addition to the environment and their

13

Page 19: Comparative Analysis of Weighted Pathfinding in Realistic ...1111201/FULLTEXT01.pdf · Pathfinding in Realistic Environments RICHARD UGGELBERG ANDREAS LUNDBLOM KTH SKOLAN FÖR DATAVETENSKAP

respective representative cases are not set in stone. Hence what we’vereferred to here as forests, lakes etc. are just mere examples of what theenvironmental features might represent.

3.5 Pathfinding phase

The simulation itself is split into two phases: pathfinding and traveling.

The pathfinding phase is where most of the calculations are done. Itall starts with establishing nodes to calculate a path between. All thenodes of the graph are inserted into a priority queue. This data structuresorts the nodes based on their priority (in our case, the distance traveledon the path to get to a certain node). Several threads are used to findseveral different paths (one for each traveler). The number of threads isspecified by the user when starting the simulation. The threads will usedifferent weighting values in their calculations of paths and outputtingtheir respective running times. Thus providing us with the essential datawe need.

The heuristic used is based on Euclidean distance but with differentweighting values that changes how large of a factor the distance to thegoal node is. A very large weighted value will force the algorithm tochoose nodes that are closer to the goal each step of the way, even ifthis eventually results in a longer path. Keep in mind the Dijkstra’s al-gorithm is identical to an A* implementation with a weighting value ofzero.

Example 1 If we have a simulation with 10 threads and a max heuris-tic value of 10 the Thread0 will use a heuristic value of 0 (technicallyequivalent to Dijkstra’s algorithm). Thread1 will use 1, Thread2 will use2 and so on. This means that Thread0 will be using an unaltered versionof Dijkstra’s algorithm while Thread10 will multiply the weight associatedwith nodes in the opposite direction of the goal with 10. Thus showcasinga significant preference to nodes closer to the goal.

3.6 Traveling phase

The traveling phase is the second phase of the simulation. Here simulatedtravelers will travel between different points in the previously created en-vironment, using the paths calculated in the previous phase. The amount

14

Page 20: Comparative Analysis of Weighted Pathfinding in Realistic ...1111201/FULLTEXT01.pdf · Pathfinding in Realistic Environments RICHARD UGGELBERG ANDREAS LUNDBLOM KTH SKOLAN FÖR DATAVETENSKAP

of travelers is equal to the amount of threads and every traveler is as-signed to the path of its corresponding thread i.e. the path calculatedby the thread with that same index.

Every traveler’s path calculation will run on their own thread. Exe-cution time will be monitored and result in a delay before the travelercan move. When this delay is over, the traveler will start moving. Thecumulative computation time and path length (also referred to as "pathweight") will be stored as results and then compared to each other.

While the efficiency of the algorithms is revealed during the calcula-tion process, the accuracy is demonstrated when they are used by thetravelers.

3.7 Evaluation

Both calculation time and travel time will be important as the differencewill showcase the various strengths and weaknesses of the algorithms andtheir weightings. Since there are some cases where calculation time is themost important aspect and accuracy is allowed to suffer, and vice versa,the ratio of the values is subject to change to reflect the specific require-ments of any given situation. The difference in results for the differentrequirements will be showcased in the results section.

All results derived from this study will be domain specific to our environ-ment in terms of the numbers presented. This is because each environ-ment will favor different heuristics. However the conclusions presentedlater can be applied to different environments since the balance betweenperformance and accuracy is still present. It is simply the point of equi-librium that changes with the environment.

Every result is calculated through the generation of a 2048x2048 ran-domized map with the constraints described in the previous section.The environment includes the features of lakes, forests and roads. Inthese tests there were 20 travelers traveling between 40 cities, utilizing aweighting corresponding to their index (see 3.5). The weighting is whatis directing the travelers towards the goal by multiplying the distance ofpath in the opposite direction with the given value.

15

Page 21: Comparative Analysis of Weighted Pathfinding in Realistic ...1111201/FULLTEXT01.pdf · Pathfinding in Realistic Environments RICHARD UGGELBERG ANDREAS LUNDBLOM KTH SKOLAN FÖR DATAVETENSKAP

4 ResultsThis section is reserved for the results of our study. Each result is ac-companied by a brief description.

All values for both computation time and path length are the averagesof 5 consecutive runs.

4.1 Computation time

First we’ll take a look at the differences in computation time in relationto the heuristic value.

Figure 9: The relation between path length and heuristic value

Here we can clearly see that there is a strong correlation between a higherheuristic value and lower computation time. Although there little differ-ence between the calculation times where h(g) > 5 and negligible differ-ence where h(g) > 15.

4.2 Path length

Secondly the length of the resulting path in relation to the heuristicsvalue.

16

Page 22: Comparative Analysis of Weighted Pathfinding in Realistic ...1111201/FULLTEXT01.pdf · Pathfinding in Realistic Environments RICHARD UGGELBERG ANDREAS LUNDBLOM KTH SKOLAN FÖR DATAVETENSKAP

Figure 10: The relation between computation time and heuristicvalue

A dynamic similar to the one found in the section on computation time(see figure 9) is found here too. However, in this case, a higher heuristicvalue clearly results in a longer path.

4.3 Relation

Thirdly we can see that the rate at which the path length gets longer isnot identical to the rate at which the computation time gets longer. Thusto illustrate this relation we took the average ratio between path lengthand computation time and added that to each of the ratios between them.This normalizes the computation time values.

17

Page 23: Comparative Analysis of Weighted Pathfinding in Realistic ...1111201/FULLTEXT01.pdf · Pathfinding in Realistic Environments RICHARD UGGELBERG ANDREAS LUNDBLOM KTH SKOLAN FÖR DATAVETENSKAP

Figure 11: The sum of path length and normalized computationtime for each h(g)

This figure shows a compromise between computation time and pathlength. As both values are ideally low they had to be normalized andadded together. This sum then showcases the heuristic values for whichthe the values provide the most efficient compound. Efficient in thiscontext refers shorter computation time accompanied by shorter pathlength.

18

Page 24: Comparative Analysis of Weighted Pathfinding in Realistic ...1111201/FULLTEXT01.pdf · Pathfinding in Realistic Environments RICHARD UGGELBERG ANDREAS LUNDBLOM KTH SKOLAN FÖR DATAVETENSKAP

5 DiscussionTwo very important aspects to consider when conducting a comparativeanalysis are comparability and repeatability. Comparability is a require-ment since without the elimination of variable factors and the properhomogenization of testing environments a comparative analysis wouldnot be at all comparative. Thus rendering any results void. Repeatabil-ity is also of major importance since differing results nullify each other.This is especially important when considering results provided by testingin a randomized environment. If there is any contradiction in the resultsthen those would have to be accounted for.

To achieve comparability we made sure that the generated environments,while different, were essentially similar in character and used the exactsame constraints. Since the worlds are randomly generated, any layoutis possible. If the locations that are set as targets are right next to eachother, no conclusions can be drawn since there is essentially no marginfor the heuristics to outperform each other. We made sure that no suchworlds were generated. Locations of cities and obstacles shifted betweenthe differ but the foundation remained a similar composition.

To achieve repeatability we ran the simulation several times and av-eraged the results. Thus outmaneuvering any unlucky generations suchas cities in the middle of lakes or clusters of untraversable nodes. Giventhe intuitive nature of the results we can clearly see that no significantdeviation from the intended generation took place.

5.1 Calculation time

As we could see in figure 9 there was a clear inverse relation betweenthe heuristic value h(g) and the computation time. This is because whata higher heuristic value does is to ignore more paths that deviate fromthe euclidean distance. Thus decreasing the amount of nodes to evaluate.

However the resulting graph shows a convergence to a certain calculationtime below which the program will not reach irrespective of heuristic.This is likely due to the map size. In this case the value of convergenceseems to be around 18 seconds. Naturally the time is dependent on themap size and will show variance in relation to that. The convergence,however, will likely remain regardless of map size.

19

Page 25: Comparative Analysis of Weighted Pathfinding in Realistic ...1111201/FULLTEXT01.pdf · Pathfinding in Realistic Environments RICHARD UGGELBERG ANDREAS LUNDBLOM KTH SKOLAN FÖR DATAVETENSKAP

5.2 Accuracy

Similarly to the findings in the section on calculation times, we can alsosee clear trends in figure 10. Path lengths do, very reliably, vary withheuristic value. A higher heuristic value clearly correlates to a longerpath. This result is largely due to the environment. In a less varied en-vironment the similarities between different heuristics (or lack thereof)matter less.

Although a similar dynamic to the previous results can be seen herewhere the value clearly approaches a convergent value, the perceivedconvergence is not as substantial and does not taper off in the samemanner. As the heuristic value gets higher and higher the path will passthrough tougher and tougher terrain and thus result in a longer (or lesstraversable) path.

5.3 Relative importance

Naturally the relative importance between calculation time and accuracyis key when choosing a heuristic value. The choice is clear when eithercalculation time or accuracy is unimportant. When calculation time isunimportant and the actually shortest path must be found using a lowerheuristic value is the obvious choice and vice versa.

As we can see in figure 11 the most efficient heuristic values for caseswhere a compromise between path length and computation time is de-sired is 0 < h(g) < 3. Most likely somewhere around 2 in this case.

This is not a general truth for all applications of pathfinding. Howeverthis proves that there is an efficient compromise between path length andcomputation time were neither values is taken to its extreme. The mostefficient heuristic value will vary depending on a lot of factors relatedto each world on a case-by-case basis. The optimal h(g) = 2 is domainspecific to our simulation and is only applicable in this context.

5.4 Limitations

There are several limitations imposed on this research.First and foremost certain possible optimizations for the pathfinding al-gorithms have not been used due to time constraints. These, would,however, not change relative value of the resulting numbers but merelythe numbers themselves.

20

Page 26: Comparative Analysis of Weighted Pathfinding in Realistic ...1111201/FULLTEXT01.pdf · Pathfinding in Realistic Environments RICHARD UGGELBERG ANDREAS LUNDBLOM KTH SKOLAN FÖR DATAVETENSKAP

Secondly, our analysis of A* only covers heuristics that use the euclideandistance. While this might seem reasonable, it contributes heavily to ourheuristic being non-admissible. This is why the passes using any heuris-tic value above zero is very far from the optimal path. In many casesthis is an acceptable trade-off, since the calculation speed is improvedtremendously, but this is not always the case. The loss of the admissibil-ity of our heuristic limits the usefulness of our result.

Since we opted to use a priority queue instead of a Fibonacci heap, ouranalysis does not cover the top performance of pathfinding algorithms.However, this is not of significant importance, since the target of ouranalysis is the relative performance of the heuristics. If a heuristic valueincreases the speed by 15%, it will be same for all data structures (ap-proximately), since the most significant factors of the performance is thefactors affected by the heuristics.

5.5 Future research

Future research in this area can take many different forms. Most top-ics will, however, have to be determined on a case-by-case basis sinceheuristics in pathfinding is greatly dependent on its specific usage case.Providing general research is therefore difficult. This thesis has a generalfocus on heuristics in a specific type of environment. Many permutationsof pathfinding methods and environment types can be examined and thusthis provides future research with a platitude of options.

An interesting topic for future research is comparing non-admissible andadmissible heuristics. An admissible heuristic for A* will always find theshortest path, just like Dijkstra’s, but a far fewer amount of nodes willbe inspected in comparison. However, ensuring that the heuristic is ad-missible (never overestimates the cost of reaching the goal) can be fairlycostly. Finding ways to lower this cost while still ensuring an optimalresult is very useful in cases where errors are not tolerated.

21

Page 27: Comparative Analysis of Weighted Pathfinding in Realistic ...1111201/FULLTEXT01.pdf · Pathfinding in Realistic Environments RICHARD UGGELBERG ANDREAS LUNDBLOM KTH SKOLAN FÖR DATAVETENSKAP

6 ConclusionThe main focus of this thesis has been to examine the effects of heuristicsin pathfinding. More specifically, how different strengths in heuristics al-ter the balance between computation time and accuracy. As an algorithmwith a higher heuristic value will prefer to pass through difficult areasrather than finding alternative paths the overall computation time willdecrease. This results in a much more difficult path for the traveler.

However, since in a lot of cases there is not a clear preference for thelimitation of either computation time or path length. Ideally both shallbe as small as possible. For this reason we’ve procured a range of valuesfor which the heuristics will provide the most efficiency. This range willvary between usage cases but will always favor a lower end of the rangeabove zero.

Thus, the conclusion is that for each usage case that does not disre-gard either path length or computation time, there is a heuristic valuefor which the pathfinding calculations will offer the least amount of com-putation power in relation to the accuracy of the resulting paths.

22

Page 28: Comparative Analysis of Weighted Pathfinding in Realistic ...1111201/FULLTEXT01.pdf · Pathfinding in Realistic Environments RICHARD UGGELBERG ANDREAS LUNDBLOM KTH SKOLAN FÖR DATAVETENSKAP

7 References

[1] J. A. Bondy and U. S. R. Murty. Graph Theory With Applications.Department of Combinatorics and Optimization, 1976.

[2] H. Choset. Robotic motion planning: A* and d* search.URL: https://www.cs.cmu.edu/ motionplanning/lecture/AppH-astar-dstar_howie.pdf. Visited 2017-04-05.

[3] E. W. Dijsktra. A note on two problems in connexion with graphs.Numerische Mathematlk 1, 269 - 271, 1959.

[4] R. G. et al. Pathfinding in computer games. The ITB Journal, 4:62,2003.

[5] M. L. Fredman and R. E. Tarjan. Algorithms and Data Structures:The Basic Toolbox. Foundations of Computer Science, 25th AnnualSymposium on, 1984.

[6] K. Mehlhorn and P. Sanders. Fibonacci heaps and their uses in im-proved network optimization algorithms. URL: http://people.mpi-inf.mpg.de/ mehlhorn/ftp/Toolbox/ShortestPaths.pdf. Visited2017-03-27.

[7] Merriam-Webster. Definition of heuristic. URL:https://www.merriam-webster.com/dictionary/heuristic. Vis-ited 2017-04-05.

[8] A. Patel. Heuristics. URL:http://theory.stanford.edu/ amitp/GameProgramming/Heuristics.html.Visited 2017-02-18.

[9] J. Pearl. Heuristics: Intelligent Search Strategies for ComputerProblem Solving. Addison-Wesley, 1984.

[10] Robin. Complexity in heuristic search. URL:http://intelligence.worldofcomputing.net/ai-search/complexity-in-heuristic- search.html. Visited 2017-03-27.

[11] S. Russell and P. Norvig. Artificial Intelligence: A Modern Ap-proach. Prentice Hall, 2002.

[12] technopedia. Heuristic programming. URL:https://www.techopedia.com/definition/3811/heuristic-programming. Visited 2017-04-05.

23

Page 29: Comparative Analysis of Weighted Pathfinding in Realistic ...1111201/FULLTEXT01.pdf · Pathfinding in Realistic Environments RICHARD UGGELBERG ANDREAS LUNDBLOM KTH SKOLAN FÖR DATAVETENSKAP

[13] K. Wang. Compare A* with dijkstra algorithm. URL:https://www.youtube.com/watch?v=g024lzsknDo. Visited 2017-02-19.

24

Page 30: Comparative Analysis of Weighted Pathfinding in Realistic ...1111201/FULLTEXT01.pdf · Pathfinding in Realistic Environments RICHARD UGGELBERG ANDREAS LUNDBLOM KTH SKOLAN FÖR DATAVETENSKAP

www.kth.se