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: