%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Example to illustrate reaction curves, infinite games, and 
% Nash equilibria.
%
% Author: K. Passino
% Version: 2/5/02
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

clear all


% Define payoff (cost) functions for each player

theta1=-4:0.05:4; % Ok, while we think of it as an infinite game computationally
	              % we of course only study a finite number of points.
m=length(theta1);
theta2=-5:0.05:5;
n=length(theta2);

% A set of cost functions, J1 for player 1 and J2 for player 2
for ii=1:length(theta1)
	for jj=1:length(theta2)
		J1(ii,jj)=-1*(exp( (-(theta1(ii)-2)^2)/8  + ((-(theta2(jj)-4)^2)/2))); 
		J2(ii,jj)=-1*(exp( (-(theta1(ii)-1)^2)/1  + ((-(theta2(jj)+1)^2)/6)));
	end
end


% Next, compute the reaction curves for each player and will plot on top of J1 and J2

for j=1:length(theta2)
	[temp,t1]=min(J1(:,j)); % Find the min point on J1 with theta2 fixed, for all theta2
	R1(j)=theta1(t1); % Compute the theta1 value that is the best reaction to each theta2
end

for i=1:length(theta1)
	[temp,t2]=min(J2(i,:)); % Find the min point on J2 with theta1 fixed
	R2(i)=theta2(t2); % Compute the theta2 value that is the best reaction to each theta1
end


figure(1)
clf
subplot(121)
contour(theta2,theta1,J1,10)
hold on
contour(theta2,theta1,J2,10)
hold on
plot(theta2,R1,'k-')
hold on
plot(R2,theta1,'k--')
xlabel('\theta^2')
ylabel('\theta^1')
title('(a) J_1, J_2, reaction curves R_1 (-) and R_2 (--)')
hold off

% Compute the Nash equilibria (with the view that it is a bimatrix game):

flag=0; % Flag for saying if there is no Nash equilibria

for i=1:m	
    for j=1:n	
		if J1(i,j)<=min(J1(:,j)) & J2(i,j)<=min(J2(i,:)), % Conduct two inequality tests
			display('Nash equilibrium and outcome:') % If satisfied, then diplay solution
			i
			theta1(i)
			j
			theta2(j)
			J1(i,j) 
			J2(i,j)
			flag=1; % Indicates that there was one Nash equilibrium (or more)
		end
    end
end

if flag==0
	display('There were no Nash equilibria')
end



%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Another possible set of cost functions:
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


for ii=1:length(theta1)
	for jj=1:length(theta2)
		J1(ii,jj)=-2*(exp( (-(theta1(ii)-2)^2)/5 +(-4*(theta1(ii)*theta2(jj))/20) + ((-(theta2(jj)-3)^2)/2))); 
		J2(ii,jj)=-1*(exp( (-(theta1(ii)-1)^2)/4 + (5*(theta1(ii)*theta2(jj))/10) + ((-(theta2(jj)+1)^2)/2)));
	end
end

% Next, compute the reaction curves for each player and will plot on top of J1 and J2

for j=1:length(theta2)
	[temp,t1]=min(J1(:,j)); % Find the min point on J1 with theta2 fixed, for all theta2
	R1(j)=theta1(t1); % Compute the theta1 value that is the best reaction to each theta2
end

for i=1:length(theta1)
	[temp,t2]=min(J2(i,:)); % Find the min point on J2 with theta1 fixed
	R2(i)=theta2(t2); % Compute the theta2 value that is the best reaction to each theta1
end



figure(1)
subplot(122)
contour(theta2,theta1,J1,10)
hold on
contour(theta2,theta1,J2,10)
hold on
plot(theta2,R1,'k-')
hold on
plot(R2,theta1,'k--')
xlabel('\theta^2')
ylabel('\theta^1')
title('(b) J_1, J_2, reaction curves R_1 (-) and R_2 (--)')
hold off



% Compute the Nash equilibria (with the view that it is a bimatrix game):

flag=0; % Flag for saying if there is no Nash equilibria

for i=1:m	
    for j=1:n	
		if J1(i,j)<=min(J1(:,j)) & J2(i,j)<=min(J2(i,:)), % Conduct two inequality tests
			display('Nash equilibrium and outcome:') % If satisfied, then diplay solution
			i
			theta1(i)
			j
			theta2(j)
			J1(i,j) 
			J2(i,j)
			flag=1; % Indicates that there was one Nash equilibrium (or more)
		end
    end
end

if flag==0
	display('There were no Nash equilibria')
end



figure(2)
clf
contour(theta2,theta1,J1,10)
hold on
contour(theta2,theta1,J2,10)
hold on
plot(theta2,R1,'k-')
hold on
plot(R2,theta1,'k--')
xlabel('\theta^2')
ylabel('\theta^1')
title('J_1, J_2, R_1 (-), R_2 (--), and iteration trajectory')
hold off

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

