From b54517d41922c4a08c7fa43c3c73a8b55b6f8864 Mon Sep 17 00:00:00 2001 From: Theofrastos Mantadelis Date: Thu, 2 Dec 2010 14:54:47 +0100 Subject: [PATCH] Updating ProbLog and adding ADs --- packages/ProbLog/dtproblog.yap | 2 +- packages/ProbLog/problog.yap | 37 +++++- packages/ProbLog/problog/os.yap | 108 ++++++++++++----- .../ProbLog/problog/{tptree.yap => ptree.yap} | 41 ++----- packages/ProbLog/problog/utils.yap | 113 ++++++++++++++++-- packages/ProbLog/problog/utils_learning.yap | 97 +++++++++------ packages/ProbLog/problog_learning.yap | 6 +- 7 files changed, 287 insertions(+), 117 deletions(-) rename packages/ProbLog/problog/{tptree.yap => ptree.yap} (98%) diff --git a/packages/ProbLog/dtproblog.yap b/packages/ProbLog/dtproblog.yap index f368e019c..0fca0f292 100644 --- a/packages/ProbLog/dtproblog.yap +++ b/packages/ProbLog/dtproblog.yap @@ -304,7 +304,7 @@ :- use_module('problog/os', [convert_filename_to_working_path/2, convert_filename_to_problog_path/2]). -:- use_module('problog/tptree', [delete_ptree/1]). +:- use_module('problog/ptree', [delete_ptree/1]). :- use_module('problog/tabling', [clear_tabling/0]). diff --git a/packages/ProbLog/problog.yap b/packages/ProbLog/problog.yap index f7be7462e..0f729fc5c 100644 --- a/packages/ProbLog/problog.yap +++ b/packages/ProbLog/problog.yap @@ -2,8 +2,8 @@ %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % -% $Date: 2010-11-09 02:47:35 +0100 (Tue, 09 Nov 2010) $ -% $Revision: 4991 $ +% $Date: 2010-11-23 11:47:48 +0100 (Tue, 23 Nov 2010) $ +% $Revision: 5027 $ % % This file is part of ProbLog % http://dtai.cs.kuleuven.be/problog @@ -314,7 +314,36 @@ :- use_module('problog/flags'). :- use_module('problog/print'). :- use_module('problog/os'). -:- use_module('problog/tptree'). +:- use_module('problog/ptree', [init_ptree/1, + delete_ptree/1, + member_ptree/2, + enum_member_ptree/2, + insert_ptree/2, + delete_ptree/2, + edges_ptree/2, + count_ptree/2, + prune_check_ptree/2, + empty_ptree/1, + merge_ptree/2, + merge_ptree/3, + bdd_ptree/3, + bdd_struct_ptree/3, + bdd_ptree_map/4, + bdd_struct_ptree_map/4, + traverse_ptree/2, %theo + print_ptree/1, %theo + statistics_ptree/0, %theo + print_nested_ptree/1, %theo + trie_to_bdd_trie/5, %theo + trie_to_bdd_struct_trie/5, + nested_trie_to_bdd_trie/5, %theo + nested_trie_to_bdd_struct_trie/5, + ptree_decomposition/3, + ptree_decomposition_struct/3, + nested_ptree_to_BDD_script/3, %theo + nested_ptree_to_BDD_struct_script/3, + ptree_db_trie_opt_performed/3, + bdd_vars_script/1]). :- use_module('problog/tabling'). :- use_module('problog/sampling'). :- use_module('problog/intervals'). @@ -393,7 +422,7 @@ :- initialization(( problog_define_flag(first_threshold, problog_flag_validate_indomain_0_1_open, 'starting threshold iterative deepening', 0.1, inference), - problog_define_flag(last_threshold, problog_flag_validate_indomain_0_1_open, 'stopping threshold iterative deepening', 1e-30, inference, flags:last_threshold_handler), + problog_define_flag(last_threshold, problog_flag_validate_indomain_0_1_open, 'stopping threshold iterative deepening', 1.0E-30, inference, flags:last_threshold_handler), problog_define_flag(id_stepsize, problog_flag_validate_indomain_0_1_close, 'threshold shrinking factor iterative deepening', 0.5, inference, flags:id_stepsize_handler) )). diff --git a/packages/ProbLog/problog/os.yap b/packages/ProbLog/problog/os.yap index 42bcea592..670a9a28b 100644 --- a/packages/ProbLog/problog/os.yap +++ b/packages/ProbLog/problog/os.yap @@ -2,8 +2,8 @@ %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % -% $Date: 2010-10-06 12:56:13 +0200 (Wed, 06 Oct 2010) $ -% $Revision: 4877 $ +% $Date: 2010-11-29 10:58:04 +0100 (Mon, 29 Nov 2010) $ +% $Revision: 5029 $ % % This file is part of ProbLog % http://dtai.cs.kuleuven.be/problog @@ -208,18 +208,18 @@ % Collected OS depended instructions %%%%%%%% :- module(os, [set_problog_path/1, - problog_path/1, - convert_filename_to_working_path/2, - convert_filename_to_problog_path/2, - concat_path_with_filename/3, - concat_path_with_filename2/3, - split_path_file/3, - check_existance/1, - calc_md5/2]). + problog_path/1, + convert_filename_to_working_path/2, + convert_filename_to_problog_path/2, + concat_path_with_filename/3, + concat_path_with_filename2/3, + split_path_file/3, + check_existance/1, + calc_md5/2]). % load library modules -:- use_module(library(system), [exec/3, file_exists/1]). +:- use_module(library(system), [exec/3, file_exists/1,wait/2]). :- use_module(library(lists), [memberchk/2]). % load our own modules @@ -228,10 +228,23 @@ :- dynamic(problog_path/1). :- dynamic(problog_working_path/1). + +%======================================================================== +%= +%= +%= +%======================================================================== + set_problog_path(Path):- retractall(problog_path(_)), assertz(problog_path(Path)). +%======================================================================== +%= +%= +%= +%======================================================================== + convert_filename_to_working_path(File_Name, Path):- flag_get(dir, Dir), concat_path_with_filename(Dir, File_Name, Path). @@ -240,22 +253,30 @@ convert_filename_to_problog_path(File_Name, Path):- problog_path(Dir), concat_path_with_filename(Dir, File_Name, Path). + +%======================================================================== +%= +%= +%= +%======================================================================== + concat_path_with_filename(Path, File_Name, Result):- - nonvar(File_Name), - nonvar(Path), + nonvar(File_Name), + nonvar(Path), + + % make sure, that there is no path delimiter at the end + prolog_file_name(Path,Path_Absolute), - % make sure, that there is no path delimiter at the end - prolog_file_name(Path,Path_Absolute), - - path_seperator(Path_Seperator), - atomic_concat([Path_Absolute, Path_Seperator, File_Name], Result). + path_seperator(Path_Seperator), + atomic_concat([Path_Absolute, Path_Seperator, File_Name], Result). concat_path_with_filename2(Path, File_Name, Result):- - nonvar(File_Name), - nonvar(Path), - path_seperator(Path_Seperator), - (atomic_concat(Path_Absolute, Path_Seperator, Path) ; Path_Absolute = Path), - atomic_concat([Path_Absolute, Path_Seperator, File_Name], Result). + nonvar(File_Name), + nonvar(Path), + path_seperator(Path_Seperator), + (atomic_concat(Path_Absolute, Path_Seperator, Path) ; Path_Absolute = Path), + atomic_concat([Path_Absolute, Path_Seperator, File_Name], Result). + %======================================================================== %= Calculate the MD5 checksum of +Filename by calling md5sum @@ -275,10 +296,10 @@ calc_md5(Filename,MD5):- calc_md5_intern(Filename,Command,MD5) :- ( file_exists(Filename) -> true ; throw(md5_file(Filename)) ), - atomic_concat([Command,' "',Filename,'"'],Call), + atomic_concat([Command,' "',Filename,'"'],Call), % execute the md5 command - exec(Call,[null,pipe(S),null],_PID), + exec(Call,[null,pipe(S),null],PID), bb_put(calc_md5_temp,End-End), % use difference list bb_put(calc_md5_temp2,0), @@ -308,28 +329,49 @@ calc_md5_intern(Filename,Command,MD5) :- !, close(S), + wait(PID,_Status), bb_delete(calc_md5_temp, FinalList-[]), bb_delete(calc_md5_temp2,_), atom_codes(MD5,FinalList). - + + +%======================================================================== +%= +%= +%= +%======================================================================== path_seperator('\\'):- yap_flag(windows, true), !. path_seperator('/'). + +%======================================================================== +%= +%= +%= +%======================================================================== + split_path_file(PathFile, Path, File):- path_seperator(PathSeperator), - atomic_concat(Path, File, PathFile), name(PathSeperator, [PathSeperatorName]), + + atomic_concat(Path, File, PathFile), name(File, FileName), \+ memberchk(PathSeperatorName, FileName), !. -% (Path = '' ; atomic_concat(_, PathSeperator, Path)). + +%======================================================================== +%= +%= +%= +%======================================================================== + check_existance(FileName):- - convert_filename_to_problog_path(FileName, Path), - catch(file_exists(Path), _, fail). + convert_filename_to_problog_path(FileName, Path), + catch(file_exists(Path), _, fail). check_existance(FileName):- - problog_path(PD), - write(user_error, 'WARNING: Can not find file: '), write(user_error, FileName), - write(user_error, ', please place file in problog path: '), write(user_error, PD), nl(user_error). + problog_path(PD), + write(user_error, 'WARNING: Can not find file: '), write(user_error, FileName), + write(user_error, ', please place file in problog path: '), write(user_error, PD), nl(user_error). diff --git a/packages/ProbLog/problog/tptree.yap b/packages/ProbLog/problog/ptree.yap similarity index 98% rename from packages/ProbLog/problog/tptree.yap rename to packages/ProbLog/problog/ptree.yap index 9ff552589..2450f27b0 100644 --- a/packages/ProbLog/problog/tptree.yap +++ b/packages/ProbLog/problog/ptree.yap @@ -2,8 +2,8 @@ %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % -% $Date: 2010-11-03 19:08:13 +0100 (Wed, 03 Nov 2010) $ -% $Revision: 4984 $ +% $Date: 2010-12-02 14:35:05 +0100 (Thu, 02 Dec 2010) $ +% $Revision: 5041 $ % % This file is part of ProbLog % http://dtai.cs.kuleuven.be/problog @@ -483,12 +483,11 @@ nested_ptree_to_BDD_struct_script(Trie, BDDFileName, Variables):- true; VarCNT = 0 ), - create_bdd_file_with_header(BDDFileName,VarCNT,InterCNT,TmpFile1), - delete_file_silent(TmpFile1), + prefix_bdd_file_with_header(BDDFileName,VarCNT,InterCNT,TmpFile1), cleanup_BDD_generation );( close(BDDS), - delete_file_silent(TmpFile1), + delete_file_silently(TmpFile1), cleanup_BDD_generation, fail ) @@ -610,8 +609,7 @@ ptree_decomposition_struct(Trie, BDDFileName, Variables) :- write('L1'), nl ), told, - create_bdd_file_with_header(BDDFileName,VarCnt,LCnt,TmpFile1), - delete_file_silent(TmpFile1). + prefix_bdd_file_with_header(BDDFileName,VarCnt,LCnt,TmpFile1). %%%%%%%%%%%%%%%%%%%%%%%% % write BDD info for given ptree to file @@ -1113,15 +1111,14 @@ nested_ptree_to_BDD_script(Trie, BDDFileName, VarFileName):- true; VarCNT = 0 ), - create_bdd_file_with_header(BDDFileName,VarCNT,InterCNT,TmpFile1), - delete_file_silent(TmpFile1), + prefix_bdd_file_with_header(BDDFileName,VarCNT,InterCNT,TmpFile1), open(VarFileName, 'write', VarStream), bddvars_to_script(Vars, VarStream), close(VarStream), cleanup_BDD_generation ; close(BDDS), - delete_file_silent(TmpFile1), + delete_file_silently(TmpFile1), cleanup_BDD_generation, fail ). @@ -1853,8 +1850,7 @@ ptree_decomposition(Trie, BDDFileName, VarFileName) :- write('L1'), nl ), told, - create_bdd_file_with_header(BDDFileName,VarCnt,LCnt,TmpFile1), - delete_file_silent(TmpFile1). + prefix_bdd_file_with_header(BDDFileName,VarCnt,LCnt,TmpFile1). get_next_inter_step(I):- nb_getval(next_inter_step, I), @@ -2040,24 +2036,3 @@ mark_deref(DB_Trie):- ), fail. mark_deref(_). - -% end of Theo - -create_bdd_file_with_header(BDD_File_Name,VarCount,IntermediateSteps,TmpFile) :- - open(BDD_File_Name,write,H), - % this is the header of the BDD script for problogbdd - format(H, '@BDD1~n~q~n0~n~q~n',[VarCount,IntermediateSteps]), - - % append the content of the file TmpFile - open(TmpFile,read,H2), - - ( - repeat, - get_byte(H2,C), - put_byte(H,C), - at_end_of_stream(H2), - ! - ), - close(H2), - - close(H). \ No newline at end of file diff --git a/packages/ProbLog/problog/utils.yap b/packages/ProbLog/problog/utils.yap index ca395066d..92291640c 100644 --- a/packages/ProbLog/problog/utils.yap +++ b/packages/ProbLog/problog/utils.yap @@ -2,8 +2,8 @@ %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % -% $Date: 2010-10-20 18:06:47 +0200 (Wed, 20 Oct 2010) $ -% $Revision: 4969 $ +% $Date: 2010-12-02 14:35:05 +0100 (Thu, 02 Dec 2010) $ +% $Revision: -1 $ % % This file is part of ProbLog % http://dtai.cs.kuleuven.be/problog @@ -204,22 +204,54 @@ % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -:- module(utils, [delete_file_silent/1, +:- module(utils, [delete_file_silently/1, + delete_files_silently/1, + delete_file_pattern_silently/2, variable_in_term_exactly_once/2, - slice_n/4]). + slice_n/4, + sorted_overlap_test/2, + prefix_bdd_file_with_header/4, + split_list/3]). + +:- use_module(library(system), [delete_file/1]). + +% load swi library, at some point vitor will make swi_expand_file_name/2 a built-in +:- load_foreign_files([libplstream], [], initIO). + +% load our own modules +:- use_module(os). -:- use_module(library(system), [delete_file/1, file_exists/1]). %======================================================================== %= %= %======================================================================== -delete_file_silent(File) :- - file_exists(File), +delete_file_silently(File) :- delete_file(File), !. -delete_file_silent(_). +delete_file_silently(_). + +%======================================================================== +%= +%= +%======================================================================== + +delete_files_silently([]). +delete_files_silently([H|T]) :- + delete_file_silently(H), + delete_files_silently(T). + +%======================================================================== +%= +%= +%======================================================================== + +delete_file_pattern_silently(Path,Pattern) :- + concat_path_with_filename(Path,Pattern,AbsolutePattern), + swi_expand_file_name(AbsolutePattern,Files), + + delete_files_silently(Files). %======================================================================== %= Split a list into the first n elements and the tail @@ -234,7 +266,7 @@ slice_n([H|T],N,[H|T2],T3) :- !, N2 is N-1, slice_n(T,N2,T2,T3). -slice_n(L,_,[],L). +slice_n(L,0,[],L). %======================================================================== %= succeeds if the variable V appears exactly once in the term T @@ -255,3 +287,66 @@ var_memberchk_none([H|T],V) :- var_memberchk_none(T,V). var_memberchk_none([],_). +%======================================================================== +%= sorted_overlap_test(+L1,+L2) +%= L1 and L2 are ground sorted lists +%= the predicate is true if there is an element X both appearing in L1 and L2 +%======================================================================== + +sorted_overlap_test([H|_],[H|_]) :- + !. +sorted_overlap_test([H1|T1],[H2|T2]) :- + H1 @> H2, + !, + sorted_overlap_test([H1|T1],T2). +sorted_overlap_test([_|T1],[H2|T2]) :- + sorted_overlap_test(T1,[H2|T2]). + +%======================================================================== +%= TmpFile is the file name that contains the "body" of a SimpleCUDD +%= script file. This predicate creates a new file BDD_File_Name that +%= starts with the right header and contains the body. The body file +%= is deleted afterwards. +%= +%= prefix_bdd_file_with_header(+BDD_File_Name,+VarCount,+IntermediateSteps,+TmpFile) +%======================================================================== + +prefix_bdd_file_with_header(BDD_File_Name,VarCount,IntermediateSteps,TmpFile) :- + open(BDD_File_Name,write,H), + % this is the header of the BDD script for problogbdd + format(H, '@BDD1~n~q~n0~n~q~n',[VarCount,IntermediateSteps]), + + % append the content of the file TmpFile + open(TmpFile,read,H2), + + ( + repeat, + get_byte(H2,C), + put_byte(H,C), + at_end_of_stream(H2), + ! + ), + close(H2), + + close(H), + delete_file_silently(TmpFile). + + +%======================================================================== +%= +%= +%= +%======================================================================== + + +split_list([],[],[]). +split_list([H|T],L1,L2) :- + length([H|T],Len), + Len1 is integer(Len/2+0.5), + split_list_intern(Len1,[H|T],L1,L2). + +split_list_intern(0,L,[],L). +split_list_intern(N,[H|T],[H|T1],L) :- + N>0, + N2 is N-1, + split_list_intern(N2,T,T1,L). \ No newline at end of file diff --git a/packages/ProbLog/problog/utils_learning.yap b/packages/ProbLog/problog/utils_learning.yap index 85402a6b2..64551487f 100644 --- a/packages/ProbLog/problog/utils_learning.yap +++ b/packages/ProbLog/problog/utils_learning.yap @@ -2,8 +2,8 @@ %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % -% $Date: 2010-10-20 18:06:47 +0200 (Wed, 20 Oct 2010) $ -% $Revision: 4969 $ +% $Date: 2010-12-02 14:35:05 +0100 (Thu, 02 Dec 2010) $ +% $Revision: 5041 $ % % This file is part of ProbLog % http://dtai.cs.kuleuven.be/problog @@ -205,12 +205,13 @@ %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% :- module(utils_learning, [empty_bdd_directory/0, - empty_output_directory/0]). + empty_output_directory/0, + create_known_values_file_name/2, + create_bdd_file_name/3, + create_bdd_input_file_name/2, + create_bdd_output_file_name/4]). -% load library modules -:- use_module(library(lists), [append/3, member/2]). -:- use_module(library(system), [delete_file/1, directory_files/2, file_exists/1]). % load our own modules :- use_module(os). @@ -225,14 +226,11 @@ empty_bdd_directory :- problog_flag(bdd_directory,Path), !, - - atom_codes('query_', PF1), % 'query_*' - - directory_files(Path,List), - delete_files_with_matching_prefix(List,Path,[PF1]). + delete_file_pattern_silently(Path,'query*'). empty_bdd_directory :- throw(error(problog_flag_does_not_exist(bdd_directory))). + %======================================================================== %= %= @@ -245,41 +243,72 @@ empty_output_directory :- concat_path_with_filename(Path,'log.dat',F1), concat_path_with_filename(Path,'out.dat',F2), - delete_file_silent(F1), - delete_file_silent(F2), + delete_file_silently(F1), + delete_file_silently(F2), - atom_codes('values_', PF1), % 'values_*_q_*.dat' - atom_codes('factprobs_', PF2), % 'factprobs_*.pl' - atom_codes('input_', PF3), % 'input_*.pl' - atom_codes('trainpredictions_',PF4), % 'trainpredictions_*.pl' - atom_codes('testpredictions_',PF5), % 'testpredictions_*.pl' - atom_codes('predictions_',PF6), % 'predictions_*.pl' - directory_files(Path,List), - delete_files_with_matching_prefix(List,Path,[PF1,PF2,PF3,PF4,PF5,PF6]). + delete_file_pattern_silently(Path,'values_*'), + delete_file_pattern_silently(Path,'factprobs_*'), + delete_file_pattern_silently(Path,'input_*'), + delete_file_pattern_silently(Path,'trainpredictions_*'), + delete_file_pattern_silently(Path,'testpredictions_*'), + delete_file_pattern_silently(Path,'predictions_*'). empty_output_directory :- throw(error(problog_flag_does_not_exist(output_directory))). +%======================================================================== +%= +%= +%======================================================================== + +create_known_values_file_name(QueryID,Absolute_File_Name) :- + problog_flag(bdd_directory,Path), + !, + + atomic_concat(['query_',QueryID,'_known_values'],File_Name), + concat_path_with_filename(Path,File_Name,Absolute_File_Name). +create_known_values_file_name(_,_) :- + throw(error(problog_flag_does_not_exist(bdd_directory))). + %======================================================================== %= %= %======================================================================== -delete_files_with_matching_prefix([],_,_). -delete_files_with_matching_prefix([Name|T],Path,Prefixes) :- - atom_codes(Name,NameCode), +create_bdd_file_name(QueryID,ClusterID,Absolute_File_Name) :- + problog_flag(bdd_directory,Path), + !, - ( - (member(Prefix,Prefixes), append(Prefix,_Suffix,NameCode)) - -> - ( - concat_path_with_filename(Path,Name,F), - delete_file_silent(F) - ); - true - ), + atomic_concat(['query_',QueryID,'_cluster_',ClusterID],File_Name), + concat_path_with_filename(Path,File_Name,Absolute_File_Name). +create_bdd_file_name(_,_,_) :- + throw(error(problog_flag_does_not_exist(bdd_directory))). - delete_files_with_matching_prefix(T,Path,Prefixes). +%======================================================================== +%= +%= +%======================================================================== +create_bdd_output_file_name(QueryID,ClusterID,Iteration,Absolute_File_Name) :- + problog_flag(output_directory,Path), + !, + + atomic_concat(['values_',Iteration,'_query_',QueryID,'_cluster_',ClusterID],File_Name), + concat_path_with_filename(Path,File_Name,Absolute_File_Name). +create_bdd_output_file_name(_,_,_,_) :- + throw(error(problog_flag_does_not_exist(output_directory))). +%======================================================================== +%= +%= +%======================================================================== + +create_bdd_input_file_name(Iteration,Absolute_File_Name) :- + problog_flag(output_directory,Path), + !, + + atomic_concat(['input_',Iteration,'.txt'],File_Name), + concat_path_with_filename(Path,File_Name,Absolute_File_Name). +create_bdd_input_file_name(_,_) :- + throw(error(problog_flag_does_not_exist(output_directory))). \ No newline at end of file diff --git a/packages/ProbLog/problog_learning.yap b/packages/ProbLog/problog_learning.yap index 82a350075..407e4f4c5 100644 --- a/packages/ProbLog/problog_learning.yap +++ b/packages/ProbLog/problog_learning.yap @@ -2,8 +2,8 @@ %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % -% $Date: 2010-10-20 18:06:47 +0200 (Wed, 20 Oct 2010) $ -% $Revision: 4969 $ +% $Date: 2010-12-02 14:35:05 +0100 (Thu, 02 Dec 2010) $ +% $Revision: 5041 $ % % This file is part of ProbLog % http://dtai.cs.kuleuven.be/problog @@ -770,7 +770,7 @@ update_values :- % start write current probabilities to file %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% bdd_input_file(Probabilities_File), - delete_file_silent(Probabilities_File), + delete_file_silently(Probabilities_File), open(Probabilities_File,'write',Handle),