/* Alarm in Picat. https://homepages.ecs.vuw.ac.nz/~yimei/resources/COMP307/lec16_Tutorial_GoodExample_BuildingBN.pdf """ Situation: - I'm at work - John calls to say that the in my house alarm went off but Mary (my neighbor) did not call - The alarm will usually be set off by burglars but sometimes it may also go off because of minor earthquakes Variables: Burglary, Earthquake, Alarm, JohnCalls, MaryCalls Network topology reflects causal knowledge: - A burglar can set the alarm off - An earthquake can set the alarm off - The alarm can cause Mary to call - The alarm can cause John to cal """ This program was created by Hakan Kjellerstrand, hakank@gmail.com See also my Picat page: http://www.hakank.org/picat/ */ import ppl_distributions,ppl_utils. main => go. /* This model var : burglary Probabilities: true: 0.7867760123013839 false: 0.2132239876986161 mean = [true = 0.786776,false = 0.213224] var : earthquake Probabilities: false: 0.7721681189133778 true: 0.2278318810866223 mean = [false = 0.772168,true = 0.227832] var : alarm Probabilities: true: 0.7139928241927217 false: 0.2860071758072783 mean = [true = 0.713993,false = 0.286007] var : calls(john) Probabilities: true: 1.0000000000000000 mean = [true = 1.0] var : calls(mary) Probabilities: false: 1.0000000000000000 mean = [false = 1.0] */ go ?=> reset_store, run_model(10_000,$model,[show_probs,mean]), fail, nl. go => true. calls(X,Alarm) = cond(Alarm, flip(0.8),flip(0.1)). model() => Burglary = flip(0.7), Earthquake = flip(0.2), Alarm = cond( (Burglary, Earthquake), flip(0.9), cond( (Burglary, not Earthquake), flip(0.8), cond( (not Burglary, Earthquake), flip(0.1), false))), CallsJohn = calls(john,Alarm), CallsMary = calls(mary,Alarm), observe(CallsJohn, not CallsMary), if observed_ok then add("burglary",Burglary), add("earthquake",Earthquake), add("alarm",Alarm), add("calls(john)",CallsJohn), add("calls(mary)",CallsMary), end. go2 ?=> reset_store, run_model(10_000,$model2,[show_probs,mean]), fail, nl. go2 => true. calls(X,Alarm) = cond(Alarm, flip(0.8),flip(0.1)). model() => Burglary = flip(0.7), Earthquake = flip(0.2), Alarm = cond( (Burglary, Earthquake), flip(0.9), cond( (Burglary, not Earthquake), flip(0.8), cond( (not Burglary, Earthquake), flip(0.1), false))), CallsJohn = calls(john,Alarm), CallsMary = calls(mary,Alarm), observe(CallsJohn, not CallsMary), if observed_ok then add("burglary",Burglary), add("earthquake",Earthquake), add("alarm",Alarm), add("calls(john)",CallsJohn), add("calls(mary)",CallsMary), end.