time efficient graph version of the a* algorithm ...

3 downloads 0 Views 209KB Size Report
The A* algorithm uses two types of memories called as the OPEN list and the ... as well as in the CLOSED list has to contain at least four features necessary for ...
TIME EFFICIENT GRAPH VERSION OF THE A* ALGORITHM OPTIMIZED FOR MATLAB Pavel Skrabanek University of Pardubice Faculty of Electrical Engineering and Informatics Studentska 95, 532 10 Pardubice Czech Republic [email protected] Abstract: The paper describes a version of the A* algorithm which is based on matrix operations and is optimized for MATLAB. The algorithm is designed to be highly time efficient which is demonstrated on a case study in which the time efficiency of the optimized A* algorithm is compared with the time efficiency of the classical A* algorithm realized without using the matrix operations. Keywords: shortest path searching, MATLAB, A* algorithm, matrix operations

1

Introduction

The searching of a shortest path in a graph is the quite often solved problem in different area of human activities, like in networking, telecommunications, robotics, transportation etc. The path searching algorithms are well known and have been published many times; however, their realization blindly following the published instructions in combination with ingrained programmer procedures is not optimal in all the programing languages. This is also the case of MATLAB scripting language thus the paper brings the description of a highly time efficient A* algorithm optimized for MATLAB.

2

Shortest path searching algorithms

The shortest path problem is a general term used in the graph theory for problems by which are searched shortest paths between vertices of a graph. It covers many different issues and one of them is searching of the shortest path from one given vertex to another called as the single-source shortest path problem which is considered in the paper. The graph theory offers several path searching algorithms. Well-known are Breadth-First Search (BFS) [10], Dijkstra’s algorithm [4], the Bellman-Ford algorithm [6, 7] or the Floyd algorithm [5]. The mentioned algorithms differ in the possibility of their application as well as in their time complexity. While the BFS can be applied on finite undirected graphs Dijkstra’s algorithm is suitable for finite digraphs with nonnegative edge path weights. The Bellman-Ford algorithm or the Floyd algorithm can be applied on finite digraphs with negative edge path weights; however, the Floyd algorithm cannot be applied on graphs with negative circuits. The basic family of the search algorithms is extended by other algorithms like A* algorithm and its modifications. The A* algorithm is based on the best-first search algorithm; however, the selection of a vertex to be expanded is based on an admissible heuristic function introduced by Hart and col. in [8]. An appropriate formulation of the evaluation function can significantly reduce the size of the search-tree which naturally reduces the processing time. Moreover, the evaluation function is quite flexible and allows inclusion of some specific requirements of a solved problem which are the main reasons of its popularity in robotics, computer games, etc. 2.1

The A* algorithm

The A* algorithm is in fact the best-first search algorithm with extended evaluation function described for the j th vertex vj by the equation (1) where f (j) is the estimation of the total path cost from the initial vertex vinit to the goal vertex vgoal via vj , the g(j) is the cost of the path from vinit to vj and h(j) is the heuristic estimation of the path cost from vj to vgoal . f (j) = g(j) + h(j)

(1)

The formulation of the heuristic function has the significant impact on the A* algorithm functionality. The fundamental requirement on the heuristic function is its admissibility [8]. If this condition is met, the algorithm finds the optimal solution. The heuristic function also hardly influences the real processing time.

The A* algorithm uses two types of memories called as the OPEN list and the CLOSED list. The OPEN list is realized as a priority queue sorted according to the estimated costs of the paths f . An entry in the OPEN list as well as in the CLOSED list has to contain at least four features necessary for the run of the algorithm.They are for the j th vertex following: its marker vj , the marker of its parent vi , the estimation of the total path cost f (j) and the cost of the path from the initial vertex g(j). The OPEN list and the CLOSED list can be considered to be sets thus there are labeled as Vopen respectively as Vclosed in the following text. The high-level flow of the classical A* algorithm is described by the Algorith 1. Algorithm 1 The high-level flow of the classical A* algorithm 1: create Vopen and Vclosed as empty variables 2: write vinit to Vopen {its vi , f (j) and g(j) are zero} 3: while Vopen is not empty do 4: find vi with the smallest f (i) in Vopen 5: if vi is vgoal then 6: terminate the loop and reconstruct the found path 7: end if 8: move vi entry from Vopen to Vclosed 9: expand vi to all adjacent vertexes {the set of found vertexes is labeled as Vadj } 10: evaluate all the vertexes in Vadj {get the g and f values} 11: if vj from Vadj is also in Vclosed then 12: discard vj 13: else if vj from Vadj is also in Vopen and g(j) of vj from Vadj is smaller than g(j) of vj from Vopen then 14: rewrite its values vi , f (j) and g(j) in Vopen by the new one 15: else 16: save vj , vi , f (j) and g(j) to Vopen 17: end if 18: end while The standard approach to the expanding, the evaluation and the following dividing of vertexes is usually realized as a cycle which is not optimal in MATLAB. Also the priority queue is not a standard MATLAB tool and its realization is complex [1]. On the other hand, MATLAB naturally handles matrix and vector operations very well. Considering these facts, an effective A* algorithm in MATLAB has to be realized different manner. One such algorithm is described in the following chapter.

3

The matrix version of the A* algorithm

The matrix version of the A* algorithm tries to use the matrix operations as much as possible thus the graph representation by the adjacency matrix A is the natural choice. A nonzero value of the matrix A indicates an edge between corresponding vertexes; to be more specific the nonzero aij indicates an edge from the i th vertex to the j th vertex. In the original conception [2] the aij value is equal to the number of edges between vi and vj ; however, the value of the edge weight wij can be used instead of it [3] which is supposed also in this algorithm since it can be very useful in the path-planning. The weight can represent the distance between two vertexes for example. The size of A is (n, n) where n is the number of vertexes in the graph G. The algorithm uses the logical indexing which is one of the indexing methods of MATLAB [9] thus the realization of the OPEN list and the CLOSED list as vectors of logical values is advantageous. Their size is (n, 1). The true value on the i th position represents that the vertex vi is in the list while the false value the opposite. The OPEN list is represented by the vector o and the CLOSED list by the vector c. The algorithm is extended by a so called EXPAND list which is the list of vertexes to be expanded. The list is realized as a vector ex of the same size and logic as o and c. All the information about the current state of the searching process is saved in the matrix ST. The basic size of the matrix is (n, 3) although more columns can be used in case of need. The additional columns can save necessary information related to a particular problem. The j th row of the matrix ST labeled as stj contains in the basic three columns variant information about the j th vertex vj and it is described by the equation (2) where vi is the parent vertex marker, f (j) is the estimation of the total cost via vj and g(j) is the cost of the path from vinit to vj . stj =



vi

f (j) g(j)



(2)

The representation of information in the matrix form and using of the logical indexing are the essence of the algorithm high time efficiency. These two aspects allow the multiple processing of information in each step

of the searching process. Naturally, the evaluation function has to be modified too. Its output is a matrix E of the same size and structure as ST. The matrix contains zero on all positions except the rows corresponding to the evaluated vertexes thus the matrix E contains on the j th row ej all the information about the evaluated vertex vj in accordance with the stj structure described by the equation (2). The high-level flow of the optimized A* algorithm is described by Algorithm 2. Algorithm 2 The high-level flow of the A* algorithm matrix version 1: create o, c and ex as logical vectors of false values 2: create ST as the matrix with zero values in 1st column and infinity in 2nd and 3rd column {infinity realized as a sufficiently big number k}  1 if aij > 0 3: create logical matrix L from A where lij = 0 else 4: set o(vinit ) to 1 and stinit to [0 0 0] 5: while (ex(vgoal ) < 1 ∧ max o) do 6: find vi with the smallest f : f (i) = min(f + kc) {f is the 2nd column of ST} 7: set c(i) to 1 and o(i) to 0 8: find all adjacent non-expanded vertexes: ex = lTi ∧ (¬c) {li is the i th row of L} 9: evaluate vertexes according to ex {results saved in E} 10: find vertexes already saved in o: io = ex ∧ o 11: if ((gST (j) > gE (j)) ∧ (io)(j) = 1) then 12: rewrite stj by ej {gST is the g value in ST, gE is the g value in E} 13: end if 14: find all new evaluated vertexes: dc = ex ∧ (¬o) 15: copy their features to ST: stj = ej for dc(j) = 1 16: add them to o: o = o + dc 17: if (ex(vgoal ) = 1) then 18: reconstruct the found path by the back tracking 19: end if 20: end while

4

Case study

To show the matrix version asset two MATLAB functions of the A* algorithm were realized. One function was realized as a standard programmer solution according to the classical approach described in subchapter 2.1 while the second one is the matrix version realized according to the chapter 3. The main asset of the matrix version is the lower time complexity in compare to the classical solution. It is demonstrated on three case studies by which the processing time of the A* algorithms was measured. The processing time includes also the evaluation of the vertexes thus serious attention was paid to the evaluation functions programming. The aim of the case studies is to find the shortest path between two points placed in a maze of a forward known structure which is probably the most illustrative task for the motion planning demonstration. The maze is of the rectangle outer shape with the rectangular inner configuration. The maze is the closed word system and it is inner fully interconnected. The inner layout of the maze allows its transformation by using of an appropriate grid of equally width square cells thus the exact cell decomposition [11] can be used for the layout transformation into the graph G. Each cell of the created grid is marked by a positive integer value which is also used as the marker of a G vertex. The Figure 1 shows an illustrative example of the maze and of its transformation to the graph by using of the grid. The first output of the transformation is the adjacency matrix A. The A* algorithm requires a heuristic function to be formulized. The most used heuristic function by the motion planning is the Euclidean distance which is also used in the case studies. It is calculated as the distance between the cells centers where the j th vertex cell center is a row vector cj . The vector cj contains the cell center coordinates (xj , yj ) in a Cartesian system with the origin in the top left corner of the maze and with the orientation according to the Figure 1. Thus the second output of the transformation is the matrix of vertexes centers C where its j th row is the vector cj and its size is (n, 2). 4.1

The classical A* algorithm

The classical A* algorithm is realized according to the subchapter 2.1; however, some modifications have been done by its programming. They are described in the subchapter 4.1.1. The important part of the A* algorithm

Figure 1: Transformation of a maze layout to a graph

is the evaluation function which realization for the classical A* algorithm is described in the subchapter 4.1.2. 4.1.1

Algorithm realization

The MATLAB function roughly follows the algorithm described in the subchapter 2.1. Since MATLAB requires strange treatment in some cases several nonstandard approaches are used by the A* algorithm realization. The OPEN list and the CLOSED list are realized as matrixes of four columns according to the specification in the subchapter 2.1 and their numbers of rows dynamically change during the function execution. The priority queue is substituted by the sortrows command allowing sorting of the matrixes rows according to the f values. The adjacent vertexes are looked up in the adjacency matrix A by using of the function find. The detection of vertexes presence in the OPEN list and in the CLOSED list is realized by using of the function setdiff. 4.1.2

Evaluation function formulation

The evaluation function used by the classical A* algorithm is realized in the scalar form according to the equation (1). The heuristic function h(j) is based on the normalized Euclidian distance according to the equation (3). The hnorm is the normalization coefficient which is equal to the size of the grid cell used by the decomposition and cj and cgoal are rows of the matrix C. h(j) =

kcj cgoal k hnorm

(3)

The cost of the path g(j) is given by the equation (4) where g(i) is the cost of the path from vinit to vi and the cost of the path from vi to vj is taken from the adjacency matrix A. g(j) = A (i, j) + g(i) 4.2

(4)

Matrix version of the A* algorithm

The optimized A* algorithm is realized according to the chapter 3 and only the standard mathematical, logical and programing constructions were used by its programing thus no special attention has to be paid to its realization here which is not the case of the evaluation function thus its realization is described in the following subchapter. 4.2.1

Evaluation function formulation

The matrix version of the A* algorithm requires the matrix E as the output of the evaluation function. Since the function significant inputs A, C and ex as well as the output E are vectors or matrixes all the operations by the evaluation can be realized as vector operations which is naturally very covetable in MATLAB environment thus the evaluation function uses the vector form of the equation (1) which is described by the equation (5) where f , g and h are column vectors of f, g and h values on the rows belonging to the evaluated vertexes while the other rows are zero. f =g+h

(5)

The vertexes to be evaluated are given by the logical vector ex. The g values are calculated according to the equation (4), the h values according to the equation (3). Naturally, the calculations are realized in the vector

form. The evaluation function returns all the results in the matrix E given by the equation (6) where p is a column vector of the size n with the parent marker vi on the positions given by the vector ex while the rest of the vector contains zero values. E= 4.3



p

f

g



(6)

Experiment

Three mazes are used to show the difference in the performance of the matrix and the classical version of the A* algorithm by solving same tasks in MATLAB. The mazes are of the same size; however, of different inner layouts. In each maze are placed two points for which the shortest path should be found. The shortest path is searched by using of the evaluation functions described by the equations (1) and (5). The mazes used by the experiments are shown on the Figure 2 where the points are symbolized as squares and the shortest path as the dashed line.

Figure 2: The mazes used by experiments: a) Maze 1, b) Maze 2 and c) Maze 3

The aim of the experiments is the acquisition of information about the processing time of the algorithms in different situation. In order to make possible the statistical evaluation, the path searching was repeated ten times for each maze and each algorithm. The obtained data was used by the calculation of the processing time mean values. The average processing time of the classical A* algorithm tcls as well as the average processing time of the matrix version tmat are presented for all the experiments in the Table 1. Their ratios for all the experiments clearly show the high efficiency of the matrix version in MATLAB. Table 1: The average processing time of the algorithms by the experiments Maze 1 2 3

5

tcls , s 0.1358 0.1404 0.0868

tmat , s 0.0121 0.0101 0.0027

Conclusion

The paper describes an A* algorithm designed for the specific environment of MATLAB. The algorithm uses the potential of MATLAB which is in the matrix operations and matrix manipulations. The high time efficiency of the algorithm in MATLAB is shown on three case studies in which the matrix and the classical version of the A* algorithm are applied on same problems with the simultaneous measuring of their processing time. The obtained results are in the Table 1 and they confirm the time efficiency of the matrix version in MATLAB. The matrix version of the A* algorithm is an accomplished tool for applications where a quick response is required or the searching is repeated many times. The good example is the multi-robot path planning problem which was also the impulse for the algorithm creating.

References [1] How to implement priority queue in matlab (2014). URL http://stackoverflow.com/questions/12469502/howto-implement-priority-queue-in-matlab. [Online; accessed 03-Apr-2014] [2] Bronshtein, I., Semendyayev, K., Musiol, G., M¨ uhlig, H.: Handbook of Mathematics. Springer Berlin Heidelberg (2007) [3] Cormen, T., Leiserson, C., R.L., R., Stein, C.: Introduction to Algorithms, 3rd edn. The MIT Press (2009) [4] Dijkstra, E.: A note on two problems in connexion with graphs. Numerische Mathematik 1(1), 269–271 (1959) [5] Floyd, R.: Algorithm 97: Shortest path. Commun. ACM 5(6), 345 (1962) [6] Ford, L.: Network flow theory. Report P-923, The Rand Corporation (1956) [7] Ford, L., Fulkerson, D.: Flows in Networks. Princeton University Press (1962) [8] Hart, P., Nilsson, N., Raphael, B.: A formal basis for the heuristic determination of minimum cost paths. IEEE Transactions on Systems Science and Cybernetics SSC-4(2), 100–107 (1968) [9] Matlab: R2013b. Natick, Massachusetts (2013) [10] Moore, E.: The shortest path through a maze. In: Proceedings of the International Symposium on the Theory of Switching, and Annals of the Computation Laboratory of Harvard University, pp. 285–292. Harvard University Press (1959) [11] Siegwart, R., Nourbakhsh, I.: Introduction to Autonomous Mobile Robots. Bradford Book (2004)

Suggest Documents