function [p,corners] = CS_KB_mean_chop(KB)
% CS_KB_mean_chop - get atom probabilities and extrema projection points
% On input:
%     KB (struct KB): KB info
%      KB(k).clauses (1xk vector): disjunction as integers
%      KB(k).prob (float): clause probability
% On output:
%     p (nx1 vector): mean atom probabilities (wrt projected points)
%     corners (2nxn array): feasible region points associated with
%                           projected extrema
% Call:
%     [p,co] = CS_KB_mean_chop(KB);
% Author:
%     T. Henderson
%     UU
%     Summer 2022
%

num_atoms = max(CS_vars(KB,[]));
p = zeros(num_atoms,1);

h = CS_KB2hp(KB);
min_x = -ones(num_atoms,1);
max_x = ones(num_atoms,1);
corners = zeros(2*num_atoms,num_atoms);
options = optimoptions('linprog','Display','none');
coefs = -h(:,1:end-1);
b = h(:,end);
for k = 1:num_atoms
    [k,num_atoms];
    index = (k-1)*2 + 1;
    e = zeros(num_atoms,1);
    e(k) = 1;
    x = linprog(e,coefs,b,[],[],min_x,max_x,options);
    if isempty(x)
        p = [];
        return
    end
    if length(find(x>0.9|x<0.1))==num_atoms
        k;
    end
    corners(index,:) = x;
    index = index + 1;
    e = -e;
    x = linprog(e,coefs,b,[],[],min_x,max_x,options);
    corners(index,:) = x;
end

p = mean(corners);

tch = 0;
