Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/swipl -f -q
- % This is a PROLOG translation of the example in the book
- % "Artificial Intelligence: A Modern Approach" by Stuart Russell and Peter Norvig
- % Page 512, Figure 14.2
- % It uses Ivan Bratko's interpreter for belief networks, figure 15.11,
- % from the book "Prolog Programming for Artificial Intelligence"
- % Source: http://media.pearsoncmg.com/intl/ema/ema_uk_he_bratko_prolog_3/prolog/ch15/fig15_11.txt
- % That program should be renamed fig15_11.pl and put in the same directory to load properly.
- % Additionally, if you're using a recent version of SWI Prolog, then
- % change the three occurences (on lines 31, 33, 67) of "not X" (or "not Y") in
- % fig15_11.pl to "not(X)" (or "not(Y)" respectively). And change "Cond" on line 49
- % to an underbar "_" to avoid the "Singleton variables" warning.
- :- [fig15_11].
- % Setup the belief network...
- parent(burglary, alarm).
- parent(earthquake, alarm).
- parent(alarm, johncalls).
- parent(alarm, marycalls).
- % Setup probabilities
- p(johncalls, [alarm], 0.90).
- p(johncalls, [not(alarm)], 0.05).
- p(marycalls, [alarm], 0.70).
- p(marycalls, [not(alarm)], 0.01).
- p(alarm, [burglary, earthquake], 0.95).
- p(alarm, [burglary, not(earthquake)], 0.94).
- p(alarm, [not(burglary), earthquake], 0.29).
- p(alarm, [not(burglary), not(earthquake)], 0.001).
- p(burglary, 0.001).
- p(earthquake, 0.002).
- % Run queries...
- :- initialization main.
- main :-
- prob([johncalls, marycalls, alarm, not(burglary), not(earthquake)], [], P1), % Should be 0.000628
- format('Pr(johncalls, maryCalls, alarm, ~~burglary, ~~earthquake) = ~f (should be 0.000628)~n', [P1]),
- prob(burglary, [johncalls, marycalls], P2), % Should be 0.284172
- format('Pr(burglary|johncalls, marycalls) = ~f (should be 0.284172)~n', [P2]),
- halt.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement