Download as pdf or txt
Download as pdf or txt
You are on page 1of 63

Solving Problems By

Searching
Lecture # 04
Tuesday, February 4, 2020
Spring 2020
FAST – NUCES, Faisalabad Campus

Zain Iqbal
zain.iqbal@nu.edu.pk
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

Three ways to represent states and the


transitions between them.
Atomic representation:
A state (such as Bor C) is a black box with
no internal structure;
⚫ ◦The algorithms underlying search and game-playing,
Hidden Markov models, and Markov decision
processes etc.
04-Feb-20 3
State Representation
Factored representation:
⚫ A state consists of a vector of attribute values;
values can be Boolean, real valued, or one of a
fixed set of symbols.
⚫ ◦Constraintsatisfaction algorithms, PropositionalLogic,
planning,Bayesian networks
Structured representation:
⚫ A state includes objects, each of which may
have attributes of its own as well as
relationships to other objects
⚫ ◦First-order logic, Knowledge-based learning
04-Feb-20 4
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

⚫ but cannot work well in environments


⚫ which this mapping would be too large to store
⚫ and would take too long to learn

Hence, goal-based agent is used

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

• Initial state is known and the environment is


observable

• Discrete, enumerate alternative courses of


actions/finite actions to choose from
• Deterministic, solutions to problems are single
sequences of actions, so they cannot handle any
unexpected events, and solutions are executed
without paying attention to the percepts—open-
loop system
04-Feb-20 13
Search
Because there are many ways to achieve
the same goal
⚫ Those ways are together expressed as a tree
⚫ Multiple options of unknown value at a point,
⚫ the agent can examine different possible
sequences of actions, and choose the best
⚫ This process of looking for the best sequence
is called search
⚫ The best sequence is then a list of actions,
called solution
04-Feb-20 14
Search algorithm
Defined as
⚫ taking a problem
⚫ and returns a solution

Once a solution is found


⚫ the agent follows the solution
⚫ and carries out the list of actions –
execution phase
Design of an agent
⚫ “Formulate, search, execute”
04-Feb-20 15
Source: S. Russell & P. Norvig
16
04-Feb-20
Well-defined problems and solutions
A problem is defined by 5 components:
Initial state
Actions
Transition model or
(Successor functions)
Goal Test.
Path Cost.

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

Usually the path cost is


⚫ the sum of the step costs of the individual
actions (in the action list)

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?? two locations with or without dirt: 2 x 22=8


states.
Initial state?? Any state can be initial
Actions?? {Left, Right, Suck}
Goal test?? Check whether squares are clean.
Path cost?? Number of actions to reach goal.
04-Feb-20 30
Example: 8-puzzle

States??
Initial state??
Actions??
Goal test??
Path cost??

04-Feb-20 31
Example: 8-puzzle

States?? Integer location of each tile


Initial state?? Any state can be initial
Actions?? {Left, Right, Up, Down}
Goal test?? Check whether goal configuration is
reached
Path cost?? Number of actions to reach goal
04-Feb-20 32
Example: 8-puzzle

8 2 1 2 3

3 4 7 4 5 6
5 1 6 7 8

Initial state Goal state

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

Size of the state space = 9!/2 = 181,440

15-puzzle → .65 x 1012


0.18 sec
6 days
24-puzzle → .5 x 1025
12 billion years

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.

A solution Not a solution

04-Feb-20 37
Example: 8-queens problem

Incremental formulation vs. complete-state formulation


• States??
• Initial state??
• Actions??
• Goal test??
• Path cost??
04-Feb-20 38
Example: 8-queens
Formulation #1:
•States: any arrangement of
0 to 8 queens on the board
• Initial state: 0 queens on the
board
• Actions: add a
queen in any square
• Goal test: 8 queens on the
board, none attacked
• Path cost: none

→ 648 states with 8 queens


04-Feb-20 39
Example: 8-queens
Formulation #2:
•States: any arrangement of
k = 0 to 8 queens in the k
leftmost columns with none
attacked
• Initial state: 0 queens on the
board
• Successor function: add a
queen to any square in the
leftmost empty column such
that it is not attacked
by any other queen
• Goal test: 8 queens on the
→ 2,057 states board
04-Feb-20 40
Real-World Problems

04-Feb-20 41
16

Example: Route finding problem


• states: each is represented by a location (e.g. An airport) and the
current time
• Initial state: specified by the problem
• Successor function: returns the states resulting from taking any
scheduled flight, leaving later than the current time plus the within
airport transit time, from the current airport to another
• goal test: are we at the destination by some pre-specified time
• Path cost:monetary cost, waiting time, flight time, customs and
immigration procedures, seat quality, time of day, type of airplane,
frequent-flyer mileage awards, etc

• Route finding algorithms are used in a variety of applications, such


as routing in computer networks, military operations planning,
airline travel planning systems

04-Feb-20 42
17

Other example problems


• Touring problems: visit every city at least once,
starting and ending at Bucharest
• Travelling salesperson problem (TSP) : each city must
be visited exactly once – find the shortest tour
• VLSI layout design: positioning millions of
components and connections on a chip to minimize
area, minimize circuit delays, minimize stray
capacitances, and maximize manufacturing yield
• Robot navigation
• Internet searching
• Automatic assembly sequencing
• Protein design

04-Feb-20 43
Example: robot assembly

States??
Initial state??
Actions??
Goal test??
Path cost??

04-Feb-20 44
Example: robot assembly

States?? Real-valued coordinates of robot joint


angles; parts of the object to be assembled.
Initial state?? Any arm position and object
configuration.
Actions?? Continuous motion of robot joints
Goal test?? Complete assembly (without robot)
Path cost?? Time to execute
04-Feb-20 45
Basic search algorithms
How do we find the solutions of previous
problems?
⚫ Search the state space (remember complexity of
space depends on state representation)
⚫ Here: search through explicit tree generation
⚫ ROOT= initial state.
⚫ Nodes and leafs generated through successor function.
⚫ In general search generates a graph (same
state through multiple paths)

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

You might also like