Senin, 14 Desember 2009

Expert System Design in Prolog


EXPERT SYSTEM DESIGN FOR KNOWING THE CHARACTERISTIC OF BLOOD TYPE in SWI PROLOG


Pada Final Project 4, kita telah membuat desain system pakar yang berguna untuk mengetahui karakteristik atau komponen apa saja yang terdapat di dalam setiap golongan darah. Karakteristik tiap golongan darah pun telah kita jelaskan pada final project 4 beserta prolog nya. Namun, kami menganggap prolog yang kami buat sebelumnya belum sempurna. Oleh karena itu, kita akan menunjukkan desain system pakar baru yang kami buat di prolog. Mari kita perhatikan desain system pakar yang telah kami buat:


Sistem Pakar Karakteristik golongan darah ini memiliki data-data sebagai berikut:

Tipe A:

  1. Memiliki Aglutinogen A
  2. Memiliki Aglutinin B
  3. Jika darah ditetesi zat Anti A maka darah akan menggumpal dan Jika darah ditetesi zat Anti B maka darah tidak akan menggumpal.

Tipe B:

  1. Memiliki Aglutinogen B
  2. Memiliki Aglutinin A
  3. Jika darah ditetesi zat Anti A maka darah tidak akan menggumpal dan Jika darah ditetesi zat Anti B maka darah akan menggumpal.

Tipe AB:

  1. Memiliki Aglutinogen A dan B
  2. Tidak memiliki agglutinin
  3. Jika darah ditetesi zat Anti A maka darah akan menggumpa dan Jika darah ditetesi zat Anti B maka darah juga akan menggumpal.

Tipe O:

  1. Tidak Memiliki Aglutinogen
  2. Memiliki Aglutinogen A dan B
  3. Jika darah ditetesi zat Anti A ataupun zat Anti B maka darah tidak akan menggumpal.

Untuk menyimpan Data-Data tersebut dalam notepad agar bisa dijadikan program di prolog maka dapat ditulis atau diketik rule nya pada notepad sebagai berikut:


bloodtype(type_A,aglutinogen_A,aglutinin_B,clump,not_clump).

bloodtype(type_B,aglutinogen_B,aglutinin_A,not_clump,clump).

bloodtype(type_AB,aglutinogen_A_and_B,no_aglutinin,clump,clump).

bloodtype(type_O,no_aglutinogen,aglutinin_A_and_B,not_clump,not_clump).


NB(forgive us if its make you confuse):

type=menjelaskan tentang tipe darah

aglutinogen=menjelaskan aglutinogen yang dimiliki

aglutinin= menjelaskan aglutinin yang dimiliki

clump/not clump=clump/not clump setelah aglutinin menyatakan reaksi darah setelah ditetesi zat Anti A, sedangkan pada clump/not clump bagian akir menyatakan reaksi darah setelah ditetesi zat Anti B.



Kemudian, kami akan membuat program dimana saat di masukkan sebuah kondisi maka dapat disimpulkan jenis golongan darah tersebut. Misalnya:

1. JIKA : Memiliki Aglutinogen A

MAKA : Jenis Golongan Darah adalah Golongan Darah A.

2. JIKA : Memiliki Aglutinin A

MAKA : Jenis Golongan Darah adalah Golongan Darah B.

3. JIKA : Ditetesi zat Anti A menggumpal dan saat ditetesi zat Anti B tidak menggumpal

MAKA : Jenis Golongan Darah adalah Golongan Darah A.


Program pertama yang kami buat yaitu menentukan kondisi aglutinogen yang dimiliki oleh tiap tipe golongan darah. Maka dapat ditulis atau diketik caranya atau rule pada notepad sebagai berikut :


aglutinogen_A:-bloodtype(Darah,Aglutino,_,_,_),Aglutino=aglutinogen_A,

write('Blood Type is '),write(Darah),nl,fail.

aglutinogen_A.


Cara ini juga bisa dilakukan pada setiap golongan darah namun harus merubah persyaratannya terlebih dahulu. Untuk rule selengkapnya, lihat gambar di bawah ini:


Program kedua yang kami buat yaitu menentukan kondisi aglutinin yang dimiliki oleh tiap tipe golongan darah. Maka kita dapat mengetik rulenya pada notepad sebagai berikut :


no_aglutinin:-

bloodtype(Darah,_,Aglutinin,_,_),Aglutinin=no_aglutinin,write('Blood Type is '),write(Darah),nl,fail.

no_aglutinin.

Cara ini juga bisa dilakukan pada setiap golongan darah namun harus merubah persyaratannya terlebih dahulu. Untuk rule selengkapnya, lihat gambar di bawah ini:



Program ketiga yang kami buat yaitu menentukan kondisi saat ditetesi zat anti a dan zat anti b yang terjadi pada tiap tipe golongan darah. Kita dapatt mengetik rulenya pada notepad sebagai berikut :


clump_not:-

bloodtype(Darah,_,_,clump,not_clump),

write('Blood Type is '),write(Darah),nl,fail.

clump_not.


NB: clump_not memiliki arti clump = menggumpal saat ditetesi Zat anti A dan

not = tidak menggumpal saat ditetesi zat anti B.

more example: not_not memiliki arti not = tidak menggumpal saat ditetesi zat anti A dan not= tidak menggumpal saat ditetesi zat anti B.


Cara ini juga bisa dilakukan pada setiap golongan darah namun harus merubah persyaratannya terlebih dahulu. Untuk rule selengkapnya, lihat gambar di bawah ini:



Selain ketiga program diatas, kita juga membuat satu lagi yaitu untuk memunculkan semua karakteristik setiap golongan darah. Caranya yaitu:


tipeO:-bloodtype(type_O,Forename,Surname,Forename1,Surname1),write('The characteristic are: '),nl,write(Forename),nl,write(Surname),nl,write('when its dropped'),write(' by Anti A'),write(' ,its will be '),write(Forename1),nl,write('when its dropped'),write(' by Anti B'),write(' ,its will be '),write(Surname1),nl,fail.

tipeO.


NB: forgive us if this rule too complicated to make you understand.

Cara ini juga bisa dilakukan pada setiap golongan darah namun harus merubah persyaratannya terlebih dahulu. Untuk rule selengkapnya, lihat gambar di bawah ini:



Then, setelah kita menyimpan semua rule yang telah kita kerjakan menjadi satu di notepad dengan format .pl atau prolog, maka kita dapat melihat tampilan program kita dalam SWI Prolog sebagai berikut:


1. Kondisi Aglutiogen



NB: keyword yang digunakan yaitu aglutinogen_(tipe aglutinogen) jika tidak ada maka no_aglutinogen.


2. Kondisi Aglutinin



NB: keyword yang digunakan yaitu aglutinin_(tipe aglutinogen) jika tidak ada maka no_aglutinin.


3.Kondisi saat ditetesi zat Anti A dan zat Anti B



NB: keyword yang digunakan yaitu (kondisi darah saat ditetesi zat antiA)_ (darah kondisi darah saat ditetesi zat antiA) jika tidak menggumpal=not, dan jika menggumpal=clump


4. Menampilkan seluruh karakter yang dimiliki oleh golongan darah



NB: keyword yang digunakan yaitu tipe(jenis golongan darah).


This program inspirated by Logic Programming chapter 6

Senin, 07 Desember 2009

LOOPS

Most conventional programming languages have a looping facility that enables a set of instructions to be executed repeatedly either a fixed number of times or until a given condition is met. In Prolog, this can be done in a variety of ways, using backtracking, recursion, built-in predicates, or a combination of these.

1. Looping a Fixed Number of Times
No such facility is available in Prolog (directly), but a similar effect can be obtained using recursion,

2. Looping Until a Condition Is Satisfied
Again, no such facility is available directly in Prolog, but a similar effect can be obtained in several ways.
1. Recursion
The function of recursion is to read terms entered by the user from the keyboard and output them to the screen, until end is encountered. This recursive program repeatedly prompts the user to enter a term until either yes or no is entered.
2. Using the 'repeat' Predicate
Recursion is not always the easiest way to provide the types of looping required in Prolog programs. Another method that is often used is based on the built-in predicate repeat.
The goal repeat does not repeat anything; it merely succeeds whenever it is called. The great value of repeat is that it also succeeds (as many times as necessary) on backtracking. The effect of this, as for any other goal succeeding, is to change the order of evaluating goals from 'right to left' (i.e. backtracking) back to 'left-to-right'. This can be used to create a looping effect.
This program repeatedly prompts the user to enter a term until either yes or no is entered. It is an alternative to the recursive program shown at the end of the previous section. In this case it is debatable whether using repeat is an improvement on using recursion.
Goals to the left of repeat in the body of a clause will never be reached on backtracking. The next program reads a sequence of terms from a specified file and outputs them to the current output stream until the term end is encountered. This program shows how to implement a menu structure which loops back repeatedly to request more input. Entering go at the prompt causes Prolog to output a menu from which the user can choose activities one at a time until option d is chosen. Note that all inputs are terms and so must be followed by a full stop character.
3. Backtracking with Failure
As the name implies, the predicate fail always fails, whether on 'standard' evaluation left-to-right or on backtracking. Advantage can be taken of this, combined with Prolog's automatic backtracking, to search through the database to find all the clauses with a specified property.
1. Searching the Prolog Database
2. Finding Multiple Solutions


This chapter describes how a set of goals can be evaluated repeatedly in Prolog, either a fixed number of times or until a specified condition is met, and how multiple solutions can be arrived at using the technique of 'backtracking with failure'.

LOOPS

LOOPS
The Answer of Exercise 6


1. if we want to define the values of the squares of the integers from N1 to N2 inclusive and test it with N1 = 6 and N2 = 12. So, first we must write the rule or program in notepad. we can use writesquares to find the value of its squares.In our program, we change N1 to A, and N2 to B (but it's not problem).
then, You can see the rule in the picture below:




So, the result will be shown on our SWI Prolog if we write writesquares(6,12). on our SWI Prolog. Lets see the picture below:


2. If we want to define and test a predicate to read in a series of characters input by the user and output all of those before the first new line or ? character. So, first we must write the rule or program in notepad. we can use this rule to solve this problem. You can see the rule in the picture below:


then, the result will be shown if we write go., and then we can write anyword that we want, for the example; we write MatdisHolic on our SWI Prolog. Lets see the picture below:



3. if we want to find the professions of all those in data which have age over 40, So, for the first we must write the rule or program in notepad. we can use rule like in example 6.3.1 to solve this problem. You can see the rule in the picture below:

Notes: we can change "allover40" to other word that we want because its can't make the result will be error.

then, the result will be shown in our SWI Prolog if we write "allover40." before. Lets see the picture below:

Rabu, 25 November 2009

SUMMARY OF CHAPTER 5

SUMMARY OF INPUT AND OUTPUT

1. Outputting Terms
The main built-in predicate provided for outputting terms is write/1. The write/1 predicate takes a single argument, which must be a valid Prolog term. Evaluating the predicate causes the term to be written to the current output stream, which by default is the user's screen. The built-in predicate nl/0, It takes no arguments. Evaluating a nl goal causes a new line to be output to the current output stream. for examples;

?- write(26),nl.
26
yes

2. Inputting Terms
The built-in predicate read/1 is provided to input terms. It takes a single argument, which must be a variable. Evaluating it causes the next term to be read from the current input stream,
which by default is the user's keyboard. When a read goal is evaluated, the input term is unified with the argument variable. If the variable is unbound (which is usually the case) it is bound to the input value. for example;

?- read(X).
: 26.
X = 26

If the argument variable is already bound (which for most users is far more likely to occur by mistake than by design), the goal succeeds if and only if the input term is identical to the previously bound value.

?- X=fred,read(X
: fred.
X = fred

3. Input and Output Using Characters
Although input and output of terms is straightforward, the use of quotes and full stops can be cumbersome and is not always suitable. A much better approach for problems of this kind is to input a character at a time.
All printing characters and many non-printing characters (such as space and tab) have a corresponding ASCII (American Standard Code for Information Interchange) value, which is an integer from 0 to 255. The table below gives the numerical ASCII values corresponding to the main printable characters and some others.

4. Outputting Characters
Characters are output using the built-in predicate put/1. The predicate takes a single argument, which must be a number from 0 to 255 or an expression that evaluates to an integer in that range. Evaluating a put goal causes a single character to be output to the current output stream. This is the character corresponding to the numerical value (ASCII value) of its argument, for example

?- put(97),nl.
a
yes

5. Inputting Characters
The get0 predicate takes a single argument, which must be a variable. Evaluating a get0 goal causes a character to be read from the current input stream. The variable is then unified with the ASCII value of this character. Assuming the argument variable is unbound (which will usually be the case), it is bound to the ASCII value of the input character.

?- get0(N).
: a
N = 97

The get predicate takes a single argument, which must be a variable. The variable is then unified with the ASCII value of this character in the same way as for get0.

6. Input and Output Using Files
Prolog takes all input from the current input stream and writes all output to the current output stream. By default both of these are the stream named user, denoting the user's terminal.
The user may open and close input and output streams associated with any number of named files but there can only be one current input stream and one current output stream at any time. Note that no file can be open for both input and output at the same time (except user) and that the user input and output streams
cannot be closed.


7. file Output: Changing the Current Output Stream
The current output stream can be changed using the tell/1 predicate. This takes a single argument, which is an atom or variable representing a file name. Evaluating a tell goal causes the named file to become the current outputstream.
If the file is not already open, a file with the specified name is first created.

The default current output stream is user, i.e. the user's terminal. This value can be restored either by using the told predicate or by tell(user).
The built-in predicate told/0 takes no arguments. Evaluating a told goal causes the current output file to be closed and the current output stream to be reset to user, i.e. the user's terminal.
The built-in predicate telling/1 takes one argument, which must be a variable and will normally be unbound. Evaluating a telling goal causes the variable to be bound to the name of the current output stream.


8. File Input: Changing the Current Input Stream

The current input stream can be changed using the see/1 predicate. This takes a single argument, which is an atom or variable representing a file name, e.g. see('myfile.txt').
Evaluating a see goal causes the named file to become the current input stream. If the file is not already open it is first opened (for read access only). If it is not possible to open a file with the given name, an error will be generated.
Note that the file corresponding to the previous current input stream remains open when a new current input stream is selected. Only the current input stream can be closed (using the seen predicate described below).
The default current input stream is user, i.e. the user's terminal. This value can be restored either by using the seen predicate or by see(user).
The built-in predicate seen/0 takes no arguments. Evaluating a see goal causes the current input file to be closed and the current input stream to be reset to user, i.e. the user's terminal.
The built-in predicate seeing/1 takes one argument, which must be a variable and will normally be unbound. Evaluating a seeing goal causes the variable to be bound to the name of the current input stream.

11.Reading from Files: End of File

If the end of file is encountered when evaluating the goal read(X), variable X will be bound to the atom end_of_file.


12.Reading from Files: End of Record

Typically the end of a line of input at the user's terminal will be indicated byt the character with ASCII value 13. The end of a record in a file will generally be indicated by two ASCII values: 13 followed by 10. The following program shows how to read in a series of characters from the keyboard and print them out, one per line.

INPUT AND OUTPUT

The Answer of Practical Exercise 5 Page : 82-83.

1. Number 1 is about predicate makelower

if we want to convert any upper case letters to lower case, we must write some rules or program in notepad use predicate makelower in order that we can consult it in SWI Prolog. for the first we can write some rules in notepad like the picture below:


After you write its program,
you must write makelower . and enter character that you want on your SWI Prolog. So, you can see the result will be shown in SWI Prolog like the picture below:


2. Number 2 is about predicate copyterms

if we want to reads all the term in a text file and outputs them as terms to another text file one by one on separates lines, we must use predicate copyterms. for the first we must make a file in notepad for input file and save as with txt. format. we give name "infile" for our file.

second, we must make a new file again in notepad for output file, we give name "outfile" for our file.
third, we must make some rules in notepad like this picture below in order that we can consult it to SWI Prolog.
After you write its program, you must write copyterms(‘infile.txt’,'outfile.txt’) . or enter the file's name of your file on your SWI Prolog. So, you can see the result will be shown in SWI Prolog like the picture below:

3. Number 3 is about predicate readfile

if we want to convert a characters to the ASCII value, we can use predicate readfile. according to question number 3, we must make a file in notepad which we must write:

Giving name of the file "tesla" and save it with .txt fomat.
in order we can consult on SWI Prolog, so we must make some rules in notepad like this picture below:

After you write its program, So, you can see the result will be shown in SWI Prolog like the picture below:


4. Number 4 is about predicate combine


create 2 text files in1.txt and in2.txt, each comprising a number of terms terminated by end. you can see this picture below:

to define a predicate combine that takes names of two input files as its first two arguments and the name of an output file as its third argument. in order that the output file contain the terms in the first input file followed by the terms in the second, you must write some rules using predicate combine in notepad like the picture below:



After you write its program, So, you can see the result will be shown in SWI Prolog like the picture below:



5. Number 5 is about predicate compare

in order that we can reads that two text files term by term and each pair of corresponding terms output a messsage saying that they are the same or different, we must make two text files which contain the same number of terms and the final term in each is end. Look the picture below:


then, we must make some rules in notepad using predicate compare like this picture below in order that we can consult it to SWI Prolog.


After you write its program, So, you can see the result will be shown in SWI Prolog like the picture below:





Slide 4