-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
166 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
%% Iterates the Prisoner Dilemma between two players | ||
% | ||
% Returns four arguments: the two lists of strategies (0=defect, 1=coop) | ||
% and the two scores. | ||
|
||
function [X,Y,Xscore,Yscore]=iteratedpd(r,p1strat,p2strat,payoff) | ||
|
||
% Init | ||
mdefect=payoff(1,1); | ||
mcoop=payoff(2,2); | ||
suckerlose=payoff(1,2); | ||
suckerwin=payoff(2,1); | ||
|
||
X=[]; | ||
Y=[]; | ||
Xscore=0; | ||
Yscore=0; | ||
|
||
for i=1:r | ||
|
||
% Determine the next move based on the past history | ||
newX=play(X,Y,p1strat); | ||
newY=play(Y,X,p2strat); | ||
|
||
% Add the last move | ||
X=[X,newX]; | ||
Y=[Y,newY]; | ||
|
||
% Update the score | ||
if (newX==0) %P1 defect | ||
if(newY==0) | ||
Xscore=Xscore+mdefect; | ||
Yscore=Yscore+mdefect; | ||
else | ||
Xscore=Xscore+suckerwin; | ||
Yscore=Yscore+suckerlose; | ||
end | ||
else %P1 cooperate | ||
if(newY==0) | ||
Xscore=Xscore+suckerlose; | ||
Yscore=Yscore+suckerwin; | ||
else | ||
Xscore=Xscore+mcoop; | ||
Yscore=Yscore+mcoop; | ||
end | ||
end | ||
end | ||
end | ||
|
||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
%% Play a round of Prisoner Dilemma | ||
% Evaluate previous history and execute a strategy. | ||
% | ||
% Returns 1 to cooperate or 0 to defect. | ||
% | ||
% Strategies implemented: | ||
% | ||
% * 1 = always defect | ||
% * 2 = always cooperate | ||
% * 3 = Tit-for-tat | ||
% * 4 = GRIM (defect forever if opponent ever defects) | ||
% * Any other input implements RANDOM- 50% chance of either. | ||
|
||
function x=play(player,opp,strategy) | ||
|
||
if (strategy == 1) %always defect | ||
x=0; | ||
|
||
elseif (strategy == 2) %always cooperate | ||
x=1; | ||
|
||
elseif(strategy==3) %titfortat | ||
if(isempty(opp)) | ||
x=1; % Be nice first | ||
else | ||
x=opp(length(opp)); % Repeat last opponent's move | ||
end | ||
|
||
elseif(strategy==4) %GRIM | ||
if(isempty(opp)) % Be nice first | ||
x=1; | ||
else | ||
if(sum(opp)<length(opp)) % After one defect, always defect | ||
x=0; | ||
else | ||
x=1; | ||
end | ||
end | ||
|
||
else % random | ||
s=rand(1,1); | ||
|
||
if (strategy==5) | ||
threshold = 0.5; | ||
else | ||
threshold = 0.9; | ||
end | ||
|
||
if(s<threshold) | ||
x=0; | ||
else | ||
x=1; | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
%% Runs an IPD (Iterated Prisoner Dilemma) tournament | ||
% Players with different strategies iteratedtly face each others and decide | ||
% to cooperate or defect. | ||
|
||
% Strategies are based on the previous history of the game | ||
% The right number of random players can enrich the dynamics of the game | ||
|
||
fprintf('\nStart the tournament...\n'); | ||
R=200; % number of rounds | ||
|
||
%% PLAYERS & STRATEGIES | ||
% Set up 4 players and assign them different strategies | ||
% 1 always defect | ||
% 2 always cooperate | ||
% 3 Tit-for-Tat | ||
% 4 GRIM | ||
% 5 Random 0.5 defect | ||
% 6 Random 0.9 defect | ||
Q=[3,4,5,6] | ||
n=length(Q); | ||
|
||
%% MATRIX PAYOFF | ||
% Set up the payoff matrix according to Axelrod (1984) The Evolution of | ||
% Cooperation. | ||
% Notice: 3+3 > 5+0 | ||
|
||
bothDefect = 1; | ||
bothCooperate = 3; | ||
temptationToDefect = 5; | ||
suckerPayoff = 0; | ||
|
||
payoffMatrix = [bothDefect,suckerPayoff; | ||
temptationToDefect,bothCooperate] | ||
|
||
%% START THE TOURNAMENT | ||
% | ||
Z=zeros(n,n); % tournament table | ||
|
||
for i=1:n | ||
for j=1:n | ||
[hP1,hP2,scoreP1,scoreP2]=iteratedpd(R,Q(i),Q(j),payoffMatrix); | ||
Z(i,j)=scoreP1; | ||
end | ||
end | ||
|
||
|
||
%% PRINT RESULTS | ||
% | ||
display(Z); | ||
scores = zeros(n,1); | ||
for i=1:n | ||
scores(i)=sum(Z(i,1:n)); | ||
end | ||
display(scores); | ||
|
||
imagesc(Z); | ||
|
||
% Who wins? | ||
% Why? | ||
|