Unit II
Unit II
n
t
n
✓ UNIT-II
Looping
Problem Solving and Searching Techniques: Problem Characteristics,
Production Systems, Control Strategies, Breadth First Search, Depth First
Search, Hill climbing and its Variations, Heuristics Search Techniques:
Best First Search, A* algorithm, Constraint Satisfaction Problem,
Introduction to Game Playing, Min-Max and Alpha-Beta pruning
algorithms.
Outline
▪ Problem Solving
▪ Production Systems
▪ Searching Techniques
▪ Constraint Satisfaction Problem
▪ Game Playing
Problem Solving
Problem solving
Problem solving:-It is a process of generating solution from observed data.
Problem is characterize by set of goals, set of object and set of operations.
For AI system implementation, problem must be well defined.
A well-defined problem must have five components:-
Initial State: Start point of problem
Final State: The finish point of problem. Aka Goal or solution state
States: Total states in problem
Transition Model: How one can shift from one state to another
Actions: Actions set, used to move from one state to another
Path Cost: What is total effort (cost) from initial state to final state
Problem Statement
Definition
Limitation or Constraints or Restrictions
Problem Solution
Solution Space
Operators
Problem Formulation
Examples of Problem Formulation-Mouse Path Problem
Problem Statement
Problem Definition: Mouse is hungry, mouse is in a puzzle where
there are some cheese. Mouse will only be satisfied if mouse eat
cheese
Problem Limitation: Some paths are close i-e dead end, mouse can
only travel through open paths
Problem Solution: Reach location where is cheese and eat minimum
one cheese. There are possible solutions (cheese pieces)
Solution Space: To reach cheese there are multiple paths possible
Operators: Mouse can move in four possible directions, these directions
are operators or actions which are UP, DOWN, LEFT and RIGHT
Water Jug Problem
Problem statement: Given two jugs, a 4-gallon and 3-gallon having no measuring
markers on them. There is a pump that can be used to fill the jugs with water. How
can you get exactly 2 gallons of water into 4- gallon jug.
Solution: State for this problem can be described as the set of ordered pairs of
integers (X, Y) such that
X represents the number of gallons of water in 4-gallon jug and
Y for 3-gallon jug.
Start state is (0,0)
Goal state is (2, N) for any value of N.
To solve this problem we make certain rules which help us to make action
Rules for resolve this problem.
Rule 1: Fill jug 1.
Rule 2: Fill jug 2.
Rule 3: Empty jug 1.
Rule 4: Empty jug 2.
Rule 5: Transfer some water from jug 2 to jug 1 until jug 1 is full.
Rule 6: Transfer some water from jug 1 to jug 2 until jug 2 is full.
Rule 7: Transfer all water from jug 2 to jug 1.
Rule 8: Transfer all water from jug 1 to jug 2.
Few examples(Constraint Satisfaction Problem)
8 Queens Problem
States: Any arrangement of 0 to 8 queens on the chess board is a state.
Initial state: No queens on the board or randomly shuffled 8 queens on board
Actions: Add a queen to any empty square or move queens one by one
Transition model: Returns the board with a queen added to the specified square.
Goal state: 8 queens are on the board, none attacked.
NXN square board , N queen need to be placed
Following limitation /constraint need to be satisfied.
(i)No raw should contain more than one queen.
(ii)No column should contain more than one queen
(iii)No diagonal should contain more than one queen
There should not be vacant any row or column.
Continuee..
Production Systems
What is a production system?
A production system is a set of rules or procedures for carrying out a task. In artificial
intelligence, production systems are used to create programs that can solve problems.
Production systems are made up of a set of production rules. Each rule has a condition and an
action. The condition is tested to see if it is true. If the condition is true, the action is carried
out.
Production systems are used in many different areas of artificial intelligence. They are used in
expert systems, natural language processing, and machine learning.
components of a production system
A production system is a set of rules or procedures that are followed in order to produce a
desired outcome. In AI, production systems are used to create intelligent agents that can act
and make decisions on their own.
1. A knowledge base: This is a collection of facts and information that the production system
can use to make decisions.
Production Systems
2. Inference engine: This is the part of the system that uses the knowledge base
to make decisions.
3. Working memory: This is where the production system stores information about
the current situation.
4. Control strategy: This is the set of rules that the production system uses to
decide what actions to take.
production systems work
Production systems are similar to decision trees, but they are more flexible and can be adapted
to changing conditions.
Production systems are made up of a set of rules, or productions, that are triggered by certain
conditions. Productions can be thought of as if-then statements: if a certain condition is met,
then a certain action is taken.
Production Systems
Production systems are often used in robotics, because they can handle complex
tasks that require many different actions. For example, a robot might need to pick
up a object, move it to another location, and then put it down. A production
system could be used to program the robot to do all of these actions.
Production systems are also used in artificial intelligence applications such as expert systems.
Expert systems are programs that use a set of rules to make decisions, just like humans do. For
example, a medical expert system might be used to diagnose a patient's illness.
benefits of using production systems
most important benefits is that production systems can be used to create intelligent agents.
Intelligent agents are able to reason and make decisions on their own.
they can be used to create expert systems. Expert systems are AI applications that are
designed to solve problems in a specific domain.
create decision support systems. Decision support systems are AI applications that help
humans make better decisions.
Production Systems
Characteristics of a Production System
There are mainly four characteristics of the production system in AI that is
simplicity, modifiability, modularity, and knowledge-intensive.
Simplicity
This is in the form of an ‘IF-THEN’ statement.
Every rule in the production system has a unique structure.
Modifiability:-It may modifiable if any change required in near future.
Modularity
production rule helps in its incremental improvement as the production rule can be in discrete
parts.
Modularity helps enhance the performance of the production system by adjusting the
parameters of the rules.
Production Systems
Inefficiency
There are various rules that we employ for solving a problem. The rules can be effective in different
ways. There are conditions where multiple rules get activated during execution. All the individual rules
apply exhaustive searches in each cycle that reduces the efficiency of the production system.
Inability to Learn
A simple production system based on certain rules is not capable of learning through experience, unlike
advanced AI systems. They are simply bound to specific rules for actions.
Types of production systems
Monotonic Production System
In this type of a production system, the rules can be applied simultaneously as the use of one rule does
not prevent the involvement of another rule that is selected at the same time.
Partially Commutative Production System
This class helps create a production system that can give the results even by interchanging the states of
rules. If using a set of rules transforms State A into State B, then multiple combinations of those rules
will be capable to convert State A into State B.
Production Systems
Non-monotonic Production System
These systems does not require backtracking to correct the previous incorrect moves. The
non-monotonic production systems are necessary from the implementation point of view to find
an efficient solution.
Commutative System
Commutative systems are helpful where the order of an operation is not important. Also,
problems where the changes are reversible use commutative systems. On the other hand,
partially commutative production systems help in working on problems, where the changes are
irreversible such as a chemical process. When dealing with partially commutative systems, the
order of processes is important to get the correct results.
Control Strategies
Control Strategy in Artificial Intelligence scenario is a technique or strategy, tells us about
which rule has to be applied next while searching for the solution of a problem within problem
space.
Control Strategy
Control Strategy decides which rule to apply next during the process of searching for a solution to a problem.
Requirements for a good Control Strategy
In water jug problem, if we apply a simple control strategy of starting each time from the top of rule list and choose
the first applicable one, then we will never move towards solution.
It should explore the solution space in a systematic manner
If we choose another control strategy, say, choose a rule randomly from the applicable rules then definitely it
causes motion and eventually will lead to a solution. But one may arrive to same state several times. This is
because control strategy is not systematic.
Searching:-Searching in data structure refers to the process of finding the required information from a collection
of items stored as elements in the computer memory.
These sets of items are in different forms, such as an array, linked list, graph, or tree.
These are some commonly used searching algorithms:
Linear Search: In this simple algorithm, each element in the collection is sequentially checked until the desired
item is found, or the entire list is traversed. It is suitable for small-sized or unsorted lists.
Binary Search: This algorithm is applicable only to sorted lists. It repeatedly compares the middle element of the
list with the target element and narrows down the search range by half based on the comparison result. Binary
search has a time complexity of O(log n), making it highly efficient for large sorted lists.
Linear search
Linear search is a sequential searching algorithm where we start from one end and check every
element of the list until the desired element is found.
Linear search is also called as sequential search algorithm.
It is the simplest searching algorithm.
Linear Search(steps)
Step 1 - Read the search element from the user.
Step 2 - Compare the search element with the first element in the list.
Step 3 - If both are matched, then display "Given element is found!!!" and terminate the function
Step 4 - If both are not matched, then compare search element with the next element in the list.
Step 5 - Repeat steps 3 and 4 until search element is compared with last element in the list.
Step 6 - If last element in the list also doesn't match, then display "Element is not found!!!" and
terminate the function.
Linear search
Consider the following list of elements and the element to be searched
Both are matching, so we stop comparing and display element found at index 5.
Binary search
Binary Search is a searching algorithm for finding an element's position in a sorted array.
So need to sort the array first if not sorted. Then search the element.
Step:1The array in which searching is to be performed:-
Let x = 4 be the element to be searched.
Step:2-Set two pointers low and high at the lowest and the highest positions respectively.
Step:3-Find the middle element mid of the array i.e. arr[(low + high)/2] = 6
Step:4-If x == mid, then return mid . Else, compare the element to be searched with m.
Step:5-If x > mid, compare x with the middle element of the elements on the right side of mid.
This is done by setting low to low = mid + 1.
Binary search
Step:6-Else, compare x with the middle element of the elements on the left side of mid. This is
done by setting high to high = mid - 1.
a
ab
abc
abcd
abcde
abcdef
abcdefg
Depth First Search
The Depth First Search (DFS) algorithm is a recursive algorithm that is used to
traverse or search for nodes in a graph or a tree data structure.
The algorithm begins at a specific node of the graph and explores as far as
possible in a single branch before backtracking.
The steps involved in the DFS algorithm are as follows:
Begin by creating a stack with a capacity equal to the total number of vertices
in the graph.
Select any vertex as the starting point of traversal and add it to the top of the
stack.
Look for a non-visited vertex that is adjacent to the vertex on the top of the
stack and push it onto the top of the stack.
Repeat steps 3 and 4 until there are no remaining unvisited vertices connected
to the vertex on the top of the stack.
If there are no vertices left, backtrack by popping the vertex from the top of the
stack.
Depth First Search
Stack is a linear data structure that follows a particular order in which the operations are
performed. Operated as LIFO-last in first out
We start from node 0, mark it visited, and then travel towards node 1.
Depth First Search
On reaching node 1, we see it’s unvisited — we mark it visited and move towards its child node
4.
Node 1 does not have any child left to explore (in other words, we have exhausted node 1).
Therefore, we backtrack to node 0.
Depth First Search
Node 0 is not yet completely explored — we travel to its child, node 2, and mark it visited.
Node 3 has a neighbor, node 2, but since it's already visited; so, we don’t go over it again and
backtrack to node 0. Node 0 does not have any unvisited neighbors left. In this manner, we will
visit the complete graph using DFS.
Difference
BFS DFS
Stands for “Breadth-first search” Stands for “Depth-first search”
The nodes are explored breadth wise level by level. The nodes are explored depth-wise until there are only leaf nodes
and then backtracked to explore other unvisited nodes.
BFS is performed with the help of queue data structure. DFS is performed with the help of stack data structure.
Slower in performance. Faster than BFS.
Useful in finding the shortest path between two nodes. Used mostly to detect cycles in graphs.
Depth-Limited Search
A depth-limited search algorithm is similar to depth-first search with a predetermined limit.
Depth-limited search can solve the drawback of the infinite path in the Depth-first search. In this
algorithm, the node at the depth limit will treat as it has no successor nodes further.
Depth-limited search can be terminated with two Conditions of failure:
Standard failure value: It indicates that problem does not have any solution.
Cutoff failure value: It defines no solution for the problem within a given depth limit.
Advantages:
Depth-limited search is Memory efficient.
Disadvantages:
Depth-limited search also has a disadvantage of incompleteness.
It may not be optimal if the problem has more than one solution.
Heuristic Search Techniques
Every search process can be viewed as a traversal of a directed graph, in which the nodes
represent problem states and the arcs represent relationships between states.
The search process must find a path through this graph, starting at an initial state and ending in
one or more final states.
Domain-specific knowledge must be added to improve search efficiency.
The Domain-specific knowledge about the problem includes the nature of states, cost of
transforming from one state to another, and characteristics of the goals.
This information can often be expressed in the form of Heuristic Evaluation Function.
Visiting each District of Gujarat State
• Start with any random city.
• Go to the next nearest city.
Nearest Neighbor
Heuristic
Heuristic Search Techniques
Heuristic function maps from problem state descriptions to the measures of desirability, usually
represented as numbers.
The value of the heuristic function at a given node in the search process gives a good estimate
of whether that node is on the desired path to a solution.
Well-designed heuristic functions can play an important role in efficiently guiding a search
process toward a solution.
In general, heuristic search improves the quality of the path that is explored.
In such problems, the search proceeds using current information about the problem to predict
which path is closer to the goal and follow it, although it does not always guarantee to find the
best possible solution.
Such techniques help in finding a solution within reasonable time and space (memory).
Heuristic Search Techniques
Some prominent intelligent search algorithms are stated below:
1. Generate and Test Search
2. Best-first Search
3. A* Search
4. Constraint Search
5. Means-ends analysis
Generate and Test Strategy
Generate-and-test search algorithm is a very simple algorithm that guarantees to find a solution
if done systematically and there exists a solution.
Algorithm: Generate-And-Test
1.Generate a possible solution.
2.Test to see if this is the expected solution.
3.If the solution has been found quit else go to step 1.
Potential solutions that need to be generated vary depending on the kinds of problems.
For some problems the possible solutions may be particular points in the problem space and
for some problems, paths from the start state.
Hill Climbing Example - Blocks World Problem
Start Goal
A D
0 4
D C
C B
B A
Local heuristic:
+1 for each block that is resting on the thing it is supposed to be resting on.
-1 for each block that is resting on a wrong thing.
Hill Climbing Example - Blocks World Problem
Start Goal
A D
0 4
D D +1 C
2
C C +1 B
B -1 B A +1 A
0 A
D 0
C C D C 0
Local
heuristic: B B A B A D
Hill Climbing Example - Blocks World Problem
Start Goal
A -3 D +3
-6 6
D -2 C +2
C -1 B +1
B 0 A 0
Global heuristic:
For each block that has the correct support structure:
+1 to every block in the support structure.
For each block that has a wrong support structure:
-1 to every block in the support structure.
Hill Climbing Example - Blocks World Problem
Start Goal
A D
-6 6
D D C
-3
C C B
B B A A
-6 A
D -2
C C D C -1
Global
heuristic: B B A B A D
Simple Hill Climbing - Algorithm
1. Evaluate the initial state. If it is also goal state, then return
it and quit. Otherwise continue with the initial state as the
current state.
2. Loop until a solution is found or until there are no new operators
left to be applied in the current state:
a. Select an operator that has not yet been applied to the current
state and apply it to produce a new state.
b. Evaluate the new state
i. If it is the goal state, then return it and quit.
ii. If it is not a goal state but it is better than the current state,
then make it the current state.
iii. If it is not better than the current state, then continue in the
loop.
Drawbacks of Hill Climbing ▪ Local Maxima: a local maximum is a state that is better
than all its neighbors but is not better than some other
states further away.
▪ To overcome local maximum problem: Utilize
backtracking technique. Maintain a list of visited states
and explore a new path.
In simple hill climbing, the first closer node is chosen, whereas in steepest ascent hill climbing all
successors are compared and the closest to the solution is chosen.
Best First Search
DFS is good because it allows a solution to be found without expanding all competing
branches. BFS is good because it does not get trapped on dead end paths.
Best first search combines the advantages of both DFS and BFS into a single method.
One way of combining BFS and DFS is to follow a single path at a time, but switch paths
whenever some competing path looks more promising than the current one does.
At each step of the Best First Search process, we select the most promising of the nodes we
have generated so far.
This is done by applying an appropriate heuristic function to each of them.
We then expand the chosen node by using the rules to generate its successors.
If one of them is a solution, we can quit. If not, all those new nodes are added to the set of
nodes generated so far.
How to find a better path
A Which Path is
p better?
q r s
B C D E
OR
a b c d
Z
What is Best First Search?
Unlike uninformed search where the agent blindly traverses to the next node in best first search a uses an
evaluation function to determine which neighbor node would be most appropriate to move to.
It uses the concept of a Priority queue and heuristic search.
Method of Best First Search algorithm
Create two empty lists
Start from the initial node and add it to the ordered open list
Next the below steps are repeated until the final node or endpoint is reached
If the open list is empty exit the loop and return a False statement which says that the final node cannot be
reached
Select the top node in the open list and move it to the closed list while keeping track of the parent node
If the node removed is the endpoint node return a True statement meaning a path has been found and moving the
node to the closed list
However if it is not the endpoint node then list down all the neighboring nodes of it and add them to the open list
According to the evaluation function re order the nodes.
Best First Search
Input: Graph, start node, goal node, heuristic function
Initialize an empty priority queue.
Enqueue the start node into the priority queue with its heuristic value.
Initialize an empty set to keep track of visited nodes.
While the priority queue is not empty: a. Dequeue the node with the lowest priority from the
priority queue. b. If the dequeued node is the goal node, return true (goal reached). c. Mark the
dequeued node as visited. d. Generate the child nodes of the dequeued node. e. For each child
node: i. If the child node has not been visited: — Calculate its heuristic value. — Enqueue the
child node into the priority queue with its heuristic value.
If the priority queue becomes empty and the goal node has not been reached, return false (no
solution found).
Example:
Consider a mathematical graph with nodes A, B, C, D, E, F, and G, along with their corresponding
heuristic values:
Best First Search
The goal is to find the shortest path from node ‘A’ to node ‘G’ using
Best-First Search.
Step-by-Step Explanation:
Step 1: Start with node A (heuristic value: 10).
Graph:
Step 2: Expand node A and check its neighbors.
Step 3:Choose the node with the lowest heuristic value, which is B
(heuristic value: 5).
Graph:
Best First Search
Step 4: Expand node B and check its neighbors.
Graph:
Step 5: Choose the node with the lowest heuristic value, which is D
(heuristic value: 4).
Graph:
Step 12: Since the goal node G is reached, the search is complete.
Shortest Path: A -> B -> E -> G
Best First Search
Advantages:
Best first search can switch between BFS and DFS by gaining the advantages of both the
algorithms.
This algorithm is more efficient than BFS and DFS algorithms.
Disadvantages:
It can behave as an unguided depth-first search in the worst case scenario.
It can get stuck in a loop as DFS.
This algorithm is not optimal.
What is an A* Algorithm?
It is a searching algorithm that is used to find the shortest path between an initial and a final
point.
It is a handy algorithm that is often used for map traversal to find the shortest path to be taken.
It still remains a widely popular algorithm for graph traversal.
It searches for shorter paths first, thus making it an optimal and complete algorithm. An
optimal algorithm will find the least cost outcome for a problem, while a complete algorithm
finds all the possible outcomes of a problem.
Another aspect that makes A* so powerful is the use of weighted graphs in its implementation.
A weighted graph uses numbers to represent the cost of taking each path or course of action.
A major drawback of the algorithm is its space and time complexity. It takes a large amount of
space to store all possible paths and a lot of time to find them.
A* Example
h’(n): The function h’ is an estimate of the additional cost of
f’(n) = g(n) + h’(n) getting from the current node to a goal state.
A 6 4
6 E 4
13 6 1 0
5 3
17 S B 2
F G
7
10 4 D 6
C 6
A* Example
S
16 14
18
A C
B
16 18
E D
31 17
B F
A* Example
S
16 14
18
A C
B
16 18
E D
17
F
24 19
D G
A* Example
S
16 14
18
A C
B
16 18
E D
23
36
17
B F
F
19
G
A* Example
S
16 14
18
A C
B
16 18
14 15
E D E D
22 19
17
F C F
19
G
A* Example
S
16 14
A 18
C
B
16
14 15
E D E
16
27
17
F A F
19 23 18
G D G
A* Example Solution
10
A 6 4
6 E 4
13 6 1 0
5 3
17 S B 2
F G
7
10 4 D 6
C 6
Problem
Consider the following graph-
Find the most cost-effective path to reach from
start state A to final state J using A* Algorithm.
Step-01:
We start with node A.
Node B and Node F can be reached from node
A.
A* Algorithm calculates f(B) and f(F).
f(B) = 6 + 8 = 14
f(F) = 3 + 6 = 9
Since f(F) < f(B), so it decides to go to node F.
Path- A → F
Problem
Step-02:
Node G and Node H can be reached from node F.
A* Algorithm calculates f(G) and f(H).
f(G) = (3+1) + 5 = 9
f(H) = (3+7) + 3 = 13
Since f(G) < f(H), so it decides to go to node G.
Path- A → F → G
Step-03:
Node I can be reached from node G.
A* Algorithm calculates f(I).
f(I) = (3+1+3) + 1 = 8
It decides to go to node I.
Path- A → F → G → I
Problem
Step-04:
Node E, Node H and Node J can be reached from node
I.
A* Algorithm calculates f(E), f(H) and f(J).
f(E) = (3+1+3+5) + 3 = 15
f(H) = (3+1+3+2) + 3 = 12
f(J) = (3+1+3+3) + 0 = 10
Since f(J) is least, so it decides to go to node J.
Path- A → F → G → I → J
This is the required shortest path from node A to node
J.
The A* Algorithm
This algorithm uses following functions:
1. f’: Heuristic function that estimates the merits of each node we generate. f’ represents an estimate of the
cost of getting from the initial state to a goal state along with the path that generated the current node. f’ =
g + h’
2. g: The function g is a measure of the cost of getting from initial state to the current node.
3. h’: The function h’ is an estimate of the additional cost of getting from the current node to a goal state.
Find the most cost-effective path to reach the final state from initial state using A* Algorithm.
Consider g(n) = Depth of node and h(n) = Number of misplaced tiles.
Solution-A* Algorithm maintains a tree of paths originating at the initial state.
It extends those paths as one edge at a time.
It continues until final state is reached.
A* Algorithm
Advantages:
A* search algorithm is the best algorithm than other search algorithms.
A* search algorithm is optimal and complete.
This algorithm can solve very complex problems.
Disadvantages:
It does not always produce the shortest path as it mostly based on heuristics and
approximation.
A* search algorithm has some complexity issues.
The main drawback of A* is memory requirement as it keeps all generated nodes in the
memory, so it is not practical for various large-scale problems.
Problem Reduction
AND-OR graph (or tree) is useful for representing the solution of problems that can be solved by
decomposing them into a set of smaller problems, all of which must then be solved.
This decomposition or reduction generates arcs that we call AND arcs.
One AND arc may point to any numbers of successor nodes. All of which must then be solved in
order for the arc to point solution.
In order to find solution in an AND-OR graph we need an algorithm similar to best –first search
but with the ability to handle the AND arcs appropriately.
We define FUTILITY, if the estimated cost of solution becomes greater than the value of
FUTILITY then we abandon the search, FUTILITY should be chosen to correspond to a threshold.
In following figure AND arcs are indicated with a line connecting all the components.
Thank You!