Advertisement
krusader74

burglar_alarm.pl

Nov 30th, 2014
836
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Prolog 1.75 KB | None | 0 0
  1. #!/usr/bin/swipl -f -q
  2.  
  3. % This is a PROLOG translation of the example in the book
  4. % "Artificial Intelligence: A Modern Approach" by Stuart Russell and Peter Norvig
  5. % Page 512, Figure 14.2
  6.  
  7. % It uses Ivan Bratko's interpreter for belief networks, figure 15.11,
  8. % from the book "Prolog Programming for Artificial Intelligence"
  9. % Source: http://media.pearsoncmg.com/intl/ema/ema_uk_he_bratko_prolog_3/prolog/ch15/fig15_11.txt
  10. % That program should be renamed fig15_11.pl and put in the same directory to load properly.
  11. % Additionally, if you're using a recent version of SWI Prolog, then
  12. % change the three occurences (on lines 31, 33, 67) of "not X" (or "not Y") in
  13. % fig15_11.pl to "not(X)" (or "not(Y)" respectively). And change "Cond" on line 49
  14. % to an underbar "_" to avoid the "Singleton variables" warning.
  15. :- [fig15_11].
  16.  
  17. % Setup the belief network...
  18. parent(burglary, alarm).
  19. parent(earthquake, alarm).
  20. parent(alarm, johncalls).
  21. parent(alarm, marycalls).
  22.  
  23. % Setup probabilities
  24. p(johncalls, [alarm], 0.90).
  25. p(johncalls, [not(alarm)], 0.05).
  26. p(marycalls, [alarm], 0.70).
  27. p(marycalls, [not(alarm)], 0.01).
  28. p(alarm, [burglary, earthquake], 0.95).
  29. p(alarm, [burglary, not(earthquake)], 0.94).
  30. p(alarm, [not(burglary), earthquake], 0.29).
  31. p(alarm, [not(burglary), not(earthquake)], 0.001).
  32. p(burglary, 0.001).
  33. p(earthquake, 0.002).
  34.  
  35. % Run queries...
  36. :- initialization main.
  37.  
  38. main :-
  39. prob([johncalls, marycalls, alarm, not(burglary), not(earthquake)], [], P1), % Should be 0.000628
  40.   format('Pr(johncalls, maryCalls, alarm, ~~burglary, ~~earthquake) = ~f (should be 0.000628)~n', [P1]),
  41.   prob(burglary, [johncalls, marycalls], P2), % Should be 0.284172
  42.   format('Pr(burglary|johncalls, marycalls) = ~f (should be 0.284172)~n', [P2]),
  43.   halt.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement