chapter 2

49
Chapter 2 Problem Solving by Searching (1) King Saud University College of Computer and Information Sciences Information Technology Department IT422 - Intelligent systems 1

Upload: neola

Post on 22-Feb-2016

30 views

Category:

Documents


0 download

DESCRIPTION

King Saud University College of Computer and Information Sciences Information Technology Department IT422 - Intelligent systems . Chapter 2. Problem Solving by Searching (1). Objectives. Learn how to formulate a search problem. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Chapter 2

1

Chapter 2

Problem Solving by Searching (1)

King Saud UniversityCollege of Computer and Information Sciences Information Technology DepartmentIT422 - Intelligent systems

Page 2: Chapter 2

2

Objectives

• Learn how to formulate a search problem.

• Learn the different algorithms used to solve problems by searching.

• Learn how to assess the performance of a search algorithm.

Page 3: Chapter 2

3

Introduction

• Solving a particular problem → Need to define the elements of a problem and its solution.1. Before start searching for solutions, Identify the

goal and Define the problem precisely.2. Isolate and represent the task knowledge that is

necessary to solve the problem.3. Choose and apply the best problem solving

technique(s) to the particular problem.

Page 4: Chapter 2

4

Introduction: Problem solving as search

Task = Search• Search is required to solve a wide range of

problems.• Search is a method that can be used by

computers to examine a huge problem space to find a goal.– e.g Searching for a contact lens on a football field

• Challenge: How to find the goal as quickly as possible or without using too many resources.

Page 5: Chapter 2

5

Introduction: Problem solving as search

• A problem can be considered to consist of a goal and a set of actions that can be taken to lead to the goal

• Problem: On holiday in Romania; currently in Arad. Flight leaves tomorrow from Bucharest. Find a short route to drive to Bucharest.

• Task = finding the sequence of actions that lead to the desirable goal.

Start

End

Page 6: Chapter 2

6

Introduction: Problem solving as search

• Toy problems– Vacuum world– The N-Queen– Rubik's cube– The 8-Puzzle

• The 8-puzzle belongs to the family of Sliding-block puzzles.

• The 8-puzzle has 9!/2=181,440 reachable states.

• The 15-puzzle has around 1.3 trillion states…

• Real world Problems- Touring problems- Route Finding- Travelling salesperson- VLSI Layout- Robot navigation- Internet searching

Page 7: Chapter 2

7

Introduction: Problem solving as search

• Search can be performed without any additional information about how to reach a solution:

Blind Search (Uniformed search)

• Search can be performed using heuristics: Informed Search

Page 8: Chapter 2

8

Uninformed search

• To successfully operate, blind search should satisfy some properties:1. It must be complete: It must generate every

possible solution otherwise it might miss a suitable solution.

2. It must be able to find the best solution.

Page 9: Chapter 2

9

Uninformed search

• Key questions to be addressed:– What goal do we need to achieve?– How to know when a goal is reached?– What knowledge we need?– What actions do we need to do?

• A goal can be described as:– A task to be accomplished– A situation to be reached– A set of properties to be acquired

Page 10: Chapter 2

10

Uninformed search: Basic concepts

• State: configuration, situation, point……

• Problem formulation: Process of deciding what actions and states to consider given a goal.

• Solution: Sequence of actions that help achieving the goal: a path from an initial state to the goal state .

• Search: Process of looking for a solution. Takes input (problem) and returns solution.

• Execution: To carry out the actions recommended by the solution

Page 11: Chapter 2

11

Uninformed search: Problem formulation

• Problem formulation: a problem can be formally defined by:1. State representation: What information is necessary to encode to solve

a problem.

2. Initial state: starting point.3. Actions: A description of the possible actions that can be executed in a

particular state.

4. Transition model (Description of what each action does)

5. Goal test: determines whether a given state is a goal state.

6. Path cost function: assigns a cost to each path. Should reflect the performance measure used to assess the quality of any solution.

Page 12: Chapter 2

12

Uninformed search: Problem formulation

• Successor function:– Given a particular state x,

fsuccessor(x) = {(<action, new state>) } where action is one of the legal actions in state x and new state is the successor state that can be reached from x by applying action.

• The successor function allows together with the initial state to define the state space.

• STATE SPACE: The set of all states reachable from the initial state.(see AIMA 3rd Ed. page 67)

• A path in the state space is a sequence of states connected by a sequence of actions.

Page 13: Chapter 2

13

Uninformed search: Problem formulation

• More about state space:– A state space can be represented as a directed graph:• where the nodes are states and the arcs between them

are the actions• e.g.: the Map of Romania in slide #5 (if we view each road as

standing for two driving directions)

Page 14: Chapter 2

14

Uninformed search: Problem formulation

Example problem 1: The vacuum world

1. State representation: The agent is on one of two locations, each of which might or might not contain dirt. Thus, there are 2 x 22 = 8 possible world states.

2. Initial state: Any state can be designated as the initial state.3. Actions: each state has just 3 actions: Left, Right, and Suck.4. Transition Model: Generates the legal states that result from applying the

following actions: (Left, Right, Suck). 5. Goal test: Check whether all the squares are clean.6. Path cost function: Each step costs 1. So the path cost is the number of steps.

Page 15: Chapter 2

15

Uninformed search: Problem formulation

Example problem 1: The vacuum world state space as a graph

Page 16: Chapter 2

16

Uninformed search: Problem formulation

• If the initial state is known, for simplicity, the state space can also be drawn as a tree.

• We designate an initial state for the vacuum world:

Page 17: Chapter 2

17

Uninformed search: Problem formulation

• A tree is a directed acyclic graph all of whose nodes have at most one parent.

• A root of a tree is a node with no parents.• A leaf is a node with no children.• Graphs can be turned into trees by duplicating nodes and breaking cyclic

paths, if any.• To convert a graph into a tree:

– choose a root node– trace every path from that node until you reach a leaf node (goal) or a node

already in that path (Repeated State or R.S.)• AGAIN, trees are used only for simplicity, i.e. if the problem is complex

and it will be hard to draw it as a graph.

Page 18: Chapter 2

18

Uninformed search: Problem formulation

• Vacuum world state space as a tree

LR

S

R.S.L R S L R S

R.S.R.S.R.S. R.S. L R SL R S

R.S.R.S. Goal StateR.S.R.S. L R S

Goal StateR.S.R.S.

What is the total number of unrepeated states?

Page 19: Chapter 2

19

Uninformed search: Problem formulation

• Solution definition: – A potential solution 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.

Page 20: Chapter 2

20

Uninformed search: Problem formulation

• An example of solution path:

Arad

Zerind Sibiu Timisoara

Oradea Fagaras Rimnicu VilceaArad

Sibiu Bucharest

Page 21: Chapter 2

21

Example problem 2 - The Touring problem: Given a set of n cities, the touring problem consists in visiting cities at least once starting and ending in the same city.

• States: Specified by the current city and the set of cities already visited.

• Initial state: Any state can be designed as the initial state.

• Actions: take a trip between adjacent cities.

• Transition model (Successor function): Generates the next city to visit according to the current state.

• Goal test: Ending city reached and all cities have been visited.

• Path cost: Sum of all step costs.

Uninformed search: Problem formulation

Page 22: Chapter 2

22

Uninformed search: Searching for solutions

• To solve the problem, we need to define a search strategy that allows to explore efficiently the state space.

• A solution is an action sequence.

• The possible action sequences starting at the initial state form a search tree.

• Each state is represented by a node.

• Expansion operation: A node is expanded if the successor function for the corresponding state generates new states.

Page 23: Chapter 2

23

Uninformed search: Informal description

Page 24: Chapter 2

24

Uninformed search: Towards a formal description

• Search algorithms require a data structure to keep track of the search tree.

• Node n definition: Data structure with 5 components:– State: representation of a physical configuration. – Parent node: The node in the search tree that

generated this node.– Action: action applied to the parent node to

generate the node.– Path cost: cost denoted by g(n), of the path from

the initial state to the node n.– Depth: The number of steps along the path from

the initial state.

Parent node

Node(State)

Children

Page 25: Chapter 2

25

Uninformed search: Towards a formal description

• How to deal with non expanded nodes?Frontier: set of nodes generated but not yet expanded. Each node is a leaf node.

• What is the suitable representation of frontier?1. Set of nodes: Simplest way but could be

computationally expensive.2. Queue: best implementation of the frontier.

Page 26: Chapter 2

26

Uninformed search: Towards a formal description

• What are the operations to be performed on the queue?– Make-Node(state,parent,action,depth,cost): creates a node

with the given parameters.– Empty?(queue): returns TRUE only if there are no more nodes

in the queue.– First(queue) : returns the first node of the queue.– Remove-First(queue): returns First(queue) and removes it from

the queue.– Insert(element, queue): inserts a node into the queue and

returns the resulting queue.– Insert-All(elements, queue): inserts a set of nodes into the

queue and returns the resulting queue.

Page 27: Chapter 2

27

Uninformed search: Repeated states

• Problem: Possibility of wasting time by expanding states that have already been encountered and expanded before.

• Occurs when actions are reversible. (Example: move left, and move right)

• A solvable problem can become unsolvable if the algorithm does not detect them systematically.

• For detection purposes, a comparison operation is needed. Solution: keep a list to store every expanded node.

Page 28: Chapter 2

28

Uninformed search: Avoiding repeated states

Page 29: Chapter 2

29

Uninformed search: Performance evaluation

• The performance of problem solving algorithms can be evaluated along the following dimensions:

1. Completeness: is it guaranteed to find a solution when there is one?

2. Optimality: Does the strategy find the optimal solution (i.e., Has the lowest path cost among all solutions)?

3. Time complexity: how long does it take to find a solution? It can be measured by the number of generated nodes.

4. Space complexity: how much memory is needed to perform the search? It can be measured in terms of the maximum number of nodes stored in memory.

Page 30: Chapter 2

30

Uninformed search: Performance evaluation

Complexity for search on tree is expressed in terms of 3 quantities :

• b: The branching factor: maximum number of successors of any node.

• d: The depth of the shallowest goal node (i.e., the number of steps along the path from the root).

• m: The maximum length of any path in the state space.

Page 31: Chapter 2

31

Uninformed search: Basic strategies

• Breadth First Search (BFS)

• Depth First Search (DFS)

• Depth Limited Search (DLS)

• Uniform Cost Search (UCS)

• Iterative Deepening Search (IDS)

Page 32: Chapter 2

32

Uninformed search: Basic strategies

• Strategies that order nodes without using any domain specific information (only problem definition is provided).

• Generate successors and simply differentiate between a goal state and a non goal state.

• Blind search strategies differ by the order of node expansion.

Page 33: Chapter 2

33

Uninformed search: Breadth First Search

• Main idea: Nodes at depth i are expanded before nodes at depth (i+1).

• Implementation: use of a First-In-First-Out queue (FIFO) for the frontier. Nodes visited first are expanded first.

• The goal test is applied to each node when it is generated rather then when it is selected for expansion.

• Shallow nodes are expanded before deeper nodes.

Page 34: Chapter 2

34

Breadth-first search on a graphfunction BREADTH-FIRST-SEARCH(problem) returns a solution, or failure

node ←a node with STATE = problem.INITIAL-STATE, PATH-COST = 0if problem.GOAL-TEST(node.STATE) then return SOLUTION(node)frontier ←a FIFO queue with node as the only elementexplored ←an empty setloop do

if EMPTY?( frontier) then return failurenode←POP( frontier ) /* chooses the shallowest node in frontier */add node.STATE to exploredfor each action in problem.ACTIONS(node.STATE) do

child ←CHILD-NODE(problem, node, action)if child .STATE is not in explored or frontier thenif problem.GOAL-TEST(child .STATE) then return SOLUTION(child )frontier ←INSERT(child , frontier )

Page 35: Chapter 2

35

Uninformed search: BFS evaluation• Completeness: Yes, if the branching factor b is finite.

• Optimality: shallowest goal is not necessarily the optimal one. It is optimal if all actions have the same cost.

• Time complexity: At the worst case, BFS expands every node (except goal node) thus taking time as follows:

1+b+ + b2 + b3 +….+ bd = O(bd)

If the goal test is applied to nodes when selected for expansion, rather then generated, the time complexity would be: O(bd+1)

• Memory complexity: BFS keeps every node in memory. Space is a big problem.

Page 36: Chapter 2

36

Uninformed search: Depth First Search

• DFS expands the deepest node in the current frontier on the search tree.

• DFS algorithm is an instance of the graph-search algorithm.

• As nodes are expanded, they are dropped from the frontier so then the search

“backs up” to the next shallowest node that still has unexplored successors.

• DFS strategy is implemented using (LIFO) queue or stack (i.e., the most recently

generated node is chosen for expansion).

• Another alternative is to implement DFS with a recursive function that calls

itself on each of its children in turn.

Page 37: Chapter 2

37

Uninformed search: Depth First Search

Page 38: Chapter 2

38

Uninformed search: DFS evaluation• Completeness: Incomplete in case of unbounded depth containing no solution.

• Optimality: does not provide always optimal solutions.

• Time complexity: At the worst case, DFS generates about O(bm) nodes in the search tree.

• Memory complexity: DFS requires less memory than BFS. It needs to store only a single path from the root to a leaf node along with the remaining unexpanded sibling nodes for each node in the path. The required storage is O(bm) where b is the branching factor and m maximum depth.

Page 39: Chapter 2

39

Uninformed search: Depth Limited Search

• It is simply DFS with a depth bound.

• Searching is not permitted beyond the depth bound.

• Works well if we know what the depth of the solution is.

• Termination is guaranteed.

• If the solution is beneath the depth bound, the search cannot find the goal (hence this search algorithm is incomplete).

• Otherwise use Iterative deepening search (IDS).

Page 40: Chapter 2

40

Uninformed search: Iterative Deepening depth First Search

Page 41: Chapter 2

41

Uninformed search: IDS (cont.)• Key idea: Iterative deepening search (IDS) applies DLS repeatedly with

increasing depth. It terminates when a solution is found or no solutions exists.

• IDS combines the benefits of BFS and DFS: Like DFS the memory requirements are very modest (O(bd)). Like BFS, it is complete when the branching factor is finite. IDS is optimal when the step costs are all identical.

• The time complexity: the total number of generated nodes is : – N(IDS)=(d)b + (d-1) b2 + …+(1)bd=O(bd)

• In general, iterative deepening is the preferred Uninformed search method when there is a large search space and the depth of the solution is not known.

Page 42: Chapter 2

42

Uninformed search: Uniform Cost Search

• In case of equal step costs, Breadth First search finds the optimal solution.

• For any step-cost function, Uniform Cost search expands the node n with the lowest path cost.

• UCS takes into account the total cost.

• UCS is guided by path costs rather than depths. Nodes are ordered according to their path cost.

Page 43: Chapter 2

43

UCS algorithm on a graph

Page 44: Chapter 2

44

Uninformed search: UCS

Page 45: Chapter 2

45

Uninformed search: UCS

Page 46: Chapter 2

46

Uninformed search: UCS

Page 47: Chapter 2

47

Bidirectional Search

• Idea: run 2 simultaneous searches. One forward from the initial state and

the other backward from the goal. The hope target is that the 2 searches

meet in the middle.

• Motivation: bd/2+ bd/2 < bd

• Implementation: check whether the frontiers of the 2 searches intersect

rather than the goal test. If they do, a solution has been found (not

necessarily an optimal one).

Page 48: Chapter 2

48

Bidirectional Search• The check is done when each node is generated or selected for

expansion and, with a hash table, will take constant time.• Example: A problem has solution depth d=6• Each direction runs BFS one node at a time. In the worst case they will

meet when they have generated all of the nodes at depth 3.• For b=10, total node generations=2220 compared with 1 111 110 for a

standard BFS.• Weakness: At least 1 of the 2 frontiers must be kept in memory.• Bidirectional search is more attractive because the reduction of time

complexity is significant. But how do we search backward? Some cases may require substantial ingenuity.

Page 49: Chapter 2

49

Uninformed search: Summary• Search: process of constructing sequences of actions that achieve a goal given a problem.

• Goal identification is the first step in solving problems by searching. It facilitates problem formulation.

• Formulating a problem requires specifying 5 components: Initial state, a set of actions, a transition model describing the results of those actions, a goal test and path cost function. Environment is represented as a state space.

• A solution is a path from the initial state to a goal state.

• Search algorithms are judged on the basis of completeness, optimality, time complexity and space complexity.

• Several search strategies, the basic algorithms are: BFS, DFS, DLS, IDS,UCS and Bidirectional search.