0% found this document useful (0 votes)
47 views78 pages

AI Chapter 3

Uploaded by

abduwasi ahmed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views78 pages

AI Chapter 3

Uploaded by

abduwasi ahmed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 78

Problem solving

Mr. Gebreyes

10/20/2024 Problem Solving 1


What is a Problem?
 It is a gap between what actually is and what is desired.
 A problem exists when an individual becomes aware of
the existence of an obstacle which makes it difficult to
achieve a desired goal or objective.

10/20/2024 Problem Solving 2


Types of problem in AI
Toy problems: are problems that are useful to test and
demonstrate methodologies.
• Can be used by researchers to compare the performance
of different algorithms
• may require little or no ingenuity, good for games design
• e.g. 8-puzzle, n-queens, vacuum cleaner world, towers of
Hanoi, …

Real-life problems: are problems that have much greater


commercial/economic impact if solved.
• Such problems are more difficult and complex to solve,
and there is no single agreed-upon description
• E.g. Route finding, Traveling sales person, etc.

10/20/2024 Problem Solving 3


Example problems
8-Puzzle
 Given an initial configuration of 8
numbered tiles on a 3 x 3 board, move the
tiles so as to produce a desired goal
configuration of the tiles.
Missionaries and Cannibals
 There are 3 missionaries, 3 cannibals, & 1
boat that can carry up to two people on one
side of a river. Move all the missionaries &
cannibals across the river.
Water Jug Problem
 Given a 5-gallon jug & a 2-gallon jug, with
the 5-gallon jug initially full of water and
the 2-gallon jug empty, the goal is to fill
the 2-gallon jug with exactly one gallon of 5
water. 2
10/20/2024 Problem Solving 4
Example Problems
The Eight Queens Game
 Place eight queens on a chess board in such a way that
they don't attack each other? A queen can attack
horizontally, vertically, or diagonally.
Tic Tac Toe Game
 In Tic Tac Toe, a player wins if he/she can place three of
his/her pawns adjacent to each other, either horizontally,
vertically, or diagonally forming a line.
Towers of Hanoi
 The goal is to move all the discs from the left peg to the right one.
Only one disc may be moved at a time. A disc can be placed either on
an empty peg or on top of a larger disc. Try to move all the discs using
the smallest number of moves possible.

10/20/2024 Problem Solving 5


Example problems
Route-finding problem:
Consider the agent in the city of Jimma, toward the end of touring
holiday. Flight leaves tomorrow from city Addis Ababa airport. The
agent has a ticket to fly out the following day. The ticket is non-
refundable, the agent’s visa is about to expire after tomorrow and
there are no seats available for six weeks.

10/20/2024 Problem Solving 6


Solving a problem
Formalize the problem: Identify the collection of
information that the agent will use to decide what to do.
Define states
 States describe distinguishable stages during the
problem-solving process
 Example- What are the various states in route finding
problem?
• The various places including the location of the agent
Define operators/rules
 Identify the available operators for getting from one
state to the next.
 Operators cause an action that brings transitions from
one state to another by applying on a current state.
Construct state space
 Suggest a suitable representation (such as graph, table,… or a
combination of them) to construct the state space
10/20/2024 Problem Solving 7
State Space of the Problem
 The state space defines the set of all relevant states reachable by
(any) sequence of actions from the initial state until the goal state is
reached.
 State space (also called search space/problem space) of the problem
includes the various states
 Initial state: defines where the agent starts or begins its task
 Goal state: defines the situation the agent attempts to achieve
 Transition states: other states in between initial and goal states

 Our aim is building Goal-based intelligent Agent; that is


An agent which has a goal to reach by applying a sequence of actions starting the
initial state
• Driving from point A to point B
• Put 8 queens on a chess board such that no one attacks another
10/20/2024 Problem Solving 8
Building Goal-Based Agents
 To build a goal-based agent we need to answer the
following questions:
 What is the goal to be achieved?
 What are the actions?
 What relevant information is necessary to encode about the
world to describe the state of the world, describe the available
transitions, and solve the problem?

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?

10/20/2024 Problem Solving 10


Example: state space for Coloring problem

10/20/2024 Problem Solving 11


The 8 puzzle problem
 This is the problem of arranging the tiles so that all the
tiles are in the correct positions. You do this by moving
tiles or space up, down, left, or right, so long as the
following conditions are met:
 a) there's no other tile blocking you in the direction of the
movement; and
 b) you're not trying to move outside of the boundaries/ edges.
 Identify possible states & operators?
 Construct state space?

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

10/20/2024 Problem Solving 13


Operations
Defining set of actions or operators that take us from one state to
another state.
Fill 4L jug (x, y) where x<4 (4, y)

Fill 3L jug (x, y) where y<3 (x, 3)

Empty 4L jug on ground (x, y) where x>0 (0, y)

Empty 3L jug on ground (x, y) where y>0 (x, 0)

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)

10/20/2024 Problem Solving 14


Graphical representation

10/20/2024 Problem Solving 15


Exercise: River Crossing Puzzles
Goat, Wolf and Cabbage problem
A farmer returns from the market, where he bought a goat, a
cabbage and a wolf. On the way home he must cross a river.
His boat is small and unable to transport more than one of
his purchases. He cannot leave the goat alone with the
cabbage (because the goat would eat it), nor he can leave the
goat alone with the wolf (because the goat would be eaten).
How can the farmer get everything safely on the other side?
1. Identify the set of possible states and operators
2. Construct the state space of the problem using suitable
representation

10/20/2024 Problem Solving 16


Exercise: River Crossing Puzzles
Missionary-and-cannibal problem
 Three missionaries and three cannibals are on one side
of a river that they wish to cross. There is a boat that
can hold one or two people. Find an action sequence
that brings everyone safely to the opposite bank (i.e.
Cross the river). But you must never leave a group of
missionaries outnumbered by cannibals on the same
bank (in any place).
1. Identify the set of possible states and operators
2. Construct the state space of the problem using suitable
representation

10/20/2024 Problem Solving 17


Knowledge and types of problems
 Problems can be classified into four types based on the
level of knowledge that an agent can have concerning its
action and the state of the world
 Single state problems (Type 1)
 Multiple state problems (Type 2)
 Exploration problems (Type 3)
 Contingency Problems (Type 4)

10/20/2024 Problem Solving 18


 Solving a problem requires knowledge about:
 The states: are they fully accessible or inaccessible
 Effects of its action: deterministic or non-deterministic

 Thus, the 1st step in problem formulation for an agent is to see


 To what extent the state of the world is accessible to the agent to update its
knowledge
 The extent of knowledge it has concerning the effects of its action on the
environment
 This knowledge depends on how it is connected to the environment
via its percepts and actions

10/20/2024 Problem Solving 19


Example: Vacuum cleaner world problem
To simplify the problem (rather than the full version), let;
The world has only two locations
– Each location may or may not contain dirt
– The agent may be in one location or the other
• Eight possible world states
• Three possible actions (Left, Right, Suck)
– Suck operator clean the dirt
– Left and Right operators move the agent from location to
location
• Goal: to clean up all the dirt

10/20/2024 Problem Solving 20


Clean House Task

10/20/2024 Problem Solving 21


Vacuum Cleaner state Space

10/20/2024 Problem Solving 22


Single state problem
Fully observable: The world is accessible to the agent
 It can determine its exact state through its sensors
 The agent’s sensor knows which state it is in
Deterministic: The agent knows exactly the effect of its
actions
 It can then calculate exactly which state it will be in after
any sequence of actions
Action sequence is completely planned
Example - Vacuum cleaner world
 What will happen if the agent is initially at state = 5 and
formulates action sequence - [Right, Suck]?
 Agent calculates and knows that it will get to a goal state
• Right  {6}
10/20/2024
• Suck Problem Solving
{8} 23
Multiple state problems
• Partially observable: The agent has limited access to the world state
–It might not have sensors to get full access to the environment states
or as an extreme, it can have no sensors at all (due to lack of
percepts)
• Deterministic: The agent knows exactly what each of its actions do
–It can then calculate which state it will be in after any sequence of
actions
• If the agent has full knowledge of how its actions change the world,
but does not know of the state of the world, it can still solve the task
Example - Vacuum cleaner world
–Agent’s initial state is one of the 8 states: {1,2,3,4,5,6,7,8}
–Action sequence: {right, suck, left, suck}
–Because agent knows what its actions do, it can discover and reach
to goal state.

Right  [2.4.6.8.] Suck  {4,8}


Left  {3,7} Suck  {7}
10/20/2024 Problem Solving 24
Contingency Problems
Partially observable: The agent has limited access to the world
state
Non-deterministic: The agent is ignorant of the effect of its
actions
–Sometimes ignorance prevents the agent from finding a
guaranteed solution sequence.
Suppose the agent is in Murphy’s law world
–The agent has to sense during the execution phase, since things
might have changed while it was carrying out an action. This
implies that
• the agent has to compute a tree of actions, rather than a linear
sequence of action
–Example - Vacuum cleaner world:
• action ‘Suck’ deposits dirt on the carpet, but only if there is no
dirt already. Depositing dirt rather than sucking returns from
ignorance about the effects of actions
10/20/2024 Problem Solving 25
Contingency Problems (cont…)
Example - Vacuum cleaner world
–Agent’s initial state is one of the 8 states: {1,2,3,4,5,6,7,8}
–Action sequence: {right, suck, left, suck}
{1,2,3,4,5,6,7,8}  {2,4,6,8}  {4,2,8,6}  {3,1,7,5}  {7,5,3,1}(failure)
Is there a way to solve this problem?
– Thus, solving this problem requires local sensing, i.e. sensing the
execution phase,
– Start from one of the states {1,2,3,4,5,6,7,8}, and take improved
action sequence [Right, Suck (only if there is dirt there), Left,
Suck (only if there is dirt there)]
{1,2,3,4,5,6,7,8}  [2,4,6,8]  {4,8}  {3,7}  {7}

Many problems in the real world are contingency problems


(exact prediction is impossible)
– For this reason people keep their eyes open while walking around
or driving.
10/20/2024 Problem Solving 26
Exploration problem
The agent has no knowledge of the environment
 World Partially observable : No knowledge of states
(environment)
• Unknown state space (no map, no sensor)
 Non-deterministic: No knowledge of the effects of its actions.
 Problem faced by (intelligent) agents (like, new-born babies).
 This is a kind of problem in the real world rather than in a model,
which may involve significant danger for an ignorant agent. If the
agent survives, it learns about the environment
 The agent must experiment, learn and build the model of the
environment through its results, gradually, discovering
– What sort of states exist and What its action do
– Then it can use these to solve subsequent (future) problems

Example: In solving Vacuum cleaner world problem the agent learns


the state space and the effects of its action sequences, say: [suck,
Right].
10/20/2024 Problem Solving 27
Well-defined problems and solutions
 To define a problem, we need the following elements: states, operators,
goal test function and cost function.
 Constructing state space
 Construct a graph with initial, transition and goal states that are connected by
using operators for moving starting from initial state to the goal state through
transition states.
 The Initial state: is the state that the agent starts in or begins with. The goal
state is the state the agent wants to achieve.

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

Coloring problem: All rectangles black


 IsGoal(rectangle[], n)
for i=1 to n do
if rectangle[i] = ‘WHITE’ then
return 0
return 1
end isGoal
8-puzzle ??

10/20/2024 Problem Solving 30


Path cost function
A function that assigns a cost to a path (sequence of
actions).
 It is the sum of the costs of the individual actions along the path
(from one state to another state).
 Measure path cost of a sequence of actions in the state space.
For example, we may prefer paths with fewer or less costly
actions.

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?

10/20/2024 Problem Solving 31


Steps in problem solving
Goal formulation
 is a step that specifies exactly what the agent is trying to achieve
 This step narrows down the scope that the agent has to look at
Problem formulation
 is a step that puts down the actions and states that the agent has to
consider given a goal (avoiding any redundant states), like:
 the initial state
 the allowable actions etc…
Search
 is the process of looking for the various sequence of actions that
lead to a goal state, evaluating them and choosing the optimal
sequence.
Execute
 is the final step that the agent executes the chosen sequence of
actions to get it to the solution/goal.
10/20/2024 Problem Solving 32
Problem Solving Agent Algorithm
function SIMPLE-PROBLEM-SOLVING-AGENT(percept)
returns an action
static: seq, an action sequence, initially empty
state, some description of the current world state
goal, a goal initially null
problem, a problem formulation

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

10/20/2024 Problem Solving 35


Searching Strategy

10/20/2024 Problem Solving 36


Searching
• Examine different possible sequences of actions & states,
and come up with specific optimal sequence of
operators/actions that will take you from the initial state to
the goal state
 Given a state space with initial state and goal state, find
optimal sequence of actions leading through a sequence of
states to the final goal state from initial state

• Searching in State Space


 select min-cost/max-profit node/state in the state space,
 test whether the state selected is the goal state or not,
 if not, expand the node further to identify its successor.

10/20/2024 Problem Solving 38


Search Tree
The searching process is like building the search tree that is super
imposed over the state space
 A search tree is a representation in which nodes denote paths and branches
connect paths. The node with no parent is the root node. The nodes with no
children are called leaf nodes.
Example: Route finding Problem
• Partial search tree for route finding from Sidist Kilo to Stadium.
goal test
(a) The initial state SidistKilo

SidistKilo
(b) After expanding Sidist Kilo generating a new state
AratKilo Giorgis ShiroMeda
choosing one SidistKilo
option

(c) After expanding Arat Kilo AratKilo Giorgis ShiroMeda

MeskelSquare Piassa Megenagna


10/20/2024 Problem Solving 39
Search algorithm
Two functions needed for conducting search
 Generator (or successors) function: Given a
state and action, produces its successor states
(in a state space)
 Tester (or IsGoal) function: Tells whether
given state S is a goal state IsGoal(S) 
True/False
 IsGoal and Successors functions depend on
problem domain.

10/20/2024 Problem Solving 40


 Two lists maintained during searching
 OPEN list: stores the nodes we have seen but not explored
 CLOSED list: the nodes we have seen and explored
 Generally, search proceeds by examining each node on the
OPEN list, performing some expansion operation that
adds its children to the OPEN list, & moving the node to
the CLOSED list.
 Merge function: Given successor nodes, it either
append, prepend or arrange based on evaluation cost
 Path cost: function assigning a numeric cost to each
path; either from initial node to current node and/or
from current node to goal node

10/20/2024 Problem Solving 41


Search algorithm
• Input: a given problem (Initial State + Goal state + transit states and
operators)
• Output: returns optimal sequence of actions to reach the goal.
function GeneralSearch (problem, strategy)
open = (initialState); //put initial state in the List
closed = {}; //maintain list of nodes examined earlier
while (not (empty (open)))
f = remove_first(open);
if IsGoal (f) then return (f);
closed = append (closed, f);
succ = Successors (f)
left = not-in-closed (succ, closed );
open = merge (rest(open), left); //append or prepend l to open list
end while
return ('fail')
end GeneralSearch
10/20/2024 Problem Solving 42
Algorithm Evaluation:
 Is the search strategies find solutions to problems with no
solutions
 Is it produces incorrect solutions to problems
 Completeness
 Is the algorithm guarantees in finding a solution whenever one
exists
 Think about the density of solutions in space and evaluate
whether the searching technique guaranteed to find all solutions
or not.
 Optimality
 is the algorithm finding an optimal solution; i.e. the one with
minimum cost
• how good is our solution?

10/20/2024 Problem Solving 43


Algorithm Evaluation: Time & Space Trade-offs
With many computing projects, we worry about:
 Speed versus memory
 Time complexity: how long does it take to find a
solution
 Space complexity: how much space is used by the
algorithm
Fast programs can be written
 But they use up too much memory
Memory efficient programs can be written
 But they are slow
We consider various search strategies
 In terms of their memory/speed tradeoffs
10/20/2024 Problem Solving 44
Searching Strategies
Search strategy gives the order in which the
search space is examined
Uninformed (= blind) search
 they do not need domain knowledge that guide them
towards the goal
 Have no information about the number of steps or the
path cost from the current state to the goal
 It is important for problems for which there is no
additional information to consider
Informed (= heuristic) search
 have problem-specific knowledge (knowledge that is
true from experience)
 Have knowledge about how far are the various state
from the goal
 Can find solutions more efficiently than uninformed
search
10/20/2024 Problem Solving 45
Search Methods:
Uninformed search
 Breadth first
 Depth first
 Uniform cost, …
 Depth limited search
 Iterative deepening
 etc.

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

10/20/2024 Problem Solving 47


Breadth first search
Expand shallowest unexpanded node,
 i.e. expand all nodes on a given
level of the search tree before
moving to the next level
Implementation: use queue data
structure to store the list:
 Expansion: put successors at the
end of queue
 Pop nodes from the front of the
queue

10/20/2024 Problem Solving 48


Breadth first search
•Properties:
 Takes space: keeps every node in memory
 Optimal and complete: guarantees to find solution

Time complexity = 1+2+4+…= 20+21+22+...


= b0+b1+b2+…. + bd = O(bd)

10/20/2024 Problem Solving 49


Example: Breadth first search

Open list Closed list


{S} {}
{CS,BS,AS} {S}
{GSA,CS,BS} {S,A}
{GSB,GSA,CS} {S,A,B}
{GSC,GSB,GSA} {S,A,B,C}

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

10/20/2024 Problem Solving 52


Uniform cost Search
Properties:
 This strategy finds the cheapest solution provided
the cost of a path must never decrease as we go
along the path
g(successor(n)) ≥ g(n), for every node n
 Takes space since it keeps every node in memory
Time complexity = 1+2+4+…= 20+21+22+...
= b0+b1+b2+…. + bd = O(bd)

10/20/2024 Problem Solving 53


Example: Uniform cost Search

Open list Closed list


{S(0)} {}
{CS(15),BS(5),AS(1)} {S}
{CS(15), GSA(11),BS(5)} {S,A}
{CS(15),GSA(11), GSB(10)} {S,A,B}

Optimal path: S  B  G with minimum cost of 10


10/20/2024 Problem Solving 54
Depth-first search
 Expand one of the node at the deepest
level of the tree.
 Only when the search hits a non-goal dead
end does the search go back and expand
nodes at shallower levels
Implementation: treat the list as stack
 Expansion: push successors at the top of
stack
 Pop nodes from the top of the stack

10/20/2024 Problem Solving 56


Depth-first search
Properties
 Incomplete and not optimal: fails in infinite-depth spaces,
spaces with loops.
 Modify to avoid repeated states along the path
 Takes less space (Linear): Only needs to remember up to the
depth expanded
Time complexity = 1+2+4+…= 20+21+22+...
= b0+b1+b2+…. + bd = O(bd)
Space complexity= 1+2+2+…= 20+21+21+...
= b0+b1+b1+…. + b1 = b0+(d-1)b1
=
1+bd-b = O(bd)

10/20/2024 Problem Solving 57


Example: Depth-first search

Open list Closed list


{S} {}
{CS,BS,AS} {S}
{CS,BS,GSA} {S,A}

Optimal path: S  A  G

10/20/2024 Problem Solving 58


Informed Search
• Informed Search
• Best First Search
• Local Search

10/20/2024 Problem Solving 62


Informed Search
• Search efficiency would improve greatly if there is
a way to order the choices so that the most
promising are explored first.
– This requires domain knowledge of the problem (i.e.
heuristic) to undertake focused search
• Define a heuristic function, h(n) that estimates the.
goodness of a node n, based on domain specific
information that is computable from the current
state description
– h(n) is an estimate of how close we are to a goal state
– It is an estimated cost of path from state n to a goal
state.
10/20/2024 Problem Solving 63
Example problems
• Route-finding problem:
– actual distance, g(x) vs. straight line distance, h(x)

10/20/2024 Problem Solving 64


Best First Search
• It is a generic name to the class of informed methods
• The two best first approaches to find the shortest path:
– Greedy search: minimizes estimated cost to reach a goal
– A*-search: minimizes the total path cost
• When expanding a node n in the search tree, greedy
search uses the estimated cost to get from the current state
to the goal state, defined as h(n).
– In route finding problem h(n) is the straight-line distance
• We also possess the sum of the cost to reach that node
from the start state, defined as g(n).
– In route finding problem; this is the sum of the step costs for the
search path.
• For each node in the search tree, A*-search uses an
evaluation function, f(n):
f(n) = g(n) + h(n)

10/20/2024 Problem Solving 66


Admissibility
 Search algorithms (such as greedy and A*- search) are
admissible when the heuristic function never over
estimated the actual cost so that
 the algorithm always terminates in an optimal path from the
initial state to goal node if one exists.
 Check Admissibility of estimated cost h(n): make sure
that h(n) is not overestimated as compared to g(n)
 g (n): Actual cost of shortest path from n to z (not known)
 h(n) is said to be an admissible heuristic function if for all n, h(n)
≤ g(n)
 The closer estimated cost to actual cost the fewer extra nodes that
will be expanded
 Using an admissible heuristics guarantees that the solution found
by searching algorithm is optimal
10/20/2024 Problem Solving 67
Greedy search
 A best first search that uses a heuristic function h(n) alone
to guide the search
 Selects node to expand that is closest (hence it’s greedy) to a goal
node
 The algorithm doesn’t take minimum path costs from initial to
current nodes into account, it just go ahead optimistically and
never looks back.

 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

10/20/2024 Problem Solving 68


Greedy search: Properties
 Incomplete and Not optimal
 may not reach goal state because it may start down an infinite
path and never return to try other possibilities

 Exponential time complexity and memory requirement


 It retains all the nodes in memory

 With good heuristic function (that estimate cost nearer to actual


cost) the space and time complexity can be reduced substantially
 The problem with greedy search is that it didn’t take costs so
far into account.
 We need a search algorithm (like A* search) that takes into
consideration this cost so that it avoids expanding paths that are
already expensive

10/20/2024 Problem Solving 69


Example S
8
Initial state
 Using Greedy search identify the 1 5 8
optimal path from the initial state, S
to the Goal state, G.
 Which algorithm suggests the
C B A
8 4 3
optimal path? 3 9
4
7 5
D E G
  0 Goal state

Open list Closed list


{S(8)} {}
{CS(8),BS(4),AS(3)} {S}
{CS(8),BS(4),GSA(0)} {S,A}

10/20/2024 path: S  A  G with


Optimal minimum
Problem Solving cost of 13 70
A*-search Algorithm
 It considers both estimated cost of getting from n to the goal
node h(n), and cost of getting from initial node to node n, g(n)
 Apply three functions over every nodes
 g(n): Cost of path found so far from initial state to n
 h(n): Estimated cost of shortest path from n to z
 f(n): Estimated total cost of shortest path from a to z via n
 Evaluation function f(n) = h(n) + g(n)
 Implementation: Expand the node for which the evaluation
function f(n) is lowest
 Rank nodes by f(n) that goes from the start node to goal node via
given node
 Example: Route finding problem
10/20/2024 Problem Solving 71
Properties of A* search
 A* search is complete, optimal, and optimally efficient
for any given admissible heuristic function
 Complete
 It guarantees to reach to goal state
 Optimality
 If the given heuristic function is admissible, then A* search is
optimal.
 Takes memory:
 It keeps all generated nodes in memory
 Both time and space complexity is exponential
 Time and space complexity of heuristic algorithms depends on
the quality of the heuristic function.
 Worst case is exponential

10/20/2024 Problem Solving 72


Example
S Initial state
• Using A* search identify the 8
optimal path from the initial state, S
1 5 8
to the Goal state, G.
• Is Greedy or A* algorithm suggests C B A
the optimal path? 8 4 3
3 9
4
7 5
D E G Goal state
  0
Open list Closed list
{S(8)} {}
{AS(11),CS(9),BS(9)} {S}
{AS(11), GSB(9), CS(9)} {S,B}
{AS(11), GSC(10),GSB(9)} {S,B,C}
10/20/2024 Problem Solving 73
Optimal path: S  B  G with minimum cost of 9
Beam search
• Beam search is a heuristic search algorithm that explores a state
space by expanding the most promising node in a limited set.
• Beam search is an optimization of best-first search that reduces
its memory requirements.
– Best-first search is a graph search which orders all partial
solutions (states) according to some heuristic which attempts to
predict how close a partial solution is to a complete solution (goal
state).
– In beam search, only a predetermined number of best partial
solutions are kept as candidates.
• Beam search generates all successors of the states at the current
level, sorting them in increasing order of heuristic cost.
– However, it only stores a predetermined number of states at each
level (called the beam width).

10/20/2024 Problem Solving 75


Beam search
• Keep track of k states rather than just one
• Algorithm
Start with k randomly generated best states
repeat
generate all the successors of all k states
if any one is a goal state, return;
else select the k best successors
• Information is shared between k search points
– Each k state generates successors
– Best k successors are selected
– Some search points may contribute none to best
successors
– One search point may contribute all k successors
10/20/2024 Problem Solving 76
Example S Initial state
8
• Using Beam search identify the 1 5 8
optimal path from the initial state,
S to the Goal state, G. Assume
k=2 (the number of nodes
C B A
8 4 3
considered at a time)
3 9
4
7 5
D E G
  0 Goal state
Open list Closed list
{AS(11),S(8)} {}
{GSA(11),CS(9),BS(9)} {S}
{AS(11),GSC(10),GSB(9)} {S,B,C}

10/20/2024 Problem Solving 77


Optimal path: S  B  G with minimum cost of 9
Assignment: Search space
• Given the search space below, with initial node S and
goal node G, find the optimal path using:
 Uninformed search, Uniform Cost Search
 Informed search, A* search
 What do you observe in the process of applying the two
searching approaches C
2 4
S
• Note that: numbers 4 3 4
with in the nodes are 3 D
3 3
h(n) & edges are B E
labeled with g(n) 6 2

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.

10/20/2024 Problem Solving 83


B
(100)

B
(125) B
C
A (125) (75)
C
C
(50)
D (125)
C
(100) (100)
D
(50)
E
(75)

10/20/2024 Problem Solving 84


Problems with Hill Climbing
• May lead to local maxima – once it reaches to local maxima, it
can’t go to global max. It caught by small foothills.
• Walking in the Plateau - situations where all of the local steps
are indistinguishable; all of them have the same value.
• ridges traversal - there is a direction we would like to move, but
none of the allowed steps actually takes us in that direction.

10/20/2024 Problem Solving 85


Possible Improvements
• Random-restart (Simulated Annealing)
–Execute hill climbing several times, choose best result.
–If p is probability of a search succeeding, then expected
number of restarts is 1/p.
• Stochastic hill climbing
–Choose at random from uphill moves
–Probability of move could be influenced by steepness
• First-choice hill climbing
–Generate successors at random until one is better than
current.
• Other Solutions: backtracking, making big jumps (to handle
plateaus or poor local maxima).
10/20/2024 Problem Solving 86
THANK YOU

10/20/2024 Problem Solving 88

You might also like