AI Chapter 3
AI Chapter 3
“a path or
collection of
paths,
typically
from an
entrance to a
goal”
Cont..
● Problem solving agents are goal-directed agents:
1. Goal Formulation: Set of one or more (desirable)
world states (e.g. checkmate in chess).
2. Problem formulation: What actions and states to
consider given a goal and an initial state.
3. Search for solution: Given the problem, search for a
solution; a sequence of actions to achieve the goal
starting from the initial state.
4. Execution of the solution
Well-defined Problem and Solutions
● A problem can be defined by 5 Components :
a) Initial state: The initial state that the agent starts in.
b) Action: A description of the possible actions available to the
agent.
c) Transition model :A description of what each action does;
Initial state + Actions + Transition Model = state Space (directed-graph)
d) Path cost- functions that assigns a cost to a path
e) Goal test- test to determine the goal state
States
● A problem is defined by its elements and their relations.
● In each instant of the resolution of a problem, those
elements have specific descriptors (How to select them?) and
relations.
● A state is a representation of those elements in a given
moment.
● Two special states are defined:
– Initial state (starting point)
– Final state (goal state)
State space
● The state space is the set of all states reachable from the
initial state.
● It forms a graph (or map) in which the nodes are states and the
arcs between nodes are actions.
● A path in the state space is a sequence of states
connected by a sequence of actions.
● The solution of the problem is part of the map formed by
the state space.
Problem solution
● A solution in the state space is a path from the initial state to a goal
state or, sometimes, just a goal state.
● Path/solution cost: function that assigns a numeric cost to each
path, the cost of applying the operators to the states
● Solution quality is measured by the path cost function, and an optimal
solution has the lowest path cost among all solutions.
● Solutions: any, an optimal one, all. Cost is important depending on the
problem and the type of solution required.
Example A: A Tourist in Romania ….. From Arad
Going to Bucharest
●
3rd step:
●
List all the action in the production rule(as rules or operators as
follows)
Possible answers
Option 2
Option 1
(4,0)
(0,3)
(1,3)
(3,0)
(1,0)
(3,3)
(0,1)
(4,2)
(4,1)
(0,2)
(2,3)
(2,0)
(2,0)
Example C: The wolf-goat-cabbage problem
Cont..
A farmer has a goat a wolf and a cabbage on the west side of the
river. He wants to get all of his animals and his cabbage across the
river onto the cost side. The farmer has a boat but he only has
enough room for himself and one other thing.
Case 1: The goat will eat the cabbage if they are left together alone.
Case 2: The wolf will eat the goat if they are left alone. How
can the farmer get everything on the other side?
Possible solution
● State Space Representation:
● We can represent the states of the problem with tow sets W and E. We
can also can have representations for the elements in the two sets as
f,g,w,c representing the farmer, goat, wolf, and cabbage.
● Actions :
● Start state:
● W={f,g,c,w), E={}
● Goal state:
● W={},E={f,g,c,w}
One possible Solution:
● Farmer takes goat across the river, W={w,c},E={f,g}
● Farmer comes back alone, W={f,c,w,},E={g}
● Farmer takes wolf across the river, W={c},E=f,g,w}
● Farmer comes back with goat, W=={f,g,c},E={w}
● Farmer takes cabbage across the river, W={g},E={f,w,c}
● Farmer comes back alone, W={f,g}, E={w,c}
● Farmer takes goat across the river, W={},E={f,g,w,c}
Exercise
1. The missionaries and cannibals problem is usually stated as
follows. Three missionaries and three cannibals are on one side of
a river, along with a boat that can hold one or two people. Find a
way to get everyone to the other side without ever leaving a group
of missionaries in one place outnumbered by the cannibals in that
place.
A. Formulate the problem precisely, making only those distinctions
necessary to ensure a valid solution.
2. For each of the following activities, give a PEAS
description of the task environment
S h o p p i n g for used AI books on the Internet.
P l a y i n g soccer
Search Algorithm Terminologies
●
Search: Searching is a step by step procedure to solve a search-problem in a given
search space. A search problem can have three main factors:
● Search Space: Search space represents a set of possible solutions, which a
system may have.
●
Start State: It is a state from where agent begins the search.
●
Goal test: It is a function which observe the current state and returns whether
the goal state is achieved or not.
●
Search tree: A tree representation of search problem is called Search tree. The
root of the search tree is the root node which is corresponding to the initial state.
Path Cost: It is a function which assigns a numeric cost to each path.
●
Solution: It is an action sequence which leads from the start node to the goal node.
●
Optimal Solution: If a solution has the lowest cost among all solutions.
Properties of Search Algorithms
● Following are the four essential properties of search
algorithms to compare the efficiency of these algorithms
● Completeness: Is the algorithm guaranteed to find a
solution when there is one?
● Optimality: Does the strategy find the optimal solution
● Time Complexity: How long does it take to find a
solution?
● Space Complexity: How much memory is needed
to perform the search?
Cont..
● Different search strategies are evaluated along
completeness, time complexity, space complexity and
optimality.
● The time and space complexity are measured in terms of:
● b: the branching factor or maximum number of
successors of any node.
● d: depth of the solution or the number of steps along
the path from the root
● m: the maximum length of any path in the state space.
Types of search algorithms
● Based on the search problems we can classify the
search algorithms into uninformed (Blind search) search and
informed search (Heuristic search) algorithms.
I. Uninformed/Blind Search
● The uninformed search does not contain any domain
knowledge such as closeness, the location of the goal.
● It operates in a brute-force way as it only includes
information about how to traverse the tree and how to identify
leaf and goal nodes.
● Uninformed search applies a way in which search tree is
searched without any information about the search space like initial
state operators, so it is also called blind search.
Cont..
● It examines each node of the tree until it achieves the goal
node.
● It can be divided into five main types:
1. Breadth-first search
2. Depth-first search
3. Uniform cost search
4. Iterative deepening depth-first search
5. Bidirectional Search
1. Breadth-first Search (BFS)
● Breadth-first search is the most common search strategy for
traversing a tree or graph.
● This algorithm searches breadthwise in a tree or graph, so it is
called breadth-first search.
● BFS algorithm starts searching from the root node of the tree and
expands all successor node at the current level before moving
to nodes of next level.
● The breadth-first search algorithm is an example of a
general-graph search algorithm.
Cont..
● Breadth-first search implemented using FIFO queue data
structure.
● Advantages:
● BFS will provide a solution if any solution exists.
● If there are more than one solutions for a given problem, then
BFS will provide the minimal solution which requires the least
number of steps.
Cont..
Disadvantages:
● It requires lots of memory since each level of the tree
must be saved into memory to expand the next level.
● BFS needs lots of time if the solution is far away from
the root node.
Example:
● Traversing of the tree using BFS algorithm from the root
node S to goal node K.
BFS search algorithm traverse in layers, so it will follow the path
which is shown by the dotted arrow, and the traversed path will be:
S---> A--->B---->C--->D---->G--->H--->E---->F---->I---->K
Cont..
The process constructs a search tree is as follows, where
Search tree may be infinite because of loops even if state space is small
• The search problem will return as a solution; a path to a goal node.
• Treat agenda as queue
Expansion: put children at the end of the queue; Get new nodes from the
front of the queue
Search tree
2014 32
BFS illustrated
Step 1: Initially fringe contains only one node corresponding to the
source state A.
Fringe: A
2014 33
BFS illustrated
Step 2: A is removed from fringe. The node is expanded, and its
children B and C are generated. They are placed at the back of
fringe.
Fringe: BC
2014 34
BFS illustrated
Step 3: Node B is removed from fringe and is expanded. Its
children D, E are generated and put at the back of fringe.
Fringe: CDE
2014 35
BFS illustrated
Step 4: Node C is removed from fringe and is expanded. Its
children D and G are added to the back of fringe.
Fringe: DEDG
2014 36
BFS illustrated
Step 5: Node D is removed from fringe. Its children C and F are
generated and added to the back of fringe.
Fringe: EDGCF
2014 37
BFS illustrated
Step 6: Node E is removed from fringe. It has no children.
Fringe: DGCF
2014 38
BFS illustrated
Step 7: D is expanded, B and F are put in OPEN.
Fringe: GCFBF
2014 39
cont..
2014 40
Cont..
● Time Complexity: Time Complexity of BFS algorithm can be
obtained by the number of nodes traversed in BFS until the
shallowest Node. Where the d = depth of shallowest solution and
b is a node at every state. T (b) = 1+b2+b3+.......+ bd= O (bd)
● Space Complexity: Space complexity of BFS algorithm is given
by the Memory size of frontier which is O(bd).
● Completeness: BFS is complete, which means if the shallowest
goal node is at some finite depth, then BFS will find a solution.
● Optimality: Yes (if cost = 1 per step).
2. Depth-first Search
● Depth-first search is a recursive algorithm for traversing a tree or
graph data structure.
● It starts from the root node and follows each path to its greatest
depth node before moving to the next path.
● DFS uses a stack data structure for its implementation.
● The process of the DFS algorithm is similar to the BFS
algorithm.
Cont..
Advantage:
● DFS requires very less memory as it only needs to store a
stack of the nodes on the path from root node to the current
node.
Disadvantage:
● There is the possibility that many states keep re-
occurring, and there is no guarantee of finding the
solution.
● DFS algorithm goes for deep down searching and
sometime it may go to the infinite loop.
Cont..
Example:
● In the below search tree, we have shown the flow of
depth-first search, and it will follow the order as:
● Root node--->Left node ----> right node.
● It will start searching from root node S, and traverse A, then B,
then D and E, after traversing E, it will backtrack the tree as E
has no other successor and still goal node is not found.
● After backtracking it will traverse node C and then G, and
here it will terminate as it found goal node.
cont..
Search tree for the state space
Let us now run Depth First Search on the search space given
in Figure below, and trace its progress
2010 46
DFS illustrated
Fringe: A
2010 47
DFS illustrated
Step 2: A is removed from fringe. A is expanded and its
children B and C are put in front of fringe
Fringe: BC
2010 48
DFS illustrated
Step 3: Node B is removed from fringe, and its children D and E
are pushed in front of fringe.
Fringe: DEC
2010 49
DFS illustrated
Step 4: Node D is removed from fringe. C and F are pushed in front of
fringe.
Fringe: CFEC
2010 50
DFS illustrated
Step 5: Node C is removed from fringe. Its child G is pushed in front
of fringe.
Fringe: GFEC
2010 51
DFS illustrated
Step 6: Node G is expanded and found to be a goal node. The
solution path A-B-D-C-G is returned and the algorithm terminates.
Goal.
Fringe: GFEC
2010 52
Cont..
● 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.
● Complete: A* algorithm is complete as long as:
● Branching factor is finite. Cost at
● every action is fixed.
Cont..
● 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(b^d), where b is the branching factor.
● Space Complexity: The space complexity of A* search algorithm is O(b^d)
Exercise: Find the path from S to G using greedy search and A* search.
Search Algorithms So Far
●
Designed to explore search space systematically:
• keep one or more paths in memory
• record which have been explored and which have not
• a path to goal represents the solution
Local Search Algorithms
• In many optimization problems, the path to the goal is
irrelevant, the goal state itself is the solution.
• In such cases, we can use local search algorithms
• Search algorithms that maintain a single node and searches by
moving to a neighboring node
Local Search Algorithms
• Keep a single "current" state;
use very little memory, usually a constant amount
find reasonable solutions in large or infinite state
spaces for which systematic solutions are unsuitable
useful for solving optimization problems
• Types of local searches Algorithm
1. Hill-climbing Search
2. Simulated Annealing
3. Local Beam Search
4. Genetic Algorithm