Merge ssh://xato/home/vsc/yap

This commit is contained in:
Vitor Santos Costa 2018-09-14 17:46:00 +01:00
commit 9ec1ffb2c7
9 changed files with 111 additions and 81 deletions

View File

@ -358,17 +358,6 @@ remove_duplicates([Elem|L], [Elem|NL]) :-
delete(L, Elem, Temp),
remove_duplicates(Temp, NL).
% reverse(List, Reversed)
% is true when List and Reversed are lists with the same elements
% but in opposite orders. rev/2 is a synonym for reverse/2.
reverse(List, Reversed) :-
reverse(List, [], Reversed).
reverse([], Reversed, Reversed).
reverse([Head|Tail], Sofar, Reversed) :-
reverse(Tail, [Head|Sofar], Reversed).
% same_length(?List1, ?List2)
% is true when List1 and List2 are both lists and have the same number

View File

@ -135,13 +135,13 @@ void trie_max_stats(YAP_Int *memory, YAP_Int *tries, YAP_Int *entries, YAP_I
void trie_usage(TrEntry trie, YAP_Int *entries, YAP_Int *nodes, YAP_Int *virtual_nodes);
void trie_print(TrEntry trie);
void trie_data_construct(TrNode node);
void trie_set_traverse_mode(YAP_Int mode);
YAP_Int trie_get_traverse_mode(void);
TrData trie_traverse_first(TrEntry trie);
TrData trie_traverse_next(TrData data);
void trie_disable_hash_table(void);
void trie_enable_hash_table(void);
extern void trie_data_construct(TrNode node);
extern void trie_set_traverse_mode(YAP_Int mode);
extern YAP_Int trie_get_traverse_mode(void);
extern TrData trie_traverse_first(TrEntry trie);
extern TrData trie_traverse_next(TrData data);
extern void trie_disable_hash_table(void);
extern void trie_enable_hash_table(void);
YAP_Term trie_to_list(TrEntry trie);

View File

@ -287,32 +287,32 @@ typedef struct trie_hash {
/* API */
/* --------------------------- */
TrEngine core_trie_init_module(void);
TrNode core_trie_open(TrEngine engine);
void core_trie_close(TrEngine engine, TrNode node, void (*destruct_function)(TrNode));
void core_trie_close_all(TrEngine engine, void (*destruct_function)(TrNode));
void core_trie_set_mode(YAP_Int mode);
YAP_Int core_trie_get_mode(void);
TrNode core_trie_put_entry(TrEngine engine, TrNode node, YAP_Term entry, YAP_Int *depth);
TrNode core_trie_check_entry(TrNode node, YAP_Term entry);
YAP_Term core_trie_get_entry(TrNode node);
void core_trie_remove_entry(TrEngine engine, TrNode node, void (*destruct_function)(TrNode));
void core_trie_remove_subtree(TrEngine engine, TrNode node, void (*destruct_function)(TrNode));
void core_trie_add(TrNode node_dest, TrNode node_source, void (*add_function)(TrNode, TrNode));
void core_trie_join(TrEngine engine, TrNode node_dest, TrNode node_source, void (*add_function)(TrNode, TrNode), void (*copy_function)(TrNode, TrNode));
void core_trie_intersect(TrEngine engine, TrNode node_dest, TrNode node_source, void (*add_function)(TrNode, TrNode), void (*destruct_function)(TrNode));
YAP_Int core_trie_count_join(TrNode node1, TrNode node2);
YAP_Int core_trie_count_intersect(TrNode node1, TrNode node2);
void core_trie_save(TrNode node, FILE *file, void (*save_function)(TrNode, FILE *));
TrNode core_trie_load(TrEngine engine, FILE *file, void (*load_function)(TrNode, YAP_Int, FILE *));
void core_trie_stats(TrEngine engine, YAP_Int *memory, YAP_Int *tries, YAP_Int *entries, YAP_Int *nodes);
void core_trie_max_stats(TrEngine engine, YAP_Int *memory, YAP_Int *tries, YAP_Int *entries, YAP_Int *nodes);
void core_trie_usage(TrNode node, YAP_Int *entries, YAP_Int *nodes, YAP_Int *virtual_nodes);
void core_trie_print(TrNode node, void (*print_function)(TrNode));
extern TrEngine core_trie_init_module(void);
extern TrNode core_trie_open(TrEngine engine);
extern void core_trie_close(TrEngine engine, TrNode node, void (*destruct_function)(TrNode));
extern void core_trie_close_all(TrEngine engine, void (*destruct_function)(TrNode));
extern void core_trie_set_mode(YAP_Int mode);
extern YAP_Int core_trie_get_mode(void);
extern TrNode core_trie_put_entry(TrEngine engine, TrNode node, YAP_Term entry, YAP_Int *depth);
extern TrNode core_trie_check_entry(TrNode node, YAP_Term entry);
extern YAP_Term core_trie_get_entry(TrNode node);
extern void core_trie_remove_entry(TrEngine engine, TrNode node, void (*destruct_function)(TrNode));
extern void core_trie_remove_subtree(TrEngine engine, TrNode node, void (*destruct_function)(TrNode));
extern void core_trie_add(TrNode node_dest, TrNode node_source, void (*add_function)(TrNode, TrNode));
extern void core_trie_join(TrEngine engine, TrNode node_dest, TrNode node_source, void (*add_function)(TrNode, TrNode), void (*copy_function)(TrNode, TrNode));
extern void core_trie_intersect(TrEngine engine, TrNode node_dest, TrNode node_source, void (*add_function)(TrNode, TrNode), void (*destruct_function)(TrNode));
extern YAP_Int core_trie_count_join(TrNode node1, TrNode node2);
extern YAP_Int core_trie_count_intersect(TrNode node1, TrNode node2);
extern void core_trie_save(TrNode node, FILE *file, void (*save_function)(TrNode, FILE *));
extern TrNode core_trie_load(TrEngine engine, FILE *file, void (*load_function)(TrNode, YAP_Int, FILE *));
extern void core_trie_stats(TrEngine engine, YAP_Int *memory, YAP_Int *tries, YAP_Int *entries, YAP_Int *nodes);
extern void core_trie_max_stats(TrEngine engine, YAP_Int *memory, YAP_Int *tries, YAP_Int *entries, YAP_Int *nodes);
extern void core_trie_usage(TrNode node, YAP_Int *entries, YAP_Int *nodes, YAP_Int *virtual_nodes);
extern void core_trie_print(TrNode node, void (*print_function)(TrNode));
void core_disable_hash_table(void);
void core_enable_hash_table(void);
extern void core_disable_hash_table(void);
extern void core_enable_hash_table(void);
YAP_Term core_trie_to_list(TrNode node);
extern YAP_Term core_trie_to_list(TrNode node);
#include "core_dbtries.h"

View File

@ -263,7 +263,7 @@
% this is a test to determine whether YAP provides the needed trie library
:- initialization(
( predicate_property(trie_disable_hash, imported_from(tries)) ->
( predicate_property(trie_disable_hash, imported_from(M)) ->
trie_disable_hash
; print_message(warning,'The predicate tries:trie_disable_hash/0 does not exist. Please update trie library.')
)

View File

@ -389,8 +389,6 @@ reset_learning :-
do_learning(Iterations) :-
do_learning(Iterations,-1).
:- spy do_learning/2.
do_learning(Iterations,Epsilon) :-
current_predicate(user:example/4),
!,
@ -501,6 +499,7 @@ init_learning :-
% if yes, switch to problog_exact
% continuous facts are not supported yet.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
set_default_gradient_method,
( problog_flag(continuous_facts, true )
->
problog_flag(init_method,(_,_,_,_,OldCall)),
@ -517,10 +516,6 @@ init_learning :-
true
)
;
true
),
(
problog_tabled(_)
->
(
@ -529,7 +524,6 @@ init_learning :-
);
true
),
succeeds_n_times(user:test_example(_,_,_,_),TestExampleCount),
format_learning(3,'~q test examples~n',[TestExampleCount]),
@ -555,7 +549,8 @@ init_learning :-
(
(user:example(_,_,P,_),P<1,P>0)
->
set_problog_flag(alpha,1.0);
set_problog_flag(alpha,1.0)
;
(
succeeds_n_times((user:example(_,_,P,=),P=:=1.0),Pos_Count),
succeeds_n_times((user:example(_,_,P,=),P=:=0.0),Neg_Count),
@ -563,6 +558,8 @@ init_learning :-
set_problog_flag(alpha,Alpha)
)
)
;
true
),
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
@ -587,6 +584,24 @@ init_learning :-
empty_bdd_directory.
set_default_gradient_method :-
problog_flag(continuous_facts, true),
!,
problog_flag(init_method,OldMethod),
format_learning(2,'Theory uses continuous facts.~nWill use problog_exact/3 as initalization method.~2n',[]),
set_problog_flag(init_method,(Query,Probability,BDDFile,ProbFile,problog_exact_save(Query,Probability,_Status,BDDFile,ProbFile))).
set_default_gradient_method :-
problog_tabled(_), problog_flag(fast_proofs,false),
!,
format_learning(2,'Theory uses tabling.~nWill use problog_exact/3 as initalization method.~2n',[]),
set_problog_flag(init_method,(Query,Probability,BDDFile,ProbFile,problog_exact_save(Query,Probability,_Status,BDDFile,ProbFile))).
set_default_gradient_method :-
problog_flag(init_method,(gene(X,Y),N,Bdd,graph2bdd(X,Y,N,Bdd))),
!.
set_default_gradient_method :-
set_problog_flag(init_method,(Query,1,BDD,
problog_kbest_as_bdd(user:Query,1,BDD))).
%========================================================================
%= This predicate goes over all training and test examples,
%= calls the inference method of ProbLog and stores the resulting
@ -609,6 +624,7 @@ init_one_query(QueryID,Query,Type) :-
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% if BDD file does not exist, call ProbLog
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
trace,
(
recorded(QueryID, _, _)
->
@ -619,7 +635,6 @@ init_one_query(QueryID,Query,Type) :-
Query =.. [_,X,Y]
->
Bdd = bdd(Dir, Tree, MapList),
% trace,
graph2bdd(X,Y,N,Bdd),
rb_new(H0),
maplist_to_hash(MapList, H0, Hash),
@ -629,28 +644,22 @@ init_one_query(QueryID,Query,Type) :-
recordz(QueryID,bdd(Dir, Grad, MapList),_)
;
b_setval(problog_required_keep_ground_ids,false),
problog_flag(init_method,(Query,Bdd,Call)),
rb_new(H0),
problog_flag(init_method,(Query,NOf,Bdd,problog_kbest_as_bdd(Call,1,Bdd))),
strip_module(Call,_,gene(X,Y)),
!,
Bdd = bdd(Dir, Tree, MapList),
% trace,
once(Call),
rb_new(H0),
problog:problog_kbest_as_bdd(user:gene(X,Y),1,Bdd),
maplist_to_hash(MapList, H0, Hash),
Tree \= [],
%put_code(0'.),
writeln(QueryID),
tree_to_grad(Tree, Hash, [], Grad),
recordz(QueryID,bdd(Dir, Grad, MapList),_)
),
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% check wether this BDD is similar to another BDD
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
(
problog_flag(check_duplicate_bdds,true)
->
true /* ignore this flag for now */
;
true
),!.
init_one_query(_QueryID,_Query,_Type).
).
init_one_query(_QueryID,_Query,_Type) :-
throw(unsupported_init_method).
@ -1008,8 +1017,8 @@ inv_sigmoid(T,InvSig) :-
%========================================================================
save_old_probabilities :-
problog_flag(continous_facts, true),
!,
problog_flag(continous_facts, true),
!,
forall(tunable_fact(FactID,_),
(
continuous_fact(FactID)
@ -1028,6 +1037,14 @@ save_old_probabilities :-
)
)
).
save_old_probabilities :-
forall(tunable_fact(FactID,_),
(
get_fact_probability(FactID,OldProbability),
atomic_concat(['old_prob_',FactID],Key),
bb_put(Key,OldProbability)
)
).
save_old_probabilities :-
forall(tunable_fact(FactID,_),
@ -1153,7 +1170,8 @@ gradient_descent :-
save_old_probabilities,
update_values,
reset_gradients.
reset_gradients,
compute_gradients(Handle).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% start set gradient to zero
@ -1177,7 +1195,14 @@ gradient_descent :-
bb_put(Key,0.0)
)
)
),
).
reset_gradients :-
forall(tunable_fact(FactID,_),
(
atomic_concat(['grad_',FactID],Key),
bb_put(Key,0.0)
)
).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% stop gradient to zero
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
@ -1185,6 +1210,7 @@ gradient_descent :-
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% start calculate gradient
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
compute_gradients(Handle) :-
bb_put(mse_train_sum, 0.0),
bb_put(mse_train_min, 0.0),
bb_put(mse_train_max, 0.0),
@ -1532,8 +1558,8 @@ my_5_min(V1,V2,V3,V4,V5,F1,F2,F3,F4,F5,VMin,FMin) :-
%========================================================================
init_flags :-
prolog_file_name('queries',Queries_Folder), % get absolute file name for './queries'
prolog_file_name('output',Output_Folder), % get absolute file name for './output'
prolog_file_name(queries,Queries_Folder), % get absolute file name for './queries'
prolog_file_name(output,Output_Folder), % get absolute file name for './output'
problog_define_flag(bdd_directory, problog_flag_validate_directory, 'directory for BDD scripts', Queries_Folder,learning_general),
problog_define_flag(output_directory, problog_flag_validate_directory, 'directory for logfiles etc', Output_Folder,learning_general,flags:learning_output_dir_handler),
problog_define_flag(log_frequency, problog_flag_validate_posint, 'log results every nth iteration', 1, learning_general),
@ -1551,7 +1577,6 @@ init_flags :-
writeln(1),
problog_define_flag(line_search_tolerance,problog_flag_validate_posnumber,'tolerance value for line search',0.05,learning_line_search),
problog_define_flag(line_search_interval, problog_flag_validate_dummy,'interval for line search',(0,100),learning_line_search,flags:linesearch_interval_handler).
init_logger :-
logger_define_variable(iteration, int),
@ -1574,7 +1599,7 @@ init_logger :-
logger_define_variable(llh_test_queries,float).
:- initialization(init_flags).
:- initialization(init_logger).

View File

@ -136,3 +136,14 @@ prolog:length(L, M) :-
%% @}
% reverse(List, Reversed)
% is true when List and Reversed are lists with the same elements
% but in opposite orders. rev/2 is a synonym for reverse/2.
lists:reverse(List, Reversed) :-
lists:reverse(List, [], Reversed).
lists:reverse([], Reversed, Reversed).
lists:reversae([Head|Tail], Sofar, Reversed) :-
lists:reverse(Tail, [Head|Sofar], Reversed).

View File

@ -855,7 +855,10 @@ nb_setval('$if_le1vel',0).
'__NB_getval__'('$lf_status', TOpts, fail),
'$lf_opt'( initialization, TOpts, Ref),
nb:nb_queue_close(Ref, Answers, []),
lists:member(G, Answers),
'$process_init_goal'(Answers).
'$exec_initialization_goals'.
'$process_init_goal'([G|_]) :-
'$yap_strip_module'( G, M0, G0),
(
catch(M0:G0, Error, user:'$LoopError'(Error, top))
@ -865,7 +868,8 @@ nb_setval('$if_le1vel',0).
format(user_error,':- ~w:~w failed.~n',[M0,G0])
),
fail.
'$exec_initialization_goals'.
'$process_init_goal'([_|Gs]) :-
'$process_init_goal'(Gs).
/**
@pred include(+ _F_) is directive

View File

@ -54,6 +54,7 @@ load_mega_clause( Stream ) :-
*/
prolog:load_db(Fs) :-
'$current_module'(M0),
retractall(dbloading(_Na,_Arity,_M,_T,_NaAr,_)),
prolog_flag(agc_margin,Old,0),
dbload(Fs,M0,load_db(Fs)),
load_facts,
@ -76,7 +77,7 @@ dbload(F, _, G) :-
'$do_error'(type_error(atom,F),G).
do_dbload(F0, M0, G) :-
'$full_filename'(F0, F, G),
'$full_filename'(F0, F),
assert(dbprocess(F, M0)),
open(F, read, R),
check_dbload_stream(R, M0),
@ -92,7 +93,7 @@ check_dbload_stream(R, M0) :-
).
dbload_count(T0, M0) :-
get_module(T0,M0,T,M),
gemodule(T0,M0,T,M),
functor(T,Na,Arity),
% dbload_check_term(T),
(

View File

@ -535,7 +535,7 @@ predicate_property(Pred,Prop) :-
functor(P,N,A),
once(recorded('$module','$module'(_TFN,M,_S,Publics,_L),_)),
lists:memberchk(N/A,Publics).
'$predicate_property'(P,M,M0,imported_from(M0)) :-
'$predicate_property'(P,M,M0,imported_from(M)) :-
M \= M0.
'$predicate_property'(P,Mod,_,number_of_clauses(NCl)) :-
'$number_of_clauses'(P,Mod,NCl).