This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
yap-6.3/packages/bdd/ddnnf.yap

250 lines
5.7 KiB
Plaintext
Raw Normal View History

2012-04-16 23:47:36 +01:00
:- module(ddnnf,
2012-04-20 22:43:30 +01:00
[cnf_to_ddnnf/3,
ddnnf/3,
2012-04-16 23:47:36 +01:00
ddnnf_is/2]).
:- use_module(library(lists)).
:- use_module(library(readutil)).
:- use_module(library(lineutils)).
:- use_module(library(terms)).
:- use_module(library(cnf)).
:- use_module(library(simpbool)).
2012-04-20 13:07:19 +01:00
%
% convert a CNF as list with Variables Vars and Existential variables
% in DDNNF, Exs \in LVars into DDNNF with extra existential vars
%
2012-04-20 22:43:30 +01:00
cnf_to_ddnnf(CNF0, PVs, DDNNF) :-
2012-04-20 13:07:19 +01:00
list2cnf(CNF0, CNF, []),
2012-04-23 13:13:25 +01:00
mkddnnf(CNF, PVs, DDNNF).
mkddnnf(CNF, PVs, DDNNF) :-
term_variables(CNF, AllVars),
2012-04-20 13:07:19 +01:00
% (numbervars(CNF,1,_), writeln(CNF), fail ; true),
open(dimacs, write, S),
cnf_to_file(CNF, AllVars, S),
2012-04-16 23:47:36 +01:00
close(S),
2012-04-20 13:07:19 +01:00
% execute c2d at this point, but we're lazy%
% unix(system('c2d -dt_method 3 -in dimacs')),
2012-04-23 13:13:25 +01:00
% unix(system('c2d -visualize -in dimacs')),
unix(system('dsharp -Fnnf dimacs.nnf dimacs')),
2012-04-16 23:47:36 +01:00
open('dimacs.nnf',read,R),
2012-04-20 13:07:19 +01:00
SVars =.. [v|AllVars],
2012-04-16 23:47:36 +01:00
% ones(LVars),
2012-04-20 22:43:30 +01:00
input_ddnnf(R, SVars, PVs, DDNNF),
2012-04-16 23:47:36 +01:00
% writeln(DDNNF),
close(R).
2012-04-20 13:07:19 +01:00
list2cnf([]) --> [].
list2cnf([(O=A)|Impls]) --> !,
{cvt(O,FO,NO),
and2cnf(A,Conj,[]) },
[[FO|Conj]],
disj(A, NO),
list2cnf(Impls).
list2cnf([CNF|Impls]) -->
2012-04-16 23:47:36 +01:00
{ to_format(CNF, Format, []) },
[Format],
2012-04-20 13:07:19 +01:00
list2cnf(Impls).
cvt(O,O,-O) :- var(O), !.
cvt(not(O),-O,O).
2012-04-16 23:47:36 +01:00
2012-04-20 13:07:19 +01:00
neg(O,-O) :- var(O), !.
neg(-O,O).
2012-04-16 23:47:36 +01:00
to_format(A) -->
{ var(A) },
!,
[A].
to_format(A+B) -->
!,
to_format(A),
to_format(B).
to_format(not(A)) -->
!,
[-A].
to_format(A) -->
[A].
and2cnf(A) -->
{ var(A) },
!,
[-A].
and2cnf(A*B) -->
!,
and2cnf(A),
and2cnf(B).
and2cnf(not(A)) -->
!,
[A].
and2cnf(A) -->
!,
[-A].
2012-04-20 13:07:19 +01:00
disj(A, NO) -->
{ var(A) }, !,
[[NO,A]].
disj(A*B, NO) --> !,
disj(A, NO),
disj(B, NO).
disj(A, NO) -->
[[NO,A]].
%
% convert a boolean expression with Variables Vars and Existential variables
% in DDNNF, Exs \in LVars into DDNNF with extra existential vars
%
% ex: (A*B+not(B))*(C=B) into something complicated
%
2012-04-20 22:43:30 +01:00
ddnnf(List, PVs, DDNNF) :-
2012-04-16 23:47:36 +01:00
exps2conj(List, Conj),
2012-04-20 13:07:19 +01:00
cnf(Conj, CNF),
2012-04-23 13:13:25 +01:00
mkddnnf(CNF, PVs, DDNNF).
2012-04-16 23:47:36 +01:00
2012-04-20 13:07:19 +01:00
exps2conj((C1,C2), CC1*CC2) :- !,
exps2conj(C1, CC1),
exps2conj(C2, CC2).
exps2conj((Conj), CConj) :-
cvt_el(Conj, CConj).
2012-04-16 23:47:36 +01:00
cvt_el(V, V) :- var(V), !.
cvt_el(not(X), -X1) :- !,
cvt_el(X, X1).
cvt_el(X+Y, X1+Y1) :- !,
cvt_el(X, X1),
cvt_el(Y, Y1).
cvt_el(X*Y, X1*Y1) :- !,
cvt_el(X, X1),
cvt_el(Y, Y1).
2012-04-20 13:07:19 +01:00
cvt_el(X=Y, X1==Y1) :- !,
cvt_el(X, X1),
cvt_el(Y, Y1).
2012-04-16 23:47:36 +01:00
cvt_el(X, X).
cnf_to_file(List, Vars, S) :-
number_ivars(Vars, 1, M),
length(List, N),
M1 is M-1,
format(S,'p cnf ~d ~d~n',[M1,N]),
output_list(List, S),
fail.
cnf_to_file(_List, _Vars, _S).
number_ivars([], M, M).
number_ivars([I0|IVars], I0, M) :-
I is I0+1,
number_ivars(IVars, I, M).
output_list([], _S).
output_list([CNF|List], S) :-
output_cnf(CNF, S),
output_list(List, S).
output_cnf([], S) :-
format(S, '0~n', []).
output_cnf([-V|CNF], S) :- !,
format(S, '-~d ',[V]),
output_cnf(CNF, S).
output_cnf([V|CNF], S) :-
format(S, '~d ',[V]),
output_cnf(CNF, S).
2012-04-20 22:43:30 +01:00
input_ddnnf(Stream, SVars, PVs, ddnnf(Out, SVars, Result)) :-
2012-04-16 23:47:36 +01:00
read_line_to_codes(Stream, Header),
split(Header, ["nnf",VS,_ES,_NS]),
number_codes(NVs, VS),
functor(TempResults, nnf, NVs),
2012-04-20 22:43:30 +01:00
process_nnf_lines(Stream, SVars, PVs, 1, TempResults, Out, Last),
2012-04-16 23:47:36 +01:00
Last1 is Last-1,
arg(Last1, TempResults, Result).
2012-04-20 22:43:30 +01:00
process_nnf_lines(Stream, SVars, PVs, LineNumber, TempResults, O, LL) :-
2012-04-16 23:47:36 +01:00
read_line_to_codes(Stream, Codes),
( Codes = end_of_file -> O = [], LL = LineNumber ;
% (LineNumber > 1 -> N is LineNumber-1, arg(N,TempResults,P), format("~w ",[P]);true),
% format("~s~n",[Codes]),
arg(LineNumber, TempResults, P),
2012-04-20 22:43:30 +01:00
process_nnf_line(SVars, PVs, TempResults, Exp0, Codes, []),
2012-04-16 23:47:36 +01:00
simplify_line(P=Exp0, Lines, O),
NewLine is LineNumber+1,
2012-04-20 22:43:30 +01:00
process_nnf_lines(Stream, SVars, PVs, NewLine, TempResults, Lines, LL)
2012-04-16 23:47:36 +01:00
).
2012-04-20 22:43:30 +01:00
process_nnf_line(SVars, PVs, _TempResults, Exp) --> "L ",
nnf_leaf(SVars, PVs, Exp).
2012-04-20 13:07:19 +01:00
process_nnf_line(_SVars, _, TempResults, Exp) --> "A ",
2012-04-16 23:47:36 +01:00
nnf_and_node(TempResults, Exp).
2012-04-20 13:07:19 +01:00
process_nnf_line(_SVars, _, TempResults, Exp) --> "O ",
2012-04-16 23:47:36 +01:00
nnf_or_node(TempResults, Exp).
2012-04-20 22:43:30 +01:00
nnf_leaf(SVars, PVs, Prob, Codes, []) :-
2012-04-16 23:47:36 +01:00
number_codes(Number, Codes),
Abs is abs(Number),
arg(Abs, SVars, Node),
2012-04-20 13:07:19 +01:00
(Number < 0 ->
2012-04-20 22:43:30 +01:00
(parameter(Node,PVs) -> Prob = 1-Node ; Prob = 1 )
2012-04-20 13:07:19 +01:00
;
Prob = Node
).
2012-04-20 22:43:30 +01:00
parameter(F,[F1|_Exs]) :- F == F1, !.
parameter(F,[_|Exs]) :-
parameter(F, Exs).
2012-04-16 23:47:36 +01:00
nnf_and_node(TempResults, Product, Codes, []) :-
split(Codes, [_|NumberAsStrings]),
multiply_nodes(NumberAsStrings, TempResults, Product).
multiply_nodes([], _, 1).
multiply_nodes(NumberAsString.NumberAsStrings, TempResults, Product) :-
number_codes(Pos, NumberAsString),
Pos1 is Pos+1,
arg(Pos1, TempResults, P),
Product = Product0*P,
multiply_nodes(NumberAsStrings, TempResults, Product0).
nnf_or_node(TempResults, Sum, Codes, []) :-
split(Codes, [_J,_C|NumberAsStrings]),
add_nodes(NumberAsStrings, TempResults, Sum).
add_nodes([], _, 0).
add_nodes(NumberAsString.NumberAsStrings, TempResults, Product) :-
number_codes(Pos, NumberAsString),
Pos1 is Pos+1,
arg(Pos1, TempResults, P),
Product = Product0+P,
add_nodes(NumberAsStrings, TempResults, Product0).
ones([]).
ones([1|LVars]) :-
ones(LVars).
simplify_line((A=Exp0), List, Final) :-
simplify_exp(Exp0, Exp),
propagate_constants(Exp, A, List, Final).
propagate_constants(Exp, A, Lines, Lines) :- var(Exp), !, A=Exp.
propagate_constants(0, 0, Lines, Lines) :- !.
propagate_constants(1, 1, Lines, Lines) :- !.
propagate_constants(Exp, A, Lines, [(A=Exp)|Lines]).
2012-04-20 13:07:19 +01:00
%
% compute the value of a SP
%
%
2012-04-20 22:43:30 +01:00
ddnnf_is(ddnnf(F, Vs, Out), Out) :-
term_variables(Vs,LVs),
ones(LVs),
2012-04-20 13:07:19 +01:00
%(numbervars(F,1,_),writeln(F),fail;true),
2012-04-16 23:47:36 +01:00
ddnnf_is_acc(F).
%ddnnf_is_acc([H=Exp|_]) :- writeln((H=Exp)),fail.
ddnnf_is_acc([]).
ddnnf_is_acc([H=Exp|Attrs]) :-
H is Exp,
2012-04-20 13:07:19 +01:00
%writeln(Exp:H),
2012-04-16 23:47:36 +01:00
ddnnf_is_acc(Attrs).