function sol = CS_WALKSAT(KB,p,max_flips)
%

sol = [];

wb = waitbar(0,'WALKSAT');
n = max(CS_vars(KB,[]));
model = rand(1,n)>0.5;
for f = 1:max_flips
    waitbar(f/max_flips);
    if CS_check_SAT_sol(KB,model)
        sol = model;
        close(wb);
        return
    end
    clause = CS_unsat_clause(model,KB);
    if rand<p
        atoms = abs(clause);
        num_atoms = length(atoms);
        k = randi(num_atoms);
        literal = clause(k);
        model(abs(literal)) = 1 - model(abs(literal));
    else
        model = CS_flip_max_sat(clause,KB,model);
    end
end
sol = 0;
close(wb);
