function [probs,q] = BR_MC3(KB,samp_size,max_samples)
%

ZERO_THRESH = 0.001;

E = Inf;

m = length(KB);
n = length(BR_vars(KB,[]));
x = rand(samp_size,n)<0.5;
c_len = zeros(m,1);
for c = 1:m
    c_len(c) = length(KB(c).clauses);
end
indexes = find(c_len==1);
for k = 1:length(indexes)
    index = indexes(k);
    clause = KB(index).clauses;
    x(:,abs(clause)) = 1 - 1*(sign(clause)<0);
end

[v,q] = BR_sentence_err(KB,x);
tr = [v];
t = 1;
figure(2);
clf
plot(0,tr(1,1),'k.');
hold on
while t<max_samples&E>ZERO_THRESH
    t = t + 1;
    if rem(t,1000)==0
        tch = 0;
    end
    i = randi(samp_size);
    j = randi(n);
    y = x;
    y(i,j) = 1 - x(i,j);
    y(i,:) = rand(1,n)<0.5;
    for k = 1:length(indexes)
        index = indexes(k);
        clause = KB(index).clauses;
        x(:,abs(clause)) = 1 - 1*(sign(clause)<0);
    end
    [v2,q2] = BR_sentence_err(KB,y);
    if v2<tr(t-1,1)
%    if U<=alpha
        x = y;
        tr = [tr;v2];
        E = v2;
        q = q2;
    else
        tr = [tr;tr(t-1,1)];
    end
    plot([t-1,t],[tr(t-1,1),tr(t,1)],'k');
    drawnow;
end
probs = mean(x);
tch = 0;
return

samps = zeros(n,1);
t = 0;
while t<n
    t = t + 1;
    y = normrnd(x,sigma);
    U = rand;
    v1 = feval(fname,x);
    v2 = feval(fname,y);
    v = v2/v1;
    if isnan(v)
        v = 1;
    end
    alpha = min(1,v);
    alpha = v;
    if U<=alpha
        samps(t) = y;
        x = y;
    else
        samps(t) = x;
    end
end

E = mean(samps);
tch = 0;
