%SPEEDCOMPAR1 -- Companion script to C:/work/PLPaper2/simul/SpeedCompar.txt
%
% See also SPEEDCOMPAR, PLM_HEBB1, PLM_STATS, PLM_STATS2.

% Original coding by Alexander Petrov, UC Irvine.
% $Revision: 1.0 $  $Date: 2005/06/10 18:11 $

% The following variables must exist in the global workspace.
% See C:/work/PLPaper2/simul/SpeedCompar.txt for details.
%  Name            Size         Bytes  Class
%-----------------------------------------------------------
%  Mpar_decn       1x1           2012  struct array
%  Mpar_crit       1x1           2012  struct array
%  N_runs          1x1              8  double array
%  PLM_CACHE      12x1       16803792  struct array (global)
%  sched1         32x1           9088  cell array
%  sched2         32x1           9088  cell array
%  sched36_1      36x1          10224  cell array
%  sched36_2      36x1          10224  cell array
%-----------------------------------------------------------

% After making and summarizing 8*N_runs of PLM_HEBB1, the
% script creates the following variables in the global workspace:
%  Name            Size         Bytes  Class
%-----------------------------------------------------------
%  sst32fdbk_Mdecn       1x1           9812  struct array
%  sst32fdbk_Mcrit       1x1           9812  struct array
%  sst32nofk_Mdecn       1x1           9812  struct array
%  sst32nofk_Mcrit       1x1           9812  struct array
%  sst36fdbk_Mdecn       1x1           9540  struct array
%  sst36fdbk_Mcrit       1x1           9540  struct array
%  sst36nofk_Mdecn       1x1           9540  struct array
%  sst36nofk_Mcrit       1x1           9540  struct array
%-----------------------------------------------------------

% Takes about 6 minutes to execute on Dell Optiplex when N_runs=10.
% If saved, the 8 SST structures take about 73 KB disc space.

fprintf('\n%s ',datestr(now));

%%% First, generate sequences according to the 32-block PLExp1 protocol, counterbalanced 50/50.
d1=zeros(9600,5,N_runs/2);d2=d1;
for k=1:N_runs/2 
   d=block_from_cache(sched1,PLM_CACHE); 
   d1(:,:,k)=cat(1,d{:});
end
for k=1:N_runs/2 
   d=block_from_cache(sched2,PLM_CACHE);
   d2(:,:,k)=cat(1,d{:});
end
stim32=cat(3,d1,d2);      % [9600,5,N_runs]
clear d1 d2 d
fprintf('#') ;

% Run the model WITH feedback, under Mpar_decn
o=zeros(9600,5,N_runs);
for k=1:N_runs  
   s=stim32(:,:,k);
   fdbk=s(:,1);       % <-- fdbk=correct
   [o(:,:,k),Wh]=PLM_Hebb1([s,fdbk],Mpar_decn);
end
st=PLM_stats(stim32,o(:,1,:),o(:,2,:),o(:,3,:));
sst32fdbk_Mdecn=PLM_summary(st); 
clear o st s fdbk
fprintf('.') ;

% Run the model on the same stimuli WITHOUT feedback, under Mpar_decn
o=zeros(9600,5,N_runs);
fdbk=zeros(9600,1);    % <-- fdbk=0
for k=1:N_runs  
   s=stim32(:,:,k);
   [o(:,:,k),Wh]=PLM_Hebb1([s,fdbk],Mpar_decn);
end
st=PLM_stats(stim32,o(:,1,:),o(:,2,:),o(:,3,:));
sst32nofk_Mdecn=PLM_summary(st); 
clear o st s fdbk
fprintf(',') ;

% Run the model on the same stimuli WITH feedback, under Mpar_crit
o=zeros(9600,5,N_runs);
for k=1:N_runs  
   s=stim32(:,:,k);
   fdbk=s(:,1);       % <-- fdbk=correct
   [o(:,:,k),Wh]=PLM_Hebb1([s,fdbk],Mpar_crit);
end
st=PLM_stats(stim32,o(:,1,:),o(:,2,:),o(:,3,:));
sst32fdbk_Mcrit=PLM_summary(st); 
clear o st s fdbk
fprintf('.') ;

% Run the model on the same stimuli WITHOUT feedback, under Mpar_crit
o=zeros(9600,5,N_runs);
fdbk=zeros(9600,1);    % <-- fdbk=0
for k=1:N_runs  
   s=stim32(:,:,k);
   [o(:,:,k),Wh]=PLM_Hebb1([s,fdbk],Mpar_crit);
end
st=PLM_stats(stim32,o(:,1,:),o(:,2,:),o(:,3,:));
sst32nofk_Mcrit=PLM_summary(st); 
clear o st s fdbk
fprintf(',') ;

clear stim32 Wh

%%% Second, generate sequences according to the 36-block PLExp2 protocol, counterbalanced 50/50.
d1=zeros(10800,5,N_runs/2);
d2=d1;
for k=1:N_runs/2 
   d=block_from_cache(sched36_1,PLM_CACHE);
   d1(:,:,k)=cat(1,d{:});
end
for k=1:N_runs/2 
   d=block_from_cache(sched36_2,PLM_CACHE);
   d2(:,:,k)=cat(1,d{:});
end
stim36=cat(3,d1,d2);      % [10800,5,N_runs]
clear d1 d2 d
fprintf(' *') ;

curr_dir = pwd ;
cd('..\..\PLExp2\modelfits');     % access to PLM_stats2.m

% Run the model WITH feedback, under Mpar_decn
o=zeros(10800,5,N_runs);
for k=1:N_runs  
   s=stim36(:,:,k);
   fdbk=s(:,1);
   [o(:,:,k),Wh]=PLM_Hebb1([s,fdbk],Mpar_decn);
end
st=PLM_stats2(stim36,o(:,1,:),o(:,2,:),o(:,3,:));
sst36fdbk_Mdecn=PLM_summary(st);
clear o st s fdbk
fprintf('.') ;

% Run the model on the same stimuli WITHOUT feedback, under Mpar_decn
o=zeros(10800,5,N_runs);
fdbk=zeros(10800,1);
for k=1:N_runs  
   s=stim36(:,:,k);
   [o(:,:,k),Wh]=PLM_Hebb1([s,fdbk],Mpar_decn);
end
st=PLM_stats2(stim36,o(:,1,:),o(:,2,:),o(:,3,:));
sst36nofk_Mdecn=PLM_summary(st);
clear o st s fdbk
fprintf(',') ;

% Run the model on the same stimuli WITH feedback, under Mpar_crit
o=zeros(10800,5,N_runs);
for k=1:N_runs  
   s=stim36(:,:,k);
   fdbk=s(:,1);
   [o(:,:,k),Wh]=PLM_Hebb1([s,fdbk],Mpar_crit);
end
st=PLM_stats2(stim36,o(:,1,:),o(:,2,:),o(:,3,:));
sst36fdbk_Mcrit=PLM_summary(st); 
clear o st s fdbk
fprintf('.') ;

% Run the model on the same stimuli WITHOUT feedback, under Mpar_crit
o=zeros(10800,5,N_runs);
fdbk=zeros(10800,1);
for k=1:N_runs  
   s=stim36(:,:,k);
   [o(:,:,k),Wh]=PLM_Hebb1([s,fdbk],Mpar_crit);
end
st=PLM_stats2(stim36,o(:,1,:),o(:,2,:),o(:,3,:));
sst36nofk_Mcrit=PLM_summary(st);
clear o st s fdbk
fprintf(', ') ;

cd(curr_dir) ;
clear stim36 Wh k curr_dir

%%%%% End of script SPEEDCOMPAR1.M