Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

12-10-2013_09-24-50 / [Simon_Kendal,_Malcolm_Creen]_Introduction_to_Know(BookFi.org)

.pdf
Скачиваний:
108
Добавлен:
14.03.2016
Размер:
1.71 Mб
Скачать

Expert System Shells, Environments and Languages

173

? - ventilator(fred).

and

? - ventilator(tina).

Activity 62

Produce PROLOG statements for the facts listed below.

Remember that the person owning an object will normally appear inside of the brackets, with the object being owned appearing outside the brackets.

Fact

PROLOG statement

Bill is male

 

Linda is female

 

Peter is male

 

Bill has a single ticket

 

Linda has a return ticket

 

What PROLOG query would you use to determine whether Bill has a return ticket?

Feedback 62

 

Fact

PROLOG statement

Bill is male

male(bill).

Linda is female

female(linda).

Peter is male

male(peter).

Bill has a single ticket

single-ticket(bill).

Linda has a return ticket

return-ticket(linda).

You should also have been able to construct the following to query whether Bill has a return ticket:

? - return-ticket(bill).

Extracting a Set of Records from an ES

The question structure within PROLOG can be used to identify and extract a set of related facts from the total of all facts given to PROLOG. A variable is placed where a query is to be made about the facts, PROLOG then searches through the facts and returns any matches.

For example, we may need to find out which patients are female from the set of facts concerning patients used above. The PROLOG statement will be written as:

? - female(Patient).

174

An Introduction to Knowledge Engineering

The initial capital letter in ‘Patient’ indicates that it is a variable; all variables must begin with an uppercase letter.

Activity 63

Using the information concerning train tickets from the last exercise, write a PROLOG query that will find people holding a return ticket.

Feedback 63

? - return-ticket(Customer).

You may use a different noun to Customer. This is fine, as long as it describes the person travelling on the train (and uses a capital letter). Other possible examples could be Traveller, Commuter, etc.

Combining Queries

In some situations, it will be necessary to extract records from two sets of different facts using PROLOG.

For example, names of some males and respiratory conditions are stored in the following facts.

male(tim). male(marc). male(simon). resp(tim,acute). resp(marc,medium). resp(simon,acute).

A match will be generated from this set of facts to the query male(X) where X is an instance of male. A match will also be generated from this set of facts to the query resp(X,Y) if the patient X has a respiratory condition of the state Y.

However, a reasonable question to ask the ES is:

‘Do any male patients have an acute respiratory condition?’

Or in PROLOG format:

? - male(X),resp(X,acute)

Literally, is there an X who is male and has a respiratory condition that is acute?

Expert System Shells, Environments and Languages

175

Given the facts above, a solution cannot be found directly because the gender male is not linked to the respiratory condition facts. Therefore, PROLOG will have to check the two sub-goals and match the results from these before the query can be answered.

Activity 64

Given the PROLOG facts below, write a query that asks PROLOG to search for any female who has a return ticket.

Facts one

Facts two

male(fred).

single-ticket(bill).

female(linda).

return-ticket(peter).

male(peter).

return-ticket(linda).

male(nigel).

single-ticket(john).

female(jayne).

single-ticket(nigel).

Feedback 64

Your query should be as follows:

? - female(X),return-ticket(X).

Inferences

The principle of inference has already been discussed. PROLOG can perform backward-chaining inference from facts provided to it. For example, the following clause can be used to determine whether or not two people can marry.

can_marry (X, Y) :- male (X),

female (Y), not_married (X), not_married (Y).

From this information PROLOG can determine that two people can marry if X is male and Y is female, and if neither person is already married.

Activity 65

Examine the PROLOG program listing below, and the following goals:

suspect(X).

Who is a suspect?

find-motive(X,M).

Who

had

a motive?

killer(X).

Who

was

the killer?

176

An Introduction to Knowledge Engineering

By working through the logic on paper try to list the suspects, those with a motive and finally find the killer.

PROLOG Program

/* Adapted from a program created by the Prolog Development Center the makers of Visual Prolog */

person(bert,55,m,carpenter). person(allan,25,m,football-player). person(allan,25,m,butcher). person(john,25,m,pickpocket). person(barbara,39,f,doctor).

had-affair(barbara,john). had-affair(barbara,allan). had-affair(susan,john). had-affair(susan,bert).

killed-with(susan,club). killed(susan).

smeared-in(bert, blood). smeared-in(susan, blood). smeared-in(allan, mud). smeared-in(john, chocolate). smeared-in(barbara, blood).

owns(bert,wooden-leg). owns(john,pistol).

/* Background knowledge */ operates-identically(wooden-leg, club). operates-identically(bar, club). operates-identically(pair-of-scissors, knife). operates-identically(football-boot, club).

owns-probably(X,football-boot):- person(X,-,-,football-player).

owns-probably(X,pair-of-scissors):- person(X,-,-,hairdresser).

owns-probably(X,Object):- owns(X,Object).

/* Suspect all those who owned a weapon with which Susan could have been killed * /

suspect(X):-

killed-with (susan,Weapon), operates- identically(Object,Weapon), owns-probably(X,Object).

Expert System Shells, Environments and Languages

177

/* Men who have had an affair with Susan have a motive 'jealousy'. */

find-motive(X,jealousy):- person(X,-,m,-), had-affair(susan,X).

/* Females who have had an affair with someone that Susan knew also have a motive. */

find-motive(X,jealousy):- person(X,-,f,-), had-affair(X,Man), had-affair(susan,Man).

/* Pickpockets have a motive 'money'.*/ find-motive(X,money):-

person(X,-,-,pickpocket).

/* How to work out the killer */ killer(Killer):-

person(Killer,-,-,-), killed(Killed),

not(Killed= Killer), /* i.e., Not suicide */ suspect(Killer),

find-motive(Killer,-), smeared-in(Killer,Goo), smeared-in(Killed,Goo).

Feedback 65

This is an exercise in backtracking, not a test of your ability to solve murder mysteries.

The suspects are Bert and Allan.

The following people had a motive:

Bert (jealousy)

John (jealousy)

Barbara (jealousy)

John (jealousy).

Bert is guilty because he is a suspect, has a motive and is smeared in the same stuff as the victim.

Working with Lists

PROLOG provides a mechanism for working with lists – its main mechanism for handling large quantities of data.

178

An Introduction to Knowledge Engineering

A list can be broken down into two parts: Its head, i.e., the first element and its tail, i.e., the rest of the list after the first element has been removed (this may be empty).

For example, in the list [fred, albert, jim], the head is the element ‘fred’ and the tail is the list [albert,jim].

Activity 66

Identify the head and tail of the following lists:

[alice,tim,bert]

[alice,tim]

[alan] [ ]

Feedback 66

List

Head

Tail

Explanation

 

 

 

 

[alice,tim,bert]

Alice

[tim,bert]

 

[alice,tim]

Alice

[tim]

Note: here the head is a

 

 

 

single item but the tail is

 

 

 

a list. It only has one

 

 

 

element in it but is

 

 

 

nonetheless a list.

[alan]

Alan

[ ]

Here the tail is an empty

 

 

 

list.

[ ]

Fail

 

This will fail because the

 

 

 

PROLOG cannot assign a

 

 

 

value to the head.

When a list is matched to notation in the form [X|Y], X is instantiated to (given the value of) the head and Y is instantiated to the tail.

Activity 67

Assuming the following, very short, program is entered . . .

letters([a,b,c,d]).

What would be the result of the following goal?

letters([H|T]).

Expert System Shells, Environments and Languages

179

Feedback 67

The result of the above will be true (or Yes) when H is ‘a’ and T is [b,c,d].

The program in the next activity displays each element of a list. It takes the first element from the front of a list, prints out this element and then calls the function print (i.e., itself) to display the rest of the list.

In other words

to print the list [a,b,c] means print ‘a’ and then print the list [b,c].

to print the list [b,c] means print ‘b’ and then print the list [c].

to print the list [c] means print ‘c’ and then print the list [].

Thus a function call to print the list [a,b,c] will cause ‘a’ to be printed and then ‘b’ and finally ‘c’.

When a function uses itself, as the print function does in Program 3, then this is called recursive programming. It is unusual, complex for beginners but nonetheless a powerful mechanism.

Activity 68

Read through the following program carefully. Try to follow its logic.

print([X|Y]) :-

write(X), /* write is a function to print out a value*/ nl, /* nl prints a new line */

print(Y).

What would this program do with the following goal?

print ([a,b,c,d,e]).

Feedback 68

The program will print out the following

a b c d e f

180

An Introduction to Knowledge Engineering

A More Complex PROLOG Program

Imagine a member function, which has two parameters, an element (an item we are looking for) and a list (in which we are looking for it).

Given such a function an example goal might be:

member(sunderland,[newcastle,durham,sunderland,middlesbroug- h]).

This member function could be written as follows:

member(X,[X|-]). member(X,[-|Y]) :- member(X,Y).

The first line of the member function splits the list into two parts and says that if the element has the same value as the head of the list then, irrespective of the rest of the list, that element is a member of the list.

In other words given the element ‘fred’ and the list ‘[fred,bert,jim]’ fred is a member of the list.

The second line of the member function says that, after failing the first test of membership, the list should be split into two parts and we must now check to see if the element is a member of the remaining section of the list. The head is of no concern because we have already determined that the element is not the same as the head of the list.

For example, given the element ‘fred’ and the list ‘[bert,jim,fred]’ as fred is not the first element in the list we must check the rest of the list, i.e., [jim,fred]. If ‘fred’ is a member of this shorter list then it must also be a member of the original list.

Summary

This section has provided an overview of how the PROLOG programming language uses rules to make decisions and uses lists as a method of storing data.

Self-Assessment Questions

Question 1

Given the following PROLOG facts, write a query to search for any male who has a single ticket.

Expert System Shells, Environments and Languages

181

 

 

 

 

 

Facts one

Facts two

 

 

male(fred)

ticket(bill,single)

 

 

female(linda)

ticket(peter,return)

 

 

male(peter)

ticket(linda,return)

 

 

male(nigel)

ticket(john,single)

 

 

female(jayne)

ticket(nigel,single)

 

 

 

 

 

Question 2

You have been asked to develop an online university admission system that offers intelligent advice and makes automatic course offers. The system will have a web front end and contain an ES to advise prospective students of appropriate courses at a university. The system will make actual course offers (subject to confirmation of results) to prospective students. The web-based KBS system is to be integrated with a University admissions database system so that prospective overseas applicants gain advice and where appropriate make an automatic application. It has also been decided that the system should contain an ES integrated with a CBR system. The system will only make offers if both the ES and the CBR agree that an offer should be made.

Consider each of the following types of tool and select the most appropriate for this problem:

An ES shell

A KBS development environment (e.g. Aion BRE)

An AI language (e.g. PROLOG)

A conventional programming language (e.g. C++ or Java).

Answer to Self-Assessment Questions

Answer 1

Your query should be as follows:

? - male(X),ticket(X,single)

Answer 2

The system specified is very complex containing an ES integrated with a webbased information system, a CBR system and a conventional information system. If this entire system were to be developed using one tool then it requires a flexible solution, certainly most shells would not be appropriate nor would PROLOG.

PROLOG does not have the facilities to develop the web front end or the CBR system though it could be used to develop the knowledge-based component and this could be integrated with components created with other software. The use of

182

An Introduction to Knowledge Engineering

PROLOG would allow more flexibility for developing the ES component. However, nothing in the problem suggests that such flexibility is required.

A conventional programming language could be used to develop the entire system but would not contain any tools to support the ES component, thus everything would need to be developed from scratch and this would be costly and take time.

Aion BRE would support the ES component and, as it is a full OO (Object Oriented) programming development tool, it could also be used to develop the other components. Thus if one tool had to be chosen this would be the best choice. However, in the real world an application such as this would be developed in separate components using a range of tools. Thus Aion BRE could be used to develop the ES component, a visual programming environment could be used to develop the web-based front end, a CBR tool could be used to develop the CBR component and a procedural programming tool could be used to develop the database/information system. These separate components would then be integrated to form one complete system.