Wednesday, October 21, 2009

Fact, Rules, Predicate, and Variable in Prolog

Today’s Problem
(1) Type the following program into a file and load it into Prolog.

/* Animals Database */

animal(mammal,tiger,carnivore,stripes).

animal(mammal,hyena,carnivore,ugly).

animal(mammal,lion,carnivore,mane).

animal(mammal,zebra,herbivore,stripes).

animal(bird,eagle,carnivore,large).

animal(bird,sparrow,scavenger,small).

animal(reptile,snake,carnivore,long).

animal(reptile,lizard,scavenger,small).


Devise and test goals to find (a) all the mammals, (b) all the carnivores that are mammals, (c) all the mammals with stripes, (d) whether there is a reptile that has a
mane.
(2) Type the following program into a file

/* Dating Agency Database */

person(bill,male).

person(george,male).

person(alfred,male).

person(carol,female).

person(margaret,female).

person(jane,female).
Extend the program with a rule that defines a predicate couple with two arguments, the first being the name of a man and the second the name of a woman.
Load your revised program into Prolog and test it.

To solve those problem, please do this step. Check this out!

First Question
1. Type all words in the box in Notepad, save it and don’t forget to insert .pl at the end of your name.
2. Consult your saved file in PROLOG.
3. To find the answer of number 1.a, you must type this.
?- animal(mammal,X,_,_).
Then press Enter.
4. To find another answers, type ; then another answers can appear until word No appears. It means there is no answer again.
5. To find the answer of number 1.b, you must type this.
?- animal(mammal,Y,carnivore,_).
Then press Enter.
6. To find another answer, follow the instruction number 4.
7. To find the answer of number 1.c, you must type this.
?- animal(mammal,Z,_,stripes).
Then press Enter.
8. To find another answer, follow the instruction number 4.
9. To find the answer of number 1.d, you must type this.
?- animal(reptile,R,_,mane).
Then press Enter.
10. Oh, what happened with this answer? Is there a wrong step? Absolutely no, because there is no reptile has mane in that program.


Second Question

1. Type all words in the box in Notepad.
2. We must make a rule to find all possible couples. The couple must a man and a woman, not both of them are men or women. So the correct rule for this problem is :
couple(X,Y) :- person(X,male),person(Y,female).
3. Type it and save but don’t forget to insert .pl at the end of your name.
4. Consult your saved file in PROLOG.
5. To find the answer, type this
?- couple(X,Y).
Then press Enter.
6. To find another answers, type ( ; ) then another answers can appear until word No appears. It means there is no answer again.

To make it clearly, please click this FILE!!!

Rule – Based Expert System

A rule-based expert system is an expert system which works as a production system in which rules encode expert knowledge. Most expert systems are rule-based.

Data-driven Rule-based Expert Systems

Use Forward Chaining:

Given a certain set of facts in WM, use the rules to generate new facts until the desired goal is reached.

To forward chain the inference engine must:

1. Match the condition patterns of rules against facts in working memory.

2. If there is more than one rule that could be used (that could "fire"), select which one to apply (this is called conflict resolution)

3. Apply the rule, maybe causing new facts to be added to working memory

4. Halt when some useful (or goal) conclusion is added to WM (or until all possible conclusions have been drawn.)

Goal-driven Rule-based Expert Systems

Use Backward Chaining:

Work backwards from a hypothesised goal, attempting to prove it by linking the goal to the initial facts.

To backward chain from a goal in WM the inference engine must:

1. Select rules with conclusions matching the goal.

2. Replace the goal by the rule's premises. These become sub-goals.

3. Work backwards till all sub-goals are known to be true -

either they are facts (in WM)

or the user provides the information.

Example

A production system IDENTIFIER, which identifies animals.

R1 IF the animal has hair
THEN it is a mammal

R2 IF the animal gives milk
THEN it is a mammal

R3 IF the animal has feathers
THEN it is a bird

R4 IF the animal flies
the animal lays eggs
THEN it is a bird

R5 IF the animal is a mammal
the animal eats meat
THEN it is a carnivore

R6 IF the animal is a mammal
the animal has pointed teeth
the animal has claws
the animal's eyes point forward
THEN it is a carnivore

R7 IF the animal is a mammal
the animal has hooves
THEN it is an ungulate

R8 IF the animal is a mammal
the animal chews cud
THEN it is an ungulate AND
it is even-toed

R9 IF the animal is a carnivore
the animal has a tawny colour
the animal has dark spots
THEN it is a cheetah

R10 IF the animal is a carnivore
the animal has a tawny colour
the animal has black stripes
THEN it is a tiger

R11 IF the animal is an ungulate
the animal has long legs
the animal has a long neck
THEN it is a giraffe

R12 IF the animal is an ungulate
the animal has a white colour
the animal has black stripes
THEN it is a zebra

R13 IF the animal is a bird
the animal does not fly
the animal has long legs
the animal has a long neck
the animal is black and white
THEN it is an ostrich

R14 IF the animal is a bird
the animal does not fly
the animal swims
the animal is black and white
THEN it is a penguin

R15 IF the animal is a bird
the animal is a good flier
THEN it is an albatross

PROBLEM:

Given these facts in working memory initially:

the animal gives milk
the animal chews its cud
the animal has long legs
the animal has a long neck

Establish by forward chaining that the animal is a giraffe.

Given the facts that:

the animal has hair
the animal has claws
the animal has pointed teeth
the animal's eyes point forward
the animal has a tawny colour
the animal has dark spots

Establish by backward chaining that the animal is a cheetah.

[HINT: start with R9, first subgoal : the animal is a carnivore etc.]

Goal-driven search is suggested if:

A goal or hypothesis is given in the problem statement or can be easily formulated
(theorem-proving, diagnosis hypothesis testing).

There are a large number of rules that match the facts, producing a large number of conclusions - choosing a goal prunes the search space.

Problem data are not given (or easily available) but must be acquired as necessary (e.g. medical tests).

Data-driven search is suggested if:

All or most of the data is given in the problem statement (interpretation problems)

Large number of potential goals but few achievable in a particular problem instance.

It is difficult to formulate a goal or hypothesis.

Tuesday, October 13, 2009

How to list all possible combination (consisting two elements) of a set in PROLOG and capture the image from PROLOG

The most wellknown on mathemathics lesson is sets. What is sets? Sets is a collection of well-defined object.

To definite subset operation, we will begin with definite a new predicate sis/2.

  • Firstly,write op(710,xfx,sis). On SWI-prolog then press enter .
  • Second, Write op(200,yfx,and). Then press enter
  • Write again op(200,yfx,or). Then press enter

We don’t need to definite the minus operator for difference because its already been definited.

Now we need the definition from and, or, and when we using sis connector. We use findall/3 predicate. Write on the notepad then save with format .pl

Example : lap.pl

This is used to find all of element’s member that same from A and B sets, all of member on A or B sets, and all of member in A but not exist in B.

Then, on the prolog, we consult the data that has been saved. File – Consult – lap.pl. then press enter.

Now,we write the member of sets on prolog, then follow the instruction A sis F and M, so do for or and

Example : F=[acer,toshiba,dell.apple],M=[dell,apple,fujitsu]. Then press enter. so do for Or and

Now, we are trying to combine chance from two sets. Here, example of chance from two sets about laptop and operation system.

  1. Write the member of sets on notepad, then save with .pl format

  1. Open swi-prolog program, choose File - consult choose our data .pl

  1. On prolog, write laptop(X),operationsystem(Y). And don’t forget to give .(dot/titik) on the last sentence.Then press enter.
  2. Then will appear the first chance. Then press ( ; ) until the last chance appear and NO also appeared.

That’s all the way to combine two sets on prolog. Actually this is very easy if we want to try it and do more practice. FIGHTING!!!

^_^

For make it clearly, please click this file!