1

I have a problem in Prolog for my final project. I try to reason about train system disruption pattern using bayesian network and prolog. I have bayesian network looks like following figure : Bayesian Network Picture

I read on books Prolog Programming for Articial Intellegent 3rd addtion by Ivan Bratko, and I found how to represent Bayesian Network in Prolog. You can see the Prolog code as follow :

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

Here also some the implementation of the bayesian network information in prolog (I only add some of them because it was too long):

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

for the full code you may see on here

unfortunately I have a problem when I try to reason some probabilities like :

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

It was said the error is something like this :

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

Any one can help me to fix this error? Thanks in advance.

yunitarp
  • 13
  • 4
  • You need to check your math. You're dividing by zero. I don't think anyone is going to go through all of your offline code to find the cause for you. What debugging have you done? – lurker Jun 10 '18 at 02:24
  • I already use trace to debugging, but still not found which part of code that make it dividing by zero. No trace result that indicate it divided with zero number. – yunitarp Jun 10 '18 at 04:23
  • what happens if you just try to patch the offending line, with a test? `Py > 0, P is Px * PyGivenX / Py.` – CapelliC Jun 10 '18 at 05:22

2 Answers2

1

After introducing the safety test,

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

and corrected a typo and several singleton warnings in your github code, I have these results:

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

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

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

So you can now try to understand why the code yields false instead of P = 0.0...

CapelliC
  • 59,646
  • 5
  • 47
  • 90
0

I realize why the intrpreter derive answer with zero_divisor error, it because when I query with :

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

All of them processed with rule :

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

Whereas they should be processed with rule underneath :

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

Because the query not involves descendant of X

Any idea to distinguish both rules? Because I still use both rules recursively later?

I already try adding condition parent(X, Cond0) in prob(X, Cond0, P), but when I query with condition involves a descendant of X, the answer is wrong

yunitarp
  • 13
  • 4
  • I haven't looked at all the code, but maybe you can just add `Py > 0` after the call to `prob/4` where you calculate `Py`. – Tomas By Jun 10 '18 at 13:35
  • Yes, I add Py <> 0 and creat a new predicate prob/4 and it works, thank you very much :) – yunitarp Jun 10 '18 at 14:57