Sei sulla pagina 1di 39

Problems and Search

To build a system to solve a particular problem we need:


To define the problem precisely: Specify initial solution(s), and finally what acceptable solutions will be. Analyze the problem: Finding out important features which can have important impact on the technique of solving the problem Isolate and represent the task knowledge necessary to solve the problem. Choose the best problem solving technique(s) and apply it to solve the particular problem.
2

An initial state is the description of the starting configuration of the agent. An action or an operator takes the agent from one state to another state which is called a successor state. A plan is a sequence of actions. The cost of a plan is referred to as the path cost. It is a positive number. Search is the process of considering various possible sequences of operators applied to the initial state, and finding out a sequence which culminates in a goal state.
3

So A search problem consists of the following: S: the full set of states S0 : the initial state A:SS is a set of operators G is the set of final states. (G S). This sequence of actions is called a solution plan. A sequence of states is called a path.

A search problem is represented using a directed graph. The states are represented as nodes. The allowed actions are represented as arcs. 4

State Space Search: Playing Chess


Each position can be described by an 8-by-8 array.

Initial position is the game opening position.


Goal position is any position in which the opponent
does not have a legal move and his or her king is under attack.

Legal moves can be described by a set of rules. State space is a set of legal positions.
Steps for solution :

Starting at the initial state. Using the set of rules to move from one state to
another.

Attempting to end up in a goal state.

State Space Search: Water Jug Problem


You are given two jugs, a 4-litre one and a 3-litre one. Neither has any measuring markers on it. There is a pump that can be used to fill the jugs with water. How can you get exactly 2 litres of water into 4-litre jug.

State: (x, y)
x = 0, 1, 2, 3, or 4 y = 0, 1, 2, 3

Start state: (0, 0).

Goal state: (2, n) for any n. Attempting to end up in a goal state.


6

Valid Operations Allowed:


1. (x, y) (4, y) if x 4

2.
3. 4. 5. 6. 7. 8. 9.

(x, y)
(x, y) (x, y) (x, y) (x, y) (x, y) (x, y) (x, y)

(x, 3)
(x (x, y (0, y) (x, 0) (4, y (x (x (0, x (2, 0) (0, y) (3 y, 0) y) (4 x)) y), 3) d, y) d)

if y
if x if y if x if y if x if x if x if x

3
0 0 0 0 y y y y 4, y 3, x 4, y 3, x 0 0 0 0

10. (x, y) 11. (0, 2) 12. (2, y)

State Space Search: 8 queens problem


The problem is to place 8 queens on a chessboard so that no two queens are in the same row, column or diagonal.

N queens problem formulation 1


States: Any arrangement of 0 to 8 queens on the board Initial state: 0 queens on the board Successor function: Add a queen in any square Goal test: 8 queens on the board, none are attacked. The initial state has 64 successors. Each of the states at the next level have 63 successors, and so on.

N queens problem formulation 2


States: Any arrangement of 8 queens on the board Initial state: All queens are at column 1 Successor function: Change the position of any one queen Goal test: 8 queens on the board, none are attacked

10

N queens problem formulation 3


States: Any arrangement of k queens in the first k rows such that none are attacked Initial state: 0 queens on the board Successor function: Add a queen to the (k+1)th row so that none are attacked. Goal test : 8 queens on the board, none are attacked

11

Explicit vs Implicit state space The state space may be explicitly represented by a graph. But more typically the state space can be implicitly represented and generated when required. To generate the state space implicitly, the agent needs to know: The initial state. The operators and a description of the effects of the operators.

12

Search Strategies
Requirements of a good search strategy:
1. It causes motion: Otherwise, it will never lead to a solution.

2. It is systematic: Otherwise, it may use more steps than necessary.


3. It is efficient: Find a good, but not necessarily the best, answer.

13

The basic search algorithm

States generated are nodes. We also maintain a list of nodes called the fringe. The successors of the current expanded node are put in fringe.
14

Issues in Search Strategy:


The search tree may be unbounded. Due to state space being infinite. Due to loops in the search space. What should we return a path or a node? The answer to this depends on the problem. If we select a node for expansion, then which node should we select? The search graph may be weighted or unweighted. Some problem may demand to find a minimal cost path or to find path as soon as possible. Is there any heuristic available about intermediate states.
15

Evaluating Search strategies: Generally these are evaluated by the following criterias. 1. Completeness: Is the strategy guaranteed to find a solution if one exists?

2. Optimality: Does the solution have low cost or the minimal cost?
3. What is the search cost associated with the time and memory required to find a solution? a. Time complexity: Time taken (number of nodes expanded) (worst or average case) to find a solution. b. Space complexity: Space used by the algorithm measured in terms of the maximum size of 16 fringe.

Different Search Strategies


1. Uninformed search (blind search)

Having no information about the number of steps from the current state to the goal.

2. Informed search (heuristic search) More efficient than uninformed search.

17

Search Tree
Consider the explicit state space graph shown in the figure.

The corresponding search tree is: Start from initial state and list all possible paths.

18

Search Tree Terminology


Root Node: Leaf Node: Ancestor/Descendant: Branching factor: Path:

Data structure of a Node: A node contains the following: A state description A pointer to the parent of the node Depth of the node The operator that generated this node Cost of this path (sum of operator costs from the start state) The nodes that the algorithm has generated are kept in 19 the a data structure called OPEN or fringe. Initially only start node is in OPEN.

The search process constructs a search tree, where root is the initial state and leaf nodes are nodes not yet expanded (i.e., in fringe) or having no successors (i.e., dead-ends) Each of these nodes a partial solution path. Search tree may be infinite because of loops even if state space is small. The search problem will return as a solution a path to a goal node or the goal node itself. Like path finding in solving 15-puzzle. Like the N-queens problem for which the path to the solution is not important. In case of large state space, it becomes impractical to represent state space.
So state space makes explicit a sufficient portion of implicit 20 state space graph to find a goal node.

Search problem representation: S: set of states. Initial state S0 S. A:SS`, An action moves from one state to another state S` Search Problem: {S, s0, A,G} A plan is a sequence of actions. P={a0, a1, a2, . . . an}. Leading to traversal of several states {S0,S1,S2. . . .SN+1 G } 21

Breadth First Search:

Note that in breadth first search the newly generated nodes are put at the back of fringe. It expands the shallowest node first.

22

Properties of Breadth-First Search


Assume that every non-leaf node has b children, d is the depth of the shallowest goal node, and m maximum the depth of total space. FIFO Complete. Optimal (i.e., admissible) if all operators have the same cost. Otherwise, breadth first search finds a solution with the shortest path length but path cost may not be cheap. It has exponential time and space complexity. Then the time and space complexity of the algorithm is O(bd). A complete search tree of depth d where each nonleaf node has b children, has a total of 1 + b + b2 + ... + bd = (b(d+1) - 1)/(b-1) nodes OR order of O(bd)
23

Uniform Cost Search



Put by Dijkstra [1959]. The algorithm expands nodes in the order of their cost from the source. The newly generated nodes are put in FRINGE / OPEN according to their path costs. This ensures that when a node is selected for expansion it is the node with cheapest cost . If g(n) = cost of the path from the start node to the current node n, then sort nodes by increasing value of g. Properties of this search algorithm are: Complete Optimal/Admissible Exponential time and space complexity, O(bd)
24

Uniform-cost search Sample

25

Uniform-cost search Sample

75

X 140 118

26

Uniform-cost search Sample


146

X X
140 118

27

Uniform-cost search Sample


146

X X
140

229

28

Depth First Search:

Expand the deepest node first

29

Properties of Depth First Search:


LIFO Exponential time O(bd). Space taken is linear, the depth of the search tree, O(bm), If m is the maximum depth of a node in the search space. It can be seen that the time taken by the algorithm is related to the maximum depth of the search tree. If the search tree has infinite depth, the algorithm may not terminate. This can happen if the search space is infinite. It can also happen if the search space contains cycles. The latter case can be handled by checking for cycles in the algorithm. Thus Depth First Search is not complete. 30

Depth Limited Search

The incompleteness of DFS can be taken care if depth is made limited. Here, Nodes are only expanded if they have depth less than the bound.

31

Depth-First Iterative Deepening (DFID)

If we fix the depth initially, it might happen that goal may not fall within it, So. First do DFS to depth 0 (i.e., treat start node as having no successors), then, if no solution found, do DFS to depth 1, etc.

32

If branching factor is b and solution is at depth d, then nodes at depth d are generated once and nodes at depth d-1 are generated twice etc. Hence bd+2b(d-1)++db=bd/(1-1/b)2=O(bd) Linear space complexity is O(bd). Has advantage of BFS i.e. complete Has advantage of DFS i.e. limited space and finds longer paths more quickly. Requires linear memory. Generally used in large state space with solution depth unknown.

33

Bi-directional search
Consider that the search problem is such that the arcs are bidirectional. Such that, if there is an operator that maps from state A to state B, there is another operator that maps from state B to state A. Many search problems have reversible arcs, such as 8-puzzle, 15-puzzle, path planning etc. However the water jug problem is a problem that does not have this property. But if the arcs are reversible, we can instead of starting from the start state and searching for the goal, we may start from a goal state and try reaching the start state. If there is a single state that satisfies the goal property, the search problems are identical. 34

35

S A B C 11 D 14 E D E A B B D E F

Forward Backwards

C 17

A 15

C 15

G 13

G 19

C G 19 17

F
G 25
36

d d/2 d/2

Time ~ bd/2 + bd/2 < bd

37

Problems:
If more than one goal. How do we search backwards from goal? One should be able to generate predecessor states. Predecessors of node n are all the nodes that have n as successor.
Algorithm: Bidirectional search involves alternate searching from the start state toward the goal and from the goal state toward the start. The algorithm stops when the frontiers intersect. For Bi-directional search to work better, there should be an efficient way to check whether a node belongs to another tree. Select an algorithm for each half, can sometime lead to find solution faster. 38

Summary of algorithms

39

Potrebbero piacerti anche