%规则网格下的囚徒困境博弈-9邻居-单次
clear all;
N=200;
T=1.85;
T0=100;
agent=zeros(N,N,'single');
monny=zeros(N,N,T0);
for i=1:N
for j=1:N
if rand<0.9
agent(i,j)=1;
end
end
end
% Define the Moore neighborhood, i.e. the 8 nearest neighbors
neigh = [-1 -1; 0 -1; 1 -1; 1 0; 1 1; 0 1; -1 1; -1 0;0 0];
% Create a new figure
figure;
hold on
bili=zeros(1,T0);
for t=1:T0
t
clf; % Clear figure
imagesc(agent); % Display grid
pause(0.001); % Pause for 0.01 s
%colormap([1 0 0; 0 1 0; 0 0 1]); % Define colors: Red, Green, Blue
%博弈
bili(t)=sum(sum(agent))/N^2;
for i=1:N
for j=1:N
for k=1:9
i2=i+neigh(k,1);
if i2<1
i2=N;
end
if i2>N
i2=1;
end
j2=j+neigh(k,2);
if j2<1
j2=N;
end
if j2>N
j2=1;
end
if agent(i2,j2)==1
if agent(i,j)==0
monny(i,j,t)=monny(i,j,t)+T;
else
monny(i,j,t)=monny(i,j,t)+1;
end
end
end
end
end
%更新
for i=1:N
for j=1:N
a=monny(i,j,t);
for k=1:8
i2=i+neigh(k,1);
if i2<1
i2=N;
end
if i2>N
i2=1;
end
j2=j+neigh(k,2);
if j2<1
j2=N;
end
if j2>N
j2=1;
end
if monny(i2,j2,t)>a
a=monny(i2,j2,t);
agent(i,j)=agent(i2,j2);
end
end
end
end
end
figure;
plot(bili);
a=mean(bili(80:T0))