-
-
Notifications
You must be signed in to change notification settings - Fork 24
/
localint.jl
392 lines (307 loc) · 12.2 KB
/
localint.jl
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
#=
Tools for local interaction model
=#
using SparseArrays
# AbstractRevision #
"""
AbstractRevision
Abstract type representing revision method.
"""
abstract type AbstractRevision end
"""
AsynchronousRevision
Type representing an asynchronous revision.
"""
struct AsynchronousRevision <: AbstractRevision end
"""
SimultaneousRevision
Type representing a simultaneous revision.
"""
struct SimultaneousRevision <: AbstractRevision end
# LocalInteraction
"""
LocalInteraction{N, T, S, A, TR}
Type representing the local interaction model with N players.
# Fields
- `players::NTuple{N,Player{2,T}}` : Tuple of `Player` instances.
- `num_actions::Integer` : The number of actions for players.
- `adj_matrix::Array{S,2}` : Adjacency matrix of the graph in the model.
- `revision<:AbstractRevision` : The way to revise the action profile.
"""
struct LocalInteraction{N,T<:Real,S<:Real,A<:Integer,TR<:AbstractRevision}
players::NTuple{N,Player{2,T}}
num_actions::Int
adj_matrix::SparseMatrixCSC{S,A}
revision::TR
end
"""
LocalInteraction(g, adj_matrix[, revision=SimultaneousRevision()])
Construct a `LocalInteraction` instance.
# Arguments
- `g::NormalFormGame` : The game used in the model.
- `adj_matrix::AbstractMatrix` : Adjacency matrix of the graph in the model.
- `revision::AbstractRevision` : Arguments to specify the revision method;
`SimultaneousRevision()` or `AsynchronousRevision()`.
# Returns
- `::LocalInteraction` : The local interaction model.
"""
function LocalInteraction(g::NormalFormGame{2,T},
adj_matrix::AbstractMatrix{S},
revision::AbstractRevision=SimultaneousRevision()
) where {T<:Real,S<:Real}
if size(adj_matrix, 1) != size(adj_matrix, 2)
throw(ArgumentError("Adjacency matrix must be square"))
end
N = size(adj_matrix, 1)
players = ntuple(i -> g.players[1], N)
num_actions = g.nums_actions[1]
if num_actions != g.nums_actions[2]
throw(ArgumentError("Payoff matrix must be square"))
end
sparse_adj = sparse(adj_matrix)::SparseMatrixCSC{S}
return LocalInteraction(players, num_actions, sparse_adj, revision)
end
"""
LocalInteraction(payoff_matrix,
adj_matrix[, revision=SimultaneousRevision()])
Construct a `LocalInteraction` instance.
# Arguments
- `payoff_matrix::Matrix` : The payoff matrix of the game.
- `adj_matrix::AbstractMatrix` : Adjacency matrix of the graph in the model.
- `revision::AbstractRevision` : Arguments to specify the revision method.
`SimultaneousRevision()` or `AsynchronousRevision`
# Returns
- `::LocalInteraction` : The local interaction model.
"""
function LocalInteraction(payoff_matrix::Matrix{T},
adj_matrix::AbstractMatrix{S},
revision::AbstractRevision=SimultaneousRevision()
) where {T<:Real,S<:Real}
N = size(adj_matrix, 1)
if N != size(adj_matrix, 2)
throw(ArgumentError("Adjacency matrix must be square"))
end
players = ntuple(i -> Player(payoff_matrix), N)
num_actions = size(payoff_matrix, 1)
if num_actions != size(payoff_matrix, 2)
throw(ArgumentError("Payoff matrix must be square"))
end
sparse_adj = sparse(adj_matrix)::SparseMatrixCSC{S}
return LocalInteraction(players, num_actions, sparse_adj, revision)
end
# play!
function play!(li::LocalInteraction{N},
actions::Vector{<:Integer},
player_ind::AbstractVector{<:Integer},
options::BROptions) where N
actions_matrix = sparse(1:N, actions, ones(Int, N), N, li.num_actions)
opponent_action = li.adj_matrix[player_ind,:] * actions_matrix
for (k, i) in enumerate(player_ind)
actions[i] = best_response(li.players[i], Vector(opponent_action[k,:]),
options)
end
return actions
end
play!(li::LocalInteraction, actions::Vector{<:Integer}, player_ind::Integer,
options::BROptions) = play!(li, actions, [player_ind], options)
play!(li::LocalInteraction{N}, actions::Vector{<:Integer},
options::BROptions) where {N} = play!(li, actions, 1:N, options)
function play!(li::LocalInteraction,
actions::Vector{<:Integer},
options::BROptions,
player_ind::Union{AbstractVector{<:Integer},Integer},
num_reps::Integer=1)
for t in 1:num_reps
play!(li, actions, player_ind, options)
end
return actions
end
@doc """
play!(li, actions, options, player_ind[, num_reps=1])
Update an action profile `num_reps` times.
# Arguments
- `li::LocalInteraction` : `LocalInteraction` instance.
- `actions::Vector{<:Integer}` : Action profile in the intial period.
- `options::BROptions` : Options for `best_response` method.
- `player_ind::Union{Vector{<:Integer},Integer} : Integer or vector of integers
representing the index of players to take an action.
- `num_reps::Integer` : The number of iterations.
# Returns
- `actions::Vector{Int}` : Updated `actions`.
"""
# play
function play(li::LocalInteraction{N},
actions::PureActionProfile,
player_ind::Union{AbstractVector{<:Integer},Integer},
options::BROptions=BROptions();
num_reps::Integer=1) where N
actions_vector = [i for i in actions]
actions_vector = play!(li, actions_vector, options, player_ind, num_reps)
new_actions = ntuple(i -> actions_vector[i], N)
return new_actions
end
function play(li::LocalInteraction{N},
actions::PureActionProfile,
options::BROptions=BROptions();
num_reps::Integer=1) where N
play(li, actions, 1:N, options, num_reps=num_reps)
end
@doc """
play(li, actions, player_ind[, options=BROptions(); num_reps=1])
Return the action profile after `num_reps` time iterations.
# Arguments
- `li::LocalInteraction{N}` : `LocalInteraction` instance.
- `actions::PureActionProfile` : Initial actions of each players.
- `player_ind::Union{AbstractVector{<:Integer},Integer}` : Integer or vector of
integers representing the index of players to take an action with asynchronous
revision.
- `options::BROptions` : Options for `best_response` method.
- `num_reps::Integer` : The number of iterations.
# Returns
- `::PureActionProfile` : Actions of each players after iterations.
"""
# time_series!
"""
time_series!(li, out, options, player_ind_seq)
Update the matrix `out` which is used in `time_series` method given player index
sequence.
# Arguments
- `li::LocalInteraction{N}` : `LocalInteraction` instance.
- `out::Matrix{<:Integer}` : Matrix representing a time series of action
profiles.
- `options::BROptions` : Options for `best_response` method.
- `player_ind_seq::Vector{<:Integer}` : Vector representing the index of players
to take an action.
# Returns
- `out::Matrix{<:Integer}` : Updated `out`.
"""
function time_series!(li::LocalInteraction{N},
out::Matrix{<:Integer},
options::BROptions,
player_ind_seq::Vector{<:Integer}) where N
ts_length = size(out, 2)
if ts_length != length(player_ind_seq) + 1
throw(ArgumentError("The length of `ts_length` and
`player_ind_seq` are mismatched"))
end
actions = [out[i,1] for i in 1:N]
for t in 2:ts_length
play!(li, actions, options, player_ind_seq[t-1])
out[:,t] = actions
end
return out
end
"""
time_series!(li, out, options)
Update the matrix `out` which is used in `time_series` method. All players take
their actions simultaneously.
# Arguments
- `li::LocalInteraction{N}` : `LocalInteraction` instance.
- `out::Matrix{<:Integer}` : Matrix representing a time series of action
profiles.
- `options::BROptions` : Options for `best_response` method.
# Returns
- `out::Matrix{<:Integer}` : Updated `out`.
"""
function time_series!(li::LocalInteraction{N},
out::Matrix{<:Integer},
options::BROptions) where N
ts_length = size(out, 2)
actions = [out[i,1] for i in 1:N]
for t in 2:ts_length
play!(li, actions, options)
out[:,t] = actions
end
return out
end
# time_series
function time_series(rng::AbstractRNG,
li::LocalInteraction{N},
ts_length::Integer,
init_actions::PureActionProfile,
player_ind_seq::Vector{<:Integer},
options::BROptions=BROptions()) where N
out = Matrix{Int}(undef, N, ts_length)
for i in 1:N
out[i,1] = init_actions[i]
end
time_series!(li, out, options, player_ind_seq)
end
time_series(li::LocalInteraction, ts_length::Integer,
init_actions::PureActionProfile, player_ind_seq::Vector{<:Integer},
options::BROptions=BROptions()) =
time_series(Random.GLOBAL_RNG, li, ts_length, init_actions, player_ind_seq,
options)
function time_series(rng::AbstractRNG,
li::LocalInteraction{N,T,S,A,TR},
ts_length::Integer,
init_actions::PureActionProfile,
options::BROptions=BROptions()
) where {N,T,S,A,TR<:SimultaneousRevision}
out = Matrix{Int}(undef, N, ts_length)
for i in 1:N
out[i, 1] = init_actions[i]
end
time_series!(li, out, options)
end
time_series(li::LocalInteraction{N,T,S,A,TR},
ts_length::Integer,
init_actions::PureActionProfile,
options::BROptions=BROptions()
) where {N,T,S,A,TR<:SimultaneousRevision} =
time_series(Random.GLOBAL_RNG, li, ts_length, init_actions, options)
function time_series(rng::AbstractRNG,
li::LocalInteraction{N,T,S,A,TR},
ts_length::Integer,
init_actions::PureActionProfile,
options::BROptions=BROptions()
) where {N,T,S,A,TR<:AsynchronousRevision}
player_ind_seq = rand(rng, 1:N, ts_length-1)
time_series(rng, li, ts_length, init_actions, player_ind_seq, options)
end
time_series(li::LocalInteraction{N,T,S,A,TR},
ts_length::Integer,
init_actions::PureActionProfile,
options::BROptions=BROptions()
) where {N,T,S,A,TR<:AsynchronousRevision} =
time_series(Random.GLOBAL_RNG, li, ts_length, init_actions, options)
function time_series(rng::AbstractRNG,
li::LocalInteraction{N},
ts_length::Integer,
player_ind_seq::Vector{<:Integer},
options::BROptions=BROptions()) where N
nums_actions = ntuple(i -> li.num_actions, N)
init_actions = random_pure_actions(rng, nums_actions)
time_series(rng, li, ts_length, init_actions, player_ind_seq, options)
end
time_series(li::LocalInteraction, ts_length::Integer,
player_ind_seq::Vector{<:Integer}, options::BROptions=BROptions()) =
time_series(Random.GLOBAL_RNG, li, ts_length, player_ind_seq, options)
function time_series(rng::AbstractRNG,
li::LocalInteraction{N},
ts_length::Integer,
options::BROptions=BROptions()) where N
nums_actions = ntuple(i -> li.num_actions, N)
init_actions = random_pure_actions(rng, nums_actions)
time_series(rng, li, ts_length, init_actions, options)
end
time_series(li::LocalInteraction, ts_length::Integer,
options::BROptions=BROptions()) =
time_series(Random.GLOBAL_RNG, li, ts_length, options)
@doc """
time_series([rng=Random.GLOBAL_RNG, ]li, ts_length, init_actions,
player_ind_seq[, options=BROptions()])
Return the time series of action profiles.
# Arguments
- `rng::AbstractRNG` : Random number generator used.
- `li::LocalInteraction{N}` : `LocalInteraction` instance.
- `ts_length::Integer` : The length of time series.
- `init_actions::PureActionProfile` : Action profile in the initial period. If
not provided, it is selected randomly.
- `player_ind_seq::Vector{<:Integer}` : Vector of integers representing the
index of players to take an action with asynchronous revision. If not
provided, it is selected randomly.
- `options::BROptions` : Options for `best_response` method.
# Returns
- `::Matrix{<:Integer}` : The time series of action profiles.
"""