AI Chapter 3
AI Chapter 3
Mr. Gebreyes
Initial Goal
state state
Actions
10/20/2024 Problem Solving 9
Example: Coloring problem
There are 3 rectangles. Both are initially white. The
problem is to change all rectangles with white color to
black color. Color change is one rectangle at a time.
Identify possible states and Operators?
Construct state space for coloring problem?
1 2 3 1 2 3
8 4 5 8 4
7 6 7 6 5
10/20/2024 Problem Solving 12
Water Jug problem
Consider the following problem:
You are given two jugs, a 4L one and a 3L one, a pump which
has unlimited H2O which you can use to fill the jug, and the
ground on which H2O may be poured. Neither jug has any
measuring markings on it nor any measurement instruments.
How can you get exactly 2L of H2O in the 4L jug?
State representation and initial state
We will represent a state of the problem as a tuple (x, y) where x
represents the amount of water in the 4L jug and y note 0≤x≤4, and
0≤y≤3.
Our initial State: (0,0)
Goal state = (2, y) where 0≤y≤3
Pour water from 3L jug to fill 4L jug (x, y) where 0<x+y≥4 & y>0 (4, y-(4-x))
Pour water from 4L jug to fill 3L jug (x, y) where 0<x+y≥3 & x>0 (x-(3-y), 3))
Pour all of water from 3L jug into 4L jug (x, y) where 0<x+y≤4 & y≥0 (x+y, 0)
Pour all of water from 4L jug into 3L jug (x, y) where 0<x+y≤3 & x≥0 (0, x+y)
Example- the initial and goal states for each of the following:
• Coloring problem
» Initial state: All rectangles white
» Goal state: All rectangles black
• Route finding problem
» The point where the agent start its journey (SidistKilo?)
» The point where the agent wants to reach (Stadium?)
•
10/20/2024 8-puzzle problem ?? Problem Solving 28
Operators
The set of possible actions available to the agent, i.e.
Which state(s) will be reached by carrying out the action in a
particular state
A Successor function S(x)
• Is a function that returns the set of states that are reachable from a
single state by any single action/operator
• Given state x, S(x) returns the set of states reachable from x by
any single action
• Example:
Coloring problem: Paint with black color paint (color w,
color b)
Route finding problem: Drive through cities/places drive
(place x, place y)
8-puzzle ??
10/20/2024 Problem Solving 29
Goal Test Function
The agent execute to determine if it has reached the goal state or not
Is a function which determines if the state is a goal state or not
Example:
Route finding problem: Reach Addis Ababa airport on time
IsGoal(x, Goal)
if x = Goal then
return 1
return 0
end isGoal
Example:
Route finding problem:
• Path cost from initial to goal state
Coloring problem:
• One for each transition from state to state till goal state is reached
8-puzzle?
state UPDATE-STATE(state,percept)
if seq is empty then do
goal FORMULATE-GOAL(state)
problem FORMULATE-PROBLEM(state,goal)
seq SEARCH(problem)
action FIRST(seq)
seq REST(seq)
return action
10/20/2024 Problem Solving 33
Exercise
Consider one of the following problem:
Towers of Hanoi
Tic-Tac-Toe • Backgammon
Travelling salesperson problem •
8-queens problem Robot navigation
• VLSI Layout
Remove 5 sticks
Water Jug Problem(3:4; 4:5; 2:5) • The Knuth Sequence
River Crossing Puzzles
• Missionary-and-cannibal problem
• Goat, Wolf and Cabbage problem
Monkey and Banana problem
Process or assembly planning
Nine Dots Puzzle
Counterfeit Coin Problem
1. Identify the set of states and operators; based on which construct
the state space of the problem
2. Write goal test function and also determine path cost
10/20/2024 Problem Solving 34
To Be
Continued…..!!!
Searching Strategy
SidistKilo
(b) After expanding Sidist Kilo generating a new state
AratKilo Giorgis ShiroMeda
choosing one SidistKilo
option
Informed search
Greedy search
A*-search
Iterative improvement,
Constraint satisfaction
etc.
10/20/2024 Problem Solving 46
Uninformed Search
Breadth first
Depth first
Uniform cost
Optimal path: S A G
10/20/2024 Problem Solving 50
Uniform cost Search
•The goal of this technique is to find the shortest path
to the goal in terms of cost.
–It modifies the BFS by always expanding least-cost
unexpanded node
•Implementation: nodes in list keep track of total path
length from start to that node
–List kept in priority queue ordered by path cost
A
S S S
1 10 S
B
5 5 0
S G A B C A B C A B C
1 5 15 5 15 15
G G G
15 5
11 11 10
C
Optimal path: S A G
Implementation:
expand 1st the node closest to the goal state, i.e. with evaluation
function f(n) = h(n)
h(n) = 0 if node n is the goal state
Otherwise h(n) ≥ 0; an estimated cost of the cheapest path
from the state at node n to a goal state
2 2
G
0
10/20/2024 Problem Solving 78
Local Search
• In many problems, the path to the goal is irrelevant; the
goal state itself is the solution
e.g., n-queens: Put n queens on an n × n board with no two
queens on the same row, column, or diagonal.
–In these cases, we can use local search algorithms that keep
a single current state, try to improve it.
• Being able to ignore the path offers two main advantages:
–Less memory is used, generally a constant amount.
–A reasonable solution can be found in infinite search spaces
for which systematic search would be unsuited.
• Iterative improvement algorithm (IIA) like Hill climbing,
Constraint satisfaction problem (CSP) and Map coloring
are designed for local search
– The general idea is to start with a complete configuration
and to make modifications to improve its quality
–It keeps track of only the current state and try to improve it
until goal state is reached.
10/20/2024 Problem Solving 79
Hill–climbing Algorithm
• The idea here is that we don’t keep track of our path,
we don’t check for repeated states;
–It just keep track of the present state
–it looks around current position & moves uphill or downhill.
• Hill climbing is best suited to problems where the
heuristic improves the closer it gets to the solution;
–it works poorly where there are sharp drop-offs. It assumes
that local improvement will lead to global improvement.
• Implementation:
Evaluate the successors and
chose the best state that leads us
closer to the goal (according to
the heuristic estimate), and
continue from there until the goal
state is reached
10/20/2024 Problem Solving 80
Hill climbing algorithm
algorithm hill_climbing(problem) returns goal state
variable: current and next (nodes)
current = initial state
while current ≠ NULL do
next = a highest value successor of current
if (value (next) < value (current)) then
return current
current = next
end while loop
end algorithm
• Properties
– Incomplete and not optimal: Since it didn’t remember previously
generated nodes, it may get back to the node that is already
examined and hence become infinite
–fast to reach to goal state: it just looks a head to determine if any
successor is better than the current state
–takes less memory: space requirement is constant since current
state is the only node stored
10/20/2024 Problem Solving 81
Traveling salesperson problem
• Traveling Sales person problem: Given a set of cities, with costs of
getting from one to the other, find a minimum cost route that goes
through each of them exactly once
Example: Salesperson has to visit 5 cities, and must return home
afterwards. Assume that the salesperson lives in city A and will
return back to A.
B
(125) B
C
A (125) (75)
C
C
(50)
D (125)
C
(100) (100)
D
(50)
E
(75)