-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbddem.c
4055 lines (3515 loc) · 101 KB
/
bddem.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
EMBLEM and SLIPCASE
Copyright (c) 2013, Fabrizio Riguzzi and Elena Bellodi
This package uses the library cudd, see http://vlsi.colorado.edu/~fabio/CUDD/
for the relative license.
*/
#include <math.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "cudd.h"
#include <SWI-Prolog.h>
#include <unistd.h>
#include <sys/types.h>
#ifdef _WIN32
#include <Windows.h>
#endif
#define BUFSIZE 200000
#define LOGZERO log(0.01)
#define CACHE_SLOTS 1
#define UNIQUE_SLOTS 1
#define RETURN_IF_FAIL if (ret!=TRUE) return ret;
#define MINUS_INF -100000
#define DEBUG 0
typedef struct
{
int nVal,nRule;
int firstBoolVar;
int abducible;
int query;
int decision;
} variable;
typedef struct
{
DdNode *key;
double value;
} rowel;
typedef struct
{
int cnt;
rowel *row;
} tablerow;
typedef struct environment
{
DdManager * mgr; //Cudd manager
int * bVar2mVar; //array that maps Boolean vars to multi-valued vars
variable * vars; // multivalued variables
int nVars; // number of multivalued variables
double * probs; // probabilities of Boolean variables
int boolVars; // number of Boolean variables
int nRules; // number of rules
int * rules; // array with the number of head atoms for each rule
int n_abd;
int n_abd_boolVars;
} environment;
typedef struct
{
environment * env; // one environment for each example
int ex; // number of examples
double * sigma; // sigma array for taking into account deleted paths
double ***eta; // eta array: for each rule, each Bool var stores two doubles
double ***eta_temp; // eta array storing the contribution of the current example
int * rules; // array with the number of head atoms for each rule
int * tunable_rules; // array with 1 if the parameters of the rule are tunable, 0 otherwise
int nRules; // number of rules
double **arrayprob; //value of paramters. One value ofr each rule and Bool var
double * nodes_probs;
tablerow * nodesB; // tables of probabilities for nodes in Backward step
tablerow * nodesFE; // tables of probabilities for nodes in Forward step
tablerow * nodesFO; // tables of probabilities for nodes in Forward step
double * example_prob; // probability (frequency) of examples in the data
double alpha; // type of parameter initialization in EM:
// 0 for truncated Dirichlet process
// >0 for symmetric Dirichlet distribution with values alpha
} example_data;
typedef struct
{
int var,val;
} assign;
typedef struct explan
{
assign a; // for abduction, an int is sufficient, since there are only 1 in a->val
struct explan * next;
} explan_t;
typedef struct
{
double prob;
explan_t * mpa;
} prob_abd_expl;
typedef struct
{
int n_elements; // quanti elementi ci sono nella lista
double prob;
explan_t **mpa; // lista di liste
} prob_abd_expl_list;
typedef struct
{
DdNode *node;
int comp;
} explkey;
typedef struct
{
explkey key;
prob_abd_expl value;
} explrowel;
typedef struct
{
explkey key;
prob_abd_expl_list value;
} explrowel_abd;
typedef struct
{
int cnt;
explrowel *row;
} expltablerow;
typedef struct
{
int cnt;
explrowel_abd *row;
} expltablerow_abd;
// structure representing
// the root of the add and
// the min and max terminal
typedef struct {
DdNode *root;
double impact;
} node_impact;
static foreign_t ret_prob(term_t,term_t,term_t);
static foreign_t ret_abd_prob(term_t,term_t,term_t,term_t);
static foreign_t ret_map_prob(term_t,term_t,term_t,term_t);
double Prob(DdNode *node,environment *env,tablerow *table);
// prob_abd_expl abd_Prob(DdNode *node,environment *env,expltablerow expltable, tablerow *table, int comp_par);
prob_abd_expl map_Prob(DdNode *node, environment * env,
expltablerow * maptable, tablerow * table,
int comp_par);
static foreign_t end_ex(term_t);
static foreign_t init(term_t);
static foreign_t end(term_t arg1);
static foreign_t init_ex(term_t arg1, term_t arg2);
static foreign_t add_var(term_t,term_t,term_t,term_t);
static foreign_t add_query_var(term_t,term_t,term_t,term_t);
static foreign_t add_abd_var(term_t,term_t,term_t,term_t);
static foreign_t init_em(term_t);
static foreign_t end_em(term_t);
static foreign_t EM(term_t,term_t,term_t,term_t,
term_t,term_t,term_t,term_t,term_t);
static foreign_t reorder(term_t arg1);
static foreign_t make_query_var(term_t arg1, term_t arg2, term_t arg3);
static foreign_t init_par(example_data * ex_d, term_t ruleHeadsTerm);
double ProbPath(example_data * ex_d,DdNode *node, int nex);
//static int rec_deref(void);
void Forward(example_data * ex_d,DdNode *node, int nex);
void UpdateForward(example_data * ex_d,DdNode * node, int nex,
DdNode *** nodesToVisit,int * NnodesToVisit);
double GetOutsideExpe(example_data *ex_d,DdNode *root,double ex_prob, int nex);
void Maximization(example_data * ex_d);
static double Expectation(example_data *ex_d,DdNode **nodes_ex, int lenNodes);
int reorder_int(environment *env);
FILE *open_file(char *filename, const char *mode);
tablerow* init_table(int varcnt);
double * get_value(tablerow *tab, DdNode *node);
void add_or_replace_node(tablerow *tab, DdNode *node, double value);
void add_node(tablerow *tab, DdNode *node, double value);
void destroy_table(tablerow *tab,int varcnt);
expltablerow* expl_init_table(int varcnt);
prob_abd_expl * expl_get_value(expltablerow *tab, DdNode *node, int comp);
void expl_add_node(expltablerow *tab, DdNode *node, int comp, prob_abd_expl value);
void expl_destroy_table(expltablerow *tab,int varcnt);
DdNode* get_node(DdNode *node,tablerow *tab);
install_t install(void);
void write_dot(environment * env, DdNode * bdd, FILE * file);
explan_t * insert(assign assignment,explan_t * head);
explan_t * duplicate(explan_t * head);
void free_list(explan_t * head);
term_t clist_to_pllist(explan_t *mpa, environment * env);
term_t abd_clist_to_pllist(explan_t *mpa);
double uniform_sample();
double gauss_sample(double mean,double var);
double gamma_sample(double shape, double scale);
double gamma_sample_gt1(double shape);
void dirichlet_sample(double * alpha,int K, double * theta);
void symmetric_dirichlet_sample(double alpha,int K, double * theta);
static foreign_t gamma_sample_pl(term_t arg1,term_t arg2,term_t arg3);
static foreign_t gauss_sample_pl(term_t arg1,term_t arg2,term_t arg3);
static foreign_t uniform_sample_pl(term_t arg1);
static foreign_t dirichlet_sample_pl(term_t arg1,term_t arg2);
static foreign_t symmetric_dirichlet_sample_pl(term_t arg1,term_t arg2, term_t arg3);
static foreign_t discrete_sample_pl(term_t arg1,term_t arg2);
static foreign_t initial_values_pl(term_t arg1, term_t arg2);
DdNode* Probability_dd(environment *env, DdNode *current_node, tablerow *table);
DdNode* Probability_dd_bdd(environment *env, DdNode *current_node);
void traverse_tree(DdNode *node, DdNode **bestNode, int *index, double *value);
void traverse_tree_depth_bound(DdNode *node, DdNode **bestNode, int *index, double *value, int current_lv, int max_lv, int precise);
int find_path(DdNode *node, double value, int **array, int *len);
void debug_cudd_env(environment *env, int i);
void dump_var(variable *var);
void dump_env(environment *env);
int compare_utils(const void *a, const void *b);
DdNode *setLowerBound(DdManager *dd, DdNode *current_node, double lowerBound);
static foreign_t add_decision_var(term_t env_ref, term_t rule,term_t var_out);
static foreign_t probability_dd(term_t env_ref, term_t bdd_ref, term_t add_out);
static foreign_t add_prod(term_t env_ref, term_t add_in, term_t value, term_t add_out);
static foreign_t add_sum(term_t env_ref, term_t add_A, term_t add_B, term_t add_out);
static foreign_t ret_strategy(term_t env_ref, term_t add_A, term_t strategy_list, term_t cost);
static foreign_t compute_best_strategy(term_t env_ref, term_t b_list, term_t u_list, term_t strategy_list, term_t cost);
static term_t debug_cudd_var(term_t env_ref, term_t out_null);
void print_prob_abd_expl(prob_abd_expl *pae);
void print_abd_explan(explan_t *et);
int in_list(explan_t *list, assign element);
explan_t *merge_explain(explan_t *a, explan_t *b);
prob_abd_expl_list abd_Prob(DdNode *node, environment *env, expltablerow_abd *expltable, tablerow *table, int comp_par);
void print_prob_abd_expl_list(const prob_abd_expl_list *pael);
void expl_destroy_table_abd(expltablerow_abd *tab,int varcnt);
prob_abd_expl_list * expl_get_value_abd(expltablerow_abd *tab, DdNode *node, int comp);
expltablerow_abd *expl_init_table_abd(int varcnt);
explan_t ** insert_abd(assign assignment,explan_t ** head, int n_elements);
void expl_add_node_abd(expltablerow_abd *tab, DdNode *node, int comp, prob_abd_expl_list value);
explan_t **split_explan(assign assignment, explan_t **head_true, explan_t **head_false, int n_true, int n_false, int *n_new_elements, int no_false);
explan_t *insord_abd(assign assignment, explan_t *head);
explan_t **insert_abd_ordered(assign assignment, explan_t **head, int n_elements);
explan_t **split_explan_v2(assign assignment, explan_t **head_true, explan_t **head_false, int *n_true, int *n_false, int *n_new_elements);
int is_subset(explan_t *e01, explan_t *e02);
static foreign_t uniform_sample_pl(term_t arg1)
{
double sample;
int ret;
term_t out;
sample=uniform_sample();
out=PL_new_term_ref();
ret=PL_put_float(out,sample);
RETURN_IF_FAIL
return PL_unify(out,arg1);
}
double uniform_sample()
{
return ((double)rand())/RAND_MAX;
}
static foreign_t gauss_sample_pl(term_t arg1,term_t arg2,term_t arg3)
{
double mean, var, sample;
int ret;
term_t out;
ret=PL_get_float(arg1,&mean);
RETURN_IF_FAIL
ret=PL_get_float(arg2,&var);
RETURN_IF_FAIL
sample=gauss_sample(mean,var);
out=PL_new_term_ref();
ret=PL_put_float(out,sample);
RETURN_IF_FAIL
return PL_unify(out,arg3);
}
double gauss_sample(double mean,double var)
{
double u1,u2,r,theta,s;
u1= uniform_sample();
u2= uniform_sample();
r= sqrt(-2*log(u1));
theta=2*M_PI*u2;
s=r*cos(theta);
return sqrt(var)*s+mean;
}
static foreign_t gamma_sample_pl(term_t arg1,term_t arg2,term_t arg3)
{
double shape, scale, sample;
int ret;
term_t out;
ret=PL_get_float(arg1,&shape);
RETURN_IF_FAIL
ret=PL_get_float(arg2,&scale);
RETURN_IF_FAIL
sample=gamma_sample(shape,scale);
out=PL_new_term_ref();
ret=PL_put_float(out,sample);
RETURN_IF_FAIL
return PL_unify(out,arg3);
}
double gamma_sample(double shape, double scale)
{
double u,s;
if (shape>=1)
return gamma_sample_gt1(shape)*scale;
else
{
u=uniform_sample();
s=gamma_sample_gt1(shape+1);
return pow(s*u,1/shape)*scale;
}
}
double gamma_sample_gt1(double shape)
{
double c,d,x,v,u;
d=shape-1.0/3.0;
c =1.0/sqrt(9.0*d);
do
{
do
{
x=gauss_sample(0.0,1.0);
v=pow(1+c*x,3);
} while (v<=0);
u=uniform_sample();
} while (u>=1-0.0331*pow(x,4) && log(u)>=0.5*pow(x,2)+d*(1-v+log(v)));
return d*v;
}
static foreign_t symmetric_dirichlet_sample_pl(term_t arg1,term_t arg2, term_t arg3)
{
double alpha, * sample;
int ret, i, K;
term_t out, head;
head=PL_new_term_ref();
out=PL_new_term_ref();
ret=PL_get_integer(arg2,&K);
RETURN_IF_FAIL
sample=malloc(sizeof(double)*K);
ret=PL_get_float(arg1,&alpha);
RETURN_IF_FAIL
symmetric_dirichlet_sample(alpha,K,sample);
ret=PL_put_nil(out);
RETURN_IF_FAIL
for (i=0;i<K;i++)
{
ret=PL_put_float(head,sample[i]);
RETURN_IF_FAIL
ret=PL_cons_list(out,head,out);
RETURN_IF_FAIL
}
return PL_unify(out,arg3);
}
static foreign_t dirichlet_sample_pl(term_t arg1,term_t arg2)
{
double * alpha, * sample;
int ret, i;
size_t K;
term_t out,alphaterm, head;
head=PL_new_term_ref();
out=PL_new_term_ref();
alphaterm=PL_copy_term_ref(arg1);
ret=PL_skip_list(alphaterm,0,&K);
if (ret!=PL_LIST) return FALSE;
alpha=malloc(sizeof(double)*K);
sample=malloc(sizeof(double)*K);
for (i=0;i<K;i++)
{
ret=PL_get_list(alphaterm,head,alphaterm);
RETURN_IF_FAIL
ret=PL_get_float(head,&alpha[i]);
RETURN_IF_FAIL
}
dirichlet_sample(alpha,K,sample);
ret=PL_put_nil(out);
RETURN_IF_FAIL
for (i=0;i<K;i++)
{
ret=PL_put_float(head,sample[i]);
RETURN_IF_FAIL
ret=PL_cons_list(out,head,out);
RETURN_IF_FAIL
}
return PL_unify(out,arg2);
}
static foreign_t discrete_sample_pl(term_t arg1,term_t arg2)
{
double * theta;
double u, p;
int ret, i;
size_t K;
term_t out,thetaterm, head;
head=PL_new_term_ref();
out=PL_new_term_ref();
thetaterm=PL_copy_term_ref(arg1);
ret=PL_skip_list(thetaterm,0,&K);
if (ret!=PL_LIST) return FALSE;
theta=malloc(sizeof(double)*K);
for (i=0;i<K;i++)
{
ret=PL_get_list(thetaterm,head,thetaterm);
RETURN_IF_FAIL
ret=PL_get_float(head,&theta[i]);
RETURN_IF_FAIL
}
u=uniform_sample();
i=0;
p=theta[0];
while (u>p && i<K)
{
i++;
p=p+theta[i];
}
ret=PL_put_integer(out,i);
RETURN_IF_FAIL
free(theta);
return PL_unify(out,arg2);
}
void symmetric_dirichlet_sample(double alpha,int K, double * theta)
{
int i;
double * alphas;
alphas=malloc(sizeof(double)*K);
for (i=0;i<K;i++)
alphas[i]=alpha;
dirichlet_sample(alphas,K,theta);
free(alphas);
}
void dirichlet_sample(double * alpha,int K, double * theta)
{
int i;
double sum;
double * gamma;
gamma=malloc(sizeof(double)*K);
sum=0.0;
for (i=0;i<K;i++)
{
gamma[i]=gamma_sample(alpha[i],1.0);
sum=sum+gamma[i];
}
for (i=0;i<K;i++)
theta[i]=gamma[i]/sum;
free(gamma);
}
static foreign_t init_em(term_t arg1)
{
int ret;
example_data * ex_d;
term_t ex_d_t=PL_new_term_ref();
ex_d=(example_data *)malloc(sizeof(example_data));
ex_d->ex=0;
ex_d->nRules=0;
ex_d->env=NULL;
ex_d->eta=NULL;
ex_d->eta_temp=NULL;
ex_d->rules=NULL;
ex_d->nodes_probs=NULL;
ex_d->tunable_rules=NULL;
ex_d->arrayprob=NULL;
ex_d->alpha=0.0;
ret=PL_put_pointer(ex_d_t,(void *)ex_d);
RETURN_IF_FAIL
return(PL_unify(ex_d_t,arg1));
}
static foreign_t initial_values_pl(term_t arg1, term_t arg2)
{
example_data * ex_d;
int ret;
ret=PL_get_pointer(arg1,(void **)&ex_d);
RETURN_IF_FAIL
ret=PL_get_float(arg2,&(ex_d->alpha)); // <- MOD arg1 -> arg2
RETURN_IF_FAIL
PL_succeed;
}
static foreign_t init_ex(term_t arg1, term_t arg2)
{
example_data * ex_d;
DdManager * mgr;
term_t env_t;
int ex,ret;
env_t=PL_new_term_ref();
ret=PL_get_pointer(arg1,(void **)&ex_d);
RETURN_IF_FAIL
ex=ex_d->ex;
ex_d->env=(environment *) realloc(ex_d->env, (ex+1)*sizeof(environment));
ex_d->env[ex].mgr=Cudd_Init(0,0,UNIQUE_SLOTS,CACHE_SLOTS,5120);
mgr=ex_d->env[ex].mgr;
// Cudd_AutodynEnable(mgr, CUDD_REORDER_GROUP_SIFT);
Cudd_SetMaxCacheHard(mgr, 0);
Cudd_SetLooseUpTo(mgr, 0);
Cudd_SetMinHit(mgr, 15);
ex_d->env[ex].bVar2mVar=NULL;
ex_d->env[ex].vars=NULL;
ex_d->env[ex].nVars=0;
ex_d->env[ex].probs=NULL;
ex_d->env[ex].boolVars=0;
ex_d->env[ex].nRules=ex_d->nRules;
ex_d->env[ex].rules=ex_d->rules;
ret=PL_put_pointer(env_t,(void *) (ex_d->env+ex));
RETURN_IF_FAIL
return(PL_unify(env_t,arg2));
}
static foreign_t end_ex(term_t arg1)
{
int ret;
example_data *ex_d;
ret=PL_get_pointer(arg1,(void **)&ex_d);
RETURN_IF_FAIL
ex_d->ex=ex_d->ex+1;
PL_succeed;
}
static foreign_t init(term_t arg1)
{
term_t env_t;
environment * env;
int ret;
env_t=PL_new_term_ref();
env=(environment *)malloc(sizeof(environment));
// env->mgr=Cudd_Init(0,0,UNIQUE_SLOTS,CACHE_SLOTS,0);
env->mgr=Cudd_Init(0,0,CUDD_UNIQUE_SLOTS,CACHE_SLOTS,0);
Cudd_AutodynEnable(env->mgr, CUDD_REORDER_GROUP_SIFT);
Cudd_SetMaxCacheHard(env->mgr, 0);
Cudd_SetLooseUpTo(env->mgr, 0);
Cudd_SetMinHit(env->mgr, 15);
// Cudd_SetMaxMemory(env->mgr,8000000);
env->bVar2mVar=NULL;
env->vars=NULL;
env->nVars=0;
env->probs=NULL;
env->boolVars=0;
env->nRules=0;
env->rules= NULL;
env->n_abd=0;
env->n_abd_boolVars=0;
ret=PL_put_pointer(env_t,(void *) env);
RETURN_IF_FAIL
return(PL_unify(env_t,arg1));
}
static foreign_t end(term_t arg1)
{
int ret;
environment *env;
ret=PL_get_pointer(arg1,(void **)&env);
RETURN_IF_FAIL
// printf("Pre - Cudd_ReadMemoryInUse(env->mgr) = %ld bytes\n",Cudd_ReadMemoryInUse(env->mgr));
// printf("Cudd_CheckZeroRef(env->mgr) = %d\n",Cudd_CheckZeroRef(env->mgr));
// debug_cudd_env(env,0);
Cudd_Quit(env->mgr);
free(env->bVar2mVar);
free(env->vars);
free(env->probs);
free(env->rules);
free(env);
PL_succeed;
}
static double Expectation(example_data * ex_d,DdNode **nodes_ex,int lenNodes)
{
int i;
double rootProb,CLL=0;
for(i=0;i<lenNodes;i++)
{
if (!Cudd_IsConstant(nodes_ex[i]))
{
ex_d->nodesB=init_table(ex_d->env[i].boolVars);
ex_d->nodesFE=init_table(ex_d->env[i].boolVars);
ex_d->nodesFO=init_table(ex_d->env[i].boolVars);
Forward(ex_d,nodes_ex[i],i);
rootProb=GetOutsideExpe(ex_d,nodes_ex[i],ex_d->example_prob[i],i);
if (rootProb<=0.0)
CLL = CLL + LOGZERO*ex_d->example_prob[i];
else
CLL = CLL + log(rootProb)*ex_d->example_prob[i];
ex_d->nodes_probs[i]=rootProb;
destroy_table(ex_d->nodesB,ex_d->env[i].boolVars);
destroy_table(ex_d->nodesFE,ex_d->env[i].boolVars);
destroy_table(ex_d->nodesFO,ex_d->env[i].boolVars);
}
else
if (nodes_ex[i]==Cudd_ReadLogicZero(ex_d->env[i].mgr))
{
CLL=CLL+LOGZERO*ex_d->example_prob[i];
ex_d->nodes_probs[i]=0.0;
}
else
ex_d->nodes_probs[i]=1.0;
}
return CLL;
}
static foreign_t end_em(term_t arg1)
{
int r,i,ret;
example_data * ex_d;
ret=PL_get_pointer(arg1,(void **)&ex_d);
RETURN_IF_FAIL
for (i=0;i<ex_d->ex;i++)
{
Cudd_Quit(ex_d->env[i].mgr);
free(ex_d->env[i].bVar2mVar);
free(ex_d->env[i].vars);
free(ex_d->env[i].probs);
}
free(ex_d->env);
for (r=0;r<ex_d->nRules;r++)
{
if (ex_d->tunable_rules[r])
{
for (i=0;i<ex_d->rules[r]-1;i++)
{
free(ex_d->eta[r][i]);
free(ex_d->eta_temp[r][i]);
}
free(ex_d->eta[r]);
free(ex_d->eta_temp[r]);
}
}
free(ex_d->eta);
free(ex_d->eta_temp);
free(ex_d->rules);
free(ex_d);
PL_succeed;
}
static foreign_t ret_prob(term_t arg1, term_t arg2, term_t arg3)
{
term_t out;
environment * env;
DdNode * node;
tablerow * table;
double prob;
int ret;
ret=PL_get_pointer(arg1,(void **)&env);
RETURN_IF_FAIL
ret=PL_get_pointer(arg2,(void **)&node);
RETURN_IF_FAIL
out=PL_new_term_ref();
if (!Cudd_IsConstant(node))
{
table=init_table(env->boolVars);
prob=Prob(node,env,table);
if (Cudd_IsComplement(node))
prob=1.0-prob;
ret=PL_put_float(out,prob);
RETURN_IF_FAIL
destroy_table(table,env->boolVars);
}
else
{
if (node==Cudd_ReadOne(env->mgr))
{
ret=PL_put_float(out,1.0);
RETURN_IF_FAIL
}
else
{
ret=PL_put_float(out,0.0);
RETURN_IF_FAIL
}
}
return(PL_unify(out,arg3));
}
int reorder_int(environment *env)
{
int i,j,var_ind,abd_ind=0,ind=env->n_abd_boolVars;
variable var,* vars=env->vars;
DdManager *mgr=env->mgr;
// int boolVars=env->boolVars;
int * permutation;
int * bVar2mVar=env->bVar2mVar;
// permutation=malloc((boolVars)*sizeof(int));
permutation = malloc((Cudd_ReadSize(env->mgr))*sizeof(int));
for (i=0;i<Cudd_ReadSize(env->mgr);i++)
{
j=Cudd_ReadInvPerm(mgr,i);
var_ind=bVar2mVar[j];
var=vars[var_ind];
if (var.abducible || var.query)
{
permutation[abd_ind]=j;
abd_ind++;
}
else
{
permutation[ind]=j;
ind++;
}
}
j = Cudd_ShuffleHeap(mgr,permutation);
free(permutation);
return j;
}
static foreign_t reorder(term_t arg1)
{
environment * env;
int ret;
ret=PL_get_pointer(arg1,(void **)&env);
RETURN_IF_FAIL
ret=reorder_int(env);
RETURN_IF_FAIL
return 1;
}
double Prob_given_expl(DdNode *node, environment *env, tablerow *table, explan_t *expl, int comp_par)
{
int index, comp, pos;
double p, pf, pt, res;
double * value_p;
explan_t *exp = expl; // copia perché scorro
comp = Cudd_IsComplement(node);
comp = (comp && !comp_par) ||(!comp && comp_par);
// printf("Comp: %d\n",comp);
if(Cudd_IsConstant(node)) {
p = 1;
// if (comp)
// p = 1.0 - 1;
// printf("Terminal: %lf\n",p);
return p;
}
index = Cudd_NodeReadIndex(node);
pos = Cudd_ReadPerm(env->mgr,index);
if (pos >= env->n_abd_boolVars)
{
p = Prob(node,env,table);
// if (comp)
// p = 1.0 - p;
// printf("Computed prob: %lf\n",p);
return p;
}
value_p = get_value(table,Cudd_Regular(node));
if (value_p != NULL) {
return *value_p;
}
p = env->probs[index];
// printf("cerco indice: %d che ha prob: %lf\n",index,p);
while(exp != NULL && exp->a.var != index) {
exp = exp->next;
}
pt = 1;
pf = 1;
if(exp != NULL) {
if(exp->a.val == 1) {
// printf("selected true: pt %lf\n",pt);
pt = Prob_given_expl(Cudd_T(node),env,table,expl,comp);
pf = 0;
}
else {
// printf("selected false pf: %lf\n",pf);
pf = Prob_given_expl(Cudd_E(node),env,table,expl,comp);
if(Cudd_IsComplement(Cudd_E(node))) {
pf = 1 - pf;
}
pt = 0;
}
}
else {
// printf("Ass not found - ERRORE\n");
}
// printf("Indice: %d, p: %lf, pf: %lf, pt: %lf\n",index,p,pf,pt);
res = pf*(1-p) + pt*p;
// printf("res: %lf\n",res);
add_node(table,Cudd_Regular(node),res);
return res;
}
static foreign_t ret_abd_prob(term_t arg_env, term_t arg_bdd, term_t arg_prob, term_t arg_expl)
{
term_t out,outass,out_assign;
environment * env;
DdNode * node;
expltablerow_abd * expltable;
tablerow * table;
//abdtablerow * abdtable;
prob_abd_expl_list delta;
// double *prob_ics = NULL;
int ret;
// double p;
// explan_t ** mpa;
int i;
// assign *array_mpa;
ret=PL_get_pointer(arg_env,(void **)&env);
RETURN_IF_FAIL
ret=PL_get_pointer(arg_bdd,(void **)&node);
RETURN_IF_FAIL
// ret=PL_get_pointer(arg_bdd_ic,(void **)&node_ic);
// RETURN_IF_FAIL
out=PL_new_term_ref();
ret=reorder_int(env);
RETURN_IF_FAIL
// array_mpa = malloc(sizeof(assign) * env->boolVars);
if (!Cudd_IsConstant(node))
{
expltable = expl_init_table_abd(env->boolVars);
table=init_table(env->boolVars);
delta.n_elements = 0;
delta.mpa = NULL;
delta.prob = -1;
delta = abd_Prob(node,env,expltable,table,0);
ret = PL_put_float(out,delta.prob);
RETURN_IF_FAIL
out_assign = PL_new_term_ref();
PL_put_nil(out_assign);
RETURN_IF_FAIL
for(i = 0; i < delta.n_elements; i++) {
outass=abd_clist_to_pllist(delta.mpa[i]);
ret = PL_cons_list(out_assign,outass,out_assign);
RETURN_IF_FAIL
}
// expl_destroy_table_abd(expltable,env->boolVars);
destroy_table(table,env->boolVars);
}
else
{
if (node==Cudd_ReadOne(env->mgr))
{
ret=PL_put_float(out,1.0);
RETURN_IF_FAIL
}
else
{
ret=PL_put_float(out,0.0);
RETURN_IF_FAIL
}
out_assign=PL_new_term_ref();
ret=PL_put_nil(out_assign);
RETURN_IF_FAIL
}
return(PL_unify(out,arg_prob)&&PL_unify(out_assign,arg_expl));
}
static foreign_t ret_map_prob(term_t arg1, term_t arg2,
term_t arg3, term_t arg4)
{
term_t out,outass;
environment * env;
DdNode * node;
expltablerow * maptable;
tablerow * table;
//abdtablerow * abdtable;
prob_abd_expl delta;
int ret;
double p;
explan_t * mpa;
ret=PL_get_pointer(arg1,(void **)&env);
RETURN_IF_FAIL
ret=PL_get_pointer(arg2,(void **)&node);
RETURN_IF_FAIL
out=PL_new_term_ref();
ret=reorder_int(env);
RETURN_IF_FAIL
if (!Cudd_IsConstant(node))
{
maptable=expl_init_table(env->boolVars);
table=init_table(env->boolVars);
//abdtable=init_abd_table(env->n_abd);
delta=map_Prob(node,env,maptable,table,0);
p=delta.prob;
mpa=delta.mpa;
ret=PL_put_float(out,p);
RETURN_IF_FAIL
//destroy_table(abdtable,env->n_abd);
outass=clist_to_pllist(mpa,env);
RETURN_IF_FAIL
expl_destroy_table(maptable,env->boolVars);
destroy_table(table,env->boolVars);
}
else
{
if (node==Cudd_ReadOne(env->mgr))
{
ret=PL_put_float(out,1.0);
RETURN_IF_FAIL
}
else
{
ret=PL_put_float(out,0.0);
RETURN_IF_FAIL