Sie sind auf Seite 1von 53

(TES3111 / TIC3151)

Artificial Intelligence
Lecture 3
Solving Problems by Searching
(Part I)
1

Contents
3.1 Problem-Solving Agents (goal-based agent)
3.2 Example Problems
3.3 Searching for Solutions
3.4 Search Tree Terminology
3.5 Measuring Problem-Solving Performance

Introduction
If a person/program doesn't know better, one way to solve a
problem is exhaustive search

Try all possible sequences of steps until solution is found

Just like finding your


way through a maze

Introduction
Intelligent agents are supposed to maximize their performance measure

This can be simplified if the agent can adopt a goal and aim at
satisfying it
Goal formulation (based on the current situation and the agents
performance measure)
is the first step in problem solving
Problem formulation is the process of deciding what actions and
states to consider, given a goal (desirable state)

IOI Mall, Puchong

41 mins

36 mins
43 mins

Goal
MMU, Cyberjaya
5

Introduction
Search is the process of looking for a sequence of actions that
reaches the goal state
Once a solution is found, the recommended actions can be carried
out
This phase is called the execution phase

Problem-Solving Agents
Problem-solving agent (goal-based agent)
Decides what to do by finding a sequence of actions that lead
to desirable states (goal)
Steps in problem-solving:

1. Goal Formulation desirable state


2. Problem formulation what actions and states to consider
given a goal.

3. Search for solution given the problem, search for a


solution (a sequence of actions) to achieve the goal.
4. Execution of the solution

Problem-Solving Agents
A problem can be defined into FOUR components:
1. an initial state
2. a set of actions
3. a goal test
4. a path cost function

Initial state + actions + goal test + path cost function

Problem-Solving Agents
1.

Initial state
the state that the agent starts, e.g., In(Puchong)

2.

Actions
a description of possible actions available to the agent
uses a successor function
SUCCESSOR-FN(X) returns a set of

action, successor
one of the legal
actions in state x

a state that can be reached


from x by applying the action
9

Problem-Solving Agents
The initial state and successor function define the state space of the
problem
State space
the set of all possible states reachable from the initial state
forms a graph
nodes states

arcs between nodes actions

10

Problem-Solving Agents
State space graph
KL
PJ
Initial state
Puchong

Goal
Cyberjaya

Subang
Shah Alam

From the state In(Puchong), the successor function would return:

Go(Subang ), In(Subang ) , Go(PJ ), In(PJ ) , Go(Cyberjaya ), In(Cyberjaya )


11

Problem-Solving Agents
3.

Goal test
determines whether a given state is a goal state,
e.g., In(Cyberjaya)

4.

Path cost function

assigns a numeric cost to each path


E.g., sum of distances, number of actions executed, etc.
The cost of a path can be described as the sum of the costs of
the individual actions along the path
The step cost of taking action a to go from state x to state y is
denoted by:
c(x, a, y)
12

Problem-Solving Agents

A solution to a problem is a path from the initial state to a goal


state

Solution quality is measured by the path cost function

An optimal solution has the lowest path cost among all solutions

13

Problem-Solving Agents
Example: Romanian Holiday (Romania driving problem)

Goal
Start

On holiday in Romania;
currently in Arad.
14

Problem-Solving Agents
Formulate goal:
in Bucharest
Formulate problem:
states: various cities

actions: drive between cities


Find solution:

sequence of cities
e.g., Arad, Sibiu, Fagaras, Bucharest

15

Problem-Solving Agents
[Recap]: A problem is defined by FOUR components:

Initial state + actions + goal test + path cost function

e.g., at Arad

e.g., x = at Bucharest

Go(Sibiu ), In(Sibiu ) , Go(Timisoara), In(Timisoara) , Go(Zerind ), In(Zerind


Sum of distances

16

Example Problems
The toy problems to illustrate example problems:
The Vacuum World
8-queens problem

Real-world-problems tend not to have a single agreed-upon description


17

Example Problems
The Vacuum world example:
Assume, the agent knows the locations and the dirt places

Floor has two locations


Vacuum can move from one
location to the other
Locations are clean/dirty
Vacuum can clean the location in
which it is

Number of possible states = 2 22

Clean the floor


18

Eg: Vacuum World

1. Initial state: Any state can be designated as initial state


2. Successor function/actions: This generates the legal states
that result from trying the three actions (move Left, move
Right, and Suck)
3. Goal test: This checks whether all squares are clean (no dirt
left in any square)
4. Path cost: Each step costs 1, so the path cost is the number
of steps in the path.

19

Example Problems
The state space for the vacuum world example

Links denote actions:


L = Left, R = Right, S = Suck
20

Example Problems
The state space for the vacuum world example
states? discrete: dirt and robot location
initial state? any
actions? Left, Right, Suck
goal test? no dirt at all locations
path cost? 1 per action

21

Example Problems
1

Actions: Left, Right, Suck

Goal state:: 7, 8

Initial state:: 5
Solution:: [Right, Suck]
7

22

Example Problems
The 8-queens Problem
The goal of the 8-queens problem is to place eight queens on a
chessboard so that no two queens are in the same row, column, or
diagonal.

A solution

23

Not a solution

Example Problems
The 8-queens Problem
A queen threatens (attacks) another queen in the same row,
column or in diagonal
In this example, the two queens
on the corners are the only queens
threatening each other.
Goal test: 8 queens on board, none attacked.

Possible sequences: 64 x 6357


1.8 1014
24

Example Problems
The 8-queens Problem
There are two main kinds of formulation.

An incremental formulation involves operators that augment


the state description, starting with an empty state; for the 8queens problem, this means that each action adds a queen to
the state.

A complete-state formulation starts with all 8 queens on the


board and moves them around.

In either case, the path cost is of no interest because only the final
state counts.
25

Example Problems
The 8-queens Problem
states? - any arrangement of n 8 queens
- or arrangements of n 8 queens in leftmost n columns,
1 per column, such that no queen attacks any other.
initial state? no queens on the board
actions? - add queen to any empty square
- or add queen to leftmost empty square such that it is
not attacked by other queens.
goal test? 8 queens on the board, none attacked.

26

Example Problems
Other Real-World Problems
Route-finding problem
Routing in computer networks,
Military operations planning,
Airline travel planning systems
Touring problems
Visit every city exactly once, starting and ending in the same
city
State: must include not just the current location but also the
set of cities the agent has visited
27

Searching for Solutions


Search Trees
are data structure that represent how the search algorithm explores
the state space, i.e., they dynamically evolve as the search
proceeds
Tree nodes correspond to states

28

Searching for Solutions


Given a state space,
start state S,
goal states G

the search algorithm must determine how to get from S to an


element of G

29

Searching for Solutions


A solution is an action sequence, so search algorithms work by
considering various possible action sequences
Search through the state space
We will consider search techniques that use an explicit search tree
that is generated by the initial state + successor function

initialize (initial node)


loop
choose a node for expansion according to strategy;
goal node? done
expand node with successor function
30

Searching for Solutions


A Romanian Holiday Example
The route-finding problem from Arad to Bucharest
State space: Cities in Romania
Initial state: In(Arad).
Goal : in Bucharest In(Bucharest)
Actions: Drive between cities
Solution: Sequence of cities
Pathcost: number of cities, distance, time, fuel

31

Searching for Solutions: Romanian Holiday


The state space

32

Searching for Solutions: Romanian Holiday


The root of the
search tree is
the search node
corresponding
to the initial
state

The state space

The set of all leaf nodes available for expansion at any


given point is called frontier.
The initial state
33

Searching for Solutions: Romanian Holiday


The state space
If the
current
node is NOT
the goal
node,
expand the
current
state to
generate a
new set of
states

After expanding Arad


34

Searching for Solutions: Romanian Holiday


The choice of which state to
expand is determined by the
search strategy

After expanding Sibiu


35

Searching for Solutions: Romanian Holiday


Partial search tree for route finding from Arad to Bucharest

36

Searching for Solutions: Romanian Holiday


The solution

37

Searching for Solutions: Graph Problem


Grow a tree from the start by expanding using the successor
function

38

Searching for Solutions: Graph Problem

39

Searching for Solutions: Graph Problem

40

Search Tree Terminology


Search tree is generated as the search space is traversed

the search space itself is not necessarily a tree, frequently it is


a graph

the tree specifies possible paths through the search space

Expansion of nodes

As states are explored, the corresponding nodes are expanded


by applying the successor function

this generates a new set of (child) nodes

41

Search Tree Terminology


A node is data structure consisting of FIVE components:
1. The state in the state space to which node corresponds;
the node's state

2. The parent node


the node in the search tree that generated this node

3. The action
the action that was applied to the parent node that generated this
node

4. The path cost


The cost, denoted as g(n), of the path from the initial state to the
node

5. The depth
the number of steps in the path from the initial state
42

Search Tree Terminology


State space vs. search tree
A state is a (representation of) physical configuration
A node is a data structure used to represent the search tree
node= <state, parent-node, action, path-cost, depth>

g(n): the path cost


from initial state to
the node (n).

points from child to parent

43

Search Tree Terminology


Search tree is different from state space
Search tree could be arbitrarily big
State space could be finite

Example: the Romanian driving


problem
the state space has 20
distinct states (1 per city)
whereas, the search tree is
infinite

44

Search Tree Terminology


Search tree is different from state space
Search tree could be arbitrarily big
State space could be finite

A search tree with initial


state at the root has:
branches (are actions)
nodes (corresponds to
states in the state space of
the problem)

45

Search Tree Terminology


This is a variable whose value
will be a function

MAKE-QUEUE(Elements)
creates a queue with the
given elements

Returns solution

Takes element from queue


and returns it
QUEUING-FN(Elements,Queue) inserts a set of elements into the queue.
Different varieties of the queuing function produce different varieties of the
search algorithm.
46

Search Tree Terminology


Search Algorithm - Implementation
The collection of nodes is implemented as a queue. The operations
on a queue are follows:
MAKE-QUEUE(element, ) creates a queue with the given
element(s).

EMPTY?(queue) returns true only if there are no more elements


in the queue.
FIRST(queue) removes the first element of the queue and
returns it (REMOVE-FIRST(queue))
INSERT(element, queue) inserts an element into the queue and
returns the resulting queue
Queues are characterized by the order in which they store the
inserted nodes
47

Search Tree Terminology


Search Algorithm - Implementation
Three common queues are:
1. The first-in, first-out or FIFO queue, which pops the oldest
element of the queue
2. The last-in, first out or LIFO queue (also known as stack,
which pops the newest element of the queue

3. The priority queue, which pops the element of the queue


with highest priority according to some ordering function

48

Measuring Problem-Solving Performance


Evaluating Search Performance (Complexity Analysis)
Completeness:
Is the algorithm guaranteed to find a solution when there is one?

Time Complexity:
How long does it take to find a solution? (no. of nodes generated)

Space Complexity
How much memory is needed to perform the search? (max. no. of

nodes generated)
Optimality
Does the strategy find the optimal (least-cost) solution?
49

Measuring Problem-Solving Performance


Time and space complexity are measured in terms of:
b: maximum branching factor of the search tree
d: depth of the shallowest goal node
m: maximum length of any path in the state space (may be )

50

Measuring Problem-Solving Performance


Branching factor, b
b is the maximum number of
children of any node

d = Depth
S

b=2

Depth, d (number of steps along


path from initial state)

The shallowest number of


branches from root to a goal
node
Arc weight, cost of a path g(n)
Maximum length of any path, m

51

Measuring Problem-Solving Performance


Why Search can be difficult?
At the start of the search, the search algorithm does not know
the size of the tree
the shape of the tree

the depth of the goal states

How big can a search tree be?


say there is a constant branching factor b
and one goal exists at depth d
search tree, which includes a goal can have
bd different branches in the tree (worst case)
52

The End

53

Das könnte Ihnen auch gefallen