29 Discussion 01: Search (From Spring 2025)
Slides
29.1 Search Algorithms in Action
For each of the following graph search strategies, work out the order in which states are expanded, as well as the path returned by graph search. In all cases, assume ties resolve in such a way that states with earlier alphabetical order are expanded first. Remember that in graph search, a state is expanded only once.
29.1.1 (a)
Depth-first search.
Answer
States Expanded: Start, A, C, D, Goal
Path Returned: Start-A-C-D-Goal
29.1.2 (b)
Breadth-first search.
Answer
States Expanded: Start, A, B, D, C, Goal
Path Returned: Start-D-Goal
29.1.3 (c)
Uniform cost search.
Answer
States Expanded: Start, A, B, D, C, Goal Path Returned: Start-A-C-Goal
29.2 Search and Heurstics
Imagine a car-like agent wishes to exit a maze like the one shown below:
- The agent is directional and at all times faces some direction \(d \in (N, S, E, W)\).
- With a single action, the agent can either move forward at an adjustable velocity \(v\) or turn. The turning actions are left and right, which change the agent’s direction by 90 degrees. Turning is only permitted when the velocity is zero (and leaves it at zero).
- The moving actions are fast and slow. Fast increments the velocity by 1 and slow decrements the velocity by 1; in both cases the agent then moves a number of squares equal to its NEW adjusted velocity (see example below).
- A consequence of this formulation is that it is impossible for the agent to move in the same nonzero velocity for two consecutive timesteps.
- Any action that would result in a collision with a wall or reduce v below 0/above a maximum speed \(V_{max}\) is illegal.
- The agent’s goal is to find a plan which parks it (stationary) on the exit square using as few actions (time steps) as possible.
As an example: if at timestep \(t\) the agent’s current velocity is 2, by taking the fast action, the agent first increases the velocity to 3 and move 3 squares forward, such that at timestep \(t+1\) the agent’s current velocity will be 3 and will be 3 squares away from where it was at timestep \(t\). If instead the agent takes the slow action, it first decreases its velocity to 1 and then moves 1 square forward, such that at timestep \(t+1\) the agent’s current velocity will be 1 and will be 1 squares away from where it was at timestep \(t\). If, with an instantaneous velocity of 1 at timestep \(t+1\), it takes the slow action again, the agent’s current velocity will become 0, and it will not move at timestep \(t+1\). Then at timestep \(t+2\), it will be free to turn if it wishes. Note that the agent could not have turned at timestep \(t+1\) when it had a current velocity of 1, because it has to be stationary to turn.
29.2.1 (a)
If the grid is \(M\) by \(N\), what is the size of the state space? Justify your answer. You should assume that all configurations are reachable from the start state.
Answer
The size of the state space is \(4MN(V_{max} + 1)\). The state representation is (direction facing, \(x\), \(y\),speed). Note that the speed can take any value in {\(0\), …, \(V_{max}\)}.
29.2.2 (b)
Is the Manhattan distance from the agent’s location to the exit’s location admissible? Why or why not?
Answer
No, Manhattan distance is not an admissible heuristic. The agent can move at an average speed of greater than 1 (by first speeding up to \(V_{max}\) and then slowing down to 0 as it reaches the goal), and so can reach the goal in less time steps than there are squares between it and the goal. A specific example: A timestep 0, the agent’s starts stationary at square 0 and the target is 9 squares away at square 9. At timestep 0, the agent takes the \(fast\) action and ends up at square 1 with a velocity of 1. At timestep 1, the agent takes the \(fast\) action and ends up at square 3 with a velocity of 2. At timestep 2, the agent takes the \(fast\) action and ends up at square 6 with a velocity of 3. At timestep 3, the agent takes the \(slow\) action and ends up at square 8 with a velocity of 2. At timestep 4, the agent takes the \(slow\) action and ends up at square 9 with a velocity of 1. At timestep 5, the agent takes the \(slow\) action and stays at square 9 with a velocity of 0. Therefore, the agent can move 9 squares by taking 6 actions.
29.2.3 (c)
State and justify a non-trivial admissible heuristic for this problem which is not the Manhattan distance to the exit.
Answer
There are many answers to this question. Here are a few, in order of weakest to strongest:
(a) The number of turns required for the agent to face the goal.
(b) Consider a relaxation of the problem where there are no walls, the agent can turn, change speed arbitrarily, and maintain constant velocity. In this relaxed problem, the agent would move with \(V_{max}\), and then suddenly stop at the goal, thus taking \(d_{manhattan}/V_{max}\) time.
(c) We can improve the above relaxation by accounting for the acceleration and deceleration dynamics. In this case the agent will have to accelerate from 0 from the start state, maintain a constant velocity of \(V_{max}\), and slow down to 0 when it is about to reach the goal. Note that this heuristic will always return a greater value than the previous one, but is still not an overestimate of the true cost to reach the goal. We can say that this heuristic dominates the previous one.
In particular, let us assume that \(d_{manhattan}\) is greater than and equal to the distance it takes to accelerate to and decelerate from \(V_{max}\). (In the case that \(d_{manhattan}\) is smaller than this distance, we can still use \(d_{manhattan}/V_{max}\) as a heuristic). We can break up the \(d_{manhattan}\) into three parts: \(d_{accel}\), \(d_{V_{max}}\), and \(d_{decel}\). The agent travels a distance of \(d_{accel}\) when it accelerates from 0 to \(V_{max}\) velocity. The agent travels a distance of \(d_{decel}\) when it decelerates from \(V_{max}\) to 0 velocity. In between acceleration and deceleration, the agent travels a distance of \(d_{V_{max}} = d_{manhattan} - d_{accel} - d_{decel}\). \(d_{accel} = 1+2+3+...+V_{max} = \frac{(V_{max})(V_{max}+1)}{2}\) and \(d_{decel} = (V_{max}-1)+(V_{max}-2)+...+1+0=\frac{(V_{max})(V_{max}-1)}{2}\). So \(d_{V_{max}} = d_{manhattan} - \frac{(V_{max})(V_{max}+1)}{2} - \frac{(V_{max})(V_{max}-1)}{2} = d_{manhattan} - V_{max}^2\). It takes \(V_{max}\) steps to travel the initial \(d_{accel}\), \(\frac{d_{manhattan}-V_{max}^2}{V_{max}}\) steps to travel the middle \(d_{V_{max}}\) and \(V_{max}\) steps to travel the last \(d_{decel}\). Therefore, our heuristic is
\[ \begin{cases} \frac{d_{manhattan}}{V_{max}}, & \text{if } d_{manhattan} \le V_{max}^2 = d_{accel} + d_{decel} \\ \frac{d_{manhattan}}{V_{max}} + V_{max}, & \text{if } d_{manhattan} > V_{max}^2 = d_{accel} + d_{decel} \end{cases} [cite_start]\]
29.2.4 (d)
If we used an inadmissible heuristic in A* graph search, would the search be complete? Would it be optimal?
Answer
If the heuristic function is bounded, then A* graph search would visit all the nodes eventually, and would find a path to the goal state if there exists one. An inadmissible heuristic does not guarantee optimality as it can make the good optimal goal look as though it is very far off, and take you to a suboptimal goal.
29.2.5 (e)
Give a possible advantage that an inadmissible heuristic might have over an admissible one.
Answer
The time to solve an A* search problem is a function of two factors: the number of nodes expanded, and the time spent per node. An inadmissible heuristic may be faster to compute, leading to a solution that is obtained faster due to less time spent per node. It can also be a closer estimate to the actual cost function (even though at times it will overestimate!), thus expanding less nodes. We lose the guarantee of optimality by using an inadmissible heuristic. But sometimes we may be okay with finding a suboptimal solution to a search problem.
29.3 Pacfriends Unite
Pacman and his Pacfriends have decided to combine forces and go on the offensive, and are now chasing ghosts instead! In a grid of size \(M\) by \(N\), Pacman and \(P − 1\) of his Pacfriends are moving around to collectively eliminate all of the ghosts in the grid by stepping on the same square as each of them. Moving onto the same square as a ghost will eliminate it from the grid, and move the Pacman into that square.
Every turn, Pacman and his Pacfriends may choose one of the following four actions: \(left, right, up, down\), but may not collide with each other. In other words, any action that would result in two or more Pacmen occupying the same square will result in no movement for either Pacman or the Pacfriends. Additionally, Pacman and his Pacfriends are indistinguishable from each other. There are a total of \(G\) ghosts, which are indistinguishable from each other, and cannot move.
Treating this as a search problem, we consider each configuration of the grid to be a state, and the goal state to be the configuration where all of the ghosts have been eliminated from the board. Below is an example starting state, as well as an example goal state:
Assume each of the following subparts are independent from each other. Also assume that regardless of how many Pacmen move in one turn, the total cost of moving is still 1.
29.3.1 (a)
Suppose that Pacman has no Pacfriends :( , so \(P\) = 1.
29.3.1.1 (i)
What is the size of the minimal state space representation given this condition? Recall that \(P\) = 1
Answer
For each of the following heuristics, select whether the heuristic is admissible or inadmissible. Recall that \(P\) = 1.
29.3.1.2 (ii)
\(h(n)\) = the sum of the Manhattan distances from Pacman to every ghost.
Answer
29.3.1.3 (iii)
\(h(n)\) = the number of ghosts times the maximum Manhattan distance between Pacman and any of the ghosts.
Answer
29.3.1.4 (iv)
\(h(n)\) = the number of ghosts times the maximum Manhattan distance between Pacman and any of the ghosts.
Answer
29.3.2 (b)
Suppose that Pacman has exactly one less Pacfriend than there are number of ghosts :) ; therefore \(P = G\). Recall that Pacman and his Pacfriends are indistinguishable from each other.
29.3.2.1 (i)
What is the size of the minimal state space representation given this condition? Recall that \(P = G\).
Answer
For each of the following heuristics, select whether the heuristic is admissible or inadmissible. Recall that \(P = G\).
29.3.2.2 (ii)
\(h(n)\) = the largest of the Manhattan distances between each Pacman and its closest ghost.
Answer
29.3.2.3 (iii)
\(h(n)\) = the smallest of the Manhattan distances between each Pacman and its closest ghost.
Answer
29.3.2.4 (iv)
\(h(n)\) = the number of remaining ghosts.
Answer
29.3.2.5 (v)
\(h(n)\) = \(\frac{number\:of\:remaining\:ghosts}{P}\)