專題報告 題目:8-puzzle 的研究 實驗室 : 系統模擬實驗室 指導老師 : 許玟斌 組員 :廖志偉 闕呂叡 何嘉峰 Abstract The 8-puzzle is the largest puzzle of its type that can be completely solved. It is simple and yet obeys a combinatorial large problem space of 9!/2 states. The N*N extension of the 8-puzzle is NP-hard. In the first part of this paper, we introduce how an eight- puzzle transforms from its start state to its goal state. In part two, we discuss about searching tree strategies, which includes, Breadth-first search (BFS) ,Depth-first search(DFS), and heuristic algorithms. Next, we define how heuristic algorithm is used in solving short-path searches and implemented in IDA*. The forth part is an experiment along with a short discussion of the result. We use heuristic algorithm and recursive algorithm (non-heuristic) to solve a scrambled eight-puzzle, and discuss about the way to find the values of heuristic problems. What’s more we make a comparison between heuristic algorithms and recursive algorithms; and calculate the t-test statistic of thirty scrambled puzzles using of both heuristic and non-heuristic condition .The last part is our conclusions. 1. Introduction The n-puzzle problem is known in various versions, including the 8 puzzle, the 15 puzzle, and with various names [ 1.Wikipedia]. It is a sliding puzzle that consists of a grid of numbered squares with one square missing, and the labels on the squares jumbled up. If the grid is 3×3, the puzzle is called the 8-puzzle or 9-puzzle. If the grid is 4×4, the puzzle is called the 15-puzzle or 16-puzzle. The goal of the puzzle is to un-jumble the squares by only making moves which slide squares into the empty space, in turn revealing another empty space in the position of the moved piece.Consider the an instance of the 8-puzzle configuration(where is figure.1) . There are eight tiles numbered 1 through 8 on a 3 by three grid with nine locations so that one location is left empty. We can “move" by sliding a tile adjacent to the empty location into the empty location. The goal is to take a given configuration and through a series of moves arrange the tiles in order from left to right and top to bottom. The set of configurations of the tiles forms a graph where two nodes (configurations) are connected if one node can be converted to the other by a single move.In our research,we serveyed tree-structure algorithms and heuristic algorithms.Then an experiment is conducted in evaluating the performances of a heuristic and a recursive approaches.Finally,we summarized our founding in the discussion and conclusion sectoion. ‧ 2. Tree-structure algorithms 2.1 Search Trees We have seen that the control system’s job involves searching the state graph to find a path from the start node to the goal. A simple method of performing this search is to traverse each of the arrows leading from the start state and in each case record the destination state, then traverse the arrows leaving these new states and again record the results, and so on. Our search for the goal spreads out from the start state like a drop of dye in water. This process continues until one of the new states is the goal, at which point a solution has been found, and the control system needs merely to apply the productions along the discovered path from the start state to the goal. The effect of this strategy is to build a tree, called a search tree, that consists of the part of the state graph that has been investigated by the control system. The root node of the search tree is the start state, and the children of each node are those states reachable from the parent by applying one production. Each arc between nodes in a search tree represent the application of a single production, and each path from the root to a leaf represents a path between the corresponding states in the state graph. The search tree that would be produced when solving the eight-puzzle from the configuration shown in Figure.2 is shown in Figure.3. The leftmost branch of this tree represents an attempt to solve the problem by first moving the 6 tile up, the center branch represents the approach of moving the 2 tile to the right., and the rightmost branch represent moving the 5 tile down. Furthermore, the search tree shows that if we do begin by moving the 6 tile up, the only production allowable next is to move the 8 tile to the right.( Actually, at that point we could also move the 6 tile down but that would merely reverse the previous production and thus be an extraneous move.) The goal state occurs in the last level of the search tree of Figure.3.Since this indicates that a solution has been found, the control system can terminate its search procedure and begin constructing the instruction sequence that will be used to solve the puzzle in the external environment. This turns out to be the simple process of walking up the search tree from the location of the goal node while pushing the productions represented by the tree arcs on a stack as they are encountered. Applying this technique to the search tree in Figure.3 produces the stack of productions in Figure.4. The control system can now solve the puzzle in the outside world by executing the instructions as they are popped from this stack. There is one more observation that we should make. Recall that the trees we discussed to use a pointer system that points down the tree, thereby allowing us to move from a parent node to its children. In the case of a search tree, however, the control system must be able to move from a child to its parent as it moves up the tree from the goal start state. Such trees are constructed with their pointer system pointing up rather than down. That is, each child node contains a pointer to its parent rather than the parent node containing pointers to their children.(In some applications, both sets of pointers are used to allow movement in the tree in both directions.) 2.2 Search strategies One strategy for countering this problem is to change the order in which the search tree is constructed. Rather than building it in a breadth-first [2.Dept. Information & Computer Science ] manner ( meaning that the tree is constructed layer by layer ), we can pursue the more promising paths to greater depths and consider the options only if these original choices turn out to be false leads. This result in a depth-first [2.Dept. Information & Computer Science ] construction of the search tree, meaning that the tree is constructed by building vertical paths rather than horizontal layers. The depth-first approach is similar to the strategy that we as humans would apply when faced with the eight-puzzle. We would rarely pursue several options at the same time, as modeled by the breadth-first approach. Instead, we probably would select the option that appeared most promising and follow it. Note that we said appeared most promising. We rarely know for sure which option is best at the particular point. We merely follow our intuition, which may, of course, lead us astray. Nonetheless, the use of such intuitive information seems to give humans an advantage over the brute-force methods in which each option was given equal attention, and it would therefore seem prudent to apply intuitive methods in automated control systems. 2.2.1 Breadth-first search breadth-first search (BFS) is a search algorithm that begins at the root node and explores all the neighboring nodes. Then for each of those nearest nodes, it explores their unexplored neighbor nodes, and so on, until it finds the goal. For the following graph: It will visit the nodes in the following order: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,11,12. BFS is an uninformed search method that aims to expand and examine all nodes of a graph systematically in search of a solution. In other words, it exhaustively searches the entire graph without considering the goal until it finds it. It does not use a heuristic.Take an eight puzzle figure.5 as an example: 2.2.2 Depth-first search (DFS) Depth-first search (DFS) is an algorithm for traversing or searching a tree, tree structure, or graph. Intuitively, one starts at the root (selecting some node as the root in the graph case) and explores as far as possible along each branch before backtracking Formally, DFS is an uninformed search that progresses by expanding the first child node of the search tree that appears and thus going deeper and deeper until a goal node is found, or until it hits a node that has no children. Then the search backtracks, returning to the most recent node it hadn't finished exploring. In a non-recursive implementation, all freshly expanded nodes are added to a LIFO stack for expansion.Space complexity of DFS is much lower than BFS (breadth-first search). It also lends itself much better to heuristic methods of choosing a likely-looking branch. Time complexity of both algorithms are proportional to the number of vertices plus the number of edges in the graphs they traverse.When searching large graphs that can not be fully contained in memory, DFS suffers from non-termination when the length of a path in the search tree is infinite. The simple solution of "remember which nodes I have already seen" doesn't always work because there can be insufficient memory. This can be solved by maintaining an increasing limit on the depth of the tree, which is called iterative deepening depth-first search For the following graph: a depth-first search starting at A, assuming that the left edges in the shown graph are chosen before right edges, and assuming the search remembers previously-visited nodes and will not repeat them (since this is a small graph), will visit the nodes in the following order: A, B, D, F, E, C, G. Performing the same search without remembering previously visited nodes results in visiting nodes in the order A, B, D, F, E, A, B, D, F, E, etc. forever, caught in the A, B, D, F, E cycle and never reaching C or G. Iterative deepening prevents this loop and will reach the following nodes on the following depths, assuming it proceeds left-to-right as above: 0: A 1: A (repeated), B, C, E (Note that iterative deepening has now seen C, when a conventional depth-first search did not.) 2: A, B, D, F, C, G, E, F (Note that it still sees C, but that it came later. Also note that it sees E via a different path, and loops back to F twice.) 3: A, B, D, F, E, C, G, E, F, B For this graph, as more depth is added, the two cycles "ABFE" and "AEFB" will simply get longer before the algorithm gives up and tries another branch. Take an eight puzzle figure.6 as an example: 3.Heuristic algorithm 3.1 Rationales of heuristic algorithm Search is a flexible tool which can be used, in principle, to obtain a solution to any problem. In practice, there is a serious difficulty. For most problems, the search tree is just too big to search in a reasonable amount of time. Even for a problem as simple as the 8-puzzle, there are more than 31 thousand, million states to be checked. Checking states at the rate of one per millisecond, this would still take nearly a year. If the search process is left to blindly explore the entire search space there is the risk that it will take too long. It is generally necessary to provide knowledge which will enable the search to move more directly towards a solution node. Search processes with knowledge of this type are said to be informed. Processes which carry out the search in a blind or exhaustive fashion are uninformed. Knowledge is provided to the search in the form of an evaluation function for search nodes. This function returns a value which estimates the `promise' of a given node, i.e., how close it is to a goal node. (In practice, evaluation functions usually return a cost value, i.e., how far away the node is from a solution.) The search process can use the function to select the best node to expand at any point (i.e., to choose `which way to go').Evaluation functions are often called heuristic functions on the grounds that they utilise rules-of-thumb.Search using evaluation functions is therefore heuristic search. However, this is more than just terminology. It would be inconsistent to use a completely accurate evaluation function for purposes of `guiding' a search. Or at least it would be strange to describe the resulting process as `search'. With a completely accurate evaluation function, the appropriate branch can be selected at every stage. Search is therefore not required.Two fundamental goals in computer science are finding algorithms with provably good run times and with provably good or optimal solution quality. A heuristic is an algorithm that gives up one or both of these goals; for example, it usually finds pretty good solutions, but there is no proof the solutions could not get arbitrarily bad; or it usually runs reasonably quickly, but there is no argument that this will always be the case. Often, one can find specially crafted problem instances where the heuristic will in fact produce very bad results or run very slowly; however, these instances might never occur in practice because of their special structure. Therefore, the use of heuristics is very common in real world implementations. For many practical problems, a heuristic algorithm may be the only way to get good solutions in a reasonable amount of time. There is a class of general heuristic strategies called metaheuristics, which often use randomized search for example. They can be applied to a wide range of problems, but good performance is never guaranteed. 3.2 In shortest-path problems For shortest path problems [3.CC Skiscim, BL Golden - Annals of Operations Research, 1989], the term has a different meaning. Here, a heuristic is a function, h(n) defined on the nodes of a search tree, which serves as an estimate of the cost of the cheapest path from that node to the goal node. Heuristics are used by informed search algorithms such as Greedy best-first search and A* to choose the best node to explore. Greedy best-first search will choose the node that has the lowest value for the heuristic function. A* search will expand nodes that have the lowest value for g(n) + h(n), where g(n) is the (exact) cost of the path from the initial state to the current node. If h(n) is admissible—that is, if h(n) never overestimates the costs of reaching the goal—, then A* will always find an optimal solution. The classical problem involving heuristics is the n-puzzle. Commonly used heuristics for this problem include counting the number of misplaced tiles and finding the sum of the Manhattan distances between each block and its position in the goal configuration. Note that both are admissible. 3.3.Best-first search Best-first search is a search algorithm which optimizes breadth-first search by [ From Wikipedia ] expanding the most promising node chosen according to some rule. Judea Pearl [4. From computer Science Department] described best-first search as estimating the promise of node n by a "heuristic evaluation function f(n) which, in general, may depend on the description of n, the description of the goal, the information gathered by the search up to that point, and most important, on any extra knowledge about the problem domainThis general sense of the term is used by many authors.Other authors have used best-first search to refer specifically to a search with a heuristic that attempts to predict how close the end of a path is to a solution, so that paths which are judged to be closer to a solution are extended first. This specific type of search is called greedy best-first search by Russell & Norvig.Efficient selection of the current best candidate for extension is typically implemented using a priority queue.Examples of best-first search algorithms include the A* search algorithm,and in turn, Dijkstra's algorithm (which can be considered a specialisation of A*). Best-first algorithms are often used for path finding in combinatorial search. 3.4 Heuristic search with IDA* Consider an evaluation function f(n) = g(n) + h(n), where n is any state encountered in the search,g(n) is the cost of n from the start state (have known),and h(n) is the heuristic estimate of the cost of going from n to a goal node.If this evaluation function is used with the best-first search algorithm, the result is called Algorithm A.If algorithm A is used with an evaluation function in which h(n) is less or equal to the cost of the minimum path from n to the goal h*(n), the resulting search algorithm is called Algorithm A*.[5.Form search Methodologies ] A search algorithm is admissible if it is guaranteed to find a minimal path to a solution whenever such a path exists.All A* Algorithms are admissible,with the following properties : h(n) ≤ h*(n) for all n (under-estimate),and h(n) vs. h*(n) : as close as good for search efficiency.All heuristics satisfy the constraint of A* algorithm, h(n) ≦ h*(n) (i.e. optimal steps could be found for every heuristic), but with different efficiency. 4. Experiment In this section,we describe the shuffling process and brief procedures of the algorithms used in the experiment.We compare the performances of the two algorithms based on the number of nodes generated during the search processes.A t-test is performed to justify the significant difference between the two algorithms on 30 randomly shuffed initial configurations. 4.1 shuffling a puzzle The initial configuration is created by shuffling an ordered configuration randomly. Initially, the ordered puzzle has the empty position at the button-right corner of the grid. We select randomly a tile adjacent to the position move the selected tile to the empties position that would leave a different empty position. We continue this moving procedures many times,(also randomly assigned)the final instance is considered as a well shuffled 8-puzzle configuration. 4.2 heuristic algorithm 1..Heuristic values 的算法 此為原圖,就是 heuristic(0),也就是 h(0)的圖 1 2 3 4 5 6 7 8 舉一例子說明,如下圖 1 2 4 6 3 7 8 5 我們比較一下和上圖的差別,各數字與它各自原來的位置距離各是多少? (1)在原位,所以 heuristic 值= 0,也就是 h(0), (2)離他原本的位置差距=1,所以 heuristic 值=1,也就是 h(1), 依此類推 3.4.5.6.7.8 他們的 h(∑值)=5. 2.程式實作做法 0 1 1 3 2 2 4 4 6 5 5 7 7 3 6 8 8 以上圖來說明,細體黑字的是我們在 stack 裡面的存放位置(pos),而粗體藍字是我 們原來 h(0)圖的數字排列!我們發現一方法,知道數字如何放回他們原來的位置: pos+1=數字 這規律. 而又因為此圖是二維的,所以我們訂出(X,Y)座標來訂 position, 我們訂出數字 1 就位在於(0,0)的位置,2 位在於(1,0),4 位在於(0,1),以此類 推 我們舉例下圖說明: Y X (0,0) 0 1 1 3 2 2 4 4 3 5 5 6 7 7 6 8 8 (2,2) 定出左上角座標為(0,0),最右下角為(2,2) 以下圖為例子 Y X (0,0) 0 1 2 3 4 5 6 7 8 1 2 (2,2) 暫且不論其他數字位置在哪,就光看數字 1,2 而論,對 1 來說,位於原位(因為數字 1-1=pos=0),所以不用討論,再來我們注意 2 這數字.我們找出它原來是應該要排放 在數字 2-1=pos=1 的位置上,但是並沒有,所以我們找出 2 的座標位置位於(2,2),而 原來位置的座標為(1,0), |(2,2)-(1,0)| 之後我們算出 2 與原來座標位置差距是 3, 所以我們也就知道 heuristic 值是= 3 了! 4.3recursive algorithm 利用 BFS 的搜尋方式 以下面的例子說明: 一開始找到一個圖形為起始的 state . 1 3 4 2 7 8 5 一開始找到一個圖形為起始的 state,從以下的樹狀圖得到 6 , , 的分支圖(figure.7) 利用廣度搜尋的方式將每個節點依序跑過直到找到目標節點(goal),我們以下面 的圖來說明之,在 figure.8.9.10 中可看到每個 puzzle 所延伸的 state,我們利用 BFS 的觀念下去作樹的搜尋,由左到右一個一個節點找,直到找到目標為止(goal), 所以這個圖形將會經過 53 個節點數,當然搜尋數的策略不只 BFS 而已,包含了 DFS 以及 heuristics 的演算法。 4.4working examples of both alogorithms Fig 1.0 Fig 1.0 為我們隨機產生的一個例子,我們分別用 searching tree algorithm 的方法以 及 heuristic 去解 Fig 1.1 Fig 1.2 Fig 1.1 是用 searching tree algorithm 最後搬移的結果,我們可以看到最少移動步數 是 6 步,產生了 90 個 node Fig 1.2 是用 heuristic algorithm 最後搬移的結果,同樣最少移動步數是 6 步,但是我 們很明顯的看到產生的 node 只有 13 個,比用 searching tree algorithm 還少了很多 所以執行時間也比較短 我們再舉一個例子 Fig 1.3 Fig 1.3 為我們隨機產生的一個例子,我們分別用 searching tree algorithm 的方法以 及 heuristic 去解 Fig 1.4 Fig 1.5 Fig 1.1 是用 searching tree algorithm 最後搬移的結果,我們可以看到最少移動步數 是 8 步,產生了 337 個 node Fig 1.2 是用 heuristic algorithm 最後搬移的結果,同樣最少移動步數是 8 步,但是我 們很明顯的看到產生的 node 只有 16 個,比用 searching tree algorithm 還少了很多 所以執行時間也比較短 4.5 simulation results puzzle non-heuristics heuristics Di 1 . 1 2 4 7 Found solution with 2steps used 13 nodes Found solution with 2steps used 7 nodes 6 3 Found solution with 5steps Found solution with 5steps 49 used 61 nodes used 12 nodes 3 5 8 6 2. 2 1 4 5 7 8 6 1 5 2 Found solution with 10 steps Found solution with10steps 8 7 3 used 706 nodes used 21 nodes 4 6 3. 685 4. 1 2 4 7 5. 6 3 Found solution with 6steps Found solution with 6steps 8 used 129 nodes used 15 nodes 5 114 1 3 Found solution with 6steps Found solution with 6steps used 90 nodes used 13 nodes 5 2 6 4 7 8 1 2 3 7 4 8 6 5 2 77 6. Found solution with 7steps used 183 nodes Found solution with 7steps used 15 nodes 168 3 Found solution with 3steps Found solution with 3steps 10 4 5 used 19nodes used 9 nodes 8 6 2 5 3 Found solution with 8steps Found solution with 8 steps 1 6 8 used 268 nodes used 16 nodes 4 7 2 3 Found solution with 4steps Found solution with 4steps 4 5 used 31 nodes used 10 nodes 7 8 6 7. 1 7 8. 252 9. 1 10. 1 3 6 21 2 Found solution with 6steps Found solution with 6 steps 5 8 used 129 nodes used 41 nodes 2 3 5 6 Found solution with 4steps used 8 nodes 23 1 Found solution with 4steps used 31nodes 4 7 8 1 2 3 Found solution with 7steps Found solution with 7steps 165 7 6 used 183 nodes used 18 nodes 5 4 8 2 3 Found solution with 3steps Found solution with 3steps 5 6 used 19 nodes used 7 nodes 7 8 1 2 3 4 6 7 5 8 3 6 4 7 88 11. 12. 13. 1 4 12 14. 15. 1 Found solution with 3 steps Found solution with 3steps used 19 nodes used 9 nodes 10 4 2 7 5 8 Found solution with 6steps Found solution with 6 steps used 90 nodes used 13 nodes 77 16. 1 2 3 7 4 5 8 Found solution with 5 steps used 61 nodes Found solution with 5 steps used 12 nodes 49 Found solution with 7 steps used 183 nodes Found solution with 7steps used 15 nodes 168 Found solution with 8 steps Used 337 nodes Found solution with 8 steps used 16 nodes 321 Found solution with 7steps used 183 nodes Found solution with 7 steps used 15 nodes 168 Found solution with 6steps used 90 nodes Found solution with 6 steps used 13 nodes 77 6 17. 4 1 3 7 2 5 8 6 18. 4 1 7 8 2 3 5 6 19. 1 2 4 8 3 7 6 5 1 2 3 7 4 5 8 6 20. 21. 1 3 6 4 2 8 7 5 Found solution with 6steps used 90 nodes Found solution with 6 steps used 13 nodes 77 Found solution with 7steps Used 183 nodes Found solution with 7steps used 15 nodes 168 Found solution with 5steps used 61 nodes Found solution with 5steps used 12 nodes 49 Found solution with 9 steps Used 493 nodes Found solution with 9 steps used 18 nodes 475 Found solution with 7steps Used 183 nodes Found solution with 7 steps used 15 nodes 168 Found solution with 4 steps used 31 nodes Found solution with 4 steps used 10 nodes 21 22. 1 5 4 8 7 6 2 3. 23. 1 3 5 2 6 4 7 8 1 2 8 3 7 6 1 3 6 5 2 4 7 8 2 3 1 4 6 7 5 8 24. 4 5 25. 26. 27. 2 4 3 1 8 5 7 Found solution with 7steps used 183 nodes Found solution with 7 steps used 22 nodes 161 Found solution with 4 steps Found solution with 4 steps 21 used 31 nodes used 10 nodes 6 28. 1 3 4 2 6 7 5 8 1 3 6 7 2 5 4 8 1 3 2 5 8 6 29. Found solution with 9 steps used 493 nodes Found solution with 9 steps used 21 nodes 472 30. 4 7 Found solution with 5steps used 61 nodes Found solution with 5 steps used 12 nodes 49 _________________________________________________________________________ We compare the performances between non-heuristics and heuristics using the t-test statistic t d sd / n where , d =Σdi/n and Sd= Σ(di- d )2/(n-1) in our simulation, d = Σdi∕30=140.03333, Sd = 148.7551, then t = 5.14646 We conclude the two algorithms resulting a significant difference, since t>t(29,05)=1.699 5.conclusions It is possible to use parity arguments to show that some starting positions for the n-puzzle are impossible to resolve, no matter how many moves are made. This is done by considering a function of the tile configuration that is invariant under any valid move, and then using this to partition the space of all possible labeled states into equivalence classes of reachable and unreachable states. references: 1.http://en.wikipedia.org/wiki/N-puzzle 2.http://www.ics.uci.edu/~eppstein/161/960215.html 3.http://www.springerlink.com/content/b82151641m55hj88/ 4.http://singapore.cs.ucla.edu/jp_home.html 5.http://www.cs.ntust.edu.tw/
© Copyright 2026 Paperzz