1 / 18

Prolog 5

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(Animal), write('I guess that the animal is: '), write(Animal), nl, undo. hypothesize(cheetah) :- cheetah, ! .

lynsey
Download Presentation

Prolog 5

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Prolog 5 M.Fatih Amasyalı Web : http://www.ce.yildiz.edu.tr/myfile.php?id=14 E-mail : mfatih@ce.yildiz.edu.tr

  2. Hangisi? go :- 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

  3. 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).

  4. 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), !. ungulate :- mammal, verify(chews_cud). Yeni sentaks: if P then Q else R ( P->Q ; R )

  5. Fatih Sarı’ya teşekkürler

  6. Kitchen_dry Leak_in_bathroom Hall_wet Problem_in_kitchen Bathroom_dry Leak_in_kitchen Window_closed No_water_from_outside No_rain Evde su var? Window ve Kitchen ve Hall Bathroom veya

  7. Sızıntı nerede? leak_in_bathroom :- hall_wet, kitchen_dry. problem_in_kitchen :- hall_wet, bathroom_dry. no_water_from_outside :- window_closed; no_rain. leak_in_kitchen :- problem_in_kitchen, no_water_from_outside. hall_wet. bathroom_dry. window_closed. ?- leak_in_kitchen. Yes Geriye doğru çıkarım

  8. Yeni Sentaks Ö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). is_true(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) ; is_true(P2). 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

  9. Kitchen_dry Leak_in_bathroom Hall_wet Problem_in_kitchen Bathroom_dry Leak_in_kitchen Window_closed No_water_from_outside No_rain ileriye doğru çıkarım :- 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) ; composed_fact(Cond2). :- op(800, fx, if). :- op(700, xfx, then). :- op(300, xfy, or). :- op(200, xfy, and). ?- forward. Derived:problem_in_kitchen Derived:no_water_from_outside Derived:leak_in_kitchen No more facts. Yes ?- forward. No more facts. ?- 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

  10. 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

  11. olasılıklar 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).

  12. Kitchen_dry : 0 0: Leak_in_bathroom:0.8= 0 Hall_wet : 1 1: Problem_in_kitchen : 0.9 = 0.9 Bathroom_dry : 1 0.784 Leak_in_kitchen 0.5 =0.392 Window_closed : 0 0.8 No_water_from_outside: 0.98 = 0.784 No_rain: 0.8 ?- certainty(leak_in_kitchen,H).H = 0.392 ;

  13. 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). 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).

  14. çö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.

  15. Somut (yer kaplar) Canlı (ürer) Memeli (süt verir) İnsan (konuşur) Kedi (kuyruğu var) Erkek Kadın Ev kedisi Kaplan Aslan 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). ?- oz2(D,G). % diye sorsak 2 - 3 üstten miras nasıl alınır mı?

  16. Semantik Ağlar’da Miras alma D = erkek, G = urer ; D = erkek, G = yerkaplar ; D = kadin, G = konusur ; D = kadin, G = sutverir ; D = kadin, G = urer ; D = kadin, G = yerkaplar ; D = evkedisi, G = kuyruguvar ; D = evkedisi, G = sutverir ; D = evkedisi, G = urer ; ?- oz2(D,G). D = somut, G = yerkaplar ; D = canli, G = urer ; D = memeli, G = sutverir ; D = insan, G = konusur ; D = kedigiller, G = kuyruguvar ; D = canli, G = yerkaplar ; D = memeli, G = urer ; D = memeli, G = yerkaplar ; D = insan, G = sutverir ; D = insan, G = urer ; D = insan, G = yerkaplar ; D = kedigiller, G = sutverir ; D = kedigiller, G = urer ; D = kedigiller, G = yerkaplar ; D = erkek, G = konusur ; D = erkek, G = sutverir ; D = evkedisi, G = yerkaplar ; D = kaplan, G = kuyruguvar ; D = kaplan, G = sutverir ; D = kaplan, G = urer ; D = kaplan, G = yerkaplar ; D = aslan, G = kuyruguvar ; D = aslan, G = sutverir ; D = aslan, G = urer ; D = aslan, G = yerkaplar ; false.

  17. Özet • Uzman Sistem • Hangi Hayvan? • Hipotez doğrulama • Sızıntı Nerede? • Geriye / ileriye doğru çıkarım • Olasılıklar • Semantik Ağlar’da Miras

  18. 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

More Related