%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Simulator for predatory/prey environment for
% SPSA for Attentional Strategy Design
%
% By: Kevin Passino
% Version: 4/28/01
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [J,e,predpreyfocusedon,T,avgT,maxT]=attentionenv(w);

% Define global variables from associated program

global Tmax Tsample Nsteps N
global f deltai
%global f deltaperturb deltai
global sched a deltas

%-----------------------------------
% Start simulation loop:
%-----------------------------------

% First, initialize variables:

e=0*ones(N,Nsteps+1); % Allocate memory for predators/prey signals
T=0*ones(N,Nsteps+1); % T(i,k) will denote the number of steps that predator/prey i has
					  % been ignored as of step k.  
avgT=0*ones(Nsteps+1,1); % avgT(k,1) denotes the average size of T at each step
						 % (average of N values)
maxT=0*ones(Nsteps+1,1); 

fcounter(:,1)=ones(N,1); % Allocate memory for counters 
peakest=0*ones(N,1); % Allocate memory for computation of estimate of peak for sched=4
deltacounter=0; % Used to implement the deltas delay

decisiontime=1; % Signals that it is a time (if =1) that pick a new predators/prey 
				% to focus on (initially pick one to focus on)
predpreyfocusedon=0*ones(Nsteps+1,1);; % Initialize variable that indicates which predators/prey is 
					% currently being focused on (if in a delay period, means
					% the one that it is trying to focus on)
predpreyprocess=0;	% Flag to indicate that switch delay is over
detectedpredprey=0; % Flag that indicates that the predators/prey that is focused on is detected
					% (=0 means not detected, =1 means detected)

for k=1:Nsteps
	
	for i=1:N	 % First generate the appearance signals at step k
		if fcounter(i,1)>=f(i,1); % Check if have waited long enough to appear
								  %(notice here have >= so since due to how the frequency of
								  % occurences of appearances are defined so that f can be  fraction)
			e(i,k+1)=1; % Appear
			fcounter(i,1)=1; % Reset counter
		else
			e(i,k+1)=0; % Do not appear
			fcounter(i,1)=fcounter(i,1)+1; % Increment counter
		end
	end

	% Compute some performance measures that can be used in decision making:
	% (and for plotting later)
	avgT(k,1)=(1/N)*sum(T(:,k));
	maxT(k,1)=max(T(:,k));

	if decisiontime~=1; 
		predpreyfocusedon(k,1)=predpreyfocusedon(k-1,1); % If not a decision time then same focus as last time
	end
	
	% Decide which predators/prey to focus on:
	if decisiontime==1; % Test if it is a decision time
		
		% Policy of choosing the predators/prey that may be most difficult to detect
		if sched==4 
			for j=1:N
			peakest(j,1)=w(j,1)*(a(j,1)/(1-a(j,1)))*(T(j,k)+deltas+deltai(j,1));
			end
		[val,predpreyfocusedon(k,1)]=max(peakest); % Picks the predator/prey expected to have the highest peak
		end
		
		decisiontime=0; % Do not decide again until detect that predators/prey (consider it
						% "detected" when T(i,.) goes to zero)
						
	end

	if deltacounter>=deltas  % First, implement delay due to switching from one predator/prey to another
		predpreyprocess=1;  % Flag to indicate that we are now going to focus on the predator/prey
							% (once deltas is achieved and predpreyprocess=1; the counter does not increment
							% more and so it always lets predpreyprocess=1; to represent that it is continually
							% processing it.... and it resets deltacounter only after it has detected)
	else
		deltacounter=deltacounter+Tsample; % Increment delay since computing T(i,k+1) (note that
										% at every time we are focusing on something so
										% at every time we need to compute this delay)
										% Also, note that below the test for the delay 
										% accounts for incrementing the counter past delta

	end

	if predpreyprocess==1  % If switch delay is over, then can start trying to detect predator/prey
		if e(predpreyfocusedon(k,1),k)==1 % Consider this test for "detection" to only indicate one pulse
			detectedpredprey=1;  % If detect predators/prey set flag
		end
	end  % Note: When for times that predpreyprocess==1, after the first one appearance that sets
	     % detectedpredprey=1; this loop will have no effect since the if..end will either leave
		 % detectedpredprey=1; by not being true or if it is true.

	if predpreyprocess==1 & detectedpredprey==1
		deltaT=((1-a(predpreyfocusedon(k,1),1))/a(predpreyfocusedon(k,1),1))*Tsample;
		if T(predpreyfocusedon(k,1),k)-deltaT>0;  % Must test if will hit zero (to keep nonneg)
			T(predpreyfocusedon(k,1),k+1)=T(predpreyfocusedon(k,1),k)-deltaT; 
		else 
			T(predpreyfocusedon(k,1),k+1)=0; % Set it to zero since it finishes (an approximation
							% that is accurate in the sense that it is conservative)
			deltacounter=0; % Reset the delay counter to prepare for next predator/prey
			decisiontime=1; % Signal a decision time since just detected an predator/prey
			detectedpredprey=0; % Reset for use in detection of next predator/prey
			predpreyprocess=0;  % Reset for next loop
		end
		
	else
		T(predpreyfocusedon(k,1),k+1)=T(predpreyfocusedon(k,1),k)+Tsample; % Increment amount of time 
																		% since last detect
	end

	for i=1:N 
		if i~=predpreyfocusedon(k,1); % If predator/prey that is focused on is not i
			T(i,k+1)=T(i,k)+Tsample; % Update the amounts of time since last focused on each predator/prey
		end
	end

end



% Compute the output of the subroutine

J=mean(avgT);


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% End of program
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
