/* Rolling dice problem in Picat. https://dtai.cs.kuleuven.be/problog/tutorial/basic/03_dice.html """ The following example illustrates the use of a logic program with recursion and lists. We start by rolling the first die. Every roll determines the next die to roll, but we stop if we have used that die before. We query for the possible sequences of rolled dice. We use three-sided dice instead of the regular six-sided ones simply to restrict the number of possible outcomes (and thus inference time). """ Cf my Gamble model gamble_rolling_dice_problem.rkt 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. import util. main => go. /* Here we test some different sizes (2, 5, 8, 11) without and with the observation that the first element in the array is 1. Though there don't seems to be any difference between the two cases (of not observed and observed). [n = 2,observation = false] var : len Probabilities: 3: 0.5051000000000000 2: 0.4949000000000000 mean = 2.5051 [n = 2,observation = true] var : len Probabilities: 3: 0.5020820939916716 2: 0.4979179060083284 mean = 2.50208 [n = 5,observation = false] var : len Probabilities: 3: 0.3191000000000000 4: 0.2942000000000000 2: 0.1940000000000000 5: 0.1531000000000000 6: 0.0396000000000000 mean = 3.5252 [n = 5,observation = true] var : len Probabilities: 3: 0.3297764227642276 4: 0.2840447154471545 2: 0.1854674796747967 5: 0.1620934959349593 6: 0.0386178861788618 mean = 3.53862 [n = 8,observation = false] var : len Probabilities: 4: 0.2491000000000000 3: 0.2160000000000000 5: 0.2065000000000000 6: 0.1290000000000000 2: 0.1223000000000000 7: 0.0575000000000000 8: 0.0173000000000000 9: 0.0023000000000000 mean = 4.2571 [n = 8,observation = true] var : len Probabilities: 4: 0.2410646387832700 3: 0.2197718631178707 5: 0.2098859315589354 2: 0.1209125475285171 6: 0.1193916349809886 7: 0.0646387832699620 8: 0.0220532319391635 9: 0.0022813688212928 mean = 4.28061 [n = 11,observation = false] var : len Probabilities: 4: 0.2005000000000000 5: 0.1975000000000000 3: 0.1652000000000000 6: 0.1531000000000000 7: 0.0983000000000000 2: 0.0976000000000000 8: 0.0515000000000000 9: 0.0269000000000000 10: 0.0074000000000000 11: 0.0020000000000000 mean = 4.8371 [n = 11,observation = true] var : len Probabilities: 5: 0.2014051522248244 4: 0.1873536299765808 3: 0.1697892271662763 6: 0.1451990632318501 7: 0.1018735362997658 2: 0.0936768149882904 8: 0.0620608899297424 9: 0.0327868852459016 11: 0.0035128805620609 10: 0.0023419203747073 mean = 4.8911 */ go ?=> member(N,2..3..11), member(Obs,[false,true]), println([n=N,observation=Obs]), reset_store, run_model(10_000,$model(N,Obs),[show_probs_trunc,mean]), nl, % show_store_lengths, fail, nl. go => true. roll(A,N) = Ret => if A.len != A.remove_dups.len then Ret = A else Ret = roll(A++[random_integer1(N)],N) end. model(N,Obs) => A = roll([],N), if Obs == true then observe(A[1] == 1), if observed_ok then add("len",A.len) end else add("len",A.len) end.