Lecture 23

advertisement
Lecture 23



Reminder: Homework 4 due Thursday,
Research Paper drafts due Thursday (either
hardcopy or PDF by email)
Example Prolog code will be on csserver in
/home/hwang/cs430/prolog/ after class.
Questions?
Tuesday, April 3
CS 430 Artificial Intelligence - Lecture 23
1
Outline


Prolog – gprolog

Structures

Lists

Arithmetic

Debugging

Backtracking and Cut (!)
Chapter 8 – First-Order Logic

Knowledge Engineering
Tuesday, April 3
CS 430 Artificial Intelligence - Lecture 23
2
Review: Prolog Basics

Identifier syntax convention is opposite FOL



Variables start with uppercase letters
Constants and predicate names start with
lowercase
Terms



Atoms, numbers, variables (including _ )
Structures: predicates and nested predicates,
names can be overloaded with different arity
Terms cannot have space between predicate and '('
Tuesday, April 3
CS 430 Artificial Intelligence - Lecture 23
3
Review: Prolog Basics

Operators

Predicates
exit interpreter
:-
left implication ()
halt
,
conjunction
read(x) input a term
;
disjunction
write(x) display arg
=
unification
nl
\=
non-unification
is
expression
evaluation &
assignment
listing list rules
& facts
Tuesday, April 3
CS 430 Artificial Intelligence - Lecture 23
display newline
4
Knowledge Bases


KB is a set of sentences.

<head> :- <term1>, <term2>, ..., <termn>.

May consist only of a head.

If no variables, sentence is called a fact.

Sentences with variables are called rules.
Often, functions are "recursive"


Base case must be listed first.
Recursive case is a rule of the same name that
uses the function on the right-hand side.
Tuesday, April 3
CS 430 Artificial Intelligence - Lecture 23
5
Structures

Structures are compound terms used to group
data. Examples:
owns(john, 'Wuthering Heights').
owns(john, book('Wuthering Heights', bronte)).
owns(john, book('Wuthering Heights',
author(bronte, emily))).
owns(john, book('Jane Eyre',
author(bronte, charlotte))).

Write a query that asks: "Does John own any
books by any author whose last name is
Bronte?"
Tuesday, April 3
CS 430 Artificial Intelligence - Lecture 23
6
Lists




Lists are enclosed in [ ] and separated by
commas. Empty list is [ ]. May be nested. E.g.,
[a, [b,c], d]
Built-in list functions like member(Elem,List)
and append(List1,List2,Result)
Manipulated by matching head (first element)
and tail (rest of elements) using [H|T] notation
Write function rules to answer queries:
"What is the last element of a list?"
"What is the length of a list?"
Tuesday, April 3
CS 430 Artificial Intelligence - Lecture 23
7
Arithmetic

Operators
X =:= Y
X =\= Y
X =< Y
<, >, >=
+, -, *, /
//
mod

X and Y stand for the same number
X and Y stand for different numbers
X less than or equal to
usual meanings
usual meanings
integer quotient
integer remainder
Write function rules for Minimum. Write
Population and Area facts, and a function to
compute Density
Tuesday, April 3
CS 430 Artificial Intelligence - Lecture 23
8
Debugging

Can turn on the debugger for tracing using



Default is single-stepping using command 'c'
(for creep). Command 'a' to abort run.
Turn off tracing using


?­ trace.
?­ notrace.
See gprolog manual for setting breakpoints,
etc.
Tuesday, April 3
CS 430 Artificial Intelligence - Lecture 23
9
Backtracking


When a goal fails (or user requests alternative
answers), Prolog backtracks up proof tree to
consider other possibilities for unification
Sometimes don't want this to happen




Know there's only one match
Know if get to a particular place in proof tree, query
will fail
Only want one result
Control backtracking with cut (!) operator
Tuesday, April 3
CS 430 Artificial Intelligence - Lecture 23
10
Cut (!)

Cut (!) operator is a built-in term

Effect of cut is:




Any variables that are bound to values at this point
cannot take on other values
No other versions of predicates called before the
cut will be considered
No other subsequent version of the predicate at the
head of the current rule will be considered
The cut always succeeds.
Tuesday, April 3
CS 430 Artificial Intelligence - Lecture 23
11
Prolog Problems

Compute letter grades


Compute sum of 1 to N


Use cut for efficiency
Use cut to produce one answer; prevents infinite
recursion when there is a request for backtracking.
Display instructions for Tower of Hanoi
Tuesday, April 3
CS 430 Artificial Intelligence - Lecture 23
12
Knowledge Engineering


General process for constructing KBs is
knowledge engineering. Knowledge engineer
investigates a particular domain, learns what
concepts are important, and creates formal
representations of objects and relations in the
domain.
Process is similar to other engineering efforts.
Thursday, March 29
CS 430 Artificial Intelligence - Lecture 22
13
Knowledge Engineering


Identify the task. What kinds of facts will be
available and what kinds of questions will be
supported.
Assemble the relevant knowledge. The KE
may be a domain expert or may need to extract
knowledge from experts – knowledge
acquisition.
Thursday, March 29
CS 430 Artificial Intelligence - Lecture 22
14
Knowledge Engineering


Decide on a vocabulary of predicates,
functions, and constants. I.e., translate
domain-level concepts into logic-level names.
Create an ontology of the domain.
Encode general knowledge about the
domain. Write axioms for all vocabulary terms.
Check for misconceptions or gaps in
vocabulary.
Thursday, March 29
CS 430 Artificial Intelligence - Lecture 22
15
Knowledge Engineering



Encode a description of the specific
problem instance. Should consist of writing
simple atomic sentences about instances of
concepts already part of the ontology.
Pose queries and get answers. Inference
procedure avoids application specific solution.
Debug the KB. If an axiom is missing or too
weak, KB may not be able produce an answer.
Incorrect axioms produce false statements:

 x NumOfLegs(x,4)  Mammal(x)
Thursday, March 29
CS 430 Artificial Intelligence - Lecture 22
16
Download