Lecture 04 & 05 Problem Solving
Lecture 04 & 05 Problem Solving
Searching
Lecture # 04
Tuesday, February 4, 2020
Spring 2020
FAST – NUCES, Faisalabad Campus
Zain Iqbal
[email protected]
In which we will see how an
agent can find a sequence of
actions that achieves its goal
when no single action will do
04-Feb-20 2
State Representation
04-Feb-20 5
Problem-Solving Agent
sensors
?
environment
agent
actuators
04-Feb-20 6
Problem-Solving Agent
sensors
?
environment
agent
actuators
• Formulate Goal
• Formulate Problem
•States
•Actions
04-Feb-20 • Find Solution 7
Solving Problems by Searching
Reflex agent is simple
⚫ base their actions on
⚫ a direct mapping from states to actions
04-Feb-20 8
Problem-solving agent
Problem-solving agent
⚫ A kind of goal-based agent
⚫ It solves problem by
⚫ finding sequences of actions that lead to
desirable states (goals)
⚫ To solve a problem,
⚫ the first step is the goal formulation, based on
the current situation and Agent performance
04-Feb-20 9
Goal formulation
The goal is formulated
⚫ as a set of world states, in which the goal is
satisfied
Reaching from initial state → goal state
⚫ Actions are required
Actions are the operators
⚫ causing transitions between world states
⚫ Actions should be abstract enough at a
certain degree, instead of very detailed
⚫ E.g., turn left VS turn left 30 degree, etc.
04-Feb-20 10
Problem formulation
The process of deciding
⚫ what actions and states to consider ,given
a goal
E.g., driving Romania → Bucharest
⚫ in-between states and actions defined
States: Some places in Romania &
Bucharest
⚫ Actions: Turn left, Turn right, go straight,
accelerate & brake, etc.
04-Feb-20 11
An agent with several
immediate options of unknown
value can decide what to do
by first examining future actions
that eventually lead to states
of known value.
04-Feb-20 12
8
Environment Assumptions
• Static, formulating and solving the problem is
done without paying attention to any changes that
might be occurring in the environment
04-Feb-20 17
Well-defined problems and solutions
A problem is defined by 5 components:
⚫ The initial state
⚫ that the agent starts in
⚫ The set of possible actions
⚫ Transition model: description of what each action
does.
(successor functions): refer to any state reachable from
given state by a single action
⚫ Initial state, actions and Transition model define the
state space
⚫ the set of all states reachable from the initial state by any
sequence of actions.
⚫ A path in the state space:
⚫ any sequence of states connected by a sequence of actions.
04-Feb-20 18
Well-defined problems and solutions
The goal test
⚫ Applied to the current state to test
⚫ if the agent is in its goal
-Sometimes there is an explicit set of possible goal states.
(example: {In(bucharest)}.
-Sometimes the goal is described by an abstract property
⚫ instead of stating explicitly the set of states
⚫ Example: Chess
⚫ the agent wins if it can capture the KING of the opponent on
next move ( checkmate).
⚫ no matter what the opponent does
04-Feb-20 19
Well-defined problems and solutions
A path cost function,
⚫ assigns a numeric cost to each path
⚫ = performance measure
⚫ denoted by g
⚫ to distinguish the best path from others
04-Feb-20 20
Well-defined problems and solutions
Together a problem is defined by
⚫ Initial state
⚫ Actions
⚫ Successor function
⚫ Goal test
⚫ Path cost function
The solution of a problem is then
⚫ a path from the initial state to a state satisfying the goal
test
Optimal solution
⚫ the solution with lowest path cost among all solutions
04-Feb-20 21
Formulating problems
Besides the four components for problem
formulation
⚫ anything else?
Abstraction
⚫ the process to take out the irrelevant information
⚫ leave the most essential parts to the description of the
states
( Remove detail from representation)
⚫ Conclusion: Only the most important parts that are
contributing to searching are used
04-Feb-20 22
12
•
Problem Formulation
A solution to a problem is a path from the initial state to the
goal state
• Solution quality is measured by the path cost function and an
optimal solution has the lowest path cost among all solutions
• Real world is absurdly complex
– state space must be abstracted for problem solving
• (Abstract) state = set of real states
• (Abstract) action = complex combination of real actions
– e.g., "Arad → Zerind" represents a complex set of possible
routes, detours, rest stops, etc.
• For guaranteed realizability, any real state "in Arad“ must get
to some real state "in Zerind"
• (Abstract) solution =
– set of real paths that are solutions in the real world
• Each abstract action should be "easier" than the original
problem
04-Feb-20 23
Example: Route finding
04-Feb-20 24
Holiday Planning
On holiday in Romania; Currently in Arad.
Flight leaves tomorrow from Bucharest.
Formulate Goal:
Be in Bucharest
Formulate Problem:
States: various cities
Actions: drive between cities
Find solution:
Sequence of cities: Arad, Sibiu, Fagaras, Bucharest
04-Feb-20 25
Problem Solving
States
Actions
Start Solution
Goal
04-Feb-20 26
Example problems
Toy problems
⚫ those intended to illustrate or exercise
various problem-solving methods
⚫ E.g., puzzle, chess, etc.
Real-world problems
⚫ tend to be more difficult and whose
solutions people actually care about
⚫ E.g., Design, planning, etc.
04-Feb-20 27
Vacuum World
04-Feb-20 28
Example: vacuum world
States??
Initial state??
Actions??
Goal test??
Path cost??
04-Feb-20 29
Example: vacuum world
States??
Initial state??
Actions??
Goal test??
Path cost??
04-Feb-20 31
Example: 8-puzzle
8 2 1 2 3
3 4 7 4 5 6
5 1 6 7 8
04-Feb-20 33
Example: 8-puzzle
8 2 7
3 4
8 2 5 1 6
3 4 7
5 1 6 8 2 8 2
3 4 7 3 4 7
5 1 6 5 1 6
04-Feb-20 34
Search space for Eight Puzzle
Slid
e7
Example: 8-puzzle
10 million states/sec
04-Feb-20 36
Example: 8-queens
Place 8 queens in a chessboard so that no two queens
are in the same row, column, or diagonal.
04-Feb-20 37
Example: 8-queens problem
04-Feb-20 41
16
04-Feb-20 42
17
04-Feb-20 43
Example: robot assembly
States??
Initial state??
Actions??
Goal test??
Path cost??
04-Feb-20 44
Example: robot assembly
04-Feb-20 46
Simple Tree Search Algorithm
function TREE-SEARCH(problem, strategy) return solution
or failure
Initialize search tree to the initial state of the problem
do
if no candidates for expansion then return failure
choose leaf node for expansion according to strategy
if node contains goal state then return solution
else expand the node and add resulting nodes to the
search tree
enddo
04-Feb-20 47
Search of State Space
04-Feb-20 48
Search of State Space
04-Feb-20 49
Search State Space
04-Feb-20 50
Search of State Space
04-Feb-20 51
Search of State Space
04-Feb-20 52
Search of State Space
→ search tree
04-Feb-20 53
3.3 Searching for solutions
Finding out a solution is done by
⚫ searching through the state space
All problems are transformed
⚫ as a search tree
⚫ generated by the initial state and
successor function
04-Feb-20 54
Search tree
Initial state
⚫ The root of the search tree is a search node
Expanding
⚫ applying successor function to the current state
⚫ thereby generating a new set of states
leaf nodes
⚫ the states having no successors
Frontier/Fringe/open-list : Set of search nodes
that have not been expanded yet.
Refer to next figure
04-Feb-20 55
Tree search example
04-Feb-20 56
Tree search example
04-Feb-20 57
Search tree
Parent Node:
⚫ In(Arad)
Child Node:
⚫ In(sibiu), In(Timisoara) , In (Zerind).
Leaf Node:
⚫ In(Arad),In(Fagaras),In(Oradea),
In(Riminicu Vilcea)
04-Feb-20 58
Tree search algorithm
function TREE-SEARCH(problem) returns a solution, or failure
initialize the frontier using the initial state of problem
loop do
if the frontier is empty then return failure
choose a leaf node and remove it from the frontier
if the node contains a goal state then return the
corresponding solution
expand the chosen node, adding the resulting nodes to
the frontier
04-Feb-20 59
Tree search algorithm (2)
04-Feb-20 60
Search tree
The essence of searching
⚫ in case the first choice is not correct
⚫ choosing one option and keep others for later
inspection
Hence we have the search strategy
⚫ which determines the choice of which state to
expand
⚫ good choice → fewer work → faster
Important:
⚫ state space ≠ search tree
04-Feb-20 61
Search tree
State space
⚫ has unique states {A, B}
⚫ while a search tree may have cyclic paths:
A-B-A-B-A-B- …
A good search strategy should avoid
such paths
04-Feb-20 62
Reading Material
Russell & Norvig Chapter # 3
04-Feb-20 134