用prolog推理信仰网络

时间:2018-06-09 23:09:10

标签: prolog bayesian-networks reasoning

我的Prolog有一个问题,我的最终项目。我尝试使用贝叶斯网络和序言来推断列车系统中断模式。我有贝叶斯网络如下图: Bayesian Network Picture

我读过Ivan Bratko的书籍Prolog Programming for Articial Intellegent 3rd addtion,我发现如何在Prolog中代表贝叶斯网络。 您可以看到Prolog代码如下:

%here is the rule for reasoning in bayesian network from the book :
prob([X|Xs],Cond,P) :- !,
prob(X, Cond, Px),
prob(Xs, [X|Cond], PRest),
P is Px * PRest.

prob([],_,1):- !.

prob(X, Cond, 1) :-
    member(X, Cond),!.



prob(X, Cond, 0) :-
    member(\+ X, Cond), !.

prob(\+ X, Cond, P) :- !,
    prob(X, Cond, P0),
    P is 1-P0.

%Use Bayes rule if condition involves a descendant of X
prob(X, Cond0, P):-
    delete(Y, Cond0, Cond),
    predecessor(X,Y),!,             %Y is a descendant of X
    prob(X, Cond, Px),
    prob(Y, [X|Cond], PyGivenX),
    prob(Y, Cond, Py),
    P is Px * PyGivenX / Py.        %Assuming Py > 0

%Cases when condition does not involves a descendant

prob(X, Cond, P) :-
    p(X, P),!.                      % X a root cause - its probability given

prob(X, Cond, P) :- !,
    findall((CONDi, Pi), p(X,CONDi,Pi), CPlist),        %Condition on parents
    sum_probs(CPlist, Cond, P).

sum_probs([],_,0).
sum_probs([(COND1,P1) | CondsProbs], COND, P) :-
    prob(COND1, COND, PC1),
    sum_probs(CondsProbs, COND, PRest),
    P is P1 * PC1 + PRest.


predecessor(X, \+ Y) :- !,          %Negated variable Y
    predecessor(X,Y).

predecessor(X,Y) :-
    parent(X,Y).

predecessor(X,Z) :-
    parent(X,Y),
    predecessor(Y,Z).

member(X, [X|_]).
member(X, [_|L]) :-
    member(X,L).

delete(X, [X|L], L).
delete(X, [Y|L], [Y|L2]) :-
    delete(X, L, L2). 

这里还介绍了一些prolog中的贝叶斯网络信息的实现(我只添加了一些,因为它太长了):

p(static_inverter, [overhead_line], 0.005050505).
p(static_inverter, [\+ overhead_line], 0.000213767).

p(ac, [static_inverter], 0.5).
p(ac, [\+ static_inverter], 0.029749692).

p(door, [compressor], 0.026315789).
p(door, [\+ compressor], 0.006821).

p(horn, [compressor], 0.026315789).
p(horn, [\+ compressor], 0.000206697).

p(brake, [compressor], 0.026315789).
p(brake, [\+ compressor], 0.004340637).

p(switch, [signal, service_table], 0.5).
p(switch, [\+ signal, service_table], 0.346153846).
p(switch, [signal, \+ service_table], 0.054098361).
p(switch, [\+ signal, \+ service_table], 0.041364933).

p(overhead_line, [fire, fallen_tree], 0.5).
p(overhead_line, [fire, \+ fallen_tree], 0.005882353).
p(overhead_line, [\+ fire, fallen_tree], 0.304878049).
p(overhead_line, [\+ fire, \+ fallen_tree], 0.038850284).

p(pantograph, [overhead_line, fallen_tree], 0.038461538).
p(pantograph, [overhead_line, \+ fallen_tree], 0.002702703).
p(pantograph, [\+ overhead_line, fallen_tree], 0.017241379).
p(pantograph, [\+ overhead_line, \+ fallen_tree], 0.00440955).

您可以在here

上看到的完整代码

不幸的是,当我尝试推理一些概率时,我遇到了问题:

?- prob(series, [horn], P).
?- prob(series, [brake], P).
?- prob(pantograph, [overhead_line], P).

有人说错误是这样的:

ERROR: Arithmetic: evaluation error: `zero_divisor'
ERROR: In:
ERROR:   [27] _43124 is 0.045454539961694*0/0
ERROR:   [25] prob([compressor],[\+brake,traction|...],_43166) at d:/kuliah/tugas/semester 8/for ta/[2] ta program/reasoningtraindisruptionwithprolog/rules.pl:2
ERROR:   [24] sum_probs([(...,0.026315789),...],[\+brake,traction|...],_43216) at d:/kuliah/tugas/semester 8/for ta/[2] ta program/reasoningtraindisruptionwithprolog/rules.pl:37
ERROR:   [22] prob([horn,door|...],[\+brake,traction|...],_43278) at d:/kuliah/tugas/semester 8/for ta/[2] ta program/reasoningtraindisruptionwithprolog/rules.pl:2
ERROR:   [21] prob([\+brake,horn|...],[traction,wiper|...],_43334) at d:/kuliah/tugas/semester 8/for ta/[2] ta program/reasoningtraindisruptionwithprolog/rules.pl:3
ERROR:   [20] prob([traction,...|...],[wiper,speedometer|...],_43390) at d:/kuliah/tugas/semester 8/for ta/[2] ta program/reasoningtraindisruptionwithprolog/rules.pl:3

任何人都可以帮我修复此错误?提前谢谢。

2 个答案:

答案 0 :(得分:1)

介绍安全测试后,

...
prob(Y, Cond, Py),
Py > 0,
P is Px * PyGivenX / Py.        %Assuming Py > 0

并修正了你的github代码中的一个拼写错误和几个singleton warnings,我得到了以下结果:

?- prob(series, [horn], P).
false.

?- prob(series, [brake], P).
P = 0.086661842800551.

?- prob(pantograph, [overhead_line], P).
false.

因此,您现在可以尝试理解为什么代码会产生false而不是P = 0.0 ......

答案 1 :(得分:0)

我意识到为什么解释器使用zero_divisor错误派生回答,因为当我查询时:

?- prob(series, [horn], P).
?- prob(series, [brake], P).
?- prob(pantograph, [overhead_line], P)

所有这些都使用规则处理:

prob(X, Cond0, P):-
    delete(Y, Cond0, Cond),
    predecessor(X,Y),!,             %Y is a descendant of X
    prob(X, Cond, Px),
    prob(Y, [X|Cond], PyGivenX),
    prob(Y, Cond, Py),
    P is Px * PyGivenX / Py.        %Assuming Py > 0

虽然它们应该使用下面的规则进行处理:

prob(X, Cond, P) :-
    p(X, P),!.                      % X a root cause - its probability given

prob(X, Cond, P) :- !,
    findall((CONDi, Pi), p(X,CONDi,Pi), CPlist),        %Condition on parents
    sum_probs(CPlist, Cond, P).

因为查询不涉及X的后代

有什么想法来区分这两个规则吗?因为我以后还会递归地使用这两个规则吗?

我已经尝试在parent(X, Cond0)中添加条件prob(X, Cond0, P),但是当我查询条件涉及X的后代时,答案是错误的