From 69dbf7255343709874049466dbfa4bce14232ad8 Mon Sep 17 00:00:00 2001 From: Theofrastos Mantadelis Date: Fri, 1 Oct 2010 11:40:24 +0200 Subject: [PATCH 01/14] Various ProbLog improvements --- packages/ProbLog/problog.yap | 77 ++++--- packages/ProbLog/problog/logger.yap | 26 ++- packages/ProbLog/problog/os.yap | 38 +-- packages/ProbLog/problog_learning.yap | 35 ++- packages/ProbLog/simplecudd/problogmath.c | 268 +++++++++------------- packages/ProbLog/simplecudd/simplecudd.c | 76 ++++-- 6 files changed, 290 insertions(+), 230 deletions(-) diff --git a/packages/ProbLog/problog.yap b/packages/ProbLog/problog.yap index 1b1e34395..42eb9af8c 100644 --- a/packages/ProbLog/problog.yap +++ b/packages/ProbLog/problog.yap @@ -2,8 +2,8 @@ %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % -% $Date: 2010-09-29 13:03:26 +0200 (Wed, 29 Sep 2010) $ -% $Revision: 4843 $ +% $Date: 2010-09-30 14:48:30 +0200 (Thu, 30 Sep 2010) $ +% $Revision: 4859 $ % % This file is part of ProbLog % http://dtai.cs.kuleuven.be/problog @@ -313,6 +313,7 @@ % problog related modules :- use_module('problog/variables'). :- use_module('problog/extlists'). +:- use_module('problog/gflags', [flag_store/2]). :- use_module('problog/flags'). :- use_module('problog/print'). :- use_module('problog/os'). @@ -2208,16 +2209,27 @@ problog_max_id(Goal, Prob, Clauses) :- % version with _save at the end renames files for problogbdd to keep them %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% problog_kbest_save(Goal, K, Prob, Status, BDDFile, ParamFile) :- - problog_kbest(Goal, K, Prob, Status), - ( Status=ok -> - problog_flag(bdd_file,InternBDDFlag), - problog_flag(bdd_par_file,InternParFlag), - convert_filename_to_working_path(InternBDDFlag, InternBDD), - convert_filename_to_working_path(InternParFlag, InternPar), - rename_file(InternBDD,BDDFile), - rename_file(InternPar,ParamFile) - ; - true). + problog_flag(dir, InternWorkingDir), + problog_flag(bdd_file, InternBDDFlag), + problog_flag(bdd_par_file, InternParFlag), + split_path_file(BDDFile, WorkingDir, BDDFileName), + split_path_file(ParamFile, _WorkingDir, ParamFileName), + flag_store(dir, WorkingDir), + flag_store(bdd_file, BDDFileName), + flag_store(bdd_par_file, ParamFileName), + problog_kbest(Goal, K, Prob, Status), + flag_store(dir, InternWorkingDir), + flag_store(bdd_file, InternBDDFlag), + flag_store(bdd_par_file, InternParFlag). +% ( Status=ok -> +% problog_flag(bdd_file,InternBDDFlag), +% problog_flag(bdd_par_file,InternParFlag), +% convert_filename_to_working_path(InternBDDFlag, InternBDD), +% convert_filename_to_working_path(InternParFlag, InternPar), +% rename_file(InternBDD,BDDFile), +% rename_file(InternPar,ParamFile) +% ; +% true). problog_kbest(Goal, K, Prob, Status) :- problog_flag(first_threshold,InitT), @@ -2357,23 +2369,34 @@ problog_exact(Goal,Prob,Status) :- problog_control(off, exact). problog_exact_save(Goal,Prob,Status,BDDFile,ParamFile) :- - problog_control(on, exact), + problog_flag(dir, InternWorkingDir), + problog_flag(bdd_file, InternBDDFlag), + problog_flag(bdd_par_file, InternParFlag), + split_path_file(BDDFile, WorkingDir, BDDFileName), + split_path_file(ParamFile, _WorkingDir, ParamFileName), + flag_store(dir, WorkingDir), + flag_store(bdd_file, BDDFileName), + flag_store(bdd_par_file, ParamFileName), + problog_control(on, exact), problog_low(Goal,0,Prob,Status), problog_control(off, exact), - ( - Status==ok - -> - ( - problog_flag(bdd_file,InternBDDFlag), - problog_flag(bdd_par_file,InternParFlag), - problog_flag(dir,DirFlag), - atomic_concat([DirFlag,InternBDDFlag],InternBDD), - atomic_concat([DirFlag,InternParFlag],InternPar), - rename_file(InternBDD,BDDFile), - rename_file(InternPar,ParamFile) - ); - true - ). + flag_store(dir, InternWorkingDir), + flag_store(bdd_file, InternBDDFlag), + flag_store(bdd_par_file, InternParFlag). +% ( +% Status==ok +% -> +% ( +% problog_flag(bdd_file,InternBDDFlag), +% problog_flag(bdd_par_file,InternParFlag), +% problog_flag(dir,DirFlag), +% atomic_concat([DirFlag,InternBDDFlag],InternBDD), +% atomic_concat([DirFlag,InternParFlag],InternPar), +% rename_file(InternBDD,BDDFile), +% rename_file(InternPar,ParamFile) +% ); +% true +% ). problog_collect_trie(Goal):- problog_call(Goal), diff --git a/packages/ProbLog/problog/logger.yap b/packages/ProbLog/problog/logger.yap index 6af370739..4a1795e80 100644 --- a/packages/ProbLog/problog/logger.yap +++ b/packages/ProbLog/problog/logger.yap @@ -2,8 +2,8 @@ %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % -% $Date: 2010-09-28 21:04:43 +0200 (Tue, 28 Sep 2010) $ -% $Revision: 4838 $ +% $Date: 2010-09-30 13:50:45 +0200 (Thu, 30 Sep 2010) $ +% $Revision: 4857 $ % % This file is part of ProbLog % http://dtai.cs.kuleuven.be/problog @@ -217,7 +217,8 @@ logger_write_data/0, logger_write_header/0, logger_variable_is_set/1, - logger_add_to_variable/2]). + logger_add_to_variable/2, + logger_reset_all_variables/0]). :- use_module(library(system),[datime/1,mktime/2]). :- use_module(library(lists),[append/3,member/2]). @@ -480,9 +481,7 @@ logger_write_data :- logger_write_data_intern(Variables,Handle), close(Handle), - % reset variables - findall(_,(member((Name,_),Variables),atom_concat(logger_data_,Name,Key),bb_put(Key,null)),_), - findall(_,(member((Name,time),Variables),atom_concat(logger_start_time_,Name,Key2),bb_put(Key2,null)),_). + logger_reset_all_variables. logger_write_data_intern([],_). logger_write_data_intern([(Name,_Type)],Handle) :- @@ -511,6 +510,21 @@ variablevalue_with_nullcheck(Name,Result) :- %= %======================================================================== +logger_reset_all_variables :- + bb_get(logger_variables,Variables), + + % reset variables + findall(_,(member((Name,_),Variables),atom_concat(logger_data_,Name,Key),bb_put(Key,null)),_), + findall(_,(member((Name,time),Variables),atom_concat(logger_start_time_,Name,Key2),bb_put(Key2,null)),_). + + +%======================================================================== +%= +%= +%= +%======================================================================== + + logger_write_header :- bb_get(logger_filename,FName), bb_get(logger_variables,Variables), diff --git a/packages/ProbLog/problog/os.yap b/packages/ProbLog/problog/os.yap index f9ab70f26..cc677f026 100644 --- a/packages/ProbLog/problog/os.yap +++ b/packages/ProbLog/problog/os.yap @@ -2,8 +2,8 @@ %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % -% $Date: 2010-09-28 21:04:43 +0200 (Tue, 28 Sep 2010) $ -% $Revision: 4838 $ +% $Date: 2010-09-30 16:05:52 +0200 (Thu, 30 Sep 2010) $ +% $Revision: 4863 $ % % This file is part of ProbLog % http://dtai.cs.kuleuven.be/problog @@ -212,11 +212,13 @@ convert_filename_to_working_path/2, convert_filename_to_problog_path/2, concat_path_with_filename/3, + split_path_file/3, calc_md5/2]). % load library modules :- use_module(library(system), [exec/3, file_exists/1]). +:- use_module(library(lists), [memberchk/2]). % load our own modules :- use_module(gflags, _, [flag_get/2]). @@ -237,20 +239,14 @@ convert_filename_to_problog_path(File_Name, Path):- 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), - ( - yap_flag(windows, true) - -> - 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). %======================================================================== %= Calculate the MD5 checksum of +Filename by calling md5sum @@ -306,3 +302,17 @@ calc_md5_intern(Filename,Command,MD5) :- 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]), + name(File, FileName), + \+ memberchk(PathSeperatorName, FileName), + !. +% (Path = '' ; atomic_concat(_, PathSeperator, Path)). diff --git a/packages/ProbLog/problog_learning.yap b/packages/ProbLog/problog_learning.yap index f11bd5f1c..f209a35a5 100644 --- a/packages/ProbLog/problog_learning.yap +++ b/packages/ProbLog/problog_learning.yap @@ -2,8 +2,8 @@ %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % -% $Date: 2010-09-29 18:43:14 +0200 (Wed, 29 Sep 2010) $ -% $Revision: 4854 $ +% $Date: 2010-09-30 16:15:04 +0200 (Thu, 30 Sep 2010) $ +% $Revision: 4864 $ % % This file is part of ProbLog % http://dtai.cs.kuleuven.be/problog @@ -206,7 +206,8 @@ :- module(learning,[do_learning/1, - do_learning/2 + do_learning/2, + reset_learning/0 ]). % switch on all the checks to reduce bug searching time @@ -233,6 +234,9 @@ :- dynamic(query_probability_intern/2). :- dynamic(query_gradient_intern/4). :- dynamic(last_mse/1). +:- dynamic(query_is_similar/2). +:- dynamic(query_md5/2). + % used to identify queries which have identical proofs :- dynamic(query_is_similar/2). @@ -409,6 +413,27 @@ check_examples :- throw(error(examples)) ); true ). +%======================================================================== +%= +%======================================================================== + +reset_learning :- + retractall(current_iteration(_)), + retractall(learning_initialized), + + retractall(values_correct), + retractall(current_iteration(_)), + retractall(example_count(_)), + retractall(query_probability_intern(_,_)), + retractall(query_gradient_intern(_,_,_)), + retractall(last_mse(_)), + retractall(query_is_similar(_,_)), + retractall(query_md5(_,_,_)), + + set_problog_flag(alpha,auto), + set_problog_flag(learning_rate,examples), + logger_reset_all_variables. + %======================================================================== @@ -763,9 +788,7 @@ update_values :- ( % go over all continuous facts get_continuous_fact_parameters(ID,gaussian(Mu,Sigma)), - %SigmaL is log(Sigma), - SigmaL=Sigma, - format(Handle,'@x~q_*~n0~n0~n~10f;~10f~n',[ID,Mu,SigmaL]), + format(Handle,'@x~q_*~n0~n0~n~10f;~10f~n',[ID,Mu,Sigma]), fail; % go to next continuous fact true diff --git a/packages/ProbLog/simplecudd/problogmath.c b/packages/ProbLog/simplecudd/problogmath.c index b4fe41591..2f21a5356 100644 --- a/packages/ProbLog/simplecudd/problogmath.c +++ b/packages/ProbLog/simplecudd/problogmath.c @@ -3,185 +3,143 @@ * SimpleCUDD library (www.cs.kuleuven.be/~theo/tools/simplecudd.html) * * SimpleCUDD was developed at Katholieke Universiteit Leuven(www.kuleuven.be) * * * -* Copyright Katholieke Universiteit Leuven 2008, 2009, 2010 * +* Copyright T. Mantadelis, A. Kimmig, B. Gutmann * +* and Katholieke Universiteit Leuven 2008 * * * * Author: Bernd Gutmann * * File: problogmath.c * -* $Date:: 2010-08-25 15:23:30 +0200 (Wed, 25 Aug 2010) $ * -* $Revision:: 4683 $ * +* $Date:: 2010-09-30 14:29:42 +0200 (Thu, 30 Sep 2010) $ * +* $Revision:: 4858 $ * * * * * ******************************************************************************** * * -* Artistic License 2.0 * +* The "Artistic License" * * * -* Copyright (c) 2000-2006, The Perl Foundation. * +* Preamble * * * -* Everyone is permitted to copy and distribute verbatim copies of this license * -* document, but changing it is not allowed. * +* The intent of this document is to state the conditions under which a * +* Package may be copied, such that the Copyright Holder maintains some * +* semblance of artistic control over the development of the package, * +* while giving the users of the package the right to use and distribute * +* the Package in a more-or-less customary fashion, plus the right to make * +* reasonable modifications. * * * -* Preamble * +* Definitions: * * * -* This license establishes the terms under which a given free software Package * -* may be copied, modified, distributed, and/or redistributed. The intent is * -* that the Copyright Holder maintains some artistic control over the * -* development of that Package while still keeping the Package available as * -* open source and free software. * +* "Package" refers to the collection of files distributed by the * +* Copyright Holder, and derivatives of that collection of files * +* created through textual modification. * * * -* You are always permitted to make arrangements wholly outside of this license * -* directly with the Copyright Holder of a given Package. If the terms of this * -* license do not permit the full use that you propose to make of the Package, * -* you should contact the Copyright Holder and seek a different licensing * -* arrangement. * -* Definitions * +* "Standard Version" refers to such a Package if it has not been * +* modified, or has been modified in accordance with the wishes * +* of the Copyright Holder as specified below. * * * -* "Copyright Holder" means the individual(s) or organization(s) named in the * -* copyright notice for the entire Package. * +* "Copyright Holder" is whoever is named in the copyright or * +* copyrights for the package. * * * -* "Contributor" means any party that has contributed code or other material to * -* the Package, in accordance with the Copyright Holder's procedures. * +* "You" is you, if you're thinking about copying or distributing * +* this Package. * * * -* "You" and "your" means any person who would like to copy, distribute, or * -* modify the Package. * +* "Reasonable copying fee" is whatever you can justify on the * +* basis of media cost, duplication charges, time of people involved, * +* and so on. (You will not be required to justify it to the * +* Copyright Holder, but only to the computing community at large * +* as a market that must bear the fee.) * * * -* "Package" means the collection of files distributed by the Copyright Holder, * -* and derivatives of that collection and/or of those files. A given Package * -* may consist of either the Standard Version, or a Modified Version. * +* "Freely Available" means that no fee is charged for the item * +* itself, though there may be fees involved in handling the item. * +* It also means that recipients of the item may redistribute it * +* under the same conditions they received it. * * * -* "Distribute" means providing a copy of the Package or making it accessible * -* to anyone else, or in the case of a company or organization, to others * -* outside of your company or organization. * +* 1. You may make and give away verbatim copies of the source form of the * +* Standard Version of this Package without restriction, provided that you * +* duplicate all of the original copyright notices and associated disclaimers. * * * -* "Distributor Fee" means any fee that you charge for Distributing this * -* Package or providing support for this Package to another party. It does not * -* mean licensing fees. * +* 2. You may apply bug fixes, portability fixes and other modifications * +* derived from the Public Domain or from the Copyright Holder. A Package * +* modified in such a way shall still be considered the Standard Version. * * * -* "Standard Version" refers to the Package if it has not been modified, or has * -* been modified only in ways explicitly requested by the Copyright Holder. * +* 3. You may otherwise modify your copy of this Package in any way, provided * +* that you insert a prominent notice in each changed file stating how and * +* when you changed that file, and provided that you do at least ONE of the * +* following: * * * -* "Modified Version" means the Package, if it has been changed, and such * -* changes were not explicitly requested by the Copyright Holder. * +* a) place your modifications in the Public Domain or otherwise make them * +* Freely Available, such as by posting said modifications to Usenet or * +* an equivalent medium, or placing the modifications on a major archive * +* site such as uunet.uu.net, or by allowing the Copyright Holder to include * +* your modifications in the Standard Version of the Package. * * * -* "Original License" means this Artistic License as Distributed with the * -* Standard Version of the Package, in its current version or as it may be * -* modified by The Perl Foundation in the future. * +* b) use the modified Package only within your corporation or organization. * * * -* "Source" form means the source code, documentation source, and configuration * -* files for the Package. * +* c) rename any non-standard executables so the names do not conflict * +* with standard executables, which must also be provided, and provide * +* a separate manual page for each non-standard executable that clearly * +* documents how it differs from the Standard Version. * * * -* "Compiled" form means the compiled bytecode, object code, binary, or any * -* other form resulting from mechanical transformation or translation of the * -* Source form. * -* Permission for Use and Modification Without Distribution * +* d) make other distribution arrangements with the Copyright Holder. * * * -* (1) You are permitted to use the Standard Version and create and use * -* Modified Versions for any purpose without restriction, provided that you do * -* not Distribute the Modified Version. * -* Permissions for Redistribution of the Standard Version * +* 4. You may distribute the programs of this Package in object code or * +* executable form, provided that you do at least ONE of the following: * * * -* (2) You may Distribute verbatim copies of the Source form of the Standard * -* Version of this Package in any medium without restriction, either gratis or * -* for a Distributor Fee, provided that you duplicate all of the original * -* copyright notices and associated disclaimers. At your discretion, such * -* verbatim copies may or may not include a Compiled form of the Package. * +* a) distribute a Standard Version of the executables and library files, * +* together with instructions (in the manual page or equivalent) on where * +* to get the Standard Version. * * * -* (3) You may apply any bug fixes, portability changes, and other * -* modifications made available from the Copyright Holder. The resulting * -* Package will still be considered the Standard Version, and as such will be * -* subject to the Original License. * -* Distribution of Modified Versions of the Package as Source * +* b) accompany the distribution with the machine-readable source of * +* the Package with your modifications. * * * -* (4) You may Distribute your Modified Version as Source (either gratis or for * -* a Distributor Fee, and with or without a Compiled form of the Modified * -* Version) provided that you clearly document how it differs from the Standard * -* Version, including, but not limited to, documenting any non-standard * -* features, executables, or modules, and provided that you do at least ONE of * -* the following: * +* c) give non-standard executables non-standard names, and clearly * +* document the differences in manual pages (or equivalent), together * +* with instructions on where to get the Standard Version. * * * -* (a) make the Modified Version available to the Copyright Holder of the * -* Standard Version, under the Original License, so that the Copyright Holder * -* may include your modifications in the Standard Version. * -* (b) ensure that installation of your Modified Version does not prevent the * -* user installing or running the Standard Version. In addition, the Modified * -* Version must bear a name that is different from the name of the Standard * -* Version. * -* (c) allow anyone who receives a copy of the Modified Version to make the * -* Source form of the Modified Version available to others under * -* (i) the Original License or * -* (ii) a license that permits the licensee to freely copy, modify and * -* redistribute the Modified Version using the same licensing terms that apply * -* to the copy that the licensee received, and requires that the Source form of * -* the Modified Version, and of any works derived from it, be made freely * -* available in that license fees are prohibited but Distributor Fees are * -* allowed. * -* Distribution of Compiled Forms of the Standard Version or Modified Versions * -* without the Source * +* d) make other distribution arrangements with the Copyright Holder. * * * -* (5) You may Distribute Compiled forms of the Standard Version without the * -* Source, provided that you include complete instructions on how to get the * -* Source of the Standard Version. Such instructions must be valid at the time * -* of your distribution. If these instructions, at any time while you are * -* carrying out such distribution, become invalid, you must provide new * -* instructions on demand or cease further distribution. If you provide valid * -* instructions or cease distribution within thirty days after you become aware * -* that the instructions are invalid, then you do not forfeit any of your * -* rights under this license. * +* 5. You may charge a reasonable copying fee for any distribution of this * +* Package. You may charge any fee you choose for support of this * +* Package. You may not charge a fee for this Package itself. However, * +* you may distribute this Package in aggregate with other (possibly * +* commercial) programs as part of a larger (possibly commercial) software * +* distribution provided that you do not advertise this Package as a * +* product of your own. You may embed this Package's interpreter within * +* an executable of yours (by linking); this shall be construed as a mere * +* form of aggregation, provided that the complete Standard Version of the * +* interpreter is so embedded. * * * -* (6) You may Distribute a Modified Version in Compiled form without the * -* Source, provided that you comply with Section 4 with respect to the Source * -* of the Modified Version. * -* Aggregating or Linking the Package * +* 6. The scripts and library files supplied as input to or produced as * +* output from the programs of this Package do not automatically fall * +* under the copyright of this Package, but belong to whoever generated * +* them, and may be sold commercially, and may be aggregated with this * +* Package. If such scripts or library files are aggregated with this * +* Package via the so-called "undump" or "unexec" methods of producing a * +* binary executable image, then distribution of such an image shall * +* neither be construed as a distribution of this Package nor shall it * +* fall under the restrictions of Paragraphs 3 and 4, provided that you do * +* not represent such an executable image as a Standard Version of this * +* Package. * * * -* (7) You may aggregate the Package (either the Standard Version or Modified * -* Version) with other packages and Distribute the resulting aggregation * -* provided that you do not charge a licensing fee for the Package. Distributor * -* Fees are permitted, and licensing fees for other components in the * -* aggregation are permitted. The terms of this license apply to the use and * -* Distribution of the Standard or Modified Versions as included in the * -* aggregation. * +* 7. C subroutines (or comparably compiled subroutines in other * +* languages) supplied by you and linked into this Package in order to * +* emulate subroutines and variables of the language defined by this * +* Package shall not be considered part of this Package, but are the * +* equivalent of input as in Paragraph 6, provided these subroutines do * +* not change the language in any way that would cause it to fail the * +* regression tests for the language. * * * -* (8) You are permitted to link Modified and Standard Versions with other * -* works, to embed the Package in a larger work of your own, or to build * -* stand-alone binary or bytecode versions of applications that include the * -* Package, and Distribute the result without restriction, provided the result * -* does not expose a direct interface to the Package. * -* Items That are Not Considered Part of a Modified Version * +* 8. Aggregation of this Package with a commercial distribution is always * +* permitted provided that the use of this Package is embedded; that is, * +* when no overt attempt is made to make this Package's interfaces visible * +* to the end user of the commercial distribution. Such use shall not be * +* construed as a distribution of this Package. * * * -* (9) Works (including, but not limited to, modules and scripts) that merely * -* extend or make use of the Package, do not, by themselves, cause the Package * -* to be a Modified Version. In addition, such works are not considered parts * -* of the Package itself, and are not subject to the terms of this license. * -* General Provisions * +* 9. The name of the Copyright Holder may not be used to endorse or promote * +* products derived from this software without specific prior written * +* permission. * * * -* (10) Any use, modification, and distribution of the Standard or Modified * -* Versions is governed by this Artistic License. By using, modifying or * -* distributing the Package, you accept this license. Do not use, modify, or * -* distribute the Package, if you do not accept this license. * -* * -* (11) If your Modified Version has been derived from a Modified Version made * -* by someone other than you, you are nevertheless required to ensure that your * -* Modified Version complies with the requirements of this license. * -* * -* (12) This license does not grant you the right to use any trademark, service * -* mark, tradename, or logo of the Copyright Holder. * -* * -* (13) This license includes the non-exclusive, worldwide, free-of-charge * -* patent license to make, have made, use, offer to sell, sell, import and * -* otherwise transfer the Package with respect to any patent claims licensable * -* by the Copyright Holder that are necessarily infringed by the Package. If * -* you institute patent litigation (including a cross-claim or counterclaim) * -* against any party alleging that the Package constitutes direct or * -* contributory patent infringement, then this Artistic License to you shall * -* terminate on the date that such litigation is filed. * -* * -* (14) Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER * -* AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES. THE * -* IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR * -* NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY YOUR LOCAL LAW. * -* UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR CONTRIBUTOR WILL BE LIABLE * -* FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING IN * -* ANY WAY OUT OF THE USE OF THE PACKAGE, EVEN IF ADVISED OF THE POSSIBILITY OF * -* SUCH DAMAGE. * +* 10. THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR * +* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED * +* WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. * * * * The End * * * @@ -266,6 +224,7 @@ double cumulative_normal_upper_dsigma(double high,double mu,double sigma) { // it is used to parse in the parameters of continues variables from the input file density_integral parse_density_integral_string(char *input, char *variablename) { density_integral result; + double sigma; int i; char garbage[64], s1[64],s2[64],s3[64],s4[64]; @@ -275,21 +234,18 @@ density_integral parse_density_integral_string(char *input, char *variablename) exit(EXIT_FAILURE); } - if (IsRealNumber(s1)) { - result.mu=atof(s1); - } else { + if (!getRealNumber(s1, &result.mu)) { fprintf(stderr, "Error at parsing the string %s in the function parse_density_integral_string\n",input); fprintf(stderr, "%s is not a number\n",s1); exit(EXIT_FAILURE); } - if (IsRealNumber(s2)) { - result.log_sigma=atof(s2); - } else { + if (!getRealNumber(s2, &sigma) || sigma<=0.0) { fprintf(stderr, "Error at parsing the string %s in the function parse_density_integral_string\n",input); fprintf(stderr, "%s is not a number\n",s2); exit(EXIT_FAILURE); } + result.log_sigma=log(sigma); /* if (result.sigma<=0) { */ /* fprintf(stderr, "Error at parsing the string %s in the function parse_density_integral_string",input); */ @@ -322,17 +278,13 @@ density_integral parse_density_integral_string(char *input, char *variablename) } } - if (IsRealNumber(s3)) { - result.low=atof(s3); - } else { + if (!getRealNumber(s3, &result.low)) { fprintf(stderr, "Error at parsing the string %s in the function parse_density_integral_string\n",input); fprintf(stderr, "%s is not a number\n",s1); exit(EXIT_FAILURE); } - if (IsRealNumber(s4)) { - result.high=atof(s4); - } else { + if (!getRealNumber(s4, &result.high)) { fprintf(stderr, "Error ar parsing the string %s in the function parse_density_integral_string\n",input); fprintf(stderr, "%s is not a number\n",s1); exit(EXIT_FAILURE); @@ -349,6 +301,6 @@ density_integral parse_density_integral_string(char *input, char *variablename) exit(EXIT_FAILURE); } - + return result; } diff --git a/packages/ProbLog/simplecudd/simplecudd.c b/packages/ProbLog/simplecudd/simplecudd.c index d42867a78..caa67134b 100644 --- a/packages/ProbLog/simplecudd/simplecudd.c +++ b/packages/ProbLog/simplecudd/simplecudd.c @@ -378,6 +378,10 @@ int simpleNamedBDDtoDot(DdManager *manager, namedvars varmap, DdNode *bdd, char DdNode *f[1]; int ret; FILE *fd; + // Reordering until getting the optimal bdd // +/* Cudd_AutodynDisable(manager); + Cudd_ReduceHeap(manager, CUDD_REORDER_SIFT_CONVERGE, 1);*/ + // better before making an ADD // f[0] = Cudd_BddToAdd(manager, bdd); fd = fopen(filename, "w"); if (fd == NULL) { @@ -690,8 +694,7 @@ int LoadVariableData(namedvars varmap, char *filename) { if (hasvar >= 0) { switch(idat) { case 0: - if (IsRealNumber(dataread)) dvalue = atof(dataread); - else { + if (!getRealNumber(dataread, &dvalue)) { fprintf(stderr, "Error at file: %s. Variable: %s can't have non real value: %s.\n", filename, varname, dataread); fclose(data); free(varname); @@ -701,8 +704,7 @@ int LoadVariableData(namedvars varmap, char *filename) { idat++; break; case 1: - if (IsNumber(dataread)) ivalue = atoi(dataread); - else { + if (!getIntNumber(dataread, &ivalue)) { fprintf(stderr, "Error at file: %s. Variable: %s can't have non integer value: %s.\n", filename, varname, dataread); fclose(data); free(varname); @@ -1706,34 +1708,46 @@ void onlinetraverse(DdManager *manager, namedvars varmap, hisqueue *HisQueue, Dd inputline[icur] = '\0'; if ((icur > 0) && (inputline[0] == '@') && (inputline[2] == ',' || inputline[2] == '\0')) { switch(inputline[1]) { + case '?': + printf("Available instructions:\n\t@c : current node\n\t@n,[BFS, DFS] : expand and go to next node\n\t@t,[BFS, DFS] : throw and go to next node\n"); + printf("\t@h : high node of current\n\t@l : low node of current\n\t@v,[variable] : variable values\n\t@e terminates\n"); + break; case 'c': if (iRoot) { iRoot = 0; - printf("bdd_temp_value('%s', %i).\n", GetNodeVarNameDisp(manager, varmap, curnode), 1); + printf("bdd_temp_value('%s', %i, %p).\n", GetNodeVarNameDisp(manager, varmap, curnode), 1, (void *) curnode); } else { - printf("bdd_temp_value('%s', %i).\n", GetNodeVarNameDisp(manager, varmap, curnode), iQsize); + printf("bdd_temp_value('%s', %i, %p).\n", GetNodeVarNameDisp(manager, varmap, curnode), iQsize, (void *) curnode); } fflush(stdout); break; case 'n': if (curnode != HIGH(manager) && curnode != LOW(manager) && (hnode = GetNode(his, varmap.varstart, curnode)) == NULL) { - //AddNode(his, varmap.varstart, curnode, 0.0, 0, NULL); l_node = LowNodeOf(manager, curnode); h_node = HighNodeOf(manager, curnode); - inQ = 0; + iQsize += 2; + Q = (DdNode **) realloc(Q, sizeof(DdNode *) * iQsize); + Q[iQsize - 2] = l_node; + Q[iQsize - 1] = h_node; + //AddNode(his, varmap.varstart, curnode, 0.0, 0, NULL); +/* inQ = 0; for(i = 0; (i < iQsize / 2) && (inQ < 3); i++) - inQ = (Q[i] == l_node) || (Q[iQsize - i] == l_node) + 2 * (Q[i] == h_node) || (Q[iQsize - i] == h_node); - if ((inQ & 1) == 0) inQ = inQ + (GetNode(his, varmap.varstart, l_node) != NULL); - if ((inQ & 2) == 0) inQ = inQ + 2 * (GetNode(his, varmap.varstart, h_node) != NULL); - if ((inQ & 1) == 1) inQ = inQ - (l_node == HIGH(manager) || l_node == LOW(manager)); - if ((inQ & 2) == 2) inQ = inQ - 2 * (h_node == HIGH(manager) || h_node == LOW(manager)); - inQ = 0; + inQ = (Q[i] == l_node) || (Q[iQsize - i - 1] == l_node) + 2 * (Q[i] == h_node) || (Q[iQsize - i - 1] == h_node); + if ((l_node == HIGH(manager) || l_node == LOW(manager))) { + inQ = (inQ & 2); + } else { + if ((inQ & 1) == 0) inQ = inQ + (GetNode(his, varmap.varstart, l_node) != NULL); + } + if (h_node == HIGH(manager) || h_node == LOW(manager)) { + inQ = (inQ & 1); + } else { + if ((inQ & 2) == 0) inQ = inQ + 2 * (GetNode(his, varmap.varstart, h_node) != NULL); + }*/ +/* if ((inQ & 1) == 1) inQ = inQ - (l_node == HIGH(manager) || l_node == LOW(manager)); + if ((inQ & 2) == 2) inQ = inQ - 2 * (h_node == HIGH(manager) || h_node == LOW(manager));*/ +/* inQ = 0; switch(inQ) { case 0: - iQsize += 2; - Q = (DdNode **) realloc(Q, sizeof(DdNode *) * iQsize); - Q[iQsize - 2] = l_node; - Q[iQsize - 1] = h_node; break; case 1: iQsize++; @@ -1749,7 +1763,7 @@ void onlinetraverse(DdManager *manager, namedvars varmap, hisqueue *HisQueue, Dd break; default: break; - } + }*/ } if (inputline[2] == '\0' || strcmp(inputline + 3, "DFS") == 0) { if (iQsize > 0) { @@ -1774,6 +1788,30 @@ void onlinetraverse(DdManager *manager, namedvars varmap, hisqueue *HisQueue, Dd exit(-1); } break; + case 't': + if (inputline[2] == '\0' || strcmp(inputline + 3, "DFS") == 0) { + if (iQsize > 0) { + iQsize--; + curnode = Q[iQsize]; + Q = (DdNode **) realloc(Q, sizeof(DdNode *) * iQsize); + } + } else if (strcmp(inputline + 3, "BFS") == 0) { + if (iQsize > 0) { + iQsize--; + curnode = Q[0]; + Q2 = (DdNode **) malloc(sizeof(DdNode *) * iQsize); + for(i = 0; i < iQsize; i++) + Q2[i] = Q[i + 1]; + free(Q); + Q = Q2; + } + } else { + fprintf(stderr, "Error: Could not find method: %s, Correct syntax @n,[DFS, BFS].\n", inputline + 3); + free(Q); + free(inputline); + exit(-1); + } + break; case 'h': printf("bdd_temp_value('%s').\n", GetNodeVarNameDisp(manager, varmap, HighNodeOf(manager, curnode))); fflush(stdout); From c1c02d1e957a79b42a704d9696447ac9d65243c9 Mon Sep 17 00:00:00 2001 From: Theofrastos Mantadelis Date: Fri, 1 Oct 2010 12:04:45 +0200 Subject: [PATCH 02/14] Correcting some versioning issues --- packages/ProbLog/simplecudd/general.c | 19 ++ packages/ProbLog/simplecudd/general.h | 5 + packages/ProbLog/simplecudd/problogbdd.c | 11 +- packages/ProbLog/simplecudd/problogmath.c | 251 +++++++++++++--------- packages/ProbLog/simplecudd/problogmath.h | 5 +- 5 files changed, 175 insertions(+), 116 deletions(-) diff --git a/packages/ProbLog/simplecudd/general.c b/packages/ProbLog/simplecudd/general.c index 4637ff209..e13bf5cae 100644 --- a/packages/ProbLog/simplecudd/general.c +++ b/packages/ProbLog/simplecudd/general.c @@ -189,6 +189,25 @@ /* Number Handling */ +int getRealNumber(char *c, double *number) { + char *unparsed_string; + errno = 0; + *number = strtod(c, &unparsed_string); + return !(errno == ERANGE || unparsed_string == c || *unparsed_string != '\0'); +} + +int getIntNumber(char *c, int *number) { + char *unparsed_string; + errno = 0; + long int numberl = strtol(c, &unparsed_string, 10); + *number = (int) numberl; + return !(errno == ERANGE || unparsed_string == c || *unparsed_string != '\0' || numberl > INT_MAX || numberl < INT_MIN); +} + +inline int getPosNumber(char *c, int *number) { + return (getIntNumber(c, number) && *number >= 0); +} + int IsRealNumber(char *c) { int i, l; l = strlen(c); diff --git a/packages/ProbLog/simplecudd/general.h b/packages/ProbLog/simplecudd/general.h index e05def182..a872cc386 100644 --- a/packages/ProbLog/simplecudd/general.h +++ b/packages/ProbLog/simplecudd/general.h @@ -188,12 +188,17 @@ #include #include #include +#include +#include #define IsNumberDigit(c) ('0' <= c && c <= '9') #define IsSignDigit(c) (c == '+' || c == '-') #define isOperator(x) (x == '+' || x == '*' || x == '#' || x == '=') #define freadline(fd) freadstr(fd, "\n"); +int getRealNumber(char *c, double *number); +int getIntNumber(char *c, int *number); +inline int getPosNumber(char *c, int *number); int IsRealNumber(char *c); int IsPosNumber(const char *c); int IsNumber(const char *c); diff --git a/packages/ProbLog/simplecudd/problogbdd.c b/packages/ProbLog/simplecudd/problogbdd.c index a25eb33eb..1370b21b4 100644 --- a/packages/ProbLog/simplecudd/problogbdd.c +++ b/packages/ProbLog/simplecudd/problogbdd.c @@ -6,7 +6,7 @@ * Copyright Katholieke Universiteit Leuven 2008, 2009, 2010 * * * * Author: Theofrastos Mantadelis, Angelika Kimmig, Bernd Gutmann * -* File: ProblogBDD.c * +* File: problogbdd.c * * * ******************************************************************************** * * @@ -1451,8 +1451,7 @@ int LoadVariableDataForForest(namedvars varmap, char *filename) { if (hasvar >= 0) { switch(idat) { case 0: - if (IsRealNumber(dataread)) dvalue = atof(dataread); - else { + if (!getRealNumber(dataread, &dvalue)) { fprintf(stderr, "Error at file: %s. Variable: %s can't have non real value: %s.\n", filename, varname, dataread); fclose(data); free(varname); @@ -1462,8 +1461,7 @@ int LoadVariableDataForForest(namedvars varmap, char *filename) { idat++; break; case 1: - if (IsNumber(dataread)) ivalue = atoi(dataread); - else { + if (!getIntNumber(dataread, &ivalue)) { fprintf(stderr, "Error at file: %s. Variable: %s can't have non integer value: %s.\n", filename, varname, dataread); fclose(data); free(varname); @@ -1675,9 +1673,8 @@ parameters loadparam(int argc, char **arg) { } break; case 10: - if ((argc > i + 1) && (IsRealNumber(arg[i + 1]))) { + if ((argc > i + 1) && (getRealNumber(arg[i + 1], & params.sigmoid_slope))) { i++; - params.sigmoid_slope = atof(arg[i]); } else { params.error[params.errorcnt] = i; params.errorcnt++; diff --git a/packages/ProbLog/simplecudd/problogmath.c b/packages/ProbLog/simplecudd/problogmath.c index 2f21a5356..cdeb9bbaa 100644 --- a/packages/ProbLog/simplecudd/problogmath.c +++ b/packages/ProbLog/simplecudd/problogmath.c @@ -3,143 +3,182 @@ * SimpleCUDD library (www.cs.kuleuven.be/~theo/tools/simplecudd.html) * * SimpleCUDD was developed at Katholieke Universiteit Leuven(www.kuleuven.be) * * * -* Copyright T. Mantadelis, A. Kimmig, B. Gutmann * -* and Katholieke Universiteit Leuven 2008 * -* * -* Author: Bernd Gutmann * -* File: problogmath.c * -* $Date:: 2010-09-30 14:29:42 +0200 (Thu, 30 Sep 2010) $ * -* $Revision:: 4858 $ * +* Copyright Katholieke Universiteit Leuven 2008, 2009, 2010 * * * +* Author: Bernd Gutmann * +* File: problogmath.c * * * ******************************************************************************** * * -* The "Artistic License" * +* Artistic License 2.0 * * * -* Preamble * +* Copyright (c) 2000-2006, The Perl Foundation. * * * -* The intent of this document is to state the conditions under which a * -* Package may be copied, such that the Copyright Holder maintains some * -* semblance of artistic control over the development of the package, * -* while giving the users of the package the right to use and distribute * -* the Package in a more-or-less customary fashion, plus the right to make * -* reasonable modifications. * +* Everyone is permitted to copy and distribute verbatim copies of this license * +* document, but changing it is not allowed. * * * -* Definitions: * +* Preamble * * * -* "Package" refers to the collection of files distributed by the * -* Copyright Holder, and derivatives of that collection of files * -* created through textual modification. * +* This license establishes the terms under which a given free software Package * +* may be copied, modified, distributed, and/or redistributed. The intent is * +* that the Copyright Holder maintains some artistic control over the * +* development of that Package while still keeping the Package available as * +* open source and free software. * * * -* "Standard Version" refers to such a Package if it has not been * -* modified, or has been modified in accordance with the wishes * -* of the Copyright Holder as specified below. * +* You are always permitted to make arrangements wholly outside of this license * +* directly with the Copyright Holder of a given Package. If the terms of this * +* license do not permit the full use that you propose to make of the Package, * +* you should contact the Copyright Holder and seek a different licensing * +* arrangement. * +* Definitions * * * -* "Copyright Holder" is whoever is named in the copyright or * -* copyrights for the package. * +* "Copyright Holder" means the individual(s) or organization(s) named in the * +* copyright notice for the entire Package. * * * -* "You" is you, if you're thinking about copying or distributing * -* this Package. * +* "Contributor" means any party that has contributed code or other material to * +* the Package, in accordance with the Copyright Holder's procedures. * * * -* "Reasonable copying fee" is whatever you can justify on the * -* basis of media cost, duplication charges, time of people involved, * -* and so on. (You will not be required to justify it to the * -* Copyright Holder, but only to the computing community at large * -* as a market that must bear the fee.) * +* "You" and "your" means any person who would like to copy, distribute, or * +* modify the Package. * * * -* "Freely Available" means that no fee is charged for the item * -* itself, though there may be fees involved in handling the item. * -* It also means that recipients of the item may redistribute it * -* under the same conditions they received it. * +* "Package" means the collection of files distributed by the Copyright Holder, * +* and derivatives of that collection and/or of those files. A given Package * +* may consist of either the Standard Version, or a Modified Version. * * * -* 1. You may make and give away verbatim copies of the source form of the * -* Standard Version of this Package without restriction, provided that you * -* duplicate all of the original copyright notices and associated disclaimers. * +* "Distribute" means providing a copy of the Package or making it accessible * +* to anyone else, or in the case of a company or organization, to others * +* outside of your company or organization. * * * -* 2. You may apply bug fixes, portability fixes and other modifications * -* derived from the Public Domain or from the Copyright Holder. A Package * -* modified in such a way shall still be considered the Standard Version. * +* "Distributor Fee" means any fee that you charge for Distributing this * +* Package or providing support for this Package to another party. It does not * +* mean licensing fees. * * * -* 3. You may otherwise modify your copy of this Package in any way, provided * -* that you insert a prominent notice in each changed file stating how and * -* when you changed that file, and provided that you do at least ONE of the * -* following: * +* "Standard Version" refers to the Package if it has not been modified, or has * +* been modified only in ways explicitly requested by the Copyright Holder. * * * -* a) place your modifications in the Public Domain or otherwise make them * -* Freely Available, such as by posting said modifications to Usenet or * -* an equivalent medium, or placing the modifications on a major archive * -* site such as uunet.uu.net, or by allowing the Copyright Holder to include * -* your modifications in the Standard Version of the Package. * +* "Modified Version" means the Package, if it has been changed, and such * +* changes were not explicitly requested by the Copyright Holder. * * * -* b) use the modified Package only within your corporation or organization. * +* "Original License" means this Artistic License as Distributed with the * +* Standard Version of the Package, in its current version or as it may be * +* modified by The Perl Foundation in the future. * * * -* c) rename any non-standard executables so the names do not conflict * -* with standard executables, which must also be provided, and provide * -* a separate manual page for each non-standard executable that clearly * -* documents how it differs from the Standard Version. * +* "Source" form means the source code, documentation source, and configuration * +* files for the Package. * * * -* d) make other distribution arrangements with the Copyright Holder. * +* "Compiled" form means the compiled bytecode, object code, binary, or any * +* other form resulting from mechanical transformation or translation of the * +* Source form. * +* Permission for Use and Modification Without Distribution * * * -* 4. You may distribute the programs of this Package in object code or * -* executable form, provided that you do at least ONE of the following: * +* (1) You are permitted to use the Standard Version and create and use * +* Modified Versions for any purpose without restriction, provided that you do * +* not Distribute the Modified Version. * +* Permissions for Redistribution of the Standard Version * * * -* a) distribute a Standard Version of the executables and library files, * -* together with instructions (in the manual page or equivalent) on where * -* to get the Standard Version. * +* (2) You may Distribute verbatim copies of the Source form of the Standard * +* Version of this Package in any medium without restriction, either gratis or * +* for a Distributor Fee, provided that you duplicate all of the original * +* copyright notices and associated disclaimers. At your discretion, such * +* verbatim copies may or may not include a Compiled form of the Package. * * * -* b) accompany the distribution with the machine-readable source of * -* the Package with your modifications. * +* (3) You may apply any bug fixes, portability changes, and other * +* modifications made available from the Copyright Holder. The resulting * +* Package will still be considered the Standard Version, and as such will be * +* subject to the Original License. * +* Distribution of Modified Versions of the Package as Source * * * -* c) give non-standard executables non-standard names, and clearly * -* document the differences in manual pages (or equivalent), together * -* with instructions on where to get the Standard Version. * +* (4) You may Distribute your Modified Version as Source (either gratis or for * +* a Distributor Fee, and with or without a Compiled form of the Modified * +* Version) provided that you clearly document how it differs from the Standard * +* Version, including, but not limited to, documenting any non-standard * +* features, executables, or modules, and provided that you do at least ONE of * +* the following: * * * -* d) make other distribution arrangements with the Copyright Holder. * +* (a) make the Modified Version available to the Copyright Holder of the * +* Standard Version, under the Original License, so that the Copyright Holder * +* may include your modifications in the Standard Version. * +* (b) ensure that installation of your Modified Version does not prevent the * +* user installing or running the Standard Version. In addition, the Modified * +* Version must bear a name that is different from the name of the Standard * +* Version. * +* (c) allow anyone who receives a copy of the Modified Version to make the * +* Source form of the Modified Version available to others under * +* (i) the Original License or * +* (ii) a license that permits the licensee to freely copy, modify and * +* redistribute the Modified Version using the same licensing terms that apply * +* to the copy that the licensee received, and requires that the Source form of * +* the Modified Version, and of any works derived from it, be made freely * +* available in that license fees are prohibited but Distributor Fees are * +* allowed. * +* Distribution of Compiled Forms of the Standard Version or Modified Versions * +* without the Source * * * -* 5. You may charge a reasonable copying fee for any distribution of this * -* Package. You may charge any fee you choose for support of this * -* Package. You may not charge a fee for this Package itself. However, * -* you may distribute this Package in aggregate with other (possibly * -* commercial) programs as part of a larger (possibly commercial) software * -* distribution provided that you do not advertise this Package as a * -* product of your own. You may embed this Package's interpreter within * -* an executable of yours (by linking); this shall be construed as a mere * -* form of aggregation, provided that the complete Standard Version of the * -* interpreter is so embedded. * +* (5) You may Distribute Compiled forms of the Standard Version without the * +* Source, provided that you include complete instructions on how to get the * +* Source of the Standard Version. Such instructions must be valid at the time * +* of your distribution. If these instructions, at any time while you are * +* carrying out such distribution, become invalid, you must provide new * +* instructions on demand or cease further distribution. If you provide valid * +* instructions or cease distribution within thirty days after you become aware * +* that the instructions are invalid, then you do not forfeit any of your * +* rights under this license. * * * -* 6. The scripts and library files supplied as input to or produced as * -* output from the programs of this Package do not automatically fall * -* under the copyright of this Package, but belong to whoever generated * -* them, and may be sold commercially, and may be aggregated with this * -* Package. If such scripts or library files are aggregated with this * -* Package via the so-called "undump" or "unexec" methods of producing a * -* binary executable image, then distribution of such an image shall * -* neither be construed as a distribution of this Package nor shall it * -* fall under the restrictions of Paragraphs 3 and 4, provided that you do * -* not represent such an executable image as a Standard Version of this * -* Package. * +* (6) You may Distribute a Modified Version in Compiled form without the * +* Source, provided that you comply with Section 4 with respect to the Source * +* of the Modified Version. * +* Aggregating or Linking the Package * * * -* 7. C subroutines (or comparably compiled subroutines in other * -* languages) supplied by you and linked into this Package in order to * -* emulate subroutines and variables of the language defined by this * -* Package shall not be considered part of this Package, but are the * -* equivalent of input as in Paragraph 6, provided these subroutines do * -* not change the language in any way that would cause it to fail the * -* regression tests for the language. * +* (7) You may aggregate the Package (either the Standard Version or Modified * +* Version) with other packages and Distribute the resulting aggregation * +* provided that you do not charge a licensing fee for the Package. Distributor * +* Fees are permitted, and licensing fees for other components in the * +* aggregation are permitted. The terms of this license apply to the use and * +* Distribution of the Standard or Modified Versions as included in the * +* aggregation. * * * -* 8. Aggregation of this Package with a commercial distribution is always * -* permitted provided that the use of this Package is embedded; that is, * -* when no overt attempt is made to make this Package's interfaces visible * -* to the end user of the commercial distribution. Such use shall not be * -* construed as a distribution of this Package. * +* (8) You are permitted to link Modified and Standard Versions with other * +* works, to embed the Package in a larger work of your own, or to build * +* stand-alone binary or bytecode versions of applications that include the * +* Package, and Distribute the result without restriction, provided the result * +* does not expose a direct interface to the Package. * +* Items That are Not Considered Part of a Modified Version * * * -* 9. The name of the Copyright Holder may not be used to endorse or promote * -* products derived from this software without specific prior written * -* permission. * +* (9) Works (including, but not limited to, modules and scripts) that merely * +* extend or make use of the Package, do not, by themselves, cause the Package * +* to be a Modified Version. In addition, such works are not considered parts * +* of the Package itself, and are not subject to the terms of this license. * +* General Provisions * * * -* 10. THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR * -* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED * -* WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. * +* (10) Any use, modification, and distribution of the Standard or Modified * +* Versions is governed by this Artistic License. By using, modifying or * +* distributing the Package, you accept this license. Do not use, modify, or * +* distribute the Package, if you do not accept this license. * +* * +* (11) If your Modified Version has been derived from a Modified Version made * +* by someone other than you, you are nevertheless required to ensure that your * +* Modified Version complies with the requirements of this license. * +* * +* (12) This license does not grant you the right to use any trademark, service * +* mark, tradename, or logo of the Copyright Holder. * +* * +* (13) This license includes the non-exclusive, worldwide, free-of-charge * +* patent license to make, have made, use, offer to sell, sell, import and * +* otherwise transfer the Package with respect to any patent claims licensable * +* by the Copyright Holder that are necessarily infringed by the Package. If * +* you institute patent litigation (including a cross-claim or counterclaim) * +* against any party alleging that the Package constitutes direct or * +* contributory patent infringement, then this Artistic License to you shall * +* terminate on the date that such litigation is filed. * +* * +* (14) Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER * +* AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES. THE * +* IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR * +* NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY YOUR LOCAL LAW. * +* UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR CONTRIBUTOR WILL BE LIABLE * +* FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING IN * +* ANY WAY OUT OF THE USE OF THE PACKAGE, EVEN IF ADVISED OF THE POSSIBILITY OF * +* SUCH DAMAGE. * * * * The End * * * diff --git a/packages/ProbLog/simplecudd/problogmath.h b/packages/ProbLog/simplecudd/problogmath.h index 17a7c8270..8734d6f88 100644 --- a/packages/ProbLog/simplecudd/problogmath.h +++ b/packages/ProbLog/simplecudd/problogmath.h @@ -7,9 +7,8 @@ * * * Author: Bernd Gutmann * * File: problogmath.h * -* $Date:: 2010-08-25 15:23:30 +0200 (Wed, 25 Aug 2010) $ * -* $Revision:: 4683 $ * -* * +* $Date:: 2010-02-18 18:04:54 +0100 (Thu, 18 Feb 2010) $ * +* $Revision:: 3623 $ * * * ******************************************************************************** * * From 047f1ad809dbf3c3b1901eb516673a66b49f5728 Mon Sep 17 00:00:00 2001 From: Paulo Moura Date: Mon, 4 Oct 2010 19:35:22 +0100 Subject: [PATCH 03/14] Corrected some spelling errors in the user manual. --- docs/yap.tex | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/docs/yap.tex b/docs/yap.tex index c52d6e974..582214752 100644 --- a/docs/yap.tex +++ b/docs/yap.tex @@ -2651,7 +2651,7 @@ Note also that you can use chains of commit operators like: Note that @code{(->)/2} does not affect the scope of cuts in its arguments. -@item +@var{Conditon} *-> +@var{Action} ; +@var{Else} +@item +@var{Condition} *-> +@var{Action} ; +@var{Else} @findex ->*/2 @snindex ->*/2 @cnindex ->*/2 @@ -3593,13 +3593,13 @@ the call. @item digit @var{Char} is a digit. -@item digit(@var{Weigth}) +@item digit(@var{Weight}) @var{Char} is a digit with value - @var{Weigth}. I.e. @code{char_type(X, digit(6))} yields @code{X = + @var{Weight}. I.e. @code{char_type(X, digit(6))} yields @code{X = '6'}. Useful for parsing numbers. -@item xdigit(@var{Weigth}) - @var{Char} is a hexa-decimal digit with value @var{Weigth}. I.e. char_type(a, xdigit(X) yields X = '10'. Useful for parsing numbers. +@item xdigit(@var{Weight}) + @var{Char} is a hexa-decimal digit with value @var{Weight}. I.e. char_type(a, xdigit(X) yields X = '10'. Useful for parsing numbers. @item graph @var{Char} produces a visible mark on a page when printed. Note that the space is not included! @@ -3816,7 +3816,7 @@ of length @var{S}. @node Arithmetic, I/O, Comparing Terms, Top @section Arithmetic -YAP now supposets several different numeric types: +YAP now supports several different numeric types: @table @code @item integers @@ -4712,9 +4712,9 @@ Unify @var{LinePosition} with the position on current text stream @cnindex stream_position/2 Unify @var{StreamPosition} with the packaged information of position on current stream @var{Stream}. Use @code{stream_position_data/3} to -retrieve information on charater or line count. +retrieve information on character or line count. -@item stream_position_data(+@var{Field},+@var{StreamPsition},-@var{Info}) +@item stream_position_data(+@var{Field},+@var{StreamPosition},-@var{Info}) @findex stream_position_data/3 @syindex stream_position_data/3 @cnindex stream_position_data/3 @@ -8036,7 +8036,7 @@ profiling information. Profiling data can be read through the @item prompt_alternatives_on(atom, changeable) @findex prompt_alternatives_on (yap_flag/2 option) -SWI-Compatible opttion, determines prompting for alternatives in the Prolog toplevel. Default is @t{groundness}, YAP prompts for alternatives if and only if the query contains variables. The alternative, default in SWI-Prolog is @t{determinism} which implies the system prompts for alternatives if the goal succeeded while leaving choicepoints. +SWI-Compatible option, determines prompting for alternatives in the Prolog toplevel. Default is @t{groundness}, YAP prompts for alternatives if and only if the query contains variables. The alternative, default in SWI-Prolog is @t{determinism} which implies the system prompts for alternatives if the goal succeeded while leaving choicepoints. @item redefine_warnings @@ -8620,7 +8620,7 @@ The predicates are: No @end example - Notice that @var{Goal} is copied repeatetly, which may cause + Notice that @var{Goal} is copied repeatedly, which may cause problems if attributed variables are involved. @item [det]free_variables(:Generator, +@var{Template}, +VarList0, -VarList) @@ -9169,7 +9169,7 @@ need not be ordered. This package provides a set of useful predicates to manipulate sequences of characters codes, usually first read in as a line. It is -avalailable by loading the library @code{library(lineutils)}. +available by loading the library @code{library(lineutils)}. @table @code From 5323a3677d6e1688116d9ec957faa472c44b6b29 Mon Sep 17 00:00:00 2001 From: Theofrastos Mantadelis Date: Tue, 5 Oct 2010 18:26:40 +0200 Subject: [PATCH 04/14] ProbLog Versioning System --- packages/ProbLog/problog.yap | 13 +- packages/ProbLog/problog/os.yap | 25 +- packages/ProbLog/problog/print.yap | 36 +- packages/ProbLog/problog/version_control.yap | 241 ++ packages/ProbLog/problog_examples/graph.pl | 1 + .../ProbLog/problog_examples/graph_tabled.pl | 1 + .../ProbLog/problog_examples/learn_graph.pl | 18 +- packages/ProbLog/problog_examples/office.pl | 11 +- packages/ProbLog/problog_examples/problog.yap | 3133 +++++++++++++++++ .../problog_examples/problog_learning.yap | 1677 +++++++++ .../problog_examples/viralmarketing.pl | 6 + .../problog_examples/viralmarketing_tabled.pl | 6 + packages/ProbLog/problog_learning.yap | 8 +- 13 files changed, 5136 insertions(+), 40 deletions(-) create mode 100644 packages/ProbLog/problog/version_control.yap create mode 100644 packages/ProbLog/problog_examples/problog.yap create mode 100644 packages/ProbLog/problog_examples/problog_learning.yap diff --git a/packages/ProbLog/problog.yap b/packages/ProbLog/problog.yap index 42eb9af8c..873c53837 100644 --- a/packages/ProbLog/problog.yap +++ b/packages/ProbLog/problog.yap @@ -2,8 +2,8 @@ %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % -% $Date: 2010-09-30 14:48:30 +0200 (Thu, 30 Sep 2010) $ -% $Revision: 4859 $ +% $Date: 2010-10-05 18:15:57 +0200 (Tue, 05 Oct 2010) $ +% $Revision: 4876 $ % % This file is part of ProbLog % http://dtai.cs.kuleuven.be/problog @@ -505,14 +505,6 @@ init_global_params :- problog_define_flag(mc_logfile, problog_flag_validate_file, 'logfile for montecarlo', 'log.txt', mcmc, flags:working_file_handler), check_existance('problogbdd'). -check_existance(FileName):- - 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). - % parameter initialization to be called after returning to user's directory: :- initialization(init_global_params). @@ -3139,4 +3131,3 @@ user:term_expansion(Term,ExpandedTerm) :- prolog_load_context(module,Mod), problog:term_expansion_intern(Term,Mod,ExpandedTerm). - diff --git a/packages/ProbLog/problog/os.yap b/packages/ProbLog/problog/os.yap index cc677f026..6d3f11a14 100644 --- a/packages/ProbLog/problog/os.yap +++ b/packages/ProbLog/problog/os.yap @@ -2,8 +2,8 @@ %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % -% $Date: 2010-09-30 16:05:52 +0200 (Thu, 30 Sep 2010) $ -% $Revision: 4863 $ +% $Date: 2010-10-05 18:15:57 +0200 (Tue, 05 Oct 2010) $ +% $Revision: 4876 $ % % This file is part of ProbLog % http://dtai.cs.kuleuven.be/problog @@ -208,12 +208,13 @@ % 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, - split_path_file/3, - calc_md5/2]). + problog_path/1, + convert_filename_to_working_path/2, + convert_filename_to_problog_path/2, + concat_path_with_filename/3, + split_path_file/3, + check_existance/1, + calc_md5/2]). % load library modules @@ -316,3 +317,11 @@ split_path_file(PathFile, Path, File):- \+ memberchk(PathSeperatorName, FileName), !. % (Path = '' ; atomic_concat(_, PathSeperator, Path)). + +check_existance(FileName):- + 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). diff --git a/packages/ProbLog/problog/print.yap b/packages/ProbLog/problog/print.yap index 3d55ec691..60395b476 100644 --- a/packages/ProbLog/problog/print.yap +++ b/packages/ProbLog/problog/print.yap @@ -2,8 +2,8 @@ %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % -% $Date: 2010-09-28 21:04:43 +0200 (Tue, 28 Sep 2010) $ -% $Revision: 4838 $ +% $Date: 2010-10-05 18:15:57 +0200 (Tue, 05 Oct 2010) $ +% $Revision: 4876 $ % % This file is part of ProbLog % http://dtai.cs.kuleuven.be/problog @@ -226,10 +226,13 @@ % load library modules :- use_module(library(lists), [member/2]). +:- use_module(library(system), [directory_files/2]). % load our own modules :- use_module(flags). :- use_module(variables). +:- use_module(os, [check_existance/1, convert_filename_to_problog_path/2]). +:- use_module(version_control, [get_version/3]). % size, line_char, line_char_bold @@ -296,10 +299,11 @@ make_column_format([HC|TC], [HS|TS], Format, Acc):- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% problog_help :- - format('~2nProbLog inference currently offers the following inference methods:~n',[]), + print_version, + format('~nProbLog inference currently offers the following inference methods:~n',[]), show_inference, problog:problog_path(PD), - format('~nProblog directory: ~q~n',[PD]), + format('~nProbLog directory: ~q~n',[PD]), format('~nThe following global parameters are available:~n',[]), problog_flags, print_sep_line, @@ -310,6 +314,30 @@ problog_help :- nl, flush_output. +print_version :- + nl, + print_group_line('Version Information'), + check_existance('problog.yap'), + convert_filename_to_problog_path('problog.yap', Path), + get_version(Path, Version, Revision), + format('~w~35+ Last Modified at:~w~65+Revision:~w~n', ['problog.yap', Version, Revision]), + convert_filename_to_problog_path('problog', ProblogPath), + directory_files(ProblogPath, ProblogFiles), + print_version(ProblogFiles), + print_sep_line. + +print_version([]). +print_version([H|T]):- + atom_concat('.', _, H), !, + print_version(T). +print_version([H|T]):- + atom_concat('problog/', H, FileName), + convert_filename_to_problog_path(FileName, FilePath), + get_version(FilePath, Version, Revision), + format('~w~35+ Last Modified at:~w~65+Revision:~w~n', [FileName, Version, Revision]), + print_version(T). + + show_inference :- format('~n',[]), print_sep_line, diff --git a/packages/ProbLog/problog/version_control.yap b/packages/ProbLog/problog/version_control.yap new file mode 100644 index 000000000..0c86057ba --- /dev/null +++ b/packages/ProbLog/problog/version_control.yap @@ -0,0 +1,241 @@ +%%% -*- Mode: Prolog; -*- + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% +% $Date: 2010-10-05 18:15:57 +0200 (Tue, 05 Oct 2010) $ +% $Revision: 4876 $ +% +% This file is part of ProbLog +% http://dtai.cs.kuleuven.be/problog +% +% ProbLog was developed at Katholieke Universiteit Leuven +% +% Copyright 2008, 2009, 2010 +% Katholieke Universiteit Leuven +% +% Main authors of this file: +% Theofrastos Mantadelis +% +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% +% Artistic License 2.0 +% +% Copyright (c) 2000-2006, The Perl Foundation. +% +% Everyone is permitted to copy and distribute verbatim copies of this +% license document, but changing it is not allowed. Preamble +% +% This license establishes the terms under which a given free software +% Package may be copied, modified, distributed, and/or +% redistributed. The intent is that the Copyright Holder maintains some +% artistic control over the development of that Package while still +% keeping the Package available as open source and free software. +% +% You are always permitted to make arrangements wholly outside of this +% license directly with the Copyright Holder of a given Package. If the +% terms of this license do not permit the full use that you propose to +% make of the Package, you should contact the Copyright Holder and seek +% a different licensing arrangement. Definitions +% +% "Copyright Holder" means the individual(s) or organization(s) named in +% the copyright notice for the entire Package. +% +% "Contributor" means any party that has contributed code or other +% material to the Package, in accordance with the Copyright Holder's +% procedures. +% +% "You" and "your" means any person who would like to copy, distribute, +% or modify the Package. +% +% "Package" means the collection of files distributed by the Copyright +% Holder, and derivatives of that collection and/or of those files. A +% given Package may consist of either the Standard Version, or a +% Modified Version. +% +% "Distribute" means providing a copy of the Package or making it +% accessible to anyone else, or in the case of a company or +% organization, to others outside of your company or organization. +% +% "Distributor Fee" means any fee that you charge for Distributing this +% Package or providing support for this Package to another party. It +% does not mean licensing fees. +% +% "Standard Version" refers to the Package if it has not been modified, +% or has been modified only in ways explicitly requested by the +% Copyright Holder. +% +% "Modified Version" means the Package, if it has been changed, and such +% changes were not explicitly requested by the Copyright Holder. +% +% "Original License" means this Artistic License as Distributed with the +% Standard Version of the Package, in its current version or as it may +% be modified by The Perl Foundation in the future. +% +% "Source" form means the source code, documentation source, and +% configuration files for the Package. +% +% "Compiled" form means the compiled bytecode, object code, binary, or +% any other form resulting from mechanical transformation or translation +% of the Source form. +% +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% +% Permission for Use and Modification Without Distribution +% +% (1) You are permitted to use the Standard Version and create and use +% Modified Versions for any purpose without restriction, provided that +% you do not Distribute the Modified Version. +% +% Permissions for Redistribution of the Standard Version +% +% (2) You may Distribute verbatim copies of the Source form of the +% Standard Version of this Package in any medium without restriction, +% either gratis or for a Distributor Fee, provided that you duplicate +% all of the original copyright notices and associated disclaimers. At +% your discretion, such verbatim copies may or may not include a +% Compiled form of the Package. +% +% (3) You may apply any bug fixes, portability changes, and other +% modifications made available from the Copyright Holder. The resulting +% Package will still be considered the Standard Version, and as such +% will be subject to the Original License. +% +% Distribution of Modified Versions of the Package as Source +% +% (4) You may Distribute your Modified Version as Source (either gratis +% or for a Distributor Fee, and with or without a Compiled form of the +% Modified Version) provided that you clearly document how it differs +% from the Standard Version, including, but not limited to, documenting +% any non-standard features, executables, or modules, and provided that +% you do at least ONE of the following: +% +% (a) make the Modified Version available to the Copyright Holder of the +% Standard Version, under the Original License, so that the Copyright +% Holder may include your modifications in the Standard Version. (b) +% ensure that installation of your Modified Version does not prevent the +% user installing or running the Standard Version. In addition, the +% modified Version must bear a name that is different from the name of +% the Standard Version. (c) allow anyone who receives a copy of the +% Modified Version to make the Source form of the Modified Version +% available to others under (i) the Original License or (ii) a license +% that permits the licensee to freely copy, modify and redistribute the +% Modified Version using the same licensing terms that apply to the copy +% that the licensee received, and requires that the Source form of the +% Modified Version, and of any works derived from it, be made freely +% available in that license fees are prohibited but Distributor Fees are +% allowed. +% +% Distribution of Compiled Forms of the Standard Version or +% Modified Versions without the Source +% +% (5) You may Distribute Compiled forms of the Standard Version without +% the Source, provided that you include complete instructions on how to +% get the Source of the Standard Version. Such instructions must be +% valid at the time of your distribution. If these instructions, at any +% time while you are carrying out such distribution, become invalid, you +% must provide new instructions on demand or cease further +% distribution. If you provide valid instructions or cease distribution +% within thirty days after you become aware that the instructions are +% invalid, then you do not forfeit any of your rights under this +% license. +% +% (6) You may Distribute a Modified Version in Compiled form without the +% Source, provided that you comply with Section 4 with respect to the +% Source of the Modified Version. +% +% Aggregating or Linking the Package +% +% (7) You may aggregate the Package (either the Standard Version or +% Modified Version) with other packages and Distribute the resulting +% aggregation provided that you do not charge a licensing fee for the +% Package. Distributor Fees are permitted, and licensing fees for other +% components in the aggregation are permitted. The terms of this license +% apply to the use and Distribution of the Standard or Modified Versions +% as included in the aggregation. +% +% (8) You are permitted to link Modified and Standard Versions with +% other works, to embed the Package in a larger work of your own, or to +% build stand-alone binary or bytecode versions of applications that +% include the Package, and Distribute the result without restriction, +% provided the result does not expose a direct interface to the Package. +% +% Items That are Not Considered Part of a Modified Version +% +% (9) Works (including, but not limited to, modules and scripts) that +% merely extend or make use of the Package, do not, by themselves, cause +% the Package to be a Modified Version. In addition, such works are not +% considered parts of the Package itself, and are not subject to the +% terms of this license. +% +% General Provisions +% +% (10) Any use, modification, and distribution of the Standard or +% Modified Versions is governed by this Artistic License. By using, +% modifying or distributing the Package, you accept this license. Do not +% use, modify, or distribute the Package, if you do not accept this +% license. +% +% (11) If your Modified Version has been derived from a Modified Version +% made by someone other than you, you are nevertheless required to +% ensure that your Modified Version complies with the requirements of +% this license. +% +% (12) This license does not grant you the right to use any trademark, +% service mark, tradename, or logo of the Copyright Holder. +% +% (13) This license includes the non-exclusive, worldwide, +% free-of-charge patent license to make, have made, use, offer to sell, +% sell, import and otherwise transfer the Package with respect to any +% patent claims licensable by the Copyright Holder that are necessarily +% infringed by the Package. If you institute patent litigation +% (including a cross-claim or counterclaim) against any party alleging +% that the Package constitutes direct or contributory patent +% infringement, then this Artistic License to you shall terminate on the +% date that such litigation is filed. +% +% (14) Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT +% HOLDER AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED +% WARRANTIES. THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A +% PARTICULAR PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT +% PERMITTED BY YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT +% HOLDER OR CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, +% INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE +% OF THE PACKAGE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +% +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% Figuring out the version of a problog file in a nasty way :-D +% This way SVN does the work of keeping the file date +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + +:- module(version_control, [get_version/3]). + +:- use_module(library(lists), [append/3, reverse/2]). + +get_version(FilePath, Version, Revision):- + open(FilePath, read, SI), + repeat, + get_line(SI, Line), + (append(_, ['D','a','t','e',':'|Date], Line) ; at_end_of_stream(SI)), + !, + repeat, + get_line(SI, NLine), + (append(_, ['R','e','v','i','s','i','o','n',':'|Revision1], NLine) ; at_end_of_stream(SI)), + !, + close(SI), + append(ReadDate,['$'|_],Date), + append(Revision2,['$'|_],Revision1), + atomic_concat(ReadDate, Version), + atomic_concat(Revision2, Revision). + +get_line(Stream, Line):- + get_line(Stream, LineR, []), + reverse(LineR, Line). +get_line(Stream, Line, Acc):- + get_char(Stream, Char), + ((Char = '\n' ; at_end_of_stream(Stream)) -> + Line = Acc + ; + get_line(Stream, Line, [Char|Acc]) + ). diff --git a/packages/ProbLog/problog_examples/graph.pl b/packages/ProbLog/problog_examples/graph.pl index cacb94297..aedbc9f4c 100644 --- a/packages/ProbLog/problog_examples/graph.pl +++ b/packages/ProbLog/problog_examples/graph.pl @@ -3,6 +3,7 @@ %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % ProbLog program describing a probabilistic graph % (running example from ProbLog presentations) +% $Id: graph.pl 4875 2010-10-05 15:28:35Z theo $ %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% :- use_module('../problog'). diff --git a/packages/ProbLog/problog_examples/graph_tabled.pl b/packages/ProbLog/problog_examples/graph_tabled.pl index 5a37f2ada..10e2b3465 100644 --- a/packages/ProbLog/problog_examples/graph_tabled.pl +++ b/packages/ProbLog/problog_examples/graph_tabled.pl @@ -3,6 +3,7 @@ %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % ProbLog program describing a probabilistic graph using tabling % (running example from ProbLog presentations) +% $Id: graph_tabled.pl 4875 2010-10-05 15:28:35Z theo $ %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% :- use_module('../problog'). diff --git a/packages/ProbLog/problog_examples/learn_graph.pl b/packages/ProbLog/problog_examples/learn_graph.pl index f5cb0fdc9..ff75fe1b4 100644 --- a/packages/ProbLog/problog_examples/learn_graph.pl +++ b/packages/ProbLog/problog_examples/learn_graph.pl @@ -3,13 +3,15 @@ %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % ProbLog program describing a probabilistic graph % (running example from ProbLog presentations) -% +% $Id: learn_graph.pl 4875 2010-10-05 15:28:35Z theo $ +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % example for parameter learning with LeProbLog % % training and test examples are included at the end of the file -% -% query ?- do_learning(20). -% will run 20 iterations of learning with default settings +% query ?- do_learning(20). +% will run 20 iterations of learning with default settings %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% :- use_module('../problog'). @@ -23,10 +25,10 @@ path(X,Y) :- path(X,Y,[X],_). path(X,X,A,A). path(X,Y,A,R) :- - X\==Y, - edge(X,Z), - absent(Z,A), - path(Z,Y,[Z|A],R). + X\==Y, + edge(X,Z), + absent(Z,A), + path(Z,Y,[Z|A],R). % using directed edges in both directions edge(X,Y) :- dir_edge(Y,X). diff --git a/packages/ProbLog/problog_examples/office.pl b/packages/ProbLog/problog_examples/office.pl index 76cfd988e..115a22d4a 100644 --- a/packages/ProbLog/problog_examples/office.pl +++ b/packages/ProbLog/problog_examples/office.pl @@ -2,12 +2,8 @@ %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % ProbLog program describing an office window -% % example for using hybrid ProbLog -% -% query ?- problog_exact(room_has_window, Prob, Status). -% Prob = 0.008527075, -% Status = ok ? +% $Id: office.pl 4876 2010-10-05 16:15:57Z theo $ %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% :- use_module('../problog'). @@ -25,3 +21,8 @@ room_has_window:- room_has_window:- in_corridor,corridor_has_window. +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% query ?- problog_exact(room_has_window, Prob, Status). +% Prob = 0.01517076, +% Status = ok ? +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% diff --git a/packages/ProbLog/problog_examples/problog.yap b/packages/ProbLog/problog_examples/problog.yap new file mode 100644 index 000000000..873c53837 --- /dev/null +++ b/packages/ProbLog/problog_examples/problog.yap @@ -0,0 +1,3133 @@ +%%% -*- Mode: Prolog; -*- + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% +% $Date: 2010-10-05 18:15:57 +0200 (Tue, 05 Oct 2010) $ +% $Revision: 4876 $ +% +% This file is part of ProbLog +% http://dtai.cs.kuleuven.be/problog +% +% ProbLog was developed at Katholieke Universiteit Leuven +% +% Copyright 2008, 2009, 2010 +% Katholieke Universiteit Leuven +% +% Main authors of this file: +% Angelika Kimmig, Vitor Santos Costa,Bernd Gutmann, +% Theofrastos Mantadelis, Guy Van den Broeck +% +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% +% Artistic License 2.0 +% +% Copyright (c) 2000-2006, The Perl Foundation. +% +% Everyone is permitted to copy and distribute verbatim copies of this +% license document, but changing it is not allowed. Preamble +% +% This license establishes the terms under which a given free software +% Package may be copied, modified, distributed, and/or +% redistributed. The intent is that the Copyright Holder maintains some +% artistic control over the development of that Package while still +% keeping the Package available as open source and free software. +% +% You are always permitted to make arrangements wholly outside of this +% license directly with the Copyright Holder of a given Package. If the +% terms of this license do not permit the full use that you propose to +% make of the Package, you should contact the Copyright Holder and seek +% a different licensing arrangement. Definitions +% +% "Copyright Holder" means the individual(s) or organization(s) named in +% the copyright notice for the entire Package. +% +% "Contributor" means any party that has contributed code or other +% material to the Package, in accordance with the Copyright Holder's +% procedures. +% +% "You" and "your" means any person who would like to copy, distribute, +% or modify the Package. +% +% "Package" means the collection of files distributed by the Copyright +% Holder, and derivatives of that collection and/or of those files. A +% given Package may consist of either the Standard Version, or a +% Modified Version. +% +% "Distribute" means providing a copy of the Package or making it +% accessible to anyone else, or in the case of a company or +% organization, to others outside of your company or organization. +% +% "Distributor Fee" means any fee that you charge for Distributing this +% Package or providing support for this Package to another party. It +% does not mean licensing fees. +% +% "Standard Version" refers to the Package if it has not been modified, +% or has been modified only in ways explicitly requested by the +% Copyright Holder. +% +% "Modified Version" means the Package, if it has been changed, and such +% changes were not explicitly requested by the Copyright Holder. +% +% "Original License" means this Artistic License as Distributed with the +% Standard Version of the Package, in its current version or as it may +% be modified by The Perl Foundation in the future. +% +% "Source" form means the source code, documentation source, and +% configuration files for the Package. +% +% "Compiled" form means the compiled bytecode, object code, binary, or +% any other form resulting from mechanical transformation or translation +% of the Source form. +% +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% +% Permission for Use and Modification Without Distribution +% +% (1) You are permitted to use the Standard Version and create and use +% Modified Versions for any purpose without restriction, provided that +% you do not Distribute the Modified Version. +% +% Permissions for Redistribution of the Standard Version +% +% (2) You may Distribute verbatim copies of the Source form of the +% Standard Version of this Package in any medium without restriction, +% either gratis or for a Distributor Fee, provided that you duplicate +% all of the original copyright notices and associated disclaimers. At +% your discretion, such verbatim copies may or may not include a +% Compiled form of the Package. +% +% (3) You may apply any bug fixes, portability changes, and other +% modifications made available from the Copyright Holder. The resulting +% Package will still be considered the Standard Version, and as such +% will be subject to the Original License. +% +% Distribution of Modified Versions of the Package as Source +% +% (4) You may Distribute your Modified Version as Source (either gratis +% or for a Distributor Fee, and with or without a Compiled form of the +% Modified Version) provided that you clearly document how it differs +% from the Standard Version, including, but not limited to, documenting +% any non-standard features, executables, or modules, and provided that +% you do at least ONE of the following: +% +% (a) make the Modified Version available to the Copyright Holder of the +% Standard Version, under the Original License, so that the Copyright +% Holder may include your modifications in the Standard Version. (b) +% ensure that installation of your Modified Version does not prevent the +% user installing or running the Standard Version. In addition, the +% modified Version must bear a name that is different from the name of +% the Standard Version. (c) allow anyone who receives a copy of the +% Modified Version to make the Source form of the Modified Version +% available to others under (i) the Original License or (ii) a license +% that permits the licensee to freely copy, modify and redistribute the +% Modified Version using the same licensing terms that apply to the copy +% that the licensee received, and requires that the Source form of the +% Modified Version, and of any works derived from it, be made freely +% available in that license fees are prohibited but Distributor Fees are +% allowed. +% +% Distribution of Compiled Forms of the Standard Version or +% Modified Versions without the Source +% +% (5) You may Distribute Compiled forms of the Standard Version without +% the Source, provided that you include complete instructions on how to +% get the Source of the Standard Version. Such instructions must be +% valid at the time of your distribution. If these instructions, at any +% time while you are carrying out such distribution, become invalid, you +% must provide new instructions on demand or cease further +% distribution. If you provide valid instructions or cease distribution +% within thirty days after you become aware that the instructions are +% invalid, then you do not forfeit any of your rights under this +% license. +% +% (6) You may Distribute a Modified Version in Compiled form without the +% Source, provided that you comply with Section 4 with respect to the +% Source of the Modified Version. +% +% Aggregating or Linking the Package +% +% (7) You may aggregate the Package (either the Standard Version or +% Modified Version) with other packages and Distribute the resulting +% aggregation provided that you do not charge a licensing fee for the +% Package. Distributor Fees are permitted, and licensing fees for other +% components in the aggregation are permitted. The terms of this license +% apply to the use and Distribution of the Standard or Modified Versions +% as included in the aggregation. +% +% (8) You are permitted to link Modified and Standard Versions with +% other works, to embed the Package in a larger work of your own, or to +% build stand-alone binary or bytecode versions of applications that +% include the Package, and Distribute the result without restriction, +% provided the result does not expose a direct interface to the Package. +% +% Items That are Not Considered Part of a Modified Version +% +% (9) Works (including, but not limited to, modules and scripts) that +% merely extend or make use of the Package, do not, by themselves, cause +% the Package to be a Modified Version. In addition, such works are not +% considered parts of the Package itself, and are not subject to the +% terms of this license. +% +% General Provisions +% +% (10) Any use, modification, and distribution of the Standard or +% Modified Versions is governed by this Artistic License. By using, +% modifying or distributing the Package, you accept this license. Do not +% use, modify, or distribute the Package, if you do not accept this +% license. +% +% (11) If your Modified Version has been derived from a Modified Version +% made by someone other than you, you are nevertheless required to +% ensure that your Modified Version complies with the requirements of +% this license. +% +% (12) This license does not grant you the right to use any trademark, +% service mark, tradename, or logo of the Copyright Holder. +% +% (13) This license includes the non-exclusive, worldwide, +% free-of-charge patent license to make, have made, use, offer to sell, +% sell, import and otherwise transfer the Package with respect to any +% patent claims licensable by the Copyright Holder that are necessarily +% infringed by the Package. If you institute patent litigation +% (including a cross-claim or counterclaim) against any party alleging +% that the Package constitutes direct or contributory patent +% infringement, then this Artistic License to you shall terminate on the +% date that such litigation is filed. +% +% (14) Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT +% HOLDER AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED +% WARRANTIES. THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A +% PARTICULAR PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT +% PERMITTED BY YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT +% HOLDER OR CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, +% INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE +% OF THE PACKAGE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +% +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% ProbLog inference +% +% assumes probabilistic facts as Prob::Fact and clauses in normal Prolog format +% +% provides following inference modes (16/12/2008): +% - approximation with interval width Delta (IJCAI07): problog_delta(+Query,+Delta,-Low,-High,-Status) +% - bounds based on single probability threshold: problog_threshold(+Query,+Threshold,-Low,-High,-Status) +% - as above, but lower bound only: problog_low(+Query,+Threshold,-Low,-Status) +% - lower bound based on K most likely proofs: problog_kbest(+Query,+K,-Low,-Status) +% - explanation probability (ECML07): problog_max(+Query,-Prob,-FactsUsed) +% - exact probability: problog_exact(+Query,-Prob,-Status) +% - sampling: problog_montecarlo(+Query,+Delta,-Prob) +% +% +% angelika.kimmig@cs.kuleuven.be +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +:- module(problog, [problog_delta/5, + problog_threshold/5, + problog_low/4, + problog_kbest/4, + problog_kbest_save/6, + problog_max/3, + problog_exact/3, + problog_exact_save/5, + problog_montecarlo/3, + problog_dnf_sampling/3, + problog_answers/2, + problog_kbest_answers/3, + problog_table/1, + clear_retained_tables/0, + problog_neg/1, + get_fact_probability/2, + set_fact_probability/2, + get_continuous_fact_parameters/2, + set_continuous_fact_parameters/2, + get_fact/2, + tunable_fact/2, + tunable_continuous_fact/2, + continuous_fact/1, + non_ground_fact/1, + export_facts/1, + problog_help/0, + show_inference/0, + problog_dir/1, + set_problog_flag/2, + problog_flag/2, + problog_flags/0, + problog_flags/1, + reset_problog_flags/0, + problog_assert/1, + problog_assert/2, + problog_retractall/1, + problog_statistics/2, + problog_statistics/0, + grow_atom_table/1, + problog_exact_nested/3, + problog_tabling_negated_synonym/2, + problog_control/2, + build_trie/2, + build_trie/3, + problog_infer/2, + problog_infer/3, + problog_infer_forest/2, + write_bdd_struct_script/3, + problog_bdd_forest/1, + require/1, + unrequire/1, + bdd_files/2, + delete_bdd_forest_files/1, + recover_grounding_id/2, + grounding_is_known/2, + grounding_id/3, + decision_fact/2, + reset_non_ground_facts/0, + '::'/2, + probabilistic_fact/3, + init_problog/1, + problog_call/1, + problog_infer_forest_supported/0, + problog_bdd_forest_supported/0, + problog_real_kbest/4, + op( 550, yfx, :: ), + op( 550, fx, ?:: ), + op(1149, yfx, <-- ), + op( 1150, fx, problog_table ), + in_interval/3, + below/2, + above/2]). + +:- style_check(all). +:- yap_flag(unknown,error). + +:- set_prolog_flag(to_chars_mode,quintus). + +% general yap modules +:- use_module(library(charsio)). +:- use_module(library(lists)). +:- use_module(library(terms)). +:- use_module(library(random)). % PM doesn't seem to be used! +:- use_module(library(system)). +:- use_module(library(rbtrees)). % PM doesn't seem to be used! +:- use_module(library(ordsets), [list_to_ord_set/2, ord_insert/3, ord_union/3]). + +% problog related modules +:- use_module('problog/variables'). +:- use_module('problog/extlists'). +:- use_module('problog/gflags', [flag_store/2]). +:- use_module('problog/flags'). +:- use_module('problog/print'). +:- use_module('problog/os'). +:- use_module('problog/tptree'). +:- use_module('problog/tabling'). +:- use_module('problog/sampling'). +:- use_module('problog/intervals'). +:- use_module('problog/mc_DNF_sampling'). +:- catch(use_module('problog/ad_converter'),_,true). +:- catch(use_module('problog/variable_elimination'),_,true). + +% op attaching probabilities to facts +:- op( 550, yfx, :: ). +:- op( 550, fx, ?:: ). + +% for annotated disjunctions +% :- op(1149, yfx, <-- ). + +%%%%%%%%%%%%%%%%%%%%%%%% +% control predicates on various levels +%%%%%%%%%%%%%%%%%%%%%%%% + +% global over all inference methods, internal use only +:- dynamic(problog_predicate/2). +:- dynamic(problog_continuous_predicate/3). +% global over all inference methods, exported +:- dynamic(tunable_fact/2). +:- dynamic(non_ground_fact/1). +:- dynamic(continuous_fact/1). +%:- dynamic(problog_dir/1). +% global, manipulated via problog_control/2 +:- dynamic(up/0). +:- dynamic(limit/0). +:- dynamic(mc/0). +:- dynamic(remember/0). +:- dynamic(exact/0). % Theo tabling +:- dynamic(find_decisions/0). +:- dynamic(internal_strategy/0). +% local to problog_delta +:- dynamic(low/2). +:- dynamic(up/2). +:- dynamic(stopDiff/1). +% local to problog_kbest +:- dynamic(current_kbest/3). +% local to problog_max +:- dynamic(max_probability/1). +:- dynamic(max_proof/1). +% local to problog_montecarlo +:- dynamic(mc_prob/1). +% local to problog_answers +:- dynamic(answer/1). +% to keep track of the groundings for non-ground facts +:- dynamic(grounding_is_known/2). + +% for decisions +:- dynamic(decision_fact/2). + +% for fact where the proabability is a variable +:- dynamic(dynamic_probability_fact/1). +:- dynamic(dynamic_probability_fact_extract/2). + +% for storing continuous parts of proofs (Hybrid ProbLog) +:- dynamic([hybrid_proof/3, hybrid_proof/4]). +:- dynamic(hybrid_proof_disjoint/4). + +% ProbLog files declare prob. facts as P::G +% and this module provides the predicate X::Y to iterate over them +:- multifile('::'/2). + +% directory where problogbdd executable is located +% automatically set during loading -- assumes it is in same place as this file (problog.yap) +:- getcwd(PD), set_problog_path(PD). + +%%%%%%%%%%%% +% iterative deepening on minimal probabilities (delta, max, kbest): +% - first threshold (not in log-space as only used to retrieve argument for init_threshold/1, which is also used with user-supplied argument) +% - last threshold to ensure termination in case infinite search space (saved also in log-space for easy comparison with current values during search) +% - factor used to decrease threshold for next level, NewMin=Factor*OldMin (saved also in log-space) +%%%%%%%%%%%% + +:- 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(id_stepsize, problog_flag_validate_indomain_0_1_close, 'threshold shrinking factor iterative deepening', 0.5, inference, flags:id_stepsize_handler) +)). + +%%%%%%%%%%%% +% prune check stops derivations if they use a superset of facts already known to form a proof +% (very) costly test, can be switched on/off here (This is obsolete as it is not included in implementation) +%%%%%%%%%%%% + +:- initialization( + problog_define_flag(prunecheck, problog_flag_validate_switch, 'stop derivations including all facts of known proof', off, inference) +). + +%%%%%%%%%%%% +% max number of calls to probabilistic facts per derivation (to ensure termination) +%%%%%%%%%%%% + +:- initialization( + problog_define_flag(maxsteps, problog_flag_validate_posint, 'max. number of prob. steps per derivation', 1000, inference) +). + +%%%%%%%%%%%% +% BDD timeout in seconds, used as option in BDD tool +% files to write BDD script and pars +% bdd_file overwrites bdd_par_file with matching extended name +% if different name wanted, respect order when setting +% save BDD information for the (last) lower bound BDD used during inference +% produces three files named save_script, save_params, save_map +% located in the directory given by problog_flag dir +%%%%%%%%%%%% + +:- initialization(( +% problog_define_flag(bdd_path, problog_flag_validate_directory, 'problogbdd directory', '.',bdd), + problog_define_flag(bdd_time, problog_flag_validate_posint, 'BDD computation timeout in seconds', 60, bdd), + problog_define_flag(save_bdd, problog_flag_validate_boolean, 'save BDD files for (last) lower bound', false, bdd), + problog_define_flag(dynamic_reorder, problog_flag_validate_boolean, 'use dynamic re-ordering for BDD', true, bdd), + problog_define_flag(bdd_static_order, problog_flag_validate_boolean, 'use a static order', false, bdd) +)). + +%%%%%%%%%%%% +% determine whether ProbLog outputs information (number of proofs, intermediate results, ...) +% default was true, as otherwise problog_delta won't output intermediate bounds +% default is false now, as dtproblog will flood the user with verbosity +%%%%%%%%%%%% + +:- initialization( + problog_define_flag(verbose, problog_flag_validate_boolean, 'output intermediate information', false,output) +). + +%%%%%%%%%%%% +% determine whether ProbLog outputs proofs when adding to trie +% default is false +%%%%%%%%%%%% + +:- initialization( + problog_define_flag(show_proofs, problog_flag_validate_boolean, 'output proofs', false,output) +). + +%%%%%%%%%%%% +% Trie dump parameter for saving a file with the trie structure in the directory by problog_flag dir +%%%%%%%%%%%% + +:- initialization( + problog_define_flag(triedump, problog_flag_validate_boolean, 'generate file: trie_file containing the trie structure', false,output) +). + +%%%%%%%%%%%% +% Default inference method +%%%%%%%%%%%% + +:- initialization(problog_define_flag(inference, problog_flag_validate_dummy, 'default inference method', exact, inference)). + +%%%%%%%%%%%% +% Tunable Facts +%%%%%%%%%%%% + +:- initialization(problog_define_flag(tunable_fact_start_value,problog_flag_validate_dummy,'How to initialize tunable probabilities',uniform(0.1,0.9),learning_general,flags:learning_prob_init_handler)). + + + +problog_dir(PD):- problog_path(PD). + +%%%%%%%%%%%%%%%%%%%%%%%% +% initialization of global parameters +%%%%%%%%%%%%%%%%%%%%%%%% + +init_global_params :- + %grow_atom_table(1000000), + + %%%%%%%%%%%% + % working directory: all the temporary and output files will be located there + % it assumes a subdirectory of the current working dir + % on initialization, the current dir is the one where the user's file is located + % should be changed to use temporary folder structure of operating system + %%%%%%%%%%%% + tmpnam(TempFolder), + atomic_concat([TempFolder, '_problog'], TempProblogFolder), + problog_define_flag(dir, problog_flag_validate_directory, 'directory for files', TempProblogFolder, output), + problog_define_flag(bdd_par_file, problog_flag_validate_file, 'file for BDD variable parameters', example_bdd_probs, bdd, flags:working_file_handler), + problog_define_flag(bdd_result, problog_flag_validate_file, 'file to store result calculated from BDD', example_bdd_res, bdd, flags:working_file_handler), + problog_define_flag(bdd_file, problog_flag_validate_file, 'file for BDD script', example_bdd, bdd, flags:bdd_file_handler), + problog_define_flag(static_order_file, problog_flag_validate_file, 'file for BDD static order', example_bdd_order, bdd, flags:working_file_handler), +%%%%%%%%%%%% +% montecarlo: recalculate current approximation after N samples +% montecarlo: write log to this file +%%%%%%%%%%%% + problog_define_flag(mc_logfile, problog_flag_validate_file, 'logfile for montecarlo', 'log.txt', mcmc, flags:working_file_handler), + check_existance('problogbdd'). + +% parameter initialization to be called after returning to user's directory: +:- initialization(init_global_params). + + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% internal control flags +% if on +% - up: collect stopped derivations to build upper bound +% - limit: iterative deepening reached limit -> should go to next level +% - mc: using problog_montecarlo, i.e. proving with current sample instead of full program +% - remember: save BDD files containing script, params and mapping +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +problog_control(on,X) :- + call(X),!. +problog_control(on,X) :- + assertz(X). +problog_control(off,X) :- + retractall(X). +problog_control(check,X) :- + call(X). + +reset_control :- + problog_control(off,up), + problog_control(off,mc), + problog_control(off,limit), +% problog_control(off,exact), + problog_control(off,remember). + +:- initialization(reset_control). + +grow_atom_table(N):- + generate_atoms(N, 0), + garbage_collect_atoms. +generate_atoms(N, N):-!. +generate_atoms(N, A):- + NA is A + 1, + atomic_concat([theo, A], _Atom), + generate_atoms(N, NA). + + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% nice user syntax Prob::Fact +% automatic translation to internal hardware access format +% +% probabilities =1 are dropped -> normal Prolog fact +% +% internal fact representation +% - prefixes predicate name with problog_ +% - adds unique ID as first argument +% - adds logarithm of probability as last argument +% - keeps original arguments in between +% +% for each predicate appearing as probabilistic fact, wrapper clause is introduced: +% - head is most general instance of original fact +% - body is corresponding version of internal fact plus call to add_to_proof/2 to update current state during proving +% example: edge(A,B) :- problog_edge(ID,A,B,LogProb), add_to_proof(ID,LogProb). +% +% dynamic predicate problog_predicate(Name,Arity) keeps track of predicates that already have wrapper clause +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + +term_expansion_intern(A, B, C):- + catch(term_expansion_intern_ad(A, B, C), _, false). + +% converts ?:: prefix to ? :: infix, as handled by other clause +term_expansion_intern((Annotation::Fact), Module, ExpandedClause) :- + Annotation == '?', + term_expansion_intern((? :: Fact :- true), Module, ExpandedClause). + + +% handles decision clauses +term_expansion_intern((Annotation :: Head :- Body), Module, problog:ExpandedClause) :- + ( + Annotation == '?' -> + % It's a decision with a body + copy_term((Head,Body),(HeadCopy,_BodyCopy)), + functor(Head, Functor, Arity), + atomic_concat([problog_,Functor],LongFunctor), + Head =.. [Functor|Args], + append(Args,[LProb],LongArgs), + probclause_id(ID), + ProbFactHead =.. [LongFunctor,ID|LongArgs], + assertz(decision_fact(ID,Head)), + ExpandedClause = (ProbFactHead :- + user:Body, + (problog_control(check,internal_strategy) -> + dtproblog:strategy_log(ID,Head,LProb) + ; + LProb = '?' + ) + ), + assertz(dynamic_probability_fact(ID)), + assertz((dynamic_probability_fact_extract(HeadCopy,P_New) :- + dtproblog:strategy(ID,HeadCopy,P_New) + )), + (ground(Head) -> + true + ; + assertz(non_ground_fact(ID)) + ), + problog_predicate(Functor, Arity, LongFunctor, Module) + ; + % If it has a body, it's not supported + (Body == true -> + % format('Expanding annotated fact ~q :: ~q :- ~q in other clause.~n',[Annotation,Head,Body]), + fail + ; + throw(error('We do not support annotated clauses (yet)!', (Annotation :: Head :- Body))) + ) + ). + + +/* this can slow down prolog time by several orders if there's lots of them +user:term_expansion(P::Goal,Goal) :- + P \= t(_), + P =:= 1, + !. +*/ + +% handles probabilistic facts +term_expansion_intern(P :: Goal,Module,problog:ProbFact) :- + copy_term((P,Goal),(P_Copy,Goal_Copy)), + functor(Goal, Name, Arity), + atomic_concat([problog_,Name],ProblogName), + Goal =.. [Name|Args], + append(Args,[LProb],L1), + probclause_id(ID), + ProbFact =.. [ProblogName,ID|L1], + ( + (nonvar(P), P = t(TrueProb)) + -> + ( + assertz(tunable_fact(ID,TrueProb)), + sample_initial_value_for_tunable_fact(LProb) + ); + ( + ground(P) + -> + EvalP is P, % allows one to use ground arithmetic expressions as probabilities + LProb is log(P), + assert_static(prob_for_id(ID,EvalP,LProb)); % Prob is fixed -- assert it for quick retrieval + ( + % Probability is a variable... check wether it appears in the term + ( + variable_in_term(Goal,P) + -> + true; + ( + format(user_error,'If you use probabilisitic facts with a variable as probabilility, the variable has to appear inside the fact.~n',[]), + format(user_error,'You used ~q in your program.~2n',[P::Goal]), + throw(non_ground_fact_error(P::Goal)) + ) + ), + LProb=log(P), + assertz(dynamic_probability_fact(ID)), + assertz(dynamic_probability_fact_extract(Goal_Copy,P_Copy)) + ) + ) + ), + ( + ground(Goal) + -> + true; + assertz(non_ground_fact(ID)) + ), + problog_predicate(Name, Arity, ProblogName,Module). + + +sample_initial_value_for_tunable_fact(LogP) :- + problog_flag(tunable_fact_start_value,Initializer), + + ( + Initializer=uniform(Low,High) + -> + ( + Spread is High-Low, + random(Rand), + P1 is Rand*Spread+Low, + + % security check, to avoid log(0) + ( + P1>0 + -> + P=P1; + P=0.5 + ) + ); + ( + number(Initializer) + -> + P=Initializer; + throw(unkown_probability_initializer(Initializer)) + ) + ), + + LogP is log(P). + + + +% Hybrid ProbLog stuff + +is_valid_gaussian(X) :- + compound(X), + X=gaussian(Mu,Sigma), + ( + ((number(Mu),number(Sigma));(Mu=t(_),Sigma=t(_))) + -> + true; + throw(invalid_gaussian(X)) + ). + +:- multifile(user:term_expansion/1). + +user:term_expansion(Goal, problog:ProbFact) :- + compound(Goal), + Goal=..[Name|Args], + once( (nth(Pos,Args,GaussianArg),is_valid_gaussian(GaussianArg)) ), + + %Goal contains a Gaussian, there is some work to do + + ( % check for a second Gaussian + (nth(Pos2,Args,GaussianArg2),Pos2\=Pos,is_valid_gaussian(GaussianArg2)) + -> + ( + format(user_error,'We only support continous atoms with at most one Gaussian inside.~n',[]), + format(user_error,'Your program contains the atom ~w with more than one.~n',[]), + throw(unsupported_multivariate_gaussian(Goal)) + ); + true + ), + + functor(Goal, Name, Arity), + atomic_concat([problogcontinuous_,Name],ProblogName), + probclause_id(ID), + + GaussianArg=gaussian(Mu_Arg,Sigma_Arg), + + % is it a tunable fact? + ( + (number(Mu_Arg),number(Sigma_Arg)) + -> + NewArgs=Args; + ( + Mu_Random is 0.1, % random*4-2, + Sigma_Random is 0.4, % random*2+0.5, + nth(Pos,Args,_,KeepArgs), + nth(Pos,NewArgs,gaussian(Mu_Random,Sigma_Random),KeepArgs), + assertz(tunable_fact(ID,gaussian(Mu_Arg,Sigma_Arg))) + ) + ), + ProbFact =.. [ProblogName,ID|NewArgs], + + ( + ground(Goal) + -> + true; + assertz(non_ground_fact(ID)) + ), + assertz(continuous_fact(ID)), + problog_continuous_predicate(Name, Arity, Pos,ProblogName). + + +% introduce wrapper clause if predicate seen first time +problog_continuous_predicate(Name, Arity,ContinuousArgumentPosition,_) :- + problog_continuous_predicate(Name, Arity,OldContinuousArgumentPosition), + !, + ( + ContinuousArgumentPosition=OldContinuousArgumentPosition + -> + true; + ( + format(user_error,'Continuous predicates of the same name and arity must ',[]), + format(user_error,'have the continuous argument all at the same position.~n',[]), + format(user_error,'Your program contains the predicate ~q/~q. There are ',[]), + format(user_error,'atoms which have the continuous argument at position ',[]), + format(user_error,'~q and other have it at ~q.',[Name,Arity,OldContinuousArgumentPosition,ContinuousArgumentPosition]), + throw(continuous_argument(not_unique_position)) + ) + ). +problog_continuous_predicate(Name, Arity, ContinuousArgumentPosition, ProblogName) :- + + LBefore is ContinuousArgumentPosition-1, + LAfter is Arity-ContinuousArgumentPosition, + + length(ArgsBefore,LBefore), + length(ArgsAfter,LAfter), + append(ArgsBefore,[(ID,ID2,GaussianArg)|ArgsAfter],Args), + append(ArgsBefore,[GaussianArg|ArgsAfter],ProbArgs), + + OriginalGoal =.. [Name|Args], + + + ProbFact =.. [ProblogName,ID|ProbArgs], + prolog_load_context(module,Mod), + + assertz( (Mod:OriginalGoal :- ProbFact, + % continuous facts always get a grounding ID, even when they are actually ground + % this simplifies the BDD script generation + non_ground_fact_grounding_id(ProbFact,Ground_ID), + atomic_concat([ID,'_',Ground_ID],ID2), + add_continuous_to_proof(ID,ID2) + )), + + assertz(problog_continuous_predicate(Name, Arity,ContinuousArgumentPosition)), + ArityPlus1 is Arity+1, + dynamic(problog:ProblogName/ArityPlus1). + + +in_interval(ID,Low,High) :- + number(Low), + number(High), + Low + non_ground_fact_grounding_id(Goal,G_ID), + atomic_concat([ID,'_',G_ID],ID2) + ; + ID2=ID + ). + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% What to do when prolog tries to prove a problog fact +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + +prove_problog_fact(ClauseID,GroundID,Prob) :- + (problog_control(check,find_decisions) -> + signal_decision(ClauseID,GroundID) + ; + (Prob = '?' -> + add_to_proof(GroundID,0) % 0 is log(1)! + ; + % Checks needed for LeDTProbLog + (Prob = always -> + % Always true, do not add to trie + true + ; + (Prob = never -> + % Always false, do not add to trie + fail + ; + % something in between, add to proof + ProbEval is Prob, + add_to_proof(GroundID,ProbEval) + ) + ) + ) + ). + +prove_problog_fact_negated(ClauseID,GroundID,Prob) :- + (problog_control(check,find_decisions) -> + signal_decision(ClauseID,GroundID) + ; + (Prob = '?' -> + add_to_proof_negated(GroundID,-inf) % 0 is log(1)! + ; + % Checks needed for LeDTProbLog + (Prob = always -> + % Always true, do not add to trie + fail + ; + (Prob = never -> + % Always false, do not add to trie + true + ; + % something in between, add to proof + ProbEval is Prob, + add_to_proof_negated(GroundID,ProbEval) + ) + ) + ) + ). + +% generate next global identifier +:- initialization(nb_setval(probclause_counter,0)). + +probclause_id(ID) :- + nb_getval(probclause_counter,ID), !, + C1 is ID+1, + nb_setval(probclause_counter,C1), !. + +non_ground_fact_grounding_id(Goal,ID) :- + ( + ground(Goal) + -> + true; + ( + format(user_error,'The current program uses non-ground facts.~n', []), + format(user_error,'If you query those, you may only query fully-grounded versions of the fact.~n',[]), + format(user_error,'Within the current proof, you queried for ~q which is not ground.~n~n', [Goal]), + throw(error(non_ground_fact(Goal))) + ) + ), + ( + grounding_is_known(Goal,ID) + -> + true; + ( + nb_getval(non_ground_fact_grounding_id_counter,ID), + ID2 is ID+1, + nb_setval(non_ground_fact_grounding_id_counter,ID2), + assertz(grounding_is_known(Goal,ID)) + ) + ). + +reset_non_ground_facts :- + (required(keep_ground_ids) -> + true + ; + nb_setval(non_ground_fact_grounding_id_counter,0), + retractall(grounding_is_known(_,_)) + ). + +:- initialization(reset_non_ground_facts). + +% backtrack over all probabilistic facts +% must come before term_expansion +P::Goal :- + probabilistic_fact(P,Goal,_). + +% backtrack over all probabilistic facts +probabilistic_fact(P2,Goal,ID) :- + ( + ground(Goal) + -> + ( + Goal =.. [F|Args], + atomic_concat('problog_',F,F2), + append([ID|Args],[P],Args2), + Goal2 =..[F2|Args2], + length(Args2,N), + current_predicate(F2/N), + call(Goal2), + number(P), + P2 is exp(P) + ); + ( + get_internal_fact(ID,ProblogTerm,_ProblogName,_ProblogArity), + ProblogTerm =.. [F,_ID|Args], + append(Args2,[P],Args), + name(F,[_p,_r,_o,_b,_l,_o,_g,_|F2Chars]), + name(F2,F2Chars), + Goal =.. [F2|Args2], + ( + dynamic_probability_fact(ID) + -> + P2=p; + P2 is exp(P) + ) + ) + ). + + +% generates unique IDs for proofs +proof_id(ID) :- + nb_getval(problog_proof_id,ID), + ID2 is ID+1, + nb_setval(problog_proof_id,ID2). + +reset_problog_proof_id :- + nb_setval(problog_proof_id,0). + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% access/update the probability of ID's fact +% hardware-access version: naively scan all problog-predicates (except if prob is recorded in static database), +% cut choice points if ID is ground (they'll all fail as ID is unique), +% but not if it isn't (used to iterate over all facts when writing out probabilities for learning) +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% using a dummy for the static prob database is more efficient than checking for current_predicate +prob_for_id(dummy,dummy,dummy). + +get_fact_probability(A, Prob) :- + ground(A), + \+ number(A), + name(A, A_Codes), + once(append(Part1, [95|Part2], A_Codes)), % 95 = '_' + number_codes(ID, Part1), !, + % let's check whether Part2 contains an 'l' (l=low) + (member(108, Part2) -> + fail + ; + number_codes(Grounding_ID, Part2), + (dynamic_probability_fact(ID) -> + grounding_is_known(Goal, Grounding_ID), + dynamic_probability_fact_extract(Goal, Prob) + ; + get_fact_probability(ID, Prob) + ) + ). +get_fact_probability(ID,Prob) :- + ground(ID), + prob_for_id(ID,Prob,_), + !. +get_fact_probability(ID,Prob) :- + ( + ground(ID) -> + get_internal_fact(ID,ProblogTerm,_ProblogName,ProblogArity),! + ; + get_internal_fact(ID,ProblogTerm,_ProblogName,ProblogArity) + ), + arg(ProblogArity,ProblogTerm,Log), + (Log = '?' -> + throw(error('Why do you want to know the probability of a decision?')) %fail + ; + Prob is exp(Log) + ). + +get_fact_log_probability(ID,Prob) :- + ground(ID), + prob_for_id(ID,_,Prob),!. +get_fact_log_probability(ID,Prob) :- + ( + ground(ID) -> + get_internal_fact(ID,ProblogTerm,_ProblogName,ProblogArity),! + ; + get_internal_fact(ID,ProblogTerm,_ProblogName,ProblogArity) + ), + arg(ProblogArity,ProblogTerm,Prob), + Prob \== '?'. +get_fact_log_probability(ID,Prob) :- + get_fact_probability(ID,Prob1), + Prob is log(Prob1). + +set_fact_probability(ID,Prob) :- + get_internal_fact(ID,ProblogTerm,ProblogName,ProblogArity), + retract(ProblogTerm), + ProblogTerm =.. [ProblogName|ProblogTermArgs], + nth(ProblogArity,ProblogTermArgs,_,KeepArgs), + NewLogProb is log(Prob), + nth(ProblogArity,NewProblogTermArgs,NewLogProb,KeepArgs), + NewProblogTerm =.. [ProblogName|NewProblogTermArgs], + assertz(NewProblogTerm). + +get_internal_fact(ID,ProblogTerm,ProblogName,ProblogArity) :- + problog_predicate(Name,Arity), + atomic_concat([problog_,Name],ProblogName), + ProblogArity is Arity+2, + functor(ProblogTerm,ProblogName,ProblogArity), + arg(1,ProblogTerm,ID), + call(ProblogTerm). + +get_continuous_fact_parameters(ID,Parameters) :- + ( + ground(ID) -> + get_internal_continuous_fact(ID,ProblogTerm,_ProblogName,ProblogArity,ContinuousPos),! + ; + get_internal_continuous_fact(ID,ProblogTerm,_ProblogName,ProblogArity,ContinuousPos) + ), + InternalPos is ContinuousPos+1, + arg(InternalPos,ProblogTerm,Parameters). + +get_internal_continuous_fact(ID,ProblogTerm,ProblogName,ProblogArity,ContinuousPos) :- + problog_continuous_predicate(Name,Arity,ContinuousPos), + atomic_concat([problogcontinuous_,Name],ProblogName), + ProblogArity is Arity+1, + functor(ProblogTerm,ProblogName,ProblogArity), + arg(1,ProblogTerm,ID), + call(ProblogTerm). + +set_continuous_fact_parameters(ID,Parameters) :- + get_internal_continuous_fact(ID,ProblogTerm,ProblogName,_ProblogArity,ContinuousPos), + retract(ProblogTerm), + ProblogTerm =.. [ProblogName|ProblogTermArgs], + nth0(ContinuousPos,ProblogTermArgs,_,KeepArgs), + nth0(ContinuousPos,NewProblogTermArgs,Parameters,KeepArgs), + NewProblogTerm =.. [ProblogName|NewProblogTermArgs], + assertz(NewProblogTerm). + + + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% writing those facts with learnable parameters to File +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +export_facts(Filename) :- + open(Filename,'write',Handle), + + %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + ( % go over all probabilistic facts + P::Goal, + format(Handle,'~w :: ~q.~n',[P,Goal]), + + fail; % go to next prob. fact + true + ), + %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + + %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + ( % go over all continuous facts + continuous_fact(ID), + get_continuous_fact_parameters(ID,Param), + format(Handle,'~q. % ~q~n',[Param,ID]), + + fail; % go to next cont. fact + true + ), + %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + + close(Handle). + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% recover fact for given id +% list version not exported (yet?) +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% ID of ground fact +get_fact(ID,OutsideTerm) :- + get_internal_fact(ID,ProblogTerm,ProblogName,ProblogArity), + !, + ProblogTerm =.. [_Functor,ID|Args], + atomic_concat('problog_',OutsideFunctor,ProblogName), + Last is ProblogArity-1, + nth(Last,Args,_LogProb,OutsideArgs), + OutsideTerm =.. [OutsideFunctor|OutsideArgs]. +% ID of instance of non-ground fact: get fact from grounding table +get_fact(ID,OutsideTerm) :- + recover_grounding_id(ID,GID), + grounding_is_known(OutsideTerm,GID). + +recover_grounding_id(Atom,ID) :- + name(Atom,List), + reverse(List,Rev), + recover_number(Rev,NumRev), + reverse(NumRev,Num), + name(ID,Num). +recover_number([95|_],[]) :- !. % name('_',[95]) +recover_number([A|B],[A|C]) :- + recover_number(B,C). + + +get_fact_list([],[]). +get_fact_list([ID|IDs],[Fact|Facts]) :- + (ID=not(X) -> Fact=not(Y); Fact=Y, ID=X), + get_fact(X,Y), + get_fact_list(IDs,Facts). + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% ProbLog inference, core methods +% +% state of proving saved in two backtrackable global variables +% - problog_current_proof holds list of IDs of clauses used +% - problog_probability holds the sum of their log probabilities +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + +% called "inside" probabilistic facts to update current state of proving +% if number of steps exceeded, fail +% if fact used before, succeed and keep status as is +% if not prunable, calculate probability and +% if threshold exceeded, add stopped derivation to upper bound and fail +% else update state and succeed +% +% do not maintain gloabl variables in montecarlo mode +add_to_proof(ID, Prob) :- + (problog_control(check, mc) -> + montecarlo_check(ID) + ; + b_getval(problog_steps,MaxSteps), + b_getval(problog_probability, CurrentP), + nb_getval(problog_threshold, CurrentThreshold), + b_getval(problog_current_proof, IDs), + %%%% Bernd, changes for negated ground facts + \+ open_end_memberchk(not(ID),IDs), + %%%% Bernd, changes for negated ground facts + (MaxSteps =< 0 -> + fail + ; + (open_end_memberchk(ID, IDs) -> %Theo + true + ; + open_end_add(ID, IDs, NIDs), %Theo + % \+ prune_check(NIDs, Trie_Completed_Proofs), + multiply_probabilities(CurrentP, Prob, NProb), + (NProb < CurrentThreshold -> + upper_bound(NIDs), + fail + ; + b_setval(problog_probability, NProb), + b_setval(problog_current_proof, NIDs) + ) + ), + Steps is MaxSteps - 1, + b_setval(problog_steps, Steps) + ) + ). + +%%%% Bernd, changes for negated ground facts +add_to_proof_negated(ID, Prob) :- + (problog_control(check, mc) -> + % the sample has to fail if the fact is negated + \+ montecarlo_check(ID) + ; + b_getval(problog_steps, MaxSteps), + b_getval(problog_probability, CurrentP), + nb_getval(problog_threshold, CurrentThreshold), + b_getval(problog_current_proof, IDs), + \+ open_end_memberchk(ID, IDs), + (MaxSteps =< 0 -> + fail + ; + (open_end_memberchk(not(ID), IDs) -> + true + ; + open_end_add(not(ID), IDs, NIDs), %Theo + % \+ prune_check(NIDs, Trie_Completed_Proofs), + InverseProb is log(1 - exp(Prob)), + multiply_probabilities(CurrentP, InverseProb, NProb), + (NProb < CurrentThreshold -> + upper_bound(NIDs), %% checkme + fail + ; + b_setval(problog_probability, NProb), + b_setval(problog_current_proof, NIDs) + ) + ), + Steps is MaxSteps - 1, + b_setval(problog_steps, Steps) + ) + ). +%%%% Bernd, changes for negated ground facts + +%Hybrid +add_continuous_to_proof(ID,GroundID) :- + b_getval(problog_continuous_facts_used,Facts), + ( + memberchk((ID,GroundID),Facts) + -> + true; + ( + b_setval(problog_continuous_facts_used,[(ID,GroundID)|Facts]), + atomic_concat([interval,'_',GroundID],Key), + b_setval(Key,all) + ) + ). + +% if in monte carlo mode ... +% (a) for ground facts (ID is number): check array to see if it can be used +montecarlo_check(ID) :- + number(ID), + !, + array_element(mc_sample,ID,V), + ( + V == 1 -> true + ; + V == 2 -> fail + ; + new_sample(ID) + ). +% (b) for non-ground facts (ID is FactID_GroundingID): check database of groundings in current sample +montecarlo_check(ComposedID) :- +% split_grounding_id(ComposedID,ID,GID), + recorded(mc_true,problog_mc_id(ComposedID),_), + !. +montecarlo_check(ComposedID) :- +% split_grounding_id(ComposedID,ID,GID), + recorded(mc_false,problog_mc_id(ComposedID),_), + !, + fail. +% (c) for unknown groundings of non-ground facts: generate a new sample (decompose the ID first) +montecarlo_check(ID) :- + name(ID,IDN), + recover_number(IDN,FactIDName), + name(FactID,FactIDName), + new_sample_nonground(ID,FactID). + +% sampling from ground fact: set array value to 1 (in) or 2 (out) +new_sample(ID) :- + get_fact_probability(ID,Prob), + problog_random(R), + R + get_fact(ID,Fact), + split_grounding_id(ComposedID,ID,GID), + grounding_is_known(Fact,GID), + dynamic_probability_fact_extract(Fact,Prob) + ; + get_fact_probability(ID,Prob) + ), + problog_random(R), + (R < Prob -> + recorda(mc_true,problog_mc_id(ComposedID),_) + ; + recorda(mc_false,problog_mc_id(ComposedID),_), + fail + ). +% new_sample_nonground(ComposedID,_ID) :- +% recorda(mc_false,problog_mc_id(ComposedID),_), +% fail. + +split_grounding_id(Composed,Fact,Grounding) :- + name(Composed,C), + split_g_id(C,F,G), + name(Fact,F), + name(Grounding,G). +split_g_id([95|Grounding],[],Grounding) :- !. +split_g_id([A|B],[A|FactID],GroundingID) :- + split_g_id(B,FactID,GroundingID). + + + +% if threshold reached, remember this by setting limit to on, then +% if up is on, store stopped derivation in second trie +% +% List always length>=1 -> don't need []=true-case for tries +upper_bound(List) :- + problog_control(on, limit), + problog_control(check, up), + nb_getval(problog_stopped_proofs, Trie_Stopped_Proofs), + open_end_close_end(List, R), +% (prune_check(R, Trie_Stopped_Proofs) -> true; insert_ptree(R, Trie_Stopped_Proofs)). + insert_ptree(R, Trie_Stopped_Proofs). + +multiply_probabilities(CurrentLogP, LogProb, NLogProb) :- + NLogProb is CurrentLogP + LogProb. + +% this is called by all inference methods before the actual ProbLog goal +% to set up environment for proving +% it resets control flags, method specific values to be set afterwards! +init_problog(Threshold) :- + reset_problog_proof_id, + reset_non_ground_facts, + reset_control, + LT is log(Threshold), + b_setval(problog_probability, 0.0), + b_setval(problog_current_proof, []), + nb_setval(problog_threshold, LT), + problog_flag(maxsteps,MaxS), + init_tabling, + problog_var_clear_all, + b_setval(problog_steps, MaxS), + b_setval(problog_continuous_facts_used,[]), + retractall(hybrid_proof(_,_,_)), + retractall(hybrid_proof(_,_,_,_)), + retractall(hybrid_proof_disjoint(_,_,_,_)). + +% idea: proofs that are refinements of known proof can be pruned as they don't add probability mass +% note that current ptree implementation doesn't provide the check as there's no efficient method known so far... +prune_check(Proof, Trie) :- + problog_flag(prunecheck, on), + prune_check_ptree(Proof, Trie). + +% to call a ProbLog goal, patch all subgoals with the user's module context +% (as logical part is there, but probabilistic part in problog) +problog_call(Goal) :- + yap_flag(typein_module, Module), +%%% if user provides init_db, call this before proving goal + (current_predicate(_,Module:init_db) -> call(Module:init_db); true), + put_module(Goal,Module,ModGoal), + call(ModGoal). + +put_module((Mod:Goal,Rest),Module,(Mod:Goal,Transformed)) :- + !, + put_module(Rest,Module,Transformed). +put_module((Goal,Rest),Module,(Module:Goal,Transformed)) :- + !, + put_module(Rest,Module,Transformed). +put_module((Mod:Goal),_Module,(Mod:Goal)) :- + !. +put_module(Goal,Module,Module:Goal). + +% end of core + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% evaluating a DNF given as trie using BDD +% input: Trie the trie to be used +% output: probability and status (to catch potential failures/timeouts from outside) +% +% with internal BDD timeout (set using problog flag bdd_time) +% +% bdd_ptree/3 constructs files for problogbdd from the trie +% +% if calling ProblogBDD doesn't exit successfully, status will be timeout +% +% writes number of proofs in trie and BDD time to standard user output +% +% if remember is on, input files for problogbdd will be saved +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + +:- initialization(( + problog_var_define(sld_time, times, time, messages('SLD resolution', ':', ' ms')), + problog_var_define(bdd_script_time, times, time, messages('Generating BDD script', ':', ' ms')), + problog_var_define(bdd_generation_time, times, time, messages('Constructing BDD', ':', ' ms')), + problog_var_define(trie_statistics, memory, untyped, messages('Trie usage', ':', '')), + problog_var_define(probability, result, number, messages('Probabilty', ' = ', '')), + problog_var_define(bdd_script_time(Method), times, time, messages('Generating BDD script '(Method), ':', ' ms')), + problog_var_define(bdd_generation_time(Method), times, time, messages('Constructing BDD '(Method), ':', ' ms')), + problog_var_define(probability(Method), result, number, messages('Probabilty '(Method), ' = ', '')), + problog_var_define(trie_statistics(Method), memory, untyped, messages('Trie usage '(Method), ':', '')), + problog_var_define(dbtrie_statistics(Method), memory, untyped, messages('Depth Breadth Trie usage '(Method), ':', '')), + problog_var_define(db_trie_opts_performed(Method), memory, untyped, messages('Optimisations performed '(Method), ':', '')), + problog_var_define(variable_elimination_time, times, time, messages('Variable Elimination', ':', ' ms')), + problog_var_define(variable_elimination_stats, memory, untyped, messages('Variable Elimination', ':', '')) +)). + +problog_statistics(Stat, Result):- + problog_var_defined(Stat), + problog_var_is_set(Stat), + problog_var_get(Stat, Result). + +generate_order_by_prob_fact_appearance(Order, FileName):- + open(FileName, 'write', Stream), + forall(member(PF, Order), ( + ptree:get_var_name(PF, Name), + format(Stream, "@~w~n", [Name]))), +/* findall(_, (recorded(variable_elimination, prob_fact(PF, _), _), + ptree:get_var_name(PF, Name), + format(Stream, "@~w~n", [Name])), _),*/ + close(Stream). + +get_order(Trie, Order):- + findall(List, ptree:traverse_ptree(Trie, List), Proofs), + flatten(Proofs, ProbFacts), + remove_duplicates(ProbFacts, Order). + + +eval_dnf(OriTrie1, Prob, Status) :- + % Check whether we use Hybrid ProbLog + ( + hybrid_proof(_,_,_) + -> + ( % Yes! run the disjoining stuff + retractall(hybrid_proof_disjoint(_,_,_,_)), + disjoin_hybrid_proofs, + + init_ptree(OriTrie), % use this as tmp ptree + %%%%%%%%%%%%%%%%%%%%% + ( % go over all stored proofs + enum_member_ptree(List,OriTrie1), + ( + List=[_|_] + -> + Proof=List; + Proof=[List] + ), + ( + select(continuous(ProofID),Proof,Rest) + -> + ( + % this proof is using continuous facts + all_hybrid_subproofs(ProofID,List2), + append(Rest,List2,NewProof), + insert_ptree(NewProof,OriTrie) + ); + insert_ptree(Proof,OriTrie) + ), + + fail; + true + ) + %%%%%%%%%%%%%%%%%%%%% + ) ; + % Nope, just pass on the Trie + OriTrie=OriTrie1 + ), + + + ((problog_flag(variable_elimination, true), nb_getval(problog_nested_tries, false)) -> + statistics(walltime, _), + trie_check_for_and_cluster(OriTrie), + statistics(walltime, [_, VariableEliminationTime]), + trie_replace_and_cluster(OriTrie, Trie), + problog_var_set(variable_elimination_time, VariableEliminationTime), + variable_elimination_stats(Clusters, OrigPF, CompPF), + problog_var_set(variable_elimination_stats, compress(Clusters, OrigPF, CompPF)), + clean_up + ; + Trie = OriTrie + ), + (problog_flag(bdd_static_order, true) -> + get_order(Trie, Order), + problog_flag(static_order_file, SOFName), + convert_filename_to_working_path(SOFName, SOFileName), + generate_order_by_prob_fact_appearance(Order, SOFileName) + ; + true + ), + ptree:trie_stats(Memory, Tries, Entries, Nodes), + (nb_getval(problog_nested_tries, false) -> + ptree:trie_usage(Trie, TEntries, TNodes, TVirtualNodes), + problog_var_set(trie_statistics, tries(memory(Memory), tries(Tries), entries(TEntries), nodes(TNodes), virtualnodes(TVirtualNodes))) + ; + problog_var_set(trie_statistics, tries(memory(Memory), tries(Tries), entries(Entries), nodes(Nodes))) + ), + (problog_flag(triedump, true) -> + convert_filename_to_working_path(trie_file, TrieFile), + tell(TrieFile), + print_nested_ptree(Trie), + flush_output, + told, + tell(user_output) + ; + true + ), + nb_getval(problog_completed_proofs, Trie_Completed_Proofs), + ((Trie = Trie_Completed_Proofs, problog_flag(save_bdd, true)) -> + problog_control(on, remember) + ; + problog_control(off, remember) + ), + problog_flag(bdd_file, BDDFileFlag), + convert_filename_to_working_path(BDDFileFlag, BDDFile), + problog_flag(bdd_par_file, BDDParFileFlag), + convert_filename_to_working_path(BDDParFileFlag, BDDParFile), + % old reduction method doesn't support nested tries + ((problog_flag(use_old_trie, true), nb_getval(problog_nested_tries, false)) -> + statistics(walltime, _), + (problog_control(check, remember) -> + bdd_ptree_map(Trie, BDDFile, BDDParFile, Mapping), + convert_filename_to_working_path(save_map, MapFile), + tell(MapFile), + format('mapping(~q).~n', [Mapping]), + flush_output, + told + ; + bdd_ptree(Trie, BDDFile, BDDParFile) + ), + statistics(walltime, [_, ScriptGenerationTime]), + problog_var_set(bdd_script_time, ScriptGenerationTime), + + statistics(walltime, _), + execute_bdd_tool(BDDFile, BDDParFile, Prob_old, Status_old), + statistics(walltime,[_, BDDGenerationTime]), + (Status_old = ok -> + problog_var_set(bdd_generation_time, BDDGenerationTime), + problog_var_set(probability, Prob_old) + ; + problog_var_set(bdd_generation_time, fail), + problog_var_set(probability, fail) + ) + ; + true + ), + % naive method with nested trie support but not loops + ((problog_flag(use_naive_trie, true); (problog_flag(use_old_trie, true), nb_getval(problog_nested_tries, true))) -> + statistics(walltime, _), +% atomic_concat([BDDFile, '_naive'], BDDFile_naive), + BDDFile = BDDFile_naive, + nested_ptree_to_BDD_script(Trie, BDDFile_naive, BDDParFile), + statistics(walltime, [_, ScriptGenerationTime_naive]), + problog_var_set(bdd_script_time(naive), ScriptGenerationTime_naive), + + statistics(walltime, _), + execute_bdd_tool(BDDFile_naive, BDDParFile, Prob_naive, Status_naive), + statistics(walltime,[_, BDDGenerationTime_naive]), + (Status_naive = ok -> + problog_var_set(bdd_generation_time(naive), BDDGenerationTime_naive), + problog_var_set(probability(naive), Prob_naive) + ; + problog_var_set(bdd_generation_time(naive), fail), + problog_var_set(probability(naive), fail) + ) + ; + true + ), +% problog_statistics, +% print_nested_ptree(Trie), +% findall(_,(problog_chktabled(_ID, _T), writeln(problog_chktabled(_ID, _T))),_), + % reduction method with depth_breadth trie support + problog_flag(db_trie_opt_lvl, ROptLevel), + problog_flag(db_min_prefix, MinPrefix), + + (problog_flag(compare_opt_lvl, true) -> + generate_ints(0, ROptLevel, Levels) + ; + Levels = [ROptLevel] + ), + forall(member(OptLevel, Levels), ( + (problog_flag(use_db_trie, true) -> + tries:trie_db_opt_min_prefix(MinPrefix), + statistics(walltime, _), +% atomic_concat([BDDFile, '_builtin_', OptLevel], BDDFile_builtin), + BDDFile = BDDFile_builtin, + (nb_getval(problog_nested_tries, false) -> + trie_to_bdd_trie(Trie, DBTrie, BDDFile_builtin, OptLevel, BDDParFile) + ; + nested_trie_to_bdd_trie(Trie, DBTrie, BDDFile_builtin, OptLevel, BDDParFile) + ), + atomic_concat(['builtin_', OptLevel], Builtin), + ptree:trie_stats(DBMemory, DBTries, DBEntries, DBNodes), + FM is DBMemory - Memory, + FT is DBTries - Tries, + FE is DBEntries - Entries, + FN is DBNodes - Nodes, + problog_var_set(dbtrie_statistics(Builtin), tries(memory(FM), tries(FT), entries(FE), nodes(FN))), + + delete_ptree(DBTrie), + statistics(walltime, [_, ScriptGenerationTime_builtin]), + problog_var_set(bdd_script_time(Builtin), ScriptGenerationTime_builtin), + + statistics(walltime, _), + execute_bdd_tool(BDDFile_builtin, BDDParFile, Prob_builtin, Status_builtin), + statistics(walltime,[_, BDDGenerationTime_builtin]), + ptree_db_trie_opt_performed(LVL1, LVL2, LV3), + problog_var_set(db_trie_opts_performed(Builtin), opt_perform(LVL1, LVL2, LV3)), + (Status_builtin = ok -> + problog_var_set(bdd_generation_time(Builtin), BDDGenerationTime_builtin), + problog_var_set(probability(Builtin), Prob_builtin) + ; + problog_var_set(bdd_generation_time(Builtin), fail), + problog_var_set(probability(Builtin), fail) + ) + ; + true + ) + )), + + % decomposition method + (problog_flag(use_dec_trie, true) -> + statistics(walltime, _), +% atomic_concat([BDDFile, '_dec'], BDDFile_dec), + BDDFile = BDDFile_dec, + ptree_decomposition(Trie, BDDFile_dec, BDDParFile), + statistics(walltime, [_, ScriptGenerationTime_dec]), + problog_var_set(bdd_script_time(dec), ScriptGenerationTime_dec), + + statistics(walltime, _), + execute_bdd_tool(BDDFile_dec, BDDParFile, Prob_dec, Status_dec), + statistics(walltime,[_, BDDGenerationTime_dec]), + (Status_dec = ok -> + problog_var_set(bdd_generation_time(dec), BDDGenerationTime_dec), + problog_var_set(probability(dec), Prob_dec) + ; + problog_var_set(bdd_generation_time(dec), fail), + problog_var_set(probability(dec), fail) + ) + ; + true + ), + + (problog_control(check, remember) -> + convert_filename_to_working_path('save_script', SaveBDDFile), + rename_file(BDDFile, SaveBDDFile), + convert_filename_to_working_path('save_params', SaveBDDParFile), + rename_file(BDDParFile, SaveBDDParFile) + ; + true + ), + problog_control(off, remember), + (var(Status_old)-> + (var(Status_naive)-> + (var(Status_dec) -> + atomic_concat('builtin_', ROptLevel, ProbStat), + problog_statistics(probability(ProbStat), ProbB), + (ProbB = fail -> + Status = timeout + ; + Prob = ProbB, + Status = ok + ) + ; + Prob = Prob_dec, + Status = Status_dec + ) + ; + Prob = Prob_naive, + Status = Status_naive + ) + ; + Prob = Prob_old, + Status = Status_old + ), + + (Trie =\= OriTrie -> + delete_ptree(Trie) + ; + true + ). + +generate_ints(End, End, [End]). +generate_ints(Start, End, [Start|Rest]):- + Start < End, + Current is Start + 1, + generate_ints(Current, End, Rest). + +execute_bdd_tool(BDDFile, BDDParFile, Prob, Status):- + problog_flag(bdd_time, BDDTime), + problog_flag(bdd_result, ResultFileFlag), + (problog_flag(dynamic_reorder, true) -> + ParamD = '' + ; + ParamD = ' -dreorder' + ), + (problog_flag(bdd_static_order, true) -> + problog_flag(static_order_file, FileName), + convert_filename_to_working_path(FileName, SOFileName), + atomic_concat([ParamD, ' -sord ', SOFileName], Param) + ; + Param = ParamD + ), + convert_filename_to_problog_path('problogbdd', ProblogBDD), + convert_filename_to_working_path(ResultFileFlag, ResultFile), + atomic_concat([ProblogBDD, Param,' -l ', BDDFile, ' -i ', BDDParFile, ' -m p -t ', BDDTime, ' > ', ResultFile], Command), + shell(Command, Return), + (Return =\= 0 -> + Status = timeout + ; + see(ResultFile), + read(probability(Prob)), + seen, + delete_file(ResultFile), + Status = ok + ). + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% different inference methods +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% approximate inference: bounds based on single probability threshold +% problog_threshold(+Goal,+Threshold,-LowerBound,-UpperBound,-Status) +% +% use backtracking over problog_call to get all solutions +% +% trie 1 collects proofs, trie 2 collects stopped derivations, trie 3 is used to unit them for the upper bound +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + +problog_threshold(Goal, Threshold, _, _, _) :- + init_problog_threshold(Threshold), + problog_control(on,up), + problog_call(Goal), + add_solution, + fail. +problog_threshold(_, _, LP, UP, Status) :- + compute_bounds(LP, UP, Status). + +init_problog_threshold(Threshold) :- + init_ptree(Trie_Completed_Proofs), + nb_setval(problog_completed_proofs, Trie_Completed_Proofs), + init_ptree(Trie_Stopped_Proofs), + nb_setval(problog_stopped_proofs, Trie_Stopped_Proofs), + init_problog(Threshold). + +add_solution :- + % get the probabilistic facts used in this proof + b_getval(problog_current_proof, IDs), + (IDs == [] -> R = []; open_end_close_end(IDs, R)), + + % get the continuous facts used in this proof + % (Hybrid ProbLog + b_getval(problog_continuous_facts_used,Cont_IDs), + ( + Cont_IDs == [] + -> + Continuous=[]; + ( + proof_id(ProofID), + collect_all_intervals(Cont_IDs,ProofID,AllIntervals), + ( + AllIntervals==[] + -> + Continuous=[]; + ( + Continuous=[continuous(ProofID)], + assertz(hybrid_proof(ProofID,Cont_IDs,AllIntervals)) + ) + ) + ) + ), + + % we have both, no add it to the trie + nb_getval(problog_completed_proofs, Trie_Completed_Proofs), + append(R,Continuous,Final), + ( + Final==[] + -> + insert_ptree(true, Trie_Completed_Proofs); + insert_ptree(Final, Trie_Completed_Proofs) + ). + + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + +collect_all_intervals([],_,[]). +collect_all_intervals([(ID,GroundID)|T],ProofID,[Interval|T2]) :- + atomic_concat([interval,'_',GroundID],Key), + b_getval(Key,Interval), + Interval \= all, % we do not need to store continuous + % variables with domain [-oo,oo] (they have probability 1) + !, + assertz(hybrid_proof(ProofID,ID,GroundID,Interval)), + collect_all_intervals(T,ProofID,T2). +collect_all_intervals([_|T],ProofID,T2) :- + collect_all_intervals(T,ProofID,T2). + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + + +all_hybrid_subproofs(ProofID,List) :- + findall((ID,GroundID,Intervals),hybrid_proof_disjoint(ProofID,ID,GroundID,Intervals),All), + generate_all_proof_combinations(All,List). + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + +generate_all_proof_combinations([],[]). +generate_all_proof_combinations([(_ID,GroundID,Intervals)|T],Result) :- + member((Interval,Tail),Intervals), + intervals_encode(Interval,IntervalEncoded), + atomic_concat([GroundID,IntervalEncoded],FullID), + encode_tail(Tail,GroundID,TailEncoded), + append([FullID|TailEncoded],T2,Result), + generate_all_proof_combinations(T,T2). + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + +encode_tail([],_,[]). +encode_tail([A|T],ID,[not(FullID)|T2]) :- + intervals_encode(A,AEncoded), + atomic_concat([ID,AEncoded],FullID), + encode_tail(T,ID,T2). + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + +disjoin_hybrid_proofs :- + % collect all used continuous facts + findall(GroundID,hybrid_proof(_,_,GroundID,_),IDs), + sort(IDs,IDsSorted), + + disjoin_hybrid_proofs(IDsSorted). + +disjoin_hybrid_proofs([]). +disjoin_hybrid_proofs([GroundID|T]) :- + findall(Interval,hybrid_proof(_,_,GroundID,Interval),Intervals), + intervals_partition(Intervals,Partition), + + % go over all proofs where this fact occurs + ( + hybrid_proof(ProofID,ID,GroundID,Interval), + intervals_disjoin(Interval,Partition,PInterval), + assertz(hybrid_proof_disjoint(ProofID,ID,GroundID,PInterval)), + + fail; % go to next proof + true + ), + + disjoin_hybrid_proofs(T). + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + +% End Hybrid + +compute_bounds(LP, UP, Status) :- + nb_getval(problog_completed_proofs, Trie_Completed_Proofs), + nb_getval(problog_stopped_proofs, Trie_Stopped_Proofs), + eval_dnf(Trie_Completed_Proofs, LP, StatusLow), + (StatusLow \== ok -> + Status = StatusLow + ; + merge_ptree(Trie_Completed_Proofs, Trie_Stopped_Proofs, Trie_All_Proofs), + nb_setval(problog_all_proofs, Trie_All_Proofs), + eval_dnf(Trie_All_Proofs, UP, Status)), + delete_ptree(Trie_Completed_Proofs), + delete_ptree(Trie_Stopped_Proofs), + delete_ptree(Trie_All_Proofs). + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% approximate inference: lower bound based on all proofs above probability threshold +% problog_low(+Goal,+Threshold,-LowerBound,-Status) +% +% same as problog_threshold/5, but lower bound only (no stopped derivations stored) +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + + +problog_low(Goal, Threshold, _, _) :- + init_problog_low(Threshold), + problog_control(off, up), + statistics(walltime, _), + problog_call(Goal), + add_solution, + fail. +problog_low(_, _, LP, Status) :- + statistics(walltime, [_,E]), %theo + problog_var_set(sld_time, E), + nb_getval(problog_completed_proofs, Trie_Completed_Proofs), + eval_dnf(Trie_Completed_Proofs, LP, Status), + (problog_flag(verbose, true)-> + problog_statistics + ; + true + ), + delete_ptree(Trie_Completed_Proofs), + (problog_flag(retain_tables, true) -> retain_tabling; true), + clear_tabling. + +init_problog_low(Threshold) :- + init_ptree(Trie_Completed_Proofs), + nb_setval(problog_completed_proofs, Trie_Completed_Proofs), + init_problog(Threshold). + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% approximate inference: bounds by iterative deepening up to interval width Delta +% problog_delta(+Goal,+Delta,-LowerBound,-UpperBound,-Status) +% +% wraps iterative deepening around problog_threshold, i.e. +% - starts with threshold given by first_threshold flag +% - if Up-Low >= Delta, multiply threshold by factor given in id_stepsize flag and iterate +% (does not use problog_threshold as trie 1 is kept over entire search) +% +% local dynamic predicates low/2, up/2, stopDiff/1 +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + +problog_delta(Goal, Delta, Low, Up, Status) :- + problog_flag(first_threshold,InitT), + init_problog_delta(InitT,Delta), + problog_control(on,up), + problog_delta_id(Goal,Status), + nb_getval(problog_completed_proofs, Trie_Completed_Proofs), + nb_getval(problog_stopped_proofs, Trie_Stopped_Proofs), + delete_ptree(Trie_Completed_Proofs), + delete_ptree(Trie_Stopped_Proofs), + (retract(low(_,Low)) -> true; true), + (retract(up(_,Up)) -> true; true). + + +init_problog_delta(Threshold,Delta) :- + retractall(low(_,_)), + retractall(up(_,_)), + retractall(stopDiff(_)), + init_ptree(Trie_Completed_Proofs), + nb_setval(problog_completed_proofs, Trie_Completed_Proofs), + init_ptree(Trie_Stopped_Proofs), + nb_setval(problog_stopped_proofs, Trie_Stopped_Proofs), + assertz(low(0,0.0)), + assertz(up(0,1.0)), + assertz(stopDiff(Delta)), + init_problog(Threshold). + +problog_delta_id(Goal, _) :- + problog_call(Goal), + add_solution, % reused from problog_threshold + fail. +problog_delta_id(Goal, Status) :- + evaluateStep(Ans,StatusE), + problog_flag(last_threshold_log,Stop), + nb_getval(problog_threshold,Min), + (StatusE \== ok -> + Status = StatusE + ; + ( + Ans = 1 -> + Status = ok + ; + Min =< Stop -> + Status = stopreached + ; + problog_control(check,limit) -> + problog_control(off,limit), + problog_flag(id_stepsize_log,Step), + New is Min+Step, + nb_setval(problog_threshold,New), + problog_delta_id(Goal, Status) + ; + true + )). + +% call the dnf evaluation where needed +evaluateStep(Ans,Status) :- once(evalStep(Ans,Status)). + +evalStep(Ans,Status) :- + stopDiff(Delta), + nb_getval(problog_completed_proofs, Trie_Completed_Proofs), + nb_getval(problog_stopped_proofs, Trie_Stopped_Proofs), + count_ptree(Trie_Completed_Proofs, NProofs), + count_ptree(Trie_Stopped_Proofs, NCands), + ( + problog_flag(verbose,true) + -> + format(user,'~w proofs, ~w stopped derivations~n',[NProofs,NCands]); + true + ), + flush_output(user), + eval_lower(NProofs,Low,StatusLow), + ( + StatusLow \== ok + -> + Status = StatusLow; + up(_, OUP), + IntDiff is OUP-Low, + ((IntDiff < Delta; IntDiff =:= 0) -> + Up = OUP, + StatusUp = ok + ; + eval_upper(NCands, Up, StatusUp), + delete_ptree(Trie_Stopped_Proofs), + init_ptree(New_Trie_Stopped_Proofs), + nb_setval(problog_stopped_proofs, New_Trie_Stopped_Proofs) + ), + (StatusUp \== ok -> + Status = StatusUp + ; + Diff is Up-Low, + (problog_flag(verbose,true) -> format(user,'difference: ~6f~n',[Diff]);true), + flush_output(user), + ((Diff < Delta; Diff =:= 0) -> Ans = 1; Ans = 0), + Status = ok + ) + ). + +% no need to re-evaluate if no new proofs found on this level +eval_lower(N,P,ok) :- + low(N,P). +% evaluate if there are proofs +eval_lower(N,P,Status) :- + N > 0, + low(OldN,_), + N \= OldN, + nb_getval(problog_completed_proofs, Trie_Completed_Proofs), + eval_dnf(Trie_Completed_Proofs,P,Status), + (Status = ok -> + retract(low(_,_)), + assertz(low(N,P)), + (problog_flag(verbose,true) -> format(user,'lower bound: ~6f~n',[P]);true), + flush_output(user) + ; + true). + +% if no stopped derivations, up=low +eval_upper(0,P,ok) :- + retractall(up(_,_)), + low(N,P), + assertz(up(N,P)). +% else merge proofs and stopped derivations to get upper bound +% in case of timeout or other problems, skip and use bound from last level +eval_upper(N,UpP,ok) :- + N > 0, + nb_getval(problog_completed_proofs, Trie_Completed_Proofs), + nb_getval(problog_stopped_proofs, Trie_Stopped_Proofs), + merge_ptree(Trie_Completed_Proofs,Trie_Stopped_Proofs,Trie_All_Proofs), + nb_setval(problog_all_proofs, Trie_All_Proofs), + eval_dnf(Trie_All_Proofs,UpP,StatusUp), + delete_ptree(Trie_All_Proofs), + (StatusUp = ok -> + retract(up(_,_)), + assertz(up(N,UpP)) + ; + (problog_flag(verbose,true) -> format(user,'~w - continue using old up~n',[StatusUp]);true), + flush_output(user), + up(_,UpP) + ). + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% explanation probability - returns list of facts used or constant 'unprovable' as third argument +% problog_max(+Goal,-Prob,-Facts) +% +% uses iterative deepening with samw parameters as bounding algorithm +% threshold gets adapted whenever better proof is found +% +% uses local dynamic predicates max_probability/1 and max_proof/1 +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + +problog_max(Goal, Prob, Facts) :- + problog_flag(first_threshold,InitT), + init_problog_max(InitT), + problog_control(off,up), + problog_max_id(Goal, Prob, FactIDs),% theo todo + ( FactIDs = [_|_] -> get_fact_list(FactIDs, Facts); + Facts = FactIDs). + +init_problog_max(Threshold) :- + retractall(max_probability(_)), + retractall(max_proof(_)), + assertz(max_probability(-999999)), + assertz(max_proof(unprovable)), + init_problog(Threshold). + +update_max :- + b_getval(problog_probability, CurrP), + max_probability(MaxP), + (CurrP =< MaxP -> + fail + ; + b_getval(problog_current_proof, IDs), + open_end_close_end(IDs, R), + retractall(max_proof(_)), + assertz(max_proof(R)), + nb_setval(problog_threshold, CurrP), + retractall(max_probability(_)), + assertz(max_probability(CurrP)) + ). + +problog_max_id(Goal, _Prob, _Clauses) :- + problog_call(Goal), + update_max, + fail. +problog_max_id(Goal, Prob, Clauses) :- + max_probability(MaxP), + nb_getval(problog_threshold, LT), + problog_flag(last_threshold_log, ToSmall), + ((MaxP >= LT; \+ problog_control(check, limit); LT < ToSmall) -> + ((max_proof(unprovable), problog_control(check,limit), LT < ToSmall) -> + problog_flag(last_threshold, Stopping), + Clauses = unprovable(Stopping) + ; + max_proof(Clauses) + ), + Prob is exp(MaxP) + ; + problog_flag(id_stepsize_log, Step), + NewLT is LT + Step, + nb_setval(problog_threshold, NewLT), + problog_control(off, limit), + problog_max_id(Goal, Prob, Clauses) + ). + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% lower bound using k best proofs +% problog_kbest(+Goal,+K,-Prob,-Status) +% +% does iterative deepening search similar to problog_max, but for k(>=1) most likely proofs +% afterwards uses BDD evaluation to calculate probability (also for k=1 -> uniform treatment in learning) +% +% uses dynamic local predicate current_kbest/3 to collect proofs, +% only builds trie at the end (as probabilities of single proofs are important here) +% +% note: >k proofs will be used if the one at position k shares its probability with others, +% as all proofs with that probability will be included +% +% version with _save at the end renames files for problogbdd to keep them +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +problog_kbest_save(Goal, K, Prob, Status, BDDFile, ParamFile) :- + problog_flag(dir, InternWorkingDir), + problog_flag(bdd_file, InternBDDFlag), + problog_flag(bdd_par_file, InternParFlag), + split_path_file(BDDFile, WorkingDir, BDDFileName), + split_path_file(ParamFile, _WorkingDir, ParamFileName), + flag_store(dir, WorkingDir), + flag_store(bdd_file, BDDFileName), + flag_store(bdd_par_file, ParamFileName), + problog_kbest(Goal, K, Prob, Status), + flag_store(dir, InternWorkingDir), + flag_store(bdd_file, InternBDDFlag), + flag_store(bdd_par_file, InternParFlag). +% ( Status=ok -> +% problog_flag(bdd_file,InternBDDFlag), +% problog_flag(bdd_par_file,InternParFlag), +% convert_filename_to_working_path(InternBDDFlag, InternBDD), +% convert_filename_to_working_path(InternParFlag, InternPar), +% rename_file(InternBDD,BDDFile), +% rename_file(InternPar,ParamFile) +% ; +% true). + +problog_kbest(Goal, K, Prob, Status) :- + problog_flag(first_threshold,InitT), + init_problog_kbest(InitT), + problog_control(off,up), + problog_kbest_id(Goal, K), + retract(current_kbest(_,ListFound,_NumFound)), + build_prefixtree(ListFound), + nb_getval(problog_completed_proofs, Trie_Completed_Proofs), + eval_dnf(Trie_Completed_Proofs,Prob,Status), + delete_ptree(Trie_Completed_Proofs). + +problog_real_kbest(Goal, K, Prob, Status) :- + problog_flag(first_threshold,InitT), + init_problog_kbest(InitT), + problog_control(off,up), + problog_kbest_id(Goal, K), + retract(current_kbest(_,RawListFound,NumFound)), + % limiting the number of proofs is not only needed for fast SLD resolution but also for fast BDD building. + % one can't assume that kbest is called for the former and not for the latter + take_k_best(RawListFound,K,NumFound,ListFound), + build_prefixtree(ListFound), + nb_getval(problog_completed_proofs, Trie_Completed_Proofs), + eval_dnf(Trie_Completed_Proofs,Prob,Status), + delete_ptree(Trie_Completed_Proofs). + +init_problog_kbest(Threshold) :- + retractall(current_kbest(_,_,_)), + assertz(current_kbest(-999999,[],0)), %(log-threshold,proofs,num_proofs) + init_ptree(Trie_Completed_Proofs), + nb_setval(problog_completed_proofs, Trie_Completed_Proofs), + init_problog(Threshold). + +problog_kbest_id(Goal, K) :- + problog_call(Goal), + update_kbest(K), + fail. +problog_kbest_id(Goal, K) :- + current_kbest(CurrentBorder,_,Found), + nb_getval(problog_threshold, Min), + problog_flag(last_threshold_log,ToSmall), + ((Found>=K ; \+ problog_control(check,limit) ; Min < CurrentBorder ; Min < ToSmall) -> + true + ; + problog_flag(id_stepsize_log,Step), + NewLT is Min+Step, + nb_setval(problog_threshold, NewLT), + problog_control(off,limit), + problog_kbest_id(Goal, K)). + +update_kbest(K) :- + b_getval(problog_probability,NewLogProb), + current_kbest(LogThreshold,_,_), + (NewLogProb>=LogThreshold -> + b_getval(problog_current_proof,RevProof), + open_end_close_end(RevProof,Proof), + update_current_kbest(K,NewLogProb,Proof) + ; + fail). + +update_current_kbest(_,NewLogProb,Cl) :- + current_kbest(_,List,_), + memberchk(NewLogProb-Cl,List), + !. +update_current_kbest(K,NewLogProb,Cl) :- + retract(current_kbest(OldThres,List,Length)), + sorted_insert(NewLogProb-Cl,List,NewList), + NewLength is Length+1, + (NewLength < K -> + assertz(current_kbest(OldThres,NewList,NewLength)) + ; + (NewLength>K -> + First is NewLength-K+1, + cutoff(NewList,NewLength,First,FinalList,FinalLength) + ; FinalList=NewList, FinalLength=NewLength), + FinalList=[NewThres-_|_], + nb_setval(problog_threshold,NewThres), + assertz(current_kbest(NewThres,FinalList,FinalLength))). + +sorted_insert(A,[],[A]). +sorted_insert(A-LA,[B1-LB1|B], [A-LA,B1-LB1|B] ) :- + A =< B1. +sorted_insert(A-LA,[B1-LB1|B], [B1-LB1|C] ) :- + A > B1, + sorted_insert(A-LA,B,C). + +% keeps all entries with lowest probability, even if implying a total of more than k +cutoff(List,Len,1,List,Len) :- !. +cutoff([P-L|List],Length,First,[P-L|List],Length) :- + nth(First,[P-L|List],PF-_), + PF=:=P, + !. +cutoff([_|List],Length,First,NewList,NewLength) :- + NextFirst is First-1, + NextLength is Length-1, + cutoff(List,NextLength,NextFirst,NewList,NewLength). + +build_prefixtree([]). +build_prefixtree([_-[]|_List]) :- + !, + nb_getval(problog_completed_proofs, Trie_Completed_Proofs), + insert_ptree(true,Trie_Completed_Proofs). +build_prefixtree([LogP-L|List]) :- + ( + problog_flag(show_proofs,true) + -> + get_fact_list(L,ListOfFacts), + P is exp(LogP), + format(user,'~q ~q~n',[P,ListOfFacts]) + ; + true + ), + nb_getval(problog_completed_proofs, Trie_Completed_Proofs), + insert_ptree(L,Trie_Completed_Proofs), + build_prefixtree(List). + +take_k_best(In,K,OutOf,Out) :- + ( + K>=OutOf + -> + In = Out; + In = [_|R], + OutOf2 is OutOf-1, + take_k_best(R,K,OutOf2,Out) + ). + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% exact probability +% problog_exact(+Goal,-Prob,-Status) +% +% using all proofs = using all proofs with probability > 0 +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + +problog_exact(Goal,Prob,Status) :- + problog_control(on, exact), + problog_low(Goal,0,Prob,Status), + problog_control(off, exact). + +problog_exact_save(Goal,Prob,Status,BDDFile,ParamFile) :- + problog_flag(dir, InternWorkingDir), + problog_flag(bdd_file, InternBDDFlag), + problog_flag(bdd_par_file, InternParFlag), + split_path_file(BDDFile, WorkingDir, BDDFileName), + split_path_file(ParamFile, _WorkingDir, ParamFileName), + flag_store(dir, WorkingDir), + flag_store(bdd_file, BDDFileName), + flag_store(bdd_par_file, ParamFileName), + problog_control(on, exact), + problog_low(Goal,0,Prob,Status), + problog_control(off, exact), + flag_store(dir, InternWorkingDir), + flag_store(bdd_file, InternBDDFlag), + flag_store(bdd_par_file, InternParFlag). +% ( +% Status==ok +% -> +% ( +% problog_flag(bdd_file,InternBDDFlag), +% problog_flag(bdd_par_file,InternParFlag), +% problog_flag(dir,DirFlag), +% atomic_concat([DirFlag,InternBDDFlag],InternBDD), +% atomic_concat([DirFlag,InternParFlag],InternPar), +% rename_file(InternBDD,BDDFile), +% rename_file(InternPar,ParamFile) +% ); +% true +% ). + +problog_collect_trie(Goal):- + problog_call(Goal), + add_solution, + fail. +problog_collect_trie(_Goal). + +problog_save_state(State):- + nb_getval(problog_completed_proofs, Trie_Completed_Proofs), + nb_getval(problog_current_proof, IDs), + recordz(problog_stack, store(Trie_Completed_Proofs, IDs), State), + init_ptree(Sub_Trie_Completed_Proofs), + nb_setval(problog_completed_proofs, Sub_Trie_Completed_Proofs), + nb_setval(problog_current_proof, []). + +problog_restore_state(State):- + recorded(problog_stack, store(Trie_Completed_Proofs, IDs), State), + erase(State), + nb_setval(problog_completed_proofs, Trie_Completed_Proofs), + nb_setval(problog_current_proof, IDs). + +problog_exact_nested(Goal, Prob, Status):- + problog_save_state(State), + problog_collect_trie(Goal), + nb_getval(problog_completed_proofs, Trie_Completed_Proofs), +/* writeln(Goal), + print_nested_ptree(Trie_Completed_Proofs),*/ + eval_dnf(Trie_Completed_Proofs, Prob, Status), + delete_ptree(Trie_Completed_Proofs), + problog_restore_state(State). + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% probability by sampling: +% running another N samples until 95percentCI-width format('search for ~q~n',[Goal]);true), + montecarlo(Goal,Delta,K,0,File,0,T1), + problog_control(off,mc). + +% calculate values after K samples +montecarlo(Goal,Delta,K,SamplesSoFar,File,PositiveSoFar,InitialTime) :- + SamplesNew is SamplesSoFar+1, + SamplesNew mod K =:= 0, + !, + copy_term(Goal,GoalC), + (mc_prove(GoalC) -> Next is PositiveSoFar+1; Next=PositiveSoFar), + Prob is Next/SamplesNew, + statistics(walltime,[T2,_]), + Time is (T2-InitialTime),%/1000, + + problog_convergence_check(Time, Prob, SamplesNew, Delta, _Epsilon, Converged), + ((Converged = true; Converged = terminate) -> + (problog_flag(verbose,true) -> + format('Runtime ~w ms~2n',[Time]) + ; + true + ), + assertz(mc_prob(Prob)) + ; + montecarlo(Goal,Delta,K,SamplesNew,File,Next,InitialTime) + ). + + +% Epsilon is 2*sqrt(Prob*(1-Prob)/SamplesNew), +% Low is Prob-Epsilon, +% High is Prob+Epsilon, +% Diff is 2*Epsilon, +% (problog_flag(verbose,true) -> format('~n~w samples~nestimated probability ~w~n95 percent confidence interval [~w,~w]~n',[SamplesNew,Prob,Low,High]);true), +% open(File,append,Log), +% format(Log,'~w ~8f ~8f ~8f ~8f ~3f~n',[SamplesNew,Prob,Low,High,Diff,Time]), +% close(Log), + + +% ((Diff +% (problog_flag(verbose,true) -> +% format('Runtime ~w sec~2n',[Time]) +% ; +% true +% ), +% assertz(mc_prob(Prob)) +% ; +% montecarlo(Goal,Delta,K,SamplesNew,File,Next,InitialTime) +% ). + +% continue until next K samples done +montecarlo(Goal,Delta,K,SamplesSoFar,File,PositiveSoFar,InitialTime) :- + SamplesNew is SamplesSoFar+1, + copy_term(Goal,GoalC), + (mc_prove(GoalC) -> Next is PositiveSoFar+1; Next=PositiveSoFar), + montecarlo(Goal,Delta,K,SamplesNew,File,Next,InitialTime). + +mc_prove(A) :- !, + (get_some_proof(A) -> + clean_sample + ; + clean_sample,fail + ). + +clean_sample :- + reset_static_array(mc_sample), + eraseall(mc_true), + eraseall(mc_false), + reset_non_ground_facts, +% problog_abolish_all_tables. + problog_tabled(P), + problog_abolish_table(P), + fail. +clean_sample. + +% find new proof -- need to reset control after init +get_some_proof(Goal) :- + init_problog(0), + problog_control(on,mc), + problog_call(Goal). + + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% exact probability of all ground instances of Goal +% output goes to File +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +problog_answers(Goal,File) :- + set_problog_flag(verbose,false), + retractall(answer(_)), +% this will not give the exact prob of Goal! + problog_exact((Goal,ground(Goal),\+problog:answer(Goal),assertz(problog:answer(Goal))),_,_), + open(File,write,_,[alias(answer)]), + eval_answers, + close(answer). + +eval_answers :- + retract(answer(G)), + problog_exact(G,P,_), + format(answer,'answer(~q,~w).~n',[G,P]), + fail. +eval_answers. + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% find k most likely different answers (using their explanation prob as score) +% largely copied+adapted from kbest, uses same dynamic predicate +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +problog_kbest_answers(Goal,K,ResultList) :- + problog_flag(first_threshold,InitT), + init_problog_kbest(InitT), + nb_getval(problog_completed_proofs, Trie_Completed_Proofs), + delete_ptree( Trie_Completed_Proofs), % this is just because we reuse init from kbest and don't need the tree + problog_control(off,up), + problog_kbest_answers_id(Goal, K), + retract(current_kbest(_,LogResultList,_NumFound)), + transform_loglist_to_result(LogResultList,ResultList). + +problog_kbest_answers_id(Goal, K) :- + problog_call(Goal), + copy_term(Goal,GoalCopy), % needed? + update_kbest_answers(GoalCopy,K), + fail. +problog_kbest_answers_id(Goal, K) :- + current_kbest(CurrentBorder,_,Found), + nb_getval(problog_threshold, Min), + problog_flag(last_threshold_log,ToSmall), + ((Found>=K ; \+ problog_control(check,limit) ; Min < CurrentBorder ; Min < ToSmall) -> + true + ; + problog_flag(id_stepsize_log,Step), + NewLT is Min+Step, + nb_setval(problog_threshold, NewLT), + problog_control(off,limit), + problog_kbest_answers_id(Goal, K)). + +update_kbest_answers(Goal,K) :- + b_getval(problog_probability,NewLogProb), + current_kbest(LogThreshold,_,_), + (NewLogProb>=LogThreshold -> + update_current_kbest_answers(K,NewLogProb,Goal) + ; + fail). + +update_current_kbest_answers(_,NewLogProb,Goal) :- + current_kbest(_,List,_), + update_prob_of_known_answer(List,Goal,NewLogProb,NewList), + !, + keysort(NewList,SortedList),%format(user_error,'updated variant of ~w~n',[Goal]), + retract(current_kbest(K,_,Len)), + assertz(current_kbest(K,SortedList,Len)). +update_current_kbest_answers(K,NewLogProb,Goal) :- + retract(current_kbest(OldThres,List,Length)), + sorted_insert(NewLogProb-Goal,List,NewList),%format(user_error,'inserted new element ~w~n',[Goal]), + NewLength is Length+1, + (NewLength < K -> + assertz(current_kbest(OldThres,NewList,NewLength)) + ; + (NewLength>K -> + First is NewLength-K+1, + cutoff(NewList,NewLength,First,FinalList,FinalLength) + ; FinalList=NewList, FinalLength=NewLength), + FinalList=[NewThres-_|_], + nb_setval(problog_threshold,NewThres), + assertz(current_kbest(NewThres,FinalList,FinalLength))). + +% this fails if there is no variant -> go to second case above +update_prob_of_known_answer([OldLogP-OldGoal|List],Goal,NewLogProb,[MaxLogP-OldGoal|List]) :- + variant(OldGoal,Goal), + !, + MaxLogP is max(OldLogP,NewLogProb). +update_prob_of_known_answer([First|List],Goal,NewLogProb,[First|NewList]) :- + update_prob_of_known_answer(List,Goal,NewLogProb,NewList). + +transform_loglist_to_result(In,Out) :- + transform_loglist_to_result(In,[],Out). +transform_loglist_to_result([],Acc,Acc). +transform_loglist_to_result([LogP-G|List],Acc,Result) :- + P is exp(LogP), + transform_loglist_to_result(List,[P-G|Acc],Result). + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% GENERAL PURPOSE PREDICATES FOR DTPROBLOG +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% Do inference of a single goal, using the default inference method +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + +problog_infer(Goal,Prob) :- + problog_flag(inference,Method), + problog_infer(Method,Goal,Prob). + +problog_infer(exact,Goal,Prob) :- + problog_exact(Goal,Prob,ok). +problog_infer(atleast-K-best,Goal,Prob) :- + problog_kbest(Goal,K,Prob,ok). +problog_infer(K-best,Goal,Prob) :- + problog_real_kbest(Goal,K,Prob,ok). +problog_infer(montecarlo(Confidence),Goal,Prob) :- + problog_montecarlo(Goal,Confidence,Prob). +problog_infer(delta(Width),Goal,Prob) :- + problog_delta(Goal,Width,Bound_low,Bound_up,ok), + Prob is 0.5*(Bound_low+Bound_up). +problog_infer(low(Threshold),Goal,Prob) :- + problog_low(Goal,Threshold,Prob,ok). +problog_infer(threshold(Threshold),Goal,Prob) :- + problog_threshold(Goal,Threshold,Bound_low,Bound_up,ok), + Prob is 0.5*(Bound_low+Bound_up). + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% Do inference of a set of queries, using the default inference method +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + +problog_infer_forest([],[]) :- !. +problog_infer_forest(Goals,Probs) :- + (problog_infer_forest_supported -> + problog_bdd_forest(Goals), + length(Goals,N), + eval_bdd_forest(N,Probs,ok) + ; + throw(error('Flag settings not supported by problog_infer_forest/1.')) + ). + +problog_infer_forest_supported :- problog_bdd_forest_supported. + +eval_bdd_forest(N,Probs,Status) :- + bdd_files(BDDFile,BDDParFile), + problog_flag(bdd_time,BDDTime), + (problog_flag(dynamic_reorder, true) -> + ParamD = '' + ; + ParamD = ' -dreorder' + ), + (problog_flag(bdd_static_order, true) -> + problog_flag(static_order_file, FileName), + convert_filename_to_working_path(FileName, SOFileName), + atomic_concat([ParamD, ' -sord ', SOFileName], Param) + ; + Param = ParamD + ), + convert_filename_to_problog_path('problogbdd', ProblogBDD), + problog_flag(bdd_result,ResultFileFlag), + convert_filename_to_working_path(ResultFileFlag, ResultFile), + atomic_concat([ProblogBDD, Param,' -l ', BDDFile, ' -i ', BDDParFile, ' -m p -t ', BDDTime, ' > ', ResultFile], Command), + statistics(walltime,_), + shell(Command,Return), + (Return =\= 0 -> + Status = timeout + ; + statistics(walltime,[_,E3]), + (problog_flag(verbose,true) -> format(user,'~w ms BDD processing~n',[E3]);true), + see(ResultFile), + read_probs(N,Probs), + seen, + Status = ok, + % cleanup + % TODO handle flag for keeping files + (problog_flag(save_bdd,true) -> + true + ; + delete_file(BDDFile), + delete_file(BDDParFile), + delete_file(ResultFile), + delete_bdd_forest_files(N) + ) + ). + +read_probs(N,Probs) :- + (N = 0 -> + Probs = [] + ; + Probs = [Prob|Rest], + read(probability(Prob)), + N2 is N-1, + read_probs(N2,Rest) + ). + +delete_bdd_forest_files(N) :- + (N=0 -> + true + ; + bdd_forest_file(N,BDDFile), + delete_file(BDDFile,[]), + N2 is N-1, + delete_bdd_forest_files(N2) + ). + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% Build a trie using the default inference method +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + +build_trie(Goal, Trie) :- + (build_trie_supported -> + problog_flag(inference,Method), + once(build_trie(Method, Goal, Trie)) + ; + throw(error('Flag settings not supported by build_trie/2.')) + ). + +build_trie_supported :- problog_flag(inference,exact). +build_trie_supported :- problog_flag(inference,low(_)). +build_trie_supported :- problog_flag(inference,atleast-_-best). +build_trie_supported :- problog_flag(inference,_-best). + +build_trie(exact, Goal, Trie) :- + problog_control(on, exact), + build_trie(low(0), Goal, Trie), + problog_control(off, exact). + +build_trie(low(Threshold), Goal, _) :- + number(Threshold), + init_problog_low(Threshold), + problog_control(off, up), + statistics(walltime, _), + problog_call(Goal), + add_solution, + fail. +build_trie(low(Threshold), _, Trie) :- + number(Threshold), + statistics(walltime, [_,E]), + problog_var_set(sld_time, E), + nb_getval(problog_completed_proofs, Trie). + % don't clear tabling; tables can be reused by other query + +build_trie(atleast-K-best, Goal, Trie) :- + number(K), + problog_flag(first_threshold,InitT), + init_problog_kbest(InitT), + problog_control(off,up), + problog_kbest_id(Goal, K), + retract(current_kbest(_,ListFound,_NumFound)), + build_prefixtree(ListFound), + nb_getval(problog_completed_proofs, Trie), + clear_tabling. % clear tabling because tables cannot be reused by other query + + +build_trie(K-best, Goal, Trie) :- + number(K), + problog_flag(first_threshold,InitT), + init_problog_kbest(InitT), + problog_control(off,up), + problog_kbest_id(Goal, K), + retract(current_kbest(_,RawListFound,NumFound)), + % limiting the number of proofs is not only needed for fast SLD resolution but also for fast BDD building. + % one can't assume that kbest is called for the former and not for the latter + % thus, we take EXACTLY k proofs + take_k_best(RawListFound,K,NumFound,ListFound), + build_prefixtree(ListFound), + nb_getval(problog_completed_proofs, Trie), + clear_tabling. % clear tabling because tables cannot be reused by other query + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% Write BDD structure script for a trie and list all variables used +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + +write_bdd_struct_script(Trie,BDDFile,Variables) :- + % Check whether we use Hybrid ProbLog + ( + hybrid_proof(_,_,_) + -> + ( % Yes! run the disjoining stuff + retractall(hybrid_proof_disjoint(_,_,_,_)), + disjoin_hybrid_proofs, + + init_ptree(OriTrie), % use this as tmp ptree + %%%%%%%%%%%%%%%%%%%%% + ( % go over all stored proofs + enum_member_ptree(List,OriTrie1), + ( + List=[_|_] + -> + Proof=List; + Proof=[List] + ), + ( + select(continuous(ProofID),Proof,Rest) + -> + ( + % this proof is using continuous facts + all_hybrid_subproofs(ProofID,List2), + append(Rest,List2,NewProof), + insert_ptree(NewProof,OriTrie) + ); + insert_ptree(Proof,OriTrie) + ), + + fail; + true + ) + %%%%%%%%%%%%%%%%%%%%% + ) ; + % Nope, just pass on the Trie + OriTrie=OriTrie1 + ), + + ((problog_flag(variable_elimination, true), nb_getval(problog_nested_tries, false)) -> + statistics(walltime, _), + trie_check_for_and_cluster(OriTrie), + statistics(walltime, [_, VariableEliminationTime]), + trie_replace_and_cluster(OriTrie, Trie), + problog_var_set(variable_elimination_time, VariableEliminationTime), + variable_elimination_stats(Clusters, OrigPF, CompPF), + problog_var_set(variable_elimination_stats, compress(Clusters, OrigPF, CompPF)), + clean_up + ; + Trie = OriTrie + ), + (problog_flag(bdd_static_order, true) -> + get_order(Trie, Order), + problog_flag(static_order_file, SOFName), + convert_filename_to_working_path(SOFName, SOFileName), + generate_order_by_prob_fact_appearance(Order, SOFileName) + ; + true + ), + ptree:trie_stats(Memory, Tries, Entries, Nodes), + (nb_getval(problog_nested_tries, false) -> + ptree:trie_usage(Trie, TEntries, TNodes, TVirtualNodes), + problog_var_set(trie_statistics, tries(memory(Memory), tries(Tries), entries(TEntries), nodes(TNodes), virtualnodes(TVirtualNodes))) + ; + problog_var_set(trie_statistics, tries(memory(Memory), tries(Tries), entries(Entries), nodes(Nodes))) + ), + (problog_flag(triedump, true) -> + convert_filename_to_working_path(trie_file, TrieFile), + tell(TrieFile), + print_nested_ptree(Trie), + flush_output, + told, + tell(user_output) + ; + true + ), + nb_getval(problog_completed_proofs, Trie_Completed_Proofs), + ((Trie = Trie_Completed_Proofs, problog_flag(save_bdd, true)) -> + problog_control(on, remember) + ; + problog_control(off, remember) + ), + % old reduction method doesn't support nested tries + ((problog_flag(use_old_trie, true), nb_getval(problog_nested_tries, false)) -> + statistics(walltime, _), + (problog_control(check, remember) -> + bdd_struct_ptree_map(Trie, BDDFile, Variables, Mapping), + convert_filename_to_working_path(save_map, MapFile), + tell(MapFile), + format('mapping(~q).~n', [Mapping]), + flush_output, + told + ; + bdd_struct_ptree(Trie, BDDFile, Variables) + ), + statistics(walltime, [_, ScriptGenerationTime]), + problog_var_set(bdd_script_time, ScriptGenerationTime) + % omitted call to execute_bdd_tool + ; + true + ), + % naive method with nested trie support but not loops + ((problog_flag(use_naive_trie, true); (problog_flag(use_old_trie, true), nb_getval(problog_nested_tries, true))) -> + statistics(walltime, _), + atomic_concat([BDDFile, '_naive'], BDDFile_naive), + nested_ptree_to_BDD_struct_script(Trie, BDDFile_naive, Variables), + statistics(walltime, [_, ScriptGenerationTime_naive]), + problog_var_set(bdd_script_time(naive), ScriptGenerationTime_naive) + % omitted call to execute_bdd_tool + ; + true + ), + % reduction method with depth_breadth trie support + problog_flag(db_trie_opt_lvl, ROptLevel), + problog_flag(db_min_prefix, MinPrefix), + + (problog_flag(compare_opt_lvl, true) -> + generate_ints(0, ROptLevel, Levels) + ; + Levels = [ROptLevel] + ), + % Removed forall here, because it hides 'Variables' from what comes afterwards + memberchk(OptLevel, Levels), + ( + (problog_flag(use_db_trie, true) -> + tries:trie_db_opt_min_prefix(MinPrefix), + statistics(walltime, _), + (nb_getval(problog_nested_tries, false) -> + trie_to_bdd_struct_trie(Trie, DBTrie, BDDFile, OptLevel, Variables) + ; + nested_trie_to_bdd_struct_trie(Trie, DBTrie, BDDFile, OptLevel, Variables) + ), + atomic_concat(['builtin_', OptLevel], Builtin), + ptree:trie_stats(DBMemory, DBTries, DBEntries, DBNodes), + FM is DBMemory - Memory, + FT is DBTries - Tries, + FE is DBEntries - Entries, + FN is DBNodes - Nodes, + problog_var_set(dbtrie_statistics(Builtin), tries(memory(FM), tries(FT), entries(FE), nodes(FN))), + + delete_ptree(DBTrie), + statistics(walltime, [_, ScriptGenerationTime_builtin]), + problog_var_set(bdd_script_time(Builtin), ScriptGenerationTime_builtin) + % omitted call to execute_bdd_tool + ; + true + ) + ), + + % decomposition method + (problog_flag(use_dec_trie, true) -> + statistics(walltime, _), + atomic_concat([BDDFile, '_dec'], BDDFile_dec), + ptree_decomposition_struct(Trie, BDDFile_dec, Variables), + statistics(walltime, [_, ScriptGenerationTime_dec]), + problog_var_set(bdd_script_time(dec), ScriptGenerationTime_dec) + % omitted call to execute_bdd_tool + ; + true + ), + + (Trie =\= OriTrie -> + delete_ptree(Trie) + ; + true + ), + (var(Variables) -> throw(error('novars')) ; true). + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% Building a forest of BDDs +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + +problog_bdd_forest(Goals) :- + (problog_bdd_forest_supported -> + require(keep_ground_ids), + once(write_bdd_forest(Goals,[],Vars,1)), + unrequire(keep_ground_ids), + reset_non_ground_facts, + bdd_par_file(BDDParFile), + tell(BDDParFile), + bdd_vars_script(Vars), + flush_output, % isnt this called by told/0? + told, + length(Goals,L), + length(Vars,NbVars), + write_global_bdd_file(NbVars,L), + (problog_flag(retain_tables, true) -> retain_tabling; true), + clear_tabling + ; + throw(error('Flag settings not supported by problog_bdd_forest/1.')) + ). + +problog_bdd_forest_supported :- build_trie_supported. + +% Iterate over all Goals, write BDD scripts and collect variables used. +write_bdd_forest([],VarsTot,VarsTot,_). +write_bdd_forest([Goal|Rest],VarsAcc,VarsTot,N):- + build_trie(Goal, Trie), + write_nth_bdd_struct_script(N, Trie, Vars), + (problog_flag(verbose, true)-> + problog_statistics + ; + true + ), + delete_ptree(Trie), + N2 is N+1, + list_to_ord_set(Vars,VarsSet), + ord_union(VarsAcc,VarsSet,VarsAcc2), + once(write_bdd_forest(Rest,VarsAcc2,VarsTot,N2)). + +% Write files +write_nth_bdd_struct_script(N,Trie,Vars) :- + bdd_forest_file(N,BDDFile), + write_bdd_struct_script(Trie,BDDFile,Vars). + +write_global_bdd_file(NbVars,L) :- + bdd_file(BDDFile), + open(BDDFile,'write',BDDFileStream), + tell(BDDFileStream), + format('@BDD2~n~w~n~w~n~w~n',[NbVars,0,L]), + write_global_bdd_file_line(1,L), + write_global_bdd_file_query(1,L), + flush_output, + told. + +write_global_bdd_file_line(I,Max) :- + (I>Max -> + true + ; + bdd_forest_file(I,BDDFile), + format("L~q = <~w>~n",[I,BDDFile]), + I2 is I+1, + write_global_bdd_file_line(I2,Max) + ). + +write_global_bdd_file_query(I,Max) :- + (I=Max -> + format("L~q~n",[I]) + ; + format("L~q,",[I]), + I2 is I+1, + write_global_bdd_file_query(I2,Max) + ). + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% Filename specifications +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + +bdd_forest_file(N,BDDFile) :- + problog_flag(bdd_file,BDDFileFlag), + atomic_concat([BDDFileFlag,'_',N],BDDFileFlagWithN), + convert_filename_to_working_path(BDDFileFlagWithN, BDDFile). + +bdd_files(BDDFile,BDDParFile) :- + bdd_file(BDDFile), + bdd_par_file(BDDParFile). + +bdd_file(BDDFile) :- + problog_flag(bdd_file, BDDFileFlag), + convert_filename_to_working_path(BDDFileFlag, BDDFile). + +bdd_par_file(BDDParFile) :- + problog_flag(bdd_par_file, BDDParFileFlag), + convert_filename_to_working_path(BDDParFileFlag, BDDParFile). + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% Persistent Ground IDs +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + +require(Feature) :- + atom(Feature), + atomic_concat(['problog_required_',Feature],Feature_Required), + atomic_concat([Feature_Required,'_',depth],Feature_Depth), + (required(Feature) -> + b_getval(Feature_Depth,Depth), + Depth1 is Depth+1, + b_setval(Feature_Depth,Depth1) + ; + b_setval(Feature_Required,required), + b_setval(Feature_Depth,1) + %,format("starting to require ~q~n",[Feature]) + ). + +unrequire(Feature) :- + atom(Feature), + atomic_concat(['problog_required_',Feature],Feature_Required), + atomic_concat([Feature_Required,'_',depth],Feature_Depth), + b_getval(Feature_Depth,Depth), + (Depth=1 -> + nb_delete(Feature_Required), + nb_delete(Feature_Depth) + %,format("stopped keeping ground id's~n",[]) + ; + Depth1 is Depth-1, + b_setval(Feature_Depth,Depth1) + ). + +required(Feature) :- + atom(Feature), + atomic_concat(['problog_required_',Feature],Feature_Required), + catch(b_getval(Feature_Required,Val),error(existence_error(variable,Feature_Required),_),fail), + Val == required. + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% Should go to dtproblog.yap +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +signal_decision(ClauseID,GroundID) :- + (decision_fact(ClauseID,_) -> + bb_get(decisions,S), + ord_insert(S, GroundID, S2), + bb_put(decisions,S2) + ; + true + ). + + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% Term Expansion for user predicates +% Must come after clauses for '::'/2 and term_expansion_intern/3 +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + +user:term_expansion(Term,ExpandedTerm) :- + Term \== end_of_file, + prolog_load_context(module,Mod), + problog:term_expansion_intern(Term,Mod,ExpandedTerm). + diff --git a/packages/ProbLog/problog_examples/problog_learning.yap b/packages/ProbLog/problog_examples/problog_learning.yap new file mode 100644 index 000000000..b8c2ff1f0 --- /dev/null +++ b/packages/ProbLog/problog_examples/problog_learning.yap @@ -0,0 +1,1677 @@ +%%% -*- Mode: Prolog; -*- + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% +% $Date: 2010-10-05 16:52:13 +0200 (Tue, 05 Oct 2010) $ +% $Revision: 4869 $ +% +% This file is part of ProbLog +% http://dtai.cs.kuleuven.be/problog +% +% ProbLog was developed at Katholieke Universiteit Leuven +% +% Copyright 2008, 2009, 2010 +% Katholieke Universiteit Leuven +% +% Main authors of this file: +% Bernd Gutmann +% +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% +% Artistic License 2.0 +% +% Copyright (c) 2000-2006, The Perl Foundation. +% +% Everyone is permitted to copy and distribute verbatim copies of this +% license document, but changing it is not allowed. Preamble +% +% This license establishes the terms under which a given free software +% Package may be copied, modified, distributed, and/or +% redistributed. The intent is that the Copyright Holder maintains some +% artistic control over the development of that Package while still +% keeping the Package available as open source and free software. +% +% You are always permitted to make arrangements wholly outside of this +% license directly with the Copyright Holder of a given Package. If the +% terms of this license do not permit the full use that you propose to +% make of the Package, you should contact the Copyright Holder and seek +% a different licensing arrangement. Definitions +% +% "Copyright Holder" means the individual(s) or organization(s) named in +% the copyright notice for the entire Package. +% +% "Contributor" means any party that has contributed code or other +% material to the Package, in accordance with the Copyright Holder's +% procedures. +% +% "You" and "your" means any person who would like to copy, distribute, +% or modify the Package. +% +% "Package" means the collection of files distributed by the Copyright +% Holder, and derivatives of that collection and/or of those files. A +% given Package may consist of either the Standard Version, or a +% Modified Version. +% +% "Distribute" means providing a copy of the Package or making it +% accessible to anyone else, or in the case of a company or +% organization, to others outside of your company or organization. +% +% "Distributor Fee" means any fee that you charge for Distributing this +% Package or providing support for this Package to another party. It +% does not mean licensing fees. +% +% "Standard Version" refers to the Package if it has not been modified, +% or has been modified only in ways explicitly requested by the +% Copyright Holder. +% +% "Modified Version" means the Package, if it has been changed, and such +% changes were not explicitly requested by the Copyright Holder. +% +% "Original License" means this Artistic License as Distributed with the +% Standard Version of the Package, in its current version or as it may +% be modified by The Perl Foundation in the future. +% +% "Source" form means the source code, documentation source, and +% configuration files for the Package. +% +% "Compiled" form means the compiled bytecode, object code, binary, or +% any other form resulting from mechanical transformation or translation +% of the Source form. +% +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% +% Permission for Use and Modification Without Distribution +% +% (1) You are permitted to use the Standard Version and create and use +% Modified Versions for any purpose without restriction, provided that +% you do not Distribute the Modified Version. +% +% Permissions for Redistribution of the Standard Version +% +% (2) You may Distribute verbatim copies of the Source form of the +% Standard Version of this Package in any medium without restriction, +% either gratis or for a Distributor Fee, provided that you duplicate +% all of the original copyright notices and associated disclaimers. At +% your discretion, such verbatim copies may or may not include a +% Compiled form of the Package. +% +% (3) You may apply any bug fixes, portability changes, and other +% modifications made available from the Copyright Holder. The resulting +% Package will still be considered the Standard Version, and as such +% will be subject to the Original License. +% +% Distribution of Modified Versions of the Package as Source +% +% (4) You may Distribute your Modified Version as Source (either gratis +% or for a Distributor Fee, and with or without a Compiled form of the +% Modified Version) provided that you clearly document how it differs +% from the Standard Version, including, but not limited to, documenting +% any non-standard features, executables, or modules, and provided that +% you do at least ONE of the following: +% +% (a) make the Modified Version available to the Copyright Holder of the +% Standard Version, under the Original License, so that the Copyright +% Holder may include your modifications in the Standard Version. (b) +% ensure that installation of your Modified Version does not prevent the +% user installing or running the Standard Version. In addition, the +% modified Version must bear a name that is different from the name of +% the Standard Version. (c) allow anyone who receives a copy of the +% Modified Version to make the Source form of the Modified Version +% available to others under (i) the Original License or (ii) a license +% that permits the licensee to freely copy, modify and redistribute the +% Modified Version using the same licensing terms that apply to the copy +% that the licensee received, and requires that the Source form of the +% Modified Version, and of any works derived from it, be made freely +% available in that license fees are prohibited but Distributor Fees are +% allowed. +% +% Distribution of Compiled Forms of the Standard Version or +% Modified Versions without the Source +% +% (5) You may Distribute Compiled forms of the Standard Version without +% the Source, provided that you include complete instructions on how to +% get the Source of the Standard Version. Such instructions must be +% valid at the time of your distribution. If these instructions, at any +% time while you are carrying out such distribution, become invalid, you +% must provide new instructions on demand or cease further +% distribution. If you provide valid instructions or cease distribution +% within thirty days after you become aware that the instructions are +% invalid, then you do not forfeit any of your rights under this +% license. +% +% (6) You may Distribute a Modified Version in Compiled form without the +% Source, provided that you comply with Section 4 with respect to the +% Source of the Modified Version. +% +% Aggregating or Linking the Package +% +% (7) You may aggregate the Package (either the Standard Version or +% Modified Version) with other packages and Distribute the resulting +% aggregation provided that you do not charge a licensing fee for the +% Package. Distributor Fees are permitted, and licensing fees for other +% components in the aggregation are permitted. The terms of this license +% apply to the use and Distribution of the Standard or Modified Versions +% as included in the aggregation. +% +% (8) You are permitted to link Modified and Standard Versions with +% other works, to embed the Package in a larger work of your own, or to +% build stand-alone binary or bytecode versions of applications that +% include the Package, and Distribute the result without restriction, +% provided the result does not expose a direct interface to the Package. +% +% Items That are Not Considered Part of a Modified Version +% +% (9) Works (including, but not limited to, modules and scripts) that +% merely extend or make use of the Package, do not, by themselves, cause +% the Package to be a Modified Version. In addition, such works are not +% considered parts of the Package itself, and are not subject to the +% terms of this license. +% +% General Provisions +% +% (10) Any use, modification, and distribution of the Standard or +% Modified Versions is governed by this Artistic License. By using, +% modifying or distributing the Package, you accept this license. Do not +% use, modify, or distribute the Package, if you do not accept this +% license. +% +% (11) If your Modified Version has been derived from a Modified Version +% made by someone other than you, you are nevertheless required to +% ensure that your Modified Version complies with the requirements of +% this license. +% +% (12) This license does not grant you the right to use any trademark, +% service mark, tradename, or logo of the Copyright Holder. +% +% (13) This license includes the non-exclusive, worldwide, +% free-of-charge patent license to make, have made, use, offer to sell, +% sell, import and otherwise transfer the Package with respect to any +% patent claims licensable by the Copyright Holder that are necessarily +% infringed by the Package. If you institute patent litigation +% (including a cross-claim or counterclaim) against any party alleging +% that the Package constitutes direct or contributory patent +% infringement, then this Artistic License to you shall terminate on the +% date that such litigation is filed. +% +% (14) Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT +% HOLDER AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED +% WARRANTIES. THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A +% PARTICULAR PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT +% PERMITTED BY YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT +% HOLDER OR CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, +% INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE +% OF THE PACKAGE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +% +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + + +:- module(learning,[do_learning/1, + do_learning/2, + reset_learning/0 + ]). + +% switch on all the checks to reduce bug searching time +:- style_check(all). +:- yap_flag(unknown,error). + +% load modules from the YAP library +:- use_module(library(lists), [max_list/2, min_list/2, sum_list/2]). +:- use_module(library(system), [delete_file/1, file_exists/1, shell/2]). + +% load our own modules +:- use_module(problog). +:- use_module('problog/logger'). +:- use_module('problog/flags'). +:- use_module('problog/os'). +:- use_module('problog/print_learning'). +:- use_module('problog/utils_learning'). + +% used to indicate the state of the system +:- dynamic(values_correct/0). +:- dynamic(learning_initialized/0). +:- dynamic(current_iteration/1). +:- dynamic(example_count/1). +:- dynamic(query_probability_intern/2). +:- dynamic(query_gradient_intern/4). +:- dynamic(last_mse/1). +:- dynamic(query_is_similar/2). +:- dynamic(query_md5/2). + + +% used to identify queries which have identical proofs +:- dynamic(query_is_similar/2). +:- dynamic(query_md5/3). + +:- multifile(user:example/4). +user:example(A,B,C,=) :- + current_predicate(user:example/3), + user:example(A,B,C). + +:- multifile(user:test_example/4). +user:test_example(A,B,C,=) :- + current_predicate(user:test_example/3), + user:test_example(A,B,C). + + +%======================================================================== +%= store the facts with the learned probabilities to a file +%= if F is a variable, a filename based on the current iteration is used +%= +%======================================================================== + +save_model:- + current_iteration(Iteration), + atomic_concat(['factprobs_',Iteration,'.pl'],Filename), + problog_flag(output_directory,Dir), + concat_path_with_filename(Dir,Filename,Filename2), + export_facts(Filename2). + + +%======================================================================== +%= store the current succes probabilities for training and test examples +%= +%======================================================================== + +save_predictions:- + current_iteration(Iteration), + atomic_concat(['predictions_',Iteration,'.pl'],Filename), + problog_flag(output_directory,Dir), + concat_path_with_filename(Dir,Filename,Filename2), + + open(Filename2,'append',Handle), + format(Handle,"%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n",[]), + format(Handle,"% Iteration, train/test, QueryID, Query, GroundTruth, Prediction %\n",[]), + format(Handle,"%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n",[]), + !, + + %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + % start save prediction test examples + %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + ( % go over all test examples + current_predicate(user:test_example/4), + user:test_example(Query_ID,Query,TrueQueryProb,_), + query_probability(Query_ID,LearnedQueryProb), + + format(Handle,'ex(~q,test,~q,~q,~10f,~10f).\n', + [Iteration,Query_ID,Query,TrueQueryProb,LearnedQueryProb]), + + fail; % go to next test example + true + ), + !, + %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + % stop save prediction test examples + %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + + %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + % start save prediction training examples + %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + ( % go over all training examples + current_predicate(user:example/4), + user:example(Query_ID,Query,TrueQueryProb,_), + query_probability(Query_ID,LearnedQueryProb), + + format(Handle,'ex(~q,train,~q,~q,~10f,~10f).\n', + [Iteration,Query_ID,Query,TrueQueryProb,LearnedQueryProb]), + + fail; % go to next training example + true + ), + !, + %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + % stop save prediction training examples + %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + + format(Handle,'~3n',[]), + close(Handle). + + + +%======================================================================== +%= find out whether some example IDs are used more than once +%= if so, complain and stop +%= +%======================================================================== + +check_examples :- + %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + % Check example IDs + %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + ( + (current_predicate(user:example/4),user:example(ID,_,_,_), \+ atomic(ID)) + -> + ( + format(user_error,'The example id of training example ~q ',[ID]), + format(user_error,'is not atomic (e.g foo42, 23, bar, ...).~n',[]), + throw(error(examples)) + ); true + ), + + ( + (current_predicate(user:test_example/4),user:test_example(ID,_,_,_), \+ atomic(ID)) + -> + ( + format(user_error,'The example id of test example ~q ',[ID]), + format(user_error,'is not atomic (e.g foo42, 23, bar, ...).~n',[]), + throw(error(examples)) + ); true + ), + + %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + % Check example probabilities + %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + ( + (current_predicate(user:example/4),user:example(ID,_,P,_), (\+ number(P); P>1 ; P<0)) + -> + ( + format(user_error,'The training example ~q does not have a valid probability value (~q).~n',[ID,P]), + throw(error(examples)) + ); true + ), + + ( + (current_predicate(user:test_example/4),user:test_example(ID,_,P,_), (\+ number(P); P>1 ; P<0)) + -> + ( + format(user_error,'The test example ~q does not have a valid probability value (~q).~n',[ID,P]), + throw(error(examples)) + ); true + ), + + + %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + % Check that no example ID is repeated, + % and if it is repeated make sure the query is the same + %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + ( + ( + ( + current_predicate(user:example/4), + user:example(ID,QueryA,_,_), + user:example(ID,QueryB,_,_), + QueryA \= QueryB + ) ; + + ( + current_predicate(user:test_example/4), + user:test_example(ID,QueryA,_,_), + user:test_example(ID,QueryB,_,_), + QueryA \= QueryB + ); + + ( + current_predicate(user:example/4), + current_predicate(user:test_example/4), + user:example(ID,QueryA,_,_), + user:test_example(ID,QueryB,_,_), + QueryA \= QueryB + ) + ) + -> + ( + format(user_error,'The example id ~q is used several times.~n',[ID]), + throw(error(examples)) + ); true + ). +%======================================================================== +%= +%======================================================================== + +reset_learning :- + retractall(current_iteration(_)), + retractall(learning_initialized), + + retractall(values_correct), + retractall(current_iteration(_)), + retractall(example_count(_)), + retractall(query_probability_intern(_,_)), + retractall(query_gradient_intern(_,_,_)), + retractall(last_mse(_)), + retractall(query_is_similar(_,_)), + retractall(query_md5(_,_,_)), + + set_problog_flag(alpha,auto), + set_problog_flag(learning_rate,examples), + logger_reset_all_variables. + + + +%======================================================================== +%= initialize everything and perform Iterations times gradient descent +%= can be called several times +%= if it is called with an epsilon parameter, it stops when the change +%= in the MSE is smaller than epsilon +%======================================================================== + +do_learning(Iterations) :- + do_learning(Iterations,-1). + +do_learning(Iterations,Epsilon) :- + current_predicate(user:example/4), + !, + integer(Iterations), + number(Epsilon), + Iterations>0, + do_learning_intern(Iterations,Epsilon). +do_learning(_,_) :- + format(user_error,'~n~Error: No training examples specified.~n~n',[]). + + +do_learning_intern(0,_) :- + !. +do_learning_intern(Iterations,Epsilon) :- + Iterations>0, + + init_learning, + current_iteration(CurrentIteration), + retractall(current_iteration(_)), + NextIteration is CurrentIteration+1, + assertz(current_iteration(NextIteration)), + EndIteration is CurrentIteration+Iterations-1, + + format_learning(1,'~nIteration ~d of ~d~n',[CurrentIteration,EndIteration]), + logger_set_variable(iteration,CurrentIteration), + + logger_start_timer(duration), + + mse_testset, + ground_truth_difference, + gradient_descent, + + problog_flag(log_frequency,Log_Frequency), + + ( + ( Log_Frequency>0, 0 =:= CurrentIteration mod Log_Frequency) + -> + ( + once(save_predictions), + once(save_model) + ); + true + ), + + update_values, + + ( + last_mse(Last_MSE) + -> + ( + retractall(last_mse(_)), + logger_get_variable(mse_trainingset,Current_MSE), + assertz(last_mse(Current_MSE)), + !, + MSE_Diff is abs(Last_MSE-Current_MSE) + ); ( + logger_get_variable(mse_trainingset,Current_MSE), + assertz(last_mse(Current_MSE)), + MSE_Diff is Epsilon+1 + ) + ), + + ( + (problog_flag(rebuild_bdds,BDDFreq),BDDFreq>0,0 =:= CurrentIteration mod BDDFreq) + -> + ( + retractall(values_correct), + retractall(query_is_similar(_,_)), + retractall(query_md5(_,_,_)), + empty_bdd_directory, + init_queries + ); true + ), + + + !, + logger_stop_timer(duration), + + + logger_write_data, + + + + RemainingIterations is Iterations-1, + + ( + MSE_Diff>Epsilon + -> + do_learning_intern(RemainingIterations,Epsilon); + true + ). + + +%======================================================================== +%= find proofs and build bdds for all training and test examples +%= +%= +%======================================================================== +init_learning :- + learning_initialized, + !. +init_learning :- + check_examples, + + logger_write_header, + + format_learning(1,'Initializing everything~n',[]), + empty_output_directory, + + %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + % Delete the BDDs from the previous run if they should + % not be reused + %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + ( + ( + problog_flag(reuse_initialized_bdds,true), + problog_flag(rebuild_bdds,0) + ) + -> + true; + empty_bdd_directory + ), + + %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + % Check, if continuous facts are used. + % if yes, switch to problog_exact + %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + problog_flag(init_method,(_,_,_,_,OldCall)), + ( + ( + continuous_fact(_), + OldCall\=problog_exact_save(_,_,_,_,_) + ) + -> + ( + format('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))) + ); + true + ), + + + %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + % start count test examples + %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + bb_put(test_examples,0), + ( % go over all test examples + current_predicate(user:test_example/4), + user:test_example(_,_,_,_), + bb_get(test_examples, OldCounter), + NewCounter is OldCounter+1, + bb_put(test_examples,NewCounter), + + fail; % go to next text example + true + ), + bb_delete(test_examples,TestExampleCount), + format_learning(3,'~q test examples~n',[TestExampleCount]), + !, + %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + % stop count test examples + %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + + %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + % start count training examples + %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + bb_put(training_examples,0), + ( % go over all training examples + current_predicate(user:example/4), + user:example(_,_,_,_), + bb_get(training_examples, OldCounter), + NewCounter is OldCounter+1, + bb_put(training_examples,NewCounter), + + fail; %go to next training example + true + ), + bb_delete(training_examples,TrainingExampleCount), + assertz(example_count(TrainingExampleCount)), + format_learning(3,'~q training examples~n',[TrainingExampleCount]), + !, + %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + % stop count training examples + %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + + + %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + % set learning rate and alpha + %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + ( + problog_flag(learning_rate,examples) + -> + set_problog_flag(learning_rate,TrainingExampleCount); + true + ), + + ( + problog_flag(alpha,auto) + -> + auto_alpha; + true + ), + + %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + % build BDD script for every example + %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + once(init_queries), + + %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + % done + %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + assertz(current_iteration(0)), + assertz(learning_initialized), + + format_learning(1,'~n',[]). + + + +%======================================================================== +%= This predicate goes over all training and test examples, +%= calls the inference method of ProbLog and stores the resulting +%= BDDs +%======================================================================== + + +init_queries :- + format_learning(2,'Build BDDs for examples~n',[]), + ( % go over all test examples + current_predicate(user:test_example/4), + user:test_example(ID,Query,Prob,_), + format_learning(3,' test example ~q: ~q~n',[ID,Query]), + flush_output(user), + init_one_query(ID,Query,test), + + fail; % go to next test example + true + ), + ( % go over all training examples + current_predicate(user:example/4), + user:example(ID,Query,Prob,_), + format_learning(3,' training example ~q: ~q~n',[ID,Query]), + flush_output(user), + init_one_query(ID,Query,training), + + fail; %go to next training example + true + ). + + +bdd_input_file(Filename) :- + problog_flag(output_directory,Dir), + concat_path_with_filename(Dir,'input.txt',Filename). + +init_one_query(QueryID,Query,Type) :- + bdd_input_file(Probabilities_File), + problog_flag(bdd_directory,Query_Directory), + + atomic_concat(['query_',QueryID],Filename1), + concat_path_with_filename(Query_Directory,Filename1,Filename), + + %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + % if BDD file does not exist, call ProbLog + %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + ( + file_exists(Filename) + -> + format_learning(3,' Reuse existing BDD ~q~n~n',[Filename]); + ( + problog_flag(init_method,(Query,_Prob,Filename,Probabilities_File,Call)), + once(Call), + delete_file(Probabilities_File) + ) + ), + + %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + % check wether this BDD is similar to another BDD + %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + ( + problog_flag(check_duplicate_bdds,true) + -> + ( + calc_md5(Filename,Query_MD5), + ( + query_md5(OtherQueryID,Query_MD5,Type) + -> + ( + assertz(query_is_similar(QueryID,OtherQueryID)), + format_learning(3, '~q is similar to ~q~2n', [QueryID,OtherQueryID]) + ); + assertz(query_md5(QueryID,Query_MD5,Type)) + ) + ); + + true + ),!, + garbage_collect. + + + + +%======================================================================== +%= updates all values of query_probability/2 and query_gradient/4 +%= should be called always before these predicates are accessed +%= if the old values are still valid, nothing happens +%======================================================================== + +update_values :- + values_correct, + !. +update_values :- + \+ values_correct, + + %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + % delete old values + %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + retractall(query_probability_intern(_,_)), + retractall(query_gradient_intern(_,_,_,_)), + + %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + % start write current probabilities to file + %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + bdd_input_file(Probabilities_File), + delete_file_silent(Probabilities_File), + + open(Probabilities_File,'write',Handle), + + ( % go over all probabilistic facts + get_fact_probability(ID,Prob), + inv_sigmoid(Prob,Value), + ( + non_ground_fact(ID) + -> + format(Handle,'@x~q_*~n~10f~n',[ID,Value]); + format(Handle,'@x~q~n~10f~n',[ID,Value]) + ), + + fail; % go to next probabilistic fact + true + ), + + ( % go over all continuous facts + get_continuous_fact_parameters(ID,gaussian(Mu,Sigma)), + format(Handle,'@x~q_*~n0~n0~n~10f;~10f~n',[ID,Mu,Sigma]), + + fail; % go to next continuous fact + true + ), + + close(Handle), + !, + %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + % stop write current probabilities to file + %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + + assertz(values_correct). + + + +%======================================================================== +%= +%= +%= +%======================================================================== + +update_query_cleanup(QueryID) :- + ( + (query_is_similar(QueryID,_) ; query_is_similar(_,QueryID)) + -> + % either this query is similar to another or vice versa, + % therefore we don't delete anything + true; + retractall(query_gradient_intern(QueryID,_,_,_)) + ). + + +update_query(QueryID,Symbol,What_To_Update) :- + % fixme OS trouble + problog_flag(output_directory,Output_Directory), + problog_flag(bdd_directory,Query_Directory), + bdd_input_file(Probabilities_File), + ( + query_is_similar(QueryID,_) + -> + % we don't have to evaluate the BDD + format_learning(4,'#',[]); + ( + problog_flag(sigmoid_slope,Slope), + problog_dir(PD), + ((What_To_Update=all;query_is_similar(_,QueryID)) -> Method='g' ; Method='l'), + atomic_concat([PD, + '/problogbdd', + ' -i "', Probabilities_File, '"', + ' -l "', Query_Directory,'/query_',QueryID, '"', + ' -m ', Method, + ' -id ', QueryID, + ' -sl ', Slope, + ' > "', + Output_Directory, + 'values.pl"'],Command), + shell(Command,Error), + + + ( + Error = 2 + -> + throw(error('SimpleCUDD has been interrupted.')); + true + ), + ( + Error \= 0 + -> + ( + format(user_error,'SimpleCUDD stopped with error code ~q, command was ~q~n',[Error, shell(Command,Error)]), + throw(bdd_error(QueryID,Error))); + true + ), + atomic_concat([Output_Directory,'values.pl'],Values_Filename), + ( + file_exists(Values_Filename) + -> + ( + ( + once(my_load(Values_Filename,QueryID)) + -> + true; + ( + format(user_error,'ERROR: Tried to read the file ~q but my_load/1 fails.~n~q.~2n',[Values_Filename,update_query(QueryID,Symbol,What_To_Update)]), + throw(error(my_load_fails)) + ) + ); + ( + format(user_error,'ERROR: Tried to read the file ~q but it does not exist.~n~q.~2n',[Values_Filename,update_query(QueryID,Symbol,What_To_Update)]), + throw(error(output_file_does_not_exist)) + ) + ) + ), + + delete_file(Values_Filename), + format_learning(4,'~w',[Symbol]) + ) + ), + flush_output(user). + + +%======================================================================== +%= This predicate reads probability and gradient values from the file +%= the gradient ID is a mere check to uncover hidden bugs +%= +Filename +QueryID -QueryProbability +%======================================================================== + +my_load(File,QueryID) :- + open(File,'read',Handle), + read(Handle,Atom), + once(my_load_intern(Atom,Handle,QueryID)), + close(Handle). +my_load(File,QueryID) :- + format(user_error,'Error at ~q.~2n',[my_load(File,QueryID)]), + throw(error(my_load(File,QueryID))). + +my_load_intern(end_of_file,_,_) :- + !. +my_load_intern(query_probability(QueryID,Prob),Handle,QueryID) :- + !, + assertz(query_probability_intern(QueryID,Prob)), + read(Handle,X), + my_load_intern(X,Handle,QueryID). +my_load_intern(query_gradient(QueryID,XFactID,Type,Value),Handle,QueryID) :- + !, + atomic_concat(x,StringFactID,XFactID), + atom_number(StringFactID,FactID), + assertz(query_gradient_intern(QueryID,FactID,Type,Value)), + read(Handle,X), + my_load_intern(X,Handle,QueryID). +my_load_intern(X,Handle,QueryID) :- + format(user_error,'Unknown atom ~q in results file.~n',[X]), + read(Handle,X2), + my_load_intern(X2,Handle,QueryID). + + + + +%======================================================================== +%= +%= +%= +%======================================================================== +query_probability(QueryID,Prob) :- + ( + query_probability_intern(QueryID,Prob) + -> + true; + ( + query_is_similar(QueryID,OtherQueryID), + query_probability_intern(OtherQueryID,Prob) + ) + ). +query_gradient(QueryID,Fact,Type,Value) :- + ( + query_gradient_intern(QueryID,Fact,Type,Value) + -> + true; + ( + query_is_similar(QueryID,OtherQueryID), + query_gradient_intern(OtherQueryID,Fact,Type,Value) + ) + ). + +%======================================================================== +%= +%= +%= +%======================================================================== + + + +% FIXME +ground_truth_difference :- + findall(Diff,(tunable_fact(FactID,GroundTruth), + \+continuous_fact(FactID), + \+ var(GroundTruth), + get_fact_probability(FactID,Prob), + Diff is abs(GroundTruth-Prob)),AllDiffs), + ( + AllDiffs=[] + -> + ( + MinDiff=0.0, + MaxDiff=0.0, + DiffMean=0.0 + ) ; + ( + length(AllDiffs,Len), + sum_list(AllDiffs,AllDiffsSum), + min_list(AllDiffs,MinDiff), + max_list(AllDiffs,MaxDiff), + DiffMean is AllDiffsSum/Len + ) + ), + + logger_set_variable(ground_truth_diff,DiffMean), + logger_set_variable(ground_truth_mindiff,MinDiff), + logger_set_variable(ground_truth_maxdiff,MaxDiff). + +%======================================================================== +%= Calculates the mse of training and test data +%= +%= -Float +%======================================================================== + +mse_trainingset_only_for_linesearch(MSE) :- + ( + current_predicate(user:example/4) + -> + ( + update_values, + findall(SquaredError, + (user:example(QueryID,_Query,QueryProb,Type), + once(update_query(QueryID,'.',probability)), + query_probability(QueryID,CurrentProb), + once(update_query_cleanup(QueryID)), + ( + (Type == '='; (Type == '<', CurrentProb>QueryProb); (Type=='>',CurrentProb + SquaredError is (CurrentProb-QueryProb)**2; + SquaredError = 0.0 + ) + ), + + AllSquaredErrors), + + length(AllSquaredErrors,Length), + sum_list(AllSquaredErrors,SumAllSquaredErrors), + MSE is SumAllSquaredErrors/Length, + format_learning(3,' (~8f)~n',[MSE]) + ); true + ), + retractall(values_correct). + +mse_testset :- + ( + (current_predicate(user:test_example/4),user:test_example(_,_,_,_)) + -> + ( + format_learning(2,'MSE_Test ',[]), + update_values, + findall(SquaredError, + (user:test_example(QueryID,_Query,QueryProb,Type), + once(update_query(QueryID,'+',probability)), + query_probability(QueryID,CurrentProb), + once(update_query_cleanup(QueryID)), + ( + (Type == '='; (Type == '<', CurrentProb>QueryProb); (Type=='>',CurrentProb + SquaredError is (CurrentProb-QueryProb)**2; + SquaredError = 0.0 + ) + ), + AllSquaredErrors), + + length(AllSquaredErrors,Length), + sum_list(AllSquaredErrors,SumAllSquaredErrors), + min_list(AllSquaredErrors,MinError), + max_list(AllSquaredErrors,MaxError), + MSE is SumAllSquaredErrors/Length, + + logger_set_variable(mse_testset,MSE), + logger_set_variable(mse_min_testset,MinError), + logger_set_variable(mse_max_testset,MaxError), + format_learning(2,' (~8f)~n',[MSE]) + ); true + ). + +%======================================================================== +%= Calculates the sigmoid function respectivly the inverse of it +%= warning: applying inv_sigmoid to 0.0 or 1.0 will yield +/-inf +%= +%= +Float, -Float +%======================================================================== + +sigmoid(T,Sig) :- + problog_flag(sigmoid_slope,Slope), + Sig is 1/(1+exp(-T*Slope)). + +inv_sigmoid(T,InvSig) :- + problog_flag(sigmoid_slope,Slope), + InvSig is -log(1/T-1)/Slope. + + + + + + +%======================================================================== +%= Perform one iteration of gradient descent +%= +%= assumes that everything is initialized, if the current values +%= of query_probability/2 and query_gradient/4 are not up to date +%= they will be recalculated +%= finally, the values_correct/0 is retracted to signal that the +%= probabilities of the examples have to be recalculated +%======================================================================== + +save_old_probabilities :- + ( % go over all tunable facts + tunable_fact(FactID,_), + + ( + continuous_fact(FactID) + -> + + ( + get_continuous_fact_parameters(FactID,gaussian(OldMu,OldSigma)), + atomic_concat(['old_mu_',FactID],Key), + atomic_concat(['old_sigma_',FactID],Key2), + bb_put(Key,OldMu), + bb_put(Key2,OldSigma) + ); + ( + get_fact_probability(FactID,OldProbability), + atomic_concat(['old_prob_',FactID],Key), + bb_put(Key,OldProbability) + ) + ), + + fail; % go to next tunable fact + true + ). + + + +forget_old_probabilities :- + ( % go over all tunable facts + tunable_fact(FactID,_), + ( + continuous_fact(FactID) + -> + ( + atomic_concat(['old_mu_',FactID],Key), + atomic_concat(['old_sigma_',FactID],Key2), + atomic_concat(['grad_mu_',FactID],Key3), + atomic_concat(['grad_sigma_',FactID],Key4), + bb_delete(Key,_), + bb_delete(Key2,_), + bb_delete(Key3,_), + bb_delete(Key4,_) + ); + ( + atomic_concat(['old_prob_',FactID],Key), + atomic_concat(['grad_',FactID],Key2), + bb_delete(Key,_), + bb_delete(Key2,_) + ) + ), + + fail; % go to next tunable fact + true + ). + +add_gradient(Learning_Rate) :- + ( % go over all tunable facts + tunable_fact(FactID,_), + ( + continuous_fact(FactID) + -> + ( + atomic_concat(['old_mu_',FactID],Key), + atomic_concat(['old_sigma_',FactID],Key2), + atomic_concat(['grad_mu_',FactID],Key3), + atomic_concat(['grad_sigma_',FactID],Key4), + + bb_get(Key,Old_Mu), + bb_get(Key2,Old_Sigma), + bb_get(Key3,Grad_Mu), + bb_get(Key4,Grad_Sigma), + + Mu is Old_Mu -Learning_Rate* Grad_Mu, + Sigma is exp(log(Old_Sigma) -Learning_Rate* Grad_Sigma), + + set_continuous_fact_parameters(FactID,gaussian(Mu,Sigma)) + ); + ( + atomic_concat(['old_prob_',FactID],Key), + atomic_concat(['grad_',FactID],Key2), + + bb_get(Key,OldProbability), + bb_get(Key2,GradValue), + + inv_sigmoid(OldProbability,OldValue), + NewValue is OldValue -Learning_Rate*GradValue, + sigmoid(NewValue,NewProbability), + + % Prevent "inf" by using values too close to 1.0 + Prob_Secure is min(0.999999999,max(0.000000001,NewProbability)), + set_fact_probability(FactID,Prob_Secure) + ) + ), + + fail; % go to next tunable fact + true + ), + retractall(values_correct). + + +gradient_descent :- + format_learning(2,'Gradient ',[]), + + save_old_probabilities, + update_values, + + %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + % start set gradient to zero + %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + ( % go over all tunable facts + + tunable_fact(FactID,_), + ( + continuous_fact(FactID) + -> + + ( + atomic_concat(['grad_mu_',FactID],Key), + atomic_concat(['grad_sigma_',FactID],Key2), + bb_put(Key,0.0), + bb_put(Key2,0.0) + ); + ( + atomic_concat(['grad_',FactID],Key), + bb_put(Key,0.0) + ) + ), + + fail; % go to next tunable fact + + true + ), + %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + % stop gradient to zero + %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + !, + + %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + % start calculate gradient + %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + bb_put(mse_train_sum, 0.0), + bb_put(mse_train_min, 0.0), + bb_put(mse_train_max, 0.0), + + problog_flag(alpha,Alpha), + logger_set_variable(alpha,Alpha), + example_count(Example_Count), + + ( % go over all training examples + current_predicate(user:example/4), + user:example(QueryID,_Query,QueryProb,Type), + once(update_query(QueryID,'.',all)), + query_probability(QueryID,BDDProb), + ( + QueryProb=:=0.0 + -> + Y2=Alpha; + Y2=1.0 + ), + ( + (Type == '='; (Type == '<', BDDProb>QueryProb); (Type=='>',BDDProb + Y is Y2*2/Example_Count * (BDDProb-QueryProb); + Y=0.0 + ), + + + % first do the calculations for the MSE on training set + ( + (Type == '='; (Type == '<', BDDProb>QueryProb); (Type=='>',BDDProb + Squared_Error is (BDDProb-QueryProb)**2; + Squared_Error=0.0 + ), + + bb_get(mse_train_sum,Old_MSE_Train_Sum), + bb_get(mse_train_min,Old_MSE_Train_Min), + bb_get(mse_train_max,Old_MSE_Train_Max), + New_MSE_Train_Sum is Old_MSE_Train_Sum+Squared_Error, + New_MSE_Train_Min is min(Old_MSE_Train_Min,Squared_Error), + New_MSE_Train_Max is max(Old_MSE_Train_Max,Squared_Error), + bb_put(mse_train_sum,New_MSE_Train_Sum), + bb_put(mse_train_min,New_MSE_Train_Min), + bb_put(mse_train_max,New_MSE_Train_Max), + + + + ( % go over all tunable facts + tunable_fact(FactID,_), + ( + continuous_fact(FactID) + -> + + ( + atomic_concat(['grad_mu_',FactID],Key), + atomic_concat(['grad_sigma_',FactID],Key2), + + % if the following query fails, + % it means, the fact is not used in the proof + % of QueryID, and the gradient is 0.0 and will + % not contribute to NewValue either way + % DON'T FORGET THIS IF YOU CHANGE SOMETHING HERE! + query_gradient(QueryID,FactID,mu,GradValueMu), + query_gradient(QueryID,FactID,sigma,GradValueSigma), + + bb_get(Key,OldValueMu), + bb_get(Key2,OldValueSigma), + + NewValueMu is OldValueMu + Y*GradValueMu, + NewValueSigma is OldValueSigma + Y*GradValueSigma, + + bb_put(Key,NewValueMu), + bb_put(Key2,NewValueSigma) + ); + ( + atomic_concat(['grad_',FactID],Key), + + % if the following query fails, + % it means, the fact is not used in the proof + % of QueryID, and the gradient is 0.0 and will + % not contribute to NewValue either way + % DON'T FORGET THIS IF YOU CHANGE SOMETHING HERE! + query_gradient(QueryID,FactID,p,GradValue), + + bb_get(Key,OldValue), + NewValue is OldValue + Y*GradValue, + bb_put(Key,NewValue) + ) + ), + + fail; % go to next fact + true + ), + + once(update_query_cleanup(QueryID)), + fail; % go to next training example + true + ), + %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + % stop calculate gradient + %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + !, + + %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + % start statistics on gradient + %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + findall(V, ( + tunable_fact(FactID,_), + atomic_concat(['grad_',FactID],Key), + bb_get(Key,V) + ),Gradient_Values), + + ( + Gradient_Values==[] + -> + ( + logger_set_variable(gradient_mean,0.0), + logger_set_variable(gradient_min,0.0), + logger_set_variable(gradient_max,0.0) + ); + ( + sum_list(Gradient_Values,GradSum), + max_list(Gradient_Values,GradMax), + min_list(Gradient_Values,GradMin), + length(Gradient_Values,GradLength), + GradMean is GradSum/GradLength, + + logger_set_variable(gradient_mean,GradMean), + logger_set_variable(gradient_min,GradMin), + logger_set_variable(gradient_max,GradMax) + ) + ), + %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + % stop statistics on gradient + %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + + bb_delete(mse_train_sum,MSE_Train_Sum), + bb_delete(mse_train_min,MSE_Train_Min), + bb_delete(mse_train_max,MSE_Train_Max), + MSE is MSE_Train_Sum/Example_Count, + + logger_set_variable(mse_trainingset,MSE), + logger_set_variable(mse_min_trainingset,MSE_Train_Min), + logger_set_variable(mse_max_trainingset,MSE_Train_Max), + + format_learning(2,'~n',[]), + + %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + % start add gradient to current probabilities + %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + ( + problog_flag(line_search,false) + -> + problog_flag(learning_rate,LearningRate); + lineSearch(LearningRate,_) + ), + format_learning(3,'learning rate:~8f~n',[LearningRate]), + add_gradient(LearningRate), + logger_set_variable(learning_rate,LearningRate), + %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + % stop add gradient to current probabilities + %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + !, + forget_old_probabilities. + +%======================================================================== +%= +%= +%======================================================================== + +line_search_evaluate_point(Learning_Rate,MSE) :- + add_gradient(Learning_Rate), + format_learning(2,'Line search (h=~8f) ',[Learning_Rate]), + mse_trainingset_only_for_linesearch(MSE). + + +lineSearch(Final_X,Final_Value) :- + + % Get Parameters for line search + problog_flag(line_search_tolerance,Tol), + problog_flag(line_search_tau,Tau), + problog_flag(line_search_interval,(A,B)), + + format_learning(3,'Line search in interval (~4f,~4f)~n',[A,B]), + + % init values + Acc is Tol * (B-A), + InitRight is A + Tau*(B-A), + InitLeft is A + B - InitRight, + + line_search_evaluate_point(A,Value_A), + line_search_evaluate_point(B,Value_B), + line_search_evaluate_point(InitRight,Value_InitRight), + line_search_evaluate_point(InitLeft,Value_InitLeft), + + bb_put(line_search_a,A), + bb_put(line_search_b,B), + bb_put(line_search_left,InitLeft), + bb_put(line_search_right,InitRight), + + bb_put(line_search_value_a,Value_A), + bb_put(line_search_value_b,Value_B), + bb_put(line_search_value_left,Value_InitLeft), + bb_put(line_search_value_right,Value_InitRight), + + bb_put(line_search_iteration,1), + + %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + %%%% BEGIN BACK TRACKING + %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + ( + repeat, + + bb_get(line_search_iteration,Iteration), + bb_get(line_search_a,Ak), + bb_get(line_search_b,Bk), + bb_get(line_search_left,Left), + bb_get(line_search_right,Right), + + bb_get(line_search_value_a,Fl), + bb_get(line_search_value_b,Fr), + bb_get(line_search_value_left,FLeft), + bb_get(line_search_value_right,FRight), + + ( + % check for infinity, if there is, go to the left + ( FLeft >= FRight, \+ FLeft = (+inf), \+ FRight = (+inf) ) + -> + ( + AkNew=Left, + FlNew=FLeft, + LeftNew=Right, + FLeftNew=FRight, + RightNew is AkNew + Bk - LeftNew, + line_search_evaluate_point(RightNew,FRightNew), + BkNew=Bk, + FrNew=Fr + ); + ( + BkNew=Right, + FrNew=FRight, + RightNew=Left, + FRightNew=FLeft, + LeftNew is Ak + BkNew - RightNew, + + line_search_evaluate_point(LeftNew,FLeftNew), + AkNew=Ak, + FlNew=Fl + ) + ), + + Next_Iteration is Iteration + 1, + + bb_put(line_search_iteration,Next_Iteration), + + bb_put(line_search_a,AkNew), + bb_put(line_search_b,BkNew), + bb_put(line_search_left,LeftNew), + bb_put(line_search_right,RightNew), + + bb_put(line_search_value_a,FlNew), + bb_put(line_search_value_b,FrNew), + bb_put(line_search_value_left,FLeftNew), + bb_put(line_search_value_right,FRightNew), + + % is the search interval smaller than the tolerance level? + BkNew-AkNew0, + !. +line_search_postcheck(V,X,V,X) :- + problog_flag(line_search_never_stop,false), + !. +line_search_postcheck(_,_, LLH, FinalPosition) :- + problog_flag(line_search_tolerance,Tolerance), + problog_flag(line_search_interval,(Left,Right)), + + + Offset is (Right - Left) * Tolerance, + + bb_put(line_search_offset,Offset), + + %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + ( + + repeat, + + bb_get(line_search_offset,OldOffset), + NewOffset is OldOffset * Tolerance, + bb_put(line_search_offset,NewOffset), + + Position is Left + NewOffset, + line_search_evaluate_point(Position,LLH), + bb_put(line_search_llh,LLH), + + write(logAtom(lineSearchPostCheck(Position,LLH))),nl, + + + \+ LLH = (+inf), + ! + ), % cut away choice point from repeat + %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + + bb_delete(line_search_llh,LLH), + bb_delete(line_search_offset,FinalOffset), + FinalPosition is Left + FinalOffset. + + + +my_5_min(V1,V2,V3,V4,V5,F1,F2,F3,F4,F5,VMin,FMin) :- + ( + V1 + (VTemp1=V1,FTemp1=F1); + (VTemp1=V2,FTemp1=F2) + ), + ( + V3 + (VTemp2=V3,FTemp2=F3); + (VTemp2=V4,FTemp2=F4) + ), + ( + VTemp1 + (VTemp3=VTemp1,FTemp3=FTemp1); + (VTemp3=VTemp2,FTemp3=FTemp2) + ), + ( + VTemp3 + (VMin=VTemp3,FMin=FTemp3); + (VMin=V5,FMin=F5) + ). + + +%======================================================================== +%= set the alpha parameter to the value +%= # positive training examples / # negative training examples +%= +%= training example is positive if P(e)=1 +%= training example is negative if P(e)=0 +%= +%= if there are training example with 00, + !, + set_problog_flag(alpha,1.0). +auto_alpha :- + findall(1,(user:example(_,_,P,=),P=:=1.0),Pos), + findall(0,(user:example(_,_,P,=),P=:=0.0),Neg), + length(Pos,NP), + length(Neg,NN), + Alpha is NP/NN, + set_problog_flag(alpha,Alpha). + + + +%======================================================================== +%= initialize the logger module and set the flags for learning +%= don't change anything here! use set_problog_flag/2 instead +%======================================================================== + +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' + 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), + problog_define_flag(rebuild_bdds, problog_flag_validate_nonegint, 'rebuild BDDs every nth iteration', 0, learning_general), + problog_define_flag(reuse_initialized_bdds,problog_flag_validate_boolean, 'Reuse BDDs from previous runs',false, learning_general), + problog_define_flag(check_duplicate_bdds,problog_flag_validate_boolean,'Store intermediate results in hash table',true,learning_general), + problog_define_flag(init_method,problog_flag_validate_dummy,'ProbLog predicate to search proofs',(Query,Probability,BDDFile,ProbFile,problog_kbest_save(Query,100,Probability,_Status,BDDFile,ProbFile)),learning_general,flags:learning_init_handler), + problog_define_flag(alpha,problog_flag_validate_number,'weight of negative examples (auto=n_p/n_n)',auto,learning_general,flags:auto_handler), + problog_define_flag(sigmoid_slope,problog_flag_validate_posnumber,'slope of sigmoid function',1.0,learning_general), + + problog_define_flag(learning_rate,problog_flag_validate_posnumber,'Default learning rate (If line_search=false)',examples,learning_line_search,flags:examples_handler), + problog_define_flag(line_search, problog_flag_validate_boolean,'estimate learning rate by line search',false,learning_line_search), + problog_define_flag(line_search_never_stop, problog_flag_validate_boolean,'make tiny step if line search returns 0',true,learning_line_search), + problog_define_flag(line_search_tau, problog_flag_validate_indomain_0_1_open,'tau value for line search',0.618033988749,learning_line_search), + 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), + logger_define_variable(duration,time), + logger_define_variable(mse_trainingset,float), + logger_define_variable(mse_min_trainingset,float), + logger_define_variable(mse_max_trainingset,float), + logger_define_variable(mse_testset,float), + logger_define_variable(mse_min_testset,float), + logger_define_variable(mse_max_testset,float), + logger_define_variable(gradient_mean,float), + logger_define_variable(gradient_min,float), + logger_define_variable(gradient_max,float), + logger_define_variable(ground_truth_diff,float), + logger_define_variable(ground_truth_mindiff,float), + logger_define_variable(ground_truth_maxdiff,float), + logger_define_variable(learning_rate,float), + logger_define_variable(alpha,float). + +:- initialization(init_flags). +:- initialization(init_logger). + diff --git a/packages/ProbLog/problog_examples/viralmarketing.pl b/packages/ProbLog/problog_examples/viralmarketing.pl index 1a5ba3733..1cdf3d2a7 100644 --- a/packages/ProbLog/problog_examples/viralmarketing.pl +++ b/packages/ProbLog/problog_examples/viralmarketing.pl @@ -1,5 +1,11 @@ %%% -*- Mode: Prolog; -*- +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% ProbLog program describing a viral marketing problem +% example for using decision theory ProbLog +% $Id: viralmarketing.pl 4875 2010-10-05 15:28:35Z theo $ +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + % The viral marketing example consists of a social network of friend relations. You have to decide which persons to market. Sending marketing has a cost of 2, but might cause people to buy your product, giving you a profit of 5. When someone buys the product, it becomes more likely that his friends also buy the product. :- use_module('../dtproblog'). diff --git a/packages/ProbLog/problog_examples/viralmarketing_tabled.pl b/packages/ProbLog/problog_examples/viralmarketing_tabled.pl index 18fda0dca..a4bc1e687 100644 --- a/packages/ProbLog/problog_examples/viralmarketing_tabled.pl +++ b/packages/ProbLog/problog_examples/viralmarketing_tabled.pl @@ -1,5 +1,11 @@ %%% -*- Mode: Prolog; -*- +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% ProbLog program describing a viral marketing problem +% example for using tabled decision theory ProbLog +% $Id: viralmarketing_tabled.pl 4875 2010-10-05 15:28:35Z theo $ +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + % The viral marketing example consists of a social network of friend relations. You have to decido which persons to market. Sending marketing has a cost of 2, but might cause people to buy your product, giving you a profit of 5. When someone buys the product, it becomes more likely that his friends also buy the product. :- use_module('../dtproblog'). diff --git a/packages/ProbLog/problog_learning.yap b/packages/ProbLog/problog_learning.yap index f209a35a5..b8c2ff1f0 100644 --- a/packages/ProbLog/problog_learning.yap +++ b/packages/ProbLog/problog_learning.yap @@ -2,8 +2,8 @@ %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % -% $Date: 2010-09-30 16:15:04 +0200 (Thu, 30 Sep 2010) $ -% $Revision: 4864 $ +% $Date: 2010-10-05 16:52:13 +0200 (Tue, 05 Oct 2010) $ +% $Revision: 4869 $ % % This file is part of ProbLog % http://dtai.cs.kuleuven.be/problog @@ -364,7 +364,7 @@ check_examples :- (current_predicate(user:example/4),user:example(ID,_,P,_), (\+ number(P); P>1 ; P<0)) -> ( - format(user_error,'The training example ~q does not have a valid probaility value (~q).~n',[ID,P]), + format(user_error,'The training example ~q does not have a valid probability value (~q).~n',[ID,P]), throw(error(examples)) ); true ), @@ -373,7 +373,7 @@ check_examples :- (current_predicate(user:test_example/4),user:test_example(ID,_,P,_), (\+ number(P); P>1 ; P<0)) -> ( - format(user_error,'The test example ~q does not have a valid probaility value (~q).~n',[ID,P]), + format(user_error,'The test example ~q does not have a valid probability value (~q).~n',[ID,P]), throw(error(examples)) ); true ), From f2e3476a8ea1bb8b8a5f1b692d21b923d6322460 Mon Sep 17 00:00:00 2001 From: Theofrastos Mantadelis Date: Tue, 5 Oct 2010 18:27:09 +0200 Subject: [PATCH 05/14] ProbLog Versioning System --- packages/ProbLog/Makefile.in | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/ProbLog/Makefile.in b/packages/ProbLog/Makefile.in index 15813a50f..b5dadb4fb 100644 --- a/packages/ProbLog/Makefile.in +++ b/packages/ProbLog/Makefile.in @@ -50,6 +50,7 @@ PROBLOG_PROGRAMS= \ $(srcdir)/problog/variable_elimination.yap \ $(srcdir)/problog/print_learning.yap \ $(srcdir)/problog/utils_learning.yap \ + $(srcdir)/problog/version_control.yap \ $(srcdir)/problog/variables.yap PROBLOG_EXAMPLES = \ From ff6bd1dda0304878fd8bde71e714dacd147c9735 Mon Sep 17 00:00:00 2001 From: Theofrastos Mantadelis Date: Tue, 5 Oct 2010 18:29:29 +0200 Subject: [PATCH 06/14] ProbLog Versioning System --- packages/ProbLog/problog_examples/problog.yap | 3133 ----------------- .../problog_examples/problog_learning.yap | 1677 --------- 2 files changed, 4810 deletions(-) delete mode 100644 packages/ProbLog/problog_examples/problog.yap delete mode 100644 packages/ProbLog/problog_examples/problog_learning.yap diff --git a/packages/ProbLog/problog_examples/problog.yap b/packages/ProbLog/problog_examples/problog.yap deleted file mode 100644 index 873c53837..000000000 --- a/packages/ProbLog/problog_examples/problog.yap +++ /dev/null @@ -1,3133 +0,0 @@ -%%% -*- Mode: Prolog; -*- - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -% -% $Date: 2010-10-05 18:15:57 +0200 (Tue, 05 Oct 2010) $ -% $Revision: 4876 $ -% -% This file is part of ProbLog -% http://dtai.cs.kuleuven.be/problog -% -% ProbLog was developed at Katholieke Universiteit Leuven -% -% Copyright 2008, 2009, 2010 -% Katholieke Universiteit Leuven -% -% Main authors of this file: -% Angelika Kimmig, Vitor Santos Costa,Bernd Gutmann, -% Theofrastos Mantadelis, Guy Van den Broeck -% -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -% -% Artistic License 2.0 -% -% Copyright (c) 2000-2006, The Perl Foundation. -% -% Everyone is permitted to copy and distribute verbatim copies of this -% license document, but changing it is not allowed. Preamble -% -% This license establishes the terms under which a given free software -% Package may be copied, modified, distributed, and/or -% redistributed. The intent is that the Copyright Holder maintains some -% artistic control over the development of that Package while still -% keeping the Package available as open source and free software. -% -% You are always permitted to make arrangements wholly outside of this -% license directly with the Copyright Holder of a given Package. If the -% terms of this license do not permit the full use that you propose to -% make of the Package, you should contact the Copyright Holder and seek -% a different licensing arrangement. Definitions -% -% "Copyright Holder" means the individual(s) or organization(s) named in -% the copyright notice for the entire Package. -% -% "Contributor" means any party that has contributed code or other -% material to the Package, in accordance with the Copyright Holder's -% procedures. -% -% "You" and "your" means any person who would like to copy, distribute, -% or modify the Package. -% -% "Package" means the collection of files distributed by the Copyright -% Holder, and derivatives of that collection and/or of those files. A -% given Package may consist of either the Standard Version, or a -% Modified Version. -% -% "Distribute" means providing a copy of the Package or making it -% accessible to anyone else, or in the case of a company or -% organization, to others outside of your company or organization. -% -% "Distributor Fee" means any fee that you charge for Distributing this -% Package or providing support for this Package to another party. It -% does not mean licensing fees. -% -% "Standard Version" refers to the Package if it has not been modified, -% or has been modified only in ways explicitly requested by the -% Copyright Holder. -% -% "Modified Version" means the Package, if it has been changed, and such -% changes were not explicitly requested by the Copyright Holder. -% -% "Original License" means this Artistic License as Distributed with the -% Standard Version of the Package, in its current version or as it may -% be modified by The Perl Foundation in the future. -% -% "Source" form means the source code, documentation source, and -% configuration files for the Package. -% -% "Compiled" form means the compiled bytecode, object code, binary, or -% any other form resulting from mechanical transformation or translation -% of the Source form. -% -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -% -% Permission for Use and Modification Without Distribution -% -% (1) You are permitted to use the Standard Version and create and use -% Modified Versions for any purpose without restriction, provided that -% you do not Distribute the Modified Version. -% -% Permissions for Redistribution of the Standard Version -% -% (2) You may Distribute verbatim copies of the Source form of the -% Standard Version of this Package in any medium without restriction, -% either gratis or for a Distributor Fee, provided that you duplicate -% all of the original copyright notices and associated disclaimers. At -% your discretion, such verbatim copies may or may not include a -% Compiled form of the Package. -% -% (3) You may apply any bug fixes, portability changes, and other -% modifications made available from the Copyright Holder. The resulting -% Package will still be considered the Standard Version, and as such -% will be subject to the Original License. -% -% Distribution of Modified Versions of the Package as Source -% -% (4) You may Distribute your Modified Version as Source (either gratis -% or for a Distributor Fee, and with or without a Compiled form of the -% Modified Version) provided that you clearly document how it differs -% from the Standard Version, including, but not limited to, documenting -% any non-standard features, executables, or modules, and provided that -% you do at least ONE of the following: -% -% (a) make the Modified Version available to the Copyright Holder of the -% Standard Version, under the Original License, so that the Copyright -% Holder may include your modifications in the Standard Version. (b) -% ensure that installation of your Modified Version does not prevent the -% user installing or running the Standard Version. In addition, the -% modified Version must bear a name that is different from the name of -% the Standard Version. (c) allow anyone who receives a copy of the -% Modified Version to make the Source form of the Modified Version -% available to others under (i) the Original License or (ii) a license -% that permits the licensee to freely copy, modify and redistribute the -% Modified Version using the same licensing terms that apply to the copy -% that the licensee received, and requires that the Source form of the -% Modified Version, and of any works derived from it, be made freely -% available in that license fees are prohibited but Distributor Fees are -% allowed. -% -% Distribution of Compiled Forms of the Standard Version or -% Modified Versions without the Source -% -% (5) You may Distribute Compiled forms of the Standard Version without -% the Source, provided that you include complete instructions on how to -% get the Source of the Standard Version. Such instructions must be -% valid at the time of your distribution. If these instructions, at any -% time while you are carrying out such distribution, become invalid, you -% must provide new instructions on demand or cease further -% distribution. If you provide valid instructions or cease distribution -% within thirty days after you become aware that the instructions are -% invalid, then you do not forfeit any of your rights under this -% license. -% -% (6) You may Distribute a Modified Version in Compiled form without the -% Source, provided that you comply with Section 4 with respect to the -% Source of the Modified Version. -% -% Aggregating or Linking the Package -% -% (7) You may aggregate the Package (either the Standard Version or -% Modified Version) with other packages and Distribute the resulting -% aggregation provided that you do not charge a licensing fee for the -% Package. Distributor Fees are permitted, and licensing fees for other -% components in the aggregation are permitted. The terms of this license -% apply to the use and Distribution of the Standard or Modified Versions -% as included in the aggregation. -% -% (8) You are permitted to link Modified and Standard Versions with -% other works, to embed the Package in a larger work of your own, or to -% build stand-alone binary or bytecode versions of applications that -% include the Package, and Distribute the result without restriction, -% provided the result does not expose a direct interface to the Package. -% -% Items That are Not Considered Part of a Modified Version -% -% (9) Works (including, but not limited to, modules and scripts) that -% merely extend or make use of the Package, do not, by themselves, cause -% the Package to be a Modified Version. In addition, such works are not -% considered parts of the Package itself, and are not subject to the -% terms of this license. -% -% General Provisions -% -% (10) Any use, modification, and distribution of the Standard or -% Modified Versions is governed by this Artistic License. By using, -% modifying or distributing the Package, you accept this license. Do not -% use, modify, or distribute the Package, if you do not accept this -% license. -% -% (11) If your Modified Version has been derived from a Modified Version -% made by someone other than you, you are nevertheless required to -% ensure that your Modified Version complies with the requirements of -% this license. -% -% (12) This license does not grant you the right to use any trademark, -% service mark, tradename, or logo of the Copyright Holder. -% -% (13) This license includes the non-exclusive, worldwide, -% free-of-charge patent license to make, have made, use, offer to sell, -% sell, import and otherwise transfer the Package with respect to any -% patent claims licensable by the Copyright Holder that are necessarily -% infringed by the Package. If you institute patent litigation -% (including a cross-claim or counterclaim) against any party alleging -% that the Package constitutes direct or contributory patent -% infringement, then this Artistic License to you shall terminate on the -% date that such litigation is filed. -% -% (14) Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT -% HOLDER AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED -% WARRANTIES. THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A -% PARTICULAR PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT -% PERMITTED BY YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT -% HOLDER OR CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, -% INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE -% OF THE PACKAGE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -% -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% - - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -% ProbLog inference -% -% assumes probabilistic facts as Prob::Fact and clauses in normal Prolog format -% -% provides following inference modes (16/12/2008): -% - approximation with interval width Delta (IJCAI07): problog_delta(+Query,+Delta,-Low,-High,-Status) -% - bounds based on single probability threshold: problog_threshold(+Query,+Threshold,-Low,-High,-Status) -% - as above, but lower bound only: problog_low(+Query,+Threshold,-Low,-Status) -% - lower bound based on K most likely proofs: problog_kbest(+Query,+K,-Low,-Status) -% - explanation probability (ECML07): problog_max(+Query,-Prob,-FactsUsed) -% - exact probability: problog_exact(+Query,-Prob,-Status) -% - sampling: problog_montecarlo(+Query,+Delta,-Prob) -% -% -% angelika.kimmig@cs.kuleuven.be -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -:- module(problog, [problog_delta/5, - problog_threshold/5, - problog_low/4, - problog_kbest/4, - problog_kbest_save/6, - problog_max/3, - problog_exact/3, - problog_exact_save/5, - problog_montecarlo/3, - problog_dnf_sampling/3, - problog_answers/2, - problog_kbest_answers/3, - problog_table/1, - clear_retained_tables/0, - problog_neg/1, - get_fact_probability/2, - set_fact_probability/2, - get_continuous_fact_parameters/2, - set_continuous_fact_parameters/2, - get_fact/2, - tunable_fact/2, - tunable_continuous_fact/2, - continuous_fact/1, - non_ground_fact/1, - export_facts/1, - problog_help/0, - show_inference/0, - problog_dir/1, - set_problog_flag/2, - problog_flag/2, - problog_flags/0, - problog_flags/1, - reset_problog_flags/0, - problog_assert/1, - problog_assert/2, - problog_retractall/1, - problog_statistics/2, - problog_statistics/0, - grow_atom_table/1, - problog_exact_nested/3, - problog_tabling_negated_synonym/2, - problog_control/2, - build_trie/2, - build_trie/3, - problog_infer/2, - problog_infer/3, - problog_infer_forest/2, - write_bdd_struct_script/3, - problog_bdd_forest/1, - require/1, - unrequire/1, - bdd_files/2, - delete_bdd_forest_files/1, - recover_grounding_id/2, - grounding_is_known/2, - grounding_id/3, - decision_fact/2, - reset_non_ground_facts/0, - '::'/2, - probabilistic_fact/3, - init_problog/1, - problog_call/1, - problog_infer_forest_supported/0, - problog_bdd_forest_supported/0, - problog_real_kbest/4, - op( 550, yfx, :: ), - op( 550, fx, ?:: ), - op(1149, yfx, <-- ), - op( 1150, fx, problog_table ), - in_interval/3, - below/2, - above/2]). - -:- style_check(all). -:- yap_flag(unknown,error). - -:- set_prolog_flag(to_chars_mode,quintus). - -% general yap modules -:- use_module(library(charsio)). -:- use_module(library(lists)). -:- use_module(library(terms)). -:- use_module(library(random)). % PM doesn't seem to be used! -:- use_module(library(system)). -:- use_module(library(rbtrees)). % PM doesn't seem to be used! -:- use_module(library(ordsets), [list_to_ord_set/2, ord_insert/3, ord_union/3]). - -% problog related modules -:- use_module('problog/variables'). -:- use_module('problog/extlists'). -:- use_module('problog/gflags', [flag_store/2]). -:- use_module('problog/flags'). -:- use_module('problog/print'). -:- use_module('problog/os'). -:- use_module('problog/tptree'). -:- use_module('problog/tabling'). -:- use_module('problog/sampling'). -:- use_module('problog/intervals'). -:- use_module('problog/mc_DNF_sampling'). -:- catch(use_module('problog/ad_converter'),_,true). -:- catch(use_module('problog/variable_elimination'),_,true). - -% op attaching probabilities to facts -:- op( 550, yfx, :: ). -:- op( 550, fx, ?:: ). - -% for annotated disjunctions -% :- op(1149, yfx, <-- ). - -%%%%%%%%%%%%%%%%%%%%%%%% -% control predicates on various levels -%%%%%%%%%%%%%%%%%%%%%%%% - -% global over all inference methods, internal use only -:- dynamic(problog_predicate/2). -:- dynamic(problog_continuous_predicate/3). -% global over all inference methods, exported -:- dynamic(tunable_fact/2). -:- dynamic(non_ground_fact/1). -:- dynamic(continuous_fact/1). -%:- dynamic(problog_dir/1). -% global, manipulated via problog_control/2 -:- dynamic(up/0). -:- dynamic(limit/0). -:- dynamic(mc/0). -:- dynamic(remember/0). -:- dynamic(exact/0). % Theo tabling -:- dynamic(find_decisions/0). -:- dynamic(internal_strategy/0). -% local to problog_delta -:- dynamic(low/2). -:- dynamic(up/2). -:- dynamic(stopDiff/1). -% local to problog_kbest -:- dynamic(current_kbest/3). -% local to problog_max -:- dynamic(max_probability/1). -:- dynamic(max_proof/1). -% local to problog_montecarlo -:- dynamic(mc_prob/1). -% local to problog_answers -:- dynamic(answer/1). -% to keep track of the groundings for non-ground facts -:- dynamic(grounding_is_known/2). - -% for decisions -:- dynamic(decision_fact/2). - -% for fact where the proabability is a variable -:- dynamic(dynamic_probability_fact/1). -:- dynamic(dynamic_probability_fact_extract/2). - -% for storing continuous parts of proofs (Hybrid ProbLog) -:- dynamic([hybrid_proof/3, hybrid_proof/4]). -:- dynamic(hybrid_proof_disjoint/4). - -% ProbLog files declare prob. facts as P::G -% and this module provides the predicate X::Y to iterate over them -:- multifile('::'/2). - -% directory where problogbdd executable is located -% automatically set during loading -- assumes it is in same place as this file (problog.yap) -:- getcwd(PD), set_problog_path(PD). - -%%%%%%%%%%%% -% iterative deepening on minimal probabilities (delta, max, kbest): -% - first threshold (not in log-space as only used to retrieve argument for init_threshold/1, which is also used with user-supplied argument) -% - last threshold to ensure termination in case infinite search space (saved also in log-space for easy comparison with current values during search) -% - factor used to decrease threshold for next level, NewMin=Factor*OldMin (saved also in log-space) -%%%%%%%%%%%% - -:- 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(id_stepsize, problog_flag_validate_indomain_0_1_close, 'threshold shrinking factor iterative deepening', 0.5, inference, flags:id_stepsize_handler) -)). - -%%%%%%%%%%%% -% prune check stops derivations if they use a superset of facts already known to form a proof -% (very) costly test, can be switched on/off here (This is obsolete as it is not included in implementation) -%%%%%%%%%%%% - -:- initialization( - problog_define_flag(prunecheck, problog_flag_validate_switch, 'stop derivations including all facts of known proof', off, inference) -). - -%%%%%%%%%%%% -% max number of calls to probabilistic facts per derivation (to ensure termination) -%%%%%%%%%%%% - -:- initialization( - problog_define_flag(maxsteps, problog_flag_validate_posint, 'max. number of prob. steps per derivation', 1000, inference) -). - -%%%%%%%%%%%% -% BDD timeout in seconds, used as option in BDD tool -% files to write BDD script and pars -% bdd_file overwrites bdd_par_file with matching extended name -% if different name wanted, respect order when setting -% save BDD information for the (last) lower bound BDD used during inference -% produces three files named save_script, save_params, save_map -% located in the directory given by problog_flag dir -%%%%%%%%%%%% - -:- initialization(( -% problog_define_flag(bdd_path, problog_flag_validate_directory, 'problogbdd directory', '.',bdd), - problog_define_flag(bdd_time, problog_flag_validate_posint, 'BDD computation timeout in seconds', 60, bdd), - problog_define_flag(save_bdd, problog_flag_validate_boolean, 'save BDD files for (last) lower bound', false, bdd), - problog_define_flag(dynamic_reorder, problog_flag_validate_boolean, 'use dynamic re-ordering for BDD', true, bdd), - problog_define_flag(bdd_static_order, problog_flag_validate_boolean, 'use a static order', false, bdd) -)). - -%%%%%%%%%%%% -% determine whether ProbLog outputs information (number of proofs, intermediate results, ...) -% default was true, as otherwise problog_delta won't output intermediate bounds -% default is false now, as dtproblog will flood the user with verbosity -%%%%%%%%%%%% - -:- initialization( - problog_define_flag(verbose, problog_flag_validate_boolean, 'output intermediate information', false,output) -). - -%%%%%%%%%%%% -% determine whether ProbLog outputs proofs when adding to trie -% default is false -%%%%%%%%%%%% - -:- initialization( - problog_define_flag(show_proofs, problog_flag_validate_boolean, 'output proofs', false,output) -). - -%%%%%%%%%%%% -% Trie dump parameter for saving a file with the trie structure in the directory by problog_flag dir -%%%%%%%%%%%% - -:- initialization( - problog_define_flag(triedump, problog_flag_validate_boolean, 'generate file: trie_file containing the trie structure', false,output) -). - -%%%%%%%%%%%% -% Default inference method -%%%%%%%%%%%% - -:- initialization(problog_define_flag(inference, problog_flag_validate_dummy, 'default inference method', exact, inference)). - -%%%%%%%%%%%% -% Tunable Facts -%%%%%%%%%%%% - -:- initialization(problog_define_flag(tunable_fact_start_value,problog_flag_validate_dummy,'How to initialize tunable probabilities',uniform(0.1,0.9),learning_general,flags:learning_prob_init_handler)). - - - -problog_dir(PD):- problog_path(PD). - -%%%%%%%%%%%%%%%%%%%%%%%% -% initialization of global parameters -%%%%%%%%%%%%%%%%%%%%%%%% - -init_global_params :- - %grow_atom_table(1000000), - - %%%%%%%%%%%% - % working directory: all the temporary and output files will be located there - % it assumes a subdirectory of the current working dir - % on initialization, the current dir is the one where the user's file is located - % should be changed to use temporary folder structure of operating system - %%%%%%%%%%%% - tmpnam(TempFolder), - atomic_concat([TempFolder, '_problog'], TempProblogFolder), - problog_define_flag(dir, problog_flag_validate_directory, 'directory for files', TempProblogFolder, output), - problog_define_flag(bdd_par_file, problog_flag_validate_file, 'file for BDD variable parameters', example_bdd_probs, bdd, flags:working_file_handler), - problog_define_flag(bdd_result, problog_flag_validate_file, 'file to store result calculated from BDD', example_bdd_res, bdd, flags:working_file_handler), - problog_define_flag(bdd_file, problog_flag_validate_file, 'file for BDD script', example_bdd, bdd, flags:bdd_file_handler), - problog_define_flag(static_order_file, problog_flag_validate_file, 'file for BDD static order', example_bdd_order, bdd, flags:working_file_handler), -%%%%%%%%%%%% -% montecarlo: recalculate current approximation after N samples -% montecarlo: write log to this file -%%%%%%%%%%%% - problog_define_flag(mc_logfile, problog_flag_validate_file, 'logfile for montecarlo', 'log.txt', mcmc, flags:working_file_handler), - check_existance('problogbdd'). - -% parameter initialization to be called after returning to user's directory: -:- initialization(init_global_params). - - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -% internal control flags -% if on -% - up: collect stopped derivations to build upper bound -% - limit: iterative deepening reached limit -> should go to next level -% - mc: using problog_montecarlo, i.e. proving with current sample instead of full program -% - remember: save BDD files containing script, params and mapping -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -problog_control(on,X) :- - call(X),!. -problog_control(on,X) :- - assertz(X). -problog_control(off,X) :- - retractall(X). -problog_control(check,X) :- - call(X). - -reset_control :- - problog_control(off,up), - problog_control(off,mc), - problog_control(off,limit), -% problog_control(off,exact), - problog_control(off,remember). - -:- initialization(reset_control). - -grow_atom_table(N):- - generate_atoms(N, 0), - garbage_collect_atoms. -generate_atoms(N, N):-!. -generate_atoms(N, A):- - NA is A + 1, - atomic_concat([theo, A], _Atom), - generate_atoms(N, NA). - - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -% nice user syntax Prob::Fact -% automatic translation to internal hardware access format -% -% probabilities =1 are dropped -> normal Prolog fact -% -% internal fact representation -% - prefixes predicate name with problog_ -% - adds unique ID as first argument -% - adds logarithm of probability as last argument -% - keeps original arguments in between -% -% for each predicate appearing as probabilistic fact, wrapper clause is introduced: -% - head is most general instance of original fact -% - body is corresponding version of internal fact plus call to add_to_proof/2 to update current state during proving -% example: edge(A,B) :- problog_edge(ID,A,B,LogProb), add_to_proof(ID,LogProb). -% -% dynamic predicate problog_predicate(Name,Arity) keeps track of predicates that already have wrapper clause -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% - -term_expansion_intern(A, B, C):- - catch(term_expansion_intern_ad(A, B, C), _, false). - -% converts ?:: prefix to ? :: infix, as handled by other clause -term_expansion_intern((Annotation::Fact), Module, ExpandedClause) :- - Annotation == '?', - term_expansion_intern((? :: Fact :- true), Module, ExpandedClause). - - -% handles decision clauses -term_expansion_intern((Annotation :: Head :- Body), Module, problog:ExpandedClause) :- - ( - Annotation == '?' -> - % It's a decision with a body - copy_term((Head,Body),(HeadCopy,_BodyCopy)), - functor(Head, Functor, Arity), - atomic_concat([problog_,Functor],LongFunctor), - Head =.. [Functor|Args], - append(Args,[LProb],LongArgs), - probclause_id(ID), - ProbFactHead =.. [LongFunctor,ID|LongArgs], - assertz(decision_fact(ID,Head)), - ExpandedClause = (ProbFactHead :- - user:Body, - (problog_control(check,internal_strategy) -> - dtproblog:strategy_log(ID,Head,LProb) - ; - LProb = '?' - ) - ), - assertz(dynamic_probability_fact(ID)), - assertz((dynamic_probability_fact_extract(HeadCopy,P_New) :- - dtproblog:strategy(ID,HeadCopy,P_New) - )), - (ground(Head) -> - true - ; - assertz(non_ground_fact(ID)) - ), - problog_predicate(Functor, Arity, LongFunctor, Module) - ; - % If it has a body, it's not supported - (Body == true -> - % format('Expanding annotated fact ~q :: ~q :- ~q in other clause.~n',[Annotation,Head,Body]), - fail - ; - throw(error('We do not support annotated clauses (yet)!', (Annotation :: Head :- Body))) - ) - ). - - -/* this can slow down prolog time by several orders if there's lots of them -user:term_expansion(P::Goal,Goal) :- - P \= t(_), - P =:= 1, - !. -*/ - -% handles probabilistic facts -term_expansion_intern(P :: Goal,Module,problog:ProbFact) :- - copy_term((P,Goal),(P_Copy,Goal_Copy)), - functor(Goal, Name, Arity), - atomic_concat([problog_,Name],ProblogName), - Goal =.. [Name|Args], - append(Args,[LProb],L1), - probclause_id(ID), - ProbFact =.. [ProblogName,ID|L1], - ( - (nonvar(P), P = t(TrueProb)) - -> - ( - assertz(tunable_fact(ID,TrueProb)), - sample_initial_value_for_tunable_fact(LProb) - ); - ( - ground(P) - -> - EvalP is P, % allows one to use ground arithmetic expressions as probabilities - LProb is log(P), - assert_static(prob_for_id(ID,EvalP,LProb)); % Prob is fixed -- assert it for quick retrieval - ( - % Probability is a variable... check wether it appears in the term - ( - variable_in_term(Goal,P) - -> - true; - ( - format(user_error,'If you use probabilisitic facts with a variable as probabilility, the variable has to appear inside the fact.~n',[]), - format(user_error,'You used ~q in your program.~2n',[P::Goal]), - throw(non_ground_fact_error(P::Goal)) - ) - ), - LProb=log(P), - assertz(dynamic_probability_fact(ID)), - assertz(dynamic_probability_fact_extract(Goal_Copy,P_Copy)) - ) - ) - ), - ( - ground(Goal) - -> - true; - assertz(non_ground_fact(ID)) - ), - problog_predicate(Name, Arity, ProblogName,Module). - - -sample_initial_value_for_tunable_fact(LogP) :- - problog_flag(tunable_fact_start_value,Initializer), - - ( - Initializer=uniform(Low,High) - -> - ( - Spread is High-Low, - random(Rand), - P1 is Rand*Spread+Low, - - % security check, to avoid log(0) - ( - P1>0 - -> - P=P1; - P=0.5 - ) - ); - ( - number(Initializer) - -> - P=Initializer; - throw(unkown_probability_initializer(Initializer)) - ) - ), - - LogP is log(P). - - - -% Hybrid ProbLog stuff - -is_valid_gaussian(X) :- - compound(X), - X=gaussian(Mu,Sigma), - ( - ((number(Mu),number(Sigma));(Mu=t(_),Sigma=t(_))) - -> - true; - throw(invalid_gaussian(X)) - ). - -:- multifile(user:term_expansion/1). - -user:term_expansion(Goal, problog:ProbFact) :- - compound(Goal), - Goal=..[Name|Args], - once( (nth(Pos,Args,GaussianArg),is_valid_gaussian(GaussianArg)) ), - - %Goal contains a Gaussian, there is some work to do - - ( % check for a second Gaussian - (nth(Pos2,Args,GaussianArg2),Pos2\=Pos,is_valid_gaussian(GaussianArg2)) - -> - ( - format(user_error,'We only support continous atoms with at most one Gaussian inside.~n',[]), - format(user_error,'Your program contains the atom ~w with more than one.~n',[]), - throw(unsupported_multivariate_gaussian(Goal)) - ); - true - ), - - functor(Goal, Name, Arity), - atomic_concat([problogcontinuous_,Name],ProblogName), - probclause_id(ID), - - GaussianArg=gaussian(Mu_Arg,Sigma_Arg), - - % is it a tunable fact? - ( - (number(Mu_Arg),number(Sigma_Arg)) - -> - NewArgs=Args; - ( - Mu_Random is 0.1, % random*4-2, - Sigma_Random is 0.4, % random*2+0.5, - nth(Pos,Args,_,KeepArgs), - nth(Pos,NewArgs,gaussian(Mu_Random,Sigma_Random),KeepArgs), - assertz(tunable_fact(ID,gaussian(Mu_Arg,Sigma_Arg))) - ) - ), - ProbFact =.. [ProblogName,ID|NewArgs], - - ( - ground(Goal) - -> - true; - assertz(non_ground_fact(ID)) - ), - assertz(continuous_fact(ID)), - problog_continuous_predicate(Name, Arity, Pos,ProblogName). - - -% introduce wrapper clause if predicate seen first time -problog_continuous_predicate(Name, Arity,ContinuousArgumentPosition,_) :- - problog_continuous_predicate(Name, Arity,OldContinuousArgumentPosition), - !, - ( - ContinuousArgumentPosition=OldContinuousArgumentPosition - -> - true; - ( - format(user_error,'Continuous predicates of the same name and arity must ',[]), - format(user_error,'have the continuous argument all at the same position.~n',[]), - format(user_error,'Your program contains the predicate ~q/~q. There are ',[]), - format(user_error,'atoms which have the continuous argument at position ',[]), - format(user_error,'~q and other have it at ~q.',[Name,Arity,OldContinuousArgumentPosition,ContinuousArgumentPosition]), - throw(continuous_argument(not_unique_position)) - ) - ). -problog_continuous_predicate(Name, Arity, ContinuousArgumentPosition, ProblogName) :- - - LBefore is ContinuousArgumentPosition-1, - LAfter is Arity-ContinuousArgumentPosition, - - length(ArgsBefore,LBefore), - length(ArgsAfter,LAfter), - append(ArgsBefore,[(ID,ID2,GaussianArg)|ArgsAfter],Args), - append(ArgsBefore,[GaussianArg|ArgsAfter],ProbArgs), - - OriginalGoal =.. [Name|Args], - - - ProbFact =.. [ProblogName,ID|ProbArgs], - prolog_load_context(module,Mod), - - assertz( (Mod:OriginalGoal :- ProbFact, - % continuous facts always get a grounding ID, even when they are actually ground - % this simplifies the BDD script generation - non_ground_fact_grounding_id(ProbFact,Ground_ID), - atomic_concat([ID,'_',Ground_ID],ID2), - add_continuous_to_proof(ID,ID2) - )), - - assertz(problog_continuous_predicate(Name, Arity,ContinuousArgumentPosition)), - ArityPlus1 is Arity+1, - dynamic(problog:ProblogName/ArityPlus1). - - -in_interval(ID,Low,High) :- - number(Low), - number(High), - Low - non_ground_fact_grounding_id(Goal,G_ID), - atomic_concat([ID,'_',G_ID],ID2) - ; - ID2=ID - ). - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -% What to do when prolog tries to prove a problog fact -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% - -prove_problog_fact(ClauseID,GroundID,Prob) :- - (problog_control(check,find_decisions) -> - signal_decision(ClauseID,GroundID) - ; - (Prob = '?' -> - add_to_proof(GroundID,0) % 0 is log(1)! - ; - % Checks needed for LeDTProbLog - (Prob = always -> - % Always true, do not add to trie - true - ; - (Prob = never -> - % Always false, do not add to trie - fail - ; - % something in between, add to proof - ProbEval is Prob, - add_to_proof(GroundID,ProbEval) - ) - ) - ) - ). - -prove_problog_fact_negated(ClauseID,GroundID,Prob) :- - (problog_control(check,find_decisions) -> - signal_decision(ClauseID,GroundID) - ; - (Prob = '?' -> - add_to_proof_negated(GroundID,-inf) % 0 is log(1)! - ; - % Checks needed for LeDTProbLog - (Prob = always -> - % Always true, do not add to trie - fail - ; - (Prob = never -> - % Always false, do not add to trie - true - ; - % something in between, add to proof - ProbEval is Prob, - add_to_proof_negated(GroundID,ProbEval) - ) - ) - ) - ). - -% generate next global identifier -:- initialization(nb_setval(probclause_counter,0)). - -probclause_id(ID) :- - nb_getval(probclause_counter,ID), !, - C1 is ID+1, - nb_setval(probclause_counter,C1), !. - -non_ground_fact_grounding_id(Goal,ID) :- - ( - ground(Goal) - -> - true; - ( - format(user_error,'The current program uses non-ground facts.~n', []), - format(user_error,'If you query those, you may only query fully-grounded versions of the fact.~n',[]), - format(user_error,'Within the current proof, you queried for ~q which is not ground.~n~n', [Goal]), - throw(error(non_ground_fact(Goal))) - ) - ), - ( - grounding_is_known(Goal,ID) - -> - true; - ( - nb_getval(non_ground_fact_grounding_id_counter,ID), - ID2 is ID+1, - nb_setval(non_ground_fact_grounding_id_counter,ID2), - assertz(grounding_is_known(Goal,ID)) - ) - ). - -reset_non_ground_facts :- - (required(keep_ground_ids) -> - true - ; - nb_setval(non_ground_fact_grounding_id_counter,0), - retractall(grounding_is_known(_,_)) - ). - -:- initialization(reset_non_ground_facts). - -% backtrack over all probabilistic facts -% must come before term_expansion -P::Goal :- - probabilistic_fact(P,Goal,_). - -% backtrack over all probabilistic facts -probabilistic_fact(P2,Goal,ID) :- - ( - ground(Goal) - -> - ( - Goal =.. [F|Args], - atomic_concat('problog_',F,F2), - append([ID|Args],[P],Args2), - Goal2 =..[F2|Args2], - length(Args2,N), - current_predicate(F2/N), - call(Goal2), - number(P), - P2 is exp(P) - ); - ( - get_internal_fact(ID,ProblogTerm,_ProblogName,_ProblogArity), - ProblogTerm =.. [F,_ID|Args], - append(Args2,[P],Args), - name(F,[_p,_r,_o,_b,_l,_o,_g,_|F2Chars]), - name(F2,F2Chars), - Goal =.. [F2|Args2], - ( - dynamic_probability_fact(ID) - -> - P2=p; - P2 is exp(P) - ) - ) - ). - - -% generates unique IDs for proofs -proof_id(ID) :- - nb_getval(problog_proof_id,ID), - ID2 is ID+1, - nb_setval(problog_proof_id,ID2). - -reset_problog_proof_id :- - nb_setval(problog_proof_id,0). - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -% access/update the probability of ID's fact -% hardware-access version: naively scan all problog-predicates (except if prob is recorded in static database), -% cut choice points if ID is ground (they'll all fail as ID is unique), -% but not if it isn't (used to iterate over all facts when writing out probabilities for learning) -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -% using a dummy for the static prob database is more efficient than checking for current_predicate -prob_for_id(dummy,dummy,dummy). - -get_fact_probability(A, Prob) :- - ground(A), - \+ number(A), - name(A, A_Codes), - once(append(Part1, [95|Part2], A_Codes)), % 95 = '_' - number_codes(ID, Part1), !, - % let's check whether Part2 contains an 'l' (l=low) - (member(108, Part2) -> - fail - ; - number_codes(Grounding_ID, Part2), - (dynamic_probability_fact(ID) -> - grounding_is_known(Goal, Grounding_ID), - dynamic_probability_fact_extract(Goal, Prob) - ; - get_fact_probability(ID, Prob) - ) - ). -get_fact_probability(ID,Prob) :- - ground(ID), - prob_for_id(ID,Prob,_), - !. -get_fact_probability(ID,Prob) :- - ( - ground(ID) -> - get_internal_fact(ID,ProblogTerm,_ProblogName,ProblogArity),! - ; - get_internal_fact(ID,ProblogTerm,_ProblogName,ProblogArity) - ), - arg(ProblogArity,ProblogTerm,Log), - (Log = '?' -> - throw(error('Why do you want to know the probability of a decision?')) %fail - ; - Prob is exp(Log) - ). - -get_fact_log_probability(ID,Prob) :- - ground(ID), - prob_for_id(ID,_,Prob),!. -get_fact_log_probability(ID,Prob) :- - ( - ground(ID) -> - get_internal_fact(ID,ProblogTerm,_ProblogName,ProblogArity),! - ; - get_internal_fact(ID,ProblogTerm,_ProblogName,ProblogArity) - ), - arg(ProblogArity,ProblogTerm,Prob), - Prob \== '?'. -get_fact_log_probability(ID,Prob) :- - get_fact_probability(ID,Prob1), - Prob is log(Prob1). - -set_fact_probability(ID,Prob) :- - get_internal_fact(ID,ProblogTerm,ProblogName,ProblogArity), - retract(ProblogTerm), - ProblogTerm =.. [ProblogName|ProblogTermArgs], - nth(ProblogArity,ProblogTermArgs,_,KeepArgs), - NewLogProb is log(Prob), - nth(ProblogArity,NewProblogTermArgs,NewLogProb,KeepArgs), - NewProblogTerm =.. [ProblogName|NewProblogTermArgs], - assertz(NewProblogTerm). - -get_internal_fact(ID,ProblogTerm,ProblogName,ProblogArity) :- - problog_predicate(Name,Arity), - atomic_concat([problog_,Name],ProblogName), - ProblogArity is Arity+2, - functor(ProblogTerm,ProblogName,ProblogArity), - arg(1,ProblogTerm,ID), - call(ProblogTerm). - -get_continuous_fact_parameters(ID,Parameters) :- - ( - ground(ID) -> - get_internal_continuous_fact(ID,ProblogTerm,_ProblogName,ProblogArity,ContinuousPos),! - ; - get_internal_continuous_fact(ID,ProblogTerm,_ProblogName,ProblogArity,ContinuousPos) - ), - InternalPos is ContinuousPos+1, - arg(InternalPos,ProblogTerm,Parameters). - -get_internal_continuous_fact(ID,ProblogTerm,ProblogName,ProblogArity,ContinuousPos) :- - problog_continuous_predicate(Name,Arity,ContinuousPos), - atomic_concat([problogcontinuous_,Name],ProblogName), - ProblogArity is Arity+1, - functor(ProblogTerm,ProblogName,ProblogArity), - arg(1,ProblogTerm,ID), - call(ProblogTerm). - -set_continuous_fact_parameters(ID,Parameters) :- - get_internal_continuous_fact(ID,ProblogTerm,ProblogName,_ProblogArity,ContinuousPos), - retract(ProblogTerm), - ProblogTerm =.. [ProblogName|ProblogTermArgs], - nth0(ContinuousPos,ProblogTermArgs,_,KeepArgs), - nth0(ContinuousPos,NewProblogTermArgs,Parameters,KeepArgs), - NewProblogTerm =.. [ProblogName|NewProblogTermArgs], - assertz(NewProblogTerm). - - - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -% writing those facts with learnable parameters to File -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -export_facts(Filename) :- - open(Filename,'write',Handle), - - %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% - ( % go over all probabilistic facts - P::Goal, - format(Handle,'~w :: ~q.~n',[P,Goal]), - - fail; % go to next prob. fact - true - ), - %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% - - %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% - ( % go over all continuous facts - continuous_fact(ID), - get_continuous_fact_parameters(ID,Param), - format(Handle,'~q. % ~q~n',[Param,ID]), - - fail; % go to next cont. fact - true - ), - %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% - - close(Handle). - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -% recover fact for given id -% list version not exported (yet?) -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -% ID of ground fact -get_fact(ID,OutsideTerm) :- - get_internal_fact(ID,ProblogTerm,ProblogName,ProblogArity), - !, - ProblogTerm =.. [_Functor,ID|Args], - atomic_concat('problog_',OutsideFunctor,ProblogName), - Last is ProblogArity-1, - nth(Last,Args,_LogProb,OutsideArgs), - OutsideTerm =.. [OutsideFunctor|OutsideArgs]. -% ID of instance of non-ground fact: get fact from grounding table -get_fact(ID,OutsideTerm) :- - recover_grounding_id(ID,GID), - grounding_is_known(OutsideTerm,GID). - -recover_grounding_id(Atom,ID) :- - name(Atom,List), - reverse(List,Rev), - recover_number(Rev,NumRev), - reverse(NumRev,Num), - name(ID,Num). -recover_number([95|_],[]) :- !. % name('_',[95]) -recover_number([A|B],[A|C]) :- - recover_number(B,C). - - -get_fact_list([],[]). -get_fact_list([ID|IDs],[Fact|Facts]) :- - (ID=not(X) -> Fact=not(Y); Fact=Y, ID=X), - get_fact(X,Y), - get_fact_list(IDs,Facts). - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -% ProbLog inference, core methods -% -% state of proving saved in two backtrackable global variables -% - problog_current_proof holds list of IDs of clauses used -% - problog_probability holds the sum of their log probabilities -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% - -% called "inside" probabilistic facts to update current state of proving -% if number of steps exceeded, fail -% if fact used before, succeed and keep status as is -% if not prunable, calculate probability and -% if threshold exceeded, add stopped derivation to upper bound and fail -% else update state and succeed -% -% do not maintain gloabl variables in montecarlo mode -add_to_proof(ID, Prob) :- - (problog_control(check, mc) -> - montecarlo_check(ID) - ; - b_getval(problog_steps,MaxSteps), - b_getval(problog_probability, CurrentP), - nb_getval(problog_threshold, CurrentThreshold), - b_getval(problog_current_proof, IDs), - %%%% Bernd, changes for negated ground facts - \+ open_end_memberchk(not(ID),IDs), - %%%% Bernd, changes for negated ground facts - (MaxSteps =< 0 -> - fail - ; - (open_end_memberchk(ID, IDs) -> %Theo - true - ; - open_end_add(ID, IDs, NIDs), %Theo - % \+ prune_check(NIDs, Trie_Completed_Proofs), - multiply_probabilities(CurrentP, Prob, NProb), - (NProb < CurrentThreshold -> - upper_bound(NIDs), - fail - ; - b_setval(problog_probability, NProb), - b_setval(problog_current_proof, NIDs) - ) - ), - Steps is MaxSteps - 1, - b_setval(problog_steps, Steps) - ) - ). - -%%%% Bernd, changes for negated ground facts -add_to_proof_negated(ID, Prob) :- - (problog_control(check, mc) -> - % the sample has to fail if the fact is negated - \+ montecarlo_check(ID) - ; - b_getval(problog_steps, MaxSteps), - b_getval(problog_probability, CurrentP), - nb_getval(problog_threshold, CurrentThreshold), - b_getval(problog_current_proof, IDs), - \+ open_end_memberchk(ID, IDs), - (MaxSteps =< 0 -> - fail - ; - (open_end_memberchk(not(ID), IDs) -> - true - ; - open_end_add(not(ID), IDs, NIDs), %Theo - % \+ prune_check(NIDs, Trie_Completed_Proofs), - InverseProb is log(1 - exp(Prob)), - multiply_probabilities(CurrentP, InverseProb, NProb), - (NProb < CurrentThreshold -> - upper_bound(NIDs), %% checkme - fail - ; - b_setval(problog_probability, NProb), - b_setval(problog_current_proof, NIDs) - ) - ), - Steps is MaxSteps - 1, - b_setval(problog_steps, Steps) - ) - ). -%%%% Bernd, changes for negated ground facts - -%Hybrid -add_continuous_to_proof(ID,GroundID) :- - b_getval(problog_continuous_facts_used,Facts), - ( - memberchk((ID,GroundID),Facts) - -> - true; - ( - b_setval(problog_continuous_facts_used,[(ID,GroundID)|Facts]), - atomic_concat([interval,'_',GroundID],Key), - b_setval(Key,all) - ) - ). - -% if in monte carlo mode ... -% (a) for ground facts (ID is number): check array to see if it can be used -montecarlo_check(ID) :- - number(ID), - !, - array_element(mc_sample,ID,V), - ( - V == 1 -> true - ; - V == 2 -> fail - ; - new_sample(ID) - ). -% (b) for non-ground facts (ID is FactID_GroundingID): check database of groundings in current sample -montecarlo_check(ComposedID) :- -% split_grounding_id(ComposedID,ID,GID), - recorded(mc_true,problog_mc_id(ComposedID),_), - !. -montecarlo_check(ComposedID) :- -% split_grounding_id(ComposedID,ID,GID), - recorded(mc_false,problog_mc_id(ComposedID),_), - !, - fail. -% (c) for unknown groundings of non-ground facts: generate a new sample (decompose the ID first) -montecarlo_check(ID) :- - name(ID,IDN), - recover_number(IDN,FactIDName), - name(FactID,FactIDName), - new_sample_nonground(ID,FactID). - -% sampling from ground fact: set array value to 1 (in) or 2 (out) -new_sample(ID) :- - get_fact_probability(ID,Prob), - problog_random(R), - R - get_fact(ID,Fact), - split_grounding_id(ComposedID,ID,GID), - grounding_is_known(Fact,GID), - dynamic_probability_fact_extract(Fact,Prob) - ; - get_fact_probability(ID,Prob) - ), - problog_random(R), - (R < Prob -> - recorda(mc_true,problog_mc_id(ComposedID),_) - ; - recorda(mc_false,problog_mc_id(ComposedID),_), - fail - ). -% new_sample_nonground(ComposedID,_ID) :- -% recorda(mc_false,problog_mc_id(ComposedID),_), -% fail. - -split_grounding_id(Composed,Fact,Grounding) :- - name(Composed,C), - split_g_id(C,F,G), - name(Fact,F), - name(Grounding,G). -split_g_id([95|Grounding],[],Grounding) :- !. -split_g_id([A|B],[A|FactID],GroundingID) :- - split_g_id(B,FactID,GroundingID). - - - -% if threshold reached, remember this by setting limit to on, then -% if up is on, store stopped derivation in second trie -% -% List always length>=1 -> don't need []=true-case for tries -upper_bound(List) :- - problog_control(on, limit), - problog_control(check, up), - nb_getval(problog_stopped_proofs, Trie_Stopped_Proofs), - open_end_close_end(List, R), -% (prune_check(R, Trie_Stopped_Proofs) -> true; insert_ptree(R, Trie_Stopped_Proofs)). - insert_ptree(R, Trie_Stopped_Proofs). - -multiply_probabilities(CurrentLogP, LogProb, NLogProb) :- - NLogProb is CurrentLogP + LogProb. - -% this is called by all inference methods before the actual ProbLog goal -% to set up environment for proving -% it resets control flags, method specific values to be set afterwards! -init_problog(Threshold) :- - reset_problog_proof_id, - reset_non_ground_facts, - reset_control, - LT is log(Threshold), - b_setval(problog_probability, 0.0), - b_setval(problog_current_proof, []), - nb_setval(problog_threshold, LT), - problog_flag(maxsteps,MaxS), - init_tabling, - problog_var_clear_all, - b_setval(problog_steps, MaxS), - b_setval(problog_continuous_facts_used,[]), - retractall(hybrid_proof(_,_,_)), - retractall(hybrid_proof(_,_,_,_)), - retractall(hybrid_proof_disjoint(_,_,_,_)). - -% idea: proofs that are refinements of known proof can be pruned as they don't add probability mass -% note that current ptree implementation doesn't provide the check as there's no efficient method known so far... -prune_check(Proof, Trie) :- - problog_flag(prunecheck, on), - prune_check_ptree(Proof, Trie). - -% to call a ProbLog goal, patch all subgoals with the user's module context -% (as logical part is there, but probabilistic part in problog) -problog_call(Goal) :- - yap_flag(typein_module, Module), -%%% if user provides init_db, call this before proving goal - (current_predicate(_,Module:init_db) -> call(Module:init_db); true), - put_module(Goal,Module,ModGoal), - call(ModGoal). - -put_module((Mod:Goal,Rest),Module,(Mod:Goal,Transformed)) :- - !, - put_module(Rest,Module,Transformed). -put_module((Goal,Rest),Module,(Module:Goal,Transformed)) :- - !, - put_module(Rest,Module,Transformed). -put_module((Mod:Goal),_Module,(Mod:Goal)) :- - !. -put_module(Goal,Module,Module:Goal). - -% end of core - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -% evaluating a DNF given as trie using BDD -% input: Trie the trie to be used -% output: probability and status (to catch potential failures/timeouts from outside) -% -% with internal BDD timeout (set using problog flag bdd_time) -% -% bdd_ptree/3 constructs files for problogbdd from the trie -% -% if calling ProblogBDD doesn't exit successfully, status will be timeout -% -% writes number of proofs in trie and BDD time to standard user output -% -% if remember is on, input files for problogbdd will be saved -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% - -:- initialization(( - problog_var_define(sld_time, times, time, messages('SLD resolution', ':', ' ms')), - problog_var_define(bdd_script_time, times, time, messages('Generating BDD script', ':', ' ms')), - problog_var_define(bdd_generation_time, times, time, messages('Constructing BDD', ':', ' ms')), - problog_var_define(trie_statistics, memory, untyped, messages('Trie usage', ':', '')), - problog_var_define(probability, result, number, messages('Probabilty', ' = ', '')), - problog_var_define(bdd_script_time(Method), times, time, messages('Generating BDD script '(Method), ':', ' ms')), - problog_var_define(bdd_generation_time(Method), times, time, messages('Constructing BDD '(Method), ':', ' ms')), - problog_var_define(probability(Method), result, number, messages('Probabilty '(Method), ' = ', '')), - problog_var_define(trie_statistics(Method), memory, untyped, messages('Trie usage '(Method), ':', '')), - problog_var_define(dbtrie_statistics(Method), memory, untyped, messages('Depth Breadth Trie usage '(Method), ':', '')), - problog_var_define(db_trie_opts_performed(Method), memory, untyped, messages('Optimisations performed '(Method), ':', '')), - problog_var_define(variable_elimination_time, times, time, messages('Variable Elimination', ':', ' ms')), - problog_var_define(variable_elimination_stats, memory, untyped, messages('Variable Elimination', ':', '')) -)). - -problog_statistics(Stat, Result):- - problog_var_defined(Stat), - problog_var_is_set(Stat), - problog_var_get(Stat, Result). - -generate_order_by_prob_fact_appearance(Order, FileName):- - open(FileName, 'write', Stream), - forall(member(PF, Order), ( - ptree:get_var_name(PF, Name), - format(Stream, "@~w~n", [Name]))), -/* findall(_, (recorded(variable_elimination, prob_fact(PF, _), _), - ptree:get_var_name(PF, Name), - format(Stream, "@~w~n", [Name])), _),*/ - close(Stream). - -get_order(Trie, Order):- - findall(List, ptree:traverse_ptree(Trie, List), Proofs), - flatten(Proofs, ProbFacts), - remove_duplicates(ProbFacts, Order). - - -eval_dnf(OriTrie1, Prob, Status) :- - % Check whether we use Hybrid ProbLog - ( - hybrid_proof(_,_,_) - -> - ( % Yes! run the disjoining stuff - retractall(hybrid_proof_disjoint(_,_,_,_)), - disjoin_hybrid_proofs, - - init_ptree(OriTrie), % use this as tmp ptree - %%%%%%%%%%%%%%%%%%%%% - ( % go over all stored proofs - enum_member_ptree(List,OriTrie1), - ( - List=[_|_] - -> - Proof=List; - Proof=[List] - ), - ( - select(continuous(ProofID),Proof,Rest) - -> - ( - % this proof is using continuous facts - all_hybrid_subproofs(ProofID,List2), - append(Rest,List2,NewProof), - insert_ptree(NewProof,OriTrie) - ); - insert_ptree(Proof,OriTrie) - ), - - fail; - true - ) - %%%%%%%%%%%%%%%%%%%%% - ) ; - % Nope, just pass on the Trie - OriTrie=OriTrie1 - ), - - - ((problog_flag(variable_elimination, true), nb_getval(problog_nested_tries, false)) -> - statistics(walltime, _), - trie_check_for_and_cluster(OriTrie), - statistics(walltime, [_, VariableEliminationTime]), - trie_replace_and_cluster(OriTrie, Trie), - problog_var_set(variable_elimination_time, VariableEliminationTime), - variable_elimination_stats(Clusters, OrigPF, CompPF), - problog_var_set(variable_elimination_stats, compress(Clusters, OrigPF, CompPF)), - clean_up - ; - Trie = OriTrie - ), - (problog_flag(bdd_static_order, true) -> - get_order(Trie, Order), - problog_flag(static_order_file, SOFName), - convert_filename_to_working_path(SOFName, SOFileName), - generate_order_by_prob_fact_appearance(Order, SOFileName) - ; - true - ), - ptree:trie_stats(Memory, Tries, Entries, Nodes), - (nb_getval(problog_nested_tries, false) -> - ptree:trie_usage(Trie, TEntries, TNodes, TVirtualNodes), - problog_var_set(trie_statistics, tries(memory(Memory), tries(Tries), entries(TEntries), nodes(TNodes), virtualnodes(TVirtualNodes))) - ; - problog_var_set(trie_statistics, tries(memory(Memory), tries(Tries), entries(Entries), nodes(Nodes))) - ), - (problog_flag(triedump, true) -> - convert_filename_to_working_path(trie_file, TrieFile), - tell(TrieFile), - print_nested_ptree(Trie), - flush_output, - told, - tell(user_output) - ; - true - ), - nb_getval(problog_completed_proofs, Trie_Completed_Proofs), - ((Trie = Trie_Completed_Proofs, problog_flag(save_bdd, true)) -> - problog_control(on, remember) - ; - problog_control(off, remember) - ), - problog_flag(bdd_file, BDDFileFlag), - convert_filename_to_working_path(BDDFileFlag, BDDFile), - problog_flag(bdd_par_file, BDDParFileFlag), - convert_filename_to_working_path(BDDParFileFlag, BDDParFile), - % old reduction method doesn't support nested tries - ((problog_flag(use_old_trie, true), nb_getval(problog_nested_tries, false)) -> - statistics(walltime, _), - (problog_control(check, remember) -> - bdd_ptree_map(Trie, BDDFile, BDDParFile, Mapping), - convert_filename_to_working_path(save_map, MapFile), - tell(MapFile), - format('mapping(~q).~n', [Mapping]), - flush_output, - told - ; - bdd_ptree(Trie, BDDFile, BDDParFile) - ), - statistics(walltime, [_, ScriptGenerationTime]), - problog_var_set(bdd_script_time, ScriptGenerationTime), - - statistics(walltime, _), - execute_bdd_tool(BDDFile, BDDParFile, Prob_old, Status_old), - statistics(walltime,[_, BDDGenerationTime]), - (Status_old = ok -> - problog_var_set(bdd_generation_time, BDDGenerationTime), - problog_var_set(probability, Prob_old) - ; - problog_var_set(bdd_generation_time, fail), - problog_var_set(probability, fail) - ) - ; - true - ), - % naive method with nested trie support but not loops - ((problog_flag(use_naive_trie, true); (problog_flag(use_old_trie, true), nb_getval(problog_nested_tries, true))) -> - statistics(walltime, _), -% atomic_concat([BDDFile, '_naive'], BDDFile_naive), - BDDFile = BDDFile_naive, - nested_ptree_to_BDD_script(Trie, BDDFile_naive, BDDParFile), - statistics(walltime, [_, ScriptGenerationTime_naive]), - problog_var_set(bdd_script_time(naive), ScriptGenerationTime_naive), - - statistics(walltime, _), - execute_bdd_tool(BDDFile_naive, BDDParFile, Prob_naive, Status_naive), - statistics(walltime,[_, BDDGenerationTime_naive]), - (Status_naive = ok -> - problog_var_set(bdd_generation_time(naive), BDDGenerationTime_naive), - problog_var_set(probability(naive), Prob_naive) - ; - problog_var_set(bdd_generation_time(naive), fail), - problog_var_set(probability(naive), fail) - ) - ; - true - ), -% problog_statistics, -% print_nested_ptree(Trie), -% findall(_,(problog_chktabled(_ID, _T), writeln(problog_chktabled(_ID, _T))),_), - % reduction method with depth_breadth trie support - problog_flag(db_trie_opt_lvl, ROptLevel), - problog_flag(db_min_prefix, MinPrefix), - - (problog_flag(compare_opt_lvl, true) -> - generate_ints(0, ROptLevel, Levels) - ; - Levels = [ROptLevel] - ), - forall(member(OptLevel, Levels), ( - (problog_flag(use_db_trie, true) -> - tries:trie_db_opt_min_prefix(MinPrefix), - statistics(walltime, _), -% atomic_concat([BDDFile, '_builtin_', OptLevel], BDDFile_builtin), - BDDFile = BDDFile_builtin, - (nb_getval(problog_nested_tries, false) -> - trie_to_bdd_trie(Trie, DBTrie, BDDFile_builtin, OptLevel, BDDParFile) - ; - nested_trie_to_bdd_trie(Trie, DBTrie, BDDFile_builtin, OptLevel, BDDParFile) - ), - atomic_concat(['builtin_', OptLevel], Builtin), - ptree:trie_stats(DBMemory, DBTries, DBEntries, DBNodes), - FM is DBMemory - Memory, - FT is DBTries - Tries, - FE is DBEntries - Entries, - FN is DBNodes - Nodes, - problog_var_set(dbtrie_statistics(Builtin), tries(memory(FM), tries(FT), entries(FE), nodes(FN))), - - delete_ptree(DBTrie), - statistics(walltime, [_, ScriptGenerationTime_builtin]), - problog_var_set(bdd_script_time(Builtin), ScriptGenerationTime_builtin), - - statistics(walltime, _), - execute_bdd_tool(BDDFile_builtin, BDDParFile, Prob_builtin, Status_builtin), - statistics(walltime,[_, BDDGenerationTime_builtin]), - ptree_db_trie_opt_performed(LVL1, LVL2, LV3), - problog_var_set(db_trie_opts_performed(Builtin), opt_perform(LVL1, LVL2, LV3)), - (Status_builtin = ok -> - problog_var_set(bdd_generation_time(Builtin), BDDGenerationTime_builtin), - problog_var_set(probability(Builtin), Prob_builtin) - ; - problog_var_set(bdd_generation_time(Builtin), fail), - problog_var_set(probability(Builtin), fail) - ) - ; - true - ) - )), - - % decomposition method - (problog_flag(use_dec_trie, true) -> - statistics(walltime, _), -% atomic_concat([BDDFile, '_dec'], BDDFile_dec), - BDDFile = BDDFile_dec, - ptree_decomposition(Trie, BDDFile_dec, BDDParFile), - statistics(walltime, [_, ScriptGenerationTime_dec]), - problog_var_set(bdd_script_time(dec), ScriptGenerationTime_dec), - - statistics(walltime, _), - execute_bdd_tool(BDDFile_dec, BDDParFile, Prob_dec, Status_dec), - statistics(walltime,[_, BDDGenerationTime_dec]), - (Status_dec = ok -> - problog_var_set(bdd_generation_time(dec), BDDGenerationTime_dec), - problog_var_set(probability(dec), Prob_dec) - ; - problog_var_set(bdd_generation_time(dec), fail), - problog_var_set(probability(dec), fail) - ) - ; - true - ), - - (problog_control(check, remember) -> - convert_filename_to_working_path('save_script', SaveBDDFile), - rename_file(BDDFile, SaveBDDFile), - convert_filename_to_working_path('save_params', SaveBDDParFile), - rename_file(BDDParFile, SaveBDDParFile) - ; - true - ), - problog_control(off, remember), - (var(Status_old)-> - (var(Status_naive)-> - (var(Status_dec) -> - atomic_concat('builtin_', ROptLevel, ProbStat), - problog_statistics(probability(ProbStat), ProbB), - (ProbB = fail -> - Status = timeout - ; - Prob = ProbB, - Status = ok - ) - ; - Prob = Prob_dec, - Status = Status_dec - ) - ; - Prob = Prob_naive, - Status = Status_naive - ) - ; - Prob = Prob_old, - Status = Status_old - ), - - (Trie =\= OriTrie -> - delete_ptree(Trie) - ; - true - ). - -generate_ints(End, End, [End]). -generate_ints(Start, End, [Start|Rest]):- - Start < End, - Current is Start + 1, - generate_ints(Current, End, Rest). - -execute_bdd_tool(BDDFile, BDDParFile, Prob, Status):- - problog_flag(bdd_time, BDDTime), - problog_flag(bdd_result, ResultFileFlag), - (problog_flag(dynamic_reorder, true) -> - ParamD = '' - ; - ParamD = ' -dreorder' - ), - (problog_flag(bdd_static_order, true) -> - problog_flag(static_order_file, FileName), - convert_filename_to_working_path(FileName, SOFileName), - atomic_concat([ParamD, ' -sord ', SOFileName], Param) - ; - Param = ParamD - ), - convert_filename_to_problog_path('problogbdd', ProblogBDD), - convert_filename_to_working_path(ResultFileFlag, ResultFile), - atomic_concat([ProblogBDD, Param,' -l ', BDDFile, ' -i ', BDDParFile, ' -m p -t ', BDDTime, ' > ', ResultFile], Command), - shell(Command, Return), - (Return =\= 0 -> - Status = timeout - ; - see(ResultFile), - read(probability(Prob)), - seen, - delete_file(ResultFile), - Status = ok - ). - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -% different inference methods -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -% approximate inference: bounds based on single probability threshold -% problog_threshold(+Goal,+Threshold,-LowerBound,-UpperBound,-Status) -% -% use backtracking over problog_call to get all solutions -% -% trie 1 collects proofs, trie 2 collects stopped derivations, trie 3 is used to unit them for the upper bound -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% - -problog_threshold(Goal, Threshold, _, _, _) :- - init_problog_threshold(Threshold), - problog_control(on,up), - problog_call(Goal), - add_solution, - fail. -problog_threshold(_, _, LP, UP, Status) :- - compute_bounds(LP, UP, Status). - -init_problog_threshold(Threshold) :- - init_ptree(Trie_Completed_Proofs), - nb_setval(problog_completed_proofs, Trie_Completed_Proofs), - init_ptree(Trie_Stopped_Proofs), - nb_setval(problog_stopped_proofs, Trie_Stopped_Proofs), - init_problog(Threshold). - -add_solution :- - % get the probabilistic facts used in this proof - b_getval(problog_current_proof, IDs), - (IDs == [] -> R = []; open_end_close_end(IDs, R)), - - % get the continuous facts used in this proof - % (Hybrid ProbLog - b_getval(problog_continuous_facts_used,Cont_IDs), - ( - Cont_IDs == [] - -> - Continuous=[]; - ( - proof_id(ProofID), - collect_all_intervals(Cont_IDs,ProofID,AllIntervals), - ( - AllIntervals==[] - -> - Continuous=[]; - ( - Continuous=[continuous(ProofID)], - assertz(hybrid_proof(ProofID,Cont_IDs,AllIntervals)) - ) - ) - ) - ), - - % we have both, no add it to the trie - nb_getval(problog_completed_proofs, Trie_Completed_Proofs), - append(R,Continuous,Final), - ( - Final==[] - -> - insert_ptree(true, Trie_Completed_Proofs); - insert_ptree(Final, Trie_Completed_Proofs) - ). - - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -% -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% - -collect_all_intervals([],_,[]). -collect_all_intervals([(ID,GroundID)|T],ProofID,[Interval|T2]) :- - atomic_concat([interval,'_',GroundID],Key), - b_getval(Key,Interval), - Interval \= all, % we do not need to store continuous - % variables with domain [-oo,oo] (they have probability 1) - !, - assertz(hybrid_proof(ProofID,ID,GroundID,Interval)), - collect_all_intervals(T,ProofID,T2). -collect_all_intervals([_|T],ProofID,T2) :- - collect_all_intervals(T,ProofID,T2). - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -% -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% - - -all_hybrid_subproofs(ProofID,List) :- - findall((ID,GroundID,Intervals),hybrid_proof_disjoint(ProofID,ID,GroundID,Intervals),All), - generate_all_proof_combinations(All,List). - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -% -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% - -generate_all_proof_combinations([],[]). -generate_all_proof_combinations([(_ID,GroundID,Intervals)|T],Result) :- - member((Interval,Tail),Intervals), - intervals_encode(Interval,IntervalEncoded), - atomic_concat([GroundID,IntervalEncoded],FullID), - encode_tail(Tail,GroundID,TailEncoded), - append([FullID|TailEncoded],T2,Result), - generate_all_proof_combinations(T,T2). - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -% -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% - -encode_tail([],_,[]). -encode_tail([A|T],ID,[not(FullID)|T2]) :- - intervals_encode(A,AEncoded), - atomic_concat([ID,AEncoded],FullID), - encode_tail(T,ID,T2). - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -% -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% - -disjoin_hybrid_proofs :- - % collect all used continuous facts - findall(GroundID,hybrid_proof(_,_,GroundID,_),IDs), - sort(IDs,IDsSorted), - - disjoin_hybrid_proofs(IDsSorted). - -disjoin_hybrid_proofs([]). -disjoin_hybrid_proofs([GroundID|T]) :- - findall(Interval,hybrid_proof(_,_,GroundID,Interval),Intervals), - intervals_partition(Intervals,Partition), - - % go over all proofs where this fact occurs - ( - hybrid_proof(ProofID,ID,GroundID,Interval), - intervals_disjoin(Interval,Partition,PInterval), - assertz(hybrid_proof_disjoint(ProofID,ID,GroundID,PInterval)), - - fail; % go to next proof - true - ), - - disjoin_hybrid_proofs(T). - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -% -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% - -% End Hybrid - -compute_bounds(LP, UP, Status) :- - nb_getval(problog_completed_proofs, Trie_Completed_Proofs), - nb_getval(problog_stopped_proofs, Trie_Stopped_Proofs), - eval_dnf(Trie_Completed_Proofs, LP, StatusLow), - (StatusLow \== ok -> - Status = StatusLow - ; - merge_ptree(Trie_Completed_Proofs, Trie_Stopped_Proofs, Trie_All_Proofs), - nb_setval(problog_all_proofs, Trie_All_Proofs), - eval_dnf(Trie_All_Proofs, UP, Status)), - delete_ptree(Trie_Completed_Proofs), - delete_ptree(Trie_Stopped_Proofs), - delete_ptree(Trie_All_Proofs). - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -% approximate inference: lower bound based on all proofs above probability threshold -% problog_low(+Goal,+Threshold,-LowerBound,-Status) -% -% same as problog_threshold/5, but lower bound only (no stopped derivations stored) -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% - - -problog_low(Goal, Threshold, _, _) :- - init_problog_low(Threshold), - problog_control(off, up), - statistics(walltime, _), - problog_call(Goal), - add_solution, - fail. -problog_low(_, _, LP, Status) :- - statistics(walltime, [_,E]), %theo - problog_var_set(sld_time, E), - nb_getval(problog_completed_proofs, Trie_Completed_Proofs), - eval_dnf(Trie_Completed_Proofs, LP, Status), - (problog_flag(verbose, true)-> - problog_statistics - ; - true - ), - delete_ptree(Trie_Completed_Proofs), - (problog_flag(retain_tables, true) -> retain_tabling; true), - clear_tabling. - -init_problog_low(Threshold) :- - init_ptree(Trie_Completed_Proofs), - nb_setval(problog_completed_proofs, Trie_Completed_Proofs), - init_problog(Threshold). - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -% approximate inference: bounds by iterative deepening up to interval width Delta -% problog_delta(+Goal,+Delta,-LowerBound,-UpperBound,-Status) -% -% wraps iterative deepening around problog_threshold, i.e. -% - starts with threshold given by first_threshold flag -% - if Up-Low >= Delta, multiply threshold by factor given in id_stepsize flag and iterate -% (does not use problog_threshold as trie 1 is kept over entire search) -% -% local dynamic predicates low/2, up/2, stopDiff/1 -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% - -problog_delta(Goal, Delta, Low, Up, Status) :- - problog_flag(first_threshold,InitT), - init_problog_delta(InitT,Delta), - problog_control(on,up), - problog_delta_id(Goal,Status), - nb_getval(problog_completed_proofs, Trie_Completed_Proofs), - nb_getval(problog_stopped_proofs, Trie_Stopped_Proofs), - delete_ptree(Trie_Completed_Proofs), - delete_ptree(Trie_Stopped_Proofs), - (retract(low(_,Low)) -> true; true), - (retract(up(_,Up)) -> true; true). - - -init_problog_delta(Threshold,Delta) :- - retractall(low(_,_)), - retractall(up(_,_)), - retractall(stopDiff(_)), - init_ptree(Trie_Completed_Proofs), - nb_setval(problog_completed_proofs, Trie_Completed_Proofs), - init_ptree(Trie_Stopped_Proofs), - nb_setval(problog_stopped_proofs, Trie_Stopped_Proofs), - assertz(low(0,0.0)), - assertz(up(0,1.0)), - assertz(stopDiff(Delta)), - init_problog(Threshold). - -problog_delta_id(Goal, _) :- - problog_call(Goal), - add_solution, % reused from problog_threshold - fail. -problog_delta_id(Goal, Status) :- - evaluateStep(Ans,StatusE), - problog_flag(last_threshold_log,Stop), - nb_getval(problog_threshold,Min), - (StatusE \== ok -> - Status = StatusE - ; - ( - Ans = 1 -> - Status = ok - ; - Min =< Stop -> - Status = stopreached - ; - problog_control(check,limit) -> - problog_control(off,limit), - problog_flag(id_stepsize_log,Step), - New is Min+Step, - nb_setval(problog_threshold,New), - problog_delta_id(Goal, Status) - ; - true - )). - -% call the dnf evaluation where needed -evaluateStep(Ans,Status) :- once(evalStep(Ans,Status)). - -evalStep(Ans,Status) :- - stopDiff(Delta), - nb_getval(problog_completed_proofs, Trie_Completed_Proofs), - nb_getval(problog_stopped_proofs, Trie_Stopped_Proofs), - count_ptree(Trie_Completed_Proofs, NProofs), - count_ptree(Trie_Stopped_Proofs, NCands), - ( - problog_flag(verbose,true) - -> - format(user,'~w proofs, ~w stopped derivations~n',[NProofs,NCands]); - true - ), - flush_output(user), - eval_lower(NProofs,Low,StatusLow), - ( - StatusLow \== ok - -> - Status = StatusLow; - up(_, OUP), - IntDiff is OUP-Low, - ((IntDiff < Delta; IntDiff =:= 0) -> - Up = OUP, - StatusUp = ok - ; - eval_upper(NCands, Up, StatusUp), - delete_ptree(Trie_Stopped_Proofs), - init_ptree(New_Trie_Stopped_Proofs), - nb_setval(problog_stopped_proofs, New_Trie_Stopped_Proofs) - ), - (StatusUp \== ok -> - Status = StatusUp - ; - Diff is Up-Low, - (problog_flag(verbose,true) -> format(user,'difference: ~6f~n',[Diff]);true), - flush_output(user), - ((Diff < Delta; Diff =:= 0) -> Ans = 1; Ans = 0), - Status = ok - ) - ). - -% no need to re-evaluate if no new proofs found on this level -eval_lower(N,P,ok) :- - low(N,P). -% evaluate if there are proofs -eval_lower(N,P,Status) :- - N > 0, - low(OldN,_), - N \= OldN, - nb_getval(problog_completed_proofs, Trie_Completed_Proofs), - eval_dnf(Trie_Completed_Proofs,P,Status), - (Status = ok -> - retract(low(_,_)), - assertz(low(N,P)), - (problog_flag(verbose,true) -> format(user,'lower bound: ~6f~n',[P]);true), - flush_output(user) - ; - true). - -% if no stopped derivations, up=low -eval_upper(0,P,ok) :- - retractall(up(_,_)), - low(N,P), - assertz(up(N,P)). -% else merge proofs and stopped derivations to get upper bound -% in case of timeout or other problems, skip and use bound from last level -eval_upper(N,UpP,ok) :- - N > 0, - nb_getval(problog_completed_proofs, Trie_Completed_Proofs), - nb_getval(problog_stopped_proofs, Trie_Stopped_Proofs), - merge_ptree(Trie_Completed_Proofs,Trie_Stopped_Proofs,Trie_All_Proofs), - nb_setval(problog_all_proofs, Trie_All_Proofs), - eval_dnf(Trie_All_Proofs,UpP,StatusUp), - delete_ptree(Trie_All_Proofs), - (StatusUp = ok -> - retract(up(_,_)), - assertz(up(N,UpP)) - ; - (problog_flag(verbose,true) -> format(user,'~w - continue using old up~n',[StatusUp]);true), - flush_output(user), - up(_,UpP) - ). - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -% explanation probability - returns list of facts used or constant 'unprovable' as third argument -% problog_max(+Goal,-Prob,-Facts) -% -% uses iterative deepening with samw parameters as bounding algorithm -% threshold gets adapted whenever better proof is found -% -% uses local dynamic predicates max_probability/1 and max_proof/1 -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% - -problog_max(Goal, Prob, Facts) :- - problog_flag(first_threshold,InitT), - init_problog_max(InitT), - problog_control(off,up), - problog_max_id(Goal, Prob, FactIDs),% theo todo - ( FactIDs = [_|_] -> get_fact_list(FactIDs, Facts); - Facts = FactIDs). - -init_problog_max(Threshold) :- - retractall(max_probability(_)), - retractall(max_proof(_)), - assertz(max_probability(-999999)), - assertz(max_proof(unprovable)), - init_problog(Threshold). - -update_max :- - b_getval(problog_probability, CurrP), - max_probability(MaxP), - (CurrP =< MaxP -> - fail - ; - b_getval(problog_current_proof, IDs), - open_end_close_end(IDs, R), - retractall(max_proof(_)), - assertz(max_proof(R)), - nb_setval(problog_threshold, CurrP), - retractall(max_probability(_)), - assertz(max_probability(CurrP)) - ). - -problog_max_id(Goal, _Prob, _Clauses) :- - problog_call(Goal), - update_max, - fail. -problog_max_id(Goal, Prob, Clauses) :- - max_probability(MaxP), - nb_getval(problog_threshold, LT), - problog_flag(last_threshold_log, ToSmall), - ((MaxP >= LT; \+ problog_control(check, limit); LT < ToSmall) -> - ((max_proof(unprovable), problog_control(check,limit), LT < ToSmall) -> - problog_flag(last_threshold, Stopping), - Clauses = unprovable(Stopping) - ; - max_proof(Clauses) - ), - Prob is exp(MaxP) - ; - problog_flag(id_stepsize_log, Step), - NewLT is LT + Step, - nb_setval(problog_threshold, NewLT), - problog_control(off, limit), - problog_max_id(Goal, Prob, Clauses) - ). - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -% lower bound using k best proofs -% problog_kbest(+Goal,+K,-Prob,-Status) -% -% does iterative deepening search similar to problog_max, but for k(>=1) most likely proofs -% afterwards uses BDD evaluation to calculate probability (also for k=1 -> uniform treatment in learning) -% -% uses dynamic local predicate current_kbest/3 to collect proofs, -% only builds trie at the end (as probabilities of single proofs are important here) -% -% note: >k proofs will be used if the one at position k shares its probability with others, -% as all proofs with that probability will be included -% -% version with _save at the end renames files for problogbdd to keep them -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -problog_kbest_save(Goal, K, Prob, Status, BDDFile, ParamFile) :- - problog_flag(dir, InternWorkingDir), - problog_flag(bdd_file, InternBDDFlag), - problog_flag(bdd_par_file, InternParFlag), - split_path_file(BDDFile, WorkingDir, BDDFileName), - split_path_file(ParamFile, _WorkingDir, ParamFileName), - flag_store(dir, WorkingDir), - flag_store(bdd_file, BDDFileName), - flag_store(bdd_par_file, ParamFileName), - problog_kbest(Goal, K, Prob, Status), - flag_store(dir, InternWorkingDir), - flag_store(bdd_file, InternBDDFlag), - flag_store(bdd_par_file, InternParFlag). -% ( Status=ok -> -% problog_flag(bdd_file,InternBDDFlag), -% problog_flag(bdd_par_file,InternParFlag), -% convert_filename_to_working_path(InternBDDFlag, InternBDD), -% convert_filename_to_working_path(InternParFlag, InternPar), -% rename_file(InternBDD,BDDFile), -% rename_file(InternPar,ParamFile) -% ; -% true). - -problog_kbest(Goal, K, Prob, Status) :- - problog_flag(first_threshold,InitT), - init_problog_kbest(InitT), - problog_control(off,up), - problog_kbest_id(Goal, K), - retract(current_kbest(_,ListFound,_NumFound)), - build_prefixtree(ListFound), - nb_getval(problog_completed_proofs, Trie_Completed_Proofs), - eval_dnf(Trie_Completed_Proofs,Prob,Status), - delete_ptree(Trie_Completed_Proofs). - -problog_real_kbest(Goal, K, Prob, Status) :- - problog_flag(first_threshold,InitT), - init_problog_kbest(InitT), - problog_control(off,up), - problog_kbest_id(Goal, K), - retract(current_kbest(_,RawListFound,NumFound)), - % limiting the number of proofs is not only needed for fast SLD resolution but also for fast BDD building. - % one can't assume that kbest is called for the former and not for the latter - take_k_best(RawListFound,K,NumFound,ListFound), - build_prefixtree(ListFound), - nb_getval(problog_completed_proofs, Trie_Completed_Proofs), - eval_dnf(Trie_Completed_Proofs,Prob,Status), - delete_ptree(Trie_Completed_Proofs). - -init_problog_kbest(Threshold) :- - retractall(current_kbest(_,_,_)), - assertz(current_kbest(-999999,[],0)), %(log-threshold,proofs,num_proofs) - init_ptree(Trie_Completed_Proofs), - nb_setval(problog_completed_proofs, Trie_Completed_Proofs), - init_problog(Threshold). - -problog_kbest_id(Goal, K) :- - problog_call(Goal), - update_kbest(K), - fail. -problog_kbest_id(Goal, K) :- - current_kbest(CurrentBorder,_,Found), - nb_getval(problog_threshold, Min), - problog_flag(last_threshold_log,ToSmall), - ((Found>=K ; \+ problog_control(check,limit) ; Min < CurrentBorder ; Min < ToSmall) -> - true - ; - problog_flag(id_stepsize_log,Step), - NewLT is Min+Step, - nb_setval(problog_threshold, NewLT), - problog_control(off,limit), - problog_kbest_id(Goal, K)). - -update_kbest(K) :- - b_getval(problog_probability,NewLogProb), - current_kbest(LogThreshold,_,_), - (NewLogProb>=LogThreshold -> - b_getval(problog_current_proof,RevProof), - open_end_close_end(RevProof,Proof), - update_current_kbest(K,NewLogProb,Proof) - ; - fail). - -update_current_kbest(_,NewLogProb,Cl) :- - current_kbest(_,List,_), - memberchk(NewLogProb-Cl,List), - !. -update_current_kbest(K,NewLogProb,Cl) :- - retract(current_kbest(OldThres,List,Length)), - sorted_insert(NewLogProb-Cl,List,NewList), - NewLength is Length+1, - (NewLength < K -> - assertz(current_kbest(OldThres,NewList,NewLength)) - ; - (NewLength>K -> - First is NewLength-K+1, - cutoff(NewList,NewLength,First,FinalList,FinalLength) - ; FinalList=NewList, FinalLength=NewLength), - FinalList=[NewThres-_|_], - nb_setval(problog_threshold,NewThres), - assertz(current_kbest(NewThres,FinalList,FinalLength))). - -sorted_insert(A,[],[A]). -sorted_insert(A-LA,[B1-LB1|B], [A-LA,B1-LB1|B] ) :- - A =< B1. -sorted_insert(A-LA,[B1-LB1|B], [B1-LB1|C] ) :- - A > B1, - sorted_insert(A-LA,B,C). - -% keeps all entries with lowest probability, even if implying a total of more than k -cutoff(List,Len,1,List,Len) :- !. -cutoff([P-L|List],Length,First,[P-L|List],Length) :- - nth(First,[P-L|List],PF-_), - PF=:=P, - !. -cutoff([_|List],Length,First,NewList,NewLength) :- - NextFirst is First-1, - NextLength is Length-1, - cutoff(List,NextLength,NextFirst,NewList,NewLength). - -build_prefixtree([]). -build_prefixtree([_-[]|_List]) :- - !, - nb_getval(problog_completed_proofs, Trie_Completed_Proofs), - insert_ptree(true,Trie_Completed_Proofs). -build_prefixtree([LogP-L|List]) :- - ( - problog_flag(show_proofs,true) - -> - get_fact_list(L,ListOfFacts), - P is exp(LogP), - format(user,'~q ~q~n',[P,ListOfFacts]) - ; - true - ), - nb_getval(problog_completed_proofs, Trie_Completed_Proofs), - insert_ptree(L,Trie_Completed_Proofs), - build_prefixtree(List). - -take_k_best(In,K,OutOf,Out) :- - ( - K>=OutOf - -> - In = Out; - In = [_|R], - OutOf2 is OutOf-1, - take_k_best(R,K,OutOf2,Out) - ). - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -% exact probability -% problog_exact(+Goal,-Prob,-Status) -% -% using all proofs = using all proofs with probability > 0 -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% - -problog_exact(Goal,Prob,Status) :- - problog_control(on, exact), - problog_low(Goal,0,Prob,Status), - problog_control(off, exact). - -problog_exact_save(Goal,Prob,Status,BDDFile,ParamFile) :- - problog_flag(dir, InternWorkingDir), - problog_flag(bdd_file, InternBDDFlag), - problog_flag(bdd_par_file, InternParFlag), - split_path_file(BDDFile, WorkingDir, BDDFileName), - split_path_file(ParamFile, _WorkingDir, ParamFileName), - flag_store(dir, WorkingDir), - flag_store(bdd_file, BDDFileName), - flag_store(bdd_par_file, ParamFileName), - problog_control(on, exact), - problog_low(Goal,0,Prob,Status), - problog_control(off, exact), - flag_store(dir, InternWorkingDir), - flag_store(bdd_file, InternBDDFlag), - flag_store(bdd_par_file, InternParFlag). -% ( -% Status==ok -% -> -% ( -% problog_flag(bdd_file,InternBDDFlag), -% problog_flag(bdd_par_file,InternParFlag), -% problog_flag(dir,DirFlag), -% atomic_concat([DirFlag,InternBDDFlag],InternBDD), -% atomic_concat([DirFlag,InternParFlag],InternPar), -% rename_file(InternBDD,BDDFile), -% rename_file(InternPar,ParamFile) -% ); -% true -% ). - -problog_collect_trie(Goal):- - problog_call(Goal), - add_solution, - fail. -problog_collect_trie(_Goal). - -problog_save_state(State):- - nb_getval(problog_completed_proofs, Trie_Completed_Proofs), - nb_getval(problog_current_proof, IDs), - recordz(problog_stack, store(Trie_Completed_Proofs, IDs), State), - init_ptree(Sub_Trie_Completed_Proofs), - nb_setval(problog_completed_proofs, Sub_Trie_Completed_Proofs), - nb_setval(problog_current_proof, []). - -problog_restore_state(State):- - recorded(problog_stack, store(Trie_Completed_Proofs, IDs), State), - erase(State), - nb_setval(problog_completed_proofs, Trie_Completed_Proofs), - nb_setval(problog_current_proof, IDs). - -problog_exact_nested(Goal, Prob, Status):- - problog_save_state(State), - problog_collect_trie(Goal), - nb_getval(problog_completed_proofs, Trie_Completed_Proofs), -/* writeln(Goal), - print_nested_ptree(Trie_Completed_Proofs),*/ - eval_dnf(Trie_Completed_Proofs, Prob, Status), - delete_ptree(Trie_Completed_Proofs), - problog_restore_state(State). - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -% probability by sampling: -% running another N samples until 95percentCI-width format('search for ~q~n',[Goal]);true), - montecarlo(Goal,Delta,K,0,File,0,T1), - problog_control(off,mc). - -% calculate values after K samples -montecarlo(Goal,Delta,K,SamplesSoFar,File,PositiveSoFar,InitialTime) :- - SamplesNew is SamplesSoFar+1, - SamplesNew mod K =:= 0, - !, - copy_term(Goal,GoalC), - (mc_prove(GoalC) -> Next is PositiveSoFar+1; Next=PositiveSoFar), - Prob is Next/SamplesNew, - statistics(walltime,[T2,_]), - Time is (T2-InitialTime),%/1000, - - problog_convergence_check(Time, Prob, SamplesNew, Delta, _Epsilon, Converged), - ((Converged = true; Converged = terminate) -> - (problog_flag(verbose,true) -> - format('Runtime ~w ms~2n',[Time]) - ; - true - ), - assertz(mc_prob(Prob)) - ; - montecarlo(Goal,Delta,K,SamplesNew,File,Next,InitialTime) - ). - - -% Epsilon is 2*sqrt(Prob*(1-Prob)/SamplesNew), -% Low is Prob-Epsilon, -% High is Prob+Epsilon, -% Diff is 2*Epsilon, -% (problog_flag(verbose,true) -> format('~n~w samples~nestimated probability ~w~n95 percent confidence interval [~w,~w]~n',[SamplesNew,Prob,Low,High]);true), -% open(File,append,Log), -% format(Log,'~w ~8f ~8f ~8f ~8f ~3f~n',[SamplesNew,Prob,Low,High,Diff,Time]), -% close(Log), - - -% ((Diff -% (problog_flag(verbose,true) -> -% format('Runtime ~w sec~2n',[Time]) -% ; -% true -% ), -% assertz(mc_prob(Prob)) -% ; -% montecarlo(Goal,Delta,K,SamplesNew,File,Next,InitialTime) -% ). - -% continue until next K samples done -montecarlo(Goal,Delta,K,SamplesSoFar,File,PositiveSoFar,InitialTime) :- - SamplesNew is SamplesSoFar+1, - copy_term(Goal,GoalC), - (mc_prove(GoalC) -> Next is PositiveSoFar+1; Next=PositiveSoFar), - montecarlo(Goal,Delta,K,SamplesNew,File,Next,InitialTime). - -mc_prove(A) :- !, - (get_some_proof(A) -> - clean_sample - ; - clean_sample,fail - ). - -clean_sample :- - reset_static_array(mc_sample), - eraseall(mc_true), - eraseall(mc_false), - reset_non_ground_facts, -% problog_abolish_all_tables. - problog_tabled(P), - problog_abolish_table(P), - fail. -clean_sample. - -% find new proof -- need to reset control after init -get_some_proof(Goal) :- - init_problog(0), - problog_control(on,mc), - problog_call(Goal). - - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -% exact probability of all ground instances of Goal -% output goes to File -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -problog_answers(Goal,File) :- - set_problog_flag(verbose,false), - retractall(answer(_)), -% this will not give the exact prob of Goal! - problog_exact((Goal,ground(Goal),\+problog:answer(Goal),assertz(problog:answer(Goal))),_,_), - open(File,write,_,[alias(answer)]), - eval_answers, - close(answer). - -eval_answers :- - retract(answer(G)), - problog_exact(G,P,_), - format(answer,'answer(~q,~w).~n',[G,P]), - fail. -eval_answers. - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -% find k most likely different answers (using their explanation prob as score) -% largely copied+adapted from kbest, uses same dynamic predicate -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -problog_kbest_answers(Goal,K,ResultList) :- - problog_flag(first_threshold,InitT), - init_problog_kbest(InitT), - nb_getval(problog_completed_proofs, Trie_Completed_Proofs), - delete_ptree( Trie_Completed_Proofs), % this is just because we reuse init from kbest and don't need the tree - problog_control(off,up), - problog_kbest_answers_id(Goal, K), - retract(current_kbest(_,LogResultList,_NumFound)), - transform_loglist_to_result(LogResultList,ResultList). - -problog_kbest_answers_id(Goal, K) :- - problog_call(Goal), - copy_term(Goal,GoalCopy), % needed? - update_kbest_answers(GoalCopy,K), - fail. -problog_kbest_answers_id(Goal, K) :- - current_kbest(CurrentBorder,_,Found), - nb_getval(problog_threshold, Min), - problog_flag(last_threshold_log,ToSmall), - ((Found>=K ; \+ problog_control(check,limit) ; Min < CurrentBorder ; Min < ToSmall) -> - true - ; - problog_flag(id_stepsize_log,Step), - NewLT is Min+Step, - nb_setval(problog_threshold, NewLT), - problog_control(off,limit), - problog_kbest_answers_id(Goal, K)). - -update_kbest_answers(Goal,K) :- - b_getval(problog_probability,NewLogProb), - current_kbest(LogThreshold,_,_), - (NewLogProb>=LogThreshold -> - update_current_kbest_answers(K,NewLogProb,Goal) - ; - fail). - -update_current_kbest_answers(_,NewLogProb,Goal) :- - current_kbest(_,List,_), - update_prob_of_known_answer(List,Goal,NewLogProb,NewList), - !, - keysort(NewList,SortedList),%format(user_error,'updated variant of ~w~n',[Goal]), - retract(current_kbest(K,_,Len)), - assertz(current_kbest(K,SortedList,Len)). -update_current_kbest_answers(K,NewLogProb,Goal) :- - retract(current_kbest(OldThres,List,Length)), - sorted_insert(NewLogProb-Goal,List,NewList),%format(user_error,'inserted new element ~w~n',[Goal]), - NewLength is Length+1, - (NewLength < K -> - assertz(current_kbest(OldThres,NewList,NewLength)) - ; - (NewLength>K -> - First is NewLength-K+1, - cutoff(NewList,NewLength,First,FinalList,FinalLength) - ; FinalList=NewList, FinalLength=NewLength), - FinalList=[NewThres-_|_], - nb_setval(problog_threshold,NewThres), - assertz(current_kbest(NewThres,FinalList,FinalLength))). - -% this fails if there is no variant -> go to second case above -update_prob_of_known_answer([OldLogP-OldGoal|List],Goal,NewLogProb,[MaxLogP-OldGoal|List]) :- - variant(OldGoal,Goal), - !, - MaxLogP is max(OldLogP,NewLogProb). -update_prob_of_known_answer([First|List],Goal,NewLogProb,[First|NewList]) :- - update_prob_of_known_answer(List,Goal,NewLogProb,NewList). - -transform_loglist_to_result(In,Out) :- - transform_loglist_to_result(In,[],Out). -transform_loglist_to_result([],Acc,Acc). -transform_loglist_to_result([LogP-G|List],Acc,Result) :- - P is exp(LogP), - transform_loglist_to_result(List,[P-G|Acc],Result). - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -% GENERAL PURPOSE PREDICATES FOR DTPROBLOG -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -% Do inference of a single goal, using the default inference method -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% - -problog_infer(Goal,Prob) :- - problog_flag(inference,Method), - problog_infer(Method,Goal,Prob). - -problog_infer(exact,Goal,Prob) :- - problog_exact(Goal,Prob,ok). -problog_infer(atleast-K-best,Goal,Prob) :- - problog_kbest(Goal,K,Prob,ok). -problog_infer(K-best,Goal,Prob) :- - problog_real_kbest(Goal,K,Prob,ok). -problog_infer(montecarlo(Confidence),Goal,Prob) :- - problog_montecarlo(Goal,Confidence,Prob). -problog_infer(delta(Width),Goal,Prob) :- - problog_delta(Goal,Width,Bound_low,Bound_up,ok), - Prob is 0.5*(Bound_low+Bound_up). -problog_infer(low(Threshold),Goal,Prob) :- - problog_low(Goal,Threshold,Prob,ok). -problog_infer(threshold(Threshold),Goal,Prob) :- - problog_threshold(Goal,Threshold,Bound_low,Bound_up,ok), - Prob is 0.5*(Bound_low+Bound_up). - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -% Do inference of a set of queries, using the default inference method -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% - -problog_infer_forest([],[]) :- !. -problog_infer_forest(Goals,Probs) :- - (problog_infer_forest_supported -> - problog_bdd_forest(Goals), - length(Goals,N), - eval_bdd_forest(N,Probs,ok) - ; - throw(error('Flag settings not supported by problog_infer_forest/1.')) - ). - -problog_infer_forest_supported :- problog_bdd_forest_supported. - -eval_bdd_forest(N,Probs,Status) :- - bdd_files(BDDFile,BDDParFile), - problog_flag(bdd_time,BDDTime), - (problog_flag(dynamic_reorder, true) -> - ParamD = '' - ; - ParamD = ' -dreorder' - ), - (problog_flag(bdd_static_order, true) -> - problog_flag(static_order_file, FileName), - convert_filename_to_working_path(FileName, SOFileName), - atomic_concat([ParamD, ' -sord ', SOFileName], Param) - ; - Param = ParamD - ), - convert_filename_to_problog_path('problogbdd', ProblogBDD), - problog_flag(bdd_result,ResultFileFlag), - convert_filename_to_working_path(ResultFileFlag, ResultFile), - atomic_concat([ProblogBDD, Param,' -l ', BDDFile, ' -i ', BDDParFile, ' -m p -t ', BDDTime, ' > ', ResultFile], Command), - statistics(walltime,_), - shell(Command,Return), - (Return =\= 0 -> - Status = timeout - ; - statistics(walltime,[_,E3]), - (problog_flag(verbose,true) -> format(user,'~w ms BDD processing~n',[E3]);true), - see(ResultFile), - read_probs(N,Probs), - seen, - Status = ok, - % cleanup - % TODO handle flag for keeping files - (problog_flag(save_bdd,true) -> - true - ; - delete_file(BDDFile), - delete_file(BDDParFile), - delete_file(ResultFile), - delete_bdd_forest_files(N) - ) - ). - -read_probs(N,Probs) :- - (N = 0 -> - Probs = [] - ; - Probs = [Prob|Rest], - read(probability(Prob)), - N2 is N-1, - read_probs(N2,Rest) - ). - -delete_bdd_forest_files(N) :- - (N=0 -> - true - ; - bdd_forest_file(N,BDDFile), - delete_file(BDDFile,[]), - N2 is N-1, - delete_bdd_forest_files(N2) - ). - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -% Build a trie using the default inference method -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% - -build_trie(Goal, Trie) :- - (build_trie_supported -> - problog_flag(inference,Method), - once(build_trie(Method, Goal, Trie)) - ; - throw(error('Flag settings not supported by build_trie/2.')) - ). - -build_trie_supported :- problog_flag(inference,exact). -build_trie_supported :- problog_flag(inference,low(_)). -build_trie_supported :- problog_flag(inference,atleast-_-best). -build_trie_supported :- problog_flag(inference,_-best). - -build_trie(exact, Goal, Trie) :- - problog_control(on, exact), - build_trie(low(0), Goal, Trie), - problog_control(off, exact). - -build_trie(low(Threshold), Goal, _) :- - number(Threshold), - init_problog_low(Threshold), - problog_control(off, up), - statistics(walltime, _), - problog_call(Goal), - add_solution, - fail. -build_trie(low(Threshold), _, Trie) :- - number(Threshold), - statistics(walltime, [_,E]), - problog_var_set(sld_time, E), - nb_getval(problog_completed_proofs, Trie). - % don't clear tabling; tables can be reused by other query - -build_trie(atleast-K-best, Goal, Trie) :- - number(K), - problog_flag(first_threshold,InitT), - init_problog_kbest(InitT), - problog_control(off,up), - problog_kbest_id(Goal, K), - retract(current_kbest(_,ListFound,_NumFound)), - build_prefixtree(ListFound), - nb_getval(problog_completed_proofs, Trie), - clear_tabling. % clear tabling because tables cannot be reused by other query - - -build_trie(K-best, Goal, Trie) :- - number(K), - problog_flag(first_threshold,InitT), - init_problog_kbest(InitT), - problog_control(off,up), - problog_kbest_id(Goal, K), - retract(current_kbest(_,RawListFound,NumFound)), - % limiting the number of proofs is not only needed for fast SLD resolution but also for fast BDD building. - % one can't assume that kbest is called for the former and not for the latter - % thus, we take EXACTLY k proofs - take_k_best(RawListFound,K,NumFound,ListFound), - build_prefixtree(ListFound), - nb_getval(problog_completed_proofs, Trie), - clear_tabling. % clear tabling because tables cannot be reused by other query - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -% Write BDD structure script for a trie and list all variables used -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% - -write_bdd_struct_script(Trie,BDDFile,Variables) :- - % Check whether we use Hybrid ProbLog - ( - hybrid_proof(_,_,_) - -> - ( % Yes! run the disjoining stuff - retractall(hybrid_proof_disjoint(_,_,_,_)), - disjoin_hybrid_proofs, - - init_ptree(OriTrie), % use this as tmp ptree - %%%%%%%%%%%%%%%%%%%%% - ( % go over all stored proofs - enum_member_ptree(List,OriTrie1), - ( - List=[_|_] - -> - Proof=List; - Proof=[List] - ), - ( - select(continuous(ProofID),Proof,Rest) - -> - ( - % this proof is using continuous facts - all_hybrid_subproofs(ProofID,List2), - append(Rest,List2,NewProof), - insert_ptree(NewProof,OriTrie) - ); - insert_ptree(Proof,OriTrie) - ), - - fail; - true - ) - %%%%%%%%%%%%%%%%%%%%% - ) ; - % Nope, just pass on the Trie - OriTrie=OriTrie1 - ), - - ((problog_flag(variable_elimination, true), nb_getval(problog_nested_tries, false)) -> - statistics(walltime, _), - trie_check_for_and_cluster(OriTrie), - statistics(walltime, [_, VariableEliminationTime]), - trie_replace_and_cluster(OriTrie, Trie), - problog_var_set(variable_elimination_time, VariableEliminationTime), - variable_elimination_stats(Clusters, OrigPF, CompPF), - problog_var_set(variable_elimination_stats, compress(Clusters, OrigPF, CompPF)), - clean_up - ; - Trie = OriTrie - ), - (problog_flag(bdd_static_order, true) -> - get_order(Trie, Order), - problog_flag(static_order_file, SOFName), - convert_filename_to_working_path(SOFName, SOFileName), - generate_order_by_prob_fact_appearance(Order, SOFileName) - ; - true - ), - ptree:trie_stats(Memory, Tries, Entries, Nodes), - (nb_getval(problog_nested_tries, false) -> - ptree:trie_usage(Trie, TEntries, TNodes, TVirtualNodes), - problog_var_set(trie_statistics, tries(memory(Memory), tries(Tries), entries(TEntries), nodes(TNodes), virtualnodes(TVirtualNodes))) - ; - problog_var_set(trie_statistics, tries(memory(Memory), tries(Tries), entries(Entries), nodes(Nodes))) - ), - (problog_flag(triedump, true) -> - convert_filename_to_working_path(trie_file, TrieFile), - tell(TrieFile), - print_nested_ptree(Trie), - flush_output, - told, - tell(user_output) - ; - true - ), - nb_getval(problog_completed_proofs, Trie_Completed_Proofs), - ((Trie = Trie_Completed_Proofs, problog_flag(save_bdd, true)) -> - problog_control(on, remember) - ; - problog_control(off, remember) - ), - % old reduction method doesn't support nested tries - ((problog_flag(use_old_trie, true), nb_getval(problog_nested_tries, false)) -> - statistics(walltime, _), - (problog_control(check, remember) -> - bdd_struct_ptree_map(Trie, BDDFile, Variables, Mapping), - convert_filename_to_working_path(save_map, MapFile), - tell(MapFile), - format('mapping(~q).~n', [Mapping]), - flush_output, - told - ; - bdd_struct_ptree(Trie, BDDFile, Variables) - ), - statistics(walltime, [_, ScriptGenerationTime]), - problog_var_set(bdd_script_time, ScriptGenerationTime) - % omitted call to execute_bdd_tool - ; - true - ), - % naive method with nested trie support but not loops - ((problog_flag(use_naive_trie, true); (problog_flag(use_old_trie, true), nb_getval(problog_nested_tries, true))) -> - statistics(walltime, _), - atomic_concat([BDDFile, '_naive'], BDDFile_naive), - nested_ptree_to_BDD_struct_script(Trie, BDDFile_naive, Variables), - statistics(walltime, [_, ScriptGenerationTime_naive]), - problog_var_set(bdd_script_time(naive), ScriptGenerationTime_naive) - % omitted call to execute_bdd_tool - ; - true - ), - % reduction method with depth_breadth trie support - problog_flag(db_trie_opt_lvl, ROptLevel), - problog_flag(db_min_prefix, MinPrefix), - - (problog_flag(compare_opt_lvl, true) -> - generate_ints(0, ROptLevel, Levels) - ; - Levels = [ROptLevel] - ), - % Removed forall here, because it hides 'Variables' from what comes afterwards - memberchk(OptLevel, Levels), - ( - (problog_flag(use_db_trie, true) -> - tries:trie_db_opt_min_prefix(MinPrefix), - statistics(walltime, _), - (nb_getval(problog_nested_tries, false) -> - trie_to_bdd_struct_trie(Trie, DBTrie, BDDFile, OptLevel, Variables) - ; - nested_trie_to_bdd_struct_trie(Trie, DBTrie, BDDFile, OptLevel, Variables) - ), - atomic_concat(['builtin_', OptLevel], Builtin), - ptree:trie_stats(DBMemory, DBTries, DBEntries, DBNodes), - FM is DBMemory - Memory, - FT is DBTries - Tries, - FE is DBEntries - Entries, - FN is DBNodes - Nodes, - problog_var_set(dbtrie_statistics(Builtin), tries(memory(FM), tries(FT), entries(FE), nodes(FN))), - - delete_ptree(DBTrie), - statistics(walltime, [_, ScriptGenerationTime_builtin]), - problog_var_set(bdd_script_time(Builtin), ScriptGenerationTime_builtin) - % omitted call to execute_bdd_tool - ; - true - ) - ), - - % decomposition method - (problog_flag(use_dec_trie, true) -> - statistics(walltime, _), - atomic_concat([BDDFile, '_dec'], BDDFile_dec), - ptree_decomposition_struct(Trie, BDDFile_dec, Variables), - statistics(walltime, [_, ScriptGenerationTime_dec]), - problog_var_set(bdd_script_time(dec), ScriptGenerationTime_dec) - % omitted call to execute_bdd_tool - ; - true - ), - - (Trie =\= OriTrie -> - delete_ptree(Trie) - ; - true - ), - (var(Variables) -> throw(error('novars')) ; true). - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -% Building a forest of BDDs -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% - -problog_bdd_forest(Goals) :- - (problog_bdd_forest_supported -> - require(keep_ground_ids), - once(write_bdd_forest(Goals,[],Vars,1)), - unrequire(keep_ground_ids), - reset_non_ground_facts, - bdd_par_file(BDDParFile), - tell(BDDParFile), - bdd_vars_script(Vars), - flush_output, % isnt this called by told/0? - told, - length(Goals,L), - length(Vars,NbVars), - write_global_bdd_file(NbVars,L), - (problog_flag(retain_tables, true) -> retain_tabling; true), - clear_tabling - ; - throw(error('Flag settings not supported by problog_bdd_forest/1.')) - ). - -problog_bdd_forest_supported :- build_trie_supported. - -% Iterate over all Goals, write BDD scripts and collect variables used. -write_bdd_forest([],VarsTot,VarsTot,_). -write_bdd_forest([Goal|Rest],VarsAcc,VarsTot,N):- - build_trie(Goal, Trie), - write_nth_bdd_struct_script(N, Trie, Vars), - (problog_flag(verbose, true)-> - problog_statistics - ; - true - ), - delete_ptree(Trie), - N2 is N+1, - list_to_ord_set(Vars,VarsSet), - ord_union(VarsAcc,VarsSet,VarsAcc2), - once(write_bdd_forest(Rest,VarsAcc2,VarsTot,N2)). - -% Write files -write_nth_bdd_struct_script(N,Trie,Vars) :- - bdd_forest_file(N,BDDFile), - write_bdd_struct_script(Trie,BDDFile,Vars). - -write_global_bdd_file(NbVars,L) :- - bdd_file(BDDFile), - open(BDDFile,'write',BDDFileStream), - tell(BDDFileStream), - format('@BDD2~n~w~n~w~n~w~n',[NbVars,0,L]), - write_global_bdd_file_line(1,L), - write_global_bdd_file_query(1,L), - flush_output, - told. - -write_global_bdd_file_line(I,Max) :- - (I>Max -> - true - ; - bdd_forest_file(I,BDDFile), - format("L~q = <~w>~n",[I,BDDFile]), - I2 is I+1, - write_global_bdd_file_line(I2,Max) - ). - -write_global_bdd_file_query(I,Max) :- - (I=Max -> - format("L~q~n",[I]) - ; - format("L~q,",[I]), - I2 is I+1, - write_global_bdd_file_query(I2,Max) - ). - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -% Filename specifications -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% - -bdd_forest_file(N,BDDFile) :- - problog_flag(bdd_file,BDDFileFlag), - atomic_concat([BDDFileFlag,'_',N],BDDFileFlagWithN), - convert_filename_to_working_path(BDDFileFlagWithN, BDDFile). - -bdd_files(BDDFile,BDDParFile) :- - bdd_file(BDDFile), - bdd_par_file(BDDParFile). - -bdd_file(BDDFile) :- - problog_flag(bdd_file, BDDFileFlag), - convert_filename_to_working_path(BDDFileFlag, BDDFile). - -bdd_par_file(BDDParFile) :- - problog_flag(bdd_par_file, BDDParFileFlag), - convert_filename_to_working_path(BDDParFileFlag, BDDParFile). - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -% Persistent Ground IDs -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% - -require(Feature) :- - atom(Feature), - atomic_concat(['problog_required_',Feature],Feature_Required), - atomic_concat([Feature_Required,'_',depth],Feature_Depth), - (required(Feature) -> - b_getval(Feature_Depth,Depth), - Depth1 is Depth+1, - b_setval(Feature_Depth,Depth1) - ; - b_setval(Feature_Required,required), - b_setval(Feature_Depth,1) - %,format("starting to require ~q~n",[Feature]) - ). - -unrequire(Feature) :- - atom(Feature), - atomic_concat(['problog_required_',Feature],Feature_Required), - atomic_concat([Feature_Required,'_',depth],Feature_Depth), - b_getval(Feature_Depth,Depth), - (Depth=1 -> - nb_delete(Feature_Required), - nb_delete(Feature_Depth) - %,format("stopped keeping ground id's~n",[]) - ; - Depth1 is Depth-1, - b_setval(Feature_Depth,Depth1) - ). - -required(Feature) :- - atom(Feature), - atomic_concat(['problog_required_',Feature],Feature_Required), - catch(b_getval(Feature_Required,Val),error(existence_error(variable,Feature_Required),_),fail), - Val == required. - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -% Should go to dtproblog.yap -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -signal_decision(ClauseID,GroundID) :- - (decision_fact(ClauseID,_) -> - bb_get(decisions,S), - ord_insert(S, GroundID, S2), - bb_put(decisions,S2) - ; - true - ). - - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -% Term Expansion for user predicates -% Must come after clauses for '::'/2 and term_expansion_intern/3 -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% - -user:term_expansion(Term,ExpandedTerm) :- - Term \== end_of_file, - prolog_load_context(module,Mod), - problog:term_expansion_intern(Term,Mod,ExpandedTerm). - diff --git a/packages/ProbLog/problog_examples/problog_learning.yap b/packages/ProbLog/problog_examples/problog_learning.yap deleted file mode 100644 index b8c2ff1f0..000000000 --- a/packages/ProbLog/problog_examples/problog_learning.yap +++ /dev/null @@ -1,1677 +0,0 @@ -%%% -*- Mode: Prolog; -*- - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -% -% $Date: 2010-10-05 16:52:13 +0200 (Tue, 05 Oct 2010) $ -% $Revision: 4869 $ -% -% This file is part of ProbLog -% http://dtai.cs.kuleuven.be/problog -% -% ProbLog was developed at Katholieke Universiteit Leuven -% -% Copyright 2008, 2009, 2010 -% Katholieke Universiteit Leuven -% -% Main authors of this file: -% Bernd Gutmann -% -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -% -% Artistic License 2.0 -% -% Copyright (c) 2000-2006, The Perl Foundation. -% -% Everyone is permitted to copy and distribute verbatim copies of this -% license document, but changing it is not allowed. Preamble -% -% This license establishes the terms under which a given free software -% Package may be copied, modified, distributed, and/or -% redistributed. The intent is that the Copyright Holder maintains some -% artistic control over the development of that Package while still -% keeping the Package available as open source and free software. -% -% You are always permitted to make arrangements wholly outside of this -% license directly with the Copyright Holder of a given Package. If the -% terms of this license do not permit the full use that you propose to -% make of the Package, you should contact the Copyright Holder and seek -% a different licensing arrangement. Definitions -% -% "Copyright Holder" means the individual(s) or organization(s) named in -% the copyright notice for the entire Package. -% -% "Contributor" means any party that has contributed code or other -% material to the Package, in accordance with the Copyright Holder's -% procedures. -% -% "You" and "your" means any person who would like to copy, distribute, -% or modify the Package. -% -% "Package" means the collection of files distributed by the Copyright -% Holder, and derivatives of that collection and/or of those files. A -% given Package may consist of either the Standard Version, or a -% Modified Version. -% -% "Distribute" means providing a copy of the Package or making it -% accessible to anyone else, or in the case of a company or -% organization, to others outside of your company or organization. -% -% "Distributor Fee" means any fee that you charge for Distributing this -% Package or providing support for this Package to another party. It -% does not mean licensing fees. -% -% "Standard Version" refers to the Package if it has not been modified, -% or has been modified only in ways explicitly requested by the -% Copyright Holder. -% -% "Modified Version" means the Package, if it has been changed, and such -% changes were not explicitly requested by the Copyright Holder. -% -% "Original License" means this Artistic License as Distributed with the -% Standard Version of the Package, in its current version or as it may -% be modified by The Perl Foundation in the future. -% -% "Source" form means the source code, documentation source, and -% configuration files for the Package. -% -% "Compiled" form means the compiled bytecode, object code, binary, or -% any other form resulting from mechanical transformation or translation -% of the Source form. -% -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -% -% Permission for Use and Modification Without Distribution -% -% (1) You are permitted to use the Standard Version and create and use -% Modified Versions for any purpose without restriction, provided that -% you do not Distribute the Modified Version. -% -% Permissions for Redistribution of the Standard Version -% -% (2) You may Distribute verbatim copies of the Source form of the -% Standard Version of this Package in any medium without restriction, -% either gratis or for a Distributor Fee, provided that you duplicate -% all of the original copyright notices and associated disclaimers. At -% your discretion, such verbatim copies may or may not include a -% Compiled form of the Package. -% -% (3) You may apply any bug fixes, portability changes, and other -% modifications made available from the Copyright Holder. The resulting -% Package will still be considered the Standard Version, and as such -% will be subject to the Original License. -% -% Distribution of Modified Versions of the Package as Source -% -% (4) You may Distribute your Modified Version as Source (either gratis -% or for a Distributor Fee, and with or without a Compiled form of the -% Modified Version) provided that you clearly document how it differs -% from the Standard Version, including, but not limited to, documenting -% any non-standard features, executables, or modules, and provided that -% you do at least ONE of the following: -% -% (a) make the Modified Version available to the Copyright Holder of the -% Standard Version, under the Original License, so that the Copyright -% Holder may include your modifications in the Standard Version. (b) -% ensure that installation of your Modified Version does not prevent the -% user installing or running the Standard Version. In addition, the -% modified Version must bear a name that is different from the name of -% the Standard Version. (c) allow anyone who receives a copy of the -% Modified Version to make the Source form of the Modified Version -% available to others under (i) the Original License or (ii) a license -% that permits the licensee to freely copy, modify and redistribute the -% Modified Version using the same licensing terms that apply to the copy -% that the licensee received, and requires that the Source form of the -% Modified Version, and of any works derived from it, be made freely -% available in that license fees are prohibited but Distributor Fees are -% allowed. -% -% Distribution of Compiled Forms of the Standard Version or -% Modified Versions without the Source -% -% (5) You may Distribute Compiled forms of the Standard Version without -% the Source, provided that you include complete instructions on how to -% get the Source of the Standard Version. Such instructions must be -% valid at the time of your distribution. If these instructions, at any -% time while you are carrying out such distribution, become invalid, you -% must provide new instructions on demand or cease further -% distribution. If you provide valid instructions or cease distribution -% within thirty days after you become aware that the instructions are -% invalid, then you do not forfeit any of your rights under this -% license. -% -% (6) You may Distribute a Modified Version in Compiled form without the -% Source, provided that you comply with Section 4 with respect to the -% Source of the Modified Version. -% -% Aggregating or Linking the Package -% -% (7) You may aggregate the Package (either the Standard Version or -% Modified Version) with other packages and Distribute the resulting -% aggregation provided that you do not charge a licensing fee for the -% Package. Distributor Fees are permitted, and licensing fees for other -% components in the aggregation are permitted. The terms of this license -% apply to the use and Distribution of the Standard or Modified Versions -% as included in the aggregation. -% -% (8) You are permitted to link Modified and Standard Versions with -% other works, to embed the Package in a larger work of your own, or to -% build stand-alone binary or bytecode versions of applications that -% include the Package, and Distribute the result without restriction, -% provided the result does not expose a direct interface to the Package. -% -% Items That are Not Considered Part of a Modified Version -% -% (9) Works (including, but not limited to, modules and scripts) that -% merely extend or make use of the Package, do not, by themselves, cause -% the Package to be a Modified Version. In addition, such works are not -% considered parts of the Package itself, and are not subject to the -% terms of this license. -% -% General Provisions -% -% (10) Any use, modification, and distribution of the Standard or -% Modified Versions is governed by this Artistic License. By using, -% modifying or distributing the Package, you accept this license. Do not -% use, modify, or distribute the Package, if you do not accept this -% license. -% -% (11) If your Modified Version has been derived from a Modified Version -% made by someone other than you, you are nevertheless required to -% ensure that your Modified Version complies with the requirements of -% this license. -% -% (12) This license does not grant you the right to use any trademark, -% service mark, tradename, or logo of the Copyright Holder. -% -% (13) This license includes the non-exclusive, worldwide, -% free-of-charge patent license to make, have made, use, offer to sell, -% sell, import and otherwise transfer the Package with respect to any -% patent claims licensable by the Copyright Holder that are necessarily -% infringed by the Package. If you institute patent litigation -% (including a cross-claim or counterclaim) against any party alleging -% that the Package constitutes direct or contributory patent -% infringement, then this Artistic License to you shall terminate on the -% date that such litigation is filed. -% -% (14) Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT -% HOLDER AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED -% WARRANTIES. THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A -% PARTICULAR PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT -% PERMITTED BY YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT -% HOLDER OR CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, -% INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE -% OF THE PACKAGE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -% -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% - - -:- module(learning,[do_learning/1, - do_learning/2, - reset_learning/0 - ]). - -% switch on all the checks to reduce bug searching time -:- style_check(all). -:- yap_flag(unknown,error). - -% load modules from the YAP library -:- use_module(library(lists), [max_list/2, min_list/2, sum_list/2]). -:- use_module(library(system), [delete_file/1, file_exists/1, shell/2]). - -% load our own modules -:- use_module(problog). -:- use_module('problog/logger'). -:- use_module('problog/flags'). -:- use_module('problog/os'). -:- use_module('problog/print_learning'). -:- use_module('problog/utils_learning'). - -% used to indicate the state of the system -:- dynamic(values_correct/0). -:- dynamic(learning_initialized/0). -:- dynamic(current_iteration/1). -:- dynamic(example_count/1). -:- dynamic(query_probability_intern/2). -:- dynamic(query_gradient_intern/4). -:- dynamic(last_mse/1). -:- dynamic(query_is_similar/2). -:- dynamic(query_md5/2). - - -% used to identify queries which have identical proofs -:- dynamic(query_is_similar/2). -:- dynamic(query_md5/3). - -:- multifile(user:example/4). -user:example(A,B,C,=) :- - current_predicate(user:example/3), - user:example(A,B,C). - -:- multifile(user:test_example/4). -user:test_example(A,B,C,=) :- - current_predicate(user:test_example/3), - user:test_example(A,B,C). - - -%======================================================================== -%= store the facts with the learned probabilities to a file -%= if F is a variable, a filename based on the current iteration is used -%= -%======================================================================== - -save_model:- - current_iteration(Iteration), - atomic_concat(['factprobs_',Iteration,'.pl'],Filename), - problog_flag(output_directory,Dir), - concat_path_with_filename(Dir,Filename,Filename2), - export_facts(Filename2). - - -%======================================================================== -%= store the current succes probabilities for training and test examples -%= -%======================================================================== - -save_predictions:- - current_iteration(Iteration), - atomic_concat(['predictions_',Iteration,'.pl'],Filename), - problog_flag(output_directory,Dir), - concat_path_with_filename(Dir,Filename,Filename2), - - open(Filename2,'append',Handle), - format(Handle,"%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n",[]), - format(Handle,"% Iteration, train/test, QueryID, Query, GroundTruth, Prediction %\n",[]), - format(Handle,"%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n",[]), - !, - - %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% - % start save prediction test examples - %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% - ( % go over all test examples - current_predicate(user:test_example/4), - user:test_example(Query_ID,Query,TrueQueryProb,_), - query_probability(Query_ID,LearnedQueryProb), - - format(Handle,'ex(~q,test,~q,~q,~10f,~10f).\n', - [Iteration,Query_ID,Query,TrueQueryProb,LearnedQueryProb]), - - fail; % go to next test example - true - ), - !, - %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% - % stop save prediction test examples - %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% - - %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% - % start save prediction training examples - %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% - ( % go over all training examples - current_predicate(user:example/4), - user:example(Query_ID,Query,TrueQueryProb,_), - query_probability(Query_ID,LearnedQueryProb), - - format(Handle,'ex(~q,train,~q,~q,~10f,~10f).\n', - [Iteration,Query_ID,Query,TrueQueryProb,LearnedQueryProb]), - - fail; % go to next training example - true - ), - !, - %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% - % stop save prediction training examples - %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% - - format(Handle,'~3n',[]), - close(Handle). - - - -%======================================================================== -%= find out whether some example IDs are used more than once -%= if so, complain and stop -%= -%======================================================================== - -check_examples :- - %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% - % Check example IDs - %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% - ( - (current_predicate(user:example/4),user:example(ID,_,_,_), \+ atomic(ID)) - -> - ( - format(user_error,'The example id of training example ~q ',[ID]), - format(user_error,'is not atomic (e.g foo42, 23, bar, ...).~n',[]), - throw(error(examples)) - ); true - ), - - ( - (current_predicate(user:test_example/4),user:test_example(ID,_,_,_), \+ atomic(ID)) - -> - ( - format(user_error,'The example id of test example ~q ',[ID]), - format(user_error,'is not atomic (e.g foo42, 23, bar, ...).~n',[]), - throw(error(examples)) - ); true - ), - - %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% - % Check example probabilities - %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% - ( - (current_predicate(user:example/4),user:example(ID,_,P,_), (\+ number(P); P>1 ; P<0)) - -> - ( - format(user_error,'The training example ~q does not have a valid probability value (~q).~n',[ID,P]), - throw(error(examples)) - ); true - ), - - ( - (current_predicate(user:test_example/4),user:test_example(ID,_,P,_), (\+ number(P); P>1 ; P<0)) - -> - ( - format(user_error,'The test example ~q does not have a valid probability value (~q).~n',[ID,P]), - throw(error(examples)) - ); true - ), - - - %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% - % Check that no example ID is repeated, - % and if it is repeated make sure the query is the same - %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% - ( - ( - ( - current_predicate(user:example/4), - user:example(ID,QueryA,_,_), - user:example(ID,QueryB,_,_), - QueryA \= QueryB - ) ; - - ( - current_predicate(user:test_example/4), - user:test_example(ID,QueryA,_,_), - user:test_example(ID,QueryB,_,_), - QueryA \= QueryB - ); - - ( - current_predicate(user:example/4), - current_predicate(user:test_example/4), - user:example(ID,QueryA,_,_), - user:test_example(ID,QueryB,_,_), - QueryA \= QueryB - ) - ) - -> - ( - format(user_error,'The example id ~q is used several times.~n',[ID]), - throw(error(examples)) - ); true - ). -%======================================================================== -%= -%======================================================================== - -reset_learning :- - retractall(current_iteration(_)), - retractall(learning_initialized), - - retractall(values_correct), - retractall(current_iteration(_)), - retractall(example_count(_)), - retractall(query_probability_intern(_,_)), - retractall(query_gradient_intern(_,_,_)), - retractall(last_mse(_)), - retractall(query_is_similar(_,_)), - retractall(query_md5(_,_,_)), - - set_problog_flag(alpha,auto), - set_problog_flag(learning_rate,examples), - logger_reset_all_variables. - - - -%======================================================================== -%= initialize everything and perform Iterations times gradient descent -%= can be called several times -%= if it is called with an epsilon parameter, it stops when the change -%= in the MSE is smaller than epsilon -%======================================================================== - -do_learning(Iterations) :- - do_learning(Iterations,-1). - -do_learning(Iterations,Epsilon) :- - current_predicate(user:example/4), - !, - integer(Iterations), - number(Epsilon), - Iterations>0, - do_learning_intern(Iterations,Epsilon). -do_learning(_,_) :- - format(user_error,'~n~Error: No training examples specified.~n~n',[]). - - -do_learning_intern(0,_) :- - !. -do_learning_intern(Iterations,Epsilon) :- - Iterations>0, - - init_learning, - current_iteration(CurrentIteration), - retractall(current_iteration(_)), - NextIteration is CurrentIteration+1, - assertz(current_iteration(NextIteration)), - EndIteration is CurrentIteration+Iterations-1, - - format_learning(1,'~nIteration ~d of ~d~n',[CurrentIteration,EndIteration]), - logger_set_variable(iteration,CurrentIteration), - - logger_start_timer(duration), - - mse_testset, - ground_truth_difference, - gradient_descent, - - problog_flag(log_frequency,Log_Frequency), - - ( - ( Log_Frequency>0, 0 =:= CurrentIteration mod Log_Frequency) - -> - ( - once(save_predictions), - once(save_model) - ); - true - ), - - update_values, - - ( - last_mse(Last_MSE) - -> - ( - retractall(last_mse(_)), - logger_get_variable(mse_trainingset,Current_MSE), - assertz(last_mse(Current_MSE)), - !, - MSE_Diff is abs(Last_MSE-Current_MSE) - ); ( - logger_get_variable(mse_trainingset,Current_MSE), - assertz(last_mse(Current_MSE)), - MSE_Diff is Epsilon+1 - ) - ), - - ( - (problog_flag(rebuild_bdds,BDDFreq),BDDFreq>0,0 =:= CurrentIteration mod BDDFreq) - -> - ( - retractall(values_correct), - retractall(query_is_similar(_,_)), - retractall(query_md5(_,_,_)), - empty_bdd_directory, - init_queries - ); true - ), - - - !, - logger_stop_timer(duration), - - - logger_write_data, - - - - RemainingIterations is Iterations-1, - - ( - MSE_Diff>Epsilon - -> - do_learning_intern(RemainingIterations,Epsilon); - true - ). - - -%======================================================================== -%= find proofs and build bdds for all training and test examples -%= -%= -%======================================================================== -init_learning :- - learning_initialized, - !. -init_learning :- - check_examples, - - logger_write_header, - - format_learning(1,'Initializing everything~n',[]), - empty_output_directory, - - %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% - % Delete the BDDs from the previous run if they should - % not be reused - %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% - ( - ( - problog_flag(reuse_initialized_bdds,true), - problog_flag(rebuild_bdds,0) - ) - -> - true; - empty_bdd_directory - ), - - %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% - % Check, if continuous facts are used. - % if yes, switch to problog_exact - %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% - problog_flag(init_method,(_,_,_,_,OldCall)), - ( - ( - continuous_fact(_), - OldCall\=problog_exact_save(_,_,_,_,_) - ) - -> - ( - format('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))) - ); - true - ), - - - %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% - % start count test examples - %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% - bb_put(test_examples,0), - ( % go over all test examples - current_predicate(user:test_example/4), - user:test_example(_,_,_,_), - bb_get(test_examples, OldCounter), - NewCounter is OldCounter+1, - bb_put(test_examples,NewCounter), - - fail; % go to next text example - true - ), - bb_delete(test_examples,TestExampleCount), - format_learning(3,'~q test examples~n',[TestExampleCount]), - !, - %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% - % stop count test examples - %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% - - %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% - % start count training examples - %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% - bb_put(training_examples,0), - ( % go over all training examples - current_predicate(user:example/4), - user:example(_,_,_,_), - bb_get(training_examples, OldCounter), - NewCounter is OldCounter+1, - bb_put(training_examples,NewCounter), - - fail; %go to next training example - true - ), - bb_delete(training_examples,TrainingExampleCount), - assertz(example_count(TrainingExampleCount)), - format_learning(3,'~q training examples~n',[TrainingExampleCount]), - !, - %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% - % stop count training examples - %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% - - - %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% - % set learning rate and alpha - %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% - ( - problog_flag(learning_rate,examples) - -> - set_problog_flag(learning_rate,TrainingExampleCount); - true - ), - - ( - problog_flag(alpha,auto) - -> - auto_alpha; - true - ), - - %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% - % build BDD script for every example - %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% - once(init_queries), - - %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% - % done - %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% - assertz(current_iteration(0)), - assertz(learning_initialized), - - format_learning(1,'~n',[]). - - - -%======================================================================== -%= This predicate goes over all training and test examples, -%= calls the inference method of ProbLog and stores the resulting -%= BDDs -%======================================================================== - - -init_queries :- - format_learning(2,'Build BDDs for examples~n',[]), - ( % go over all test examples - current_predicate(user:test_example/4), - user:test_example(ID,Query,Prob,_), - format_learning(3,' test example ~q: ~q~n',[ID,Query]), - flush_output(user), - init_one_query(ID,Query,test), - - fail; % go to next test example - true - ), - ( % go over all training examples - current_predicate(user:example/4), - user:example(ID,Query,Prob,_), - format_learning(3,' training example ~q: ~q~n',[ID,Query]), - flush_output(user), - init_one_query(ID,Query,training), - - fail; %go to next training example - true - ). - - -bdd_input_file(Filename) :- - problog_flag(output_directory,Dir), - concat_path_with_filename(Dir,'input.txt',Filename). - -init_one_query(QueryID,Query,Type) :- - bdd_input_file(Probabilities_File), - problog_flag(bdd_directory,Query_Directory), - - atomic_concat(['query_',QueryID],Filename1), - concat_path_with_filename(Query_Directory,Filename1,Filename), - - %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% - % if BDD file does not exist, call ProbLog - %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% - ( - file_exists(Filename) - -> - format_learning(3,' Reuse existing BDD ~q~n~n',[Filename]); - ( - problog_flag(init_method,(Query,_Prob,Filename,Probabilities_File,Call)), - once(Call), - delete_file(Probabilities_File) - ) - ), - - %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% - % check wether this BDD is similar to another BDD - %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% - ( - problog_flag(check_duplicate_bdds,true) - -> - ( - calc_md5(Filename,Query_MD5), - ( - query_md5(OtherQueryID,Query_MD5,Type) - -> - ( - assertz(query_is_similar(QueryID,OtherQueryID)), - format_learning(3, '~q is similar to ~q~2n', [QueryID,OtherQueryID]) - ); - assertz(query_md5(QueryID,Query_MD5,Type)) - ) - ); - - true - ),!, - garbage_collect. - - - - -%======================================================================== -%= updates all values of query_probability/2 and query_gradient/4 -%= should be called always before these predicates are accessed -%= if the old values are still valid, nothing happens -%======================================================================== - -update_values :- - values_correct, - !. -update_values :- - \+ values_correct, - - %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% - % delete old values - %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% - retractall(query_probability_intern(_,_)), - retractall(query_gradient_intern(_,_,_,_)), - - %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% - % start write current probabilities to file - %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% - bdd_input_file(Probabilities_File), - delete_file_silent(Probabilities_File), - - open(Probabilities_File,'write',Handle), - - ( % go over all probabilistic facts - get_fact_probability(ID,Prob), - inv_sigmoid(Prob,Value), - ( - non_ground_fact(ID) - -> - format(Handle,'@x~q_*~n~10f~n',[ID,Value]); - format(Handle,'@x~q~n~10f~n',[ID,Value]) - ), - - fail; % go to next probabilistic fact - true - ), - - ( % go over all continuous facts - get_continuous_fact_parameters(ID,gaussian(Mu,Sigma)), - format(Handle,'@x~q_*~n0~n0~n~10f;~10f~n',[ID,Mu,Sigma]), - - fail; % go to next continuous fact - true - ), - - close(Handle), - !, - %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% - % stop write current probabilities to file - %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% - - assertz(values_correct). - - - -%======================================================================== -%= -%= -%= -%======================================================================== - -update_query_cleanup(QueryID) :- - ( - (query_is_similar(QueryID,_) ; query_is_similar(_,QueryID)) - -> - % either this query is similar to another or vice versa, - % therefore we don't delete anything - true; - retractall(query_gradient_intern(QueryID,_,_,_)) - ). - - -update_query(QueryID,Symbol,What_To_Update) :- - % fixme OS trouble - problog_flag(output_directory,Output_Directory), - problog_flag(bdd_directory,Query_Directory), - bdd_input_file(Probabilities_File), - ( - query_is_similar(QueryID,_) - -> - % we don't have to evaluate the BDD - format_learning(4,'#',[]); - ( - problog_flag(sigmoid_slope,Slope), - problog_dir(PD), - ((What_To_Update=all;query_is_similar(_,QueryID)) -> Method='g' ; Method='l'), - atomic_concat([PD, - '/problogbdd', - ' -i "', Probabilities_File, '"', - ' -l "', Query_Directory,'/query_',QueryID, '"', - ' -m ', Method, - ' -id ', QueryID, - ' -sl ', Slope, - ' > "', - Output_Directory, - 'values.pl"'],Command), - shell(Command,Error), - - - ( - Error = 2 - -> - throw(error('SimpleCUDD has been interrupted.')); - true - ), - ( - Error \= 0 - -> - ( - format(user_error,'SimpleCUDD stopped with error code ~q, command was ~q~n',[Error, shell(Command,Error)]), - throw(bdd_error(QueryID,Error))); - true - ), - atomic_concat([Output_Directory,'values.pl'],Values_Filename), - ( - file_exists(Values_Filename) - -> - ( - ( - once(my_load(Values_Filename,QueryID)) - -> - true; - ( - format(user_error,'ERROR: Tried to read the file ~q but my_load/1 fails.~n~q.~2n',[Values_Filename,update_query(QueryID,Symbol,What_To_Update)]), - throw(error(my_load_fails)) - ) - ); - ( - format(user_error,'ERROR: Tried to read the file ~q but it does not exist.~n~q.~2n',[Values_Filename,update_query(QueryID,Symbol,What_To_Update)]), - throw(error(output_file_does_not_exist)) - ) - ) - ), - - delete_file(Values_Filename), - format_learning(4,'~w',[Symbol]) - ) - ), - flush_output(user). - - -%======================================================================== -%= This predicate reads probability and gradient values from the file -%= the gradient ID is a mere check to uncover hidden bugs -%= +Filename +QueryID -QueryProbability -%======================================================================== - -my_load(File,QueryID) :- - open(File,'read',Handle), - read(Handle,Atom), - once(my_load_intern(Atom,Handle,QueryID)), - close(Handle). -my_load(File,QueryID) :- - format(user_error,'Error at ~q.~2n',[my_load(File,QueryID)]), - throw(error(my_load(File,QueryID))). - -my_load_intern(end_of_file,_,_) :- - !. -my_load_intern(query_probability(QueryID,Prob),Handle,QueryID) :- - !, - assertz(query_probability_intern(QueryID,Prob)), - read(Handle,X), - my_load_intern(X,Handle,QueryID). -my_load_intern(query_gradient(QueryID,XFactID,Type,Value),Handle,QueryID) :- - !, - atomic_concat(x,StringFactID,XFactID), - atom_number(StringFactID,FactID), - assertz(query_gradient_intern(QueryID,FactID,Type,Value)), - read(Handle,X), - my_load_intern(X,Handle,QueryID). -my_load_intern(X,Handle,QueryID) :- - format(user_error,'Unknown atom ~q in results file.~n',[X]), - read(Handle,X2), - my_load_intern(X2,Handle,QueryID). - - - - -%======================================================================== -%= -%= -%= -%======================================================================== -query_probability(QueryID,Prob) :- - ( - query_probability_intern(QueryID,Prob) - -> - true; - ( - query_is_similar(QueryID,OtherQueryID), - query_probability_intern(OtherQueryID,Prob) - ) - ). -query_gradient(QueryID,Fact,Type,Value) :- - ( - query_gradient_intern(QueryID,Fact,Type,Value) - -> - true; - ( - query_is_similar(QueryID,OtherQueryID), - query_gradient_intern(OtherQueryID,Fact,Type,Value) - ) - ). - -%======================================================================== -%= -%= -%= -%======================================================================== - - - -% FIXME -ground_truth_difference :- - findall(Diff,(tunable_fact(FactID,GroundTruth), - \+continuous_fact(FactID), - \+ var(GroundTruth), - get_fact_probability(FactID,Prob), - Diff is abs(GroundTruth-Prob)),AllDiffs), - ( - AllDiffs=[] - -> - ( - MinDiff=0.0, - MaxDiff=0.0, - DiffMean=0.0 - ) ; - ( - length(AllDiffs,Len), - sum_list(AllDiffs,AllDiffsSum), - min_list(AllDiffs,MinDiff), - max_list(AllDiffs,MaxDiff), - DiffMean is AllDiffsSum/Len - ) - ), - - logger_set_variable(ground_truth_diff,DiffMean), - logger_set_variable(ground_truth_mindiff,MinDiff), - logger_set_variable(ground_truth_maxdiff,MaxDiff). - -%======================================================================== -%= Calculates the mse of training and test data -%= -%= -Float -%======================================================================== - -mse_trainingset_only_for_linesearch(MSE) :- - ( - current_predicate(user:example/4) - -> - ( - update_values, - findall(SquaredError, - (user:example(QueryID,_Query,QueryProb,Type), - once(update_query(QueryID,'.',probability)), - query_probability(QueryID,CurrentProb), - once(update_query_cleanup(QueryID)), - ( - (Type == '='; (Type == '<', CurrentProb>QueryProb); (Type=='>',CurrentProb - SquaredError is (CurrentProb-QueryProb)**2; - SquaredError = 0.0 - ) - ), - - AllSquaredErrors), - - length(AllSquaredErrors,Length), - sum_list(AllSquaredErrors,SumAllSquaredErrors), - MSE is SumAllSquaredErrors/Length, - format_learning(3,' (~8f)~n',[MSE]) - ); true - ), - retractall(values_correct). - -mse_testset :- - ( - (current_predicate(user:test_example/4),user:test_example(_,_,_,_)) - -> - ( - format_learning(2,'MSE_Test ',[]), - update_values, - findall(SquaredError, - (user:test_example(QueryID,_Query,QueryProb,Type), - once(update_query(QueryID,'+',probability)), - query_probability(QueryID,CurrentProb), - once(update_query_cleanup(QueryID)), - ( - (Type == '='; (Type == '<', CurrentProb>QueryProb); (Type=='>',CurrentProb - SquaredError is (CurrentProb-QueryProb)**2; - SquaredError = 0.0 - ) - ), - AllSquaredErrors), - - length(AllSquaredErrors,Length), - sum_list(AllSquaredErrors,SumAllSquaredErrors), - min_list(AllSquaredErrors,MinError), - max_list(AllSquaredErrors,MaxError), - MSE is SumAllSquaredErrors/Length, - - logger_set_variable(mse_testset,MSE), - logger_set_variable(mse_min_testset,MinError), - logger_set_variable(mse_max_testset,MaxError), - format_learning(2,' (~8f)~n',[MSE]) - ); true - ). - -%======================================================================== -%= Calculates the sigmoid function respectivly the inverse of it -%= warning: applying inv_sigmoid to 0.0 or 1.0 will yield +/-inf -%= -%= +Float, -Float -%======================================================================== - -sigmoid(T,Sig) :- - problog_flag(sigmoid_slope,Slope), - Sig is 1/(1+exp(-T*Slope)). - -inv_sigmoid(T,InvSig) :- - problog_flag(sigmoid_slope,Slope), - InvSig is -log(1/T-1)/Slope. - - - - - - -%======================================================================== -%= Perform one iteration of gradient descent -%= -%= assumes that everything is initialized, if the current values -%= of query_probability/2 and query_gradient/4 are not up to date -%= they will be recalculated -%= finally, the values_correct/0 is retracted to signal that the -%= probabilities of the examples have to be recalculated -%======================================================================== - -save_old_probabilities :- - ( % go over all tunable facts - tunable_fact(FactID,_), - - ( - continuous_fact(FactID) - -> - - ( - get_continuous_fact_parameters(FactID,gaussian(OldMu,OldSigma)), - atomic_concat(['old_mu_',FactID],Key), - atomic_concat(['old_sigma_',FactID],Key2), - bb_put(Key,OldMu), - bb_put(Key2,OldSigma) - ); - ( - get_fact_probability(FactID,OldProbability), - atomic_concat(['old_prob_',FactID],Key), - bb_put(Key,OldProbability) - ) - ), - - fail; % go to next tunable fact - true - ). - - - -forget_old_probabilities :- - ( % go over all tunable facts - tunable_fact(FactID,_), - ( - continuous_fact(FactID) - -> - ( - atomic_concat(['old_mu_',FactID],Key), - atomic_concat(['old_sigma_',FactID],Key2), - atomic_concat(['grad_mu_',FactID],Key3), - atomic_concat(['grad_sigma_',FactID],Key4), - bb_delete(Key,_), - bb_delete(Key2,_), - bb_delete(Key3,_), - bb_delete(Key4,_) - ); - ( - atomic_concat(['old_prob_',FactID],Key), - atomic_concat(['grad_',FactID],Key2), - bb_delete(Key,_), - bb_delete(Key2,_) - ) - ), - - fail; % go to next tunable fact - true - ). - -add_gradient(Learning_Rate) :- - ( % go over all tunable facts - tunable_fact(FactID,_), - ( - continuous_fact(FactID) - -> - ( - atomic_concat(['old_mu_',FactID],Key), - atomic_concat(['old_sigma_',FactID],Key2), - atomic_concat(['grad_mu_',FactID],Key3), - atomic_concat(['grad_sigma_',FactID],Key4), - - bb_get(Key,Old_Mu), - bb_get(Key2,Old_Sigma), - bb_get(Key3,Grad_Mu), - bb_get(Key4,Grad_Sigma), - - Mu is Old_Mu -Learning_Rate* Grad_Mu, - Sigma is exp(log(Old_Sigma) -Learning_Rate* Grad_Sigma), - - set_continuous_fact_parameters(FactID,gaussian(Mu,Sigma)) - ); - ( - atomic_concat(['old_prob_',FactID],Key), - atomic_concat(['grad_',FactID],Key2), - - bb_get(Key,OldProbability), - bb_get(Key2,GradValue), - - inv_sigmoid(OldProbability,OldValue), - NewValue is OldValue -Learning_Rate*GradValue, - sigmoid(NewValue,NewProbability), - - % Prevent "inf" by using values too close to 1.0 - Prob_Secure is min(0.999999999,max(0.000000001,NewProbability)), - set_fact_probability(FactID,Prob_Secure) - ) - ), - - fail; % go to next tunable fact - true - ), - retractall(values_correct). - - -gradient_descent :- - format_learning(2,'Gradient ',[]), - - save_old_probabilities, - update_values, - - %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% - % start set gradient to zero - %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% - ( % go over all tunable facts - - tunable_fact(FactID,_), - ( - continuous_fact(FactID) - -> - - ( - atomic_concat(['grad_mu_',FactID],Key), - atomic_concat(['grad_sigma_',FactID],Key2), - bb_put(Key,0.0), - bb_put(Key2,0.0) - ); - ( - atomic_concat(['grad_',FactID],Key), - bb_put(Key,0.0) - ) - ), - - fail; % go to next tunable fact - - true - ), - %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% - % stop gradient to zero - %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% - !, - - %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% - % start calculate gradient - %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% - bb_put(mse_train_sum, 0.0), - bb_put(mse_train_min, 0.0), - bb_put(mse_train_max, 0.0), - - problog_flag(alpha,Alpha), - logger_set_variable(alpha,Alpha), - example_count(Example_Count), - - ( % go over all training examples - current_predicate(user:example/4), - user:example(QueryID,_Query,QueryProb,Type), - once(update_query(QueryID,'.',all)), - query_probability(QueryID,BDDProb), - ( - QueryProb=:=0.0 - -> - Y2=Alpha; - Y2=1.0 - ), - ( - (Type == '='; (Type == '<', BDDProb>QueryProb); (Type=='>',BDDProb - Y is Y2*2/Example_Count * (BDDProb-QueryProb); - Y=0.0 - ), - - - % first do the calculations for the MSE on training set - ( - (Type == '='; (Type == '<', BDDProb>QueryProb); (Type=='>',BDDProb - Squared_Error is (BDDProb-QueryProb)**2; - Squared_Error=0.0 - ), - - bb_get(mse_train_sum,Old_MSE_Train_Sum), - bb_get(mse_train_min,Old_MSE_Train_Min), - bb_get(mse_train_max,Old_MSE_Train_Max), - New_MSE_Train_Sum is Old_MSE_Train_Sum+Squared_Error, - New_MSE_Train_Min is min(Old_MSE_Train_Min,Squared_Error), - New_MSE_Train_Max is max(Old_MSE_Train_Max,Squared_Error), - bb_put(mse_train_sum,New_MSE_Train_Sum), - bb_put(mse_train_min,New_MSE_Train_Min), - bb_put(mse_train_max,New_MSE_Train_Max), - - - - ( % go over all tunable facts - tunable_fact(FactID,_), - ( - continuous_fact(FactID) - -> - - ( - atomic_concat(['grad_mu_',FactID],Key), - atomic_concat(['grad_sigma_',FactID],Key2), - - % if the following query fails, - % it means, the fact is not used in the proof - % of QueryID, and the gradient is 0.0 and will - % not contribute to NewValue either way - % DON'T FORGET THIS IF YOU CHANGE SOMETHING HERE! - query_gradient(QueryID,FactID,mu,GradValueMu), - query_gradient(QueryID,FactID,sigma,GradValueSigma), - - bb_get(Key,OldValueMu), - bb_get(Key2,OldValueSigma), - - NewValueMu is OldValueMu + Y*GradValueMu, - NewValueSigma is OldValueSigma + Y*GradValueSigma, - - bb_put(Key,NewValueMu), - bb_put(Key2,NewValueSigma) - ); - ( - atomic_concat(['grad_',FactID],Key), - - % if the following query fails, - % it means, the fact is not used in the proof - % of QueryID, and the gradient is 0.0 and will - % not contribute to NewValue either way - % DON'T FORGET THIS IF YOU CHANGE SOMETHING HERE! - query_gradient(QueryID,FactID,p,GradValue), - - bb_get(Key,OldValue), - NewValue is OldValue + Y*GradValue, - bb_put(Key,NewValue) - ) - ), - - fail; % go to next fact - true - ), - - once(update_query_cleanup(QueryID)), - fail; % go to next training example - true - ), - %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% - % stop calculate gradient - %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% - !, - - %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% - % start statistics on gradient - %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% - findall(V, ( - tunable_fact(FactID,_), - atomic_concat(['grad_',FactID],Key), - bb_get(Key,V) - ),Gradient_Values), - - ( - Gradient_Values==[] - -> - ( - logger_set_variable(gradient_mean,0.0), - logger_set_variable(gradient_min,0.0), - logger_set_variable(gradient_max,0.0) - ); - ( - sum_list(Gradient_Values,GradSum), - max_list(Gradient_Values,GradMax), - min_list(Gradient_Values,GradMin), - length(Gradient_Values,GradLength), - GradMean is GradSum/GradLength, - - logger_set_variable(gradient_mean,GradMean), - logger_set_variable(gradient_min,GradMin), - logger_set_variable(gradient_max,GradMax) - ) - ), - %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% - % stop statistics on gradient - %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% - - bb_delete(mse_train_sum,MSE_Train_Sum), - bb_delete(mse_train_min,MSE_Train_Min), - bb_delete(mse_train_max,MSE_Train_Max), - MSE is MSE_Train_Sum/Example_Count, - - logger_set_variable(mse_trainingset,MSE), - logger_set_variable(mse_min_trainingset,MSE_Train_Min), - logger_set_variable(mse_max_trainingset,MSE_Train_Max), - - format_learning(2,'~n',[]), - - %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% - % start add gradient to current probabilities - %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% - ( - problog_flag(line_search,false) - -> - problog_flag(learning_rate,LearningRate); - lineSearch(LearningRate,_) - ), - format_learning(3,'learning rate:~8f~n',[LearningRate]), - add_gradient(LearningRate), - logger_set_variable(learning_rate,LearningRate), - %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% - % stop add gradient to current probabilities - %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% - !, - forget_old_probabilities. - -%======================================================================== -%= -%= -%======================================================================== - -line_search_evaluate_point(Learning_Rate,MSE) :- - add_gradient(Learning_Rate), - format_learning(2,'Line search (h=~8f) ',[Learning_Rate]), - mse_trainingset_only_for_linesearch(MSE). - - -lineSearch(Final_X,Final_Value) :- - - % Get Parameters for line search - problog_flag(line_search_tolerance,Tol), - problog_flag(line_search_tau,Tau), - problog_flag(line_search_interval,(A,B)), - - format_learning(3,'Line search in interval (~4f,~4f)~n',[A,B]), - - % init values - Acc is Tol * (B-A), - InitRight is A + Tau*(B-A), - InitLeft is A + B - InitRight, - - line_search_evaluate_point(A,Value_A), - line_search_evaluate_point(B,Value_B), - line_search_evaluate_point(InitRight,Value_InitRight), - line_search_evaluate_point(InitLeft,Value_InitLeft), - - bb_put(line_search_a,A), - bb_put(line_search_b,B), - bb_put(line_search_left,InitLeft), - bb_put(line_search_right,InitRight), - - bb_put(line_search_value_a,Value_A), - bb_put(line_search_value_b,Value_B), - bb_put(line_search_value_left,Value_InitLeft), - bb_put(line_search_value_right,Value_InitRight), - - bb_put(line_search_iteration,1), - - %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% - %%%% BEGIN BACK TRACKING - %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% - ( - repeat, - - bb_get(line_search_iteration,Iteration), - bb_get(line_search_a,Ak), - bb_get(line_search_b,Bk), - bb_get(line_search_left,Left), - bb_get(line_search_right,Right), - - bb_get(line_search_value_a,Fl), - bb_get(line_search_value_b,Fr), - bb_get(line_search_value_left,FLeft), - bb_get(line_search_value_right,FRight), - - ( - % check for infinity, if there is, go to the left - ( FLeft >= FRight, \+ FLeft = (+inf), \+ FRight = (+inf) ) - -> - ( - AkNew=Left, - FlNew=FLeft, - LeftNew=Right, - FLeftNew=FRight, - RightNew is AkNew + Bk - LeftNew, - line_search_evaluate_point(RightNew,FRightNew), - BkNew=Bk, - FrNew=Fr - ); - ( - BkNew=Right, - FrNew=FRight, - RightNew=Left, - FRightNew=FLeft, - LeftNew is Ak + BkNew - RightNew, - - line_search_evaluate_point(LeftNew,FLeftNew), - AkNew=Ak, - FlNew=Fl - ) - ), - - Next_Iteration is Iteration + 1, - - bb_put(line_search_iteration,Next_Iteration), - - bb_put(line_search_a,AkNew), - bb_put(line_search_b,BkNew), - bb_put(line_search_left,LeftNew), - bb_put(line_search_right,RightNew), - - bb_put(line_search_value_a,FlNew), - bb_put(line_search_value_b,FrNew), - bb_put(line_search_value_left,FLeftNew), - bb_put(line_search_value_right,FRightNew), - - % is the search interval smaller than the tolerance level? - BkNew-AkNew0, - !. -line_search_postcheck(V,X,V,X) :- - problog_flag(line_search_never_stop,false), - !. -line_search_postcheck(_,_, LLH, FinalPosition) :- - problog_flag(line_search_tolerance,Tolerance), - problog_flag(line_search_interval,(Left,Right)), - - - Offset is (Right - Left) * Tolerance, - - bb_put(line_search_offset,Offset), - - %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% - ( - - repeat, - - bb_get(line_search_offset,OldOffset), - NewOffset is OldOffset * Tolerance, - bb_put(line_search_offset,NewOffset), - - Position is Left + NewOffset, - line_search_evaluate_point(Position,LLH), - bb_put(line_search_llh,LLH), - - write(logAtom(lineSearchPostCheck(Position,LLH))),nl, - - - \+ LLH = (+inf), - ! - ), % cut away choice point from repeat - %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% - - bb_delete(line_search_llh,LLH), - bb_delete(line_search_offset,FinalOffset), - FinalPosition is Left + FinalOffset. - - - -my_5_min(V1,V2,V3,V4,V5,F1,F2,F3,F4,F5,VMin,FMin) :- - ( - V1 - (VTemp1=V1,FTemp1=F1); - (VTemp1=V2,FTemp1=F2) - ), - ( - V3 - (VTemp2=V3,FTemp2=F3); - (VTemp2=V4,FTemp2=F4) - ), - ( - VTemp1 - (VTemp3=VTemp1,FTemp3=FTemp1); - (VTemp3=VTemp2,FTemp3=FTemp2) - ), - ( - VTemp3 - (VMin=VTemp3,FMin=FTemp3); - (VMin=V5,FMin=F5) - ). - - -%======================================================================== -%= set the alpha parameter to the value -%= # positive training examples / # negative training examples -%= -%= training example is positive if P(e)=1 -%= training example is negative if P(e)=0 -%= -%= if there are training example with 00, - !, - set_problog_flag(alpha,1.0). -auto_alpha :- - findall(1,(user:example(_,_,P,=),P=:=1.0),Pos), - findall(0,(user:example(_,_,P,=),P=:=0.0),Neg), - length(Pos,NP), - length(Neg,NN), - Alpha is NP/NN, - set_problog_flag(alpha,Alpha). - - - -%======================================================================== -%= initialize the logger module and set the flags for learning -%= don't change anything here! use set_problog_flag/2 instead -%======================================================================== - -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' - 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), - problog_define_flag(rebuild_bdds, problog_flag_validate_nonegint, 'rebuild BDDs every nth iteration', 0, learning_general), - problog_define_flag(reuse_initialized_bdds,problog_flag_validate_boolean, 'Reuse BDDs from previous runs',false, learning_general), - problog_define_flag(check_duplicate_bdds,problog_flag_validate_boolean,'Store intermediate results in hash table',true,learning_general), - problog_define_flag(init_method,problog_flag_validate_dummy,'ProbLog predicate to search proofs',(Query,Probability,BDDFile,ProbFile,problog_kbest_save(Query,100,Probability,_Status,BDDFile,ProbFile)),learning_general,flags:learning_init_handler), - problog_define_flag(alpha,problog_flag_validate_number,'weight of negative examples (auto=n_p/n_n)',auto,learning_general,flags:auto_handler), - problog_define_flag(sigmoid_slope,problog_flag_validate_posnumber,'slope of sigmoid function',1.0,learning_general), - - problog_define_flag(learning_rate,problog_flag_validate_posnumber,'Default learning rate (If line_search=false)',examples,learning_line_search,flags:examples_handler), - problog_define_flag(line_search, problog_flag_validate_boolean,'estimate learning rate by line search',false,learning_line_search), - problog_define_flag(line_search_never_stop, problog_flag_validate_boolean,'make tiny step if line search returns 0',true,learning_line_search), - problog_define_flag(line_search_tau, problog_flag_validate_indomain_0_1_open,'tau value for line search',0.618033988749,learning_line_search), - 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), - logger_define_variable(duration,time), - logger_define_variable(mse_trainingset,float), - logger_define_variable(mse_min_trainingset,float), - logger_define_variable(mse_max_trainingset,float), - logger_define_variable(mse_testset,float), - logger_define_variable(mse_min_testset,float), - logger_define_variable(mse_max_testset,float), - logger_define_variable(gradient_mean,float), - logger_define_variable(gradient_min,float), - logger_define_variable(gradient_max,float), - logger_define_variable(ground_truth_diff,float), - logger_define_variable(ground_truth_mindiff,float), - logger_define_variable(ground_truth_maxdiff,float), - logger_define_variable(learning_rate,float), - logger_define_variable(alpha,float). - -:- initialization(init_flags). -:- initialization(init_logger). - From d1839b730c219bf43628b430e97d59666f4d1f67 Mon Sep 17 00:00:00 2001 From: Theofrastos Mantadelis Date: Wed, 6 Oct 2010 12:59:18 +0200 Subject: [PATCH 07/14] ProbLog versioning, ProbLog Tabling warning message instead of error --- packages/ProbLog/problog.yap | 5 +-- packages/ProbLog/problog/os.yap | 12 +++++-- packages/ProbLog/problog/print.yap | 47 +++++++++++++++------------- packages/ProbLog/problog/tabling.yap | 9 +++--- 4 files changed, 44 insertions(+), 29 deletions(-) diff --git a/packages/ProbLog/problog.yap b/packages/ProbLog/problog.yap index 873c53837..fe47bad99 100644 --- a/packages/ProbLog/problog.yap +++ b/packages/ProbLog/problog.yap @@ -2,8 +2,8 @@ %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % -% $Date: 2010-10-05 18:15:57 +0200 (Tue, 05 Oct 2010) $ -% $Revision: 4876 $ +% $Date: 2010-10-06 12:56:13 +0200 (Wed, 06 Oct 2010) $ +% $Revision: 4877 $ % % This file is part of ProbLog % http://dtai.cs.kuleuven.be/problog @@ -249,6 +249,7 @@ non_ground_fact/1, export_facts/1, problog_help/0, + problog_version/0, show_inference/0, problog_dir/1, set_problog_flag/2, diff --git a/packages/ProbLog/problog/os.yap b/packages/ProbLog/problog/os.yap index 6d3f11a14..42bcea592 100644 --- a/packages/ProbLog/problog/os.yap +++ b/packages/ProbLog/problog/os.yap @@ -2,8 +2,8 @@ %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % -% $Date: 2010-10-05 18:15:57 +0200 (Tue, 05 Oct 2010) $ -% $Revision: 4876 $ +% $Date: 2010-10-06 12:56:13 +0200 (Wed, 06 Oct 2010) $ +% $Revision: 4877 $ % % This file is part of ProbLog % http://dtai.cs.kuleuven.be/problog @@ -212,6 +212,7 @@ 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]). @@ -249,6 +250,13 @@ concat_path_with_filename(Path, 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). + %======================================================================== %= Calculate the MD5 checksum of +Filename by calling md5sum %= in case m5sum is not installed, try md5, otherwise fail diff --git a/packages/ProbLog/problog/print.yap b/packages/ProbLog/problog/print.yap index 60395b476..70fabd14d 100644 --- a/packages/ProbLog/problog/print.yap +++ b/packages/ProbLog/problog/print.yap @@ -2,8 +2,8 @@ %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % -% $Date: 2010-10-05 18:15:57 +0200 (Tue, 05 Oct 2010) $ -% $Revision: 4876 $ +% $Date: 2010-10-06 12:56:13 +0200 (Wed, 06 Oct 2010) $ +% $Revision: 4877 $ % % This file is part of ProbLog % http://dtai.cs.kuleuven.be/problog @@ -222,7 +222,8 @@ show_inference/0, problog_flags/0, problog_flags/1, - problog_help/0]). + problog_help/0, + problog_version/0]). % load library modules :- use_module(library(lists), [member/2]). @@ -231,7 +232,7 @@ % load our own modules :- use_module(flags). :- use_module(variables). -:- use_module(os, [check_existance/1, convert_filename_to_problog_path/2]). +:- use_module(os, [check_existance/1, convert_filename_to_problog_path/2, concat_path_with_filename2/3]). :- use_module(version_control, [get_version/3]). @@ -299,8 +300,7 @@ make_column_format([HC|TC], [HS|TS], Format, Acc):- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% problog_help :- - print_version, - format('~nProbLog inference currently offers the following inference methods:~n',[]), + format('~2nProbLog inference currently offers the following inference methods:~n',[]), show_inference, problog:problog_path(PD), format('~nProbLog directory: ~q~n',[PD]), @@ -314,28 +314,33 @@ problog_help :- nl, flush_output. -print_version :- +problog_version :- + MainProblogFiles = ['problog.yap', 'problog_learning.yap', 'dtproblog.yap'], nl, - print_group_line('Version Information'), - check_existance('problog.yap'), - convert_filename_to_problog_path('problog.yap', Path), - get_version(Path, Version, Revision), - format('~w~35+ Last Modified at:~w~65+Revision:~w~n', ['problog.yap', Version, Revision]), + print_group_line_bold('Version Information'), + print_version(MainProblogFiles, ''), + print_sep_line, convert_filename_to_problog_path('problog', ProblogPath), directory_files(ProblogPath, ProblogFiles), - print_version(ProblogFiles), - print_sep_line. + sort(ProblogFiles, ProblogFilesS), + print_version(ProblogFilesS, 'problog'), + print_sep_line_bold. -print_version([]). -print_version([H|T]):- - atom_concat('.', _, H), !, - print_version(T). -print_version([H|T]):- - atom_concat('problog/', H, FileName), +print_version([], _Path). +print_version([H|T], Path):- + atom_concat(_, '.yap', H), !, + (Path == '' -> + FileName = H + ; + concat_path_with_filename2(Path, H, FileName) + ), + check_existance(FileName), convert_filename_to_problog_path(FileName, FilePath), get_version(FilePath, Version, Revision), format('~w~35+ Last Modified at:~w~65+Revision:~w~n', [FileName, Version, Revision]), - print_version(T). + print_version(T, Path). +print_version([_H|T], Path):- + print_version(T, Path). show_inference :- diff --git a/packages/ProbLog/problog/tabling.yap b/packages/ProbLog/problog/tabling.yap index 3559a446f..7ba047a96 100644 --- a/packages/ProbLog/problog/tabling.yap +++ b/packages/ProbLog/problog/tabling.yap @@ -2,8 +2,8 @@ %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % -% $Date: 2010-09-28 21:04:43 +0200 (Tue, 28 Sep 2010) $ -% $Revision: 4838 $ +% $Date: 2010-10-06 12:56:13 +0200 (Wed, 06 Oct 2010) $ +% $Revision: 4877 $ % % This file is part of ProbLog % http://dtai.cs.kuleuven.be/problog @@ -328,8 +328,9 @@ problog_table(Name/Arity, Module) :- atom_concat(['problog_', Name, '_tabled'], ExactName), % Monte carlo tabling - table(Module:MCName/Arity), - assertz(problog_tabled(Module:Name/Arity)), + catch((table(Module:MCName/Arity), + assertz(problog_tabled(Module:Name/Arity))), _, + (format(user_error, 'Warning: Tabling was not enabled over compilation, montecarlo tabling is disabled!~nPredicate: ~q~n', [Module:MCName/Arity]))), findall(_,( OriginalPred =.. [OriginalName|Args], From 1342d2dab252a9d53defa6a2a886f2c0b6a625e8 Mon Sep 17 00:00:00 2001 From: Theofrastos Mantadelis Date: Wed, 6 Oct 2010 13:24:25 +0200 Subject: [PATCH 08/14] SimpleCUDD versioning --- packages/ProbLog/simplecudd/general.c | 2 ++ packages/ProbLog/simplecudd/general.h | 2 ++ packages/ProbLog/simplecudd/problogbdd.c | 2 ++ packages/ProbLog/simplecudd/problogmath.c | 2 ++ packages/ProbLog/simplecudd/problogmath.h | 4 ++-- packages/ProbLog/simplecudd/simplecudd.c | 13 ++++++++++++- packages/ProbLog/simplecudd/simplecudd.h | 2 ++ 7 files changed, 24 insertions(+), 3 deletions(-) diff --git a/packages/ProbLog/simplecudd/general.c b/packages/ProbLog/simplecudd/general.c index e13bf5cae..b34b7dd5b 100644 --- a/packages/ProbLog/simplecudd/general.c +++ b/packages/ProbLog/simplecudd/general.c @@ -7,6 +7,8 @@ * * * Author: Theofrastos Mantadelis * * File: general.c * +* $Date:: 2010-10-06 13:20:59 +0200 (Wed, 06 Oct 2010) $ * +* $Revision:: 4880 $ * * * ******************************************************************************** * * diff --git a/packages/ProbLog/simplecudd/general.h b/packages/ProbLog/simplecudd/general.h index a872cc386..4b22bef5d 100644 --- a/packages/ProbLog/simplecudd/general.h +++ b/packages/ProbLog/simplecudd/general.h @@ -7,6 +7,8 @@ * * * Author: Theofrastos Mantadelis * * File: general.h * +* $Date:: 2010-10-06 13:20:59 +0200 (Wed, 06 Oct 2010) $ * +* $Revision:: 4880 $ * * * ******************************************************************************** * * diff --git a/packages/ProbLog/simplecudd/problogbdd.c b/packages/ProbLog/simplecudd/problogbdd.c index 1370b21b4..12a435d4b 100644 --- a/packages/ProbLog/simplecudd/problogbdd.c +++ b/packages/ProbLog/simplecudd/problogbdd.c @@ -7,6 +7,8 @@ * * * Author: Theofrastos Mantadelis, Angelika Kimmig, Bernd Gutmann * * File: problogbdd.c * +* $Date:: 2010-10-06 13:22:55 +0200 (Wed, 06 Oct 2010) $ * +* $Revision:: 4881 $ * * * ******************************************************************************** * * diff --git a/packages/ProbLog/simplecudd/problogmath.c b/packages/ProbLog/simplecudd/problogmath.c index cdeb9bbaa..d768fb790 100644 --- a/packages/ProbLog/simplecudd/problogmath.c +++ b/packages/ProbLog/simplecudd/problogmath.c @@ -7,6 +7,8 @@ * * * Author: Bernd Gutmann * * File: problogmath.c * +* $Date:: 2010-10-06 13:20:59 +0200 (Wed, 06 Oct 2010) $ * +* $Revision:: 4880 $ * * * ******************************************************************************** * * diff --git a/packages/ProbLog/simplecudd/problogmath.h b/packages/ProbLog/simplecudd/problogmath.h index 8734d6f88..2df38c626 100644 --- a/packages/ProbLog/simplecudd/problogmath.h +++ b/packages/ProbLog/simplecudd/problogmath.h @@ -7,8 +7,8 @@ * * * Author: Bernd Gutmann * * File: problogmath.h * -* $Date:: 2010-02-18 18:04:54 +0100 (Thu, 18 Feb 2010) $ * -* $Revision:: 3623 $ * +* $Date:: 2010-10-06 13:20:59 +0200 (Wed, 06 Oct 2010) $ * +* $Revision:: 4880 $ * * * ******************************************************************************** * * diff --git a/packages/ProbLog/simplecudd/simplecudd.c b/packages/ProbLog/simplecudd/simplecudd.c index caa67134b..07698b2f8 100644 --- a/packages/ProbLog/simplecudd/simplecudd.c +++ b/packages/ProbLog/simplecudd/simplecudd.c @@ -7,6 +7,8 @@ * * * Author: Theofrastos Mantadelis * * File: simplecudd.c * +* $Date:: 2010-10-06 13:20:59 +0200 (Wed, 06 Oct 2010) $ * +* $Revision:: 4880 $ * * * ******************************************************************************** * * @@ -1710,7 +1712,16 @@ void onlinetraverse(DdManager *manager, namedvars varmap, hisqueue *HisQueue, Dd switch(inputline[1]) { case '?': printf("Available instructions:\n\t@c : current node\n\t@n,[BFS, DFS] : expand and go to next node\n\t@t,[BFS, DFS] : throw and go to next node\n"); - printf("\t@h : high node of current\n\t@l : low node of current\n\t@v,[variable] : variable values\n\t@e terminates\n"); + printf("\t@h : high node of current\n\t@l : low node of current\n\t@v,[variable] : variable values\n\t@r restart traverse from parent node\n\t@e terminates\n"); + break; + case 'r': + curnode = bdd; + iQsize = 0; + iRoot = 1; + free(Q); + Q = (DdNode **) malloc(sizeof(DdNode *) * iQsize); + Q2 = NULL; + ReInitHistory(his, varmap.varcnt); break; case 'c': if (iRoot) { diff --git a/packages/ProbLog/simplecudd/simplecudd.h b/packages/ProbLog/simplecudd/simplecudd.h index 688e80d1e..577e79d83 100644 --- a/packages/ProbLog/simplecudd/simplecudd.h +++ b/packages/ProbLog/simplecudd/simplecudd.h @@ -7,6 +7,8 @@ * * * Author: Theofrastos Mantadelis * * File: simplecudd.h * +* $Date:: 2010-10-06 13:20:59 +0200 (Wed, 06 Oct 2010) $ * +* $Revision:: 4880 $ * * * ******************************************************************************** * * From 284fc9ab8bfad3ad6e1d577ec385bb20371e4050 Mon Sep 17 00:00:00 2001 From: Theofrastos Mantadelis Date: Wed, 6 Oct 2010 18:27:40 +0200 Subject: [PATCH 09/14] corrected a small bug with export --- packages/ProbLog/simplecudd/problogbdd.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/packages/ProbLog/simplecudd/problogbdd.c b/packages/ProbLog/simplecudd/problogbdd.c index 12a435d4b..c556ded4a 100644 --- a/packages/ProbLog/simplecudd/problogbdd.c +++ b/packages/ProbLog/simplecudd/problogbdd.c @@ -7,8 +7,8 @@ * * * Author: Theofrastos Mantadelis, Angelika Kimmig, Bernd Gutmann * * File: problogbdd.c * -* $Date:: 2010-10-06 13:22:55 +0200 (Wed, 06 Oct 2010) $ * -* $Revision:: 4881 $ * +* $Date:: 2010-10-06 18:06:08 +0200 (Wed, 06 Oct 2010) $ * +* $Revision:: 4883 $ * * * ******************************************************************************** * * @@ -347,6 +347,7 @@ int main(int argc, char **arg) { MyManager.f = LOW(MyManager.manager); MyManager.varmap = InitNamedVars(1, 0); bdd = OnlineGenerateBDD(MyManager.manager, &MyManager.varmap); + bakbdd = bdd; ivarcnt = GetVarCount(MyManager.manager); } else if(params.independent_forest>0){ // the flag to create a forest of independent bdds is set From da0aee0f57dc87eb4a744d9d923886f0b510df75 Mon Sep 17 00:00:00 2001 From: Vitor Santos Costa Date: Fri, 8 Oct 2010 10:44:51 +0100 Subject: [PATCH 10/14] fix operator error checking (Ulrich #192) --- pl/utils.yap | 131 +++++++++++++++++++++++++++------------------------ 1 file changed, 69 insertions(+), 62 deletions(-) diff --git a/pl/utils.yap b/pl/utils.yap index 816515f73..5416ec5c3 100644 --- a/pl/utils.yap +++ b/pl/utils.yap @@ -19,34 +19,37 @@ '$check_op'(P,T,V,op(P,T,V)), '$op'(P, T, V). - '$check_op'(P,T,V,G) :- - ( - var(P) -> - '$do_error'(instantiation_error,G) - ; - var(T) -> - '$do_error'(instantiation_error,G) - ; - var(V) -> - '$do_error'(instantiation_error,G) - ; - \+ integer(P) -> - '$do_error'(type_error(integer,P),G) - ; - \+ atom(T) -> - '$do_error'(type_error(atom,T),G) - ; - P < 0 -> - '$do_error'(domain_error(operator_priority,P),G) - ; - P > 1200 -> - '$do_error'(domain_error(operator_priority,P),G) - ; - \+ '$associativity'(T) -> - '$do_error'(domain_error(operator_specifier,T),G) - ; - '$check_op_name'(V,G) - ). +% just check the operator declarations for correctness. +'$check_op'(P,T,Op,G) :- + ( var(P) ; var(T); var(Op)), !, + '$do_error'(instantiation_error,G). +'$check_op'(P,_,_,G) :- + \+ integer(P), !, + '$do_error'(type_error(integer,P),G). +'$check_op'(P,_,_,G) :- + P < 0, !, + '$do_error'(domain_error(operator_priority,P),G). +'$check_op'(P,_,_,G) :- + P > 1200, !, + '$do_error'(domain_error(operator_priority,P),G). +'$check_op'(_,T,_,G) :- + \+ atom(T), !, + '$do_error'(type_error(atom,P),G). +'$check_op'(_,T,_,G) :- + \+ '$associativity'(T), !, + '$do_error'(domain_error(operator_specifier,T),G). +'$check_op'(P,T,V,G) :- + '$check_module_for_op'(V, G, NV), + '$check_top_op'(P, T, NV, G). + +'$check_top_op'(_, _, [], _). +'$check_top_op'(P, T, Op.NV, G) :- !, + '$check_ops'(P, T, Op.NV, G). +'$check_top_op'(P, T, V, G) :- + atom(V), !, + '$check_op_name'(P, T, V, G). +'$check_top_op'(P, T, V, G) :- + '$do_error'(type_error(atom,V),G). '$associativity'(xfx). '$associativity'(xfy). @@ -57,17 +60,43 @@ '$associativity'(fx). '$associativity'(fy). - '$check_op_name'(V,G) :- +'$check_module_for_op'(MOp, G, _) :- + var(MOp), !, + '$do_error'(instantiation_error,G). +'$check_module_for_op'(M:V, G, _) :- + var(M), !, + '$do_error'(instantiation_error,G). +'$check_module_for_op'(M:V, G, NV) :- + atom(M), !, + '$check_module_for_op'(V, G, NV). +'$check_module_for_op'(M:V, G, _) :- !, + '$do_error'(type_error(atom,P),G). +'$check_module_for_op'(V, G, V). + +'$check_ops'(P, T, [], G) :- !. +'$check_ops'(P, T, Op.NV, G) :- !, + ( + var(NV) + -> + '$do_error'(instantiation_error,G) + ; + '$check_module_for_op'(Op, G, NOp), + '$check_op_name'(P, T, NOp, G), + '$check_ops'(P, T, NV, G) + ). +'$check_ops'(P, T, Ops, G) :- + '$do_error'(type_error(list,Ops),G). + +'$check_op_name'(_,_,V,G) :- var(V), !, '$do_error'(instantiation_error,G). - '$check_op_name'(',',G) :- !, + '$check_op_name'(_,_,',',G) :- !, '$do_error'(permission_error(modify,operator,','),G). - '$check_op_name'('[]',G) :- !, - '$do_error'(permission_error(create,operator,'[]'),G). - '$check_op_name'('{}',G) :- !, + '$check_op_name'(_,_,'[]',G) :- !, + '$do_error'(permispsion_error(create,operator,'[]'),G). + '$check_op_name'(_,_,'{}',G) :- !, '$do_error'(permission_error(create,operator,'{}'),G). - '$check_op_name'('|',G) :- !, - G = op(P, T, _), + '$check_op_name'(P,T,'|',G) :- !, ( integer(P), P < 1001 @@ -75,35 +104,13 @@ Fix \== xfx, Fix \== xfy, Fix \== yfx, Fix \== yfy ), '$do_error'(permission_error(create,operator,'|'),G). -'$check_op_name'(V,_) :- +'$check_op_name'(_,_,V,_) :- atom(V), !. -'$check_op_name'(M:A, G) :- - ( - var(M) -> - '$do_error'(instantiation_error,G) - ; - var(A) -> - '$do_error'(instantiation_error,G) - ; - atom(M) -> - '$check_op_name'(A, G) - ; - '$do_error'(instantiation_error,G) - ). - '$check_op_name'([A|As], G) :- - '$check_op_name'(A, G), - '$check_op_names'(As, G). +'$check_op_name'(_,_,A,G) :- + '$do_error'(type_error(atom,A),G). -'$check_op_names'([], _). -'$check_op_names'([A|As], G) :- - '$check_op_name'(A, G), - '$check_op_names'(As, G). - - -'$op'(P, T, M:[A|As]) :- !, - '$current_module'(M), - '$opl'(P, T, M, [A|As]). -'$op'(P, T, [A|As]) :- !, +'$op'(P, T, ML) :- + strip_module(ML, M, [A|As]), !, '$opl'(P, T, M, [A|As]). '$op'(P, T, A) :- '$op2'(P,T,A). From 9187c658b8c0bed6408b02f160cf547040b9a40f Mon Sep 17 00:00:00 2001 From: Vitor Santos Costa Date: Fri, 8 Oct 2010 10:45:50 +0100 Subject: [PATCH 11/14] fix very large clause handling --- C/absmi.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/C/absmi.c b/C/absmi.c index 8f2062beb..6491394a4 100755 --- a/C/absmi.c +++ b/C/absmi.c @@ -1677,7 +1677,8 @@ Yap_absmi(int inp) /* ensure_space */ BOp(ensure_space, Osbpi); { - Int sz = PREG->u.Osbpi.i; + Int sz = PREG->u.Osbpi.i; + UInt arity = PREG->u.Osbpi.p->ArityOfPE; PREG = NEXTOP(PREG,Osbpi); if (Unsigned(H) + sz > Unsigned(YREG)-CreepFlag) { YENV[E_CP] = (CELL) CPREG; @@ -1689,7 +1690,7 @@ Yap_absmi(int inp) if (ASP > (CELL *)PROTECT_FROZEN_B(B)) ASP = (CELL *)PROTECT_FROZEN_B(B); saveregs(); - if (!Yap_gcl(sz, PREG->u.Osbpi.p->ArityOfPE, YENV, PREG)) { + if (!Yap_gcl(sz, arity, YENV, PREG)) { Yap_Error(OUT_OF_STACK_ERROR,TermNil,Yap_ErrorMessage); setregs(); FAIL(); From afa0799504f8edc55af464412d37adc4ab110b65 Mon Sep 17 00:00:00 2001 From: Vitor Santos Costa Date: Fri, 8 Oct 2010 10:50:23 +0100 Subject: [PATCH 12/14] fix error message (Ulrich's #184). --- C/arith1.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/C/arith1.c b/C/arith1.c index 3b0b0ce68..3702e64ec 100755 --- a/C/arith1.c +++ b/C/arith1.c @@ -301,7 +301,7 @@ eval1(Int fi, Term t) { out = asin(dbl); #if HAVE_ISNAN if (isnan(out)) { - return Yap_ArithError(DOMAIN_ERROR_OUT_OF_RANGE, t, "atanh(%f)", dbl); + return Yap_ArithError(EVALUATION_ERROR_UNDEFINED, t, "asin(%f)", dbl); } #endif RFLOAT(out); @@ -314,7 +314,7 @@ eval1(Int fi, Term t) { out = acos(dbl); #if HAVE_ISNAN if (isnan(out)) { - return Yap_ArithError(DOMAIN_ERROR_OUT_OF_RANGE, t, "atanh(%f)", dbl); + return Yap_ArithError(EVALUATION_ERROR_UNDEFINED, t, "acos(%f)", dbl); } #endif RFLOAT(out); From 3cfe5c74d53ba665e969430baa098273913f1263 Mon Sep 17 00:00:00 2001 From: Vitor Santos Costa Date: Fri, 8 Oct 2010 10:58:08 +0100 Subject: [PATCH 13/14] fix div to round correctly (Ulrich's #181). --- C/arith2.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/C/arith2.c b/C/arith2.c index 3e84b5a7a..6427dd2d1 100755 --- a/C/arith2.c +++ b/C/arith2.c @@ -111,7 +111,7 @@ p_div2(Term t1, Term t2) { { Int i1 = IntegerOfTerm(t1); Int i2 = IntegerOfTerm(t2); - Int res; + Int res, mod; if (i2 == 0) goto zero_divisor; if (i1 == Int_MIN && i2 == -1) { @@ -122,7 +122,10 @@ p_div2(Term t1, Term t2) { "// /2 with %d and %d", i1, i2); #endif } - res = (i1 - i1%i2) / i2; + mod = i1%i2; + if (mod && (mod ^ i2) < 0) + mod += i2; + res = (i1 - mod) / i2; RINT(res); } case (CELL)double_e: From f40ba3e6db07e9656a89edaec15d182596630336 Mon Sep 17 00:00:00 2001 From: Vitor Santos Costa Date: Fri, 8 Oct 2010 11:02:56 +0100 Subject: [PATCH 14/14] rounding function was incorrect (ulrich's #180) --- pl/flags.yap | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pl/flags.yap b/pl/flags.yap index 48149d080..7e9a88425 100644 --- a/pl/flags.yap +++ b/pl/flags.yap @@ -938,8 +938,8 @@ yap_flag(dialect,yap). '$transl_to_arity'(X1,X) :- X1 < 0, !, X = unbounded. '$transl_to_arity'(X,X). -'$transl_to_rounding_function'(0,down). -'$transl_to_rounding_function'(1,toward_zero). +'$transl_to_rounding_function'(0,toward_zero). +'$transl_to_rounding_function'(1,down). '$transl_to_trl_types'(0,chars). '$transl_to_trl_types'(1,codes).