《用Prolog建專家系統》學習筆記(3)

來源:互聯網
上載者:User
三、後向鏈非確定性推理現實世界中,事物往往不是非此即彼的簡單邏輯關係,而是與專家的經驗直覺相關的非確定性關係。以下,介紹一個後向鏈非確定性推理的專家系統,Clam,它有自己獨特的規則格式和推理機。(一)信賴度(Certainty Factors)
處理非確定性問題的最常用的辦法,是給系統中的每條資訊加上信賴度。推理機自動更新維護信賴度,並將其作為推理產生的結果。Clam 的一個用例:信賴度(cf)取值-100(全假)至+100(全真)。以下是Clam中的小型知識庫,用於診斷汽車為何不點火啟動。它描述了處理非確定性一些方法。goal problem.rule 1if not turn_over and battery_badthen problem is battery.rule 2if lights_weakthen battery_bad cf 50.rule 3if radio_weakthen battery_bad cf 50.rule 4if turn_over and smell_gasthen problem is flooded cf 80.rule 5if turn_over and gas_gauge is emptythen problem is out_of_gas cf 90.rule 6if turn_over and gas_gauge is lowthen problem is out_of_gas cf 30.ask turn_overmenu (yes no)prompt 'Does the engine turn over?'.ask lights_weakmenu (yes no)prompt 'Are the lights weak?'.ask radio_weakmenu (yes no)prompt 'Is the radio weak?'.ask smell_gasmenu (yes no)prompt 'Do you smell gas?'.ask gas_gaugemenu (empty low full)prompt 'What does the gas gauge say?'.非確定性規則以下是使用者與汽車專家系統的諮詢對話:consult, restart, load, list, trace, how, exit:consultDoes the engine turn over?: yesDo you smell gas?: yesWhat does the gas gauge say?emptylowfull: emptyproblem-out_of_gas-cf-90problem-flooded-cf-80done with problem注意,這個推理機不像Prolog那樣只管找出一個答案,而是找出全部合理答案,並且報告答案的可信程度。可見,信賴度不是機率值,而是簡單地對各個答案的可信性給出一個權重。使用者定義的信賴度,可以置入系統::consultDoes the engine turn over?: yesDo you smell gas?: yes cf 50What does the gas gauge say?emptylowfull: emptyproblem-out_of_gas-cfproblem-flooded-cf-40-90done with problem複合信賴度::consultDoes the engine turn over?: noAre the lights weak?: yesIs the radio weak?: yesproblem-battery-cf-75done with problem該例以2條規則,確定“電池缺電”的信賴度是75。信賴度的用途範圍:● 結論不確定的規則;● 前提不確定的規則;● 使用者輸入的不確定資料;● 前提和結論都不確定的規則;● 用不確定的資訊,更新黑板中的不確定資料;● 將已知的前提設為不確定。(二)MYCIN的信賴度

(三)規則格式
自行建造推理機,必須自行設計內部規則格式。自定規則格式,至少要有2個參數:一個是前提(LHS),另一個是結論(RHS)。為了更實用,再加上第3個參數,規則編號或名稱。於是,完整的規則結構是:rule(Name, LHS, RHS).結論RHS包括一個目標模式及其相應的信賴度CF:rhs(Goal, CF)前提LHS可以包括許多子目標,用於肯定或否定RHS:lhs(GoalList)表示子目標的最簡單方式,是用“屬性-值”配對:av(Attribute, Value)其中的2個參數是簡單的原子。完整的規則結構是這樣的:rule(Name,      lhs( [av(A1, V1), av(A2, V2), ....] ),      rhs( av(Attr, Val), CF) ).上述的規則五(rule 5)是這樣:rule(5,      lhs( [av(turns_over, yes), av(gas_gauge, empty)] ),      rhs( av(problem, flooded), 80) ).(四)推理機
有了固定格式的規則,就需要自造的推理機處理它們。處理行為包括:● 把信賴度綜合起來;● 維護工作資料庫(黑板),更新資訊作為推理所需的新證據;● 找出使用者所需的全部特定資訊,存入黑板。主要的謂詞3.1所示:工作資料庫:fact( av(A, V), CF).找出屬性對應的值,例如:?- findgoal( av(problem, X), CF).該謂詞必須處理3種情況:● 已知的“屬性-值”;● 有謂詞可推斷“屬性-值”;● 必須詢問使用者。可以定義新謂詞:askable(live, 'Where does it live?').要問的問題是live,問題的提示是'Where does it live?'於是,可寫出謂詞findgoal。已知屬性值:findgoal( av(Attr, Val), CF) :- fact( av(Attr, Val), CF), !.向使用者詢問屬性值:findgoal(av(Attr, Val), CF) :- not fact(av(Attr, _), _),                                askable(Attr, Prompt),                                                            query_user(Attr, Prompt),                                !,                                findgoal(av(Attr, Val), CF).謂詞query_user提示使用者,輸入屬性值和信賴度CF,並將其聲明(儲存)為“事實”。遞迴調用findgoal會用到這一事實。query_user(Attr, Prompt) :- write(Prompt),                             read(Val),                             read(CF),                             asserta( fact(av(Attr, Val), CF)).用規則推論出屬性值:findgoal(Goal, CurCF) :- fg(Goal, CurCF).fg(Goal, CurCF) :- rule(N, lhs(IfList),                    rhs(Goal, CF)),                    prove(IfList, Tally),                    adjust(CF, Tally, NewCF),                    update(Goal, NewCF, CurCF),                    CurCF == 100,                   !.fg(Goal, CF) :- fact(Goal, CF).在fg中有3個新謂詞:● prove – 對LHS前提作出證明,並且給出其信賴度CF;● adjust – 合并LHS的CF和RHS的CF;● update – 用新的結論,更新現有工作資料庫的值。prove(IfList, Tally) :- prov(IfList, 100, Tally).prov([], Tally, Tally).prov([H|T], CurTal, Tally) :- findgoal(H, CF),                               min(CurTal, CF, Tal),                               Tal >= 20,                               prov(T, Tal, Tally).min(X, Y, X) :- X =< Y,                !.min(X, Y, Y) :- Y =< X.After prove succeeds, adjust computes the combined CF based on the RHS CF and the Tally from the LHS.成功證明後,調整複合CF值。adjust(CF1, CF2, CF) :- X is CF1 * CF2 / 100,                         int_round(X, CF).int_round(X, I) :- X >= 0,                    I is integer(X + 0.5).int_round(X, I) :- X < 0,                    I is integer(X - 0.5).然後,謂詞update 將已知證據與新證據進行整合。第一個參數是推論得出的“屬性-值”對,第二個參數是其CF。第三個參數合并CF後,返回的新CF值。update(Goal, NewCF, CF) :- fact(Goal, OldCF),                            combine(NewCF, OldCF, CF),                            retract( fact(Goal, OldCF) ),                            asserta( fact(Goal, CF) ),                            !.update(Goal, CF, CF) :- asserta( fact(Goal, CF) ).combine(CF1, CF2, CF) :- CF1 >= 0,                          CF2 >= 0,                          X is CF1 + CF2*(100 - CF1)/100,                          int_round(X, CF).combine(CF1, CF2, CF) :- CF1 < 0,                          CF2 < 0,                          X is - ( -CF1 -CF2 * (100 + CF1)/100),                          int_round(X, CF).combine(CF1, CF2, CF) :- (CF1 < 0; CF2 < 0),                          (CF1 > 0; CF2 > 0),                          abs_minimum(CF1, CF2, MCF),                          X is 100 * (CF1 + CF2) / (100 - MCF),                          int_round(X, CF).表示否定的CF負值:findgoal(not Goal, NCF) :- findgoal(Goal, CF),                            NCF is - CF,                           !.(五)實現Clam外殼super :- repeat,          write('consult, load, exit'), nl,          write(':'),          read_line(X),          doit(X),          X == exit.doit(consult) :- top_goals,                 !.doit(load) :- load_rules,              !.doit(exit).調用top_goals,開始推理:top_goals :- top_goal(Attr),              top(Attr),              print_goal(Attr),              fail.top_goals.top(Attr) :- findgoal(av(Attr, Val), CF),             !.top(_) :- true.print_goal(Attr) :- nl,                     fact(av(Attr, X), CF),                     CF >= 20,                     outp(av(Attr, X), CF),                     nl,                     fail.print_goal(Attr) :- write('done with '),                     write(Attr), nl, nl.outp(av(A, V), CF) :- output(A, V, PrintList),                       write(A-'cf'-CF),                       printlist(PrintList),                      !.outp(av(A, V), CF) :- write(A-V-'cf'-CF).printlist([]).printlist([H|T]) :- write(H),                     printlist(T).(六)英語化的規則load_rules(F) :- clear_db, see(F),                  lod_ruls,                  write('rules loaded'), nl,                  seen,                 !.lod_ruls :- repeat,             read_sentence(L),             process(L),             L == eof.process(eof) :- !.process(L) :- trans(R, L, []),               assertz(R),              !.process(L) :- write('translate error on:'), nl,               write(L), nl.clear_db :- abolish(top_goal, 1),             abolish(askable, 4),             abolish(rule, 3).

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.