function [prob,q,tr] = BR_MC_query_MH3(KB,num_initial,near,...
    max_iter)
%

NUM_INITIAL = num_initial;
NEAR = near;
SIGMA = 0.1;
STEP = 0.3;

m = length(KB);
p = zeros(m,1);
for c = 1:m
    p(c) = KB(c).prob;
end

vars = BR_vars(KB,[]);
n = length(vars);
two_pow_n = 2^n;
omegas = ones(two_pow_n,1)/two_pow_n;

prob = zeros(n,1);
qp = zeros(m,1);
q = zeros(m,1);
iter = 0;
tr = [];

while iter<max_iter&norm(q-p)>NEAR
    iter = iter + 1;
    k = randi(n);
    proposed = omegas;
    down = max(0,omegas(k)-STEP);
    up = min(1,omegas(k)+STEP);
    v = down + rand*(up-down);
    proposed(k) = v;
    proposed = proposed/sum(proposed);
    for c = 1:m
        qp(c) = BR_prob_cc(KB(c).clauses,proposed);
        q(c) = BR_prob_cc(KB(c).clauses,omegas);
    end
    pp = exp(-norm(qp-p));
    ps = exp(-norm(q-p));
    alpha = min(1,pp/ps);
    if rand<=alpha
        omegas = proposed;
    end
    omegas'
    tr = [tr;omegas'];
end
prob = omegas;
tch = 0;
