You are on page 1of 74

Artificial Intelligence

Module 1 Part - 3

Solving problems by searching

Simple Vacuum Reflex Agent


function Vacuum-Agent([location,status]) returns Action if status = Dirty then return Suck else if location = A then return Right else if location = B then return Left

It is based on reflex actions, i.e., actions having a direct mapping on states. A generalized version of above algorithm is as follows:

Simple Vacuum Reflex Agent


- defined by percepts
function Simple-Reflex-Agent(percept) returns an action Static: rules, a set of conditions-action rules state Interpret-Input(percept) rule Rule-Match(state, rules) action Rule-Action[rule] return action

A simple reflex agent, i.e., it acts according to a rule whose condition matches the current state based on the percept

A model based Reflex Agent


It keeps track of the current states using an internal model, it chooses an action as a reflex agent
function Reflex-Agent-State(percept) returns an action Static: state, a description of the current world state rules, a set of conditions-action rules action, the most recent action, initially none state Update-State(state, action, percept) rule Rule-Match(state, rules) action Rule-Action[rule] return action

As the current percept is mixed with the internal state hence this model is an Intelligent agent having learning.

Goal based Agents


They consider future actions, the desirability of their outcomes as goals help organize behaviour by limiting the objectives that the agent is trying to achieve. Goal based agents are also called problem solving agents. Problem solving agents decide what to do by finding sequences of actions that lead to the desirable states. Goal formulation is based on the current situation and the agent performance measure is the first step in problem solving. Problem formulation is the process of deciding what actions and states to consider, given a goal.

Problem-solving agents

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

Source

Example: Romania

Destination

Problem types
Deterministic, fully observable single-state problem
Agent knows exactly which state it will be in; solution is a sequence

Non-observable

sensorless problem (conformant)

Agent may have no idea where it is; solution is a sequence

Nondeterministic and/or partially observable problem


percepts provide new information about current state often interleave search, execution

contingency

Unknown state space

exploration problem

Single-state problem formulation


A problem is defined by four items: 1. initial state e.g., "at Arad 2. actions or successor function S(x) = set of actionstate pairs
e.g., S(Arad) = {<Arad

Zerind, Zerind>, }

3. goal test, can be


explicit, e.g., x = "at Bucharest" implicit, e.g., Checkmate(x)

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

Selecting a state space


Real world is absurdly complex
state space must be abstracted for problem solving

(Abstract) state = set of real states (Abstract) action = complex combination of real actions
e.g., "Arad Zerind" represents a complex set of possible routes, detours, rest stops, etc.

For guaranteed realizability, any real state "in Arad must get to some real state "in Zerind" (Abstract) solution =
set of real paths that are solutions in the real world

Each abstract action should be "easier" than the original problem

Example: Water Pouring


Given a 4 gallon bucket and a 3 gallon bucket, there is unlimited amount of water supply How can we measure exactly 2 gallons into the 4 gallon bucket?
There are no markings on the bucket You must fill each bucket completely

Example: Water Pouring


State: (x, y) x = 0, 1, 2, 3, or 4 Initial state:
The buckets are empty, represented by ( 0 0 )

y = 0, 1, 2, 3

Goal state:
One of the buckets has two gallons of water in it Represented by either ( x 2 ) or ( 2 x )

Path cost:
1 per unit step

14

Example: Water Pouring


Actions and Successor Function
Fill a bucket
(x y) -> (3 y) (x y) -> (x 4)

Empty a bucket
(x y) -> (0 y) (x y) -> (x 0)

Pour contents of one bucket into another


(x y) -> (0, x+y) or (x+y-4, 4) (x y) -> (x+y, 0) or (3, x+y-3)

15

Example: Water Pouring

Water Pouring : Puzzle-solving as Search


State representation: (x, y) x: Contents of four gallon y: Contents of three gallon Start state: (0, 0) Goal state: (2, n) Operators Fill 3-gallon from reservoir, fill 4-gallon from reservoir Fill 3-gallon from 4-gallon , fill 4-gallon from 3-gallon Empty 3-gallon into 4-gallon, empty 4-gallon into 3-gallon Dump 3-gallon down drain, dump 4-gallon down drain

Production Rules for the Water Jug Problem


1. Fill the 4-gallon jug 1. Fill the 3-gallon jug 1. Pour some water out of the 4-gallon jug 1. Pour some water out of the 3-gallon jug 1. Empty the 4-gallon jug on the ground 1. Empty the 3-gallon jug on the ground 1. Pour water from the 3-gallon jug into the 4-gallon jug until the 4-gallon jug is full 2. Pour water from the 4-gallon jug into the 3-gallon jug until the 3-gallon jug is full 3. Pour all the water from the 3-gallon jug into the 4-gallon jug 4. Pour all the water from the 4-gallon jug into the 3-gallon jug

Production rules for Water Jug Problem


The operators to be used to solve the problem can be described as follows:
Sl No 1 2 3 4 5 6 7 Current state (x,y) if x < 4 (x,y) if y <3 (x,y) if x > 0 (x,y) if y > 0 (x,y) if x>0 (x,y) if y >0 (x,y) if x+y >= 4 and y >0 Next State (4,y) (x,3) (x-d, y) (x, y-d) (0, y) (x,0) (4, y-(4-x)) Descripion Fill the 4 gallon jug Fill the 3 gallon jug Pour some water out of the 4 gallon jug Pour some water out of the 3-gallon jug Empty the 4 gallon jug Empty the 3 gallon jug on the ground Pour water from the 3 gallon jug into the 4 gallon jug until the 4-gallon jug is full

Production rules for Water Jug Problem


8 (x, y) if x+y >= 3 and x>0 (x-(3-y), 3) Pour water from the 4-gallon jug into the 3-gallon jug until the 3-gallon jug is full Pour all the water from the 3gallon jug into the 4-gallon jug Pour all the water from the 4gallon jug into the 3-gallon jug Pour the 2 gallons from 3-gallon jug into the 4-gallon jug Empty the 2 gallons in the 4gallon jug on the ground

(x, y) if x+y <=4 and y>0 (x, y) if x+y <= 3 and x>0 (0,2)

(x+y, 0)

10

(0, x+y)

11

(2,0)

12

(2,y)

(0,y)

One Solution to the Water Jug Problem


Gallons in the 4-Gallon Jug 0 0 3 3 4 0 2 Gallons in the 3-Gallon Jug 0 3 0 3 2 2 0 Rule Applied 2 9 2 7 5 or 12 9 or 11

Vacuum world state space graph

states? actions? goal test? path cost?

Vacuum world state space graph

states? integer dirt and robot location actions? Left, Right, Suck goal test? no dirt at all locations path cost? 1 per action

Example: The 8-puzzle

states? actions? goal test? path cost?

Example: The 8-puzzle

states? locations of tiles actions? move blank left, right, up, down goal test? = goal state (given) path cost? 1 per move

Example: 8-queens problem

State-Space problem formulation


states? -any arrangement of n<=8 queens
-or arrangements of n<=8 queens in leftmost n columns, 1 per column, such that no queen attacks any other.

initial state? no queens on the board actions? -add queen to any empty square
-or queens. add queen to leftmost empty square such that it is not attacked by other

goal test? 8 queens on the board, none attacked. path cost? 1 per move

Example: robotic assembly

states?: real-valued coordinates of robot joint angles parts of the object to be assembled actions?: continuous motions of robot joints goal test?: complete assembly path cost?: time to execute

Tree search algorithms


Basic idea:
simulated exploration of state space by generating successors of already-explored states (expanding states)

This strategy is what differentiates different search algorithms

Tree search example

Tree search example

Tree search example

Implementation: general tree search

Search Tree for the 8 puzzle problem

Implementation: states vs. nodes


A state is a (representation of) a physical configuration A node is a data structure constituting part of a search tree includes state, parent node, action, path cost g(x), depth

The Expand function creates new nodes, filling in the various fields and using the SuccessorFn of the problem to create the corresponding states.

Search strategies
A search strategy is defined by picking the order of node expansion Strategies are evaluated along the following dimensions:
completeness: does it always find a solution if one exists? time complexity: number of nodes generated space complexity: maximum number of nodes in memory optimality: does it always find a least-cost solution?

Time, space complexity are measured in terms of


b: maximum branching factor of the search tree d: depth of the least-cost solution m: maximum depth of the state space (may be )

Uninformed search strategies


Uninformed search strategies use only the information available in the problem definition Breadth-first search Uniform-cost search Depth-first search Depth-limited search Iterative deepening search Bidirectional search

Breadth-first search
Expand shallowest unexpanded node Implementation:
fringe is a FIFO queue, i.e., new successors go at end

Breadth-first search
Expand shallowest unexpanded node Implementation:
fringe is a FIFO queue, i.e., new successors go at end

Breadth-first search
Expand shallowest unexpanded node Implementation:
fringe is a FIFO queue, i.e., new successors go at end

Breadth-first search
Expand shallowest unexpanded node Implementation:
fringe is a FIFO queue, i.e., new successors go at end

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)

Depth-first search
Expand deepest unexpanded node Implementation:
fringe = LIFO queue, i.e., put successors at front

Depth-first search
Expand deepest unexpanded node Implementation:
fringe = LIFO queue, i.e., put successors at front

Depth-first search
Expand deepest unexpanded node Implementation:
fringe = LIFO queue, i.e., put successors at front

Depth-first search
Expand deepest unexpanded node Implementation:
fringe = LIFO queue, i.e., put successors at front

Depth-first search
Expand deepest unexpanded node Implementation:
fringe = LIFO queue, i.e., put successors at front

Depth-first search
Expand deepest unexpanded node Implementation:
fringe = LIFO queue, i.e., put successors at front

Depth-first search
Expand deepest unexpanded node Implementation:
fringe = LIFO queue, i.e., put successors at front

Depth-first search
Expand deepest unexpanded node Implementation:
fringe = LIFO queue, i.e., put successors at front

Depth-first search
Expand deepest unexpanded node Implementation:
fringe = LIFO queue, i.e., put successors at front

Depth-first search
Expand deepest unexpanded node Implementation:
fringe = LIFO queue, i.e., put successors at front

Depth-first search
Expand deepest unexpanded node Implementation:
fringe = LIFO queue, i.e., put successors at front

Depth-first search
Expand deepest unexpanded node Implementation:
fringe = LIFO queue, i.e., put successors at front

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

Iterative deepening search

Iterative deepening search l =0

Iterative deepening search l =1

Iterative deepening search l =2

Iterative deepening search l =3

Iterative deepening search


Number of nodes generated in a depth-limited search to depth d with branching factor b:

NDLS = b0 + b1 + b2 + + bd-2 + bd-1 + bd


Number of nodes generated in an iterative deepening search to depth d with branching factor b: NIDS = (d+1)b0 + d b^1 + (d-1)b^2 + + 3bd-2 +2bd-1 + 1bd For b = 10, d = 5,
NDLS = 1 + 10 + 100 + 1,000 + 10,000 + 100,000 = 111,111 NIDS = 6 + 50 + 400 + 3,000 + 20,000 + 100,000 = 123,456

Overhead = (123,456 - 111,111)/111,111 = 11%

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

Depth-limited search
= depth-first search with depth limit l, i.e., nodes at depth l have no successors Recursive implementation:

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)

Bi-directional Search
Intuition: Start searching from both the initial state and the goal state, meet in the middle.
Notes Not always possible to search backwards How do we know when the trees meet? At least one search tree must be retained in memory.

Complete? Yes Optimal? Yes Time Complexity: O(bd/2), where d is the depth of the solution. Space Complexity: O(bd/2), where d is the depth of the solution.

Summary of algorithms

Repeated states
Failure to detect repeated states can turn a linear problem into an exponential one!

Graph search

Searching with partial information


Sensorless problem (conformant)
Agent may have no idea where it is; solution is a sequence

Contingency problem
Occurs for partially observable environment percepts provide new information about current state often interleave search, execution

Exploration problem unknown states the agent must discover what state it is in and the actions to take

Example: vacuum world


Single-state, start in #5. Solution?

Example: vacuum world


Single-state, start in #5. Solution? [Right, Suck] Sensorless, start in {1,2,3,4,5,6,7,8} e.g., Right goes to {2,4,6,8} Solution?

Example: vacuum world


Sensorless, start in {1,2,3,4,5,6,7,8 } e.g., Right goes to {2,4,6,8 } Solution?

[Right, Suck, Left, Suck]


Contingency
Nondeterministic: Suck may dirty a clean carpet Partially observable: location, dirt at current location. Percept: [L, Clean], i.e., start in #5 or #7

Solution?

Example: vacuum world


Sensorless, start in {1,2,3,4,5,6,7,8} e.g., Right goes to {2,4,6,8} Solution?

[Right, Suck, Left, Suck]


Contingency
Nondeterministic: Suck may dirty a clean carpet Partially observable: location, dirt at current location. Percept: [L, Clean], i.e., start in #5 or #7

Solution? [Right, if dirt then Suck]

Summary
Problem formulation usually requires abstracting away real-world details to define a state space that can feasibly be explored Variety of uninformed search strategies Iterative deepening search uses only linear space and not much more time than other uninformed algorithms

You might also like