diff --git a/packages/ProbLog/problog_examples/graph.pl b/packages/ProbLog/problog_examples/graph.pl index e8be11bd6..cacb94297 100644 --- a/packages/ProbLog/problog_examples/graph.pl +++ b/packages/ProbLog/problog_examples/graph.pl @@ -1,21 +1,23 @@ +%%% -*- Mode: Prolog; -*- + %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % ProbLog program describing a probabilistic graph % (running example from ProbLog presentations) %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -:- use_module(library(problog)). +:- use_module('../problog'). %%%% % background knowledge -%%%% +%%%% % definition of acyclic path using list of visited nodes 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(X,Y,A,R) :- + X\==Y, + edge(X,Z), + absent(Z,A), path(Z,Y,[Z|A],R). % using directed edges in both directions @@ -42,7 +44,7 @@ absent(X,[Y|Z]):-X \= Y, absent(X,Z). %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % example queries about path(1,4) % -%%% explanation probability (and facts involved) +%%% explanation probability (and facts involved) % ?- problog_max(path(1,4),Prob,FactsUsed). % FactsUsed = [dir_edge(1,2),dir_edge(2,3),dir_edge(3,4)], % Prob = 0.432 ? @@ -63,7 +65,7 @@ absent(X,[Y|Z]):-X \= Y, absent(X,Z). % ?- problog_montecarlo(path(1,4),0.01,Prob). % Prob = 0.537525 ? % yes -%%% upper and lower bound using iterative deepening, final interval width 0.01 +%%% upper and lower bound using iterative deepening, final interval width 0.01 % ?- problog_delta(path(1,4),0.01,Bound_low,Bound_up,Status). % Bound_low = 0.5354096, % Bound_up = 0.53864, @@ -82,5 +84,5 @@ absent(X,[Y|Z]):-X \= Y, absent(X,Z). % Bound_low = 0.432, % Status = ok ? % yes -% +% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% diff --git a/packages/ProbLog/problog_examples/graph_tabled.pl b/packages/ProbLog/problog_examples/graph_tabled.pl new file mode 100644 index 000000000..5a37f2ada --- /dev/null +++ b/packages/ProbLog/problog_examples/graph_tabled.pl @@ -0,0 +1,67 @@ +%%% -*- Mode: Prolog; -*- + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% ProbLog program describing a probabilistic graph using tabling +% (running example from ProbLog presentations) +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + +:- use_module('../problog'). + +% New trie method ensures Probibilistic Cycle Handling needed for tabling that handles loops +:- set_problog_flag(use_db_trie, true). +:- set_problog_flag(use_old_trie, false). + +%%%% +% background knowledge +%%%% +% definition of acyclic path using list of visited nodes + +% to table a predicate you first need to define it as a dynamic one +:- dynamic path/2. + +path(X,X). +path(X,Y) :- + X\==Y, + edge(X,Z), + path(Z,Y). + +:- problog_table path/2. +% after all predicate definitions have appeared you need to state that the predicate will be tabled + +% using directed edges in both directions +edge(X,Y) :- dir_edge(Y,X). +edge(X,Y) :- dir_edge(X,Y). + + +%%%% +% probabilistic facts +%%%% +0.9::dir_edge(1,2). +0.8::dir_edge(2,3). +0.6::dir_edge(3,4). +0.7::dir_edge(1,6). +0.5::dir_edge(2,6). +0.4::dir_edge(6,5). +0.7::dir_edge(5,3). +0.2::dir_edge(5,4). + + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% example queries about tabled path(1,4) useable only with problog_exact, problog_montecarlo currently +% +%%% success probability +% ?- problog_exact(path(1,4),Prob,Status). +% Prob = 0.53864, +% Status = ok ? +% yes +%%% approximation using monte carlo, to reach 95%-confidence interval width 0.01 +% ?- problog_montecarlo(path(1,4),0.01,Prob). +% Prob = 0.537525 ? +% yes +%%% success probability of negation +% ?- problog_exact(problog_neg(path(1,4)),Prob,Status). +% Prob = 0.46136, +% Status = ok ? +% yes +% +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% diff --git a/packages/ProbLog/problog_examples/learn_graph.pl b/packages/ProbLog/problog_examples/learn_graph.pl index 8c64e07c5..a6c0d04ab 100644 --- a/packages/ProbLog/problog_examples/learn_graph.pl +++ b/packages/ProbLog/problog_examples/learn_graph.pl @@ -1,3 +1,5 @@ +%%% -*- Mode: Prolog; -*- + %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % ProbLog program describing a probabilistic graph % (running example from ProbLog presentations) @@ -10,7 +12,7 @@ % will run 20 iterations of learning with default settings %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -:- use_module(library(problog_learning)). +:- use_module('../problog_learning'). %%%% % background knowledge diff --git a/packages/ProbLog/problog_examples/office.pl b/packages/ProbLog/problog_examples/office.pl new file mode 100644 index 000000000..76cfd988e --- /dev/null +++ b/packages/ProbLog/problog_examples/office.pl @@ -0,0 +1,27 @@ +%%% -*- Mode: Prolog; -*- + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% ProbLog program describing an office window +% +% example for using hybrid ProbLog +% +% query ?- problog_exact(room_has_window, Prob, Status). +% Prob = 0.008527075, +% Status = ok ? +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + +:- use_module('../problog'). + +width(gaussian(2,1)). +length(gaussian(9,3)). +0.8 :: office_has_window. +0.001 :: corridor_has_window. + +in_office :- width(W),length(L), in_interval(W,2,4), in_interval(L,2,4). +in_corridor :- width(W),length(L), below(W,2.5), above(L,3). + +room_has_window:- + in_office, office_has_window. +room_has_window:- + in_corridor,corridor_has_window. + diff --git a/packages/ProbLog/problog_examples/viralmarketing.pl b/packages/ProbLog/problog_examples/viralmarketing.pl new file mode 100644 index 000000000..1a5ba3733 --- /dev/null +++ b/packages/ProbLog/problog_examples/viralmarketing.pl @@ -0,0 +1,114 @@ +%%% -*- Mode: Prolog; -*- + +% 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'). + +% Decisions +? :: marketed(P) :- person(P). + +% Utility attributes +buys(P) => 5 :- person(P). +marketed(P) => -2 :- person(P). + +% Probabilistic facts +0.2 :: buy_from_marketing(_). +0.3 :: buy_from_trust(_,_). + +% Background knowledge +person(bernd). +person(ingo). +person(theo). +person(angelika). +person(guy). +person(martijn). +person(laura). +person(kurt). + +trusts(X,Y) :- trusts_directed(X,Y). +trusts(X,Y) :- trusts_directed(Y,X). + +trusts_directed(bernd,ingo). +trusts_directed(ingo,theo). +trusts_directed(theo,angelika). +trusts_directed(bernd,martijn). +trusts_directed(ingo,martijn). +trusts_directed(martijn,guy). +trusts_directed(guy,theo). +trusts_directed(guy,angelika). +trusts_directed(laura,ingo). +trusts_directed(laura,theo). +trusts_directed(laura,guy). +trusts_directed(laura,martijn). +trusts_directed(kurt,bernd). + +buys(X) :- buys(X,[X]). + +buys(X, _) :- + marketed(X), + buy_from_marketing(X). +buys(X, Visited) :- + trusts(X,Y), + buy_from_trust(X,Y), + absent(Y,Visited), + buys(Y, [Y|Visited]). + +absent(_,[]). +absent(X,[Y|Z]):-X \= Y, absent(X,Z). + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% EXAMPLE USE:: +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% +% Find the globally optimal strategy. +% +% ?- dtproblog_solve(Strategy,ExpectedValue). +% ExpectedValue = 3.21097, +% Strategy = [marketed(martijn),marketed(guy),marketed(theo),marketed(ingo)] +% +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% +% Compute the expected value for a given strategy. +% +% ?- dtproblog_ev([marketed(martijn),marketed(laura)],ExpectedValue). +% ExpectedValue = 2.35771065 +% +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% +% Find a locally optimal strategy. +% +% ?- set_problog_flag(optimization, local), dtproblog_solve(Strategy,ExpectedValue). +% ExpectedValue = 3.19528, +% Strategy = [marketed(martijn),marketed(laura),marketed(guy),marketed(ingo)] +% +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% +% Find all ground utility facts in the theory. +% +% ?- dtproblog_utility_facts(Facts). +% Facts = [buys(bernd)=>5, buys(ingo)=>5, buys(theo)=>5, buys(angelika)=>5, buys(guy)=>5, buys(martijn)=>5, buys(laura)=>5, buys(kurt)=>5, marketed(bernd)=> -2, marketed(ingo)=> -2, marketed(theo)=> -2, marketed(angelika)=> -2, marketed(guy)=> -2, marketed(martijn)=> -2, marketed(laura)=> -2, marketed(kurt)=> -2] +% +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% +% Find all ground decisions relevant to the utility attributes. +% +% ?- dtproblog_decisions(Decisions). +% Decisions = [marketed(angelika), marketed(theo), marketed(kurt), marketed(ingo), marketed(laura), marketed(martijn), marketed(guy), marketed(bernd)] +% +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% +% Set the inference method to K-best to limit the complexity. This means that only the K most likely proofs for each utility attribute are considered as an underestimate of the probabilities and utilities. In the viral marketing example, this means that the probability that someone buys the product only depends on a limited number of other people in the social network, regardless of the size of the social network. +% Finding the globally optimal strategy under these simplifying assumptions yields a good but suboptimal strategy. +% +% ?- set_problog_flag(inference,20-best), dtproblog_solve(Strategy,ExpectedValue). +% ExpectedValue = 2.62531, +% Strategy = [marketed(martijn),marketed(guy),marketed(ingo),marketed(laura)] +% +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% +% The expected value returned in the previous example is an underestimate of the real expected value of the strategy found, which can be computed as +% +% ?- set_problog_flag(inference,exact), dtproblog_ev([marketed(martijn), marketed(guy), marketed(ingo), marketed(laura)], ExpectedValue). +% ExpectedValue = 3.1952798 +% +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% diff --git a/packages/ProbLog/problog_examples/viralmarketing_tabled.pl b/packages/ProbLog/problog_examples/viralmarketing_tabled.pl new file mode 100644 index 000000000..18fda0dca --- /dev/null +++ b/packages/ProbLog/problog_examples/viralmarketing_tabled.pl @@ -0,0 +1,106 @@ +%%% -*- Mode: Prolog; -*- + +% 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'). + +% Decisions +? :: marketed(P) :- person(P). + +% Utility attributes +buys(P) => 5 :- person(P). +marketed(P) => -2 :- person(P). + +% Probabilistic facts +0.2 :: buy_from_marketing(_). +0.3 :: buy_from_trust(_,_). + +% Background knowledge +person(bernd). +person(ingo). +person(theo). +person(angelika). +person(guy). +person(martijn). +person(laura). +person(kurt). + +trusts(X,Y) :- trusts_directed(X,Y). +trusts(X,Y) :- trusts_directed(Y,X). + +trusts_directed(bernd,ingo). +trusts_directed(ingo,theo). +trusts_directed(theo,angelika). +trusts_directed(bernd,martijn). +trusts_directed(ingo,martijn). +trusts_directed(martijn,guy). +trusts_directed(guy,theo). +trusts_directed(guy,angelika). +trusts_directed(laura,ingo). +trusts_directed(laura,theo). +trusts_directed(laura,guy). +trusts_directed(laura,martijn). +trusts_directed(kurt,bernd). + +% The buys predicate is tabled to speed up exact inference. K-best inference does not support tabled predicates. + +% Add this before a tabled predicate. +:- dynamic buys/1. + +buys(X) :- + marketed(X), + buy_from_marketing(X). +buys(X) :- + trusts(X,Y), + buy_from_trust(X,Y), + buys(Y). + +% Add this after a tabled predicate. +:- problog_table buys/1. +:- set_problog_flag(use_db_trie, true). +:- set_problog_flag(use_old_trie, false). + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% EXAMPLE USE:: +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% +% Find the globally optimal strategy. +% +% ?- dtproblog_solve(Strategy,ExpectedValue). +% ExpectedValue = 3.21097, +% Strategy = [marketed(martijn),marketed(guy),marketed(theo),marketed(ingo)] +% +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% +% Compute the expected value for a given strategy. +% +% ?- dtproblog_ev([marketed(martijn),marketed(laura)],ExpectedValue). +% ExpectedValue = 2.35771065 +% +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% +% Find a locally optimal strategy. +% +% ?- set_problog_flag(optimization, local), dtproblog_solve(Strategy,ExpectedValue). +% ExpectedValue = 3.19528, +% Strategy = [marketed(martijn),marketed(laura),marketed(guy),marketed(ingo)] +% +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% +% Find all ground utility facts in the theory. +% +% ?- dtproblog_utility_facts(Facts). +% Facts = [buys(bernd)=>5, buys(ingo)=>5, buys(theo)=>5, buys(angelika)=>5, buys(guy)=>5, buys(martijn)=>5, buys(laura)=>5, buys(kurt)=>5, marketed(bernd)=> -2, marketed(ingo)=> -2, marketed(theo)=> -2, marketed(angelika)=> -2, marketed(guy)=> -2, marketed(martijn)=> -2, marketed(laura)=> -2, marketed(kurt)=> -2] +% +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% +% Find all ground decisions relevant to the utility attributes. +% +% ?- dtproblog_decisions(Decisions). +% Decisions = [marketed(angelika), marketed(theo), marketed(kurt), marketed(ingo), marketed(laura), marketed(martijn), marketed(guy), marketed(bernd)] +% +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% +% (K-best inference and optimization does not support tabled predicates. Please use the non-tabled viral marketing example.) +% +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% diff --git a/packages/ProbLog/simplecudd/Makefile.in b/packages/ProbLog/simplecudd/Makefile.in index 6f56b62db..c3192b0c6 100644 --- a/packages/ProbLog/simplecudd/Makefile.in +++ b/packages/ProbLog/simplecudd/Makefile.in @@ -17,6 +17,10 @@ BINDIR = $(EROOTDIR)/bin LIBDIR=@libdir@ YAPLIBDIR=@libdir@/Yap # +# where YAP should look for architecture-independent Prolog libraries +# +SHAREDIR=$(ROOTDIR)/share +# # CC=@CC@ # @@ -34,30 +38,25 @@ SO=@SO@ CWD=$(PWD) # -CUDD = cudd-2.4.1 DYNAMIC = CFLAGS = @CFLAGS@ INCLUDE = @CUDD_CPPFLAGS@ LINKFLAGS = -lm LINKLIBS = @CUDD_LDFLAGS@ -default: Example ProbLogBDD +default: problogbdd -Example: Example.o simplecudd.o general.o - @echo Making Example... - @echo Copyright T. Mantadelis and Katholieke Universiteit Leuven 2008 - $(CC) Example.o simplecudd.o general.o $(LINKLIBS) $(LINKFLAGS) -o Example - -ProbLogBDD: ProblogBDD.o simplecudd.o general.o - @echo Making ProblogBDD... - @echo Copyright T. Mantadelis, A. Kimmig, B. Gutmann and Katholieke Universiteit Leuven 2008 - $(CC) ProblogBDD.o simplecudd.o general.o $(LINKLIBS) $(LINKFLAGS) -o ProblogBDD +problogbdd: problogbdd.o simplecudd.o general.o problogmath.o + @echo Making problogbdd... + @echo Copyright Katholieke Universiteit Leuven 2008 + @echo Authors: T. Mantadelis, A. Kimmig, B. Gutmann, I. Thon, G. Van den Broeck + $(CC) problogbdd.o simplecudd.o general.o problogmath.o $(LINKLIBS) $(LINKFLAGS) -o problogbdd %.o : $(srcdir)/%.c $(CC) $(CFLAGS) $(INCLUDE) $(DYNAMIC) -c $< -clean: - rm -f *.o ProblogBDD Example +clean: + rm -f *.o problogbdd install: default - $(INSTALL_PROGRAM) ProblogBDD $(DESTDIR)$(YAPLIBDIR) + $(INSTALL_PROGRAM) problogbdd $(DESTDIR)$(SHAREDIR)/Yap diff --git a/packages/ProbLog/simplecudd/ProblogBDD.c b/packages/ProbLog/simplecudd/ProblogBDD.c deleted file mode 100644 index 052a055f9..000000000 --- a/packages/ProbLog/simplecudd/ProblogBDD.c +++ /dev/null @@ -1,718 +0,0 @@ -/******************************************************************************\ -* * -* 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 * -* * -* Author: Theofrastos Mantadelis, Angelika Kimmig, Bernd Gutmann * -* File: ProblogBDD.c * -* * -******************************************************************************** -* * -* 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. * -* * -* The End * -* * -\******************************************************************************/ - -#include "simplecudd.h" -#include - -typedef struct _parameters { - int loadfile; - int savedfile; - int exportfile; - int inputfile; - int debug; - int errorcnt; - int *error; - int method; - int queryid; - int timeout; - double sigmoid_slope; - int online; - int maxbufsize; - char *ppid; -} parameters; - -typedef struct _gradientpair { - double probability; - double gradient; -} gradientpair; - -typedef struct _extmanager { - DdManager *manager; - DdNode *t, *f; - hisqueue *his; - namedvars varmap; -} extmanager; - -int argtype(const char *arg); -void printhelp(int argc, char **arg); -parameters loadparam(int argc, char **arg); -parameters params; - -void handler(int num); -void pidhandler(int num); -void termhandler(int num); - -double sigmoid(double x, double slope); -void myexpand(extmanager MyManager, DdNode *Current); -double CalcProbability(extmanager MyManager, DdNode *Current); -double CalcProbabilitySigmoid(extmanager MyManager, DdNode *Current); -gradientpair CalcGradient(extmanager MyManager, DdNode *Current, int TargetVar, char *TargetPattern); -int patterncalculated(char *pattern, extmanager MyManager, int loc); -char * extractpattern(char *thestr); - -int main(int argc, char **arg) { - extmanager MyManager; - DdNode *bdd; - bddfileheader fileheader; - int i, ivarcnt, code; - gradientpair tvalue; - double probability = -1.0; - char *varpattern; - varpattern = NULL; - code = -1; - params = loadparam(argc, arg); - - if (params.errorcnt > 0) { - printhelp(argc, arg); - for (i = 0; i < params.errorcnt; i++) { - fprintf(stderr, "Error: not known or error at parameter %s.\n", arg[params.error[i]]); - } - return -1; - } - - if (params.online == 0 && params.loadfile == -1) { - printhelp(argc, arg); - fprintf(stderr, "Error: you must specify a loading file.\n"); - return -1; - } - - if (params.method != 0 && arg[params.method][0] != 'g' && arg[params.method][0] != 'p' && arg[params.method][0] != 'o' && arg[params.method][0] != 'l') { - printhelp(argc, arg); - fprintf(stderr, "Error: you must choose a calculation method beetween [p]robability, [g]radient, [l]ine search, [o]nline.\n"); - return -1; - } - - if (params.method != 0 && (arg[params.method][0] == 'g' || arg[params.method][0] == 'p' || arg[params.method][0] == 'l') && params.inputfile == -1) { - printhelp(argc, arg); - fprintf(stderr, "Error: an input file is necessary for probability, gradient or line search calculation methods.\n"); - return -1; - } - - if (params.debug) DEBUGON; - RAPIDLOADON; - SETMAXBUFSIZE(params.maxbufsize); - - signal(SIGINT, termhandler); - if (params.ppid != NULL) { - signal(SIGALRM, pidhandler); - alarm(5); - } else { - signal(SIGALRM, handler); - alarm(params.timeout); - } - if (params.online) { - MyManager.manager = simpleBDDinit(0); - MyManager.t = HIGH(MyManager.manager); - MyManager.f = LOW(MyManager.manager); - MyManager.varmap = InitNamedVars(1, 0); - bdd = OnlineGenerateBDD(MyManager.manager, &MyManager.varmap); - ivarcnt = GetVarCount(MyManager.manager); - } else { - fileheader = ReadFileHeader(arg[params.loadfile]); - switch(fileheader.filetype) { - case BDDFILE_SCRIPT: - MyManager.manager = simpleBDDinit(fileheader.varcnt); - MyManager.t = HIGH(MyManager.manager); - MyManager.f = LOW(MyManager.manager); - MyManager.varmap = InitNamedVars(fileheader.varcnt, fileheader.varstart); - bdd = FileGenerateBDD(MyManager.manager, MyManager.varmap, fileheader); - ivarcnt = fileheader.varcnt; - break; - case BDDFILE_NODEDUMP: - MyManager.manager = simpleBDDinit(fileheader.varcnt); - MyManager.t = HIGH(MyManager.manager); - MyManager.f = LOW(MyManager.manager); - MyManager.varmap = InitNamedVars(fileheader.varcnt, fileheader.varstart); - bdd = LoadNodeDump(MyManager.manager, MyManager.varmap, fileheader.inputfile); - ivarcnt = fileheader.varcnt; - break; - default: - fprintf(stderr, "Error: not a valid file format to load.\n"); - return -1; - break; - } - } - alarm(0); - - // problem specifics - - if (bdd != NULL) { - ivarcnt = RepairVarcnt(&MyManager.varmap); - code = 0; - if (params.inputfile != -1) { - if (LoadVariableData(MyManager.varmap, arg[params.inputfile]) == -1) return -1; - if (!all_loaded(MyManager.varmap, 1)) return -1; - } - MyManager.his = InitHistory(ivarcnt); - if (params.method != 0) { - switch(arg[params.method][0]) { - case 'g': - for (i = 0; i < MyManager.varmap.varcnt; i++) { - if (MyManager.varmap.vars[i] != NULL) { - varpattern = extractpattern(MyManager.varmap.vars[i]); - if ((varpattern == NULL) || (!patterncalculated(varpattern, MyManager, i))) { - tvalue = CalcGradient(MyManager, bdd, i + MyManager.varmap.varstart, varpattern); - probability = tvalue.probability; - double factor = sigmoid(MyManager.varmap.dvalue[i], params.sigmoid_slope) * (1 - sigmoid(MyManager.varmap.dvalue[i], params.sigmoid_slope)) * params.sigmoid_slope; - if (varpattern == NULL) { - printf("query_gradient(%s,%s,%1.12f).\n", arg[params.queryid], MyManager.varmap.vars[i], tvalue.gradient * factor); - } else { - varpattern[strlen(varpattern) - 2] = '\0'; - printf("query_gradient(%s,%s,%1.12f).\n", arg[params.queryid], varpattern, tvalue.gradient * factor); - } - ReInitHistory(MyManager.his, MyManager.varmap.varcnt); - } - if (varpattern != NULL) free(varpattern); - } else { - fprintf(stderr, "Error: no variable name given for parameter.\n"); - } - } - if (probability < 0.0) { - // no nodes, so we have to calculate probability ourself - tvalue = CalcGradient(MyManager, bdd, 0 + MyManager.varmap.varstart, NULL); - probability = tvalue.probability; - } - printf("query_probability(%s,%1.12f).\n", arg[params.queryid], probability); - break; - case 'l': - tvalue = CalcGradient(MyManager, bdd, 0 + MyManager.varmap.varstart, NULL); - probability = tvalue.probability; - printf("query_probability(%s,%1.12f).\n", arg[params.queryid], probability); - break; - case 'p': - printf("probability(%1.12f).\n", CalcProbability(MyManager, bdd)); - break; - case 'o': - onlinetraverse(MyManager.manager, MyManager.varmap, MyManager.his, bdd); - break; - default: - myexpand(MyManager, bdd); - break; - } - } else { - myexpand(MyManager, bdd); - } - if (params.savedfile > -1) SaveNodeDump(MyManager.manager, MyManager.varmap, bdd, arg[params.savedfile]); - if (params.exportfile > -1) simpleNamedBDDtoDot(MyManager.manager, MyManager.varmap, bdd, arg[params.exportfile]); - ReInitHistory(MyManager.his, MyManager.varmap.varcnt); - free(MyManager.his); - } - if (MyManager.manager != NULL) { - KillBDD(MyManager.manager); - free(MyManager.varmap.dvalue); - free(MyManager.varmap.ivalue); - if (MyManager.varmap.dynvalue != NULL) { - for(i = 0; i < MyManager.varmap.varcnt; i++) - if (MyManager.varmap.dynvalue[i] != NULL) { - free(MyManager.varmap.dynvalue[i]); - } - free(MyManager.varmap.dynvalue); - } - for (i = 0; i < MyManager.varmap.varcnt; i++) - free(MyManager.varmap.vars[i]); - free(MyManager.varmap.vars); - } - if (params.error != NULL) free(params.error); - - return code; - -} - -/* Shell Parameters handling */ - -int argtype(const char *arg) { - if (strcmp(arg, "-l") == 0 || strcmp(arg, "--load") == 0) return 0; - if (strcmp(arg, "-e") == 0 || strcmp(arg, "--export") == 0) return 2; - if (strcmp(arg, "-m") == 0 || strcmp(arg, "--method") == 0) return 3; - if (strcmp(arg, "-i") == 0 || strcmp(arg, "--input") == 0) return 4; - if (strcmp(arg, "-h") == 0 || strcmp(arg, "--help") == 0) return 5; - if (strcmp(arg, "-d") == 0 || strcmp(arg, "--debug") == 0) return 6; - if (strcmp(arg, "-id") == 0 || strcmp(arg, "--queryid") == 0) return 7; - if (strcmp(arg, "-t") == 0 || strcmp(arg, "--timeout") == 0) return 8; - if (strcmp(arg, "-sd") == 0 || strcmp(arg, "--savedump") == 0) return 9; - if (strcmp(arg, "-sl") == 0 || strcmp(arg, "--slope") == 0) return 10; - if (strcmp(arg, "-o") == 0 || strcmp(arg, "--online") == 0) return 11; - if (strcmp(arg, "-bs") == 0 || strcmp(arg, "--bufsize") == 0) return 12; - if (strcmp(arg, "-pid") == 0 || strcmp(arg, "--pid") == 0) return 13; - return -1; -} - -void printhelp(int argc, char **arg) { - fprintf(stderr, "\nUsage: %s -l [filename] -i [filename] -o (-s(d) [filename] -e [filename] -m [method] -id [queryid] -sl [double]) (-t [seconds] -d -h)\n", arg[0]); - fprintf(stderr, "Generates and traverses a BDD\nMandatory parameters:\n"); - fprintf(stderr, "\t-l [filename]\t->\tfilename to load supports two formats:\n\t\t\t\t\t\t1. script with generation instructions\n\t\t\t\t\t\t2. node dump saved file\n"); - fprintf(stderr, "\t-i [filename]\t->\tfilename to input problem specifics (mandatory with file formats 1, 2)\n"); - fprintf(stderr, "\t-o\t\t->\tgenerates the BDD in online mode instead from a file can be used instead of -l\n"); - fprintf(stderr, "Optional parameters:\n"); - fprintf(stderr, "\t-sd [filename]\t->\tfilename to save generated BDD in node dump format (fast loading, traverse valid only)\n"); - fprintf(stderr, "\t-e [filename]\t->\tfilename to export generated BDD in dot format\n"); - fprintf(stderr, "\t-m [method]\t->\tthe calculation method to be used: none(default), [p]robability, [g]radient, [l]ine search, [o]nline\n"); - fprintf(stderr, "\t-id [queryid]\t->\tthe queries identity name (used by gradient) default: %s\n", arg[0]); - fprintf(stderr, "\t-sl [double]\t->\tthe sigmoid slope (used by gradient) default: 1.0\n"); - fprintf(stderr, "Extra parameters:\n"); - fprintf(stderr, "\t-t [seconds]\t->\tthe seconds (int) for BDD generation timeout default 0 = no timeout\n"); - fprintf(stderr, "\t-pid [pid]\t->\ta process id (int) to check for termination default 0 = no process to check\n"); - fprintf(stderr, "\t-bs [bytes]\t->\tthe bytes (int) to use as a maximum buffer size to read files default 0 = no max\n"); - fprintf(stderr, "\t-d\t\t->\tRun in debug mode (gives extra messages in stderr)\n"); - fprintf(stderr, "\t-h\t\t->\tHelp (displays this message)\n\n"); - fprintf(stderr, "Example: %s -l testbdd -i input.txt -m g -id testbdd\n", arg[0]); -} - -parameters loadparam(int argc, char **arg) { - int i; - parameters params; - params.loadfile = -1; - params.savedfile = -1; - params.exportfile = -1; - params.method = 0; - params.inputfile = -1; - params.debug = 0; - params.errorcnt = 0; - params.queryid = 0; - params.timeout = 0; - params.sigmoid_slope = 1.0; - params.online = 0; - params.maxbufsize = 0; - params.ppid = NULL; - params.error = (int *) malloc(argc * sizeof(int)); - for (i = 1; i < argc; i++) { - switch(argtype(arg[i])) { - case 0: - if (argc > i + 1) { - i++; - params.loadfile = i; - } else { - params.error[params.errorcnt] = i; - params.errorcnt++; - } - break; - case 2: - if (argc > i + 1) { - i++; - params.exportfile = i; - } else { - params.error[params.errorcnt] = i; - params.errorcnt++; - } - break; - case 3: - if (argc > i + 1) { - i++; - params.method = i; - } else { - params.error[params.errorcnt] = i; - params.errorcnt++; - } - break; - case 4: - if (argc > i + 1) { - i++; - params.inputfile = i; - } else { - params.error[params.errorcnt] = i; - params.errorcnt++; - } - break; - case 5: - printhelp(argc, arg); - break; - case 6: - params.debug = 1; - break; - case 7: - if (argc > i + 1) { - i++; - params.queryid = i; - } else { - params.error[params.errorcnt] = i; - params.errorcnt++; - } - break; - case 8: - if ((argc > i + 1) && (IsPosNumber(arg[i + 1]))) { - i++; - params.timeout = atoi(arg[i]); - } else { - params.error[params.errorcnt] = i; - params.errorcnt++; - } - break; - case 9: - if (argc > i + 1) { - i++; - params.savedfile = i; - } else { - params.error[params.errorcnt] = i; - params.errorcnt++; - } - break; - case 10: - if ((argc > i + 1) && (IsRealNumber(arg[i + 1]))) { - i++; - params.sigmoid_slope = atof(arg[i]); - } else { - params.error[params.errorcnt] = i; - params.errorcnt++; - } - break; - case 11: - params.online = 1; - break; - case 12: - if ((argc > i + 1) && (IsPosNumber(arg[i + 1]))) { - i++; - params.maxbufsize = atoi(arg[i]); - } else { - params.error[params.errorcnt] = i; - params.errorcnt++; - } - break; - case 13: - if ((argc > i + 1) && (IsPosNumber(arg[i + 1]))) { - i++; - params.ppid = (char *) malloc(sizeof(char) * (strlen(arg[i]) + 1)); - strcpy(params.ppid, arg[i]); - } else { - params.error[params.errorcnt] = i; - params.errorcnt++; - } - break; - default: - params.error[params.errorcnt] = i; - params.errorcnt++; - break; - } - } - return params; -} - -/* Error Handlers */ - -void handler(int num) { - fprintf(stderr, "Error: Timeout %i exceeded.\n", params.timeout); - exit(-1); -} - -void pidhandler(int num) { - char *s; - if (params.timeout > 0) { - params.timeout -= 5; - if (params.timeout <= 0) { - fprintf(stderr, "Error: Timeout exceeded.\n"); - exit(-1); - } - } - s = (char *) malloc(sizeof(char) * (19 + strlen(params.ppid))); - strcpy(s, "ps "); strcat(s, params.ppid); strcat(s, " >/dev/null"); - if (system(s) != 0) exit(4); - signal(SIGALRM, pidhandler); - alarm(5); - free(s); -} - -void termhandler(int num) { - exit(3); -} - -/* General Functions */ - -double sigmoid(double x, double slope) { - return 1 / (1 + exp(-x * slope)); -} - -/* Debugging traverse function */ - -void myexpand(extmanager MyManager, DdNode *Current) { - DdNode *h, *l; - hisnode *Found; - char *curnode; - curnode = GetNodeVarNameDisp(MyManager.manager, MyManager.varmap, Current); - printf("%s\n", curnode); - if ((Current != MyManager.t) && (Current != MyManager.f) && - ((Found = GetNode(MyManager.his, MyManager.varmap.varstart, Current)) == NULL)) { - l = LowNodeOf(MyManager.manager, Current); - h = HighNodeOf(MyManager.manager, Current); - printf("l(%s)->", curnode); - myexpand(MyManager, l); - printf("h(%s)->", curnode); - myexpand(MyManager, h); - AddNode(MyManager.his, MyManager.varmap.varstart, Current, 0.0, 0, NULL); - } -} - -/* Angelicas Algorithm */ - -double CalcProbability(extmanager MyManager, DdNode *Current) { - DdNode *h, *l; - hisnode *Found; - char *curnode; - double lvalue, hvalue, tvalue; - if (params.debug) { - curnode = GetNodeVarNameDisp(MyManager.manager, MyManager.varmap, Current); - fprintf(stderr, "%s\n", curnode); - } - if (Current == MyManager.t) return 1.0; - if (Current == MyManager.f) return 0.0; - if ((Found = GetNode(MyManager.his, MyManager.varmap.varstart, Current)) != NULL) return Found->dvalue; - l = LowNodeOf(MyManager.manager, Current); - h = HighNodeOf(MyManager.manager, Current); - if (params.debug) fprintf(stderr, "l(%s)->", curnode); - lvalue = CalcProbability(MyManager, l); - if (params.debug) fprintf(stderr, "h(%s)->", curnode); - hvalue = CalcProbability(MyManager, h); - tvalue = MyManager.varmap.dvalue[GetIndex(Current) - MyManager.varmap.varstart]; - tvalue = tvalue * hvalue + lvalue * (1.0 - tvalue); - AddNode(MyManager.his, MyManager.varmap.varstart, Current, tvalue, 0, NULL); - return tvalue; -} - -/* Bernds Algorithm */ - -gradientpair CalcGradient(extmanager MyManager, DdNode *Current, int TargetVar, char *TargetPattern) { - DdNode *h, *l; - hisnode *Found; - char *curnode; - gradientpair lvalue, hvalue, tvalue; - double this_probability; - double *gradient; - if (params.debug) { - curnode = GetNodeVarNameDisp(MyManager.manager, MyManager.varmap, Current); - fprintf(stderr, "%s\n", curnode); - } - if (Current == MyManager.t) { - tvalue.probability = 1.0; - tvalue.gradient = 0.0; - return tvalue; - } - if (Current == MyManager.f) { - tvalue.probability = 0.0; - tvalue.gradient = 0.0; - return tvalue; - } - if ((Found = GetNode(MyManager.his, MyManager.varmap.varstart, Current)) != NULL) { - tvalue.probability = Found->dvalue; - tvalue.gradient = *((double *) Found->dynvalue); - return tvalue; - } - l = LowNodeOf(MyManager.manager, Current); - h = HighNodeOf(MyManager.manager, Current); - if (params.debug) fprintf(stderr, "l(%s)->", curnode); - lvalue = CalcGradient(MyManager, l, TargetVar, TargetPattern); - if (params.debug) fprintf(stderr, "h(%s)->", curnode); - hvalue = CalcGradient(MyManager, h, TargetVar, TargetPattern); - this_probability = sigmoid(MyManager.varmap.dvalue[GetIndex(Current) - MyManager.varmap.varstart], params.sigmoid_slope); - tvalue.probability = this_probability * hvalue.probability + (1 - this_probability) * lvalue.probability; - tvalue.gradient = this_probability * hvalue.gradient + (1 - this_probability) * lvalue.gradient; - if ((GetIndex(Current) == TargetVar) || - ((TargetPattern != NULL) && patternmatch(TargetPattern, MyManager.varmap.vars[GetIndex(Current)]))) { - tvalue.gradient += hvalue.probability - lvalue.probability; - } - gradient = (double *) malloc(sizeof(double)); - *gradient = tvalue.gradient; - AddNode(MyManager.his, MyManager.varmap.varstart, Current, tvalue.probability, 0, gradient); - return tvalue; -} - -char * extractpattern(char *thestr) { - char *p; - int i = 0, sl = strlen(thestr); - while((thestr[i] != '_') && (i < sl)) i++; - if (i == sl) return NULL; - i++; - p = (char *) malloc(sizeof(char) * (i + 2)); - strncpy(p, thestr, i); - p[i] = '*'; - p[i + 1] = '\0'; - return p; -} - -int patterncalculated(char *pattern, extmanager MyManager, int loc) { - int i; - if (pattern == NULL) return 0; - for (i = loc - 1; i > -1; i--) - if (patternmatch(pattern, MyManager.varmap.vars[i])) return 1; - return 0; -} diff --git a/packages/ProbLog/simplecudd/SimpleCUDD.pl b/packages/ProbLog/simplecudd/SimpleCUDD.pl deleted file mode 100644 index 9fe1ecfb0..000000000 --- a/packages/ProbLog/simplecudd/SimpleCUDD.pl +++ /dev/null @@ -1,141 +0,0 @@ -:-use_module(library(system)). -%:-use_module(library(clib)). - -bdd_init(FDO, FDI, PID):- - exec('/home/theo/BDDs/SimpleCUDD/Version4/Example -online', [pipe(FDO), pipe(FDI), std], PID). - %process_create('/home/theo/BDDs/SimpleCUDD/Version3/Example', ['-online'], [stdin(pipe(FDI)), stdout(pipe(FDO)), process(PID)]). - -bdd_commit(FDO, LINE):- - write(FDO, LINE), - write(FDO, '\n'). - -bdd_kill(FDO, FDI, PID, S):- - bdd_commit(FDO, '@e'), - wait(PID, S), - %process_wait(PID, S), - close(FDO), - close(FDI). - -bdd_line([], X, _, L):- - atomic(X), - X \= [], - (bdd_curinter(N) -> - retract(bdd_curinter(N)) - ; - N = 1 - ), - M is N + 1, - assert(bdd_curinter(M)), - atomic_concat(['L', N, '=', X], L). - -bdd_line(L, X, O, NL):- - atomic(X), - X \= [], - atom(L), - L \= [], - atomic_concat([L, O, X], NL). - -bdd_line(L, [], _, L):-!. - -bdd_line(L, [X|T], O, R):- - bdd_line(L, X, O, NL), - bdd_line(NL, T, O, R). - -bdd_AND(L, X, NL):- - bdd_line(L, X, '*', NL). -bdd_OR(L, X, NL):- - bdd_line(L, X, '+', NL). -bdd_XOR(L, X, NL):- - bdd_line(L, X, '#', NL). -bdd_NAND(L, X, NL):- - bdd_line(L, X, '~*', NL). -bdd_NOR(L, X, NL):- - bdd_line(L, X, '~+', NL). -bdd_XNOR(L, X, NL):- - bdd_line(L, X, '~#', NL). - -bdd_not(X, NX):- - atomic(X), - atomic_concat(['~', X], NX). - -bdd_laststep(L):- - bdd_curinter(N), - M is N - 1, - atomic_concat(['L', M], L), - !. - -bdd_nextDFS(FDO):- - bdd_commit(FDO, '@n'). - -bdd_nextBFS(FDO):- - bdd_commit(FDO, '@n,BFS'). - -bdd_current(FDO, FDI, N, Qcnt):- - bdd_commit(FDO, '@c'), - read(FDI, F), - assert(F), - bdd_temp_value(N, Qcnt), - retract(F). - -bdd_highnodeof(FDO, FDI, H):- - bdd_commit(FDO, '@h'), - read(FDI, F), - assert(F), - bdd_temp_value(H), - retract(F). - -bdd_lownodeof(FDO, FDI, L):- - bdd_commit(FDO, '@l'), - read(FDI, F), - assert(F), - bdd_temp_value(L), - retract(F). - -bdd_nodevaluesof(FDO, FDI, N, V):- - atomic_concat(['@v,', N], Q), - bdd_commit(FDO, Q), - read(FDI, F), - assert(F), - bdd_temp_value(V), - retract(F). -/* -bdd_addnodetohis(FDO, N, [D, I, Dyn]):- - atomic_concat(['@a,', N, ',', D, ',', I, ',', Dyn], Q), - bdd_commit(FDO, Q). - -bdd_getnodefromhis(FDO, FDI, N, V):- - atomic_concat(['@g,', N], Q), - bdd_commit(FDO, Q), - read(FDI, F), - assert(F), - bdd_temp_value(V), - retract(F). -*/ - -runme:- - bdd_init(FDO, FDI, PID), - bdd_AND([], ['A', 'B', 'C', 'D', 'E'], L1), - bdd_laststep(L1S), - bdd_commit(FDO, L1), - bdd_AND([], ['A', 'F', 'G', '~B'], L2), - bdd_laststep(L2S), - bdd_commit(FDO, L2), - bdd_AND([], ['A', 'F', 'G', '~C'], L3), - bdd_laststep(L3S), - bdd_commit(FDO, L3), - bdd_OR([], [L1S, L2S, L3S], L4), - bdd_laststep(L4S), - bdd_commit(FDO, L4), - bdd_commit(FDO, L4S), - - repeat, - bdd_current(FDO, FDI, N, I), - write(1),nl, - bdd_nodevaluesof(FDO, FDI, N, V), - write(N), write(' ('), write(V), write(')'), nl, - bdd_next(FDO), - I = 0, (N = 'TRUE' ; N = 'FALSE'), - - bdd_kill(FDO, FDI, PID, S), - write('BDD terminated with state: '), write(S), nl. - diff --git a/packages/ProbLog/simplecudd/general.c b/packages/ProbLog/simplecudd/general.c index 658ad6d2c..4637ff209 100644 --- a/packages/ProbLog/simplecudd/general.c +++ b/packages/ProbLog/simplecudd/general.c @@ -3,7 +3,7 @@ * 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 * +* Copyright Katholieke Universiteit Leuven 2008, 2009, 2010 * * * * Author: Theofrastos Mantadelis * * File: general.c * diff --git a/packages/ProbLog/simplecudd/general.h b/packages/ProbLog/simplecudd/general.h index 618c9a92e..e05def182 100644 --- a/packages/ProbLog/simplecudd/general.h +++ b/packages/ProbLog/simplecudd/general.h @@ -3,7 +3,7 @@ * 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 * +* Copyright Katholieke Universiteit Leuven 2008, 2009, 2010 * * * * Author: Theofrastos Mantadelis * * File: general.h * diff --git a/packages/ProbLog/simplecudd/problogbdd.c b/packages/ProbLog/simplecudd/problogbdd.c new file mode 100644 index 000000000..f3f8b8044 --- /dev/null +++ b/packages/ProbLog/simplecudd/problogbdd.c @@ -0,0 +1,1998 @@ +/******************************************************************************\ +* * +* 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 * +* * +* Author: Theofrastos Mantadelis, Angelika Kimmig, Bernd Gutmann * +* File: ProblogBDD.c * +* * +******************************************************************************** +* * +* 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. * +* * +* The End * +* * +\******************************************************************************/ + +//#include +//#include +#include "simplecudd.h" +#include "problogmath.h" +#include +#include + +#define VERSION "2.0.1" + + +#ifndef max + #define max( a, b ) ( ((a) > (b)) ? (a) : (b) ) +#endif + +// INFINITY macro does not work on trantor (64-bit linux of some kind) +const double my_infinity = 1.0/0.0; + +typedef struct _parameters { + int loadfile; + int savedfile; + int exportfile; + int inputfile; + int debug; + int errorcnt; + int *error; + int method; + int queryid; + int timeout; + double sigmoid_slope; + int online; + int maxbufsize; + char *ppid; + int orderfile; + int utilfile; + int independent_forest; + int local_search; + int dynreorder; + int staticorder; +} parameters; + +typedef struct _gradientpair { + double probability; + double gradient; +} gradientpair; + +typedef struct _extmanager { + DdManager *manager; + DdNode *t, *f; + hisqueue *his; + namedvars varmap; +} extmanager; + +typedef struct _bdd_mgr { + extmanager extmanager; + DdNode *root; +} bdd_mgr; + + +int argtype(const char *arg); +void printhelp(int argc, char **arg); +parameters loadparam(int argc, char **arg); +parameters params; + +void handler(int num); +void pidhandler(int num); +void termhandler(int num); + +void myexpand(extmanager MyManager, DdNode *Current); +double CalcProbability(extmanager MyManager, DdNode *Current); +double CalcProbabilitySigmoid(extmanager MyManager, DdNode *Current); +gradientpair CalcGradient(extmanager MyManager, DdNode *Current, int TargetVar, char *TargetPattern, int type); +double CalcExpectedCounts(extmanager MyManager, DdNode *Current); +int patterncalculated(char *pattern, extmanager MyManager, int loc); +char * extractpattern(char *thestr); + +// added by GUY +double* read_util_file(char * filename); +void exact_strategy_search(extmanager* MyManager, DdNode **forest, double* utilities); +DdNode* buildADDfromBDD(extmanager* MyManager, DdNode *Current, DdManager* addmgr); +void ReInitAndUnrefHistory(hisqueue *HisQueue, int varcnt, DdManager* mgr); +int extractstrategy(extmanager* MyManager, DdManager * add_mgr, DdNode *Current, DdNode *max_node); +DdNode * setLowerBound(DdManager * dd, DdNode * f, double lowerBound); +DdNode * setLowerBoundRecur(DdManager * dd, DdNode * f, double lowerBound); +void local_strategy_search(extmanager* MyManager, DdNode **forest, double* utilities); +void local_strategy_search_independent(bdd_mgr* bdd_mgrs, double* utilities, int nb_bdds, namedvars globalvars); +double expected_value(extmanager* MyManager, DdNode **forest, double* utilities); +void print_strategy(namedvars varmap); +void newManager(extmanager* MyManager,bddfileheader fileheader, int nbManagers); +bdd_mgr* generateIndependentBDDForest(bddfileheader fileheader); +int LoadVariableDataForForest(namedvars varmap, char *filename); + +int main(int argc, char **arg) { + extmanager MyManager; + DdNode *bdd, **forest, *bakbdd; + bddfileheader fileheader; + int i, ivarcnt, code, curbdd; + gradientpair tvalue; + double probability = -1.0; + char *varpattern; + bdd_mgr* bdd_mgrs; + varpattern = NULL; + code = -1; + params = loadparam(argc, arg); + + //Initializin to NULL to be safe? + bdd = NULL; + forest = NULL; + bdd_mgrs = NULL; + + if (params.errorcnt > 0) { + printhelp(argc, arg); + for (i = 0; i < params.errorcnt; i++) { + fprintf(stderr, "Error: not known or error at parameter %s.\n", arg[params.error[i]]); + } + return -1; + } + + if (params.online == 0 && params.loadfile == -1) { + printhelp(argc, arg); + fprintf(stderr, "Error: you must specify a loading file.\n"); + return -1; + } + + if (params.method != 0 && arg[params.method][0] != 'g' && arg[params.method][0] != 'p' && arg[params.method][0] != 'o' && arg[params.method][0] != 'l' && arg[params.method][0] != 's' && arg[params.method][0] != 'e') { + printhelp(argc, arg); + fprintf(stderr, "Error: you must choose a calculation method beetween [p]robability, [g]radient, [l]ine search, [s]earch for strategy, [o]nline, [e]xpected counts.\n"); + return -1; + } + + if (params.method != 0 && (arg[params.method][0] == 'g' || arg[params.method][0] == 'p' || arg[params.method][0] == 'l'|| arg[params.method][0] == 'e') && params.inputfile == -1) { + printhelp(argc, arg); + fprintf(stderr, "Error: an input file is necessary for probability, gradient, line search calculation or expected counts methods.\n"); + return -1; + } + + if (params.debug) DEBUGON; + RAPIDLOADON; + SETMAXBUFSIZE(params.maxbufsize); + + signal(SIGINT, termhandler); + if (params.ppid != NULL) { + signal(SIGALRM, pidhandler); + alarm(5); + } else { + signal(SIGALRM, handler); + alarm(params.timeout); + } + + if (params.online) { + if (params.dynreorder == 1) + MyManager.manager = simpleBDDinit(0); + else + MyManager.manager = simpleBDDinitNoReOrder(0); + MyManager.t = HIGH(MyManager.manager); + MyManager.f = LOW(MyManager.manager); + MyManager.varmap = InitNamedVars(1, 0); + bdd = OnlineGenerateBDD(MyManager.manager, &MyManager.varmap); + ivarcnt = GetVarCount(MyManager.manager); + } else if(params.independent_forest>0){ + // the flag to create a forest of independent bdds is set + fileheader = ReadFileHeader(arg[params.loadfile]); + if (_debug) fprintf(stderr,"Generating forest of independent BDDs.\n"); + bdd_mgrs = generateIndependentBDDForest(fileheader); + ivarcnt = fileheader.varcnt; + MyManager.varmap = InitNamedVars(fileheader.varcnt, fileheader.varstart); + } else{ + fileheader = ReadFileHeader(arg[params.loadfile]); + switch(fileheader.filetype) { + case BDDFILE_SCRIPT: + if (params.dynreorder == 1) + MyManager.manager = simpleBDDinit(fileheader.varcnt); + else + MyManager.manager = simpleBDDinitNoReOrder(fileheader.varcnt); + MyManager.t = HIGH(MyManager.manager); + MyManager.f = LOW(MyManager.manager); + MyManager.varmap = InitNamedVars(fileheader.varcnt, fileheader.varstart); + if (params.staticorder > 0) { + char **Order = GetVariableOrder(arg[params.staticorder], MyManager.varmap.varcnt); + for (i = 0; i < MyManager.varmap.varcnt; i++) + if (Order[i] != NULL) AddNamedVarAt(MyManager.varmap, Order[i], i); + } + if (fileheader.version > 1) { + forest = FileGenerateBDDForest(MyManager.manager, MyManager.varmap, fileheader); + bdd = forest[0]; + bakbdd = bdd; + } else { + forest = NULL; + bdd = FileGenerateBDD(MyManager.manager, MyManager.varmap, fileheader); + bakbdd = bdd; + } + ivarcnt = fileheader.varcnt; + break; + case BDDFILE_NODEDUMP: + if (params.dynreorder == 1) + MyManager.manager = simpleBDDinit(fileheader.varcnt); + else + MyManager.manager = simpleBDDinitNoReOrder(fileheader.varcnt); + MyManager.t = HIGH(MyManager.manager); + MyManager.f = LOW(MyManager.manager); + MyManager.varmap = InitNamedVars(fileheader.varcnt, fileheader.varstart); + bdd = LoadNodeDump(MyManager.manager, MyManager.varmap, fileheader.inputfile); + ivarcnt = fileheader.varcnt; + break; + default: + fprintf(stderr, "Error: not a valid file format to load.\n"); + return -1; + break; + } + } + + + alarm(0); + + // problem specifics + + if (params.method == 0 || arg[params.method][0] != 's') { + if (bdd != NULL || bdd_mgrs != NULL) { + ivarcnt = RepairVarcnt(&MyManager.varmap); + code = 0; + if (params.inputfile != -1) { + if (LoadVariableData(MyManager.varmap, arg[params.inputfile]) == -1) return -1; + if (!all_loaded(MyManager.varmap, 1)) return -1; + } + // impose a predifined order good for debugging + // can be used with a partial number of variables to impose ordering at beggining of BDD + if (params.orderfile != -1) { + ImposeOrder(MyManager.manager, MyManager.varmap, GetVariableOrder(arg[params.orderfile], MyManager.varmap.varcnt)); + } + curbdd = 0; + do { + MyManager.his = InitHistory(ivarcnt); + if (params.method != 0) { + switch(arg[params.method][0]) { + case 'g': + for (i = 0; i < MyManager.varmap.varcnt; i++) { + if (MyManager.varmap.vars[i] != NULL) { + + // check whether this is a continues fact + if (MyManager.varmap.dynvalue[i] == NULL) { // nope, regular fact + varpattern = extractpattern(MyManager.varmap.vars[i]); + if ((varpattern == NULL) || (!patterncalculated(varpattern, MyManager, i))) { + tvalue = CalcGradient(MyManager, bdd, i + MyManager.varmap.varstart, varpattern, 0); + probability = tvalue.probability; + if (varpattern == NULL) { + printf("query_gradient(%s,%s,p,%e).\n", arg[params.queryid], MyManager.varmap.vars[i], tvalue.gradient); + } else { + varpattern[strlen(varpattern) - 2] = '\0'; + printf("query_gradient(%s,%s,p,%e).\n", arg[params.queryid], varpattern, tvalue.gradient); + } + ReInitHistory(MyManager.his, MyManager.varmap.varcnt); + if (varpattern != NULL) free(varpattern); + } + } else { // it is! let's do the Hybrid Problog Magic + // first for mu + varpattern = extractpattern(MyManager.varmap.vars[i]); + if ((varpattern == NULL) || (!patterncalculated(varpattern, MyManager, i))) { + tvalue = CalcGradient(MyManager, bdd, i + MyManager.varmap.varstart, varpattern, 1); + probability = tvalue.probability; + if (varpattern == NULL) { + printf("query_gradient(%s,%s,mu,%e).\n", arg[params.queryid], MyManager.varmap.vars[i], tvalue.gradient); + } else { + varpattern[strlen(varpattern) - 2] = '\0'; + printf("query_gradient(%s,%s,mu,%e).\n", arg[params.queryid], varpattern, tvalue.gradient); + } + } + ReInitHistory(MyManager.his, MyManager.varmap.varcnt); + if (varpattern != NULL) free(varpattern); + + // then for sigma + varpattern = extractpattern(MyManager.varmap.vars[i]); + if ((varpattern == NULL) || (!patterncalculated(varpattern, MyManager, i))) { + tvalue = CalcGradient(MyManager, bdd, i + MyManager.varmap.varstart, varpattern, 2); + probability = tvalue.probability; + if (varpattern == NULL) { + printf("query_gradient(%s,%s,sigma,%e).\n", arg[params.queryid], MyManager.varmap.vars[i], tvalue.gradient); + } else { + varpattern[strlen(varpattern) - 2] = '\0'; + printf("query_gradient(%s,%s,sigma,%e).\n", arg[params.queryid], varpattern, tvalue.gradient); + } + } + ReInitHistory(MyManager.his, MyManager.varmap.varcnt); + if (varpattern != NULL) free(varpattern); + } + + } else { + fprintf(stderr, "Error: no variable name given for parameter.\n"); + } + } + if (probability < 0.0) { + // no nodes, so we have to calculate probability ourself + tvalue = CalcGradient(MyManager, bdd, 0 + MyManager.varmap.varstart, NULL, 0); + probability = tvalue.probability; + } + printf("query_probability(%s,%e).\n", arg[params.queryid], probability); + break; + case 'l': + tvalue = CalcGradient(MyManager, bdd, 0 + MyManager.varmap.varstart, NULL, 0); + probability = tvalue.probability; + printf("query_probability(%s,%e).\n", arg[params.queryid], probability); + break; + case 'e': + printf("probability(%e).\n", CalcExpectedCounts(MyManager, bdd)); + break; + case 'p': + printf("probability(%e).\n", CalcProbability(MyManager, bdd)); + break; + case 'o': + onlinetraverse(MyManager.manager, MyManager.varmap, MyManager.his, bdd); + break; + default: + myexpand(MyManager, bdd); + break; + } + } else { + myexpand(MyManager, bdd); + } + if (forest != NULL) { + curbdd++; + bdd = forest[curbdd]; + } else { + bdd = NULL; + } + // Guy: I removed it, why is it here? + // ReInitHistory(MyManager.his, MyManager.varmap.varcnt); + } while(bdd != NULL); + + bdd = bakbdd; + if (params.savedfile > -1) SaveNodeDump(MyManager.manager, MyManager.varmap, bdd, arg[params.savedfile]); + if (params.exportfile > -1) simpleNamedBDDtoDot(MyManager.manager, MyManager.varmap, bdd, arg[params.exportfile]); + } + }else{ + // param "s" is set + // do strategy search on the forest + code = 0; + if(params.independent_forest>0){ + //the forest consists of independent bdds + LoadVariableDataForForest(MyManager.varmap,arg[params.inputfile]); + if(bdd_mgrs[0].root == NULL){ + fprintf(stderr, "Error: No BDDs were generated.\n"); + return -1; + } + if (_debug) fprintf(stderr,"Initializing histories.\n"); + if(params.local_search>0){ + if (_debug) fprintf(stderr,"Independent local search.\n"); + local_strategy_search_independent(bdd_mgrs, read_util_file(arg[params.utilfile]), fileheader.intercnt, MyManager.varmap); + }else{ + if (_debug) fprintf(stderr,"Independent exact search.\n"); + fprintf(stderr, "Error: independent exact search not supported yet.\n"); + return -1; + } + }else{ + ivarcnt = RepairVarcnt(&MyManager.varmap); + //the forest is a bdd with multiple entry points + if (params.inputfile != -1) { + if (LoadVariableData(MyManager.varmap, arg[params.inputfile]) == -1) return -1; + if (!all_loaded(MyManager.varmap, 1)) return -1; + } + // impose a predifined order good for debugging + // can be used with a partial number of variables to impose ordering at beggining of BDD + if (params.orderfile != -1) { + ImposeOrder(MyManager.manager, MyManager.varmap, GetVariableOrder(arg[params.orderfile], MyManager.varmap.varcnt)); + } + MyManager.his = InitHistory(ivarcnt); + if (_debug) fprintf(stderr,"Initialized shared history with %i variables.\n", ivarcnt); + if(params.local_search>0){ + if (_debug) fprintf(stderr,"Local search.\n"); + local_strategy_search(&MyManager, forest, read_util_file(arg[params.utilfile])); + }else{ + if (_debug) fprintf(stderr,"Exact search.\n"); + exact_strategy_search(&MyManager, forest, read_util_file(arg[params.utilfile])); + } + free(MyManager.his); + } + print_strategy(MyManager.varmap); + } + if (_debug) fprintf(stderr,"Cleaning up.\n"); + if(params.independent_forest>0){ + // TODO clean up memory - the existing code gives an invalid pointer problem + }else{ + if (MyManager.manager != NULL) { + KillBDD(MyManager.manager); + free(MyManager.varmap.dvalue); + free(MyManager.varmap.ivalue); + if (MyManager.varmap.dynvalue != NULL) { + for(i = 0; i < MyManager.varmap.varcnt; i++) + if (MyManager.varmap.dynvalue[i] != NULL) { + free(MyManager.varmap.dynvalue[i]); + } + free(MyManager.varmap.dynvalue); + } + for (i = 0; i < MyManager.varmap.varcnt; i++) + free(MyManager.varmap.vars[i]); + free(MyManager.varmap.vars); + } + } + if (params.error != NULL) free(params.error); + + return code; + +} + +////////////////// +// Added by Guy // +////////////////// + +double* read_util_file(char *filename){ + FILE* file; + double line; + int nb_lines; + double *utils; + int i=0; + if ((file = fopen(filename, "r")) == NULL) { + perror(filename); + return NULL; + } + // Read file + fscanf(file, "%i\n", &nb_lines); + utils = (double *) malloc(sizeof(double)*nb_lines); + //utils = new double[nb_lines]; + while (!feof(file)) { + fscanf(file, "%lf\n", &line); + //fprintf(stderr,"read %g.\n", line); + if(i>nb_lines) { + fprintf(stderr,"The number of lines field of %i does not match the number of lines in the file.\n",nb_lines); + fclose(file); + exit(1); + } + utils[i++] = line; + } + fclose(file); + if(i!=nb_lines) { + fprintf(stderr,"The number of lines field of %i does not match the number of lines in the file %i.\n",nb_lines,i); + fclose(file); + exit(1); + } + if (params.debug) for(i=0;iutil_spread < ((const util_add*) B)->util_spread) return 1; + else if (((const util_add*) A)->util_spread > ((const util_add*) B)->util_spread) return -1; + else return 0; +} + +void exact_strategy_search(extmanager* MyManager, DdNode **forest, double* utilities){ + DdManager* add_mgr; + DdNode *sum, *temp, *add_ps, *constant; + DdNode *max_node; + FILE *outfile; //output file pointer for .dot file + int i; + char filename[128]; + char** names; + int n = forestSize(forest); + double utility_to_go = 0.000001; + util_add * util_adds = (util_add *) malloc(sizeof(util_add)*n); + + if (params.debug) { + fprintf(stderr, "init add\n"); + } + if (params.dynreorder == 1) { + add_mgr = simpleBDDinit(MyManager->varmap.varcnt); + }else{ + add_mgr = simpleBDDinitNoReOrder(MyManager->varmap.varcnt); + } + if (params.debug){ + fprintf(stderr, "end init add\n"); + } + + if (params.debug) { + names= malloc(sizeof(char*)*MyManager->varmap.varcnt); + for(i = MyManager->varmap.varstart ;i < MyManager->varmap.varcnt; i++){ + names[i- MyManager->varmap.varstart] = MyManager->varmap.vars[i]; + } + } + + for(i=n-1;i>=0;i--){ + if (params.debug) { + // write BDD file + snprintf(filename, sizeof(filename), "bdd-total.dot"); + outfile = fopen(filename,"w"); + Cudd_DumpDot(MyManager->manager, n, forest, names, NULL, outfile); + fclose(outfile); + + temp = Cudd_BddToAdd(MyManager->manager,forest[i]); + Cudd_Ref(temp); + snprintf(filename, sizeof(filename), "bdd-%i.dot", i+1); + outfile = fopen(filename,"w"); + Cudd_DumpDot(MyManager->manager, 1, &temp,names, NULL, outfile); + fclose(outfile); + Cudd_RecursiveDeref(MyManager->manager,temp); + } + + // create ADD for Ps + add_ps = buildADDfromBDD(MyManager,forest[i],add_mgr); + Cudd_Ref(add_ps); + if (params.debug) fprintf(stderr, "built add_ps\n"); + ReInitAndUnrefHistory(MyManager->his, MyManager->varmap.varcnt,add_mgr); + + Cudd_RecursiveDeref(MyManager->manager,forest[i]); + + if (params.debug) { + // write ADD-Ps file + snprintf(filename, sizeof(filename), "add-ps-%i.dot", i+1); + outfile = fopen(filename,"w"); + Cudd_DumpDot(add_mgr, 1, &add_ps, names, NULL, outfile); + fclose(outfile); + } + //if (1 || _debug) fprintf(stderr,"best terminal of add_ps after %i now %g.\n", i,cuddV(Cudd_addFindMax(add_mgr,add_ps))); + //create ADD for u + constant = Cudd_addConst(add_mgr,utilities[i]); + Cudd_Ref(constant); + util_adds[i].root = Cudd_addApply(add_mgr,Cudd_addTimes,add_ps,constant); + Cudd_Ref(util_adds[i].root); + Cudd_RecursiveDeref(add_mgr,constant); + Cudd_RecursiveDeref(add_mgr,add_ps); + //add_ps can only be dereferenced when the history is cleared or when referenced twice. + + if (params.debug) { + // write ADD-U file + snprintf(filename, sizeof(filename), "add-u-%i.dot", i+1); + outfile = fopen(filename,"w"); + Cudd_DumpDot(add_mgr, 1, &util_adds[i].root, names, NULL, outfile); + fclose(outfile); + } + + // compute the maximum achievable utility to set useless terminals to -inf + max_node = Cudd_addFindMax(add_mgr,util_adds[i].root); + util_adds[i].util_spread = cuddV(max_node); + max_node = Cudd_addFindMin(add_mgr,util_adds[i].root); + util_adds[i].util_spread += -cuddV(max_node); + utility_to_go += util_adds[i].util_spread; + + //if (1 || _debug) fprintf(stderr,"best terminal of util_add after %i now %g.\n", i,cuddV(Cudd_addFindMax(add_mgr,util_adds[i].root))); + } + + qsort(util_adds, (size_t)n, sizeof(util_add), compare_util_adds); + + sum = Cudd_addConst(add_mgr,0); + Cudd_Ref(sum); + for(i=0;ihis, MyManager->varmap.varcnt); + extractstrategy(MyManager, add_mgr, sum, max_node); + Cudd_RecursiveDeref(add_mgr,sum); + +} + +DdNode* buildADDfromBDD(extmanager* MyManager, DdNode *Current, DdManager* addmgr) { + // the created adds are not dereferenced + // must be done based on the dynamic programming table + DdNode *h, *l; + hisnode *Found; + char *curnode; + double fact_prob; + int isDecision; + DdNode *lowvalue, *highvalue, *thisvalue; + DdNode *lowAdd, *highAdd; + DdNode* var; + DdNode *posprob, *negprob; + + //if (_debug && Cudd_DebugCheck(addmgr)!=0) exit(-1); + + if (_debug) { + fprintf(stderr, "(%p) ", Current); + curnode = GetNodeVarNameDisp(MyManager->manager, MyManager->varmap, Current); + fprintf(stderr, " aka %s\n", curnode); + } + + // base cases + if (Current == MyManager->t){ + thisvalue = Cudd_ReadOne(addmgr); + Cudd_Ref(thisvalue); + //if(_debug && Cudd_DebugCheck(addmgr)!=0) exit(-1); + return thisvalue; + } + if (Current == MyManager->f){ + thisvalue = Cudd_ReadZero(addmgr); + Cudd_Ref(thisvalue); + //if(_debug && Cudd_DebugCheck(addmgr)!=0) exit(-1); + return thisvalue; + } + + //node is in cache + if ((Found = GetNode(MyManager->his, MyManager->varmap.varstart, Current)) != NULL){ + if (_debug) fprintf(stderr, "found node %p (%s) in history\n", Current,curnode); + return (DdNode*)(Found->dynvalue); + } + + //inductive case + l = LowNodeOf(MyManager->manager, Current); + if (_debug) fprintf(stderr, "l(%s)->%p", curnode,l); + lowvalue = buildADDfromBDD(MyManager,l,addmgr); + + h = HighNodeOf(MyManager->manager, Current); + if (_debug) fprintf(stderr, "h(%s)->%p", curnode,h); + highvalue = buildADDfromBDD(MyManager,h,addmgr); + + //if(params.debug && Cudd_DebugCheck(addmgr)!=0) exit(-1); + + isDecision = MyManager->varmap.ivalue[GetIndex(Current) - MyManager->varmap.varstart]; + if(isDecision){ + //decision + if (_debug) fprintf(stderr,"%p (%s) is a decision\n",Current,curnode); + var = Cudd_addIthVar(addmgr,(int)GetIndex(Current)); + Cudd_Ref(var); + thisvalue=Cudd_addIte(addmgr,var,highvalue,lowvalue); + Cudd_Ref(thisvalue); + Cudd_RecursiveDeref(addmgr,var); + } else { + //probabilistic node + if (_debug) fprintf(stderr,"%p (%s) is a probabilistic fact",Current,curnode); + fact_prob = MyManager->varmap.dvalue[GetIndex(Current) - MyManager->varmap.varstart]; + if (_debug) fprintf(stderr, " with probability %lf \n", fact_prob); + posprob = Cudd_addConst(addmgr,fact_prob); + Cudd_Ref(posprob); + highAdd=Cudd_addApply(addmgr,Cudd_addTimes,posprob,highvalue); + Cudd_Ref(highAdd); + Cudd_RecursiveDeref(addmgr,posprob); + + negprob=Cudd_addConst(addmgr,1-fact_prob); + Cudd_Ref(negprob); + lowAdd= Cudd_addApply(addmgr,Cudd_addTimes,negprob,lowvalue); + Cudd_Ref(lowAdd); + Cudd_RecursiveDeref(addmgr,negprob); + + thisvalue = Cudd_addApply(addmgr,Cudd_addPlus,highAdd,lowAdd); + Cudd_Ref(thisvalue); + + Cudd_RecursiveDeref(addmgr,lowAdd); + Cudd_RecursiveDeref(addmgr,highAdd); + } + AddNode(MyManager->his, MyManager->varmap.varstart, Current, 0, 0, thisvalue); + return thisvalue; +} + + +void ReInitAndUnrefHistory(hisqueue *HisQueue, int varcnt, DdManager* mgr) { + int i, j; + for (i = 0; i < varcnt; i++) { + if (HisQueue[i].thenode != NULL) { + for (j = 0; j < HisQueue[i].cnt; j++){ + if(HisQueue[i].thenode[j].ivalue != 0){ + //if (_debug) fprintf(stderr,"At (%i,%i), unreffing node %i",i,j,HisQueue[i].thenode[j].ivalue); + Cudd_RecursiveDeref(mgr,(DdNode*)(HisQueue[i].thenode[j].dynvalue)); + } + } + free(HisQueue[i].thenode); + HisQueue[i].thenode = NULL; + } + HisQueue[i].cnt = 0; + } +} + +char* GetAddNodeVarNameDisp(namedvars varmap, DdNode *node) { + unsigned int index; + char *buffer = malloc(sizeof(char)*128); + if (Cudd_IsConstant(node)) { + snprintf(buffer, 128, "%lf", cuddV(node)); + return buffer; + } + if (NULL == node) return "(null)"; + index = GetIndex(node); + return varmap.vars[index - varmap.varstart]; +} + +int extractstrategy(extmanager* MyManager, DdManager * add_mgr, DdNode *Current, DdNode *max_node) { + char *curnode; + int result; + hisnode *Found; + + if (params.debug) { + fprintf(stderr, "handling node %p", Current); + curnode = GetAddNodeVarNameDisp(MyManager->varmap, Current); + fprintf(stderr, " aka %s\n", curnode); + } + + if(max_node == Current) return 1; + else if (Cudd_IsConstant(Current)) return 0; + else{ + if ((Found = GetNode(MyManager->his, MyManager->varmap.varstart, Current)) != NULL) { + return Found->ivalue; + } + if(extractstrategy(MyManager,add_mgr,LowNodeOf(add_mgr, Current),max_node)){ + // set strategy to 0 + MyManager->varmap.dvalue[GetIndex(Current) - MyManager->varmap.varstart] = 0; + result = 1; + }else if(extractstrategy(MyManager,add_mgr,HighNodeOf(add_mgr, Current),max_node)){ + // set strategy to 1 + MyManager->varmap.dvalue[GetIndex(Current) - MyManager->varmap.varstart] = 1; + result = 1; + }else result = 0; + AddNode(MyManager->his, MyManager->varmap.varstart, Current, 0, result, NULL); + return result; + } +} + +DdNode * setLowerBound(DdManager * dd, DdNode * f, double lowerBound) { + DdNode *res; + do { + res = setLowerBoundRecur(dd,f,lowerBound); + } while (dd->reordered == 1); + return(res); +} + +DdNode * setLowerBoundRecur(DdManager * dd, DdNode * f, double lowerBound) { + DdNode *res, *fv, *fvn, *T, *E; + DD_CTFP1 cacheOp; + + statLine(dd); + if (cuddIsConstant(f)) { + if(cuddV(f)index,T,E); + if (res == NULL) { + Cudd_RecursiveDeref(dd,T); + Cudd_RecursiveDeref(dd,E); + return(NULL); + } + cuddDeref(T); + cuddDeref(E); + + /* Store result. */ + cuddCacheInsert1(dd,cacheOp,f,res); + return(res); +} + +// TODO extmanager* or extmanager ??????? +// Is copying the varmap not too inefficient? +void local_strategy_search(extmanager* MyManager, DdNode **forest, double* utilities){ + double tempev; + int i; + int j = 0; + int changed = 1; + double bestev = expected_value(MyManager, forest, utilities); + if (_debug) fprintf(stderr,"Initial strategy has reward %g.\n", bestev); + while(changed){ + j++; + if (_debug) fprintf(stderr,"starting iteration %i.\n", j); + changed = 0; + for(i = 0; i < MyManager->varmap.varcnt; i++){ + if (MyManager->varmap.ivalue[i] == 1) { + //it's a decision, flip it' + MyManager->varmap.dvalue[i] = 1-MyManager->varmap.dvalue[i]; + tempev = expected_value(MyManager, forest, utilities); + if(tempev > bestev){ + if (_debug) fprintf(stderr,"found new best strategy (%g > %g).\n", tempev, bestev); + bestev = tempev; + changed = 1; + }else{ + if (_debug) fprintf(stderr,"keeping old strategy (%g < %g).\n", tempev, bestev); + MyManager->varmap.dvalue[i] = 1-MyManager->varmap.dvalue[i]; + } + } + } + } + if (_debug) fprintf(stderr,"expected_value(%g).\n", bestev); + printf("expected_value(%g).\n", bestev); +} + +typedef struct _decision{ + int var; + int nb_rel_bdds; + int alloc_rel_bdds; + int* rel_bdds; + int* rel_bdds_var; +} decision; + +void local_strategy_search_independent(bdd_mgr* bdd_mgrs, double* utilities, int nb_bdds, namedvars globalvars){ + int i, j, index; + int changed; + double* bdd_ev = malloc(sizeof(double)*nb_bdds); + double* bdd_ev_temp = malloc(sizeof(double)*nb_bdds); + double difference; + double new_strategy; + int nb_dec_vars = 0; + decision* decs = (decision*) malloc(sizeof(decision)*globalvars.varcnt); + decision* decision; + + // Initialize all BDDs and compute their utility + if (_debug) fprintf(stderr,"Initializing BDDs and computing the starting utility\n"); + for(i=0;ivar = i; + decision->nb_rel_bdds = 0; + decision->alloc_rel_bdds = 8; + decision->rel_bdds = (int*) malloc(sizeof(int)*8); + decision->rel_bdds_var = (int*)malloc(sizeof(int)*8); + for(j=0;j=0){ + if (_debug) fprintf(stderr," %i", j); + bdd_mgrs[j].extmanager.varmap.dvalue[index] = globalvars.dvalue[i]; + if(decision->nb_rel_bdds == decision->alloc_rel_bdds){ + // increase array size + decision->alloc_rel_bdds = 2*decision->alloc_rel_bdds; + decision->rel_bdds = (int*) realloc(decision->rel_bdds,sizeof(int)*decision->alloc_rel_bdds); + decision->rel_bdds_var = (int*) realloc(decision->rel_bdds_var,sizeof(int)*decision->alloc_rel_bdds); + } + decision->rel_bdds[decision->nb_rel_bdds] = j; + decision->rel_bdds_var[decision->nb_rel_bdds] = index; + decision->nb_rel_bdds++; + } + } + }else{ + if (_debug) fprintf(stderr," is not a decision, affecting bdds"); + for(j=0;j=0){ + if (_debug) fprintf(stderr," %i", j); + bdd_mgrs[j].extmanager.varmap.dvalue[index] = globalvars.dvalue[i]; + } + } + } + if (_debug) fprintf(stderr,".\n"); + } + + if (_debug) fprintf(stderr,"Starting Search\n"); + if (_debug) fprintf(stderr,"There are %i decisions.\n",nb_dec_vars); + do{ + changed = 0; + if (_debug) fprintf(stderr,"New Iteration\n"); + for(i=0;i0){ + // it's an improvement + globalvars.dvalue[decs[i].var] = new_strategy; + changed = 1; + for(j=0;jhis, MyManager->varmap.varcnt); + //printf("final sum is %e.\n", sum); + return sum; +} + +void print_strategy(namedvars varmap){ + int i; + for(i = 0; i < varmap.varcnt; i++){ + if (varmap.ivalue[i] == 1) { + // it's a decision, print it + // if it contains an '_', it must be quoted, + // otherwise don't quote because it must parsed as an integer in prolog + if(strchr(varmap.vars[i]+1,'_')==NULL){ + if (_debug) fprintf(stderr,"strategy(%s,%g).\n",varmap.vars[i]+1, varmap.dvalue[i]); + printf("strategy(%s,%g).\n",varmap.vars[i]+1, varmap.dvalue[i]); + }else{ + if (_debug) fprintf(stderr,"strategy('%s',%g).\n",varmap.vars[i]+1, varmap.dvalue[i]); + printf("strategy('%s',%g).\n",varmap.vars[i]+1, varmap.dvalue[i]); + } + } + } +} + +// new manager for bdd forest needs very low memory requirements! -l +void newManager(extmanager* MyManager, bddfileheader fileheader, int nbManagers){ + MyManager->manager; + if (_debug) fprintf(stderr,"Creating new BDD manager.\n\n"); + if (_debug) fprintf(stderr,"Setting BDD manager memory consumption to %i.\n", max(1024,(512*1024*1024)/nbManagers)); + MyManager->manager = Cudd_Init((unsigned int)fileheader.varcnt, 0, + (unsigned int)max(32,CUDD_UNIQUE_SLOTS/nbManagers), + (unsigned int)max(512,CUDD_CACHE_SLOTS/nbManagers), + (unsigned int)max(5000,(1024*1024*1024)/nbManagers)); + Cudd_AutodynEnable(MyManager->manager, CUDD_REORDER_GROUP_SIFT); + //Cudd_SetMaxCacheHard(MyManager->manager, 1024*1024*1024); + //Cudd_SetLooseUpTo(MyManager->manager, 1024*1024*512); + if (_debug) Cudd_EnableReorderingReporting(MyManager->manager); + MyManager->t = HIGH(MyManager->manager); + MyManager->f = LOW(MyManager->manager); + MyManager->varmap = InitNamedVars(fileheader.varcnt, fileheader.varstart); + if (_debug) Cudd_PrintInfo(MyManager->manager,stderr); + MyManager->his = InitHistory(fileheader.varcnt); +} + + +int printTime(void){ + struct tm *current; + time_t now; + time(&now); + current = localtime(&now); + fprintf(stderr, "%i:%i:%i: ", current->tm_hour, current->tm_min, current->tm_sec); +} + +bdd_mgr* generateIndependentBDDForest(bddfileheader fileheader) { + int i; + int icomment, maxlinesize, icur, iline, curinter, iequal; + DdNode *Line; + bdd_mgr * bdd_mgrs; + char buf, *inputline, *filename, *subl; + bddfileheader interfileheader; + // Initialization of intermediate steps + //Guy: +1 to delimit array???? + bdd_mgrs = (bdd_mgr *) malloc(sizeof(bdd_mgr) * (fileheader.intercnt+1)); + for (icur = 0; icur < fileheader.intercnt+1; icur++) { + bdd_mgrs[icur].extmanager.manager = NULL; + bdd_mgrs[icur].root = NULL; + } + // Read file data + interfileheader.inputfile = NULL; + filename = NULL; // For nested files + iequal = 0; // Flag for encountered = sign + icur = 0; // Pointer for inputline buffer location + iline = 5; // Current file line (first after header) + icomment = 0; // Flag for comments + maxlinesize = 80; // inputline starting buffer size + inputline = (char *) malloc(sizeof(char) * maxlinesize); + while(!feof(fileheader.inputfile)) { + fread(&buf, 1, 1, fileheader.inputfile); + if (buf == ';' || buf == '%' || buf == '$') icomment = 1; + if (buf == '\n') { + if (icomment) icomment = 0; + if (iequal > 1) { + fprintf(stderr, "Error at line: %i. Line contains more than 1 equal(=) signs.\n", iline); + fclose(fileheader.inputfile); + free(bdd_mgrs); + free(inputline); + return NULL; + } else iequal = 0; + if (icur > 0) { + inputline[icur] = '\0'; + if (inputline[0] != 'L') { + fprintf(stderr, "Error at line: %i. Intermediate results should start with L.\n", iline); + fclose(fileheader.inputfile); + free(bdd_mgrs); + free(inputline); + return NULL; + } + curinter = getInterBDD(inputline); + if (curinter == -1) { + if (fileheader.version < 2) { + if (inputline[0] == 'L' && IsPosNumber(inputline + 1)) { + curinter = atoi(inputline + 1) - 1; + if (curinter > -1 && curinter < fileheader.intercnt && bdd_mgrs[curinter].extmanager.manager != NULL) { + if (_debug) fprintf(stderr, "Returned: %s\n", inputline); + fclose(fileheader.inputfile); + free(inputline); + //changed: just return every intermediate BDD + return bdd_mgrs; + } else { + fprintf(stderr, "Error at line: %i. Return result asked doesn't exist.\n", iline); + fclose(fileheader.inputfile); + free(bdd_mgrs); + free(inputline); + return NULL; + } + } else { + fprintf(stderr, "Error at line: %i. Invalid intermediate result format.\n", iline); + fclose(fileheader.inputfile); + free(bdd_mgrs); + free(inputline); + return NULL; + } + } else { + // Support for forest + maxlinesize = 10; + iline = -1; + for (subl = strtok(inputline, ","); subl != NULL; subl = strtok(NULL, ",")) { + if (subl[0] == 'L' && IsPosNumber(subl + 1)) { + curinter = atoi(subl + 1) - 1; + if (curinter > -1 && curinter < fileheader.intercnt && bdd_mgrs[curinter].extmanager.manager != NULL) { + iline++; + if (iline >= (maxlinesize - 1)) { + maxlinesize *= 2; + } + } else { + fprintf(stderr, "Error at line: %i. Return result asked(%s) doesn't exist.\n", iline, subl); + fclose(fileheader.inputfile); + free(bdd_mgrs); + free(inputline); + free(subl); + return NULL; + } + } else { + fprintf(stderr, "Error at line: %i. Invalid intermediate result format.\n", iline); + fclose(fileheader.inputfile); + free(bdd_mgrs); + free(inputline); + free(subl); + return NULL; + } + } + if (_debug) fprintf(stderr, "Returned: %s\n", inputline); + fclose(fileheader.inputfile); + free(inputline); + free(subl); + iline++; + //changed: just return every intermediate BDD + return bdd_mgrs; + } + } else if (curinter > -1 && curinter < fileheader.intercnt && bdd_mgrs[curinter].extmanager.manager == NULL) { + if (_debug) fprintf(stderr, "%i %s\n", curinter, inputline); + if (_debug) printTime(); + if (_debug) fprintf(stderr, "At line %i reading %s\n", (curinter+1), inputline); + filename = getFileName(inputline); + if (filename == NULL) { + fprintf(stderr, "Error at line: %i. A forest of independent BDDs cannot have formulas.\n", iline); + fclose(fileheader.inputfile); + free(bdd_mgrs); + free(inputline); + free(subl); + return NULL; + } else { + interfileheader = ReadFileHeader(filename); + if (interfileheader.inputfile == NULL) { + //Line = simpleBDDload(manager, &varmap, filename); + Line = NULL; + } else { + newManager(&(bdd_mgrs[curinter].extmanager),interfileheader,fileheader.intercnt); + Line = FileGenerateBDD(bdd_mgrs[curinter].extmanager.manager, bdd_mgrs[curinter].extmanager.varmap, interfileheader); +// for(i = 0; imanager); + if (_debug) Cudd_PrintInfo(bdd_mgrs[curinter].extmanager.manager,stderr); + } + if (Line == NULL) fprintf(stderr, "Error at line: %i. Error in nested BDD file: %s.\n", iline, filename); + free(filename); + filename = NULL; + interfileheader.inputfile = NULL; + } + if (Line == NULL) { + fclose(fileheader.inputfile); + free(bdd_mgrs); + free(inputline); + return NULL; + } + bdd_mgrs[curinter].root = Line; + icur = 0; + } else if (curinter > -1 && curinter < fileheader.intercnt && bdd_mgrs[curinter].extmanager.manager != NULL) { + fprintf(stderr, "Error at line: %i. Intermediate results can't be overwritten.\n", iline); + fclose(fileheader.inputfile); + free(bdd_mgrs); + free(inputline); + return NULL; + } else { + fprintf(stderr, "Error at line: %i. Intermediate result asked doesn't exist.\n", iline); + fclose(fileheader.inputfile); + free(bdd_mgrs); + free(inputline); + return NULL; + } + } + iline++; + } else if (buf != ' ' && buf != '\t' && !icomment) { + if (buf == '=') iequal++; + inputline[icur] = buf; + icur += 1; + if (icur == _maxbufsize) { + fprintf(stderr, "Error: Maximum buffer size(%i) exceeded.\n", _maxbufsize); + fclose(fileheader.inputfile); + free(bdd_mgrs); + free(inputline); + return NULL; + } + while (icur > maxlinesize - 1) { + maxlinesize *= 2; + inputline = (char *) realloc(inputline, sizeof(char) * maxlinesize); + } + } + } + fprintf(stderr, "Error, file either doesn't end with a blank line or no return result was asked.\n"); + fclose(fileheader.inputfile); + free(bdd_mgrs); + free(inputline); + return NULL; +} + + +int LoadVariableDataForForest(namedvars varmap, char *filename) { + FILE *data; + char *dataread, buf, *varname, *dynvalue; + double dvalue = 0.0; + int icur = 0, maxbufsize = 10, hasvar = 0, index = 0, idat = 0, ivalue = 0; + dynvalue = NULL; + if ((data = fopen(filename, "r")) == NULL) { + perror(filename); + return -1; + } + dataread = (char *) malloc(sizeof(char) * maxbufsize); + while(!feof(data)) { + fread(&buf, 1, 1, data); + if ((buf == '\n') && icur == 0) { + // ignore empty lines + } else if (buf == '\n') { + dataread[icur] = '\0'; + icur = 0; + buf = ' '; + if (dataread[0] == '@') { + if (hasvar) { + AddNamedVarAt(varmap,varname,index); + varmap.loaded[index] = 1; + varmap.dvalue[index] = dvalue; + varmap.ivalue[index] = ivalue; + if (varmap.dynvalue[index] != NULL) { + free(varmap.dynvalue[index]); + varmap.dynvalue[index] = NULL; + } + if (dynvalue != NULL) { + varmap.dynvalue[index] = (void *) malloc(sizeof(char) * (strlen(dynvalue) + 1)); + strcpy(varmap.dynvalue[index], dynvalue); + free(dynvalue); + dynvalue = NULL; + } + index++; + dvalue = 0.0; + ivalue = 0; + free(varname); + } + varname = (char *) malloc(sizeof(char) * strlen(dataread)); + strcpy(varname, dataread + 1); + hasvar = 1; + idat = 0; + } else { + if (hasvar >= 0) { + switch(idat) { + case 0: + if (IsRealNumber(dataread)) dvalue = atof(dataread); + else { + fprintf(stderr, "Error at file: %s. Variable: %s can't have non real value: %s.\n", filename, varname, dataread); + fclose(data); + free(varname); + free(dataread); + return -2; + } + idat++; + break; + case 1: + if (IsNumber(dataread)) ivalue = atoi(dataread); + else { + fprintf(stderr, "Error at file: %s. Variable: %s can't have non integer value: %s.\n", filename, varname, dataread); + fclose(data); + free(varname); + free(dataread); + return -2; + } + idat++; + break; + case 2: + dynvalue = malloc(sizeof(char) * (strlen(dataread) + 1)); + strcpy(dynvalue, dataread); + break; + } + } + } + } else { + dataread[icur] = buf; + icur++; + if (icur == _maxbufsize) { + fprintf(stderr, "Error: Maximum buffer size(%i) exceeded.\n", _maxbufsize); + fclose(data); + free(varname); + free(dataread); + return -2; + } + while (icur > maxbufsize - 1) { + maxbufsize *= 2; + dataread = (char *) realloc(dataread, sizeof(char) * maxbufsize); + } + } + } + if (hasvar) { + AddNamedVarAt(varmap,varname,index); + varmap.loaded[index] = 1; + varmap.dvalue[index] = dvalue; + varmap.ivalue[index] = ivalue; + if (varmap.dynvalue[index] != NULL) { + free(varmap.dynvalue[index]); + varmap.dynvalue[index] = NULL; + } + if (dynvalue != NULL) { + varmap.dynvalue[index] = (void *) malloc(sizeof(char) * (strlen(dynvalue) + 1)); + strcpy(varmap.dynvalue[index], dynvalue); + free(dynvalue); + } + index++; + free(varname); + } + fclose(data); + free(dataread); + return 0; +} + +/////////////////////// +// Stop Added by Guy // +/////////////////////// + +/* Shell Parameters handling */ + +int argtype(const char *arg) { + if (strcmp(arg, "-l") == 0 || strcmp(arg, "--load") == 0) return 0; + if (strcmp(arg, "-e") == 0 || strcmp(arg, "--export") == 0) return 2; + if (strcmp(arg, "-m") == 0 || strcmp(arg, "--method") == 0) return 3; + if (strcmp(arg, "-i") == 0 || strcmp(arg, "--input") == 0) return 4; + if (strcmp(arg, "-h") == 0 || strcmp(arg, "--help") == 0) return 5; + if (strcmp(arg, "-d") == 0 || strcmp(arg, "--debug") == 0) return 6; + if (strcmp(arg, "-id") == 0 || strcmp(arg, "--queryid") == 0) return 7; + if (strcmp(arg, "-t") == 0 || strcmp(arg, "--timeout") == 0) return 8; + if (strcmp(arg, "-sd") == 0 || strcmp(arg, "--savedump") == 0) return 9; + if (strcmp(arg, "-sl") == 0 || strcmp(arg, "--slope") == 0) return 10; + if (strcmp(arg, "-o") == 0 || strcmp(arg, "--online") == 0) return 11; + if (strcmp(arg, "-bs") == 0 || strcmp(arg, "--bufsize") == 0) return 12; + if (strcmp(arg, "-pid") == 0 || strcmp(arg, "--pid") == 0) return 13; + if (strcmp(arg, "-ord") == 0 || strcmp(arg, "--order") == 0) return 14; + if (strcmp(arg, "-u") == 0 || strcmp(arg, "--utilities") == 0) return 15; + if (strcmp(arg, "-if") == 0 || strcmp(arg, "--independent") == 0) return 16; + if (strcmp(arg, "-lo") == 0 || strcmp(arg, "--local") == 0) return 17; + if (strcmp(arg, "-dreorder") == 0 || strcmp(arg, "--disable-reorder") == 0) return 18; + if (strcmp(arg, "-sord") == 0 || strcmp(arg, "--static-order") == 0) return 19; + return -1; +} + +void printhelp(int argc, char **arg) { + fprintf(stderr, "\n\nProbLogBDD Tool Version: %s\n\n", VERSION); + fprintf(stderr, "SimpleCUDD library (www.cs.kuleuven.be/~theo/tools/simplecudd.html)\n"); + fprintf(stderr, "SimpleCUDD was developed at Katholieke Universiteit Leuven(www.kuleuven.be)\n"); + fprintf(stderr, "Copyright Katholieke Universiteit Leuven 2008\n"); + fprintf(stderr, "Authors: Theofrastos Mantadelis, Angelika Kimmig, Bernd Gutmann\n"); + fprintf(stderr, "This package falls under the: Artistic License 2.0\n"); + fprintf(stderr, "\nUsage: %s -l [filename] -i [filename] -o (-s(d) [filename] -e [filename] -m [method] -id [queryid] -sl [double]) (-t [seconds] -d -h)\n", arg[0]); + fprintf(stderr, "Generates and traverses a BDD\nMandatory parameters:\n"); + fprintf(stderr, "\t-l [filename]\t->\tfilename to load supports two formats:\n\t\t\t\t\t\t1. script with generation instructions\n\t\t\t\t\t\t2. node dump saved file\n"); + fprintf(stderr, "\t-i [filename]\t->\tfilename to input problem specifics (mandatory with file formats 1, 2)\n"); + fprintf(stderr, "\t-o\t\t->\tgenerates the BDD in online mode instead from a file can be used instead of -l\n"); + fprintf(stderr, "Optional parameters:\n"); + fprintf(stderr, "\t-sd [filename]\t->\tfilename to save generated BDD in node dump format (fast loading, traverse valid only)\n"); + fprintf(stderr, "\t-e [filename]\t->\tfilename to export generated BDD in dot format\n"); + fprintf(stderr, "\t-m [method]\t->\tthe calculation method to be used: none(default), [p]robability, [g]radient, [l]ine search, [o]nline, [e]xpexted counts\n"); + fprintf(stderr, "\t-id [queryid]\t->\tthe queries identity name (used by gradient) default: %s\n", arg[0]); + fprintf(stderr, "\t-sl [double]\t->\tthe sigmoid slope (used by gradient) default: 1.0\n"); + fprintf(stderr, "\t-if \t\t->\tbuild a forest of -independent- BDDs where each BDD is in a different manager. \n"); + fprintf(stderr, "\t-u [filename]\t->\tfilename where a list of utilities can be found. \n"); + fprintf(stderr, "\t-lo \t\t->\t do local strategy search. \n"); + fprintf(stderr, "Extra parameters:\n"); + fprintf(stderr, "\t-t [seconds]\t->\tthe seconds (int) for BDD generation timeout default 0 = no timeout\n"); + fprintf(stderr, "\t-pid [pid]\t->\ta process id (int) to check for termination default 0 = no process to check\n"); + fprintf(stderr, "\t-bs [bytes]\t->\tthe bytes (int) to use as a maximum buffer size to read files default 0 = no max\n"); + fprintf(stderr, "\t-ord [filename]\t->\tUse the [filename] to define a specific final BDD variable order\n"); + fprintf(stderr, "\t-dreorder\t->\tDiseable BDD dynamic variable ordering\n"); + fprintf(stderr, "\t-sord [filename]\t->\tDefine a static ordering within [filename]\n"); + fprintf(stderr, "\t-d\t\t->\tRun in debug mode (gives extra messages in stderr)\n"); + fprintf(stderr, "\t-h\t\t->\tHelp (displays this message)\n"); + fprintf(stderr, "Extra notes:\nSupports a forest of BDDs in one shared BDD.\nSelected computational methods will be applied to each BDD seperately.\nFile operations will be applied only to the first BDD.\n"); + fprintf(stderr, "\nExample: %s -l testbdd -i input.txt -m g -id testbdd\n", arg[0]); +} + +parameters loadparam(int argc, char **arg) { + int i; + parameters params; + params.loadfile = -1; + params.savedfile = -1; + params.exportfile = -1; + params.method = 0; + params.inputfile = -1; + params.debug = 0; + params.errorcnt = 0; + params.queryid = 0; + params.timeout = 0; + params.sigmoid_slope = 1.0; + params.online = 0; + params.maxbufsize = 0; + params.ppid = NULL; + params.orderfile = -1; + params.utilfile = -1; + params.independent_forest = -1; + params.local_search = -1; + params.error = (int *) malloc(argc * sizeof(int)); + params.dynreorder = 1; + params.staticorder = -1; + for (i = 1; i < argc; i++) { + switch(argtype(arg[i])) { + case 0: + if (argc > i + 1) { + i++; + params.loadfile = i; + } else { + params.error[params.errorcnt] = i; + params.errorcnt++; + } + break; + case 2: + if (argc > i + 1) { + i++; + params.exportfile = i; + } else { + params.error[params.errorcnt] = i; + params.errorcnt++; + } + break; + case 3: + if (argc > i + 1) { + i++; + params.method = i; + } else { + params.error[params.errorcnt] = i; + params.errorcnt++; + } + break; + case 4: + if (argc > i + 1) { + i++; + params.inputfile = i; + } else { + params.error[params.errorcnt] = i; + params.errorcnt++; + } + break; + case 5: + printhelp(argc, arg); + break; + case 6: + params.debug = 1; + break; + case 7: + if (argc > i + 1) { + i++; + params.queryid = i; + } else { + params.error[params.errorcnt] = i; + params.errorcnt++; + } + break; + case 8: + if ((argc > i + 1) && (IsPosNumber(arg[i + 1]))) { + i++; + params.timeout = atoi(arg[i]); + } else { + params.error[params.errorcnt] = i; + params.errorcnt++; + } + break; + case 9: + if (argc > i + 1) { + i++; + params.savedfile = i; + } else { + params.error[params.errorcnt] = i; + params.errorcnt++; + } + break; + case 10: + if ((argc > i + 1) && (IsRealNumber(arg[i + 1]))) { + i++; + params.sigmoid_slope = atof(arg[i]); + } else { + params.error[params.errorcnt] = i; + params.errorcnt++; + } + break; + case 11: + params.online = 1; + break; + case 12: + if ((argc > i + 1) && (IsPosNumber(arg[i + 1]))) { + i++; + params.maxbufsize = atoi(arg[i]); + } else { + params.error[params.errorcnt] = i; + params.errorcnt++; + } + break; + case 13: + if ((argc > i + 1) && (IsPosNumber(arg[i + 1]))) { + i++; + params.ppid = (char *) malloc(sizeof(char) * (strlen(arg[i]) + 1)); + strcpy(params.ppid, arg[i]); + } else { + params.error[params.errorcnt] = i; + params.errorcnt++; + } + break; + case 14: + if (argc > i + 1) { + i++; + params.orderfile = i; + } else { + params.error[params.errorcnt] = i; + params.errorcnt++; + } + break; + case 15: + if (argc > i + 1) { + i++; + params.utilfile = i; + } else { + params.error[params.errorcnt] = i; + params.errorcnt++; + } + break; + case 16: + params.independent_forest = i; + break; + case 17: + params.local_search = i; + break; + case 18: + params.dynreorder = -1; + break; + case 19: + if (argc > i + 1) { + i++; + params.staticorder = i; + } else { + params.error[params.errorcnt] = i; + params.errorcnt++; + } + break; + default: + params.error[params.errorcnt] = i; + params.errorcnt++; + break; + } + } + return params; +} + +/* Error Handlers */ + +void handler(int num) { + fprintf(stderr, "Error: Timeout %i exceeded.\n", params.timeout); + exit(-1); +} + +void pidhandler(int num) { + char *s; + if (params.timeout > 0) { + params.timeout -= 5; + if (params.timeout <= 0) { + fprintf(stderr, "Error: Timeout exceeded.\n"); + exit(-1); + } + } + s = (char *) malloc(sizeof(char) * (19 + strlen(params.ppid))); + strcpy(s, "ps "); strcat(s, params.ppid); strcat(s, " >/dev/null"); + if (system(s) != 0) exit(4); + signal(SIGALRM, pidhandler); + alarm(5); + free(s); +} + +void termhandler(int num) { + exit(3); +} + +/* Debugging traverse function */ + +void myexpand(extmanager MyManager, DdNode *Current) { + DdNode *h, *l; + hisnode *Found; + char *curnode; + curnode = GetNodeVarNameDisp(MyManager.manager, MyManager.varmap, Current); + printf("%s\n", curnode); + if ((Current != MyManager.t) && (Current != MyManager.f) && + ((Found = GetNode(MyManager.his, MyManager.varmap.varstart, Current)) == NULL)) { + l = LowNodeOf(MyManager.manager, Current); + h = HighNodeOf(MyManager.manager, Current); + printf("l(%s)->", curnode); + myexpand(MyManager, l); + printf("h(%s)->", curnode); + myexpand(MyManager, h); + AddNode(MyManager.his, MyManager.varmap.varstart, Current, 0.0, 0, NULL); + } +} + +/* Angelicas Algorithm */ + +double CalcProbability(extmanager MyManager, DdNode *Current) { + DdNode *h, *l; + hisnode *Found; + char *curnode, *dynvalue; + double lvalue, hvalue, tvalue; + density_integral dynvalue_parsed; + + if (params.debug) { + curnode = GetNodeVarNameDisp(MyManager.manager, MyManager.varmap, Current); + fprintf(stderr, "%s\n", curnode); + } + + // base cases: 0 and 1 terminal + if (Current == MyManager.t) return 1.0; + if (Current == MyManager.f) return 0.0; + + // case: node is in cache + if ((Found = GetNode(MyManager.his, MyManager.varmap.varstart, Current)) != NULL) { + return Found->dvalue; + } + + // case: node is not in cache + l = LowNodeOf(MyManager.manager, Current); + h = HighNodeOf(MyManager.manager, Current); + if (params.debug) fprintf(stderr, "l(%s)->", curnode); + lvalue = CalcProbability(MyManager, l); + if (params.debug) fprintf(stderr, "h(%s)->", curnode); + hvalue = CalcProbability(MyManager, h); + + dynvalue = (char*) MyManager.varmap.dynvalue[GetIndex(Current) - MyManager.varmap.varstart]; + if (dynvalue == NULL) { + // no dynvalue, node is regular probabilistic fact + tvalue = MyManager.varmap.dvalue[GetIndex(Current) - MyManager.varmap.varstart]; + } else { + // there is a dynvalue, node is continuous fact + curnode = GetNodeVarNameDisp(MyManager.manager, MyManager.varmap, Current); + dynvalue_parsed = parse_density_integral_string(dynvalue, curnode); + if (params.debug) fprintf(stderr, " cont low=%f high=%f mu=%f sigma=%f\n->", dynvalue_parsed.low, dynvalue_parsed.high, dynvalue_parsed.mu,exp(dynvalue_parsed.log_sigma) ); + tvalue = cumulative_normal(dynvalue_parsed.low, dynvalue_parsed.high, dynvalue_parsed.mu, exp(dynvalue_parsed.log_sigma))/ + (1-cumulative_normal_upper(dynvalue_parsed.low, dynvalue_parsed.mu, exp(dynvalue_parsed.log_sigma))); + } + + tvalue = tvalue * hvalue + lvalue * (1.0 - tvalue); + AddNode(MyManager.his, MyManager.varmap.varstart, Current, tvalue, 0, NULL); + return tvalue; +} + + + + + +// START HERE +double CalcExpectedCounts(extmanager MyManager, DdNode *Current) { + DdNode *h, *l; + hisnode *Found; + char *curnode, *dynvalue; + double lvalue, hvalue, tvalue; + int ivalue; + if (params.debug) { + curnode = GetNodeVarNameDisp(MyManager.manager, MyManager.varmap, Current); + fprintf(stderr, "%s\n", curnode); + } + if (Current == MyManager.t) return 1.0; + if (Current == MyManager.f) return 0.0; + + if ((Found = GetNode(MyManager.his, MyManager.varmap.varstart, Current)) != NULL) return Found->dvalue; + l = LowNodeOf(MyManager.manager, Current); + h = HighNodeOf(MyManager.manager, Current); + if (params.debug) fprintf(stderr, "l(%s)->", curnode); + lvalue = CalcProbability(MyManager, l); + if (params.debug) fprintf(stderr, "h(%s)->", curnode); + hvalue = CalcProbability(MyManager, h); + + dynvalue = (char*) MyManager.varmap.dynvalue[GetIndex(Current) - MyManager.varmap.varstart]; + // if (dynvalue == NULL) { + // no dynvalue, node is regular probabilistic fact + tvalue = MyManager.varmap.dvalue[GetIndex(Current) - MyManager.varmap.varstart]; + ivalue = MyManager.varmap.ivalue[GetIndex(Current) - MyManager.varmap.varstart]; + + if(ivalue == 0){ + tvalue = tvalue * hvalue + lvalue * (1.0 - tvalue); + }else if (ivalue ==1){ + tvalue = hvalue + lvalue ; + } + AddNode(MyManager.his, MyManager.varmap.varstart, Current, tvalue, 0, NULL); + return tvalue; +} + + +/* Bernds Algorithm */ +// type=0 regular probabilistic fact +// type=1 derive gradient for mu +// type=2 derive gradient for sigma +gradientpair CalcGradient(extmanager MyManager, DdNode *Current, int TargetVar, char *TargetPattern, int type) { + DdNode *h, *l; + hisnode *Found; + char *curnode, *dynvalue; + gradientpair lowvalue, highvalue, tvalue; + double this_probability; + double this_gradient; + double continuous_denominator,continuous_numerator; + double *gradient; + density_integral dynvalue_parsed; + + if (params.debug) { + curnode = GetNodeVarNameDisp(MyManager.manager, MyManager.varmap, Current); + fprintf(stderr, "%s\n", curnode); + } + // base cases + if (Current == MyManager.t) { + tvalue.probability = 1.0; + tvalue.gradient = 0.0; + return tvalue; + } + if (Current == MyManager.f) { + tvalue.probability = 0.0; + tvalue.gradient = 0.0; + return tvalue; + } + //node is in cache + if ((Found = GetNode(MyManager.his, MyManager.varmap.varstart, Current)) != NULL) { + tvalue.probability = Found->dvalue; + tvalue.gradient = *((double *) Found->dynvalue); + return tvalue; + } + + //inductive case + l = LowNodeOf(MyManager.manager, Current); + h = HighNodeOf(MyManager.manager, Current); + if (params.debug) fprintf(stderr, "l(%s)->", curnode); + lowvalue = CalcGradient(MyManager, l, TargetVar, TargetPattern,type); + if (params.debug) fprintf(stderr, "h(%s)->", curnode); + highvalue = CalcGradient(MyManager, h, TargetVar, TargetPattern,type); + dynvalue = (char*) MyManager.varmap.dynvalue[GetIndex(Current) - MyManager.varmap.varstart]; + if (dynvalue == NULL) { // no dynvalue, it's a regular probabilistic fact + this_probability = sigmoid(MyManager.varmap.dvalue[GetIndex(Current) - MyManager.varmap.varstart], params.sigmoid_slope); + } else { // there is a dynvalue, it's a continuous fact! let's do the hybrid ProbLog magic here + curnode = GetNodeVarNameDisp(MyManager.manager, MyManager.varmap, Current); + dynvalue_parsed = parse_density_integral_string(dynvalue, curnode); + continuous_denominator = 1-cumulative_normal_upper(dynvalue_parsed.low, dynvalue_parsed.mu, exp(dynvalue_parsed.log_sigma)); + continuous_numerator = cumulative_normal(dynvalue_parsed.low, dynvalue_parsed.high, dynvalue_parsed.mu, exp(dynvalue_parsed.log_sigma)); + this_probability= continuous_numerator/continuous_denominator; + } + + tvalue.probability = this_probability * highvalue.probability + (1 - this_probability) * lowvalue.probability; + tvalue.gradient = this_probability * highvalue.gradient + (1 - this_probability) * lowvalue.gradient; + + + // is this node, the one we want to calculcate the gradient for? + + if ((GetIndex(Current) == TargetVar) || + ((TargetPattern != NULL) && patternmatch(TargetPattern, MyManager.varmap.vars[GetIndex(Current)]))) { + + if (type == 0) { + // current node is normal probabilistic fact + this_gradient = this_probability * (1 - this_probability) * params.sigmoid_slope; + } else if (type == 1) { + // it's a continues fact and we need d/dmu + this_gradient = (cumulative_normal_dmu(dynvalue_parsed.low, dynvalue_parsed.high, dynvalue_parsed.mu, exp(dynvalue_parsed.log_sigma))*continuous_denominator+ + continuous_numerator*cumulative_normal_upper_dmu(dynvalue_parsed.low, dynvalue_parsed.mu, exp(dynvalue_parsed.log_sigma))) / + (continuous_denominator*continuous_denominator); + } else if (type == 2) { + // it's a continues fact and we need d/dsigma + + + this_gradient = exp(dynvalue_parsed.log_sigma)* + + (cumulative_normal_dsigma(dynvalue_parsed.low, dynvalue_parsed.high, dynvalue_parsed.mu, exp(dynvalue_parsed.log_sigma))*continuous_denominator + + continuous_numerator*cumulative_normal_upper_dsigma(dynvalue_parsed.low, dynvalue_parsed.mu, exp(dynvalue_parsed.log_sigma))) / + (continuous_denominator*continuous_denominator); + } + + tvalue.gradient += (highvalue.probability - lowvalue.probability) * this_gradient; + } + + + gradient = (double *) malloc(sizeof(double)); + *gradient = tvalue.gradient; + AddNode(MyManager.his, MyManager.varmap.varstart, Current, tvalue.probability, 0, gradient); + return tvalue; +} + +char * extractpattern(char *thestr) { + char *p; + int i = 0, sl = strlen(thestr); + while((thestr[i] != '_') && (i < sl)) i++; + if (i == sl) return NULL; + i++; + p = (char *) malloc(sizeof(char) * (i + 2)); + strncpy(p, thestr, i); + p[i] = '*'; + p[i + 1] = '\0'; + return p; +} + +int patterncalculated(char *pattern, extmanager MyManager, int loc) { + int i; + if (pattern == NULL) return 0; + for (i = loc - 1; i > -1; i--) + if (patternmatch(pattern, MyManager.varmap.vars[i])) return 1; + return 0; +} diff --git a/packages/ProbLog/simplecudd/Example.c b/packages/ProbLog/simplecudd/problogmath.c similarity index 73% rename from packages/ProbLog/simplecudd/Example.c rename to packages/ProbLog/simplecudd/problogmath.c index a6a38628a..b4fe41591 100644 --- a/packages/ProbLog/simplecudd/Example.c +++ b/packages/ProbLog/simplecudd/problogmath.c @@ -3,10 +3,13 @@ * 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 * +* Copyright Katholieke Universiteit Leuven 2008, 2009, 2010 * +* * +* Author: Bernd Gutmann * +* File: problogmath.c * +* $Date:: 2010-08-25 15:23:30 +0200 (Wed, 25 Aug 2010) $ * +* $Revision:: 4683 $ * * * -* Author: Theofrastos Mantadelis * -* File: Example.c * * * ******************************************************************************** * * @@ -184,141 +187,168 @@ * * \******************************************************************************/ +#include "problogmath.h" -#include "simplecudd.h" -typedef struct _extmanager { - DdManager *manager; - DdNode *t, *f; - hisqueue *his; - namedvars varmap; -} extmanager; +double sigmoid(double x, double slope) { + return 1.0 / (1.0 + exp(-x * slope)); +} -void DFS(extmanager MyManager, DdNode *Current); -int compexpand(extmanager MyManager, DdNode *Current, extmanager MyManager2, DdNode *Current2); -int bufstrcat(char *targetstr, int targetmem, const char *srcstr); -void getalltruepaths(extmanager MyManager, DdNode *Current, const char *startpath, const char *prevvar); +// This function calculates the accumulated density of the normal distribution +// For details see G. Marsaglia, Evaluating the Normal Distribution, Journal of Statistical Software, 2004:11(4). +double Phi(double x) { + double s=x; + double t=0.0; + double b=x; + double q=x*x; + double i=1; -int main(int argc, char **arg) { - extmanager MyManager; - DdNode *bdd; - bddfileheader fileheader; - int code; - char yn; - code = -1; - if (argc != 2) { - fprintf(stderr, "\nUsage: %s [filename]\nGenerates and traverses a BDD from file\n", arg[0]); - fprintf(stderr, "\nUsage: %s -online\nGenerates and traverses a BDD online mode\n", arg[0]); - return code; + // if the value is too small or too big, return + // 0/1 to avoid long computations + if (x < -10.0) { + return 0.0; } - RAPIDLOADON; - if (strcmp("-online", arg[1]) == 0) { - MyManager.manager = simpleBDDinit(0); - MyManager.t = HIGH(MyManager.manager); - MyManager.f = LOW(MyManager.manager); - MyManager.varmap = InitNamedVars(1, 0); - bdd = OnlineGenerateBDD(MyManager.manager, &MyManager.varmap); + + if (x > 10.0) { + return 1.0; + } + + // t is the value from last iteration + // s is the value from the current iteration + // iterate until they are equal + while(fabs(s-t) >= DBL_MIN) { + t=s; + i+=2; + b*=q/i; + s+=b; + } + + return 0.5+s*exp(-0.5*q-0.91893853320467274178); +} + +// integrates the normal distribution over [low,high] +double cumulative_normal(double low, double high, double mu, double sigma) { + return Phi((high-mu)/sigma) - Phi((low-mu)/sigma); +} + +// integrates the normal distribution over [-oo,high] +double cumulative_normal_upper(double high, double mu, double sigma) { + return Phi((high-mu)/sigma); +} + + +// evaluates the density of the normal distribution +double normal(double x, double mu,double sigma) { + double inner=(x-mu)/sigma; + double denom=sigma*sqrt(2*3.14159265358979323846); + return exp(-inner*inner/2)/denom; +} + +double cumulative_normal_dmu(double low, double high,double mu,double sigma) { + return normal(low,mu,sigma) - normal(high,mu,sigma); +} + +double cumulative_normal_upper_dmu(double high,double mu,double sigma) { + return - normal(high,mu,sigma); +} + + +double cumulative_normal_dsigma(double low, double high,double mu,double sigma) { + return (((mu-high)*normal(high,mu,sigma) - (mu-low)*normal(low,mu,sigma))/sigma); +} + +double cumulative_normal_upper_dsigma(double high,double mu,double sigma) { + return (mu-high)*normal(high,mu,sigma); +} + + +// this function parses two strings "$a;$b" and "???_???l$ch$d" where $a-$d are (real) numbers +// 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; + int i; + char garbage[64], s1[64],s2[64],s3[64],s4[64]; + + if(sscanf(input, "%64[^;];%64[^;]", s1,s2) != 2) { + fprintf(stderr, "Error at parsing the string %s in the function parse_density_integral_string\n",input); + fprintf(stderr, "The string should contain 2 fields seperated by ; characters.\n"); + exit(EXIT_FAILURE); + } + + if (IsRealNumber(s1)) { + result.mu=atof(s1); } else { - fileheader = ReadFileHeader(arg[1]); - switch(fileheader.filetype) { - case BDDFILE_SCRIPT: - MyManager.manager = simpleBDDinit(fileheader.varcnt); - MyManager.t = HIGH(MyManager.manager); - MyManager.f = LOW(MyManager.manager); - MyManager.varmap = InitNamedVars(fileheader.varcnt, fileheader.varstart); - bdd = FileGenerateBDD(MyManager.manager, MyManager.varmap, fileheader); - break; - case BDDFILE_NODEDUMP: - MyManager.manager = simpleBDDinit(fileheader.varcnt); - MyManager.t = HIGH(MyManager.manager); - MyManager.f = LOW(MyManager.manager); - MyManager.varmap = InitNamedVars(fileheader.varcnt, fileheader.varstart); - bdd = LoadNodeDump(MyManager.manager, MyManager.varmap, fileheader.inputfile); - break; - default: - fprintf(stderr, "Error: not a valid file format to load.\n"); - return code; - break; - } + 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 (bdd != NULL) { - printf("Do you want to load parameter values from testdata.txt [y]? "); yn = getchar(); getchar(); - if (yn == 'y') LoadVariableData(MyManager.varmap, "testdata.txt"); - code = 0; - MyManager.his = InitHistory(GetVarCount(MyManager.manager)); - if (strcmp("-online", arg[1]) != 0) { - DFS(MyManager, bdd); - printf("Do you need an export [y]? "); yn = getchar(); getchar(); - if (yn == 'y') simpleNamedBDDtoDot(MyManager.manager, MyManager.varmap, bdd, "SimpleCUDDExport.dot"); - printf("Do you want a save [y]? "); yn = getchar(); getchar(); - if (yn == 'y') SaveNodeDump(MyManager.manager, MyManager.varmap, bdd, "SimpleCUDDSave.sav"); - printf("Do you want to see all true paths [y]? "); yn = getchar(); getchar(); - if (yn == 'y') { - ReInitHistory(MyManager.his, GetVarCount(MyManager.manager)); - getalltruepaths(MyManager, bdd, "", ""); - } - } else { - onlinetraverse(MyManager.manager, MyManager.varmap, MyManager.his, bdd); - } - } - if (MyManager.manager != NULL) KillBDD(MyManager.manager); - return code; -} -void DFS(extmanager MyManager, DdNode *Current) { - DdNode *h, *l; - hisnode *Found; - char *curnode; - curnode = GetNodeVarNameDisp(MyManager.manager, MyManager.varmap, Current); - if (GetIndex(Current) < MyManager.varmap.varcnt) { - printf("%s(%f,%i,%s)\n", curnode, MyManager.varmap.dvalue[GetIndex(Current)], MyManager.varmap.ivalue[GetIndex(Current)], (char *) MyManager.varmap.dynvalue[GetIndex(Current)]); + if (IsRealNumber(s2)) { + result.log_sigma=atof(s2); } else { - printf("%s\n", curnode); + 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); } - if ((Current != MyManager.t) && (Current != MyManager.f) && - ((Found = GetNode(MyManager.his, MyManager.varmap.varstart, Current)) == NULL)) { - l = LowNodeOf(MyManager.manager, Current); - h = HighNodeOf(MyManager.manager, Current); - printf("l(%s)->", curnode); - DFS(MyManager, l); - printf("h(%s)->", curnode); - DFS(MyManager, h); - AddNode(MyManager.his, MyManager.varmap.varstart, Current, 0.0, 0, NULL); - } -} -void getalltruepaths(extmanager MyManager, DdNode *Current, const char *startpath, const char *prevvar) { - DdNode *h, *l; - char *curnode, *curpath; - int pathmaxsize = 1024; - curpath = (char *) malloc(sizeof(char) * pathmaxsize); - curpath[0] = '\0'; - pathmaxsize = bufstrcat(curpath, pathmaxsize, startpath); - pathmaxsize = bufstrcat(curpath, pathmaxsize, prevvar); - pathmaxsize = bufstrcat(curpath, pathmaxsize, "*"); - curnode = GetNodeVarNameDisp(MyManager.manager, MyManager.varmap, Current); - if (Current == MyManager.t) { - printf("%s\n", curpath); - } else if (Current != MyManager.f) { - h = HighNodeOf(MyManager.manager, Current); - if (h != MyManager.f) { - getalltruepaths(MyManager, h, curpath, curnode); +/* if (result.sigma<=0) { */ +/* fprintf(stderr, "Error at parsing the string %s in the function parse_density_integral_string",input); */ +/* fprintf(stderr, "The value for sigma has to be larger than 0.\n"); */ + +/* exit(EXIT_FAILURE); */ +/* } */ + + if (sscanf(variablename,"%64[^lh]l%64[^lh]h%64[^lh]",garbage,s3,s4) != 3) { + fprintf(stderr, "Error at parsing the string %s in the function parse_density_integral_string\n",variablename); + fprintf(stderr, "The string should contain 2 fields seperated by ; characters.\n"); + exit(EXIT_FAILURE); + } + + // replace the d by . in s1 and s2 + for(i=0; s3[i]!='\0' ; i++) { + if (s3[i]=='d') { + s3[i]='.'; } - l = LowNodeOf(MyManager.manager, Current); - if (l != MyManager.f) { - pathmaxsize = bufstrcat(curpath, pathmaxsize, "~"); - getalltruepaths(MyManager, l, curpath, curnode); + if (s3[i]=='m') { + s3[i]='-'; } } - free(curpath); -} - -int bufstrcat(char *targetstr, int targetmem, const char *srcstr) { - int strinc = strlen(srcstr), strsize = strlen(targetstr); - while ((strsize + strinc) > (targetmem - 1)) { - targetmem *= 2; - targetstr = (char *) realloc(targetstr, sizeof(char) * targetmem); + for(i=0; s4[i]!='\0' ; i++) { + if (s4[i]=='d') { + s4[i]='.'; + } + if (s4[i]=='m') { + s4[i]='-'; + } } - strcat(targetstr, srcstr); - return targetmem; + + if (IsRealNumber(s3)) { + result.low=atof(s3); + } else { + 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 { + 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); + } + + + if (result.low>result.high) { + fprintf(stderr, "Error ar parsing the string %s in the function parse_density_integral_string\n",input); + fprintf(stderr, "The value for low has to be larger than then value for high.\n"); + fprintf(stderr, " was [%f, %f]\n",result.low, result.high); + fprintf(stderr, " input %s \n",input); + fprintf(stderr, " variablename %s \n",variablename); + + exit(EXIT_FAILURE); + } + + + return result; } diff --git a/packages/ProbLog/simplecudd/problogmath.h b/packages/ProbLog/simplecudd/problogmath.h new file mode 100644 index 000000000..17a7c8270 --- /dev/null +++ b/packages/ProbLog/simplecudd/problogmath.h @@ -0,0 +1,216 @@ +/******************************************************************************\ +* * +* 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 * +* * +* Author: Bernd Gutmann * +* File: problogmath.h * +* $Date:: 2010-08-25 15:23:30 +0200 (Wed, 25 Aug 2010) $ * +* $Revision:: 4683 $ * +* * +* * +******************************************************************************** +* * +* 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. * +* * +* The End * +* * +\******************************************************************************/ + +#include +#include +#include +#include +#include + +typedef struct _density_integral { + double low; + double high; + double mu; + double log_sigma; +} density_integral; + + + +double sigmoid(double x, double slope); +double Phi(double x); + +double cumulative_normal(double low, double high, double sigma, double mu); +double cumulative_normal_dmu(double low, double high,double mu,double sigma); +double cumulative_normal_dsigma(double low, double high,double mu,double sigma); + +double cumulative_normal_upper(double high, double mu, double sigma); +double cumulative_normal_upper_dsigma(double high,double mu,double sigma); +double cumulative_normal_upper_dmu(double high,double mu,double sigma); + +density_integral parse_density_integral_string(char *input, char *variablename); diff --git a/packages/ProbLog/simplecudd/simplecudd.c b/packages/ProbLog/simplecudd/simplecudd.c index f502be8b4..cdd607264 100644 --- a/packages/ProbLog/simplecudd/simplecudd.c +++ b/packages/ProbLog/simplecudd/simplecudd.c @@ -3,7 +3,7 @@ * 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 * +* Copyright Katholieke Universiteit Leuven 2008, 2009, 2010 * * * * Author: Theofrastos Mantadelis * * File: simplecudd.c * @@ -203,6 +203,16 @@ DdManager* simpleBDDinit(int varcnt) { return temp; } +DdManager* simpleBDDinitNoReOrder(int varcnt) { + DdManager *temp; + temp = Cudd_Init(varcnt, 0, CUDD_UNIQUE_SLOTS, CUDD_CACHE_SLOTS, 0); + Cudd_AutodynDisable(temp);// Cudd_AutodynEnable(temp, CUDD_REORDER_NONE); + Cudd_SetMaxCacheHard(temp, 1024*1024*1024); + Cudd_SetLooseUpTo(temp, 1024*1024*512); + if (_debug) Cudd_EnableReorderingReporting(temp); + return temp; +} + /* BDD tree travesrsing */ DdNode* HighNodeOf(DdManager *manager, DdNode *node) { @@ -305,6 +315,7 @@ bddfileheader ReadFileHeader(char *filename) { case BDDFILE_SCRIPT: switch (temp.version) { case 1: + case 2: fscanf(temp.inputfile, "%i\n", &temp.varcnt); fscanf(temp.inputfile, "%i\n", &temp.varstart); fscanf(temp.inputfile, "%i\n", &temp.intercnt); @@ -583,6 +594,50 @@ void ExpandNodes(hisqueue *Nodes, int index, int nodenum) { Nodes[index].cnt = nodenum + 1; } +char** GetVariableOrder(char *filename, int varcnt) { + FILE *data; + char *dataread, buf, **varname; + int icur = 0, maxbufsize = 10, index = -1; + if ((data = fopen(filename, "r")) == NULL) { + perror(filename); + return NULL; + } + varname = (char **) malloc(sizeof(char *) * varcnt); + dataread = (char *) malloc(sizeof(char) * maxbufsize); + while(!feof(data)) { + fread(&buf, 1, 1, data); + if (buf == '\n') { + dataread[icur] = '\0'; + icur = 0; + buf = ' '; + if (dataread[0] == '@') { + index++; + varname[index] = (char *) malloc(sizeof(char) * strlen(dataread)); + strcpy(varname[index], dataread + 1); + } + } else { + dataread[icur] = buf; + icur++; + if (icur == _maxbufsize) { + fprintf(stderr, "Error: Maximum buffer size(%i) exceeded.\n", _maxbufsize); + fclose(data); + free(varname); + free(dataread); + return NULL; + } + while (icur > maxbufsize - 1) { + maxbufsize *= 2; + dataread = (char *) realloc(dataread, sizeof(char) * maxbufsize); + } + } + } + fclose(data); + free(dataread); + for(icur=index+1; icur < varcnt; icur++) + varname[icur] = NULL; + return varname; +} + int LoadVariableData(namedvars varmap, char *filename) { FILE *data; char *dataread, buf, *varname, *dynvalue; @@ -596,14 +651,16 @@ int LoadVariableData(namedvars varmap, char *filename) { dataread = (char *) malloc(sizeof(char) * maxbufsize); while(!feof(data)) { fread(&buf, 1, 1, data); - if (buf == '\n') { + if ((buf == '\n') && icur == 0) { + // ignore empty lines + } else if (buf == '\n') { dataread[icur] = '\0'; icur = 0; buf = ' '; if (dataread[0] == '@') { if (hasvar) { for (index = 0; index < varmap.varcnt; index++) { - if (patternmatch(varname, varmap.vars[index])) { + if ((varmap.vars[index] != NULL) && (patternmatch(varname, varmap.vars[index]))) { varmap.loaded[index] = 1; varmap.dvalue[index] = dvalue; varmap.ivalue[index] = ivalue; @@ -679,7 +736,7 @@ int LoadVariableData(namedvars varmap, char *filename) { } if (hasvar) { for (index = 0; index < varmap.varcnt; index++) { - if (patternmatch(varname, varmap.vars[index])) { + if ((varmap.vars[index] != NULL) && (patternmatch(varname, varmap.vars[index]))) { varmap.loaded[index] = 1; varmap.dvalue[index] = dvalue; varmap.ivalue[index] = ivalue; @@ -879,12 +936,56 @@ int all_loaded(namedvars varmap, int disp) { return res; } +int ImposeOrder(DdManager *manager, const namedvars varmap, char **map) { + int order[varmap.varcnt], i, mappos, index = -1, ivar; + for (i = 0; i < varmap.varcnt; i++) { + if (map[i] != NULL) { + order[i] = GetNamedVarIndex(varmap, map[i]); + index = i; + } else { + order[i] = -1; + } + } + index++; + for (i = 0; i < varmap.varcnt; i++) { + ivar = Cudd_ReadPerm(manager, i); + mappos = get_var_pos_in_map(map, varmap.vars[ivar], varmap.varcnt); + if (mappos == -1) { + order[index] = ivar; + index++; + } + } + if (index != varmap.varcnt) + fprintf(stderr, "Warning possible error in: Impose Order...\n"); + return Cudd_ShuffleHeap(manager, order); +} + +int get_var_pos_in_map(char **map, const char *var, int varcnt) { + int i; + for (i = 0; i < varcnt; i++) { + if (map[i] == NULL) return -1; + if (strcmp(map[i], var) == 0) return i; + } + return -1; +} + /* Parser */ DdNode* FileGenerateBDD(DdManager *manager, namedvars varmap, bddfileheader fileheader) { + return (FileGenerateBDDForest(manager, varmap, fileheader))[0]; +} + +void unreference(DdManager *manager, DdNode ** intermediates, int count){ +// int i; +// for(i = 0;i -1 && curinter < fileheader.intercnt && inter[curinter] != NULL) { - if (_debug) fprintf(stderr, "Returned: %s\n", inputline); - fclose(fileheader.inputfile); - Line = inter[curinter]; - free(inter); - free(inputline); - return Line; + if (fileheader.version < 2) { + if (inputline[0] == 'L' && IsPosNumber(inputline + 1)) { + curinter = atoi(inputline + 1) - 1; + if (curinter > -1 && curinter < fileheader.intercnt && inter[curinter] != NULL) { + if (_debug) fprintf(stderr, "Returned: %s\n", inputline); + fclose(fileheader.inputfile); + result = (DdNode **) malloc(sizeof(DdNode *) * 1); + result[0] = inter[curinter]; + Cudd_Ref(result[0]); + unreference(manager, inter, fileheader.intercnt); + free(inter); + free(inputline); + return result; + } else { + fprintf(stderr, "Error at line: %i. Return result asked doesn't exist.\n", iline); + fclose(fileheader.inputfile); + free(inter); + free(inputline); + return NULL; + } } else { - fprintf(stderr, "Error at line: %i. Return result asked doesn't exist.\n", iline); + fprintf(stderr, "Error at line: %i. Invalid intermediate result format.\n", iline); fclose(fileheader.inputfile); free(inter); free(inputline); return NULL; } } else { - fprintf(stderr, "Error at line: %i. Invalid intermediate result format.\n", iline); + // Support for forest + result = (DdNode **) malloc(sizeof(DdNode *) * 10); + maxlinesize = 10; + iline = -1; + for (subl = strtok(inputline, ","); subl != NULL; subl = strtok(NULL, ",")) { + if (subl[0] == 'L' && IsPosNumber(subl + 1)) { + curinter = atoi(subl + 1) - 1; + if (curinter > -1 && curinter < fileheader.intercnt && inter[curinter] != NULL) { + iline++; + if (iline >= (maxlinesize - 1)) { + maxlinesize *= 2; + result = (DdNode **) realloc(result, sizeof(DdNode *) * maxlinesize); + } + Cudd_Ref(inter[curinter]); + result[iline] = inter[curinter]; + } else { + fprintf(stderr, "Error at line: %i. Return result asked(%s) doesn't exist.\n", iline, subl); + fclose(fileheader.inputfile); + free(inter); + free(inputline); + free(subl); + return NULL; + } + } else { + fprintf(stderr, "Error at line: %i. Invalid intermediate result format.\n", iline); + fclose(fileheader.inputfile); + free(inter); + free(inputline); + free(subl); + return NULL; + } + } + if (_debug) fprintf(stderr, "Returned: %s\n", inputline); fclose(fileheader.inputfile); + unreference(manager, inter, fileheader.intercnt); free(inter); free(inputline); - return NULL; + free(subl); + iline++; + result[iline] = NULL; + return result; } } else if (curinter > -1 && curinter < fileheader.intercnt && inter[curinter] == NULL) { if (_debug) fprintf(stderr, "%i %s\n", curinter, inputline); @@ -1050,7 +1198,7 @@ DdNode* LineParser(DdManager *manager, namedvars varmap, DdNode **inter, int max int istart, iend, ilength, i, symbol, ivar, inegvar, inegoper, iconst; long startAt, endAt; double secs; - DdNode *bdd; + DdNode *bdd, *temp; char *term, curoper; bdd = HIGH(manager); Cudd_Ref(bdd); @@ -1092,6 +1240,10 @@ DdNode* LineParser(DdManager *manager, namedvars varmap, DdNode **inter, int max } else { iconst = 0; ivar = AddNamedVar(varmap, term + inegvar); +/* if (ivar == -1) { + EnlargeNamedVars(&varmap, varmap.varcnt + 1); + ivar = AddNamedVar(varmap, term + inegvar); + }*/ if (ivar == -1) { fprintf(stderr, "Line Parser Error at line: %i. More BDD variables than the reserved term: %s.\n", iline, term); free(term); diff --git a/packages/ProbLog/simplecudd/simplecudd.h b/packages/ProbLog/simplecudd/simplecudd.h index cbc114954..688e80d1e 100644 --- a/packages/ProbLog/simplecudd/simplecudd.h +++ b/packages/ProbLog/simplecudd/simplecudd.h @@ -3,7 +3,7 @@ * 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 * +* Copyright Katholieke Universiteit Leuven 2008, 2009, 2010 * * * * Author: Theofrastos Mantadelis * * File: simplecudd.h * @@ -263,6 +263,7 @@ typedef struct _nodeline { /* Initialization */ DdManager* simpleBDDinit(int varcnt); +DdManager* simpleBDDinitNoReOrder(int varcnt); /* BDD Generation */ @@ -274,6 +275,7 @@ DdNode* D_BDDXor(DdManager *manager, DdNode *bdd1, DdNode *bdd2); DdNode* D_BDDXnor(DdManager *manager, DdNode *bdd1, DdNode *bdd2); DdNode* FileGenerateBDD(DdManager *manager, namedvars varmap, bddfileheader fileheader); +DdNode** FileGenerateBDDForest(DdManager *manager, namedvars varmap, bddfileheader fileheader); DdNode* OnlineGenerateBDD(DdManager *manager, namedvars *varmap); DdNode* LineParser(DdManager *manager, namedvars varmap, DdNode **inter, int maxinter, char *function, int iline); DdNode* OnlineLineParser(DdManager *manager, namedvars *varmap, DdNode **inter, int maxinter, char *function, int iline); @@ -281,10 +283,13 @@ DdNode* BDD_Operator(DdManager *manager, DdNode *bdd1, DdNode *bdd2, char Operat int getInterBDD(char *function); char* getFileName(const char *function); int GetParam(char *inputline, int iParam); +char** GetVariableOrder(char *filename, int varcnt); int LoadVariableData(namedvars varmap, char *filename); /* Named variables */ +int ImposeOrder(DdManager *manager, const namedvars varmap, char **map); +int get_var_pos_in_map(char **map, const char *var, int varcnt); namedvars InitNamedVars(int varcnt, int varstart); void EnlargeNamedVars(namedvars *varmap, int newvarcnt); int AddNamedVarAt(namedvars varmap, const char *varname, int index);