• Sign Up
• Login
• API
• FAQ
• Tools
• Archive
SHARE
TWEET # Untitled a guest Apr 22nd, 2019 76 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. for seed = 1:100
2.     rng(seed, 'twister');      % seed
3.
4.     % T = temptation--------5
5.     T = 5;
6.     % R = reward------------3
7.     R = 3;
8.     % P = punishment--------1
9.     P = 1;
10.     % S = sucker------------0
11.     S = 0;
12.
13.     max = zeros(100,1);
14.
15.     % A = (initial, defect, cooperate, score)
16.     rand_result = randi([0, 1], 20, 3);      % 0 == cooperate, 1 == defect
17.     agents = zeros(20,4);
18.     for agentNo = 1:20
19.         agents(agentNo,:) = [rand_result(agentNo,:), 0];
20.     end
21.
22.     % game playing
23.     for generation = 1:100
24.         for agentNo = 1:20
25.
26.             agent = agents(agentNo,:);
27.
28.             for opponentNo = agentNo:20
29.                 if opponentNo ~= agentNo
30.
31.                     opponent = agents(opponentNo,:);
32.
33.                     s_agent = agent(1);
34.                     s_opponent = opponent(1);
35.
36.                     for game = 1:100
37.
38.                         if s_agent == 0 && s_opponent == 0
39.                                 agents(agentNo, 4) = agents(agentNo, 4) + R;
40.                                 agents(opponentNo, 4) = agents(opponentNo, 4) + R;
41.                                 s_agent = agent(3);
42.                                 s_opponent = opponent(3);
43.
44.                         elseif s_agent == 0 && s_opponent == 1
45.                                 agents(agentNo, 4) = agents(agentNo, 4) + S;
46.                                 agents(opponentNo, 4) = agents(opponentNo, 4) + T;
47.                                 s_agent = agent(2);
48.                                 s_opponent = opponent(3);
49.
50.                         elseif s_agent == 1 && s_opponent == 0
51.                                 agents(agentNo, 4) = agents(agentNo, 4) + T;
52.                                 agents(opponentNo, 4) = agents(opponentNo, 4) + S;
53.                                 s_agent = agent(3);
54.                                 s_opponent = opponent(2);
55.
56.                         elseif s_agent == 1 && s_opponent == 1
57.                                 agents(agentNo, 4) = agents(agentNo, 4) + P;
58.                                 agents(opponentNo, 4) = agents(opponentNo, 4) + P;
59.                                 s_agent = agent(2);
60.                                 s_opponent = opponent(2);
61.
62.                         end
63.                     end
64.                 end
65.             end
66.         end
67.
68.         % sort agents by score
69.         agents = sortrows(agents,4,'descend');
70.
71.         % record max of each generation
72.         max(generation) = agents(1,4)/(1900);
73.
74.         % copy best 10 agents and clear scoreboard
75.         agents(1:10, 4) = 0;
76.         agents(11:20,:) = agents(1:10,:);
77.
78.         % mutation
79.         for agentNo = 1:20
80.             for agentS = 1:3
81.                 random_int = randi([1,100]);
82.                 if random_int == 1
83.                     agents(agentNo,agentS) = ~agents(agentNo, agentS);
84.                 end
85.             end
86.         end
87.     end
88.     hold on;
89.     xlabel('generation')
90.     ylabel('average-payoff')
91.     xlim([0 100])
92.     ylim([0 5])
93.     title('Student ID: 20129070')
94.     plot(max)
95. end
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy.

Top