Merge branch 'master' of ssh://git.dcc.fc.up.pt/yap-6.3
This commit is contained in:
commit
299816428c
@ -168,33 +168,49 @@ static int init_test(void)
|
|||||||
arg1=YAP_ARG1;
|
arg1=YAP_ARG1;
|
||||||
nRules=YAP_IntOfTerm(arg1);
|
nRules=YAP_IntOfTerm(arg1);
|
||||||
|
|
||||||
|
ex=0;
|
||||||
mgr_ex[ex]=Cudd_Init(0,0,UNIQUE_SLOTS,CACHE_SLOTS,0);
|
mgr_ex=(DdManager **) malloc((ex+1)* sizeof(DdManager *));
|
||||||
|
mgr_ex[ex]=Cudd_Init(0,0,UNIQUE_SLOTS,CACHE_SLOTS,5120);
|
||||||
Cudd_AutodynEnable(mgr_ex[ex], CUDD_REORDER_GROUP_SIFT);
|
Cudd_AutodynEnable(mgr_ex[ex], CUDD_REORDER_GROUP_SIFT);
|
||||||
Cudd_SetMaxCacheHard(mgr_ex[ex], 1024*1024*1024);
|
Cudd_SetMaxCacheHard(mgr_ex[ex], 0);
|
||||||
Cudd_SetLooseUpTo(mgr_ex[ex], 1024*1024*512);
|
Cudd_SetLooseUpTo(mgr_ex[ex], 0);
|
||||||
rules= (int *) malloc(nRules * sizeof(int));
|
Cudd_SetMinHit(mgr_ex[ex], 15);
|
||||||
|
|
||||||
|
bVar2mVar_ex=(int **) malloc((ex+1)* sizeof(int *));
|
||||||
bVar2mVar_ex[ex]=NULL;
|
bVar2mVar_ex[ex]=NULL;
|
||||||
probs_ex[ex]=NULL;
|
|
||||||
|
vars_ex=(variable **) malloc((ex+1)* sizeof(variable *));
|
||||||
vars_ex[ex]=NULL;
|
vars_ex[ex]=NULL;
|
||||||
|
|
||||||
|
nVars_ex=(int *) malloc((ex+1)* sizeof(int ));
|
||||||
nVars_ex[ex]=0;
|
nVars_ex[ex]=0;
|
||||||
|
|
||||||
|
probs_ex=(double **) malloc((ex+1)* sizeof(double *));
|
||||||
|
probs_ex[ex]=NULL;
|
||||||
|
|
||||||
|
boolVars_ex=(int *) malloc((ex+1)* sizeof(int ));
|
||||||
boolVars_ex[ex]=0;
|
boolVars_ex[ex]=0;
|
||||||
|
|
||||||
|
rules= (int *) malloc(nRules * sizeof(int));
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static int end_test(void)
|
static int end_test(void)
|
||||||
{
|
{
|
||||||
free(bVar2mVar_ex[ex]);
|
free(bVar2mVar_ex[ex]);
|
||||||
free(vars_ex[ex]);
|
free(vars_ex[ex]);
|
||||||
free(nVars_ex+ex);
|
|
||||||
free(boolVars_ex+ex);
|
|
||||||
Cudd_Quit(mgr_ex[ex]);
|
Cudd_Quit(mgr_ex[ex]);
|
||||||
free(probs_ex[ex]);
|
free(probs_ex[ex]);
|
||||||
free(rules);
|
free(rules);
|
||||||
|
free(mgr_ex);
|
||||||
|
free(bVar2mVar_ex);
|
||||||
|
free(vars_ex);
|
||||||
|
free(probs_ex);
|
||||||
|
free(nVars_ex);
|
||||||
|
free(boolVars_ex);
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -226,7 +242,12 @@ static double Expectation(DdNode **nodes_ex,int lenNodes)
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
if (nodes_ex[i]==Cudd_ReadLogicZero(mgr_ex[i]))
|
if (nodes_ex[i]==Cudd_ReadLogicZero(mgr_ex[i]))
|
||||||
|
{
|
||||||
CLL=CLL+LOGZERO*example_prob[i];
|
CLL=CLL+LOGZERO*example_prob[i];
|
||||||
|
nodes_probs_ex[i]=0.0;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
nodes_probs_ex[i]=1.0;
|
||||||
}
|
}
|
||||||
return CLL;
|
return CLL;
|
||||||
}
|
}
|
||||||
@ -250,7 +271,6 @@ static int end(void)
|
|||||||
free(probs_ex);
|
free(probs_ex);
|
||||||
free(nVars_ex);
|
free(nVars_ex);
|
||||||
free(boolVars_ex);
|
free(boolVars_ex);
|
||||||
free(nodes_probs_ex);
|
|
||||||
for (r=0;r<nRules;r++)
|
for (r=0;r<nRules;r++)
|
||||||
{
|
{
|
||||||
for (i=0;i<rules[r]-1;i++)
|
for (i=0;i<rules[r]-1;i++)
|
||||||
@ -928,8 +948,8 @@ static int randomize(void)
|
|||||||
|
|
||||||
static int EM(void)
|
static int EM(void)
|
||||||
{
|
{
|
||||||
YAP_Term arg1,arg2,arg3,arg4,arg5,arg6,arg7,
|
YAP_Term arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8,
|
||||||
out1,out2,nodesTerm,ruleTerm,tail,pair,compoundTerm;
|
out1,out2,out3,nodesTerm,ruleTerm,tail,pair,compoundTerm;
|
||||||
DdNode * node1,**nodes_ex;
|
DdNode * node1,**nodes_ex;
|
||||||
int r,lenNodes,i,iter;
|
int r,lenNodes,i,iter;
|
||||||
long iter1;
|
long iter1;
|
||||||
@ -945,6 +965,7 @@ static int EM(void)
|
|||||||
arg5=YAP_ARG5;
|
arg5=YAP_ARG5;
|
||||||
arg6=YAP_ARG6;
|
arg6=YAP_ARG6;
|
||||||
arg7=YAP_ARG7;
|
arg7=YAP_ARG7;
|
||||||
|
arg8=YAP_ARG8;
|
||||||
|
|
||||||
nodesTerm=arg1;
|
nodesTerm=arg1;
|
||||||
ea=YAP_FloatOfTerm(arg2);
|
ea=YAP_FloatOfTerm(arg2);
|
||||||
@ -1006,11 +1027,18 @@ static int EM(void)
|
|||||||
compoundTerm=YAP_MkPairTerm(ruleTerm,YAP_MkPairTerm(tail,YAP_TermNil()));
|
compoundTerm=YAP_MkPairTerm(ruleTerm,YAP_MkPairTerm(tail,YAP_TermNil()));
|
||||||
out2=YAP_MkPairTerm(compoundTerm,out2);
|
out2=YAP_MkPairTerm(compoundTerm,out2);
|
||||||
}
|
}
|
||||||
|
out3= YAP_TermNil();
|
||||||
|
for (i=0;i<lenNodes;i++)
|
||||||
|
{
|
||||||
|
out3=YAP_MkPairTerm(YAP_MkFloatTerm(nodes_probs_ex[i]),out3);
|
||||||
|
}
|
||||||
|
YAP_Unify(out3,arg8);
|
||||||
|
|
||||||
out1=YAP_MkFloatTerm(CLL1);
|
out1=YAP_MkFloatTerm(CLL1);
|
||||||
YAP_Unify(out1,arg6);
|
YAP_Unify(out1,arg6);
|
||||||
free(nodes_ex);
|
free(nodes_ex);
|
||||||
free(example_prob);
|
free(example_prob);
|
||||||
|
free(nodes_probs_ex);
|
||||||
|
|
||||||
return (YAP_Unify(out2,arg7));
|
return (YAP_Unify(out2,arg7));
|
||||||
}
|
}
|
||||||
@ -1128,6 +1156,8 @@ static int dag_size(void)
|
|||||||
void init_my_predicates()
|
void init_my_predicates()
|
||||||
/* function required by YAP for intitializing the predicates defined by a C function*/
|
/* function required by YAP for intitializing the predicates defined by a C function*/
|
||||||
{
|
{
|
||||||
|
srand(10);
|
||||||
|
|
||||||
YAP_UserCPredicate("init",init,2);
|
YAP_UserCPredicate("init",init,2);
|
||||||
YAP_UserCPredicate("init_bdd",init_bdd,0);
|
YAP_UserCPredicate("init_bdd",init_bdd,0);
|
||||||
YAP_UserCPredicate("end",end,0);
|
YAP_UserCPredicate("end",end,0);
|
||||||
@ -1143,7 +1173,7 @@ void init_my_predicates()
|
|||||||
YAP_UserCPredicate("init_test",init_test,1);
|
YAP_UserCPredicate("init_test",init_test,1);
|
||||||
YAP_UserCPredicate("end_test",end_test,0);
|
YAP_UserCPredicate("end_test",end_test,0);
|
||||||
YAP_UserCPredicate("ret_prob",ret_prob,2);
|
YAP_UserCPredicate("ret_prob",ret_prob,2);
|
||||||
YAP_UserCPredicate("em",EM,7);
|
YAP_UserCPredicate("em",EM,8);
|
||||||
YAP_UserCPredicate("q",Q,4);
|
YAP_UserCPredicate("q",Q,4);
|
||||||
YAP_UserCPredicate("randomize",randomize,0);
|
YAP_UserCPredicate("randomize",randomize,0);
|
||||||
YAP_UserCPredicate("deref",rec_deref,1);
|
YAP_UserCPredicate("deref",rec_deref,1);
|
||||||
|
@ -419,7 +419,7 @@ random_restarts(N,Nodes,CLL0,CLL,Par0,Par,LE):-
|
|||||||
setting(epsilon_em_fraction,ER),
|
setting(epsilon_em_fraction,ER),
|
||||||
length(Nodes,L),
|
length(Nodes,L),
|
||||||
setting(iter,Iter),
|
setting(iter,Iter),
|
||||||
em(Nodes,EA,ER,L,Iter,CLLR,Par1),
|
em(Nodes,EA,ER,L,Iter,CLLR,Par1,_ExP),
|
||||||
setting(verbosity,Ver),
|
setting(verbosity,Ver),
|
||||||
(Ver>2->
|
(Ver>2->
|
||||||
format("Random_restart: CLL ~f~n",[CLLR])
|
format("Random_restart: CLL ~f~n",[CLLR])
|
||||||
@ -450,7 +450,7 @@ random_restarts_ref(N,Nodes,CLL0,CLL,Par0,Par,LE):-
|
|||||||
setting(epsilon_em_fraction,ER),
|
setting(epsilon_em_fraction,ER),
|
||||||
length(Nodes,L),
|
length(Nodes,L),
|
||||||
setting(iterREF,Iter),
|
setting(iterREF,Iter),
|
||||||
em(Nodes,EA,ER,L,Iter,CLLR,Par1),
|
em(Nodes,EA,ER,L,Iter,CLLR,Par1,_ExP),
|
||||||
setting(verbosity,Ver),
|
setting(verbosity,Ver),
|
||||||
(Ver>2->
|
(Ver>2->
|
||||||
format("Random_restart: CLL ~f~n",[CLLR])
|
format("Random_restart: CLL ~f~n",[CLLR])
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
/*
|
/*
|
||||||
|
|
||||||
EMBLEM and SLIPCASE
|
SLIPCOVER
|
||||||
|
|
||||||
Copyright (c) 2011, Fabrizio Riguzzi and Elena Bellodi
|
Copyright (c) 2013, Fabrizio Riguzzi and Elena Bellodi
|
||||||
|
|
||||||
*/
|
*/
|
||||||
:-use_module(library(lists)).
|
:-use_module(library(lists)).
|
||||||
@ -49,7 +49,8 @@ setting(specialization,bottom).
|
|||||||
/* allowed values: mode,bottom */
|
/* allowed values: mode,bottom */
|
||||||
|
|
||||||
setting(seed,rand(10,1231,30322)).
|
setting(seed,rand(10,1231,30322)).
|
||||||
|
setting(score,ll).
|
||||||
|
/* allowed values: ll aucpr */
|
||||||
|
|
||||||
sl(File):-
|
sl(File):-
|
||||||
setting(seed,Seed),
|
setting(seed,Seed),
|
||||||
@ -631,9 +632,9 @@ get_bdd_group([H|T],T1,Gmax,G1,BDD0,BDD,CE,[H|LE0],LE):-
|
|||||||
get_bdd_group(T,T1,G,G1,BDD2,BDD,CE,LE0,LE).
|
get_bdd_group(T,T1,G,G1,BDD2,BDD,CE,LE0,LE).
|
||||||
|
|
||||||
/* EM start */
|
/* EM start */
|
||||||
random_restarts(0,_Nodes,CLL,CLL,Par,Par,_LE):-!.
|
random_restarts(0,_Nodes,Score,Score,Par,Par,_LE):-!.
|
||||||
|
|
||||||
random_restarts(N,Nodes,CLL0,CLL,Par0,Par,LE):-
|
random_restarts(N,Nodes,Score0,Score,Par0,Par,LE):-
|
||||||
setting(verbosity,Ver),
|
setting(verbosity,Ver),
|
||||||
(Ver>2->
|
(Ver>2->
|
||||||
setting(random_restarts_number,NMax),
|
setting(random_restarts_number,NMax),
|
||||||
@ -648,24 +649,24 @@ random_restarts(N,Nodes,CLL0,CLL,Par0,Par,LE):-
|
|||||||
setting(epsilon_em_fraction,ER),
|
setting(epsilon_em_fraction,ER),
|
||||||
length(Nodes,L),
|
length(Nodes,L),
|
||||||
setting(iter,Iter),
|
setting(iter,Iter),
|
||||||
em(Nodes,EA,ER,L,Iter,CLLR,Par1),
|
em(Nodes,EA,ER,L,Iter,CLL,Par1,ExP),
|
||||||
|
score(LE,ExP,CLL,ScoreR),
|
||||||
setting(verbosity,Ver),
|
setting(verbosity,Ver),
|
||||||
(Ver>2->
|
(Ver>2->
|
||||||
format("Random_restart: CLL ~f~n",[CLLR])
|
format("Random_restart: Score ~f~n",[ScoreR])
|
||||||
;
|
;
|
||||||
true
|
true
|
||||||
),
|
),
|
||||||
N1 is N-1,
|
N1 is N-1,
|
||||||
(CLLR>CLL0->
|
(ScoreR>Score0->
|
||||||
random_restarts(N1,Nodes,CLLR,CLL,Par1,Par,LE)
|
random_restarts(N1,Nodes,ScoreR,Score,Par1,Par,LE)
|
||||||
;
|
;
|
||||||
random_restarts(N1,Nodes,CLL0,CLL,Par0,Par,LE)
|
random_restarts(N1,Nodes,Score0,Score,Par0,Par,LE)
|
||||||
).
|
).
|
||||||
|
|
||||||
|
random_restarts_ref(0,_Nodes,Score,Score,Par,Par,_LE):-!.
|
||||||
|
|
||||||
random_restarts_ref(0,_Nodes,CLL,CLL,Par,Par,_LE):-!.
|
random_restarts_ref(N,Nodes,Score0,Score,Par0,Par,LE):-
|
||||||
|
|
||||||
random_restarts_ref(N,Nodes,CLL0,CLL,Par0,Par,LE):-
|
|
||||||
setting(verbosity,Ver),
|
setting(verbosity,Ver),
|
||||||
(Ver>2->
|
(Ver>2->
|
||||||
setting(random_restarts_REFnumber,NMax),
|
setting(random_restarts_REFnumber,NMax),
|
||||||
@ -679,21 +680,131 @@ random_restarts_ref(N,Nodes,CLL0,CLL,Par0,Par,LE):-
|
|||||||
setting(epsilon_em_fraction,ER),
|
setting(epsilon_em_fraction,ER),
|
||||||
length(Nodes,L),
|
length(Nodes,L),
|
||||||
setting(iterREF,Iter),
|
setting(iterREF,Iter),
|
||||||
em(Nodes,EA,ER,L,Iter,CLLR,Par1),
|
em(Nodes,EA,ER,L,Iter,CLLR,Par1,ExP),
|
||||||
|
score(LE,ExP,CLLR,ScoreR),
|
||||||
setting(verbosity,Ver),
|
setting(verbosity,Ver),
|
||||||
(Ver>2->
|
(Ver>2->
|
||||||
format("Random_restart: CLL ~f~n",[CLLR])
|
format("Random_restart: Score ~f~n",[ScoreR])
|
||||||
;
|
;
|
||||||
true
|
true
|
||||||
),
|
),
|
||||||
N1 is N-1,
|
N1 is N-1,
|
||||||
(CLLR>CLL0->
|
(ScoreR>Score0->
|
||||||
random_restarts_ref(N1,Nodes,CLLR,CLL,Par1,Par,LE)
|
random_restarts_ref(N1,Nodes,ScoreR,Score,Par1,Par,LE)
|
||||||
;
|
;
|
||||||
random_restarts_ref(N1,Nodes,CLL0,CLL,Par0,Par,LE)
|
random_restarts_ref(N1,Nodes,Score0,Score,Par0,Par,LE)
|
||||||
).
|
).
|
||||||
|
|
||||||
|
|
||||||
|
score(_LE,_ExP,CLL,CLL):-
|
||||||
|
setting(score,ll),!.
|
||||||
|
|
||||||
|
score(LE,ExP,_CLL,Score):-
|
||||||
|
compute_prob(LE,ExP,LPU,0,Pos,0,Neg),
|
||||||
|
keysort(LPU,LPO),
|
||||||
|
reverse(LPO,LP),
|
||||||
|
compute_aucpr(LP,Pos,Neg,Score).
|
||||||
|
|
||||||
|
|
||||||
|
compute_prob([],[],[],Pos,Pos,Neg,Neg).
|
||||||
|
|
||||||
|
compute_prob([\+ HE|TE],[HP|TP],[P- (\+ HE)|T],Pos0,Pos,Neg0,Neg):-!,
|
||||||
|
P is 1-HP,
|
||||||
|
Neg1 is Neg0+1,
|
||||||
|
compute_prob(TE,TP,T,Pos0,Pos,Neg1,Neg).
|
||||||
|
|
||||||
|
compute_prob([ HE|TE],[HP|TP],[HP- HE|T],Pos0,Pos,Neg0,Neg):-
|
||||||
|
Pos1 is Pos0+1,
|
||||||
|
compute_prob(TE,TP,T,Pos1,Pos,Neg0,Neg).
|
||||||
|
|
||||||
|
|
||||||
|
compute_aucpr(L,Pos,Neg,A):-
|
||||||
|
L=[P_0-E|TL],
|
||||||
|
(E= (\+ _ )->
|
||||||
|
FP=1,
|
||||||
|
TP=0,
|
||||||
|
FN=Pos,
|
||||||
|
TN is Neg -1
|
||||||
|
;
|
||||||
|
FP=0,
|
||||||
|
TP=1,
|
||||||
|
FN is Pos -1,
|
||||||
|
TN=Neg
|
||||||
|
),
|
||||||
|
compute_curve_points(TL,P_0,TP,FP,FN,TN,Points),
|
||||||
|
Points=[R0-P0|_TPoints],
|
||||||
|
(R0=:=0,P0=:=0->
|
||||||
|
Flag=true
|
||||||
|
;
|
||||||
|
Flag=false
|
||||||
|
),
|
||||||
|
area(Points,Flag,Pos,0,0,0,A).
|
||||||
|
|
||||||
|
compute_curve_points([],_P0,TP,FP,_FN,_TN,[1.0-Prec]):-!,
|
||||||
|
Prec is TP/(TP+FP).
|
||||||
|
|
||||||
|
compute_curve_points([P- (\+ _)|T],P0,TP,FP,FN,TN,Pr):-!,
|
||||||
|
(P<P0->
|
||||||
|
Prec is TP/(TP+FP),
|
||||||
|
Rec is TP/(TP+FN),
|
||||||
|
Pr=[Rec-Prec|Pr1],
|
||||||
|
P1=P
|
||||||
|
;
|
||||||
|
Pr=Pr1,
|
||||||
|
P1=P0
|
||||||
|
),
|
||||||
|
FP1 is FP+1,
|
||||||
|
TN1 is TN-1,
|
||||||
|
compute_curve_points(T,P1,TP,FP1,FN,TN1,Pr1).
|
||||||
|
|
||||||
|
compute_curve_points([P- _|T],P0,TP,FP,FN,TN,Pr):-!,
|
||||||
|
(P<P0->
|
||||||
|
Prec is TP/(TP+FP),
|
||||||
|
Rec is TP/(TP+FN),
|
||||||
|
Pr=[Rec-Prec|Pr1],
|
||||||
|
P1=P
|
||||||
|
;
|
||||||
|
Pr=Pr1,
|
||||||
|
P1=P0
|
||||||
|
),
|
||||||
|
TP1 is TP+1,
|
||||||
|
FN1 is FN-1,
|
||||||
|
compute_curve_points(T,P1,TP1,FP,FN1,TN,Pr1).
|
||||||
|
|
||||||
|
area([],_Flag,_Pos,_TPA,_FPA,A,A).
|
||||||
|
|
||||||
|
area([R0-P0|T],Flag,Pos,TPA,FPA,A0,A):-
|
||||||
|
TPB is R0*Pos,
|
||||||
|
(TPB=:=0->
|
||||||
|
A1=A0,
|
||||||
|
FPB=0
|
||||||
|
;
|
||||||
|
R_1 is TPA/Pos,
|
||||||
|
(TPA=:=0->
|
||||||
|
(Flag=false->
|
||||||
|
P_1=P0
|
||||||
|
;
|
||||||
|
P_1=0.0
|
||||||
|
)
|
||||||
|
;
|
||||||
|
P_1 is TPA/(TPA+FPA)
|
||||||
|
),
|
||||||
|
FPB is TPB*(1-P0)/P0,
|
||||||
|
N is TPB-TPA+0.5,
|
||||||
|
interpolate(1,N,Pos,R_1,P_1,TPA,FPA,TPB,FPB,A0,A1)
|
||||||
|
),
|
||||||
|
area(T,Flag,Pos,TPB,FPB,A1,A).
|
||||||
|
|
||||||
|
interpolate(I,N,_Pos,_R0,_P0,_TPA,_FPA,_TPB,_FPB,A,A):-I>N,!.
|
||||||
|
|
||||||
|
interpolate(I,N,Pos,R0,P0,TPA,FPA,TPB,FPB,A0,A):-
|
||||||
|
R is (TPA+I)/Pos,
|
||||||
|
P is (TPA+I)/(TPA+I+FPA+(FPB-FPA)/(TPB-TPA)*I),
|
||||||
|
A1 is A0+(R-R0)*(P+P0)/2,
|
||||||
|
I1 is I+1,
|
||||||
|
interpolate(I1,N,Pos,R,P,TPA,FPA,TPB,FPB,A1,A).
|
||||||
|
|
||||||
|
|
||||||
randomize([],[]):-!.
|
randomize([],[]):-!.
|
||||||
|
|
||||||
randomize([rule(N,V,NH,HL,BL,LogF)|T],[rule(N,V,NH,HL1,BL,LogF)|T1]):-
|
randomize([rule(N,V,NH,HL,BL,LogF)|T],[rule(N,V,NH,HL1,BL,LogF)|T1]):-
|
||||||
|
Reference in New Issue
Block a user