Prolog 5 M.Fatih Amasyalı Web : http://www.ce.yildiz.edu.tr/myfile.php?id=14 E-mail : mfatih@ce.yildiz.edu.tr
Hangisi? go :- hypothesize(cheetah) :- cheetah, !. hypothesize(Animal), write('I guess that the animal is: '), write(Animal), nl, undo. hypothesize(cheetah) :- cheetah, !. hypothesize(tiger) :- tiger, !. hypothesize(giraffe) :- giraffe, !. hypothesize(zebra) :- zebra, !. hypothesize(ostrich) :- ostrich, !. hypothesize(penguin) :- penguin, !. hypothesize(albatross) :- albatross, !. hypothesize(unknown). Birini doğruladığı anda, Animal’in başka değerleri için doğruluk araması yapmasın diye
cheetah :- mammal, carnivore, verify(has_tawny_color), verify(has_dark_spots). tiger :- mammal, carnivore, verify(has_tawny_color), verify(has_black_stripes). giraffe :- ungulate, verify(has_long_neck), verify(has_long_legs). zebra :- ungulate, verify(has_black_stripes). ostrich :- bird, verify(does_not_fly), verify(has_long_neck). penguin :- bird, verify(does_not_fly), verify(swims), verify(is_black_and_white). albatross :- bird, verify(appears_in_story_Ancient_Mariner), verify(flys_well).
Yeni sentaks: if P then Q else R ( P->Q ; R ) ask(Question) :- write('Does the animal have the following attribute: '), write(Question), write('? '), read(Response), nl, ( (Response == yes ; Response == y) -> assert(yes(Question)) ; assert(no(Question)), fail). /*assert yes döner, no(Q) ekleyip, verify’a no dönmesi için*/ :- dynamic yes/1,no/1. /* How to verify something */ verify(S) :- (yes(S) true ; % aynı soruyu önceden sorup cevabını yes olarak almışsa bir daha sormaz ve true döner (no(S) fail ; % cevabını no olarak almışsa bir daha sormaz ve false döner ask(S))). % aynı soruyu sormamışsa sorar /* undo all yes/no assertions */ undo :- retract(yes(_)),fail. /*undo yu yeniden denesin diye.*/ undo :- retract(no(_)),fail. /*undo yu yeniden denesin diye.*/ undo. mammal :- verify(has_hair), !. mammal :- verify(gives_milk). bird :- verify(has_feathers), !. bird :- verify(flys), verify(lays_eggs). carnivore :- verify(eats_meat), !. carnivore :- verify(has_pointed_teeth), verify(has_claws), verify(has_forward_eyes). ungulate :- mammal, verify(has_hooves), !. verify(chews_cud). Yeni sentaks: if P then Q else R ( P->Q ; R )
Fatih Sarı’ya teşekkürler
Evde su var? ve Kitchen ve Hall veya Kitchen_dry Window Hall_wet Bathroom_dry Window_closed No_rain No_water_from_outside Problem_in_kitchen Leak_in_bathroom Leak_in_kitchen Window ve Kitchen ve Hall Bathroom veya
Sızıntı nerede? Geriye doğru çıkarım leak_in_bathroom :- hall_wet, kitchen_dry. problem_in_kitchen :- bathroom_dry. no_water_from_outside :- window_closed; no_rain. leak_in_kitchen :- problem_in_kitchen, no_water_from_outside. hall_wet. window_closed. ?- leak_in_kitchen. Yes Geriye doğru çıkarım
Yeni Sentaks Geriye doğru çıkarım Öncelikle ters orantılı :- op(800, fx, if). :- op(700, xfx, then). :- op(300, xfy, or). :- op(200, xfy, and). is_true(P) :- fact(P). if Condition then P, is_true(Condition). is_true(P1 and P2) :- is_true(P1), is_true(P2). is_true(P1 or P2) :- is_true(P1) ; if hall_wet and kitchen_dry then leak_in_bathroom. if hall_wet and bathroom_dry then problem_in_kitchen. if window_closed or no_rain then no_water_from_outside. if problem_in_kitchen and no_water_from_outside then leak_in_kitchen. fact(hall_wet). fact(bathroom_dry). fact(window_closed). ?- is_true(leak_in_kitchen). Yes Geriye doğru çıkarım
ileriye doğru çıkarım ?- forward. Derived:problem_in_kitchen :- dynamic fact/1. forward :- new_derived_fact(P), write('Derived:'),write(P),nl, assert(fact(P)), forward ; write('No more facts.'). new_derived_fact(Conc) :- if Cond then Conc, not(fact(Conc)), composed_fact(Cond). composed_fact(Cond) :- fact(Cond). composed_fact(Cond1 and Cond2) :- composed_fact(Cond1), composed_fact(Cond2). composed_fact(Cond1 or Cond2) :- composed_fact(Cond1) :- op(800, fx, if). :- op(700, xfx, then). :- op(300, xfy, or). :- op(200, xfy, and). Kitchen_dry Hall_wet Bathroom_dry Window_closed No_rain No_water_from_outside Problem_in_kitchen Leak_in_bathroom Leak_in_kitchen ?- forward. Derived:problem_in_kitchen Derived:no_water_from_outside Derived:leak_in_kitchen No more facts. Yes ?- fact(X). X = hall_wet ; X = bathroom_dry ; X = window_closed ; X = problem_in_kitchen ; X = no_water_from_outside ; X = leak_in_kitchen ; No
olasılıklar if hall_wet and kitchen_dry then leak_in_bathroom:0.8. if hall_wet and bathroom_dry then problem_in_kitchen:0.9. if window_closed or no_rain then no_water_from_outside:0.98. if problem_in_kitchen and no_water_from_outside then leak_in_kitchen:0.5. given(hall_wet,1). % Hall is wet given(bathroom_dry, 1). % Bathroom is dry given(kitchen_dry, 0). % Kitchen is not dry given(no_rain, 0.8). % Probably no rain, but not sure given(window_closed, 0). % Window not closed
olasılıklar certainty(P, Cert) :- given(P, Cert). c(P1 and P2)=min(c(P1),c(P2)) c(P1 or P2)=max(c(P1),c(P2)) certainty(P, Cert) :- given(P, Cert). certainty(Cond1 and Cond2, Cert) :- certainty(Cond1, Cert1), certainty(Cond2, Cert2), min(Cert1, Cert2, Cert). certainty(Cond1 or Cond2, Cert) :- certainty(Cond1, Cert1), certainty(Cond2, Cert2), max(Cert1, Cert2, Cert). certainty(P, Cert) :- if Cond then P : C1, certainty(Cond, C2), Cert is C1 * C2. if P1 then P2:X ise c(P2)=c(P1)*X max( X, Y, Max) :- X >= Y, !, Max = X ; Max = Y. min( X, Y, Min) :- X =< Y, !, Min = X ; Min = Y. :- op(800, fx, if). :- op(700, xfx, then). :- op(300, xfy, or). :- op(200, xfy, and).
?- certainty(leak_in_kitchen,H). H = 0.392 ; Kitchen_dry : 0 Hall_wet : 1 Bathroom_dry : 1 Window_closed : 0 No_rain: 0.8 0.8 No_water_from_outside: 0.98 = 0.784 1: Problem_in_kitchen : 0.9 = 0.9 0: Leak_in_bathroom:0.8= 0 0.784 Leak_in_kitchen 0.5 =0.392
GS vs. FB if gs_nin_kalecisi_mondi then gs_nin_defans_saglam:0.8. if gs_de_necati_oynuyor or gs_de_hakansukur_oynuyor then gs_nin_hucum_saglam:0.9. if fb_de_nobre_oynuyor then fb_nin_defans_saglam:0.7. if fb_de_nobre_oynuyor and fb_de_alex_oynuyor then fb_nin_hucum_saglam:0.9. if gs_nin_defans_saglam and gs_nin_hucum_saglam then gs_yener:0.8. if fb_nin_defans_saglam and fb_nin_hucum_saglam then fb_yener:0.9. given(gs_de_necati_oynuyor,1). given(fb_de_nobre_oynuyor,1). given(fb_de_alex_oynuyor,1). given(gs_de_hakansukur_oynuyor,0). given(gs_nin_kalecisi_mondi,1). certainty(P, Cert) :- given(P, Cert). certainty(Cond1 and Cond2, Cert) :- certainty(Cond1, Cert1), certainty(Cond2, Cert2), min(Cert1, Cert2, Cert). certainty(Cond1 or Cond2, Cert) :- certainty(Cond1, Cert1), certainty(Cond2, Cert2), max(Cert1, Cert2, Cert). certainty(P, Cert) :- if Cond then P : C1, certainty(Cond, C2), Cert is C1 * C2. Yukarıdaki Prolog programı consult edildikten sonra ?-certainty(fb_yener,H). sorusuna prolog “H = 0.63” şeklide cevap vermektedir. a) ?-certainty(fb_nin_defans_saglam,H). b) ?-certainty(gs_nin_hucum_saglam,H). c) ?-certainty(gs_yener,H). GS vs. FB max( X, Y, Max) :- X >= Y, !, Max = X ; Max = Y. min( X, Y, Min) :- X =< Y, !, Min = X ; Min = Y. :- op(800, fx, if). :- op(700, xfx, then). :- op(300, xfy, or). :- op(200, xfy, and).
çözüm a. 0.7 b. 0.9 c. 0.64 if gs_nin_kalecisi_mondi then gs_nin_defans_saglam:0.8. if gs_de_necati_oynuyor or gs_de_hakansukur_oynuyor then gs_nin_hucum_saglam:0.9. if gs_nin_defans_saglam and gs_nin_hucum_saglam then gs_yener:0.8. gs_nin_kalecisi_mondi gs_nin_defans_saglam ( 0.8 ) gs_de_necati_oynuyor (1) gs_de_hakansukur_oynuyor (0) max(1,0)=1 gs_nin_hucum_saglam ( 1*0.9=0.9 ) gs_nin_defans_saglam and gs_nin_hucum_saglam (0.8 and 0.9) min (0.8,0.9)=0.8 gs_yener 0.8*0.8 = 0.64.
Semantik Ağlar’da Miras alma ust(somut,canli). ust(canli,memeli). ust(memeli,insan). ust(memeli,kedigiller). ust(insan,erkek). ust(insan,kadin). ust(kedigiller,evkedisi). ust(kedigiller,kaplan). ust(kedigiller,aslan). oz(somut,yerkaplar). oz(canli,urer). oz(memeli,sutverir). oz(insan,konusur). oz(kedigiller,kuyruguvar). ust2(X,Z) :- ust(X,Z). ust2(X,Z) :- ust(X,Y),ust2(Y,Z). oz2(N,X):-oz(N,X). oz2(N,X):-ust2(Y,N),oz(Y,X). Somut (yer kaplar) Canlı (ürer) Memeli (süt verir) İnsan (konuşur) Kedi (kuyruğu var) Erkek Kadın Ev kedisi Kaplan Aslan ?- oz2(D,G). % diye sorsak 2 - 3 üstten miras nasıl alınır mı?
Semantik Ağlar’da Miras alma ?- oz2(D,G). D = somut, G = yerkaplar ; D = canli, G = urer ; D = memeli, G = sutverir ; D = insan, G = konusur ; D = kedigiller, G = kuyruguvar ; D = memeli, G = yerkaplar ; D = insan, G = sutverir ; G = urer ; D = kedigiller, D = erkek, G = konusur ; D = erkek, G = urer ; G = yerkaplar ; D = kadin, G = konusur ; G = sutverir ; D = evkedisi, G = kuyruguvar ; D = evkedisi, G = yerkaplar ; D = kaplan, G = kuyruguvar ; G = sutverir ; G = urer ; D = aslan, false.
Özet Uzman Sistem Semantik Ağlar’da Miras Hangi Hayvan? Hipotez doğrulama Sızıntı Nerede? Geriye / ileriye doğru çıkarım Olasılıklar Semantik Ağlar’da Miras
Kaynaklar PROLOG Programming for Artificial Intelligence, Bratko, I., 3rd Edition, Addison-Wesley, 2001 http://www.csupomona.edu/~jrfisher/www/prolog_tutorial/2_17.html http://igor.gold.ac.uk/~mas01rk/Teaching/CIS310/Programs/kr.txt