% company.pl, uses table modes as supported by B-Prolog version 7.3 % by Neng-Fa Zhou, April 8, 2009 % http://www.cs.kuleuven.be/~dtai/events/ASP-competition/Benchmarks/CompanyControl.shtml solve(As):- assert_facts(As), controls(_,_), % produce all answers into the table checkForMaxControls(C0), % a given fact get_controled(C0,_,Len0), (company(C), get_controled(C,_,Len), Len>Len0 -> format("UNSATISFIABLE~n"); output_proof), abolish_all. :-table controls/2. controls(X,Y):- company(X), company(Y), own(X,Y,_,P), P>50. %% only table an answer with the maximum accumulated shares :-table own(+,+,-,max). own(X,Y,[],P):- % direct (owns(X,Y,P)->true;P=0). own(X,Y,[Z|Cs],P):- % indirect own(X,Y,Cs,P1), controls(X,Z), \+ member(Z,Cs), Z\==X, owns(Z,Y,P2), P is P1+P2. get_controled(C,ControledCs,Len):- findall(ControledC,controls(C,ControledC),ControledCs), length(ControledCs,Len). output_proof:- company(C), get_controled(C,ControledCs,_), output(C,ControledCs), fail. output_proof. output(_,[]). output(X,[Y|Ys]):- format("controls(~w,~w). ",[X,Y]), output(X,Ys). test:- solve([checkForMaxControls(c1),company(c1),company(c2),company(c3),company(c4),owns(c1,c2,60),owns(c1,c3,20),owns(c2,c3,40),owns(c3,c4,51)]).