1 of 97

Chapter 3

2 of 97

Solving Problems by Searching

  • Reflex agent is simple
    • base their actions on
    • a direct mapping from states to actions
    • but cannot work well in environments
      • which this mapping would be too large to store
      • and would take too long to learn
  • Hence, goal-based agent is used

3 of 97

Problem-solving agent

  • Problem-solving agent
    • A kind of goal-based agent
    • It solves problem by
      • finding sequences of actions that lead to desirable states (goals)
    • To solve a problem,
      • the first step is the goal formulation, based on the current situation

4 of 97

Goal formulation

  • The goal is formulated
    • as a set of world states, in which the goal is satisfied
  • Reaching from initial state 🡪 goal state
    • Actions are required
  • Actions are the operators
    • causing transitions between world states
    • Actions should be abstract enough at a certain degree, instead of very detailed
    • E.g., turn left VS turn left 30 degree, etc.

5 of 97

Problem formulation

  • The process of deciding
    • what actions and states to consider
  • E.g., driving Amman 🡪 Zarqa
    • in-between states and actions defined
    • States: Some places in Amman & Zarqa
    • Actions: Turn left, Turn right, go straight, accelerate & brake, etc.

6 of 97

Search

  • Because there are many ways to achieve the same goal
    • Those ways are together expressed as a tree
    • Multiple options of unknown value at a point,
      • the agent can examine different possible sequences of actions, and choose the best
    • This process of looking for the best sequence is called search
    • The best sequence is then a list of actions, called solution

7 of 97

Search algorithm

  • Defined as
    • taking a problem
    • and returns a solution
  • Once a solution is found
    • the agent follows the solution
    • and carries out the list of actions – execution phase
  • Design of an agent
    • “Formulate, search, execute”

8 of 97

9 of 97

Well-defined problems and solutions

A problem is defined by 5 components:

  • Initial state
  • Actions
  • Transition model or

(Successor functions)

  • Goal Test.
  • Path Cost.

10 of 97

Well-defined problems and solutions

  • A problem is defined by 4 components:
    • The initial state
      • that the agent starts in
    • The set of possible actions
    • Transition model: description of what each action does.

(successor functions): refer to any state reachable from given state by a single action

    • Initial state, actions and Transition model define the state space
      • the set of all states reachable from the initial state by any sequence of actions.
    • A path in the state space:
      • any sequence of states connected by a sequence of actions.

11 of 97

Well-defined problems and solutions

  • The goal test
    • Applied to the current state to test
      • if the agent is in its goal

-Sometimes there is an explicit set of possible goal states. (example: in Amman).

-Sometimes the goal is described by the properties

      • instead of stating explicitly the set of states
    • Example: Chess
      • the agent wins if it can capture the KING of the opponent on next move ( checkmate).
      • no matter what the opponent does

12 of 97

Well-defined problems and solutions

  • A path cost function,
    • assigns a numeric cost to each path
    • = performance measure
    • denoted by g
    • to distinguish the best path from others
  • Usually the path cost is
    • the sum of the step costs of the individual actions (in the action list)

13 of 97

Well-defined problems and solutions

  • Together a problem is defined by
    • Initial state
    • Actions
    • Successor function
    • Goal test
    • Path cost function
  • The solution of a problem is then
    • a path from the initial state to a state satisfying the goal test
  • Optimal solution
    • the solution with lowest path cost among all solutions

14 of 97

Formulating problems

  • Besides the four components for problem formulation
    • anything else?
  • Abstraction
    • the process to take out the irrelevant information
    • leave the most essential parts to the description of the states

( Remove detail from representation)

    • Conclusion: Only the most important parts that are contributing to searching are used

15 of 97

Evaluation Criteria

  • formulation of a problem as search task
  • basic search strategies
  • important properties of search strategies
  • selection of search strategies for specific tasks

(The ordering of the nodes in FRINGE defines the search strategy)

16 of 97

Problem-Solving Agents

  • agents whose task is to solve a particular problem (steps)
    • goal formulation
      • what is the goal state
      • what are important characteristics of the goal state
      • how does the agent know that it has reached the goal
      • are there several possible goal states
        • are they equal or are some more preferable
    • problem formulation
      • what are the possible states of the world relevant for solving the problem
      • what information is accessible to the agent
      • how can the agent progress from state to state

17 of 97

Example

18 of 97

From our Example

1. Formulate Goal

- Be In Amman�2. Formulate Problem�

- States : Cities � - actions : Drive Between Cities ��3. Find Solution

- Sequence of Cities : ajlun – Jarash - Amman

19 of 97

Our Example

  1. Problem : To Go from Ajlun to Amman

  • Initial State : Ajlween

3. Operator : Go from One City To another .

4. State Space : {Jarash , Salat , irbed,……..}

5. Goal Test : are the agent in Amman.

6. Path Cost Function : Get The Cost From The Map.

7. Solution :{ {Aj 🡪 Ja 🡪 Ir 🡪 Ma 🡪 Za 🡪 Am} , {Aj 🡪Ir 🡪 Ma 🡪 Za 🡪 Am} …. {Aj 🡪 Ja 🡪 Am} }

8. State Set Space : {Ajlun 🡪 Jarash 🡪 Amman}

20 of 97

Example: Romania

  • On holiday in Romania; currently in Arad.
  • Flight leaves tomorrow from Bucharest

  • Formulate goal:
    • be in Bucharest�
  • Formulate problem:
    • states: various cities
    • actions: drive between cities

  • Find solution:
    • sequence of cities, e.g., Arad, Sibiu, Fagaras, Bucharest�

21 of 97

Example: Romania

22 of 97

Single-state problem formulation

A problem is defined by four items:�initial state e.g., "at Arad"

  1. actions or successor function S(x) = set of actionstate pairs
    • e.g., S(Arad) = {<Arad 🡪 Zerind, Zerind>, }

  • goal test, can be
    • explicit, e.g., x = "at Bucharest"
    • implicit, e.g., Checkmate(x)

  • path cost (additive)
    • e.g., sum of distances, number of actions executed, etc.
    • c(x,a,y) is the step cost, assumed to be ≥ 0

  • A solution is a sequence of actions leading from the initial state to a goal state

23 of 97

Example problems

  • Toy problems
    • those intended to illustrate or exercise various problem-solving methods
    • E.g., puzzle, chess, etc.
  • Real-world problems
    • tend to be more difficult and whose solutions people actually care about
    • E.g., Design, planning, etc.

24 of 97

Toy problems

  • Example: vacuum world
  • Number of states: 8
  • Initial state: Any
  • Number of actions: 4
    • left, right, suck, noOp
  • Goal: clean up all dirt
    • Goal states: {7, 8}
  • Path Cost:
    • Each step costs 1

25 of 97

26 of 97

The 8-puzzle

27 of 97

The 8-puzzle

  • States:
    • a state description specifies the location of each of the eight tiles and blank in one of the nine squares
  • Initial State:
    • Any state in state space
  • Successor function:
    • the blank moves Left, Right, Up, or Down
  • Goal test:
    • current state matches the goal configuration
  • Path cost:
    • each step costs 1, so the path cost is just the length of the path

28 of 97

The 8-queens

  • There are two ways to formulate the problem
  • All of them have the common followings:
    • Goal test: 8 queens on board, not attacking to each other
    • Path cost: zero

29 of 97

The 8-queens

  • (1) Incremental formulation
    • involves operators that augment the state description starting from an empty state
    • Each action adds a queen to the state
    • States:
      • any arrangement of 0 to 8 queens on board
    • Successor function:
      • add a queen to any empty square

30 of 97

The 8-queens

  • (2) Complete-state formulation
    • starts with all 8 queens on the board
    • move the queens individually around
    • States:
      • any arrangement of 8 queens, one per column in the leftmost columns
    • Operators: move an attacked queen to a row, not attacked by any other

31 of 97

The 8-queens

  • Conclusion:
    • the right formulation makes a big difference to the size of the search space

32 of 97

Example: River Crossing

  • Items: Man, Wolf, Corn, Chicken.
  • Man wants to cross river with all items.
    • Wolf will eat Chicken
    • Chicken will eat corn.
    • Boat will take max of two.

33 of 97

3.3 Searching for solutions

34 of 97

3.3 Searching for solutions

  • Finding out a solution is done by
    • searching through the state space
  • All problems are transformed
    • as a search tree
    • generated by the initial state and successor function

35 of 97

Search tree

  • Initial state
    • The root of the search tree is a search node
  • Expanding
    • applying successor function to the current state
    • thereby generating a new set of states
  • leaf nodes
    • the states having no successors

Fringe : Set of search nodes that have not been expanded yet.

  • Refer to next figure

36 of 97

Tree search example

37 of 97

Tree search example

38 of 97

Search tree

  • The essence of searching
    • in case the first choice is not correct
    • choosing one option and keep others for later inspection
  • Hence we have the search strategy
    • which determines the choice of which state to expand
    • good choice 🡪 fewer work 🡪 faster
  • Important:
    • state space ≠ search tree

39 of 97

Search tree

  • State space
    • has unique states {A, B}
    • while a search tree may have cyclic paths: A-B-A-B-A-B-
  • A good search strategy should avoid such paths

40 of 97

Search tree

  • A node is having five components:
    • STATE: which state it is in the state space
    • PARENT-NODE: from which node it is generated
    • ACTION: which action applied to its parent-node to generate it
    • PATH-COST: the cost, g(n), from initial state to the node n itself
    • DEPTH: number of steps along the path from the initial state

41 of 97

42 of 97

Measuring problem-solving performance

  • The evaluation of a search strategy
    • Completeness:
      • is the strategy guaranteed to find a solution when there is one?
    • Optimality:
      • does the strategy find the highest-quality solution when there are several different solutions?
    • Time complexity:
      • how long does it take to find a solution?
    • Space complexity:
      • how much memory is needed to perform the search?

43 of 97

Measuring problem-solving performance

  • In AI, complexity is expressed in
    • b, branching factor, maximum number of successors of any node
    • d, the depth of the shallowest goal node.

(depth of the least-cost solution)

    • m, the maximum length of any path in the state space
  • Time and Space is measured in
    • number of nodes generated during the search
    • maximum number of nodes stored in memory

44 of 97

Measuring problem-solving performance

  • For effectiveness of a search algorithm
    • we can just consider the total cost
    • The total cost = path cost (g) of the solution found + search cost
      • search cost = time necessary to find the solution
  • Tradeoff:
    • (long time, optimal solution with least g)
    • vs. (shorter time, solution with slightly larger path cost g)

45 of 97

3.4 Uninformed search strategies

46 of 97

3.4 Uninformed search strategies

  • Uninformed search
    • no information about the number of steps
    • or the path cost from the current state to the goal
    • search the state space blindly
  • Informed search, or heuristic search
    • a cleverer strategy that searches toward the goal,
    • based on the information from the current state so far

47 of 97

Uninformed search strategies

  • Breadth-first search
    • Uniform cost search
  • Depth-first search
    • Depth-limited search
    • Iterative deepening search
  • Bidirectional search

48 of 97

Breadth-first search

  • The root node is expanded first (FIFO)
  • All the nodes generated by the root node are then expanded
  • And then their successors and so on

49 of 97

Breadth-first search

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

50 of 97

Breadth-First Strategy

New nodes are inserted at the end of FRINGE

2

3

4

5

1

6

7

FRINGE = (1)

51 of 97

Breadth-First Strategy

New nodes are inserted at the end of FRINGE

FRINGE = (2, 3)

2

3

4

5

1

6

7

52 of 97

Breadth-First Strategy

New nodes are inserted at the end of FRINGE

FRINGE = (3, 4, 5)

2

3

4

5

1

6

7

53 of 97

Breadth-First Strategy

New nodes are inserted at the end of FRINGE

FRINGE = (4, 5, 6, 7)

2

3

4

5

1

6

7

54 of 97

Breadth-first search (Analysis)

  • Breadth-first search
    • Complete find the solution eventually
    • Optimal, if step cost is 1

The disadvantage

    • if the branching factor of a node is large,
    • for even small instances (e.g., chess)
      • the space complexity and the time complexity are enormous

55 of 97

Properties of breadth-first search

  • Complete? Yes (if b is finite)
  • Time? 1+b+b2+b3+ +bd = b(bd-1) = O(bd+1)�Space? O(bd+1) (keeps every node in memory)
  • Optimal? Yes (if cost = 1 per step)�Space is the bigger problem (more than time)
  • The appropriate data structure for this is a queue. The operations on a queue are as follows:

56 of 97

BFS algorithm

57 of 97

Breadth-first search (Analysis)

  • assuming 10000 nodes can be processed per second, each with 1000 bytes of storage

58 of 97

Cont…

  • Two lessons can be learned from Figure 3.13. First, the memory requirements are a bigger problem for breadth-first search than is the execution time.
  • One might wait 13 days for the solution to an important problem with search depth 12, but no personal computer has the petabyte of memory it would take. Fortunately, other strategies require less memory.
  • The second lesson is that time is still a major factor. If your problem has a solution at depth 16, then (given our assumptions) it will take about 350 years for breadth-first search (or indeed any uninformed search) to find it.
  • In general, exponential-complexity search problems cannot be solved by uninformed methods for any but the smallest instances.

59 of 97

Uniform cost search

  • Breadth-first finds the shallowest goal state
    • but not necessarily be the least-cost solution
    • work only if all step costs are equal
  • Uniform cost search
    • modifies breadth-first strategy
      • by always expanding the lowest-cost node
    • The lowest-cost node is measured by the path cost g(n)

60 of 97

Uniform cost search algorithm

61 of 97

Uniform cost search

  • the first found solution is guaranteed to be the cheapest
    • least in depth
    • But restrict to non-decreasing path cost
    • Unsuitable for operators with negative cost

62 of 97

Cont…

  • The reason is that the first goal node that is generated may be on a suboptimal path. The second difference is that a test is added in case a better path is found to a node currently on the frontier.
  • Both of these modifications come into play in the example shown in Figure 3.15, where the problem is to get from Sibiu to Bucharest.
  • The successors of Sibiu are Rimnicu Vilcea and Fagaras, with costs 80 and 99, respectively.
  • The least-cost node, Rimnicu Vilcea, is expanded next, adding Pitesti with cost 80 + 97 = 177. The least-cost node is now Fagaras, so it is expanded, adding Bucharest with cost 99 + 211 = 310.
  • Now a goal node has been generated, but uniform-cost search keeps going, choosing Pitesti for expansion and adding a second path to Bucharest with cost 80+ 97+ 101 = 278. Now the algorithm checks to see if this new path is better than the old one; it is, so the old one is discarded. Bucharest, now with g-cost 278, is selected for expansion and the solution is returned.

63 of 97

Uniform-cost search

  • Expand least-cost unexpanded node�
  • Implementation:
    • fringe = queue ordered by path cost�
  • Equivalent to breadth-first if step costs all equal�
  • Complete? Yes, if step cost ≥ ε�
  • Time? # of nodes with g ≤ cost of optimal solution, O(bceiling(C*/ ε)) where C* is the cost of the optimal solution
  • Space? # of nodes with g ≤ cost of optimal solution, O(bceiling(C*/ ε))
  • Optimal? Yes nodes expanded in increasing order of g(n)

let�C* be the cost of optimal solution.

ε is possitive constant (every action cost)

64 of 97

Depth-first search

  • Always expands one of the nodes at the deepest level of the tree
  • Only when the search hits a dead end
    • goes back and expands nodes at shallower levels
    • Dead end 🡪 leaf nodes but not the goal
  • Backtracking search
    • only one successor is generated on expansion
    • rather than all successors
    • fewer memory

65 of 97

Depth-first search

  • Expand deepest unexpanded node�Implementation:
    • fringe = LIFO queue, i.e., put successors at front

66 of 97

Depth-first search

  • Expand deepest unexpanded node�Implementation:
    • fringe = LIFO queue, i.e., put successors at front

67 of 97

Depth-first search

  • Expand deepest unexpanded node�Implementation:
    • fringe = LIFO queue, i.e., put successors at front

68 of 97

Depth-first search

  • Expand deepest unexpanded node�Implementation:
    • fringe = LIFO queue, i.e., put successors at front

69 of 97

Depth-first search

  • Expand deepest unexpanded node
  • Implementation:
    • fringe = LIFO queue, i.e., put successors at front

70 of 97

Depth-first search

  • Expand deepest unexpanded node�Implementation:
    • fringe = LIFO queue, i.e., put successors at front

71 of 97

Depth-first search

  • Expand deepest unexpanded node�Implementation:
    • fringe = LIFO queue, i.e., put successors at front

72 of 97

Depth-first search

  • Expand deepest unexpanded node�Implementation:
    • fringe = LIFO queue, i.e., put successors at front

73 of 97

Depth-first search

  • Expand deepest unexpanded node
  • Implementation:
    • fringe = LIFO queue, i.e., put successors at front

74 of 97

Depth-first search

  • Expand deepest unexpanded node�Implementation:
    • fringe = LIFO queue, i.e., put successors at front

75 of 97

Depth-first search

  • Expand deepest unexpanded node�Implementation:
    • fringe = LIFO queue, i.e., put successors at front

76 of 97

Depth-first search

  • Expand deepest unexpanded node�Implementation:
    • fringe = LIFO queue, i.e., put successors at front

77 of 97

Depth-first search

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

78 of 97

Depth-first search (Analysis)

  • Not complete
    • because a path may be infinite or looping
    • then the path will never fail and go back try another option
  • Not optimal
    • it doesn't guarantee the best solution
  • It overcomes
    • the time and space complexities

79 of 97

Properties of depth-first search

  • Complete? No: fails in infinite-depth spaces, spaces with loops
    • Modify to avoid repeated states along path�🡪 complete in finite spaces
  • Time? O(bm): terrible if m is much larger than d
    • but if solutions are dense, may be much faster than breadth-first
  • Space? O(bm), i.e., linear space!
  • Optimal? No

80 of 97

81 of 97

Depth-Limited Strategy

  • Depth-first with depth cutoff k (maximal depth below which nodes are not expanded)�
  • Three possible outcomes:
    • Solution
    • Failure (no solution)
    • Cutoff (no solution within cutoff)

82 of 97

Depth-limited search

  • It is depth-first search
    • with a predefined maximum depth
    • However, it is usually not easy to define the suitable maximum depth
    • too small 🡪 no solution can be found
    • too large 🡪 the same problems are suffered from
  • Anyway the search is
    • complete
    • but still not optimal

83 of 97

Depth-limited search

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

depth = 3

3

6

84 of 97

Depth Limited Search Algo

85 of 97

Iterative deepening search

  • No choosing of the best depth limit
  • It tries all possible depth limits:
    • first 0, then 1, 2, and so on
    • combines the benefits of depth-first and breadth-first search

86 of 97

87 of 97

Cont..

88 of 97

Iterative deepening search (Analysis)

  • optimal
  • complete
  • Time and space complexities
    • reasonable
  • suitable for the problem
    • having a large search space
    • and the depth of the solution is not known

89 of 97

Properties of iterative deepening search

  • Complete? Yes�
  • Time? (d+1)b0 + d b1 + (d-1)b2 + + bd = O(bd)
  • Space? O(bd)
  • Optimal? Yes, if step cost = 1

90 of 97

Iterative lengthening search

  • IDS is using depth as limit
  • ILS is using path cost as limit
    • an iterative version for uniform cost search

has the advantages of uniform cost search

      • while avoiding its memory requirements
    • but ILS incurs substantial overhead
      • compared to uniform cost search

91 of 97

Bidirectional search

  • Run two simultaneous searches
    • one forward from the initial state another backward from the goal
    • stop when the two searches meet
  • However, computing backward is difficult
    • A huge amount of goal states
    • at the goal state, which actions are used to compute it?
    • can the actions be reversible to computer its predecessors?

92 of 97

Bidirectional Strategy

2 fringe queues: FRINGE1 and FRINGE2

Time and space complexity = O(bd/2) << O(bd)

93 of 97

Bidirectional search

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

94 of 97

95 of 97

Comparing search strategies

96 of 97

Avoiding repeated states

  • for all search strategies
    • There is possibility of expanding states
      • that have already been encountered and expanded before, on some other path
    • may cause the path to be infinite 🡪 loop forever
    • Algorithms that forget their history
      • are doomed to repeat it

97 of 97

Avoiding repeated states

  • Three ways to deal with this possibility
    • Do not return to the state it just came from
      • Refuse generation of any successor same as its parent state
    • Do not create paths with cycles
      • Refuse generation of any successor same as its ancestor states
    • Do not generate any generated state
      • Not only its ancestor states, but also all other expanded states have to be checked against