function Four_Round_Search_small_d(num_protocols, vector, par1,par2,par3)

%% Searches for 4-Round Bit-Commitment Based Coin-Flipping Protocols 
%% with bias less than 0.2499 for messages of dimension 2, 3, 4, or 5
%%
%% Input: (num_protocols, vector, par1, par2, par3), where 
%% num_protocols: number of searches ran
%% vector: the precisions used in each search, e.g. [3, 4, 7]
%% (par1,par2,par3) = (0,0,0) means <= 2 dim messages
%% (par1,par2,par3) = (0,0,1) means <= 3 dim messages
%% (par1,par2,par3) = (0,1,1) means <= 4 dim messages
%% (par1,par2,par3) = (1,1,1) means <= 5 dim messages
%% 
%% Each column of Output2 is the result of a search 
%% (The first entry is the precision, the second a place holder, the third is how many 
%% made it past the symmetry reductions, and then the rest are how
%% many protocols made it past each strategy in the filter from F1 to F8.)
%% 
%% Note: the program also outputs values of beta0 to see if progress is being made.
%%
%% Questions? Contact Jamie at jamiesikora@gmail.com


diary temp

Output = [];
Output2 = [];

time_start = clock;						% To time the search

for p = 1:num_protocols

prec = 1/vector(p);

b = 0.75-0.0001;

format('long')

Best_Bias = b;					% Searching for protocols with bias less than this

iter_beta0_count = 0; 					% ^4 to get protocol count
iter_beta0beta1_count = 0;			% ^2 to get protocol count after symmetry
iter_after_F1 = 0;
iter_after_F2 = 0;
iter_after_F3 = 0;
iter_after_F4 = 0;
iter_after_F5 = 0;
iter_after_F6 = 0;
iter_after_F7 = 0;
iter_after_F8 = 0;
iter_after_SDPA = 0;
iter_after_SDPB0 = 0;
iter_after_SDPB1 = 0;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Protocol Data Generation
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

for bb1 =   0:prec:par1
for bb2 = bb1:prec:par2-bb1
for bb3 = bb2:prec:par3-bb1-bb2
for bb4 = bb3:prec:1-bb1-bb2-bb3
bb5 = 1-bb1-bb2-bb3-bb4;
if bb5 > bb4 - 0.00001
beta0 = [bb1; bb2; bb3; bb4; bb5];

diary off
beta0
diary on

for b1 = 0:prec:min([1, bb5, par1])
for b2 = 0:prec:min([1-b1, bb5, par2])
for b3 = 0:prec:min([1-b1-b2, bb5, par3])
for b4 = 0:prec:min(1-b1-b2-b3, bb5)
b5 = 1-b1-b2-b3-b4;
if b5 < bb5 + 0.00001
beta1 = [b1; b2; b3; b4; b5];

iter_beta0beta1_count = iter_beta0beta1_count + 1;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

F1 = 0.5 + 0.5 * sqrt(beta0)'*sqrt(beta1);
if F1 < Best_Bias  			

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

for aa1 =   0:prec:par1
for aa2 = aa1:prec:par2-aa1
for aa3 = aa2:prec:par3-aa1-aa2
for aa4 = aa3:prec:1-aa1-aa2-aa3
aa5 = 1-aa1-aa2-aa3-aa4;
if aa5 > aa4 - 0.00001
alpha0 = [aa1; aa2; aa3; aa4; aa5];

for a1 = 0:prec:min([1, aa5, par1])
for a2 = 0:prec:min([1-a1, aa5, par2])
for a3 = 0:prec:min([1-a1-a2, aa5, par3])
for a4 = 0:prec:min(1-a1-a2-a3, aa5)
a5 = 1-a1-a2-a3-a4;
if a5 < aa5 + 0.00001
alpha1 = [a1; a2; a3; a4; a5];

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%% 	Filter (Known cheating strategies)  %%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Apply the Filter to weed out bad protocols

iter_after_F1 = iter_after_F1 + 1;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

F2 = 0.5 + 0.25 * norm(alpha0 - alpha1,1);
if F2 < Best_Bias
iter_after_F2 = iter_after_F2 + 1;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

F3 = (0.5 + 0.5 * sqrt(alpha0)'*sqrt(alpha1)) * ...
     (0.5 + 0.25 * norm(beta0 - beta1,1));
if F3 < Best_Bias				
iter_after_F3 = iter_after_F3 + 1;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

F4 = 0.5*(sqrt(beta0)'*sqrt(alpha0))^2 ...
   + 0.5*(sqrt(beta1)'*sqrt(alpha1))^2;
if F4 < Best_Bias				
iter_after_F4 = iter_after_F4 + 1;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

F5 = 0.5*(sqrt(beta0)'*sqrt(alpha1))^2 ...
   + 0.5*(sqrt(beta1)'*sqrt(alpha0))^2;
if F5 < Best_Bias				
iter_after_F5 = iter_after_F5 + 1;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

m=0;
n=0;		  
for counter = 1:5		  
if beta1(counter) > beta0(counter)
n = n + beta1(counter); 
else m = m + beta0(counter);
end
end		  
F6 = 0.25*(m+n+sqrt((m-n)^2 +4*m*n*(sqrt(alpha0)'*sqrt(alpha1))^2));
if F6 < Best_Bias
iter_after_F6 = iter_after_F6 + 1;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

F7 = ...
0.25*(alpha0(1)+alpha1(1)+sqrt((alpha0(1)-alpha1(1))^2 +4*alpha0(1)*alpha1(1)*(sqrt(beta0)'*sqrt(beta1))^2)) + ...
0.25*(alpha0(2)+alpha1(2)+sqrt((alpha0(2)-alpha1(2))^2 +4*alpha0(2)*alpha1(2)*(sqrt(beta0)'*sqrt(beta1))^2)) + ...
0.25*(alpha0(3)+alpha1(3)+sqrt((alpha0(3)-alpha1(3))^2 +4*alpha0(3)*alpha1(3)*(sqrt(beta0)'*sqrt(beta1))^2)) + ...
0.25*(alpha0(4)+alpha1(4)+sqrt((alpha0(4)-alpha1(4))^2 +4*alpha0(4)*alpha1(4)*(sqrt(beta0)'*sqrt(beta1))^2)) + ...
0.25*(alpha0(5)+alpha1(5)+sqrt((alpha0(5)-alpha1(5))^2 +4*alpha0(5)*alpha1(5)*(sqrt(beta0)'*sqrt(beta1))^2));								
if F7 < Best_Bias
iter_after_F7 = iter_after_F7 + 1;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

me1 = 0.5*(alpha0(1)+alpha1(1)+sqrt((alpha0(1)-alpha1(1))^2 +4*alpha0(1)*alpha1(1)*(sqrt(beta0)'*sqrt(beta1))^2));
me2 = 0.5*(alpha0(2)+alpha1(2)+sqrt((alpha0(2)-alpha1(2))^2 +4*alpha0(2)*alpha1(2)*(sqrt(beta0)'*sqrt(beta1))^2));
me3 = 0.5*(alpha0(3)+alpha1(3)+sqrt((alpha0(3)-alpha1(3))^2 +4*alpha0(3)*alpha1(3)*(sqrt(beta0)'*sqrt(beta1))^2));
me4 = 0.5*(alpha0(4)+alpha1(4)+sqrt((alpha0(4)-alpha1(4))^2 +4*alpha0(4)*alpha1(4)*(sqrt(beta0)'*sqrt(beta1))^2));
me5 = 0.5*(alpha0(5)+alpha1(5)+sqrt((alpha0(5)-alpha1(5))^2 +4*alpha0(5)*alpha1(5)*(sqrt(beta0)'*sqrt(beta1))^2));

A1 = alpha0(1)*sqrt(beta0)*sqrt(beta0)' + alpha1(1)*sqrt(beta1)*sqrt(beta1)';
A2 = alpha0(2)*sqrt(beta0)*sqrt(beta0)' + alpha1(2)*sqrt(beta1)*sqrt(beta1)';
A3 = alpha0(3)*sqrt(beta0)*sqrt(beta0)' + alpha1(3)*sqrt(beta1)*sqrt(beta1)';
A4 = alpha0(4)*sqrt(beta0)*sqrt(beta0)' + alpha1(4)*sqrt(beta1)*sqrt(beta1)';
A5 = alpha0(5)*sqrt(beta0)*sqrt(beta0)' + alpha1(5)*sqrt(beta1)*sqrt(beta1)';

n = null(A1 - me1*eye(5));
S = size(n);
if S(2) > 0
  v1 = n(:,1);
  else
  [v1, e1] = eigs(A1,1);
end
v1 = abs(v1); v1 = v1/norm(v1,2); v1 = v1.*v1;

n = null(A2 - me2*eye(5));
S = size(n);
if S(2) > 0
  v2 = n(:,1);
  else
  [v2, e1] = eigs(A2,1);
end
v2 = abs(v2); v2 = v2/norm(v2,2); v2 = v2.*v2;

n = null(A3 - me3*eye(5));
S = size(n);
if S(2) > 0
  v3 = n(:,1);
  else
  [v3, e1] = eigs(A3,1);
end
v3 = abs(v3); v3 = v3/norm(v3,2); v3 = v3.*v3;

n = null(A4 - me4*eye(5));
S = size(n);
if S(2) > 0
  v4 = n(:,1);
  else
  [v4, e1] = eigs(A4,1);
end
v4 = abs(v4); v4 = v4/norm(v4,2); v4 = v4.*v4;

n = null(A5 - me5*eye(5));
S = size(n);
if S(2) > 0
  v5 = n(:,1);
  else
  [v5, e1] = eigs(A5,1);
end
v5 = abs(v5); v5 = v5/norm(v5,2); v5 = v5.*v5;

p0 = alpha0(1)*v1 + alpha0(2)*v2 + alpha0(3)*v3 + alpha0(4)*v4 + alpha0(5)*v5;               
p1 = alpha1(1)*v1 + alpha1(2)*v2 + alpha1(3)*v3 + alpha1(4)*v4 + alpha1(5)*v5;

F8 = 0.5*(sqrt(p0)'*sqrt(beta0))^2 + ...
     0.5*(sqrt(p1)'*sqrt(beta1))^2;
if F8 < Best_Bias
iter_after_F8 = iter_after_F8 + 1;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Output
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

     best_alpha0 = alpha0
     best_alpha1 = alpha1
     best_beta0 = beta0
     best_beta1 = beta1
     error('Filter not good enough')
  
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Endifs for Filter (Known Strategies)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

end % F8
end % F7
end % F6
end % F5
end % F4
end % F3
end % F2
end % F1

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Endifs for Protocol Generation
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

end
end
end
end
end
end
end
end
end
end % alpha's

end
end
end
end
end
end
end
end
end
end % beta's

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Output
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

Total_Protocols = 1; %(factorial(9+p-1)/(factorial(p)*factorial(8)))^4;				
After_Symmetry = iter_beta0beta1_count^2;
iTP = 1;
iAS = 1;
i1 = iter_after_F1 > 1/2;
i2 = iter_after_F2 > 1/2;
i3 = iter_after_F3 > 1/2;
i4 = iter_after_F4 > 1/2;
i5 = iter_after_F5 > 1/2;
i6 = iter_after_F6 > 1/2;
i7 = iter_after_F7 > 1/2;
i8 = iter_after_F8 > 1/2;
iter_after_SDPA;
iter_after_SDPB0;
iter_after_SDPB1;

Col = [vector(p), Total_Protocols, After_Symmetry, iter_after_F1, iter_after_F2, ...
iter_after_F3, iter_after_F4, iter_after_F5, ...
	   iter_after_F6, iter_after_F7, iter_after_F8]';

indic = [1, iTP, iAS, i1, i2, i3, i4, i5, i6, i7, i8]'; % indicates whether ANYTHING passed this filter

prec;

Output = [Output; Col, indic, p*ones(11,1)]
Output2 = [Output2, Col]

end 

Output;

total_time = clock - time_start

diary off

return