problems and search

Upload: mukesh-sharma

Post on 02-Apr-2018

217 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/27/2019 Problems and Search

    1/39

    Problems and Search

  • 7/27/2019 Problems and Search

    2/39

    2

    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 ofsolving 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.

  • 7/27/2019 Problems and Search

    3/39

    3

    An initial state is the description of the starting

    configuration of the agent.

    An action or an operator takes the agent from onestate 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 possiblesequences of operators applied to the initial state,

    and finding out a sequence which culminates in a

    goal state.

  • 7/27/2019 Problems and Search

    4/39

    4

    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.

  • 7/27/2019 Problems and Search

    5/39

    5

    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 opponentdoes 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.

  • 7/27/2019 Problems and Search

    6/39

    6

    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 apump 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 anyn.

    Attempting to end up in a goal state.

  • 7/27/2019 Problems and Search

    7/39

    7

    Valid Operations Allowed:1. (x, y) (4, y) ifx 4

    2. (x, y) (x, 3) ify 3

    3. (x, y) (x d, y) ifx 0

    4. (x, y) (x, y d) ify 0

    5. (x, y) (0, y) ifx 0

    6. (x, y) (x, 0) ify 0

    7. (x, y) (4, y (4 x)) ifx y 4,y 0

    8. (x, y) (x (3 y), 3) ifx y 3,x 0

    9. (x, y) (x y, 0) ifx y 4, y 0

    10. (x, y) (0, x y) ifx y 3, x 0

    11. (0, 2) (2, 0)

    12. (2, y) (0, y)

  • 7/27/2019 Problems and Search

    8/39

    8

    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.

  • 7/27/2019 Problems and Search

    9/39

    9

    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.

  • 7/27/2019 Problems and Search

    10/39

    10

    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

  • 7/27/2019 Problems and Search

    11/39

    11

    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

  • 7/27/2019 Problems and Search

    12/39

    12

    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, theagent needs to know:

    The initial state.

    The operators and a description of theeffects of the operators.

  • 7/27/2019 Problems and Search

    13/39

    13

    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.

  • 7/27/2019 Problems and Search

    14/39

    14

    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.

  • 7/27/2019 Problems and Search

    15/39

    15

    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 nodeshould 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.

  • 7/27/2019 Problems and Search

    16/39

    16

    Evaluating Search strategies:

    Generally these are evaluated by the following

    criterias.

    1. Completeness: Is the strategy guaranteed to find asolution 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 nodesexpanded) (worst or average case) to find a

    solution.

    b. Space complexity: Space used by the algorithm

    measured in terms of the maximum size offringe.

  • 7/27/2019 Problems and Search

    17/39

    17

    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.

  • 7/27/2019 Problems and Search

    18/39

    18

    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.

    h T T i l

  • 7/27/2019 Problems and Search

    19/39

    19

    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

    a data structure called OPEN or fringe. Initially only thestart node is in OPEN.

    Th h t t h t h

  • 7/27/2019 Problems and Search

    20/39

    20

    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 ifstate space is small.

    The search problem will return as a solution a path to agoal node or the goal node itself.

    Like path finding in solving 15-puzzle.

    Like the N-queens problem for which the path to thesolution is not important.

    In case of large state space, it becomes impractical torepresent state space. So state space makes explicit a sufficient portion of implicit

    state space graph to find a goal node.

    S h bl t ti

  • 7/27/2019 Problems and Search

    21/39

    21

    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 }

    B d h Fi S h

  • 7/27/2019 Problems and Search

    22/39

    22

    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.

    P ti f B dth Fi t S h

  • 7/27/2019 Problems and Search

    23/39

    23

    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 non-

    leaf node has b children, has a total of1 + b + b2 + ...+ bd = (b(d+1) - 1)/(b-1) nodes OR order of O(bd)

    U if C t S h

  • 7/27/2019 Problems and Search

    24/39

    24

    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)

  • 7/27/2019 Problems and Search

    25/39

    25

    Uniform-cost search Sample

    0

  • 7/27/2019 Problems and Search

    26/39

    26

    Uniform-cost search Sample

    75

    140

    118

    X

  • 7/27/2019 Problems and Search

    27/39

    27

    Uniform-cost search Sample

    X

    140

    118

    X

    146

  • 7/27/2019 Problems and Search

    28/39

    28

    Uniform-cost search Sample

    X

    140

    229

    X

    146

    X

    Depth First Search:

  • 7/27/2019 Problems and Search

    29/39

    29

    Depth First Search:

    Expand the deepest node first

    Properties of Depth First Search:

  • 7/27/2019 Problems and Search

    30/39

    30

    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 thesearch 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 containscycles.

    The latter case can be handled by checking for cyclesin the algorithm.

    Thus Depth First Search is not complete.

    Depth Limited Search

  • 7/27/2019 Problems and Search

    31/39

    31

    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 lessthan the bound.

    Depth First Iterative Deepening (DFID)

  • 7/27/2019 Problems and Search

    32/39

    32

    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 havingno successors), then, if no solution found, do DFS to

    depth 1, etc.

  • 7/27/2019 Problems and Search

    33/39

    33

    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.

    Bi directional search

  • 7/27/2019 Problems and Search

    34/39

    34

    Bi-directional search

    Consider that the search problem is such that the arcs

    are bidirectional.

    Such that, if there is an operator that maps fromstate 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.

  • 7/27/2019 Problems and Search

    35/39

    35

  • 7/27/2019 Problems and Search

    36/39

    36

    S

    A D

    B D A E

    C E E B B F

    D F B F C E A C G

    G C G F

    14

    19 19 17

    17 15 15 13

    G 25

    11

    Forward

    Backwards

  • 7/27/2019 Problems and Search

    37/39

    37

    d

    d/2 d/2

    Time ~ bd/2 + bd/2 < bd

    Problems:

  • 7/27/2019 Problems and Search

    38/39

    38

    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 alternatesearching 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 bean efficient way to check whether a node belongs to

    another tree.

    Select an algorithm for each half, can sometime lead to

    find solution faster.

  • 7/27/2019 Problems and Search

    39/39

    39

    Summary of algorithms