1:- module(test_bddem,
    2  [test_bddem/0]).    3:- use_module(library(plunit)).    4:- use_module(library(bddem)).    5
    6:- rand_seed(100).    7
    8test_bddem:-
    9    run_tests([
   10    prob,em,
   11    sampling,
   12    dtprob
   13    ]).
   14
   15
   16v1_0(Env,R,BDD):-
   17  add_var(Env,[0.4,0.6],R,V),equality(Env,V,0,BDD).
   18
   19v2_0(Env,R,Val,BDD):-
   20  add_var(Env,[0.4,0.3,0.3],R,V),equality(Env,V,Val,BDD).
   21
   22prepare_vars(Env,Rainy,Windy,Umbrella,Raincoat):-
   23  add_var(Env,[0.3,0.7],0,VRainy),
   24  add_var(Env,[0.5,0.5],1,VWindy),
   25  add_decision_var(Env,3,VRaincoat),
   26  add_decision_var(Env,2,VUmbrella),
   27  equality(Env,VRainy,0,Rainy),
   28  equality(Env,VWindy,0,Windy),
   29  equality(Env,VUmbrella,0,Umbrella),
   30  equality(Env,VRaincoat,0,Raincoat).
   31
   32dry(Env,BDDD,Rainy,Windy,Umbrella,Raincoat):-
   33  bdd_not(Env,Rainy,BDDNR),
   34  and(Env,Rainy,Raincoat,BDDRR),
   35  and(Env,Rainy,Umbrella,BDDRU),
   36  broken_umbrella(Env,BDDBU,Rainy,Windy,Umbrella),
   37  bdd_not(Env,BDDBU,BDDNBU),
   38  and(Env,BDDRU,BDDNBU,BDDRUNBU),
   39  or(Env,BDDNR,BDDRR,BDDOR),
   40  or(Env,BDDOR,BDDRUNBU,BDDD).
   41
   42broken_umbrella(Env,BDDBU,Rainy,Windy,Umbrella):-
   43  and(Env,Rainy,Windy,RU),
   44  and(Env,RU,Umbrella,BDDBU).
   45
   46:- begin_tests(prob, []).   47
   48:-ensure_loaded(library(bddem)).   49
   50test(one):-
   51  init(Env),
   52  v1_0(Env,0,BDD),
   53  ret_prob(Env,BDD,P),
   54  end(Env),
   55  P=:=0.4.
   56
   57test(and):-
   58  init(Env),
   59  v1_0(Env,0,BDD1),
   60  v1_0(Env,0,BDD2),
   61  and(Env,BDD1,BDD2,BDD),
   62  ret_prob(Env,BDD,P),
   63  end(Env),
   64  P=:=0.4*0.4.
   65
   66test(or):-
   67  init(Env),
   68  v1_0(Env,0,BDD1),
   69  v1_0(Env,0,BDD2),
   70  or(Env,BDD1,BDD2,BDD),
   71  ret_prob(Env,BDD,P),
   72  end(Env),
   73  P=:=0.4+0.4-0.4*0.4.
   74
   75test(nor):-
   76  init(Env),
   77  v1_0(Env,0,BDD1),
   78  v1_0(Env,0,BDD2),
   79  or(Env,BDD1,BDD2,BDDN),
   80  bdd_not(Env,BDDN,BDD),
   81  ret_prob(Env,BDD,P),
   82  end(Env),
   83  P=:=1-(0.4+0.4-0.4*0.4).
   84
   85
   86
   87:- end_tests(prob).   88
   89:- begin_tests(em, []).   90
   91:-ensure_loaded(library(bddem)).   92
   93test(one):-
   94  init_em(Cont),
   95  ex1(Cont,BDD1),
   96  ex1(Cont,BDD2),
   97  ex2(Cont,BDD3),
   98  em(Cont,[2,2],[[BDD1,1.0],[BDD2,1.0],[BDD3,1.0]],0.0001,0.001,100,LL,Par,ExP),
   99  writeln(LL),
  100  writeln(Par),
  101  writeln(ExP),
  102  end_em(Cont),
  103  abs(LL)<  1e-4.
  104
  105ex1(Cont,BDD):-
  106  init_ex(Cont,Env),
  107  v1_0(Env,0,B0),
  108  v1_0(Env,1,B1),
  109  or(Env,B0,B1,BDD),
  110  end_ex(Cont).
  111
  112ex2(Cont,BDD):-
  113  init_ex(Cont,Env),
  114  v1_0(Env,0,B00),
  115  v1_0(Env,1,B1),
  116  bdd_not(Env,B00,B0),
  117  or(Env,B0,B1,BDD),
  118  end_ex(Cont).
  119
  120test(one_dir):-
  121  init_em(Cont),
  122  ex1(Cont,BDD1),
  123  ex1(Cont,BDD2),
  124  ex2(Cont,BDD3),
  125  initial_values(Cont,1.0),
  126  em(Cont,[2,2],[[BDD1,1.0],[BDD2,1.0],[BDD3,1.0]],0.0001,0.001,100,LL,Par,ExP),
  127  writeln(LL),
  128  writeln(Par),
  129  writeln(ExP),
  130  end_em(Cont),
  131  abs(LL)<  1e-3.
  132
  133test(one_dir1):-
  134  init_em(Cont),
  135  ex1(Cont,BDD1),
  136  ex1(Cont,BDD2),
  137  ex2(Cont,BDD3),
  138  ex3(Cont,BDD4),
  139  ex4(Cont,BDD5),
  140  initial_values(Cont,1.0),
  141  em(Cont,[2,2,3],[[BDD1,1.0],[BDD2,1.0],[BDD3,1.0],[BDD4,1.0],[BDD5,1.0]],0.0001,0.001,100,LL,Par,ExP),
  142  writeln(LL),
  143  writeln(Par),
  144  writeln(ExP),
  145  end_em(Cont),
  146  abs(LL)<  1e-3.
  147
  148ex3(Cont,BDD):-
  149  init_ex(Cont,Env),
  150  v2_0(Env,2,1,B00),
  151  v2_0(Env,2,2,B1),
  152  bdd_not(Env,B00,B0),
  153  or(Env,B0,B1,BDD),
  154  end_ex(Cont).
  155
  156ex4(Cont,BDD):-
  157  init_ex(Cont,Env),
  158  v2_0(Env,2,0,B0),
  159  v2_0(Env,2,2,B1),
  160  or(Env,B0,B1,BDD),
  161  end_ex(Cont).
  162:- end_tests(em).  163
  164:- begin_tests(sampling, []).  165
  166:-ensure_loaded(library(bddem)).  167:- use_module(library(apply)).  168relatively_close_to(V,T,E):-
  169	TLow is T*(1-E),
  170	THigh is T*(1+E),
  171	TLow=<V,
  172	V=<THigh.
  173
  174close_to(V,T):-
  175	epsilon(E),
  176	TLow is T-E,
  177	THigh is T+E,
  178	TLow=<V,
  179	V=<THigh.
  180
  181
  182average([H|T],Av):-
  183  sum_list([H|T],Sum),
  184  length([H|T],N),
  185  Av is Sum/N.
  186
  187variance(L,Av,Var):-
  188  average(L,Av),
  189  maplist(sq_diff(Av),L,LS), 
  190  average(LS,Var).
  191
  192std_dev(L,Av,Dev):-
  193  variance(L,Av,Var),
  194  root(Var,Dev).
  195
  196root(Var,Dev):-
  197  Dev is sqrt(Var).
  198
  199sq_diff(Av,A,S):-
  200  S is (A-Av)^2.
  201
  202is0(0).
  203
  204is1(1).
  205
  206is2(2).
  207
  208is3(3).
  209
  210test(gamma):-
  211  findall(S,(between(1,10000,_),gamma_sample(1,2,S)),V),
  212  variance(V,M,Var),
  213  writeln(mean(M)),
  214  writeln(var(Var)),
  215  relatively_close_to(M,1*2,0.2),
  216  relatively_close_to(Var,1*2*2,0.2).
  217
  218test(gauss):-
  219  findall(S,(between(1,10000,_),gauss_sample(1,2,S)),V),
  220  variance(V,M,Var),
  221  writeln(mean(M)),
  222  writeln(var(Var)),
  223  relatively_close_to(M,1,0.1),
  224  relatively_close_to(Var,2,0.1).
  225
  226
  227test(uniform):-
  228  findall(S,(between(1,10000,_),uniform_sample(S)),V),
  229  variance(V,M,Var),
  230  writeln(mean(M)),
  231  writeln(var(Var)),
  232  relatively_close_to(M,0.5,0.1),
  233  relatively_close_to(Var,1/12,0.1).
  234
  235test(dirichlet):-
  236  findall(S,(between(1,10000,_),dirichlet_sample([1,1,1],S)),_V).
  237
  238test(dirichlet1):-
  239  findall(S,(between(1,10000,_),dirichlet_sample([1,1,1,1],D),discrete_sample(D,S)),V),
  240  check_sample(V).
  241
  242test(dirichlet2):-
  243  findall(S,(between(1,10000,_),dirichlet_sample([2,2,2,2],D),discrete_sample(D,S)),V),
  244  check_sample(V).
  245
  246test(sdirichlet1):-
  247  findall(S,(between(1,10000,_),symmetric_dirichlet_sample(1,4,D),discrete_sample(D,S)),V),
  248  check_sample(V).
  249
  250test(sdirichlet2):-
  251  findall(S,(between(1,10000,_),symmetric_dirichlet_sample(2,4,D),discrete_sample(D,S)),V),
  252  check_sample(V).
  253
  254test(discrete):-
  255  findall(S,(between(1,10000,_),discrete_sample([0.25,0.25,0.25,0.25],S)),V),
  256  check_sample(V).
  257
  258check_sample(V):-
  259  partition(is0,V,L0,_),
  260  partition(is1,V,L1,_),
  261  partition(is2,V,L2,_),
  262  partition(is3,V,L3,_),
  263  length(L0,N0),
  264  length(L1,N1),
  265  length(L2,N2),
  266  length(L3,N3),
  267  writeln(N0),
  268  writeln(N1),
  269  writeln(N2),
  270  writeln(N3),
  271  relatively_close_to(N0,2500,0.1),
  272  relatively_close_to(N1,2500,0.1),
  273  relatively_close_to(N2,2500,0.1),
  274  relatively_close_to(N3,2500,0.1).
  275:- end_tests(sampling).  276
  277
  278:- begin_tests(dtprob, []).  279
  280:-ensure_loaded(library(bddem)).  281
  282test(probabilitdd):-
  283  init(Env),
  284  prepare_vars(Env,Rainy,Windy,Umbrella,Raincoat),
  285  dry(Env,BDDD,Rainy,Windy,Umbrella,Raincoat),
  286  broken_umbrella(Env,BDDBU,Rainy,Windy,Umbrella),
  287  
  288  probability_dd(Env,BDDBU,ADDBU),
  289  add_prod(Env,ADDBU,-40,ADDDUU),
  290
  291  probability_dd(Env,Raincoat,ADDRAIN),
  292  add_prod(Env,ADDRAIN,-20,ADDRAINOUT),
  293  
  294  probability_dd(Env,Umbrella,ADDUMB),
  295  add_prod(Env,ADDUMB,-2,ADDUMBOUT),
  296
  297  probability_dd(Env,BDDD,ADDD), % <- returns ADD from BDD
  298  add_prod(Env,ADDD,60,ADDDU), % <- computes the value utility*ADD
  299  
  300  add_sum(Env,ADDDUU,ADDRAINOUT,AO1),
  301
  302  add_sum(Env,AO1,ADDUMBOUT,AO2),
  303
  304  add_sum(Env,AO2,ADDDU,AO),
  305
  306  ret_strategy(Env,AO,S,C), % <- computes the best strategy
  307  S = [2],
  308  C = 43.0,
  309  writeln(S),
  310  writeln(C),
  311  end(Env).
  312
  313:- end_tests(dtprob).