31
Using Prolog's Using Prolog's Inference Engine Inference Engine From: Chapter 2, Building Expert Systems in Prolog , http://www.amzi.com/ExpertSystemsInProlo g/xsipfrtop.htm

Using Prolog's Inference Engine

  • Upload
    vila

  • View
    70

  • Download
    0

Embed Size (px)

DESCRIPTION

Using Prolog's Inference Engine. From: Chapter 2, Building Expert Systems in Prolog , http://www.amzi.com/ExpertSystemsInProlog/xsipfrtop.htm. Contents. The Bird Identification System User Interface A Simple Shell Summary. The Bird Identification System. - PowerPoint PPT Presentation

Citation preview

Page 1: Using Prolog's  Inference Engine

Using Prolog's Using Prolog's Inference Engine Inference Engine

From: Chapter 2, Building Expert Systems in Prolog , http://www.amzi.com/ExpertSystemsInProlog/xsipfrtop.htm

Page 2: Using Prolog's  Inference Engine

Using Prolog's Inference Engine

2

Contents

• The Bird Identification System• User Interface• A Simple Shell• Summary

Page 3: Using Prolog's  Inference Engine

Using Prolog's Inference Engine

3

The Bird Identification System

• A system which identifies birds will be used to illustrate a native Prolog expert system.

• The expertise in the system is a small subset of that contained in Birds of North America by Robbins, Bruum, Zim, and Singer.

• The rules of the system were designed to illustrate how to represent various types of knowledge, rather than to provide accurate identification.

Page 4: Using Prolog's  Inference Engine

Using Prolog's Inference Engine

4

Rule formats

IFfirst premise, andsecond premise, and...

THENconclusion

conclusion :-first_premise, second_premise, ...

Page 5: Using Prolog's  Inference Engine

Using Prolog's Inference Engine

5

Rules about birds

• The most fundamental rules in the system identify the various species of birds.

• We can begin to build the system immediately by writing some rules. Using the normal IF THEN format, a rule for identifying a particular albatross is:

IFfamily is albatross andcolor is whiteTHENbird is laysan_albatross

bird(laysan_albatross) :-family(albatross), color(white).

bird(black_footed_albatross):-family(albatross), color(dark).

bird(whistling_swan) :-family(swan), voice(muffled_musical_whistle).

bird(trumpeter_swan) :-family(swan), voice(loud_trumpeting).

Page 6: Using Prolog's  Inference Engine

Using Prolog's Inference Engine

6

Rules about birds

• In order for these rules to succeed in distinguishing the two birds, we would have to store facts about a particular bird that needed identification in the program. For example if we added the following facts to the program:

family(albatross).color(dark).

?- bird(X).X = black_footed_albatross

Page 7: Using Prolog's  Inference Engine

Using Prolog's Inference Engine

7

Rules for hierarchical relationships

• The next step in building the system would be to represent the natural hierarchy of a bird classification system.

• These would include rules for identifying the family and the order of a bird.

order(tubenose) :- nostrils(external_tubular), live(at_sea), bill(hooked).

order(waterfowl) :- feet(webbed), bill(flat).

family(albatross) :- order(tubenose), size(large), wings(long_narrow).

family(swan) :- order(waterfowl), neck(long), color(white), flight(ponderous).

Page 8: Using Prolog's  Inference Engine

Using Prolog's Inference Engine

8

Rules for hierarchical relationships

nostrils(external_tubular).live(at_sea).bill(hooked).size(large).wings(long_narrow).color(dark).

?- bird(X).X = black_footed_albatross

Page 9: Using Prolog's  Inference Engine

Using Prolog's Inference Engine

9

bird

lysan_albatros black_footed_albatros trumpter_swan

family(albatros) color(white)

color(dark)

voice(loud)

family(albatros)

family(swan)

family(albatros)

family(swan)order(tubenose) live(at_sea) bill(hooked)

order(waterfowl) neck(long)

color(white)

flight(ponderous)Relationships between some of the rules in the Bird identification system

Page 10: Using Prolog's  Inference Engine

Using Prolog's Inference Engine

10

Rules for other relationships

• The Canada goose can be used to add some complexity to the system. – Since it spends its summers in Canada, and its winters in

the United States, its identification includes where it was seen and in what season.

bird(canada_goose):- family(goose), season(winter), country(united_states), head(black), cheek(white).

bird(canada_goose):- family(goose), season(summer), country(canada), head(black), cheek(white).

Page 11: Using Prolog's  Inference Engine

Using Prolog's Inference Engine

11

Rules for other relationships

country(united_states):- region(mid_west).country(united_states):- region(south_west).country(united_states):- region(north_west).country(united_states):- region(mid_atlantic).country(canada):- province(ontario).country(canada):- province(quebec).region(new_england):- state(X), member(X, [massachusetts, vermont, ....]).region(south_east):- state(X), member(X, [florida, mississippi, ....]).

bird(mallard):- family(duck), voice(quack), head(green).bird(mallard):- family(duck), voice(quack), color(mottled_brown).

Page 12: Using Prolog's  Inference Engine

Using Prolog's Inference Engine

12

User Interface

• The system can be dramatically improved by providing a user interface which prompts for information when it is needed, rather than forcing the user to enter it beforehand.

• The predicate ask will provide this function.

Page 13: Using Prolog's  Inference Engine

Using Prolog's Inference Engine

13

Attribute Value pairs

• Before looking at ask, it is necessary to understand the structure of the data which will be asked about.

• All of the data has been of the form: "attribute-value". – For example, a bird is a mallard if it has the following

values for these selected bird attributes:

Attribute Value family duck voice quack head green

Page 14: Using Prolog's  Inference Engine

Using Prolog's Inference Engine

14

Attribute Value pairs

• This is one of the simplest forms of representing data in an expert system, but is sufficient for many applications.

• More complex representations can have "object-attribute-value" triples, where the attribute-values are tied to various objects in the system.

• Still more complex information can be associated with an object and this will be covered in the chapter on frames.

• For now the simple attribute-value data model will suffice.

Page 15: Using Prolog's  Inference Engine

Using Prolog's Inference Engine

15

Asking the user

• The ask predicate will have to determine from the user whether or not a given attribute-value pair is true.

• The program needs to be modified to specify which attributes are askable.

• This is easily done by making rules for those attributes which call ask.

eats(X):- ask(eats, X).feet(X):- ask(feet, X).wings(X):- ask(wings, X).neck(X):- ask(neck, X).color(X):- ask(color, X).

• Now if the system has the goal of finding color(white) it will call ask, rather than look in the program. If ask(color, white) succeeds, color(white) succeeds

Page 16: Using Prolog's  Inference Engine

Using Prolog's Inference Engine

16

Asking the user

• The simplest version of ask

ask(Attr, Val):- write(Attr:Val),write('? '), read(yes).

?- bird(X).nostrils : external_tubular? yes.live : at_sea? yes.bill : hooked? yes.size : large? yes.wings : long_narrow? yes.color : white? yes.X = laysan_albatross

Page 17: Using Prolog's  Inference Engine

Using Prolog's Inference Engine

17

Asking the user

• There is a problem with this approach. – If the user answered "no" to the last question, then

the rule for bird(laysan_albatross) would have failed and backtracking would have caused the next rule for bird(black_footed_albatross) to be tried.

– The first subgoal of the new rule causes Prolog to try to prove family(albatross) again, and ask the same questions it already asked.

– It would be better if the system remembered the answers to questions and did not ask again.

Page 18: Using Prolog's  Inference Engine

Using Prolog's Inference Engine

18

Remembering the answer

ask(A, V):- known(yes, A, V), % succeed if true !. % stop looking ask(A, V):- known(_, A, V), % fail if false !, fail.ask(A, V):- write(A:V), % ask user write('? : '), read(Y), % get the answer asserta(known(Y, A, V)), % remember it Y == yes. % succeed or fail

Page 19: Using Prolog's  Inference Engine

Using Prolog's Inference Engine

19

Multi-valued answers

• The ask predicate now assumes that each particular attribute value pair is either true or false. – This means that the user could respond with a "yes"

to both color:white and color:black.

• In effect, we are letting the attributes be multi-valued. – This might make sense for some attributes such as

voice but not others such as bill, which only take a single value.

Page 20: Using Prolog's  Inference Engine

Using Prolog's Inference Engine

20

Multi-valued answers

• The best way to handle this is to add an additional predicate to the program which specifies which attributes are multi-valued:

multivalued(voice).multivalued(feed).

Page 21: Using Prolog's  Inference Engine

Using Prolog's Inference Engine

21

Menus for the user

• The user interface can further be improved by adding a menu capability which gives the user a list of possible values for an attribute.

• It can further enforce that the user enter a value on the menu.

size(X):- menuask(size, X, [large, plump, medium, small]).flight(X):- menuask(flight, X, [ponderous, agile, flap_glide]).

Page 22: Using Prolog's  Inference Engine

Using Prolog's Inference Engine

22

Menus for the user

menuask(A, V, MenuList) :- write('What is the value for'), write(A), write('?'), nl, write(MenuList), nl, read(X), check_val(X, A, V, MenuList), asserta( known(yes, A, X) ), X == V.

check_val(X, A, V, MenuList) :- member(X, MenuList), !.check_val(X, A, V, MenuList) :- write(X), write(' is not a legal value, try again.'), nl, menuask(A, V, MenuList).

can be implemented using asophisticated windowing interface

Page 23: Using Prolog's  Inference Engine

Using Prolog's Inference Engine

23

A Simple Shell

• The bird identification program has two distinct parts: – the knowledge base, which contains the specific

information about bird identification; and – the predicates which control the user interface.

• By separating the two parts, a shell can be created which can be used with any other knowledge base. – For example, a new expert system could be written

which identified fish. – It could be used with the same user interface code

developed for the bird identification system.

Page 24: Using Prolog's  Inference Engine

Using Prolog's Inference Engine

24

A Simple Shell

• The minimal change needed to break the two parts into two modules is a high level predicate which starts the identification process.

• The shell has a predicate called solve, which does some housekeeping and then solves for the top_goal. It looks like:

top_goal(X) :- bird(X).

solve :- abolish(known, 3), define(known, 3), top_goal(X), write('The answer is '), write(X), nl.solve :- write('No answer found.'), nl.

Page 25: Using Prolog's  Inference Engine

Using Prolog's Inference Engine

25

A Simple Shell

• In summary, the predicates of the bird identification system have been divided into two modules. The predicates which are in the shell called Native, are:

solve - starts the consultation;ask - poses simple questions to the users and remembers the answers;menuask - presents the user with a menu of choices; supporting predicates for the above three predicates.

Page 26: Using Prolog's  Inference Engine

Using Prolog's Inference Engine

26

A Simple Shell

• The predicates which are in the knowledge base are:

top_goal - specifies the top goal in the knowledge base; rules for identifying or selecting whatever it is the knowledge base was built for (for example bird, order, family, and region); rules for attributes which must be user supplied (for example size, color, eats, and wings);multivalued - defines which attributes might have multiple values.

Page 27: Using Prolog's  Inference Engine

Using Prolog's Inference Engine

27

A Simple Shell

• To use this shell with a Prolog interpreter, both the shell and the birds knowledge base must be consulted. Then the query for solve is started.

?- consult(native).yes?- consult('birds.kb').yes?- solve.nostrils : external_tubular?…

Page 28: Using Prolog's  Inference Engine

Using Prolog's Inference Engine

28

Command loop

• The shell can be further enhanced to have a top level command loop called go. To begin with, go should recognize three commands:– load - Load a knowledge base.– consult - Consult the knowledge base by satisfying the top

goal of the knowledge base.– quit - Exit from the shell.

go :- greeting, repeat, write('> '), read(X), do(X), X == quit.greeting :- write('This is the Native Prolog shell.'), nl, write('Enter load, consult, or quit at the prompt.'), nl.

do(load) :- load_kb, !.do(consult) :- solve, !.do(quit).do(X) :- write(X), write('is not a legal command.'), nl, fail.

Page 29: Using Prolog's  Inference Engine

Using Prolog's Inference Engine

29

Command loop

• Two other commands which could be added at this point are:– help - provide a list of legal commands;– list - list all of the knowns derived during the consultation (useful

for debugging).

load_kb :- write('Enter file name: '), read(F), reconsult(F).

Page 30: Using Prolog's  Inference Engine

Using Prolog's Inference Engine

30

User interfacegoaskmenuask

Inference enginesolveload

Knowledgebasetop_goalrulesmulti_valuedaskable

Workingstorageknown

Major predicates of Native Prolog shell

Page 31: Using Prolog's  Inference Engine

Using Prolog's Inference Engine

31

Command loop

• Using an interpreter the system would run as follows:

?- consult(native).yes?- go.This is the native Prolog shell.Enter load, consult, or quit at the prompt.>load.Enter file name: 'birds.kb'.>consult.nostrils : external_tubular ? yes....The answer is black_footed_albatross>quit.?-