Title: CS G120 Artificial Intelligence
1CS G120 Artificial Intelligence
- Prof. C. Hafner
- Class Notes Feb 12, 2009
2Forward chaining algorithm
- forwardChain(KB, percept) returns updated KB
- new-knowledge percept
- while new-knowledge is not empty
- p ? pop(new-knowledge)
- add p to KB
- for each rule r in KB with p contained in
LHS(r) - if all elements of LHS(r) are in KB
- push (RHS(r), new-knowledge)
- return KB
- --------------------------------------------------
---------------- - Requires an index of the rule set by LHS symbols
3Feedback on programs
- C omments in code!!!!!!!!!
- Some incorrect answers
- Representation issues OO would be better
- Some ideas for making the code simpler, more
readable - KB.facts a list of the facts
- KB.rules a list of the rules
- Rule.lhs
- Rule.rhs
- Testing generally inadequate
- What should the test cases include?
4Example
- KB fruit ? edible
- vegetable ? edible
- vegetable green ? healthy
- edible healthy ? recommended
- apple ? fruit
- banana ? fruit
- spinach ? vegetable
- spinach ? green
- apple
- Correct answer for forwardChain(KB, spinach)
- Correct answer for makeExplicit(KB)
5Backward chaining algorithm
- SUBST(COMPOSE(?1, ?2), p) SUBST(?2, SUBST(?1,
p))
6Example knowledge base
- The law says that it is a crime for an American
to sell weapons to hostile nations. The country
Nono, an enemy of America, has some missiles, and
all of its missiles were sold to it by Colonel
West, who is American. - Prove that Col. West is a criminal
7Example knowledge base contd.
- ... it is a crime for an American to sell weapons
to hostile nations - American(x) ? Weapon(y) ? Sells(x,y,z) ?
Hostile(z) ? Criminal(x) - Nono has some missiles, i.e., ?x Owns(Nono,x) ?
Missile(x)
- Owns(Nono,M1) and Missile(M1)
- all of its missiles were sold to it by Colonel
West - Missile(x) ? Owns(Nono,x) ? Sells(West,x,Nono)
- Missiles are weapons
- Missile(x) ? Weapon(x)
- An enemy of America counts as "hostile
- Enemy(x,America) ? Hostile(x)
- West, who is American
- American(West)
- The country Nono, an enemy of America
- Enemy(Nono,America)
8Backward chaining example
9Backward chaining example
10Backward chaining example
11Backward chaining example
12Backward chaining example
13Backward chaining example
14Backward chaining example
15Backward chaining example
16Properties of backward chaining
- Depth-first recursive proof search space is
linear in size of proof - Incomplete due to infinite loops
- ? fix by checking current goal against every goal
on stack - Inefficient due to repeated subgoals (both
success and failure) - ? fix using caching of previous results (extra
space) - Widely used for logic programming
17Logic programming Prolog
- Algorithm Logic Control
- Basis backward chaining with Horn clauses
bells whistles - Widely used in Europe, Japan (basis of 5th
Generation project) - Compilation techniques ? 60 million LIPS
- Program set of clauses head - literal1,
literaln. - criminal(X) - american(X), weapon(Y),
sells(X,Y,Z), hostile(Z).
- Depth-first, left-to-right backward chaining
- Built-in predicates for arithmetic etc., e.g., X
is YZ3 - Built-in predicates that have side effects (e.g.,
input and output - predicates, assert/retract predicates)
- Closed-world assumption ("negation as failure")
- e.g., given alive(X) - not dead(X).
- alive(joe) succeeds if dead(joe) fails
18EXAMPLES
- likes(X, pizza) ? likes(X, spaghetti)
- drinks(X, beer) drinks(X, wine) ? likes(X,
pizza) - likes(X, Y) Knows(X, Z) ? Likes(Z, Y)
- likes(john, pizza)
- drinks(sally, beer)
- drinks(sam, beer)
- drinks(sam, wine)
- knows(mary, john)
- knows(john, tom)
- knows(steve, sue)
- Query who likes spaghetti?
- Add knows(W, V) ? Knows(V, W)
19Challenge create a representation for clauses
- Variables begin X, Y, or Z (upper case)
- Predicate and function symbols and constants
begin with lower case letters - Logical symbols are
- AND OR NOT IMPLIES EXISTS FORALL TRUE FALSE
- (no ??) is provided
- A python class called FOLexp whose constructor
takes an expression (a string) in this language
using () and returns an instance of the class
will be posted on blackboard. The class has two
components op and args where the args is a tuple
(NOT a list). The null symbol None will be
returned for bad input. - There will be a method for finding out if a
FOLexp is an atom or a tuple, and for finding out
if an FOLexp is a variable.
20Assignment 4b. Due Feb 19
- In one file
- Standardize (e) returns a FOLexp object with the
variables replaced with new variables with unique
names. - Unify(e1, e2) returns fail or a substitution,
which is a list of variable/term pairs (2 element
lists). - These programs must be accompanied GOOD test
datasets, and programs testStandardize() and
testUnify() that runs them.
21New topic Search
- Classic Search Problems
- Missionaries and Cannibals
- 8 puzzle
- 4 color map problem
- Path finding
- Resource allocation/scheduling
- Relationship to operations research
22The search framework for problem solving
- Given
- A set of discrete states (usually finite, but
often very large) that define the problem space - A designated start state
- A subset of designated goal states
- A set of operators or actions the agent can
perform at each state - A function Next State x Op ? State
- Objective find a sequence of operators that, if
applied beginning in the start state, will lead
to a goal state - Or find the shortest or least-cost sequence
23Explore the problems space by generating and
searching a tree
- Root start state
- Each node represents a state, children are all
the next-states
24Example The 8-puzzle
- states?
- actions?
- goal test?
- path cost?
25Example The 8-puzzle
- states? locations of tiles
- actions? move blank left, right, up, down
- goal test? goal state (given)
- path cost? 1 per move
- Note optimal solution of n-Puzzle family is
NP-hard
26Example Romania
27Example 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
28Tree search example
29Tree search example
30Tree search example
31Implementation general tree search
32Implementation 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.
33Search 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 8)
34Uninformed 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
35Breadth-first search
- Expand shallowest unexpanded node
- Implementation
- fringe is a FIFO queue, i.e., new successors go
at end
36Breadth-first search
- Expand shallowest unexpanded node
- Implementation
- fringe is a FIFO queue, i.e., new successors go
at end
37Breadth-first search
- Expand shallowest unexpanded node
- Implementation
- fringe is a FIFO queue, i.e., new successors go
at end
38Breadth-first search
- Expand shallowest unexpanded node
- Implementation
- fringe is a FIFO queue, i.e., new successors go
at end
39Properties of breadth-first search
- Complete? Yes (if b is finite)
- Time? 1bb2b3 bd b(bd-1) O(bd1)
- Space? O(bd1) (keeps every node in memory)
- Optimal? Yes (if cost 1 per step)
- Space is the bigger problem (more than time)
Hypothetical in text illustrating the problem of
an exponential complexity bound b (branching
factor) 10, a node uses 1000 bytes, and 10,000
nodes per second can be generated. A solution
at depth 8 takes 31 hours and uses 1 terabyte.
At level 9 it is 129 days and 101 terabytes.
40Uniform-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 e
- Time? of nodes with g cost of optimal
solution, O(bceiling(C/ e)) where C is the cost
of the optimal solution - Space? of nodes with g cost of optimal
solution, O(bceiling(C/ e)) - Optimal? Yes nodes expanded in increasing order
of g(n)
41Depth-first search
- Expand deepest unexpanded node
- Implementation
- fringe LIFO queue, i.e., put successors at
front
42Depth-first search
- Expand deepest unexpanded node
- Implementation
- fringe LIFO queue, i.e., put successors at
front
43Depth-first search
- Expand deepest unexpanded node
- Implementation
- fringe LIFO queue, i.e., put successors at
front
44Depth-first search
- Expand deepest unexpanded node
- Implementation
- fringe LIFO queue, i.e., put successors at
front
45Depth-first search
- Expand deepest unexpanded node
- Implementation
- fringe LIFO queue, i.e., put successors at
front
46Depth-first search
- Expand deepest unexpanded node
- Implementation
- fringe LIFO queue, i.e., put successors at
front
47Depth-first search
- Expand deepest unexpanded node
- Implementation
- fringe LIFO queue, i.e., put successors at
front
48Depth-first search
- Expand deepest unexpanded node
- Implementation
- fringe LIFO queue, i.e., put successors at
front
49Depth-first search
- Expand deepest unexpanded node
- Implementation
- fringe LIFO queue, i.e., put successors at
front
50Depth-first search
- Expand deepest unexpanded node
- Implementation
- fringe LIFO queue, i.e., put successors at
front
51Depth-first search
- Expand deepest unexpanded node
- Implementation
- fringe LIFO queue, i.e., put successors at
front
52Depth-first search
- Expand deepest unexpanded node
- Implementation
- fringe LIFO queue, i.e., put successors at
front
53Properties 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. (m is maximum depth) - Space? O(bm), i.e., linear space!
- Optimal? No
54Depth-limited search
- depth-first search with depth limit l,
- i.e., nodes at depth l have no successors
- Recursive implementation
55Iterative deepening search
56Iterative deepening search l 0
57Iterative deepening search l 1
58Iterative deepening search l 2
59Iterative deepening search l 3
60Iterative 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 (d1)b0 d b1 (d-1)b2 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
61Properties of iterative deepening search
- Complete? Yes
- Time? (d1)b0 d b1 (d-1)b2 bd O(bd)
- Space? O(bd)
- Optimal? Yes, if step cost 1
62Summary of algorithms
63Repeated states
- Failure to detect repeated states can turn a
linear problem into an exponential one!
64Graph search (Dynamic Programming)