Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

AI File - 03 PDF

Download as pdf or txt
Download as pdf or txt
You are on page 1of 43

CSE 359 (CEN 307) &

CSE 360 (CEN 308)


Artificial Intelligence
By
Md. Palash Uddin
Assistant Professor
Dept. of CSE
Hajee Mohammad Danesh Science and Technology
University, Dinajpur. 1
Intelligent Agent (Cont’d)
Building Goal-Based Agents
• We have a goal to reach
– Driving from point A to point B
– Put 8 queens on a chess board such that no one attacks another
– Prove that John is an ancestor of Mary
• We have information about where we are now at the beginning
• We have a set of actions we can take to move around (change from
where we are)
• Objective: find a sequence of legal actions which will bring us from
the start point to a goal

3
Initial Simplifying Assumptions (Abstraction)

• Environment is static
– no changes in environment while problem is being solved

• Environment is observable

• Environment and actions are discrete


– (typically assumed, but we will see some exceptions)

• Environment is deterministic

4
Problem-Solving Agents
• Intelligent agents can solve problems by searching a state-space
 State-space Model
– the agent’s model of the world
– usually a set of discrete states
– e.g., in driving, the states in the model could be towns/cities

 Goal State(s)
– a goal is defined as a desirable state for an agent
– there may be many states which satisfy the goal test
• e.g., drive to a town with a ski-resort
– or just one state which satisfies the goal
• e.g., drive to Dinajpur

 Operators (actions, successor function)


– operators are legal actions which the agent can take to move from one state to
another

5
Representing states
• State space:
– Includes the initial state S and all other states that are reachable from S by a
sequence of actions
– A state space can be organized as a graph:
nodes: states in the space
arcs: actions/operations
• The size of a problem is usually described in terms of the number
of states (or the size of the state space) that are possible.
– Tic-Tac-Toe has about 3^9 states.
– Checkers has about 10^40 states.
– Rubik's Cube has about 10^19 states.
– Chess has about 10^120 states in a typical game.
– GO has more states than Chess

6
The State-Space Graph
• Graphs:
– nodes, arcs, directed arcs, paths

• Search graphs:
– States are nodes
– operators are directed arcs
– solution is a path from start S to goal G

• Problem formulation:
– Give an abstract description of states, operators, initial state and goal state.

• Problem solving:
– Generate a part of the search space that contains a solution

7
State-Space Problem Formulation
A problem is defined by four items:
1. Initial state

2. Actions or successor function

3. Goal test (or set of goal states)

4. Path cost (additive)

A Solution is a sequence of actions leading from the initial


state to a goal state
Example: Traveling in Romania
• On holiday in Romania; currently in Arad
• Flight leaves tomorrow from Bucharest

• Formulate goal:
– be in Bucharest

• Formulate problem:
– states: various cities
– actions/operators: drive between cities

• Find solution
– By searching through states to find a goal
– sequence of cities, e.g., Arad, Sibiu, Fagaras, Bucharest

• Execute states that lead to a solution


9
Example: Traveling in Romania

10
State-Space Problem Formulation for Traveling in Romania
1. Initial state: Arad

2. Actions or successor function


S(x) = set of action–state pairs
e.g., S(Arad) = {<Arad  Zerind, Zerind>, … }

3. Goal test (or set of goal states)


x = "at Bucharest”, 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


(Arad) to the goal state (Bucharest)
The Traveling Salesperson Problem
• Given a road map of n cities, find the shortest tour which visits
every city on the map exactly once and then return to the original
C
city (Hamiltonian circuit)

Geometric Version: B
A D
– a complete graph of n vertices
F
– n!/2n legal tours

– Find one legal tour that is shortest


E
– State: sequence of cities visited

– S0 = A

12
Problem Formulation: The Traveling
Salesperson Problem
1. Initial state: A C

2. Actions or successor function:


B
S(x) = set of action–state pairs
e.g., S(A) = {<A B, C>, … } A D
F
3. Goal test (or set of goal states)
x = "at A”, Checkmate(x)
4. Path cost (additive) E
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
13
Example: 8-queens problem
 Place eight queens on a chessboard such that no queen attacks any other!
 Total # of states: 4.4x10^9
 Total # of solutions: 12 (or 96)

14
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 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
Example: Robot Assembly

• States

• Initial state

• Actions

• Goal test

• Path Cost

16
Example: Robot Assembly

• States: configuration of robot (angles, positions) and object parts

• Initial state: any configuration of robot and object parts

• Actions: continuous motion of robot joints

• Goal test: object assembled?

• Path Cost: time-taken or number of actions 17


Learning a spam email classifier
States: settings of the parameters in our model

Initial state: random parameter settings

Actions: moving in parameter space

Goal test: optimal accuracy on the training data

Path Cost: time taken to find optimal parameters

This is an optimization problem – many machine learning problems


can be cast as optimization.
18
Example: 8-puzzle
Given an initial configuration of 8 numbered tiles on a 3 x 3
board, move the tiles in such a way so as to produce a
desired goal configuration of the tiles.

• states? locations of tiles

• initial state? given

• actions? move blank left, right, up, down

• goal test? goal state (given)

• path cost? 1 per move 19


8 puzzle
• State: 3 x 3 array configuration of the tiles on the board.
• Operators: Move Blank square Left, Right, Up or Down.
– This is a more efficient encoding of the operators than one in
which each of four possible moves for each of the 8 distinct tiles
is used.
• Initial State: A particular configuration of the board.
• Goal: A particular configuration of the board.
• The state space is partitioned into two subspaces
• NP-complete problem, requiring O(2^k) steps where k is the length of the
solution path.
• 15-puzzle problems (4 x 4 grid with 15 numbered tiles),
and N-puzzles (N = n^2-1)

20
A portion of the state space of a 8-
Puzzle problem
5 4
6 1 8
7 3 2

5 4 5 4 8
6 1 8 6 1
7 3 2 7 3 2

5 1 4 5 4
6 8 6 1 8
7 3 2 7 3 2

5 1 4
6 8
7 3 2
21
Cryptarithmetic puzzles
TWO
TWO+
FOUR
E.g., setting F = 1, O = 4, R = 8, T = 7, W = 3, U = 6
gives 734+734=1468

22
Cryptarithmetic puzzles…
TWO Trick: introduce auxiliary variables
TWO+ X, Y
FOUR O + O = 10X + R
W + W + X = 10Y + U
T + T + Y = 10F + O

O R W U T F

also need pairwise constraints between


original variables if they are supposed to
be different]
X Y
23
A Water Jug Problem
• You have a 4-
gallon and a 3-
gallon water jug

• You have a
faucet with an
unlimited
amount of water

• You need to get


exactly 2 gallons
in 4-gallon jug
24
Water Jug 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 faucet, fill 4-gallon from faucet
– 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

25
Production Rules for the Water Jug Problem
1 (x,y)  (4,y) Fill the 4-gallon jug
if x < 4

2 (x,y)  (x,3) Fill the 3-gallon jug


if y < 3

3 (x,y)  (x – d,y) Pour some water out of the 4-gallon jug


if x > 0

4 (x,y)  (x,y – d) Pour some water out of the 3-gallon jug


if x > 0

5 (x,y)  (0,y) Empty the 4-gallon jug on the ground


if x > 0

6 (x,y)  (x,0) Empty the 3-gallon jug on the ground


if y > 0

7 (x,y)  (4,y – (4 – x)) Pour water from the 3-gallon jug into the 4-gallon jug
if x + y ≥ 4 and y > 0 until the 4-gallon jug is full

26
The Water Jug Problem (cont’d)
8 (x,y)  (x – (3 – y),3) Pour water from the 4-gallon jug into the 3-gallon jug
if x + y ≥ 3 and x > 0 until the 3-gallon jug is full

9 (x,y)  (x + y, 0) Pour all the water from the 3-gallon jug into the 4-gallon
if x + y ≤ 4 and y > 0 jug

10 (x,y)  (0, x + y) Pour all the water from the 4-gallon jug into the 3-gallon
if x + y ≤ 3 and x > 0 jug

27
One Solution to the Water Jug
Problem
Gallons in the 4- Gallons in the 3- Rule Applied
Gallon Jug Gallon Jug

0 0 2

0 3 9

3 0 2

3 3 7

4 2 5

0 2 9

2 0

28
Missionaries and Cannibals
There are 3 missionaries, 3 cannibals,
and 1 boat that can carry up to two
people on one side of a river.
• Goal: Move all the missionaries and
cannibals across the river.
• Constraint: Missionaries can never
be outnumbered by cannibals on either
side of river, or else the missionaries
are killed. Outnumber
• State: configuration of missionaries
and cannibals and boat on each side of
river.
• Operators: Move boat containing
some set of occupants across the river
(in either direction) to the other side.
29
Missionaries and Cannibals Solution

Near side
Far side
0 Initial setup: MMMCCC B
-
1 Two cannibals cross over: MMMC
B CC
2 One comes back: MMMCC B
C
3 Two cannibals go over again: MMM
B CCC
4 One comes back: MMMC B
CC
5 Two missionaries cross: MC
B MMCC
6 A missionary & cannibal return: MMCC B
MC 30
Recape: Formalizing Search in a State Space
• A state space is a graph, (V, E) where V is a set of nodes and E is a set of arcs, where
each arc is directed from a node to another node
• node: corresponds to a state
– state description
– plus optionally other information related to the parent of the
node, operation to generate the node from that parent, and
other bookkeeping data)
• arc: corresponds to an applicable action/operation.
– the source and destination nodes are called as parent
(immediate predecessor) and child (immediate successor)
nodes with respect to each other
– ancestors( (predecessors) and descendents (successors)
– each arc has a fixed, non-negative cost associated with it,
corresponding to the cost of the action

31
Tree-based Search
• Basic idea:
– Exploration of state space by generating successors of already-
explored states (a.k.a. expanding states).
– Every state is evaluated: is it a goal state?
• In practice, the solution space can be a graph,
not a tree
– E.g., 8-puzzle
– More general approach is graph search
– Tree search can end up repeatedly visiting the same nodes
• Unless it keeps track of all nodes visited
• …but this could take vast amounts of memory

32
Tree search example

33
Tree search example

34
Tree search example

35
Tree search example

This “strategy” is
what differentiates
different search
algorithms

36
States versus Nodes
• A state is a (representation of) a physical configuration

• A node is a data structure constituting part of a search tree contains info such as: 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.
State Spaces versus Search Trees
• State Space
– Set of valid states for a problem
– Linked by operators
– e.g., 20 valid states (cities) in the Romanian travel problem
• Search Tree
– Root node = initial state
– Child nodes = states that can be visited from parent
– Note that the depth of the tree can be infinite
• E.g., via repeated states
– Partial search tree
• Portion of tree that has been expanded so far
– Fringe
• Leaves of partial search tree, candidates for expansion

Search trees = data structure to search state-space


38
Search Tree for the 8 puzzle problem

39
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 and 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 ∞)

40
Why Search can be hard
Assuming b=10, 1000 nodes/sec, 100 bytes/node

Depth of Nodes to
Solution Expand Time Memory

0 1 1 millisecond 100 bytes

2 111 0.1 seconds 11 kbytes

4 11,111 11 seconds 1 megabyte

8 108 31 hours 11 giabytes

12 1012 35 years 111 terabytes

41
Next Topics
• Uninformed search
– Breadth-first, depth-first
– Uniform cost
– Iterative deepening

• Informed (heuristic) search


– Greedy best-first
– A*
– Memory-bounded heuristic search
– And more….

• Local search and optimization


– Hill-climbing
– Simulated annealing
42
– Genetic algorithms
Summary
• Problem-solving agents where search consists of
– state space
– operators
– start state
– goal states

• Abstraction and problem formulation

• Search trees: an effective way to represent the search process

43

You might also like