-
Notifications
You must be signed in to change notification settings - Fork 0
/
FindEqUnitTests.m
executable file
·200 lines (154 loc) · 4.83 KB
/
FindEqUnitTests.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
% EQ validation tests: make sure functions perform correctly
addpath(genpath('./'));
% Network and algorithm parameters
networkType = 'geometric'; % {'ring','geometric','dir-geometric'}
n = 10;
numStrategies = 2;
numGroups = 2;
maxGroupSize = 4;
seed = 1;
%% ----------Construct a random example to use for testing-------------- %%
switch networkType
case 'ring'
[Adj,Vxy,Edges] = CreateGraph('type','torus','nrows',n,'ncols',1);
case 'geometric'
[Adj,Vxy,Edges] = CreateGraph('type','geometric','n',n,...
'connectRange',0.4,'connected',true,'seed',seed);
case 'dir-geometric'
[Adj,Vxy,Edges] = CreateGraph('type','dir-geometric','n',n,...
'connectRange',0.6,'connected',true,'seed',seed);
end
switch numStrategies
case 2
allS = [1;2];
payoffMatrix = [0 3;1 0];
% payoffMatrix = [3 0;0 1];
updateRules = repmat({'BestResponse'},n,1);
energyFunction = @IsingEnergy;
case 3
allS = [1;2;3];
payoffMatrix = [0 3 3;1 0 0;0 0 0];
updateRules = repmat({'BestResponseControl'},n,1);
energyFunction = @IsingEnergy;
end
n = size(Adj,1); m = size(Edges,1);
V = (1:n)';
deg = sum(Adj,2);
nodePayoffs = repmat(payoffMatrix,[1 1 n]);
% Construct an asynchronous network game from this network and game dynamics
ng = netgame('Adj',Adj,'Vxy',Vxy,'Edges',Edges,'updateRules',updateRules,...
'payoffs',nodePayoffs,'networkSize',n,'synchronous',false,'seed',seed,...
'payoffSource','nodes','showGames',false,'initType','random','pauseGames',...
false,'saveFigures',false,'energyFunction',energyFunction);
ng.ns = length(allS);
% Some of these will be brute force tests that involve checking all states
allStates = LogMat(n,allS);
numStates = size(allStates,1);
results = false(5,1);
%% ----------------------------FindLocalEQ------------------------------ %%
LEQ = FindLocalEq(ng);
% Construct partition matrix as if each node is it's own partition group
[Pi,PiPart] = BasePartition(Adj,V);
good = true;
for i = 1:numStates
display(['Checking state ' num2str(i) ' of ' num2str(numStates)]);
x0 = allStates(i,:);
for j = 1:n
neighbors = find(PiPart(:,j));
xj = zeros(1,n);
xj(neighbors) = x0(neighbors);
ng.x = x0';
Update(ng,j);
if ng.x(j) == x0(j)
if ~ismember(xj(neighbors),LEQ{j},'rows')
good = false;
end
else
if ismember(xj(neighbors),LEQ{j},'rows')
good = false;
end
end
end
end
if good
disp('FindLocalEQ Test: passed');
else
disp('FindLocalEQ Test: failed');
end
results(1) = good;
%% ----------------------------Local2Global----------------------------- %%
GEQ = GroupMerge(ng.LEQ,(1:n)',PiPart);
good = true;
for i = 1:numStates
display(['Checking state ' num2str(i) ' of ' num2str(numStates)]);
x0 = allStates(i,:);
ng.x0 = x0';
xf = Play(ng);
if all(x0' == xf)
if ~ismember(x0,GEQ,'rows')
good = false;
end
else
if ismember(x0,GEQ,'rows')
good = false;
end
end
end
if good
disp('Local2Global Test: passed');
else
disp('Local2Global Test: failed');
end
results(2) = good;
%% ----------------------------JigsawGroups----------------------------- %%
InitPlots(ng);
GD = JigsawGroups(ng,numGroups,maxGroupSize,(1:ng.n)',true,true);
Vpart = find(any(GD.PiOut | GD.PiIn,2));
GEQcheck = GEQ(:,Vpart);
goodNumEq = GD.numEq == size(GEQ,1);
good = all(ismember(GD.PREQ,GEQcheck,'rows')) && ...
all(ismember(GEQcheck,GD.PREQ,'rows')) && goodNumEq;
if good
disp('JigsawGroups Test: passed');
else
disp('JigsawGroups Test: failed');
end
results(3) = good;
%% ------------------------------Energies------------------------------- %%
% Compute all energies
allPhis = ComputeEnergy(ng,Adj,GEQ,(1:n)',(1:n)');
allPhiCheck = unique(allPhis);
% Computed energies
allPhi = GD.uniqPhiPREQ;
good = all(ismember(allPhi,allPhiCheck)) && all(ismember(allPhiCheck,allPhi));
if good
disp('Energy Test: passed');
else
disp('Energy Test: failed');
end
results(4) = good;
%% -----------------------------ExpandEq-------------------------------- %%
% Compute all energies
allPhi = ComputeEnergy(ng,Adj,GEQ,(1:n)',(1:n)');
numUniqPhi = length(GD.uniqPhiPREQ);
good = false(numUniqPhi,1);
for i = 1:numUniqPhi
targetPhi = GD.uniqPhiPREQ(i);
eqPhi = ExpandEq(GD,targetPhi);
eqPhiCheck = GEQ(allPhi == targetPhi,:);
good(i) = all(ismember(eqPhi,eqPhiCheck,'rows')) && ...
all(ismember(eqPhiCheck,eqPhi,'rows'));
end
if all(good)
disp('ExpandEq Test: passed');
else
disp('ExpandEq Test: failed');
end
results(5) = all(good);
%% --------------------------------All---------------------------------- %%
allGood = all(results);
if allGood
disp('All tests passed');
else
disp('One or more tests failed');
end