Download - Systemy ekspertowe 2
Shell RETE Postscriptum
Sztuczna Inteligencja i Systemy EkspertoweSystemy ekspertowe (2)
Aleksander Pohlhttp://apohllo.pl/dydaktyka/ai
Wyzsza Szkoła Zarzadzania i Bankowosci
12 maja 2009
Aleksander Pohl WSZiB
Systemy ekspertowe (2)
Shell RETE Postscriptum
Plan prezentacji
Shell
RETE
Postscriptum
Aleksander Pohl WSZiB
Systemy ekspertowe (2)
Shell RETE Postscriptum
Plan prezentacji
Shell
RETE
Postscriptum
Aleksander Pohl WSZiB
Systemy ekspertowe (2)
Shell RETE Postscriptum
Shell ekspertowy
◮ Formalizm reprezentacji wiedzy◮ Opracowanie mechanizmu wnioskowania◮ Interfejs uzytkownika
Aleksander Pohl WSZiB
Systemy ekspertowe (2)
Shell RETE Postscriptum
Reguły
◮ :- op(100, xfx, [has, gives,’does not’, eats, lays, isa]).
◮ :- op(100, xf, [swims, flies]).
◮ rule1 :: ifAnimal has hairorAnimal gives milkthenAnimal isa mammal.
Aleksander Pohl WSZiB
Systemy ekspertowe (2)
Shell RETE Postscriptum
Reguły 2
◮ rule2 :: ifAnimal has feathersorAnimal flies andAnimal lays eggsthenAnimal isa bird.
Aleksander Pohl WSZiB
Systemy ekspertowe (2)
Shell RETE Postscriptum
Reguły 3
◮ rule3 :: ifAnimal isa mammal and( Animal eats meatorAnimal has ’pointed teeth’ andAnimal has claws andAnimal has ’forward pointing eyes’ )thenAnimal isa carnivore.
Aleksander Pohl WSZiB
Systemy ekspertowe (2)
Shell RETE Postscriptum
Reguły 4
◮ rule4 :: ifAnimal isa carnivore andAnimal has ’tawny color’ andAnimal has ’dark spots’thenAnimal isa cheetah.
◮ rule5 :: ifAnimal isa carnivore andAnimal has ’tawny color’ andAnimal has ’black stripes’thenAnimal isa tiger.
Aleksander Pohl WSZiB
Systemy ekspertowe (2)
Shell RETE Postscriptum
Reguły 5
◮ rule6 :: ifAnimal isa bird andAnimal ’does not’ fly andAnimal swimsthenAnimal isa penguin.
◮ rule7 :: ifAnimal isa bird andAnimal isa ’good flyer’thenAnimal isa albatross.
Aleksander Pohl WSZiB
Systemy ekspertowe (2)
Shell RETE Postscriptum
Reguły 6◮ fact :: X isa animal :-member( X, [cheetah, tiger, penguin,albatross]).
◮ askable( _ gives _, ’Animal’ gives ’What’).◮ askable( _ flies, ’Animal’ flies).◮ askable( _ lays eggs, ’Animal’ lays eggs).◮ askable( _ eats _, ’Animal’ eats ’What’).◮ askable( _ has _, ’Animal’ has’Something’).
◮ askable( _ ’does not’ _, ’Animal’ ’doesnot’ fly).
◮ askable( _ swims, ’Animal’ swims).◮ askable( _ isa ’good flyer’, ’Animal’ isa’good flyer’).
Aleksander Pohl WSZiB
Systemy ekspertowe (2)
Shell RETE Postscriptum
Zasady działania
◮ Q jest faktem – to odpowiada ’Q is true’◮ Dla reguły ’if Condition then Q’ badaj Condition i zbuduj
odpowiedz◮ Jesli Q jest „askable” zapytaj o to uzytkownika◮ Jesli Q jest postaci ’Q1 and Q2’ badaj Q1, dla fałszywego
odpowiedz jest ’Q is false’, w przeciwnym razie zbadaj Q2i zbuduj odpowiednio odpowiedz
◮ Jesli Q jest postaci ’Q1 or Q2’ badaj Q1 oraz zbadaj Q2i zbuduj odpowiednio odpowiedz
Aleksander Pohl WSZiB
Systemy ekspertowe (2)
Shell RETE Postscriptum
Dialog (1)
◮ Question please:◮ peter isa tiger◮ Is it true: peter has hair?◮ yes◮ Is it true: peter eats meat?◮ no◮ Is it true: peter has pointed teeth?◮ yes
Aleksander Pohl WSZiB
Systemy ekspertowe (2)
Shell RETE Postscriptum
Dialog (2)
◮ Is it true: peter has claws ?◮ why◮ To investigate by rule3, peter isa carnivore
To investigate by rule5, peter isa tigerThis was your question
◮ Is it true: peter has claws ?◮ yes◮ Is it true: peter has black stripes ?◮ yes◮ Is it true: peter has tawny colour ?◮ yes
Aleksander Pohl WSZiB
Systemy ekspertowe (2)
Shell RETE Postscriptum
Dialog (3)
◮ (peter isa tiger) is true.◮ Would you like to see how ?◮ yes◮ Peter isa tiger
Was derived by rule5 from Peter isa carnivoreAnd
◮ Peter has tawny colourWas toldAnd
◮ Peter has black stripesWas told.
Aleksander Pohl WSZiB
Systemy ekspertowe (2)
Shell RETE Postscriptum
Dialog (4)
◮ Peter isa carnivoreWas derived by rule3 from
◮ Peter isa mammalAnd
◮ Peter has pointed teethWas toldAnd
◮ Peter has clawsWas toldAnd
◮ Peter has forward pointing eyesWas told.
Aleksander Pohl WSZiB
Systemy ekspertowe (2)
Shell RETE Postscriptum
Dialog (5)
◮ Peter isa mammalWas derived by rule1 fromPeter has hairWas told.
Aleksander Pohl WSZiB
Systemy ekspertowe (2)
Shell RETE Postscriptum
Predykaty składowe shella
◮ expert – uruchami shell◮ explore(Goal, Trace, Answer)
Goal – pytanieTrace – lista celów od pytania głównego do obecnegoAnswer – drzewo dowodu
◮ useranswer(Goal, Trace, Answer) – obsługujedialog z uzytkownikiem
◮ present(Answer) – odpowiada na pytanie „How”
Aleksander Pohl WSZiB
Systemy ekspertowe (2)
Shell RETE Postscriptum
Przeszukiwanie
◮ explore(Goal, Trace, Answer)
◮ Goal(X has feathers) or (X flies) and (X layseggs)
◮ Trace (drzewa przodków)[(peter isa carnivore) by rule3, (peter isatiger) by rule5]
◮ Answer (drzewo dowodu potomka)(peter eats meat) is false was told
Aleksander Pohl WSZiB
Systemy ekspertowe (2)
Shell RETE Postscriptum
Implementacja explore (1)◮ explore( Goal, Trace, _) :-copy_term( Goal, Copy),member( Copy by Rule, Trace),instance_of( Copy, Goal), !, fail.
◮ explore(Goal, Trace,Goal is true was ’found as a fact’) :-fact :: Goal.
◮ explore(Goal, Trace, Goal is TruthValuewas ’derived by’ Rule from Answer) :-Rule :: if Condition then Goal,explore(Condition, [Goal by Rule | Trace],Answer),truth(Answer, TruthValue).
Aleksander Pohl WSZiB
Systemy ekspertowe (2)
Shell RETE Postscriptum
Implementacja explore (2)
◮ explore(Goal1 and Goal2, Trace, Answer) :-!,explore(Goal1, Trace, Answer1),continue(Answer1, Goal1 and Goal2, Trace,Answer).
◮ explore( Goal1 or Goal2, Trace, Answer) :-exploreyes(Goal1, Trace, Answer);exploreyes(Goal2, Trace, Answer).
Aleksander Pohl WSZiB
Systemy ekspertowe (2)
Shell RETE Postscriptum
Implementacja explore (3)
◮ explore(Goal1 or Goal2, Trace, Answer1 andAnswer2) :- !,not exploreyes(Goal1, Trace, _),not exploreyes(Goal2, Trace, _),explore(Goal1, Trace, Answer1),explore(Goal2, Trace, Answer2).
◮ explore(Goal, Trace, Goal is Answer wastold) :-useranswer(Goal, Trace, Answer).
◮ exploreyes(Goal, Trace, Answer) :-explore(Goal, Trace, Answer),positive(Answer).
Aleksander Pohl WSZiB
Systemy ekspertowe (2)
Shell RETE Postscriptum
Implementacja
◮ instance_of(Term, Term1) :-copy_term(Term1, Term2),numbervars(Term2, 0, _), !,Term = Term2.
◮ numbervars( Term, N, Nplus1) :-var( Term), !, Term = var/N,Nplus1 is N + 1.
Aleksander Pohl WSZiB
Systemy ekspertowe (2)
Shell RETE Postscriptum
Implementacja cd.
◮ numbervars( Term, N, M) :-Term =.. [Functor | Args],numberargs( Args, N, M).
◮ numberargs( [], N, N) :- !.
◮ numberargs( [X | L], N, M) :-numbervars( X, N, N1),numberargs( L, N1, M).
Aleksander Pohl WSZiB
Systemy ekspertowe (2)
Shell RETE Postscriptum
Implementacja cd.
◮ truth( Question is TruthValue was Found,TruthValue) :- !.
◮ truth( Answer1 and Answer2, TruthValue) :-truth( Answer1, true), truth( Answer2,true), !,TruthValue = true ; TruthValue = false.
◮ positive( Answer) :-truth( Answer, true).
◮ negative( Answer) :-truth( Answer, false).
Aleksander Pohl WSZiB
Systemy ekspertowe (2)
Shell RETE Postscriptum
Pobieranie odpowiedzi
◮ getreply(R) :-read(Answer),means(Answer, Reply), !;nl, write(’Answer Unknown’),nl,getreply( Reply).
◮ means(yes,yes).
◮ means(y,yes).
◮ ...◮ getreply(Replay),(Reply = yes, interpretyes(...) ;Reply = no, interpretno(...) ;...)
Aleksander Pohl WSZiB
Systemy ekspertowe (2)
Shell RETE Postscriptum
Interpretacja odpowiedzi
◮ askable( X eats Y, ’Animal’ eats’Something’).
◮ ?- useranswer( X eats Y , [] , Answer).
◮ Is it true: Animal eats Something?◮ yes.Animal = peterSomething = meatAnswer = trueX = peterY = meat
Aleksander Pohl WSZiB
Systemy ekspertowe (2)
Shell RETE Postscriptum
Plan prezentacji
Shell
RETE
Postscriptum
Aleksander Pohl WSZiB
Systemy ekspertowe (2)
Shell RETE Postscriptum
RETE
◮ Algorytm pattern-matching rozumowania wprzód◮ C.L. Forgy z Carnegi Mellon w 1979◮ Optymalizacja rozpatyrwania LHS (left-hand side) oraz
RHS (right-hand side)◮ Złozonosc:
◮ R – ilosc reguł◮ F – ilosc faktów◮ P – ilosc dopasowan
naiwna implementacja: O(R ∗ F P)RETE: O(R ∗ F ∗ P)
Aleksander Pohl WSZiB
Systemy ekspertowe (2)
Shell RETE Postscriptum
Pojecia
◮ Kompilacja lewych stron LHS => RHS (if .. then)◮ Zamieniamy reguły na zestawy wezłów z jednym wejsciem
(filtry) i wezłów z dwoma wejsciami (złaczenia)◮ Filtry sprawdzaja czy dany fakt pasuje do elementu LHS◮ Złaczenia zapamietuja fakty, które je czesciowo spełniły
Aleksander Pohl WSZiB
Systemy ekspertowe (2)
Shell RETE Postscriptum
Przykład
◮ if x, y, z then example-2
◮ if x, y then example-3
◮ LHS => RHS◮ (x,y,z) => example-2◮ (x,y) => example-3
Aleksander Pohl WSZiB
Systemy ekspertowe (2)
Shell RETE Postscriptum
Aleksander Pohl WSZiB
Systemy ekspertowe (2)
Shell RETE Postscriptum
Aleksander Pohl WSZiB
Systemy ekspertowe (2)
Shell RETE Postscriptum
Aleksander Pohl WSZiB
Systemy ekspertowe (2)
Shell RETE Postscriptum
Plan prezentacji
Shell
RETE
Postscriptum
Aleksander Pohl WSZiB
Systemy ekspertowe (2)
Shell RETE Postscriptum
Materiały zródłowe
◮ L.Sterling, E.Shapiro - „The Art Of Prolog”◮ Ivan Bratko - „Prolog – Programming For Artificial
Intelligence”◮ Slajdy zostały przygotowane za zgoda
dr. Michała Korzyckiego na podstawie jego wykładu.
Aleksander Pohl WSZiB
Systemy ekspertowe (2)
Shell RETE Postscriptum
Dziekuje!
Aleksander Pohl WSZiB
Systemy ekspertowe (2)