You are on page 1of 27

Artificial Intelligence

CS361

Prof. Ammar Mohammed


Faculty of Computer Science
@MSA

Contanct: ammohammed@msa.edu.eg
Room: D213
Formulating Problem as a Labeled Graph

In the graph
• Each node represents a possible state
• A node is designated as the initial state
• One or more nodes represent goal states, states in which the agent’s goal
is considered accomplished
• Each edge represents a state transition caused by a specific agent action
• Associated to each edge is the cost of performing that transition
Search Graph

How do we reach a goal state?

There may be several possible ways. Or none! Factors to consider:


• cost of finding a path
• cost of traversing a path
Problem Solving as Search

Search space: set of states reachable from an initial state S0 via a (possibly
empty/finite/infinite) sequence of state transitions.
To achieve the problem’s goal
1. Search the space for a (ideally optimal) sequence of transitions
starting from S0 and leading to a goal state

2. Execute (in order) the actions associated to each transition in the


identified sequence
Example: 8-Puzzel Problem
Example: 8-Puzzel Problem

Problem: Go from state S to state G.

States: Describe the location of each tile including the blank.


Actions: Blank moves left, right, up, or down.
Initial State: The state left of the figure (S)
Goal test: Does the state match picture on the right (G)?
Path cost: Each step costs 1 unit.
Search cost: Problem is NP-complete in the size of the puzzle

Ø There are 9! Possible states (all permutations of tiles including the blank).
Example: 8-Puzzel Problem

q How can an artificial agent represent the states and the state space for this problem?

1. Choose an appropriate data structure to represent the world states


2. Define each operator as a precondition/effects pair, where the :
• Precondition holds exactly in the states the operator is applicable to
• Effects describe how a state changes into a successor state by the
application of the operator

3. Specify an initial state

4. Provide a description of the goal—to check if a reached state is a goal state


Example: 8-Puzzel Problem

States: each represented by a 3 × 3 array of numbers in [0 . . . 8], where value 0 is for


the empty cell

Initial State (S) and Goal State (G) in python


Example: 8-Puzzel Problem

Operators: 24 operators(actions) of the form OPr,c,d


Where r, c ϵ {1,2,3} , d ϵ {L,R,U,D}

If the empty space is at position (r, c), OPr,c,d moves it in the direction of d
Example: 8-Puzzel Problem

Preconditions and effects


Example: OP3,2,R

Precondition: A[3, 2] = 0 (blank)

Effects: A[3, 3] = 0

We have 24 operators in this problem formulation


Example: 8-Puzzel Problem

Better Formulation
States: each represented by a pair (A, (i, j)) where:
• A is an array of size 3×3 of numbers in [0...8]
• (i, j) is the position of the empty space (0) in the array
Example: 8-Puzzel Problem

Better Formulation
Operators: 4 operators of the form OP d where d ∈ {L, R, U, D} OPd moves
the empty space in the direction d
Example: 8-Puzzel Problem

Problem: Go from state S to state G.


Example: 8-Queen Problem

Initial State: No queen on the board


Goal test: 8 queens on the board, all safe
Path cost: 0 (only the solution counts).

Representation 1
States: any placement of 0–8 queens.
Operators: place a queen on the board.
Problem: Too many states
Example: 8-Queen Problem

Goal test: 8 queens on the board, all safe.


Path cost: 0 (only the solution counts).

Representation 2
States: 0–8 on board, all safe.
Operators: place one queen per column
starting from left.
Problem: few state (2057), but sometimes
dead-end
Example: 8-Queen Problem

Goal test: 8 queens on the board, all safe.


Path cost: 0 (only the solution counts).

Representation 3
States: 8 queens on board, one per column.
Operators: move a queen under attack in the same column.
Graph Search
Graph Search

A graph is a set of nodes and edges between them

A graph is directed if its edges can be traversed only in a specified direction

When an edge is directed from ni to nj


• it is uniquely identified by the pair (ni,nj)
• ni is a parent (or predecessor) of nj
• nj is a child (or successor) of ni
Graph Search

A path, of length k ≥ 0, is a sequence (n1,n2), (n2,n3), …, (nk,nk+1) of k successive edges

For 1 ≤ i < j ≤ k + 1,
• Ni is a ancestor of Nj; Nj is a descendant of Ni

A graph is cyclic if it has a path starting and ending with the same
node. Ex: ⟨(A,D),(D,E),(E,A)⟩
From Search Graphs to Search Trees

The set of all possible paths of a graph can be represented as a tree


• 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
• The branching factor of a node is the number of its children
From Search Graphs to Search Trees
To unravel a graph into a tree, choose a root node and trace every path from that node
until you reach a leaf node or a node already in that path

Note:
• must remember which nodes have been visited
• a node may get duplicated several times in the tree
Searching Concepts
§ A State can be expanded by generating all states that can be reached by applying a
legal operator to the state.
§ State space can also be defined by a successor function that returns all states
produced by applying a single legal operator.
§ A search Tree is generated by generating search nodes by successively expanding
states starting from the initial state as the root.
Tree Search Algorithm

Basic Idea: offline, simulated exploration of state space by generating successors of


already-explored states
Tree Search Algorithm

We need to represents the set of nodes that have been generated but not yet expanded-
Called Fringe ( a data Structure )
Tree Search Algorithm

Example:
Tree Search Algorithm

Example:

Fringe={Sibiu, Timisoara, Zerind}


Tree Search Algorithm

Example:

You might also like