410 likes | 570 Views
Search in AI. Towers of Hanoi. Solve the problem by finding the appropriate state transitions from a start state to a goal state. A state is a configuration of discs A transition takes us from one state to another via a legal move. Towers of Hanoi Observations. Moving the smallest disc
E N D
Towers of Hanoi • Solve the problem by finding the appropriate state transitions from a start state to a goal state. • A state is a configuration of discs • A transition takes us from one state to another via a legal move
Towers of Hanoi Observations • Moving the smallest disc • The smallest disc can always be moved to two other poles • If the smallest disc was moved on the previous move, it does not make sense to move it again because • One choice is to move back to where it just was. • The other choice was available on the previous move • Conclusion: There is never a reason to move the smallest disc twice in a row.
Towers of Hanoi Observations (continued) • Moving a disc other than the smallest • The disc holding the smallest disc is not a legal destination for a larger disc • The largest top-level disc can not be moved, as both possible destinations contain smaller discs • Conclusion: If we aren’t moving the smallest disc, there is only one move choice.
Towers of Hanoi Optimization • We will always have a choice of two destinations for the smallest disc, followed by a “forced” move of the smaller of the two other top-level discs. • We can therefore reduce the possible transitions at any point to two.
The Eight Puzzle • Eight puzzle - one start state and one solution state.
Eight puzzle can also have state optimizations • Change the goal-state: From to this: To this:
From each state there are 8 possible “corner twists.” • Rotate clockwise • Top-left quadrant • Top-right quadrant • Bottom-left quadrant • Bottom-right quadrant • Rotate counter-clockwise • Top-left quadrant • Top-right quadrant • Bottom-left quadrant • Bottom-right quadrant
Using state-transition graphs, many of these types of problems become be solved using graph search algorithms. • You probably already know some graph-search algorithms • Breadth-first search • Look at all the possible solutions one move deep, before looking at any possible solutions two moves deep and so on. • Depth-first search • Try a first move, then another move from that state. When you run out of moves, backtrack to the next available move.
Breadth-first Search • Place the root node in a queue • While a solution has not been found • Remove a state from the queue • If that state is not a solution • Place all possible transitions from that state into the queue. Positive qualities: With enough resources, the algorithm will find a solution. Negative qualities: Memory requirements and processing times grow exponentially for every move depth where the branching factor is greater than 1.
Depth-first Search • Depth-first-search(root) • For every transition from the root • If the state is not a solution • Depth-first-search(transition state) Positive qualities: Memory requirements do not grow exponentially Negative qualities: There is no guarantee that depth-first will find an answer, even if one exists.
Depth-First Search with Iterative Deepening • Set the depth limit to 0 • While a solution is not found • Perform Depth-First Search to the depth limit • Increase the depth limit by 1 This algorithm has positive qualities of breadth-first, without the exponential memory costs.
Heuristics-based Search • Use a set of rules to evaluate the “quality” of a state. The quality of a state is usually an estimate of how close the state is to the solution.
Sample Heuristics • For the 8-puzzle • Compute the sum of the minimum number of moves needed to move each tile from its current position its destination
Manhattan Distance – sum of the minimum number of moves each tile must make to get to its destination position 4 must move a minimum of 1 space 1 must move a minimum of 1 space 2 must move a minimum of 1 space 8 must move a minimum of 1 space 7 must move a minimum of 3 spaces 6 must move a minimum of 3 spaces 3 must move a minimum of 3 spaces 5 must move a minimum of 2 spaces Manhattan distance = 1 + 1 + 1 +1 + 3 + 3 + 3 + 2 = 15
Possible (naïve) Heuristic for Towers of Hanoi • Number of discs in final position (i.e., on the third pole on top of disc that it is on top of in the solution) – the minimum number of moves to reach a complete solution.
Hill Climbing • Make any transition that gets you from your current state to a better state. Do not store any state other than your current state. • The value of the state is typically created by a heuristic • Examples: • 8-Puzzle • Make any move that transitions to a state with a lower Manhattan distance. • Lost in HoanKiem District: • Turn onto a different street if it is larger than the street that you are on until you find something that you recognize.
Steepest Ascent Hill Climbing • Look at all possible state transitions before selecting the one with the highest value.
Problems with Hill-climbing • The heuristic may not be a good measure, or may not work in this instance • It is easy to get to a “local maximum” • You may not be climbing the highest hill in the search space • There is no mechanism for backtracking • If you make a wrong turn you may not be able to recover • There is no mechanism for detecting cycles
Problems with Hill Climbing (continued) • Plateau Problem • All transition states might have the same value as the current state.
Best-first Search • Maintain a list of nodes (the open list) that we can return to if we take a wrong path • Maintain a list of nodes that we no longer need to consider (the closed list) • Best-first search • While a solution has not been found • Remove the node with the best heuristic value from the open list and place it on the closed list • For each child of that node, if the node is not already on the closed list, place it on the open list.
Beam-Search • Expand the best k nodes on the open list • Similar to breadth-first, but each level only has k nodes explored.
Completeness • A search algorithm is said to be complete if it is guaranteed to find a solution if a solution exists.
Optimal • A search algorithm is said to be optimal if it is guaranteed to find the lowest-cost (best) solution.
Admissible Heuristic • An admissible heuristic is one that provides an estimate that is always less than or equal to the actual cost. • Admissible heuristics allow us to know that we have found the best answer when the heuristic estimates from all nodes on the open list exceed the value of the current node.
Monotonicity • A search algorithm is said to be monotonic if it is guaranteed to produce an optimal path to every intermediate node it generates.
Branch and Bound • Create a priority queue sorted on lowest cost • Insert root node into priority queue • While a solution is not found or the queue contains nodes with a cost less then the solution found • Remove a node from the priority queue • If it is the best solution found so far, record it • Add all the children to the priority queue • Return the solution
Principal of Optimality • Optimal paths are constructed from optimal subpaths • If we find a shorter alternate path to a node we can discard the longer path
Estimating the cost to a goal • Let g(x) be the cost from the start state to node x • Let h(x) be the cost from x to the goal state • The total cost from start to finish, through node x is: f(x) = g(x) + h(x) • We do not know h(x). So we create an estimate that we call h*(x) • f*(x) = g*(x) + h*(x) is our estimate of f(x)
A* Algorithm • Create a priority queue sorted on f*(x) • Add the start node to the queue • While a solution has not been found • Remove a node from the queue • Compute f*(x) for all the nodes children (that have not already been encountered) • Insert the node’s children into the queue
Assignment • Use the C++ code posted on the website to solve the 8-puzzle
Two-Player Games • Different search techniques are required to find solutions to two-player, move-based games.
Minimax Trees • Alternate between choosing the maximum and minimum values of the child nodes. • The “Value of the Game” can be computed using a bottom-up computation of the value of each parent node
Depth of Game-Trees • Ideally, all the way to the bottom • We can “solve” such games • Use some fixed depth and then apply a “static evaluation function” that estimates the value of the game at that node. • Use heuristics to determine a depth that is not necessarily uniform • e.g., search to a quiescent state
Minimax with alpha-beta pruning • If terminal node, return the value of the terminal node • If non-terminal • For each child • Score = alpha-beta(child) • If (Score > alpha) alpha
Two types of pruning • Alpha cut-off • Happens during min-level analysis • The max player will not allow this to happen, so there is no point looking any further • Beta cut-off • Happens during max-level analysis • The min player will not allow this to happen, so there is no point looking any further
Homework Assignment • Use the letters in your full name to assign values for the 16 terminal nodes in a binary game tree (lower-case only). • If you don’t have 16 letters in your name, just repeat your name • j o h n q u e s m i t h j o h n • The first player wants to maximize the alphabetic value of the selected letter • The second player wants to minimize the alphabetic value of the selected letter • Draw the minimax tree and annotate each node with the value of the subtree • Show which branches of the tree would be pruned through alpha-beta search • Specify whether alpha or beta cutoff was used for each pruning