backtracking - Find a boolean circuit with prolog -
the problem following : considering 3 inputs a,b,c, find boolean circuit and,or , not gates such output not(a), not(b), not(c) using @ 2 not gates.
i find circuit prolog. idea compute predicate "accessible" takes function , says if exists circuit computes f.
i have following predicates :
not([],[]). not([h|t],[g|s]) :- g #=# 1-h, not(t,s). or([],[],[]). or([0|t],[0|s],[0|r]) :- or(t,s,r). or([1|t],[0|s],[1|r]) :- or(t,s,r). or([1|t],[1|s],[1|r]) :- or(t,s,r). or([0|t],[1|s],[1|r]) :- or(t,s,r). and([],[],[]). and([1|t],[1|s],[1|r]) :- and(t,s,r). and([0|t],[1|s],[0|r]) :- and(t,s,r). and([1|t],[0|s],[0|r]) :- and(t,s,r). and([0|t],[0|s],[0|r]) :- and(t,s,r). accessible(_,_,0) :- !,fail. accessible([0,1,0,1,0,1,0,1],[12],_) :- !. accessible([0,0,1,1,0,0,1,1],[11],_) :- !. accessible([0,0,0,0,1,1,1,1],[10],_) :- !. accessible(f,l,c) :- cc c-1, or(g,h,f), accessible(g,m,cc), accessible(h,n,cc), l=[0, [m,n]]. accessible(f,l,c) :- cc c-1, and(g,h,f), accessible(g,m,cc), accessible(h,n,cc), l=[1,[m,n]]. accessible(f,l,c) :- cc c-1, not(f,x), accessible(x,m,cc), l=[2,m].
i compute function xor between 11,12 try following goal : accessible([0,1,1,0,0,1,1,0], x, 4).
but prolog runs while before getting answer. i'd know how improve program in order make faster.
p.s. how print string without ascii codes gnu prolog ?
you searching on arbitrary formed boolean expressions, , asking boolean algebra on bit arrays generated following bit arrays:
01010101 00110011
just recall normal forms of boolean algebra. example conjunctive normal form. conjunctive normal form reads follows:
/\ clause_i
whereby each clause has form:
\/ literal_i
and each literal has either 1 of following forms:
variable ~ variable
just take 2 variables generator bit arrays. reduces search space somehow. 2 variables there 4 different clauses. makes 2^4 different normal forms.
further, if have goal find normal form results in bit array, such specified:
01100110
you can further prune search considering value lower bound.
bye
Comments
Post a Comment