1. Home
  2. Explore
  3. Computer Science
  4. AI Course Announcements

Study methods

Flashcards
Quiz
Fill in the blanks
Written test
Lesson
Notes
Podcast
Content

AI Course Announcements

Search and Planning Agents

Agents That React and Agents That Plan

Reflex agents

A reflex agent chooses an action from its current percept, possibly using some memory, without reasoning through the consequences of that action. Reflex behavior can be useful when a rapid response is more important than deliberation. For example, pulling a hand away from a hot surface may be better than spending time predicting every possible consequence.

A reflex can be rational or irrational. Rationality depends on the quality of the behavior, not on whether the agent deliberates. A quick action can be rational if it produces the best expected outcome in the circumstances.

A simple Pac-Man reflex agent might move toward the nearest dot. This works when the route is unobstructed. In a different maze, the agent may repeatedly try to move through a wall toward the same dot. It then fails to make progress and may lose points for wasting time. The same reflex rule can therefore work well in one environment and poorly in another.

Planning agents

A planning agent asks “what if?” It considers a sequence of possible actions, predicts the states those actions would produce, and evaluates the resulting sequence against a goal.

Planning requires:

  • A model of the world that predicts how actions change states.

  • A goal that specifies what the agent is trying to achieve.

  • A way to compare possible plans, such as their total cost.

A goal may specify one exact target state, or it may be a condition satisfied by many different states. For instance, a goal could be “reach this city” or “eat all the dots.”

Planning and replanning offer different trade-offs:

  • Plan once: Compute a complete sequence of actions before acting. This can produce an optimal plan, but computing it may take a long time.

  • Replan repeatedly: Plan only as far as a nearer subgoal, act, then solve a new planning problem. For example, Pac-Man can plan a route to the nearest dot, follow it, and then plan again. This allows action to begin sooner, but each plan addresses only the next subgoal.

Properties of planning algorithms

Two important properties are:

  • Completeness: If a solution exists, the algorithm is guaranteed to find one.

  • Optimality: If a solution exists, the algorithm is guaranteed to find a least-cost solution.

An algorithm may be complete without being optimal, optimal only under certain cost assumptions, or neither. Its runtime and memory requirements also matter.


Formalizing a Search Problem

A search problem is a mathematical model of a real-world task. Its key components are:

  • State space: The set of states the problem representation can describe.

  • Successor function: For a state, specifies the available actions and the resulting states. It encodes how the modeled world works.

  • Start state: The state from which the search begins.

  • Goal test: A test that determines whether a state satisfies the goal.

  • Solution: A sequence of actions, also called a plan, that transforms the start state into a state satisfying the goal test.

A search algorithm operates on the problem representation. It does not directly reason about every detail of the real world.

The state space and successor function must be designed together. If the state representation changes, the successor function may also need to change. The model should record enough information to determine what actions are possible, what their outcomes are, and whether the goal has been reached.

Example: Finding a route between cities

For a route-finding problem on a map:

  • States: Cities or locations.

  • Successor function: From each city, the neighboring cities that can be reached by a road.

  • Transition cost: A value associated with traveling along a road, such as distance.

  • Start state: The city where the journey begins.

  • Goal test: Whether the current city is the destination.

  • Solution: A sequence of cities connected by roads.

Distance is only one possible cost. A model could instead, or additionally, account for traffic, road quality, or other factors by changing how transition costs are assigned.

Example: Pac-Man pathing

If the goal is simply to reach a particular location:

  • The search state can be Pac-Man’s location.

  • The available actions can be movements north, south, east, and west.

  • The successor function must account for walls: attempting to move into a wall may leave Pac-Man in the same location.

  • The goal test checks whether Pac-Man has reached the target.

Information that does not affect this task can be omitted. The locations of food dots and ghosts, for example, need not be included if they do not affect the movement model or goal.

Example: Eating all dots

If the goal is to eat all dots, location alone is not sufficient. The state must represent:

  • Pac-Man’s current location.

  • Which possible dot locations still contain food.

One representation assigns a Boolean value to each possible food location: the value indicates whether food remains there. The successor function then updates both Pac-Man’s location and the food configuration after each move. The goal test checks whether no food remains.

The state must contain enough detail for both the goal test and the successor function. Knowing only the number of dots left may be enough to test whether the goal has been reached, but it does not reveal where the remaining dots are, so it is not enough to plan routes toward them.

Choosing what belongs in a state

A good search state contains the information needed to predict the future of the modeled problem and to test its goal, while leaving out irrelevant details. This requires asking:

  1. Can the goal test be evaluated from the state?

  2. Can the successor function determine the effects of actions from the state?

  3. Does the state include unnecessary information that does not affect either?

This balance is a central part of problem formulation. A model that omits necessary information may produce incorrect plans; a model that includes irrelevant information may make the search unnecessarily large.


State-Space Size

The size of a state space depends on how many possible values each state variable can take. When variables can vary independently, the number of combinations is the product of their possibilities.

For a Pac-Man world with:

  • 120120120 possible Pac-Man positions,

  • 303030 possible food locations, each either occupied or empty,

  • 121212 possible positions for each of two ghosts,

  • 444 possible Pac-Man orientations,

the number of possible world states is estimated as

120×230×122×4.120 \times 2^{30} \times 12^2 \times 4.120×230×122×4.

The factor 2302^{30}230 arises because each of the 303030 food locations has two possible values. Across all locations, the number of food configurations is 2302^{30}230.

The search state space can be much smaller than the full world-state space because a particular problem may not need every variable:

  • For eat-all-dots, Pac-Man’s position and the food configuration are relevant, giving an estimated size of 120×230120 \times 2^{30}120×230.

  • For simple pathing, only Pac-Man’s position may be relevant, giving 120120120 possible search states.

These counts are often approximate. Some combinations counted by multiplying all possibilities may be impossible—for example, a state may place Pac-Man on a dot that would already have been eaten. An estimate can still be useful for recognizing whether a problem is small or enormous.


Search State Spaces and Search Trees

State-space graph

A state-space graph is a mathematical representation of a search problem:

  • Nodes represent states.

  • Arcs represent possible transitions specified by the successor function.

  • Goal states are the nodes that satisfy the goal test.

Each state appears only once in the state-space graph. This graph is usually an abstraction, not something that can be fully drawn or explicitly constructed. A problem with 2302^{30}230 possible food configurations already has too many possible states to lay out in practice.

Search tree

A search tree is generated as the algorithm explores possible plans from the start state. Each node represents a path—a sequence of states or actions taken from the start—not merely a state.

This distinction matters because different paths can lead to the same state:

  • In the state-space graph, that state appears once.

  • In the search tree, it may appear multiple times, once for each path that reaches it.

Consequently, a search tree can be larger than its state-space graph. It can even be infinite when the state-space graph is finite: if actions allow the search to cycle between states, the tree can keep generating longer paths. For example, repeatedly moving between a small number of connected states creates an indefinitely long sequence of search-tree nodes.

The full tree represents all possible plans and their consequences. Search algorithms build only the parts they need, on demand, rather than constructing the entire tree in advance.


Tree Search

A general tree-search algorithm maintains a fringe: the set of leaf nodes in the current search tree that are waiting to be expanded.

A candidate for expansion is a fringe node. Expanding a node means applying the successor function to the last state on its path and adding the resulting child nodes to the search tree.

A basic tree-search process is:

  1. Initialize the search tree and fringe with the start state.

  2. If the fringe is empty, stop: there are no remaining candidates and no solution has been found.

  3. Choose one node from the fringe according to a search strategy.

  4. Check whether the chosen node’s state satisfies the goal test. If it does, return the corresponding plan.

  5. Otherwise, expand the node using the successor function and add its children to the fringe.

  6. Repeat.

The overall procedure stays the same across many search algorithms. Their main difference is which fringe node they choose next.

The goal test is performed when a node is selected for expansion, not merely when a goal state is first generated as a child. This detail is important in cost-sensitive search: a goal may be generated by a relatively expensive path even though a cheaper path to a goal remains unexplored.


Depth-First Search

Depth-first search (DFS) selects a deepest node from the fringe. It follows one branch as far as possible before returning to explore alternatives. When multiple nodes have the same depth, a tie-breaking rule—such as alphabetical order—can determine which is chosen first.

Behavior and properties

DFS can find a solution quickly if the chosen branch leads to one. But it may explore a deep, unproductive branch while a shallow solution lies elsewhere.

Let:

  • bbb be the branching factor, the number of successors per node in the simplified analysis.

  • mmm be the maximum search-tree depth.

In the worst case, DFS may explore the entire tree, so its time complexity is

O(bm).O(b^m).O(bm).

Its memory use is much smaller than that of breadth-first search. Along a path of depth at most mmm, the fringe can contain the unexpanded siblings at each level, up to roughly bbb at each of mmm levels. The space complexity is therefore

O(bm).O(bm).O(bm).

DFS is complete when the search tree is finite: it will eventually examine every relevant branch and find a solution if one exists. It is not guaranteed to be complete on an infinite tree, because it may keep following an infinite branch and never return to explore a branch containing a solution.

DFS is not generally optimal. Its choice depends on depth and tie-breaking, not on solution cost or whether another solution is shorter.


Breadth-First Search

Breadth-first search (BFS) selects a shallowest node from the fringe. It expands the search tree one level at a time, examining plans with fewer actions before plans with more actions.

Let sss be the depth of the shallowest solution. BFS expands nodes through approximately that depth, including the work needed to select the goal. Its time and space complexities are both approximately

O(bs).O(b^s).O(bs).

BFS is complete when the branching factor is finite: if a solution exists at a finite depth, the algorithm eventually reaches that depth.

BFS finds a solution with the fewest actions. It is therefore optimal when all actions have equal cost, such as when every action costs 111. It is not generally least-cost optimal when actions have different costs: a path with fewer actions may still cost more than a longer path.

DFS and BFS trade-offs

  • BFS is useful when a shallow solution is expected or when the shortest number of actions matters. Its large fringe can require substantial memory.

  • DFS can use much less memory and may work well when solutions are deep. However, it can spend a long time exploring an unproductive branch and does not guarantee the shortest solution.

  • If memory is limited, DFS may be practical even when BFS would otherwise be attractive.


Iterative Deepening

Iterative deepening combines depth-first search’s low memory use with breadth-first search’s preference for shallow solutions.

It runs depth-limited DFS repeatedly:

  1. Search to depth 111.

  2. If no solution is found, increase the depth limit to 222.

  3. Continue increasing the limit until a solution is found.

At each iteration, the successor function is treated as providing no further progress beyond the current limit. The algorithm therefore does not follow a branch indefinitely during a single run.

The method repeats some work: shallow nodes are revisited in later iterations. However, in a tree with exponential growth, the deepest level contains most of the nodes. The repeated work in the shallower levels is consequently small compared with the work required at the final depth.

Iterative deepening retains DFS-like space use, approximately

O(bm),O(bm),O(bm),

while finding a shallowest solution like BFS when actions have equal cost. Its time complexity is approximately

O(bs),O(b^s),O(bs),

where sss is the depth of the shallowest solution.


Uniform-Cost Search

BFS orders nodes by depth, which is equivalent to cost only when every action has the same cost. Uniform-cost search (UCS) handles varying action costs by expanding the fringe node with the lowest cumulative path cost so far.

For a path, its cumulative cost is the sum of the costs of its actions. UCS proceeds by:

  1. Starting with the initial state at cost 000.

  2. Selecting the fringe path with the lowest cumulative cost.

  3. Testing its endpoint for the goal.

  4. If it is not a goal, expanding it and adding its successors with their updated cumulative costs.

  5. Repeating until a goal is selected.

UCS does not stop simply because a goal has been generated. It continues selecting the lowest-cost fringe path. A cheaper route to a goal might still be found through a path that has not yet been expanded.

Why UCS is optimal

Suppose the selected goal path has cost C∗C^*C∗. At the moment it is selected, all remaining fringe paths have cost at least C∗C^*C∗. If every action has positive cost, extending one of those paths cannot produce a goal path cheaper than C∗C^*C∗. Thus, the selected goal is least-cost optimal.

The guarantee depends on suitable cost assumptions. In particular, action costs must not allow search to improve a path indefinitely through negative-cost cycles. With positive costs and a finite branching factor, UCS is complete when a solution of finite cost exists.

Effective depth and complexity

Let:

  • C∗C^*C∗ be the cost of an optimal solution.

  • ϵ\epsilonϵ be a positive lower bound on the cost of any action.

  • bbb be the branching factor.

A path containing more than approximately C∗ϵ\frac{C^*}{\epsilon}ϵC∗​ actions must cost more than C∗C^*C∗. This gives an effective depth of about

C∗ϵ.\frac{C^*}{\epsilon}.ϵC∗​.

The work and memory required can be exponential in this effective depth, roughly

O(bC∗/ϵ).O\left(b^{C^*/\epsilon}\right).O(bC∗/ϵ).

UCS is complete and optimal under its cost assumptions, but it may explore many directions without using information about where the goal is. It prefers paths that have been cheap so far, not paths that appear promising because they are near the goal. Search methods that use additional guidance to focus exploration are a natural extension.


One Search Procedure, Different Strategies

DFS, BFS, and UCS can all be understood as tree search with different rules for selecting the next fringe node:

  • DFS: Prefer greater depth.

  • BFS: Prefer smaller depth.

  • UCS: Prefer smaller cumulative path cost.

A single implementation can support these strategies by storing the fringe in a priority queue and assigning priorities according to the algorithm’s rule. Tie-breaking may affect the order in which equally prioritized nodes are explored, but it does not replace the strategy’s main criterion.


Search and the Quality of the Model

A search algorithm solves the problem represented by its search model. A correct algorithm can still produce a poor real-world result if the model does not match the world.

For example:

  • A route planner may choose a turn that is impossible in reality because the map’s representation of a road or intersection is inaccurate.

  • A route planner may send a traveler on an implausible journey if the map omits the connection that would provide the intended route.

In either case, the difficulty may not lie in the search strategy. The route may be valid—or even optimal—in the modeled state-space graph while failing to be useful in the actual environment.

Model quality is essential: the state space, successor function, goal test, and costs must represent the real task accurately enough for the computed plan to work in practice.

Ask anything

We've read this whole study set, so questions about it get the best answers.

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent