Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please modify the following matlab code such that a game consist of 7 (n) rounds with all the neighbors. If an agent neighbors do not

Please modify the following matlab code such that a game consist of 7 (n) rounds with all the neighbors. If an agent neighbors do not cooperate the agent should change strategy in the n th round. Plot the subpopulations 1 2 3 4 5 6 7 for each round

%% ------------------------------------------------------------

%PRISONERS DILEMMA

clc; clear all; close all;

% ------------------------------------------------------------

% GENERAL HOUSEKEEPING

% Allocation to produce graphic output.

% There are four cases:

% 1: cooperators that remain cooperating (C|C)

% 2: defectors that become cooperators (C|D)

% 3: cooperators that become defectors (D|C)

% 4: defectors that remain defectors (D|D)

%

% Past move

% 1 2

% New move 1 C|C C|D 1 2

% 2 D|C D|D 3 4

%

% This is code according to Col matrix.

% 1 -> C|C: blue

% 2 -> C|D: green

% 3 -> D|D: yellow

% 4 -> D|D: red

% Index

nRoundGame = 6;

Col = [1 2

3 4];

% Each row defines: red, green, yellow and blue

map = [ 0 0 1

0 1 0

1 1 0

1 0 0];

fid = 1; %Output directed to screen (=1) or elsewhere...

sep ='-----------';

% Define the von Neumann neighborhood, i.e. the 4 nearest neighbors

neigh = [1,0; 0,-1; -1,0; 0,1];

% Maximum number of iterations

max_iter = 100;

T = 1.4; % temptation [1,2]

P = 0; % punishement [0,1]

% Lattice size

N = 42;

epsilon = 1/N^2; % Mutation rate

% Number of cells

n = N*N;

% Proportion of defectors

p = 0.10;

% Payoff matrix

Payoff = zeros(2,2); Payoff(1,1) = 1; Payoff(2,1) = T; Payoff(2,2) = P;

% Allocations

pC = zeros(max_iter,1);

pC2 = zeros(max_iter,1);

pC3 = zeros(max_iter,1);

pC4 = zeros(max_iter,1);

Payment = zeros(N,N);

NE = zeros(N,N);

Tab = zeros(N,N);

Tab_cube = zeros(N,N,max_iter);

Tab_cube2 = zeros(N,N,max_iter);

saveState = zeros(N,N);

% ------------------------------------------------------------

% INITIAL CONDITIONS

E = ones(N,N);

% % Random

% A = rand(N,N);

% % Changing E according to the initial proportion of defectors

% I = find(A < p);

% E(I) = 2;

% Fixed

E(30:30,30:30) = 2;

% load init_E;

% E=A;

% % ------------------------------------------------------------

% GLOBAL LOOP

fprintf(1,'*** Running Prisoners Dilemma. Please wait... *** ');

fprintf(fid,' Iteration ');

fprintf(fid,'%s ',sep);

for iter=1:max_iter

fprintf(fid,' %4d ',[iter]);

% Setting payments when each player plays with their 4 neighbours,

% including itself

for i=1:N

for j=1:N

pa = 0;

% Play the game nRoundGame times with all the neighbors

for k=1:length(neigh)

i2 = i+neigh(k, 1);

j2 = j+neigh(k, 2);

% Check that the cell is within the grid boundaries (i.e. also accounts for

% periodic boundary conditions)

if ( i2>=1 && j2>=1 && i2<=N && j2<=N )

% Setting payment according to the selected strategy

pa = pa + Payoff(E(i,j), E(i2,j2));

end

end

Payment(i,j) = pa;

end

end

% Evaluation of the environment and possible change of strategy

for i=1:N

for j=1:N

pay = Payment(i,j);

NE(i,j) = E(i,j);

for k=1:length(neigh)

i2 = i+neigh(k, 1);

j2 = j+neigh(k, 2);

% Check that the cell is within the grid boundaries (i.e. also accounts for

% periodic boundary conditions)

if ( i2>=1 && j2>=1 && i2<=N && j2<=N )

% If the neighbour performed better, the i,j player

% it has stopped to cooperate. At this point the agent

% switches strategy from cooperate to defect

if (Payment(i2,j2) > pay)

pay = Payment(i2,j2);

NE(i,j) = E(i2,j2);

end

end

end

end

end

% Changes of strategy becomes effective here.

for i=1:N

for j=1:N

Tab(i,j) = Col(NE(i,j),E(i,j));

% Change of strategy (updating matrix E)

E(i,j) = NE(i,j);

end

end

Tab_cube(:,:,iter) = Tab;

Tab_cube2(:,:,iter) = saveState;

% Proportion of ccoperators

pC(iter) = length(find(Tab==1)) / n;

pC2(iter) = length(find(Tab==2 )) / n;

pC3(iter) = length(find(Tab==3 )) / n;

pC4(iter) = length(find(Tab==4 )) / n;

end

%

fprintf(fid,'%s ',sep);

fprintf(1,'*** End *** ');

% % ------------------------------------------------------------

% % Graphic output

for iter=1:max_iter

pcolor(Tab_cube(:,:,iter));

colormap(map)

title(['Iteration --> ',num2str(iter)])

% Recording for a movie

F(iter) = getframe;

% pause

end

% movie(F)

% Proportion of cooperatos: evolution across iterations

figure;

plot(1:max_iter,pC,'-rs');

hold on

plot(1:max_iter,pC2,'-rs');

plot(1:max_iter,pC3,'-rs');

plot(1:max_iter,pC4,'-rs');

xlabel('Time')

ylabel('Population Fraction')

title('PRISONERS DILEMMA');

grid on;

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

The Database Experts Guide To Database 2

Authors: Bruce L. Larson

1st Edition

0070232679, 978-0070232679

More Books

Students also viewed these Databases questions