Unit 2 CH 2
Unit 2 CH 2
Unit – 2 (Part – 1)
Problem Solving
Topics to be covered
Looping
Problem Solving Agents
Well-defined problems and solutions
Example Problems
Toy problems
Real-world problems
Searching for Solution
Infrastructure for search algorithms
Measuring problem-solving performance
Search Strategies / Search Algorithms
Uninformed Search Strategies
Informed (Heuristic) Search Strategies
Local Search Algorithms and Optimization Problems
Hill-climbing search
Problem Solving Agents
Problem solving agents are goal-based agents. It decides what to do by finding a
sequence of actions that leads to a desirable state or solution.
An agent may need to plan when the best course of action is not immediately
visible. They may need to think through a series of moves that will lead them to
their goal state. Such an agent is known as a problem-solving agent, and the
computation it does is known as a search.
The problem-solving agent follows this four-phase problem solving process:
1. Goal Formulation: This is the first and most basic phase in problem solving. It
arranges specific steps to establish a target/goal that demands some activity to
reach it. AI agents are now used to formulate goals.
2. Problem Formulation: It is one of the fundamental steps in problem-solving that
determines what action should be taken to reach the goal.
Problem Solving Agents
3. Search: After the Goal and Problem Formulation, the agent simulates sequences
of actions and has to look for a sequence of actions that reaches the goal. This
process is called search, and the sequence is called a solution.
The agent might have to simulate multiple sequences that do not reach the
goal, but eventually, it will find a solution, or it will find that no solution is
possible.
A search algorithm takes a problem as input and outputs a sequence of
actions.
4. Execution: After the search phase, the agent can now execute the actions that
are recommended by the search algorithm, one at a time. This final stage is
known as the execution phase.
Well-defined problems and solutions
A problem can be defined formally by five components:
1. Initial State: It is the agent’s starting state or initial step towards its goal.
For example, if a taxi agent needs to travel to a location (B), but the taxi is
already at location (A), the problem’s initial state would be the location (A).
2. Actions: It is a description of the possible actions that the agent can take.
Given a state s, Actions(s) returns the set of actions that can be executed in
s. Each of these actions is said to be appropriate in s.
3. Transition Model: It describes what each action does.
It is specified by a function Result(s,a) that returns the state that results
from doing action a in state s.
The initial state, actions, and transition model together define the state
space of a problem.
Well-defined problems and solutions
The state space forms a graph in which the nodes are states, and the links
between the nodes are actions. A path in the state space is a sequence of
states connected by a sequence of actions.
4. Goal Test: It determines whether a given state is a goal state.
Sometimes there is an explicit list of potential goal states, and the test
simply checks whether the given state is one of them.
5. Path Cost: It assigns a numerical cost to each path that leads to the goal.
The problem-solving agents choose a cost function that matches its
performance measure.
Remember that the optimal solution has the lowest path cost of all the
solutions.
Example Problems
The problem-solving approach has been used in a wide range of work contexts.
There are two kinds of problem approaches:
1. Standardized/Toy problems: Its purpose is to demonstrate or practice various
problem solving techniques.
It can be described concisely and precisely, making it appropriate as a
benchmark for academics to compare the performance of algorithms.
2. Real-world problems: It is real-world problems that need solutions.
It does not rely on descriptions, unlike a toy problem, yet we can have a basic
description of the issue.
Toy problems: Vacuum world problem
Figure: The state space for the vacuum world. Links denote actions: L = Left, R = Right, S = Suck.
Toy problems: Vacuum world problem
Let us take a vacuum cleaner agent and it can move left or right and its jump is to
suck up the dirt from the floor.
This can be formulated as a problem as follows:
States: The state is determined by both the agent location and the dirt locations.
The agent is in one of two locations, each of which might or might not contain dirt.
Thus, there are 2 × 22 = 8 possible world states. A larger environment with n
locations has n × 2n states.
Initial state: Any state can be designated as the initial state.
Actions: In this simple environment, each state has just three actions: Left, Right,
and Suck. Larger environments might also include Up and Down.
Toy problems: Vacuum world problem
Transition model: The actions have their expected effects, except that moving
Left in the leftmost square, moving Right in the rightmost square, and Sucking in a
clean square have no effect. The complete state space is shown in figure.
Goal test: This checks whether all the squares are clean.
Path cost: Each step costs 1, so the path cost is the number of steps in the path.
Toy problems: 8-puzzle problem
The 8-puzzle, an instance of which is
shown in Figure, consists of a 3×3
board with eight numbered tiles and a
blank space.
A tile adjacent to the blank space can
slide into the space. The object is to
reach a specified goal state, such as
the one shown on the right of the
Figure: : A typical instance of the 8-puzzle.
figure.
Toy problems: 8-puzzle problem
The standard formulation is as follows:
States: A state description specifies the location of each of the eight tiles and the
blank in one of the nine squares.
Initial state: Any state can be designated as the initial state. Note that any given
goal can be reached from exactly half of the possible initial states.
Actions: The simplest formulation defines the actions as movements of the blank
space Left, Right, Up, or Down. Different subsets of these are possible depending
on where the blank is.
Transition model: Given a state and action, this returns the resulting state; for
example, if we apply Left to the start state in figure, the resulting state has the 5
and the blank switched.
Toy problems: 8-puzzle problem
Goal test: This checks whether the state matches the goal configuration shown in
figure. (Other goal configurations are possible).
Path cost: Each step costs 1, so the path cost is the number of steps in the path.
The 8-puzzle belongs to the family of sliding-block puzzles, which are often used
as test problems for new search algorithms in AI.
The 8-puzzle has 9!/2=181,440 reachable states and is easily solved. The 15-
puzzle (on a 4×4 board) has around 1.3 trillion states, and random instances can
be solved optimally in a few milliseconds by the best search algorithms.
The 24-puzzle (on a 5 × 5 board) has around 1025 states, and random instances
take several hours to solve optimally.
Toy problems: 8-queens problem
The goal of the 8-queens problem is to
place eight queens on a chessboard
such that no queen attacks any other.
(A queen attacks any piece in the
same row, column or diagonal).
Figure shows an attempted solution
that fails: the queen in the rightmost
column is attacked by the queen at the
top left.
Figure: Partial search trees for finding a route from Arad to Bucharest.
Searching for Solutions
Here, nodes that have been expanded are shaded; nodes that have been
generated but not yet expanded are outlined in bold; nodes that have not yet been
generated are shown in faint dashed lines.
Figure shows the first few steps in growing the search tree for finding a route
from Arad to Bucharest. The root node of the tree corresponds to the initial state,
In(Arad).
The first step is to test whether this is a goal state. Then we need to consider
taking various actions. We do this by expanding the current state; that is, applying
each legal action to the current state, thereby generating a new set of states.
In this case, we add three branches from the parent node In(Arad) leading to three
new child nodes: In(Sibiu), In(Timisoara), and In(Zerind). Now we must choose
which of these three possibilities to consider further.
Searching for Solutions
This is the essence of search - following up one option now and putting the others
aside for later, in case the first choice does not lead to a solution.
Suppose we choose Sibiu first. We check to see whether it is a goal state (it is
not) and then expand it to get In(Arad), In(Fagaras), In(Oradea), and
In(RimnicuVilcea).
We can then choose any of these four or go back and choose Timisoara or Zerind.
Each of these six nodes is a leaf node, that is, a node with no children in the tree.
The set of all leaf nodes available for expansion at any given point is called the
frontier (Open list).
In Figure, the frontier of each tree consists of those nodes with bold outlines.
The process of expanding nodes on the frontier continues until either a solution is
found or there are no more states to expand.
Searching for Solutions
The general algorithm is shown
informally in figure.
Search algorithms all share this
basic structure; they vary
primarily according to how they
choose which state to expand
next - the so-called search
strategy.
Note: At each point in the search space, only that node is expanded which have
the lowest value of f(n), and the algorithm terminates when the goal node is
found.
A* search algorithm
Algorithm:
1. Place the beginning node in the OPEN list as the first step.
2. Check if the OPEN list is empty or not, if the list is empty then return failure and
stops.
3. Choose the node with the shortest value of the evaluation function (g+h) from the
OPEN list; if node n is the goal node, return success and stop; otherwise, return
failure and continue.
4. Expand node n and create all its descendants, then place n in the CLOSED list.
Check whether n' is already in the OPEN or CLOSED list for each successor n'; if
not, compute the evaluation function for n' and insert it in the OPEN list.
5. If node n' is already in OPEN and CLOSED, then it should be attached to the back
pointer which reflects the lowest g(n') value.
6. Return to Step 2.
A* search algorithm
Example – In this example, we will
traverse the given graph using the
A* algorithm.
The heuristic value of all states is
given in the table so we will
calculate the f(n) of each state
using the formula
f(n)= g(n) + h(n)
where g(n) is the cost to reach any
node from start state.
A* search algorithm
Initialization: {(S, 5)}
Iteration1: {(S A, 4), (S G, 10)}
Iteration2: {(S A C, 4), (S A B, 7), (S G, 10)}
Iteration3: {(S A C G, 6), (S A C D, 11), (S A B, 7), (S G, 10)}
Iteration 4: will give the final result, as S A C G it provides the optimal path
with cost 6.
Points to remember –
A* algorithm returns the path which occurred first, and it does not search for all
remaining paths.
The efficiency of A* algorithm depends on the quality of heuristic.
A* algorithm expands all nodes which satisfy the condition f(n).
A* search algorithm
Advantages –
When compared to other search algorithms, the A* search method is the best.
The A* search algorithm is ideal and comprehensive.
This algorithm can solve very complex problems.
Disadvantages –
It does not always produce the shortest path as it mostly based on heuristics
and approximation.
The A* search algorithm has some concerns with complexity.
The fundamental disadvantage of A* is that it requires a lot of memory
because it maintains all created nodes in memory, which makes it unsuitable
for a variety of large-scale issues.
A* search algorithm
Complete: A* algorithm is complete as long as:
Branching factor is finite.
Cost at every action is fixed.
Optimal: A* search algorithm is optimal if it follows below two conditions:
Admissible: the first condition requires for optimality is that h(n) should be an admissible
heuristic for A* tree search. An admissible heuristic is optimistic in nature.
Consistency: Second required condition is consistency for only A* graph-search.
If the heuristic function is admissible, then A* tree search will always find the least cost
path.
Time Complexity: The time complexity of A* search algorithm depends on heuristic function,
and the number of nodes expanded is exponential to the depth of solution d. So, the time
complexity is O(bd), where b is the branching factor.
Space Complexity: The space complexity of A* search algorithm is O(bd).
Exercise: A* search algorithm - Example
Find the route between A and Z using A* algorithm. Values written at each vertex
shows the straight line distance heuristic function.
Exercise: A* search algorithm - Example
Apply the steps of the A* Search algorithm to find the shortest path from A to Z
using the following graph:
Exercise: A* search algorithm - Example
Apply the steps of the A* Search algorithm to find the shortest path from A to Z
using the following graph:
Local Search Algorithms and Optimization Problem
The informed and uninformed search expands the nodes systematically in two
ways:
keeping different paths in the memory and
selecting the best suitable path,
which leads to a solution state required to reach the goal node.
But beyond these “classical search algorithms”, we have some “local search
algorithms” where the path cost does not matter, and only focus on solution-
state needed to reach the goal node.
A local search algorithm completes its task by traversing on a single current node
rather than multiple paths and following the neighbours of that node generally.
Local Search Algorithms and Optimization Problem
Although local search algorithms are not systematic, still they have the
following two advantages:
Local search algorithms use a very little or constant amount of memory as
they operate only on a single path.
Most often, they find a reasonable solution in large or infinite state spaces
where the classical or systematic algorithms do not work.
The local search algorithm works for pure optimized problems.
A pure optimization problem is one where all the nodes can give a solution.
But the target is to find the best state out of all according to the objective
function.
Unfortunately, the pure optimization problem fails to find high-quality solutions to
reach the goal state from the current state.
Hill-climbing search
Hill climbing search is a local search problem.
The purpose of the hill climbing search is to climb a hill and reach the topmost
peak/point of that hill.
It is based on the heuristic search technique where the person who is climbing up
on the hill estimates the direction which will lead him to the highest peak.
Features of Hill-climbing –
Generate and Test variant: Hill-climbing is the variant of Generate and Test method. The
Generate and Test method produce feedback which helps to decide which direction to
move in the search space.
Greedy approach: Hill-climbing algorithm search moves in the direction which optimizes
the cost.
No backtracking: It does not backtrack the search space, as it does not remember the
previous states.
State-space landscape of Hill-climbing algorithm
To understand the concept of hill climbing algorithm, consider the below
landscape representing the goal state/peak and the current state of the climber.
State-space landscape of Hill-climbing algorithm
The topographical regions shown in the figure can be defined as:
Global Maximum: It is the highest point on the hill, which is the goal state.
Local Maximum: It is the peak higher than all other peaks but lower than the
global maximum.
Flat local maximum: It is the flat area over the hill where it has no uphill or
downhill. It is a saturated point of the hill.
Shoulder: It is also a flat area where the summit (mountain top) is possible.
Current state: It is the current position of the person.
Types of Hill-climbing search algorithms
There are following types of hill-climbing search:
Simple hill-climbing
Steepest-ascent hill-climbing
Stochastic hill-climbing
Random-restart hill-climbing
Types of Hill-climbing search algorithms
Simple hill-climbing
Simple hill-climbing is the simplest technique to climb a hill. The task is to reach
the highest peak of the mountain.
Here, the movement of the climber depends on his move/steps. If he finds his
next step better than the previous one, he continues to move else remain in the
same state.
This search focus only on his previous and next step.
This algorithm has the following features:
Less time consuming.
Less optimal solution and the solution is not guaranteed.
Types of Hill-climbing search algorithms
Simple hill-climbing algorithm:
Step 1: Evaluate the initial state, if it is goal state then return success and stop.
Step 2: Loop Until a solution is found or there is no new operator left to apply.
Step 3: Select and apply an operator to the current state.
Step 4: Check new state:
a. If it is goal state, then return success and quit.
b. Else if it is better than the current state then assigns new state as a
current state.
c. Else if not better than the current state, then return to step2.
Step 5: Exit.
Types of Hill-climbing search algorithms
Steepest-ascent hill-climbing
Steepest-ascent hill-climbing is different from simple hill climbing search.
Unlike simple hill climbing search, it considers all the successive nodes,
compares them, and choose the node which is closest to the solution.
Steepest hill-climbing search is similar to best-first search because it focuses on
each node instead of one.
Note: Both simple as well as steepest-ascent hill climbing search fails when there
is no closer node.
Types of Hill-climbing search algorithms
Steepest-ascent hill climbing algorithm:
Step 1: Evaluate the initial state, if it is goal state then return success and stop, else make
current state as initial state.
Step 2: Loop until a solution is found or the current state does not change.
a. Let SUCC be a state such that any successor of the current state will be better than
it.
b. For each operator that applies to the current state:
a) Apply the new operator and generate a new state.
b) Evaluate the new state.
c) If it is goal state, then return it and quit, else compare it to the SUCC.
d) If it is better than SUCC, then set new state as SUCC.
e) If the SUCC is better than the current state, then set current state to SUCC.
Step 3: Exit.
Types of Hill-climbing search algorithms
Stochastic hill-climbing
Stochastic hill-climbing does not focus on all the nodes.
It selects one node at random and decides whether it should be expanded or
search for a better one.
Random-restart hill climbing
Random-restart algorithm is based on try and try strategy.
It iteratively searches the node and selects the best one at each step until the goal
is not found.
The success depends most commonly on the shape of the hill.
If there are few plateaus, local maxima, and ridges, it becomes easy to reach the
destination.
Limitations of Hill-climbing algorithm
Hill climbing algorithm is a fast and furious approach. It finds the solution state
rapidly because it is quite easy to improve a bad state. But there are following
limitations of this search:
Local Maximum: A local maximum is a peak state in the landscape which is better
than each of its neighbouring states, but there is another state also present which
is higher than the local maximum.
Solution: Backtracking technique can be a
solution of the local maximum in state
space landscape. Create a list of the
promising path so that the algorithm can
backtrack the search space and explore
other paths as well.
Limitations of Hill-climbing algorithm
Plateau: A plateau is the flat area of the search space in which all the neighbour
states of the current state contain the same value, because of this algorithm does
not find any best direction to move. A hill-climbing search might be lost in the
plateau area.
Solution: The solution for the plateau is
to take big steps or very little steps while
searching, to solve the problem.
Randomly select a state which is far
away from the current state so it is
possible that the algorithm could find
non-plateau region.
Limitations of Hill-climbing algorithm
Ridges: A ridge is a special form of the local maximum. It has an area which is
higher than its surrounding areas, but itself has a slope, and cannot be reached in
a single move.