function P_cc = BR_Nilsson(KB)
% BR_Nilsson - Nilsson's PSAT solution
% On input:
%     KB (KB struct): KB clauses
% On output:
%     P_cc (kx1 vector): probabilities of complete conjunctions
% Call:
%     KB(1).clauses = [1];
%     KB(1).prob = 0.7;
%     KB(2).clauses = [-1,2];
%     KB(2).prob = 0.7;
%     P = BR_Nilsson(KB);
% Author:
%     T. Henderson
%     UU
%     Fall 2018
%

num_sentences = length(KB);
vars = BR_vars(KB,[]);
num_vars = length(vars);
num_cc = 2^num_vars;
V = zeros(num_sentences+1,num_cc);
P_s = zeros(num_sentences+1,1);
V(end,:) = ones(1,num_cc);
P_s(end) = 1;

for s = 1:num_sentences
    P_s(s) = KB(s).prob;
    for cc = 0:num_cc-1
        bits = BR_int2bits(cc,num_vars);
        V(s,cc+1) = BR_sat(KB(s).clauses,bits);
    end
end

P_cc = lsqlin(V,P_s,[],[],[],[],zeros(num_cc,1),ones(num_cc,1));

