notes for cs3310 artificial intelligence part 3: logical queries to a database prof. neil c. rowe...

44
Notes for CS3310 Artificial Intelligence Part 3: Logical queries to a database Prof. Neil C. Rowe Naval Postgraduate School Version of July 2009

Upload: jocelin-wade

Post on 28-Dec-2015

215 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Notes for CS3310 Artificial Intelligence Part 3: Logical queries to a database Prof. Neil C. Rowe Naval Postgraduate School Version of July 2009

Notes for CS3310 Artificial Intelligence

Part 3: Logical queries to a database

Prof. Neil C. Rowe Naval Postgraduate School

Version of July 2009

Page 2: Notes for CS3310 Artificial Intelligence Part 3: Logical queries to a database Prof. Neil C. Rowe Naval Postgraduate School Version of July 2009

Interpreters for programming languages• Interpreters run programs without compiling them,

working directly on the source code. • Example interpretive languages:

– Prolog (focused on logic expressions)– Lisp (focused on linked lists) – Matlab (focused on two-dimensional arrays) – Mathematica (focused on algebraic expressions) – Python

• Advantages of interpreters: Faster prototyping, faster debugging

• Disadvantages: Slower execution

Page 3: Notes for CS3310 Artificial Intelligence Part 3: Logical queries to a database Prof. Neil C. Rowe Naval Postgraduate School Version of July 2009

Running a Prolog interpreter on Windows machines

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

2. A Gnu Prolog interepreter is also installed on many of the general computer-science laboratory machines.

3. Use a text editor to create a file of your facts and (possibly) rules. If C: drive unavailable, store on H: drive.

4. Start Prolog by clicking on the icon created during installation. A black window should appear, and a ?- should appear inside it.

Page 4: Notes for CS3310 Artificial Intelligence Part 3: Logical queries to a database Prof. Neil C. Rowe Naval Postgraduate School Version of July 2009

Running an interpreter, cont.5. Do change_directory with argument the name of the

directory containing your files enclosed in apostrophes, e.g. change_directory('c:/prolog/progs3310'). (note the period). You must use "/" rather than "\" to indicate subdirectories.

6. Load your file of facts and rules by doing ['filename’]. (note the period) where filename is the name of your file.

7. Once your file is loaded, you can query any of the facts and rules in it by the methods discussed in class.

8. When done with Prolog, type halt. (note the period).

9. For a user's manual, click on "Help". Prolog dialects do not differ very much.

10. To preserve a record of your run, push “printscreen” and paste, or use a scripting facility.

Page 5: Notes for CS3310 Artificial Intelligence Part 3: Logical queries to a database Prof. Neil C. Rowe Naval Postgraduate School Version of July 2009

Usual operation of Prolog software

User

Prolog interpreter

Prolog database

Source code in

the Prolog language

loads

runs edits

consult

queries answers

Page 6: Notes for CS3310 Artificial Intelligence Part 3: Logical queries to a database Prof. Neil C. Rowe Naval Postgraduate School Version of July 2009

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

Page 7: Notes for CS3310 Artificial Intelligence Part 3: Logical queries to a database Prof. Neil C. Rowe Naval Postgraduate School Version of July 2009

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.

Page 8: Notes for CS3310 Artificial Intelligence Part 3: Logical queries to a database Prof. Neil C. Rowe Naval Postgraduate School Version of July 2009

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., X=tom_jones and Y=dick_deadeye in answering the query ?- father_of(X,Y).).

Page 9: Notes for CS3310 Artificial Intelligence Part 3: Logical queries to a database Prof. Neil C. Rowe Naval Postgraduate School Version of July 2009

Simple example queries

?-incumbent(csprofessor, X).

X=baer?- incumbent(J,rowe).J=csprofessor;no.

(A semicolon typed by the user means "Try to find another answer"; a carriage return means to stop.).

?- incumbent(X,P).

X=csprofessor, P=baer;

X=csprofessor, P=berzins

?- incumbent(csprofessor, rowe).

yes

?-

Page 10: Notes for CS3310 Artificial Intelligence Part 3: Logical queries to a database Prof. Neil C. Rowe Naval Postgraduate School Version of July 2009

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.

Page 11: Notes for CS3310 Artificial Intelligence Part 3: Logical queries to a database Prof. Neil C. Rowe Naval Postgraduate School Version of July 2009

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.

Page 12: Notes for CS3310 Artificial Intelligence Part 3: Logical queries to a database Prof. Neil C. Rowe Naval Postgraduate School Version of July 2009

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.

Page 13: Notes for CS3310 Artificial Intelligence Part 3: Logical queries to a database Prof. Neil C. Rowe Naval Postgraduate School Version of July 2009

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

Page 14: Notes for CS3310 Artificial Intelligence Part 3: Logical queries to a database Prof. Neil C. Rowe Naval Postgraduate School Version of July 2009

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

Page 15: Notes for CS3310 Artificial Intelligence Part 3: Logical queries to a database Prof. Neil C. Rowe Naval Postgraduate School Version of July 2009

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

Page 16: Notes for CS3310 Artificial Intelligence Part 3: Logical queries to a database Prof. Neil C. Rowe Naval Postgraduate School Version of July 2009

Logic symbols of predicate calculus, Prolog, and JavaLogic concept Predicate

calculus Prolog Java Python

conjunction ("and")

, & and

disjunction ("or") ; | or negation ("not") ~ or \+ ! not implication ("if") :- if if

grouping ( ) ( ) ( ) () argument separator , , , ,

"true" constant true true true True "false" constant false fail false False

universal quantification ("for every")

[can be implied]

[done with loops]

[done with loops]

existential quantification ("there exists")

[can be implied]

[done with loops]

[done with loops]

equivalence or = = [none] [none]

Page 17: Notes for CS3310 Artificial Intelligence Part 3: Logical queries to a database Prof. Neil C. Rowe Naval Postgraduate School Version of July 2009

Venn diagrams• Often it’s useful to draw a box representing the world and

circles representing things in the world having properties. We can often visualize logical relationships better.

• Below, everything in the “p” circle is something for which p is true.

• Everything in the area in overlap between the “p” and “r” circles but not in the “q” circle is something for which is true.

p

q r

~q r p

~q r p

Page 18: Notes for CS3310 Artificial Intelligence Part 3: Logical queries to a database Prof. Neil C. Rowe Naval Postgraduate School Version of July 2009

Equivalences of propositional calculus

• Unduplication: pp p; pp p• Tautology: p~p true• Contradiction: p ~p false• Constants: ptrue p, pfalse false,

ptrue true, pfalse p• Commutativity: pq qp, pq qp • Associativity: (pq)r p(qr),

(pq)r p(qr) • Distributivity: (pq)r (pr)(qr),

(pq)r (pr)(qr)

(p, q, and r represent any logical expression)

Page 19: Notes for CS3310 Artificial Intelligence Part 3: Logical queries to a database Prof. Neil C. Rowe Naval Postgraduate School Version of July 2009

Equivalences of propositional calculus (2)

• Double negation: ~ ~ p p• DeMorgan’s Laws: ~ (p q) ~ p ~ q, ~

(p q) ~ p ~ q• Implication disjunctive: (p q) (p ~ q)

(hence "p is true" is written as p, not truep)• Contrapositive: (p q) (~ q ~ p) • Absorption: ((pq) p) p, ((pq)

p) p• Negative absorption: ((~pq) p) (qp),

((~pq) p) (qp)

Page 20: Notes for CS3310 Artificial Intelligence Part 3: Logical queries to a database Prof. Neil C. Rowe Naval Postgraduate School Version of July 2009

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

Page 21: Notes for CS3310 Artificial Intelligence Part 3: Logical queries to a database Prof. Neil C. Rowe Naval Postgraduate School Version of July 2009

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

Page 22: Notes for CS3310 Artificial Intelligence Part 3: Logical queries to a database Prof. Neil C. Rowe Naval Postgraduate School Version of July 2009

Predicate calculus• It’s 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.

Page 23: Notes for CS3310 Artificial Intelligence Part 3: Logical queries to a database Prof. Neil C. Rowe Naval Postgraduate School Version of July 2009

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: XY(p(X,Y)) YX(p(X,Y)), XY(p(X,Y)) YX(p(X,Y))

Quantifier elimination: X(p) p, X(p) p

(p represents any predicate name; X

and Y represent any variable)

Page 24: Notes for CS3310 Artificial Intelligence Part 3: Logical queries to a database Prof. Neil C. Rowe Naval Postgraduate School Version of July 2009

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 form 1: If q(a) and X(p(X) q(X)), conclude p(a).

• Quantified modus ponens form 2: If X(q(X)) and X(p(X) q(X)), conclude X(p(X)).

Page 25: Notes for CS3310 Artificial Intelligence Part 3: Logical queries to a database Prof. Neil C. Rowe Naval Postgraduate School Version of July 2009

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.

Page 26: Notes for CS3310 Artificial Intelligence Part 3: Logical queries to a database Prof. Neil C. Rowe Naval Postgraduate School Version of July 2009

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.

Page 27: Notes for CS3310 Artificial Intelligence Part 3: Logical queries to a database Prof. Neil C. Rowe Naval Postgraduate School Version of July 2009

Proofs in predicate calculus

• A series of steps starting with “given”s 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.

Page 28: Notes for CS3310 Artificial Intelligence Part 3: Logical queries to a database Prof. Neil C. Rowe Naval Postgraduate School Version of July 2009

Example proof #1 in predicate calculus

Prove given p.

1. p (given)

2. (disjunctive addition on 1)

3. (disjunctive addition on 2)

4. (a DeMorgan’s Law on 3)

5. (disjunction equivalent of an implication on 4)

( )p q r

~p q

~ ( )p q r ~ ~p q r

( )p q r

Page 29: Notes for CS3310 Artificial Intelligence Part 3: Logical queries to a database Prof. Neil C. Rowe Naval Postgraduate School Version of July 2009

Example proof #2 in predicate calculus

Given fact p(1,2), statement , and statement , prove r(2,2).

1. p(1,2) (given)

2. (given)

3. q(1,3) (resolution of 1 and 2)

4. (given)

5. r(2,2) (modus ponens on 3 and 4)

(1,3) ~ (1,2)q p(2,2) (1,3)r q

(1,3) ~ (1,2)q p

(2,2) (1,3)r q

Page 30: Notes for CS3310 Artificial Intelligence Part 3: Logical queries to a database Prof. Neil C. Rowe Naval Postgraduate School Version of July 2009

Example proof #3 in predicate calculus

Prove given and

.

1. (given)

2. (given)

3. (distributive law on 2)

4. (conjunctive simplification on 3)

5. (universal instantiation on 1)

6. (modus tollens on 4 and 5)

)3(~ p

)]()([ XqXpX ))3()3((~))4()3((~ qpqp

))3()4(()3(~ qqp

)3()3( qp )3(~ q

)3(~ q )]()([ XqXpX ))3()3((~))4()3((~ qpqp

Page 31: Notes for CS3310 Artificial Intelligence Part 3: Logical queries to a database Prof. Neil C. Rowe Naval Postgraduate School Version of July 2009

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

Page 32: Notes for CS3310 Artificial Intelligence Part 3: Logical queries to a database Prof. Neil C. Rowe Naval Postgraduate School Version of July 2009

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.

Page 33: Notes for CS3310 Artificial Intelligence Part 3: Logical queries to a database Prof. Neil C. Rowe Naval Postgraduate School Version of July 2009

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.

Page 34: Notes for CS3310 Artificial Intelligence Part 3: Logical queries to a database Prof. Neil C. Rowe Naval Postgraduate School Version of July 2009

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.

Page 35: Notes for CS3310 Artificial Intelligence Part 3: Logical queries to a database Prof. Neil C. Rowe Naval Postgraduate School Version of July 2009

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

Page 36: Notes for CS3310 Artificial Intelligence Part 3: Logical queries to a database Prof. Neil C. Rowe Naval Postgraduate School Version of July 2009

Another backtracking exampleQuery:?- 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).

Page 37: Notes for CS3310 Artificial Intelligence Part 3: Logical queries to a database Prof. Neil C. Rowe Naval Postgraduate School Version of July 2009

Ands and ors in Javapublic 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; }

Page 38: Notes for CS3310 Artificial Intelligence Part 3: Logical queries to a database Prof. Neil C. Rowe Naval Postgraduate School Version of July 2009

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.

Page 39: Notes for CS3310 Artificial Intelligence Part 3: Logical queries to a database Prof. Neil C. Rowe Naval Postgraduate School Version of July 2009

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

Page 40: Notes for CS3310 Artificial Intelligence Part 3: Logical queries to a database Prof. Neil C. Rowe Naval Postgraduate School Version of July 2009

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.

Page 41: Notes for CS3310 Artificial Intelligence Part 3: Logical queries to a database Prof. Neil C. Rowe Naval Postgraduate School Version of July 2009

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.

Page 42: Notes for CS3310 Artificial Intelligence Part 3: Logical queries to a database Prof. Neil C. Rowe Naval Postgraduate School Version of July 2009

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

Page 43: Notes for CS3310 Artificial Intelligence Part 3: Logical queries to a database Prof. Neil C. Rowe Naval Postgraduate School Version of July 2009

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 X=pequod.

If we query:

observed(110, patrol, radar, X).

Case-based reasoning would likely find that the third fact is the best match and X=nelson.

Page 44: Notes for CS3310 Artificial Intelligence Part 3: Logical queries to a database Prof. Neil C. Rowe Naval Postgraduate School Version of July 2009

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.