Notes for CS3310 Artificial Intelligence Part 3: Logical queries to a database Prof' Neil C' Rowe Na - PowerPoint PPT Presentation

1 / 45
About This Presentation
Title:

Notes for CS3310 Artificial Intelligence Part 3: Logical queries to a database Prof' Neil C' Rowe Na

Description:

Interpreters run programs without compiling them, working directly on the source ... won't play, or you can't start the car and the radio plays but it plays faintly. ... – PowerPoint PPT presentation

Number of Views:105
Avg rating:3.0/5.0
Slides: 46
Provided by: facul52
Category:

less

Transcript and Presenter's Notes

Title: Notes for CS3310 Artificial Intelligence Part 3: Logical queries to a database Prof' Neil C' Rowe Na


1
Notes for CS3310 Artificial IntelligencePart 3
Logical queries to a databaseProf. Neil C.
Rowe Naval Postgraduate School Version of
January 2006
2
Full interpreters for programming languages
  • Interpreters run programs without compiling them,
    working directly on the source code. (Java has a
    bytecode "interpreter", but that's only a halfway
    interpreter.)
  • Example languages with full interpreters as well
    as compilers
  • Prolog (built around logic expressions)
  • Lisp (linked lists)
  • Matlab (arrays)
  • Mathematica (algebra like axbyc)
  • Python

3
Advantages and disadvantages of full interpreters
  • Faster programming, good for prototyping
  • Shorter programs
  • Easier debugging
  • But programs slower because less optimization
    done
  • And incompatibilities possible with other
    languages

4
Running a Prolog interpreter on Windows machines
  • You can install it on your home computer. Get an
    open-source interpreter. We recommend Gnu Prolog
    from http//gnu-prolog.inria.fr. Unzip this at
    the top level of the C folder.
  • A Gnu Prolog interepreter is also installed on
    many of the general computer-science laboratory
    machines.
  • Use a text editor to create a file of your facts
    and (possibly) rules. If C drive unavailable,
    store on H drive.
  • Start Prolog by clicking on the icon created
    during installation. A black window should
    appear, and a ?- should appear inside it.

5
Running an interpreter, cont.
  • Do change_directory with argument the name of the
    directory containing your files enclosed in
    apostrophes, e.g. change_directory('c/prolog/prog
    s3310'). (note the period). You must use "/"
    rather than "\" to indicate subdirectories.
  • Load your file of facts and rules by doing
    'filename. (note the period) where filename is
    the name of your file.
  • Once your file is loaded, you can query any of
    the facts and rules in it by the methods
    discussed in class.
  • When done with Prolog, type halt. (note the
    period).
  • For a user's manual, click on "Help". Prolog
    dialects do not differ very much.
  • To preserve a record of your run, push
    printscreen and paste, or use a scripting
    facility.

6
Usual operation of Prolog software
User
runs
edits
  • Source code in
  • the Prolog language

consult
Prolog interpreter
queries
answers
Prolog database
loads
7
An example Prolog database
  • incumbent(csprofessor, baer).
  • incumbent(csprofessor, berzins).
  • incumbent(csprofessor, rowe).
  • incumbent(cschairman,boger).
  • incumbent(dean05,boger).
  • incumbent(dean06, panholzer).
  • incumbent(provost, elster).
  • incumbent(director_milops, bley).
  • incumbent(superintendent, chapin).
  • bossed_by(csprofessor, cschairman).
  • bossed_by(cschairman, dean05).
  • bossed_by(dean05, provost).
  • bossed_by(dean06, provost).
  • bossed_by(provost, superintendent).
  • bossed_by(director_milops, superintendent).

8
Basic use of a Prolog interpreter
  • The interpreter expects the user to type in a
    question, a "query (like database query systems
    and Web search engines). It types ?- when ready.
  • The interpreter then tries to find a matching
    fact in its database. The database consists of
    all the files you have loaded with a consult.
  • A query answer is either "no", "yes", or a set of
    variable bindings. For instance
  • ?- bossed_by(csprofessor,cschairman).
  • yes.

9
Prolog variables
  • Variables are any word starting with a capital
    letter.
  • So constants like tom and nps cannot be
    capitalized.
  • Example X and Y in father_of(X,Y) this says
    some arbitrary X is the father of some arbitrary
    Y.
  • Variables can get bound in query matching (e.g.,
    Xtom_jones and Ydick_deadeye in answering the
    query ?- father_of(X,Y).).

10
Simple example queries
  • ?-incumbent(csprofessor, X).
  • Xbaer
  • ?- incumbent(J,rowe).
  • Jcsprofessor
  • no.
  • (A semicolon typed by the user means "Try to find
    another answer" a carriage return means to
    stop.).
  • ?- incumbent(X,P).
  • Xcsprofessor, Pbaer
  • Xcsprofessor, Pberzins
  • ?- incumbent(csprofessor, rowe).
  • yes
  • ?-

11
Conjunctive (anded) queries
  • You can "and" together (take the conjunction of)
    several predicate expressions by putting commas
    between them in a query.
  • ?- integer(X), less_than(0,X), less_than(X,10).
  • This asks for an integer X that is greater than 0
    and less than 10.
  • Predicate expressions are checked initially from
    left to right facts are searched from top to
    bottom in the database.

12
Disjunctive (ored) queries
  • You can also "or" expressions with a semicolon
  • ?- left_of(X,aircraft461) left_of(aircraft461,X).
  • The leftmost expression is matched to the
    database if it fails, the next expression to the
    right is matched.

13
Negations in queries
  • You can also negate expressions
  • ?- a_kind_of(X,destroyer), property(X,nuclear),
    \ owns(X, us).
  • This checks whether some country besides the U.S.
    owns a nuclear destroyer.
  • If a negated expression finds a match to the
    database, it fails otherwise it succeeds.
  • Negated expressions cannot bind variables.

14
Composite query examples
  • ?- bossed_by(csprofessor,X), bossed_by(X,Y).
  • ?- bossed_by(X,Y), incumbent(X,rowe),
    incumbent(Y,Z).
  • ?- incumbent(dean05,X) incumbent(dean_05,X).
  • ?- (bossed_by(J,provost) bossed_by(provost,J)),
    incumbent(J,P).
  • ?- bossed_by(J,provost), \ incumbent(J,boger).
  • (Some dialects of Prolog use not(incumbent(P,boger
    )))

15
Database for queries example
  • incumbent(csprofessor,baer).
  • incumbent(csprofessor,berzins).
  • incumbent(csprofessor,rowe).
  • incumbent(cschairman,boger).
  • incumbent(dean05,boger).
  • incumbent(dean06,panholzer).
  • incumbent(provost,elster).
  • incumbent(director_milops,bley).
  • incumbent(superintendent,chapin).
  • bossed_by(csprofessor,cschairman).
  • bossed_by(cschairman,dean05).
  • bossed_by(dean05,provost).
  • bossed_by(dean06,provost).
  • bossed_by(provost,superintendent).
  • bossed_by(director_milops,superintendent).

16
Examples of matching (unification) for queries
  • Assuming no variables have been previously bound
  • p(3) in query matches p(3) in database
  • p(X) in query matches p(3) in database
  • p(3) in query matches p(X) in database
  • p(X) in query matches p(Y) in database
  • p(3,4) in query matches p(3,4) in database
  • p(X,4) in query matches p(3,4) in database

17
  • p(3,X) in query matches p(3,4) in database
  • p(X,Y) in query matches p(3,4) in database
  • p(A4,B3) in query matches p(3,4) in database
  • p(4) in query doesn't match p(3) in database
  • p(X,5) in query doesn't match p(3,4) in database
  • p(X,X) in query doesn't match p(3,4) in database

18
Logic symbols of predicate calculus, Prolog, and
Java
19
Equivalences of propositional calculus
  • Unduplication p?p ? p p?p ? p
  • Tautology p?p ? true
  • Contradiction p ? p ? false
  • Constants p?true ? p, p?false ? false,
    p?true ? true, p?false ? p
  • Commutativity p?q ? q?p, p?q ? q?p
  • Associativity (p?q)?r ? p?(q?r),
    (p?q)?r ? p?(q?r)
  • Distributivity (p?q)?r ? (p?r)?(q?r),
    (p?q)?r ? (p?r)?(q?r)

(p, q, and r represent any logical expression)
20
Equivalences of propositional calculus (2)
  • Double negation p ? p
  • DeMorgans Laws (p ? q) ? p ? q,
    (p ? q) ? p ? q
  • Implication disjunctive (p ? q) ? (p ? q)
  • (hence "p is true" is written as p, not
    true?p)
  • Contrapositive (p ? q) ? ( q ? p)
  • Absorption ((p?q) ? p) ? p,
    ((p?q) ? p) ? p
  • Negative absorption ((p?q) ? p) ? (q?p),
    ((p?q) ? p) ? (q?p)

21
Proof methods of propositional calculus
  • Modus ponens If (p ? q) and q is true, conclude
    p.
  • Modus tollens If (p ? q) and p is false,
    conclude q is false.
  • Adjunction If p is true, and q is true, then
    conclude (p ? q).
  • Conjunctive simplification If (p ? q), then
    conclude p is true.
  • Disjunctive addition If p is true, then conclude
    (p ? q).

22
Proof methods of propositional calculus (2)
  • Transitivity of implication If (p ? q) and (q ?
    r), then conclude (p ? r).
  • Proof by contradiction If when you assume p you
    can prove false, conclude p.
  • Resolution If (p ? q) and ( p ? r), conclude
    (q ? r).

23
Predicate calculus
  • Its just propositional calculus plus variables
    and quantifiers.
  • Every variable in predicate calculus must be
    quantified, either universally or existentially.
  • But in Prolog, quantifiers are not used and are
    implicit.

24
Additional equivalences of predicate calculus
  • Quantifier negation
  • ?X(p(X)) ? ?X( p(X)),
  • ?X(p(X)) ? ?X( p(X))
  • Universal scope change
    ?X(p(X) ? q(X)) ?
    (?Xp(X) ? ?X(q(X))
  • Quantifier interchange
    ?X?Y(p(X,Y)) ? ?Y?X(p(X,Y)), ?X?Y(p(X,Y)) ?
    ?Y?X(p(X,Y))
  • Quantifier elimination
    ?X(p) ? p, ?X(p) ? p

(p represents any predicate name X and Y
represent any variable)
25
Additional proof methods of predicate calculus
  • Universal instantiation If ?X(p(X)) then
    conclude p(a) is true.
  • Existential generalization If p(a) is true,
    conclude ?X(p(X)).
  • Quantified modus ponens If ?X(q(X)) and ?X(p(X)
    ? q(X)), conclude ?X(p(X)).

26
Rules for propositional calculus expressions
  • A propositional calculus expression must be
    either
  • a predicate expression without variables (but it
    may have constants as arguments)
  • or a propositional calculus expression without
    variables with a symbol in front of it
  • or two propositional calculus expressions with
    one of the symbols ? , ? , or ? between them
  • or a parenthesized propositional calculus
    expression (parentheses are mandatory when the
    expression is ambiguous otherwise)
  • These are the only symbols allowed.

27
Rules for predicate calculus expressions
  • A predicate calculus expression is like a
    propositional calculus expression except
    predicate expressions can contain variables.
  • Whenever an expression contains a variable, the
    variable must be quantified either by ? or ?
    followed immediately by the name of the variable,
    placed somewhere before the expression, and with
    brackets or parentheses indicating the scope of
    the quantification.

28
Proofs in predicate calculus
  • A series of steps starting with givens and
    ending with the statement to be proved.
  • Each line must be justified with a reason, which
    must be one of the equivalences or proof methods
    of predicate calculus (including those of
    propositional calculus).
  • Number the steps so each line can explain which
    steps it depends on.

29
Example proof 1 in predicate calculus
  • Prove given p.
  • p (given)
  • (disjunctive addition on 1)
  • (disjunctive addition on
    2)
  • (a DeMorgans Law on
    3)
  • (disjunction
    equivalent of an implication on 4)

30
Example proof 2 in predicate calculus
  • Given fact p(1,2), statement
    , and statement
    , prove r(2,2).
  • p(1,2) (given)
  • (given)
  • q(1,3) (resolution of 1 and 2)
  • (given)
  • r(2,2) (modus ponens on 3 and 4)

31
Example proof 3 in predicate calculus
  • Prove given
    and

  • .
  • (given)

  • (given)

  • (distributive law on 2)
  • (conjunctive simplification on 3)
  • (universal instantiation on 1)
  • (modus tollens on 4 and 5)

32
From English to queries
  • Again, look for groups of words that together
    correspond to a predicate expression.
  • The special words ("and", "or", "if", "not",
    etc.) will help break the English into pieces.
  • "What", "who", "which", etc. correspond to
    variables.
  • Example "What American civilians are bossed by
    and younger than Prof. Boger? Representation
  • ?- nationality(X,american), a_kind_of(X,civilian),
    bossed_by(X,prof_boger), younger_than(X,prof_bog
    er).

33
Query simplification problem (1)
  • Check whether either you can't start the car and
    you left the lights on all night, or you can't
    start the car and the radio won't play, or you
    can't start the car and the radio plays but it
    plays faintly.
  • Write this as best you can as an efficient Prolog
    query using only 4 predicate expressions without
    variables.
  • Assume a database of appropriate facts.

34
Query simplification problem (2)
  • Find an X such that X is a civilian employee and
    has a Ph.D., or if X is a military employee and
    has a Ph.D., or if X is not a military employee
    and does not have a Ph.D. and is called an
    "adjunct".
  • Write this as best you can as an efficient Prolog
    query using only 4 predicate expressions and one
    variable X.
  • Assume a database of appropriate facts.

35
Automatic backtracking in Prolog
  • When an expression in an "and" fails, the Prolog
    interpreter returns to the previous expression
    (if any) and tries to find a new way to satisfy
    it.
  • Example
  • ? - a(X,Y), b(Y,Z), \ c(Z), \ d(Z).
  • If b(Y,Z) fails, backtrack to a(X,Y) and try to
    find new pair of X and Y otherwise fail. A
    negation always fails when backtracked to.
  • To implement backtracking The interpreter keeps
    a database pointer for every expression. These
    are pushed on and popped from a stack.

36
Backtracking example
  • ?- a(X,Y), b(X,Y), a(Y,Y).
  • with database
  • a(1,1).
  • a(2,1).
  • a(3,2).
  • a(4,4).
  • b(1,2).
  • b(1,3).
  • b(2,3).
  • b(3,2).
  • b(4,4).

37
Another backtracking example
  • Query
  • ?- a(X,Y), a(X,Z), \ a(Y,X), a(Y,Z).
  • Database
  • a(1,1).
  • a(3,4).
  • a(3,1).
  • a(1,4).

38
Ands and ors in Java
  • public static andc1c2a
  • (boolean c1, boolean c2)
  • return (c1 and c2)
  • public static andc1c2b
  • (boolean c1, boolean c2)
  • boolean flag true
  • if (c1)
  • then flag false
  • if (c2)
  • then flag false
  • return flag
  • public static orc1c2a
  • (boolean c1, boolean c2)
  • return (c1 or c2)
  • public static orc1c2b
  • (boolean c1, boolean c2)
  • boolean flag false
  • if (c1)
  • then flag true
  • if (c2)
  • then flag true
  • return flag

39
Be careful with negated unbound variables
  • Consider
  • ?- a(X), \ b(X).
  • ?- \ b(X), a(X).
  • The second query won't work right unlike the
    first query, it won't succeed with the database
  • a(1).
  • b(2).
  • The second query succeeds only if there are no
    "b" facts. Since X can't be bound inside the
    "not", the X inside the second "not" could as
    well be a Y.

40
The logic of unbound variables
  • So "and" is not commutative in Prolog when an
    expression anded has a "not" with unbound
    variables. So be careful with such expressions.
  • Logic explanation variables in Prolog queries
    are existentially quantified. The scope of the
    quantification is the entire query, except when
    inside a "not", when the scope is the inside of
    the "not". So the two queries above appear in
    predicate calculus as
  • ?X(a(X) ? b(X))
  • ?X(b(X)) ? ?X(a(X))

41
Generate-and-test versus case-based reasoning
  • So far, queries are considered as
    "generate-and-test" a value is generated for a
    variable the first time it is mentioned, and then
    that value is tested at subsequent mentions of
    the variable. If the tests fail, processing
    backtracks and generates another value.
  • An alternative is "case-based" reasoning or
    "satisficing". Sets of solution values are
    found, then ranked as to how well they pass all
    the tests.

42
Finding the best-matching case
  • The ranking can be based on how many variables
    match and how close the misses are.
  • With numeric arguments, the match closeness can
    be the absolute value of difference in numbers.
  • With nonnumeric arguments, the match closeness
    can be the number of "a_kind_of" links that must
    be followed to get from one value to the other.
  • Each variable can have a weight. The
    best-matching fact gives the first answer, the
    second-best the second answer, etc.

43
Example data for case-based reasoning
  • Consider ship observation facts (1) observed
    length, (2) observed ship type, (3) observed
    radar, and (4) ship name.
  • observed(50, fishing_vessel, none, totor).
  • observed(150, destroyer, radar, pequod).
  • observed(100, minesweeper, radar, nelson).
  • observed(700, carrier, radar, enterprise).
  • observed(200, cruise_ship, none, bali_hai).

44
Example run of case-based reasoning
  • Now if we query
  • observed(140, destroyer, none, X).
  • Case-based reasoning would likely find that the
    second fact is the best match and Xpequod.
  • If we query
  • observed(110, patrol, radar, X).
  • Case-based reasoning would likely find that the
    third fact is the best match and Xnelson.

45
The best case using the inner product
  • Application Find the Web page whose words are
    most similar to the words of an example page.
  • Compute s(j) ?(fij ei) / (??(fij)??(ei))
  • where s(j) is the similarity of example e to
    casej, fij is count of word i in case j and ei is
    count of word i in the test example, and ? means
    summation over all words i.
  • Example Suppose your example page mentions
    "Sidewinder" and "F-18 a lot. You get high
    similarity for pages that mention those words a
    lot, and zero value for pages that do not mention
    the words at all.
Write a Comment
User Comments (0)
About PowerShow.com