1 using search in problem solving part i. 2 search and ai introduction introduction search space,...

29
1 Using Search in Using Search in Problem Solving Problem Solving Part I Part I

Post on 15-Jan-2016

223 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 1 Using Search in Problem Solving Part I. 2 Search and AI Introduction Introduction Search Space, Search Trees Search Space, Search Trees Search in Graphs

1

Using Search in Using Search in Problem SolvingProblem Solving

Part IPart I

Page 2: 1 Using Search in Problem Solving Part I. 2 Search and AI Introduction Introduction Search Space, Search Trees Search Space, Search Trees Search in Graphs

2

Search and AISearch and AI

IntroductionIntroduction Search Space, Search TreesSearch Space, Search Trees Search in GraphsSearch in Graphs Search MethodsSearch Methods Uninformed searchUninformed search

Breadth-firstBreadth-first Depth-firstDepth-first

Page 3: 1 Using Search in Problem Solving Part I. 2 Search and AI Introduction Introduction Search Space, Search Trees Search Space, Search Trees Search in Graphs

3

IntroductionIntroduction you know the available actions that

you could perform to solve your problem

you don't know which ones in particular should be used and in what sequence, in order to obtain a solution

you can search through all the possibilities to find one particular sequence of actions that will give a solution.

Page 4: 1 Using Search in Problem Solving Part I. 2 Search and AI Introduction Introduction Search Space, Search Trees Search Space, Search Trees Search in Graphs

4

The ScenarioThe ScenarioInitial stateTarget (Goal) state

A set of intermediate statesA set of operations, that move us from one state to another.

The task: Find a sequence of operations that will move us from the initial state to the target state.

Solved in terms of searching a graphThe set of all states: search space

Page 5: 1 Using Search in Problem Solving Part I. 2 Search and AI Introduction Introduction Search Space, Search Trees Search Space, Search Trees Search in Graphs

5

Initial state Target

state

Page 6: 1 Using Search in Problem Solving Part I. 2 Search and AI Introduction Introduction Search Space, Search Trees Search Space, Search Trees Search in Graphs

6

Search SpaceSearch Space

Search problems can be Search problems can be represented as represented as graphsgraphs, where , where the nodes are states and the the nodes are states and the arcs correspond to arcs correspond to operations.operations.

The set of all states: The set of all states: search search spacespace

Page 7: 1 Using Search in Problem Solving Part I. 2 Search and AI Introduction Introduction Search Space, Search Trees Search Space, Search Trees Search in Graphs

7

Graphs and Trees 1Graphs and Trees 1

Graph: a set of nodes (vertices) with links (edges) between them. A link is represented usually as a pair of nodes, connected by the link.

Undirected graphs: the links do not have orientation

Directed graphs: the links have orientation

Page 8: 1 Using Search in Problem Solving Part I. 2 Search and AI Introduction Introduction Search Space, Search Trees Search Space, Search Trees Search in Graphs

8

Graphs and Trees 2Graphs and Trees 2

Path: Sequence of nodes such that each two neighbors represent an edge

Cycle: a path with the first node equal to the last and no other nodes are repeated

Acyclic graph: a graph without cycles

Tree: undirected acyclic graph, where one node is chosen to be the root

Page 9: 1 Using Search in Problem Solving Part I. 2 Search and AI Introduction Introduction Search Space, Search Trees Search Space, Search Trees Search in Graphs

9

Graphs and Trees 3Graphs and Trees 3

Given a graph and a node:

• Out-going edges: all edges that start in that node

• In-coming edges : all edges that end up in that node

• Successors (Children): the end nodes of all out-going edges

• Ancestors (Parents): the nodes that are start points of in-coming edges

Page 10: 1 Using Search in Problem Solving Part I. 2 Search and AI Introduction Introduction Search Space, Search Trees Search Space, Search Trees Search in Graphs

10

A B

D E

C

G1: Undirected graph

Path: ABDCAE

Cycle: CABEC

Successors of A: E, C, B

Parents of A: E, C, B

Page 11: 1 Using Search in Problem Solving Part I. 2 Search and AI Introduction Introduction Search Space, Search Trees Search Space, Search Trees Search in Graphs

11

A B

D E

C

G2: Directed graph

Path: ABDC

Cycle: no cycles

Successors of A: C, B

Parent of A: E

Page 12: 1 Using Search in Problem Solving Part I. 2 Search and AI Introduction Introduction Search Space, Search Trees Search Space, Search Trees Search in Graphs

12

Search TreesSearch Trees

More solutions: More than one path from the More solutions: More than one path from the initial state to a goal stateinitial state to a goal state

Different paths may have common arcsDifferent paths may have common arcs The search process can be represented by a The search process can be represented by a

search treesearch tree In the search tree the different solutions will In the search tree the different solutions will

be represented as different paths from the be represented as different paths from the initial stateinitial state

One and the same state may be represented One and the same state may be represented by different nodesby different nodes

Page 13: 1 Using Search in Problem Solving Part I. 2 Search and AI Introduction Introduction Search Space, Search Trees Search Space, Search Trees Search in Graphs

13

Search MethodsSearch Methods

Uninformed (blind, exhaustive) Uninformed (blind, exhaustive) searchsearch Depth-firstDepth-first Breadth-firstBreadth-first

Informed (heuristic) searchInformed (heuristic) search Hill climbingHill climbing Best-first searchBest-first search A* searchA* search

Page 14: 1 Using Search in Problem Solving Part I. 2 Search and AI Introduction Introduction Search Space, Search Trees Search Space, Search Trees Search in Graphs

14

Exhaustive SearchExhaustive Search

Breadth-firstBreadth-first Depth-FirstDepth-First Iterative DeepeningIterative Deepening

Page 15: 1 Using Search in Problem Solving Part I. 2 Search and AI Introduction Introduction Search Space, Search Trees Search Space, Search Trees Search in Graphs

15

Breadth-First SearchBreadth-First Search

Algorithm: using a queue

1. Queue = [initial_node] , FOUND = False

2. While queue not empty and FOUND = False do:

Remove the first node NIf N = target node then FOUND = trueElse find all successor nodes of N and put

them into the queue.

In essence this is Dijkstra's algorithm of finding the shortest path between two nodes in a graph.

Page 16: 1 Using Search in Problem Solving Part I. 2 Search and AI Introduction Introduction Search Space, Search Trees Search Space, Search Trees Search in Graphs

16

Depth-first searchDepth-first search

Algorithm: using a stack

1. Stack = [initial_node] , FOUND = False

2. While stack not empty and FOUND = False do:

Remove the top node NIf N = target node then FOUND = trueElse find all successor nodes of N and put

them onto the stack.

Page 17: 1 Using Search in Problem Solving Part I. 2 Search and AI Introduction Introduction Search Space, Search Trees Search Space, Search Trees Search in Graphs

17

Depth-First Iterative Depth-First Iterative DeepeningDeepening

An exhaustive search method based on both depth-first and breadth-first search.

Carries out depth-first search to depth of 1, then to depth of 2, 3, and so on until a goal node is found.

Efficient in memory use, and can cope with infinitely long branches.

Not as inefficient in time as it might appear, particularly for very large trees, in which it only needs to examine the largest row (the last one) once.

Page 18: 1 Using Search in Problem Solving Part I. 2 Search and AI Introduction Introduction Search Space, Search Trees Search Space, Search Trees Search in Graphs

18

Comparison of depth-first Comparison of depth-first and breadth-first searchand breadth-first search

Breadth-first: without backtrackingDepth-first : backtracking.

Length of path: breadth-first finds the shortest path first.

Memory: depth-first uses less memory

Time: If the solution is on a short path - breadth first is better, if the path is long - depth first is better.

Page 19: 1 Using Search in Problem Solving Part I. 2 Search and AI Introduction Introduction Search Space, Search Trees Search Space, Search Trees Search in Graphs

19

The Search SpaceThe Search Space

The search space is known in The search space is known in advance e.g. finding a map routeadvance e.g. finding a map route

The search space is created The search space is created while solving the problem - e.g. while solving the problem - e.g. game playinggame playing

Page 20: 1 Using Search in Problem Solving Part I. 2 Search and AI Introduction Introduction Search Space, Search Trees Search Space, Search Trees Search in Graphs

20

Generate and Test Generate and Test ApproachApproach

Search tree - built during the search process

Root - corresponds to the initial state

Nodes: correspond to intermediate states (two different nodes may correspond to one and the same state)

Links - correspond to the operators applied to move from one state to the next state

Node description

Page 21: 1 Using Search in Problem Solving Part I. 2 Search and AI Introduction Introduction Search Space, Search Trees Search Space, Search Trees Search in Graphs

21

Node DescriptionNode Description

The corresponding stateThe corresponding state The parent nodeThe parent node The operator applied The operator applied The length of the path from the root to The length of the path from the root to

that nodethat node The cost of the pathThe cost of the path

Page 22: 1 Using Search in Problem Solving Part I. 2 Search and AI Introduction Introduction Search Space, Search Trees Search Space, Search Trees Search in Graphs

22

Node GenerationNode Generation

To expand a node means to apply operators to the node and obtain next nodes in the tree i.e. to generate its successors.

Successor nodes: obtained from the current node by applying the operators

Page 23: 1 Using Search in Problem Solving Part I. 2 Search and AI Introduction Introduction Search Space, Search Trees Search Space, Search Trees Search in Graphs

23

AlgorithmAlgorithmStore root (initial state) in stack/queue

While there are nodes in stack/queue DO:

Take a node

While there are applicable operators DO:

Generate next state/node (by an operator)

If goal state is reached - stop

If the state is not repeated - add into stack/queue

To get the solution: Print the path from the root to the found goal state

Page 24: 1 Using Search in Problem Solving Part I. 2 Search and AI Introduction Introduction Search Space, Search Trees Search Space, Search Trees Search in Graphs

24

Example - the Jugs Example - the Jugs problemproblem

We have to decide:

• representation of the problem state, initial and final states

• representation of the actions available in the problem, in terms of how they change the problem state.

• what would be the cost of the solution

Path costSearch cost

Page 25: 1 Using Search in Problem Solving Part I. 2 Search and AI Introduction Introduction Search Space, Search Trees Search Space, Search Trees Search in Graphs

25

Problem representationProblem representation

Problem state: a pair of numbers (X,Y): X - water in jar 1 called A, Y - water in jar 2, called B.

Initial state: (0,0)

Final state: (2, _ ) here "_" means "any quantity"

Page 26: 1 Using Search in Problem Solving Part I. 2 Search and AI Introduction Introduction Search Space, Search Trees Search Space, Search Trees Search in Graphs

26

Operations for the Jugs Operations for the Jugs problemproblem

Operators: PreconditionsEffects

O1. Fill A X < 4 (4, Y)

O2. Fill B Y < 3 (X, 3)

O3. Empty A X > 0 (0, Y)

O4. Empty B Y > 0 (X, 0)

O5. Pour A into B X > 3 - Y ( X + Y - 3 , 3)

X 3 - Y ( 0, X + Y)

O6. Pour B into A Y > 4 - X (4, X + Y - 4)

Y 4 - X ( X + Y, 0)

Page 27: 1 Using Search in Problem Solving Part I. 2 Search and AI Introduction Introduction Search Space, Search Trees Search Space, Search Trees Search in Graphs

27

Search tree built in a Search tree built in a breadth-first mannerbreadth-first manner

0,0

3,0 0,4

3,4 0,3 3,4 3,1

O1

O2

O2

O5

O1

O6

Page 28: 1 Using Search in Problem Solving Part I. 2 Search and AI Introduction Introduction Search Space, Search Trees Search Space, Search Trees Search in Graphs

28

The A* AlgorithmThe A* Algorithm

A* always finds the best

solution, provided that h(Node)

does not overestimate the

future costs.

Page 29: 1 Using Search in Problem Solving Part I. 2 Search and AI Introduction Introduction Search Space, Search Trees Search Space, Search Trees Search in Graphs

29

107

8

8

4

4

6

10

92

4

A

B: 9D : 10

F : 6

H : 5

C: 8E: 7

K: 5

2

G: 0

J: 4

Example

Start: A

Goal: G