Module1_3

74
Artificial Intelligence Artificial Intelligence Artificial Intelligence Artificial Intelligence Module Module – 1 Part Part - 3

Transcript of Module1_3

Page 1: Module1_3

Artificial IntelligenceArtificial IntelligenceArtificial IntelligenceArtificial Intelligence

Module Module –– 11

Part Part -- 33

Page 2: Module1_3

Solving problems by searching

Page 3: Module1_3

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:

Page 4: Module1_3

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

Page 5: Module1_3

A model based 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

It keeps track of the current states using an internal

model, it chooses an action as a reflex agent

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.

Page 6: Module1_3

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

Page 7: Module1_3

Problem-solving agents

Page 8: Module1_3

Example: Romania

� On holiday in Romania; currently in Arad.

� Flight leaves tomorrow from Bucharest

� Formulate goal:� be in Bucharest

� Formulate problem:� states: various cities� states: various cities

� actions: drive between cities

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

Page 9: Module1_3

Example: RomaniaSource

Destination

Page 10: Module1_3

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 contingency � Nondeterministic and/or partially observable � contingency problem� percepts provide new information about current state

� often interleave search, execution

� Unknown state space � exploration problem

Page 11: Module1_3

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 action–state pairs � e.g., S(Arad) = {<Arad � Zerind, Zerind>, … }

3. goal test, can be3. 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

Page 12: Module1_3

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

Page 13: Module1_3

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?gallons into the 4 gallon bucket?

– There are no markings on the bucket

– You must fill each bucket completely

Page 14: Module1_3

Example: Water Pouring

• State: (x, y)

x = 0, 1, 2, 3, or 4 y = 0, 1, 2, 3

• Initial state:

14

• Initial state:

The buckets are empty, represented by ( 0 0 )

• 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

Page 15: Module1_3

Example: Water Pouring

• Actions and Successor Function– Fill a bucket

• (x y) -> (3 y)

• (x y) -> (x 4)

– Empty a bucket

15

– 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)

Page 16: Module1_3

Example: Water Pouring

Page 17: Module1_3

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)• 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

Page 18: Module1_3

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

Page 19: Module1_3

Production rules for Water Jug ProblemThe operators to be used to solve the problem can be described as follows:

Sl No Current state Next State Descripion

1 (x,y) if x < 4 (4,y) Fill the 4 gallon jug

2 (x,y) if y <3 (x,3) Fill the 3 gallon jug

3 (x,y) if x > 0 (x-d, y) Pour some water out of the 4 gallon jug

4 (x,y) if y > 0 (x, y-d) Pour some water out of the 3-gallon jug

5 (x,y) if x>0 (0, y) Empty the 4 gallon jug

6 (x,y) if y >0 (x,0) Empty the 3 gallon jug on the ground

7 (x,y) if x+y >= 4 and y >0

(4, y-(4-x)) Pour water from the 3 –gallon jug into the 4 –gallon jug until the 4-gallon jug is full

Page 20: Module1_3

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

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

(x+y, 0) Pour all the water from the 3-gallon jug into the 4-gallon jug

Production rules for Water Jug Problem

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

(0, x+y) Pour all the water from the 4-gallon jug into the 3-gallon jug

11 (0,2) (2,0) Pour the 2 gallons from 3-gallon jug into the 4-gallon jug

12 (2,y) (0,y) Empty the 2 gallons in the 4-gallon jug on the ground

Page 21: Module1_3

One Solution to the Water Jug Problem

Gallons in the 4-Gallon Jug

Gallons in the 3-Gallon Jug

Rule Applied

0 0 2

0 3 9

3 0 23 0 2

3 3 7

4 2 5 or 12

0 2 9 or 11

2 0

Page 22: Module1_3

Vacuum world state space graph

� states?� actions?� goal test?

� path cost?

Page 23: Module1_3

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

Page 24: Module1_3

Example: The 8-puzzle

� states?

� actions?

� goal test?

� path cost?

Page 25: Module1_3

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

Page 26: Module1_3

Example: 8-queens problem

Page 27: Module1_3

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• actions? -add queen to any empty square

-or add queen to leftmost empty square such that it is not attacked by other

queens.

• goal test? 8 queens on the board, none attacked.

• path cost? 1 per move

Page 28: Module1_3

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

Page 29: Module1_3

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

Page 30: Module1_3

Tree search example

Page 31: Module1_3

Tree search example

Page 32: Module1_3

Tree search example

Page 33: Module1_3

Implementation: general tree search

Page 34: Module1_3

Search Tree for the 8 puzzle problem

Page 35: Module1_3

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.

Page 36: Module1_3

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� 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 ∞)

Page 37: Module1_3

Uninformed search strategies

� Uninformed search strategies use only the information available in the problem definition

� Breadth-first search

� Uniform-cost search� Uniform-cost search

�Depth-first search

�Depth-limited search

� Iterative deepening search

� Bidirectional search

Page 38: Module1_3

Breadth-first search

� Expand shallowest unexpanded node

� Implementation:

� fringe is a FIFO queue, i.e., new successors go at end

Page 39: Module1_3

Breadth-first search

� Expand shallowest unexpanded node

� Implementation:

� fringe is a FIFO queue, i.e., new successors go at end

Page 40: Module1_3

Breadth-first search

� Expand shallowest unexpanded node

� Implementation:

� fringe is a FIFO queue, i.e., new successors go at end

Page 41: Module1_3

Breadth-first search

� Expand shallowest unexpanded node

� Implementation:

� fringe is a FIFO queue, i.e., new successors go at end

Page 42: Module1_3

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)

Page 43: Module1_3

Depth-first search

� Expand deepest unexpanded node

� Implementation:

� fringe = LIFO queue, i.e., put successors at front

Page 44: Module1_3

Depth-first search

� Expand deepest unexpanded node

� Implementation:

� fringe = LIFO queue, i.e., put successors at front

Page 45: Module1_3

Depth-first search

� Expand deepest unexpanded node

� Implementation:

� fringe = LIFO queue, i.e., put successors at front

Page 46: Module1_3

Depth-first search

� Expand deepest unexpanded node

� Implementation:

� fringe = LIFO queue, i.e., put successors at front

Page 47: Module1_3

Depth-first search

� Expand deepest unexpanded node

� Implementation:

� fringe = LIFO queue, i.e., put successors at front

Page 48: Module1_3

Depth-first search

� Expand deepest unexpanded node

� Implementation:

� fringe = LIFO queue, i.e., put successors at front

Page 49: Module1_3

Depth-first search

� Expand deepest unexpanded node

� Implementation:

� fringe = LIFO queue, i.e., put successors at front

Page 50: Module1_3

Depth-first search

� Expand deepest unexpanded node

� Implementation:

� fringe = LIFO queue, i.e., put successors at front

Page 51: Module1_3

Depth-first search

� Expand deepest unexpanded node

� Implementation:

� fringe = LIFO queue, i.e., put successors at front

Page 52: Module1_3

Depth-first search

� Expand deepest unexpanded node

� Implementation:

� fringe = LIFO queue, i.e., put successors at front

Page 53: Module1_3

Depth-first search

� Expand deepest unexpanded node

� Implementation:

� fringe = LIFO queue, i.e., put successors at front

Page 54: Module1_3

Depth-first search

� Expand deepest unexpanded node

� Implementation:

� fringe = LIFO queue, i.e., put successors at front

Page 55: Module1_3

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

Page 56: Module1_3

Iterative deepening search

Page 57: Module1_3

Iterative deepening search l =0

Page 58: Module1_3

Iterative deepening search l =1

Page 59: Module1_3

Iterative deepening search l =2

Page 60: Module1_3

Iterative deepening search l =3

Page 61: Module1_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: 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%

Page 62: Module1_3

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�Optimal? Yes, if step cost = 1

Page 63: Module1_3

Depth-limited search

= depth-first search with depth limit l,

i.e., nodes at depth l have no successors

� Recursive implementation:

Page 64: Module1_3

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 ≥ ε� 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)

Page 65: Module1_3

Bi-directional SearchIntuition: 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?

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

trees meet?

• At least one search tree must

be retained in memory.

Page 66: Module1_3

Summary of algorithms

Page 67: Module1_3

Repeated states

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

Page 68: Module1_3

Graph search

Page 69: Module1_3

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� often interleave search, execution

� Exploration problem

� unknown states

� the agent must discover what state it is in and the actions to take

Page 70: Module1_3

Example: vacuum world

� Single-state, start in #5. Solution?

Page 71: Module1_3

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} Right goes to {2,4,6,8} Solution?

Page 72: Module1_3

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?

Page 73: Module1_3

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]

Page 74: Module1_3

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