/
hk.jl
54 lines (48 loc) · 1.16 KB
/
hk.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
using StatsBase: mean
mutable struct HKAgent <: AbstractAgent
id::Int
old_opinion::Float64
new_opinion::Float64
previous_opinon::Float64
end
"""
``` julia
hk(;
numagents = 100,
ϵ = 0.2
)
```
Same as in [HK (Hegselmann and Krause) opinion dynamics model](@ref).
"""
function hk(; numagents = 100, ϵ = 0.2)
model = ABM(HKAgent, scheduler = fastest, properties = Dict(:ϵ => ϵ))
for i in 1:numagents
o = rand()
add_agent!(model, o, o, -1)
end
return model, hk_agent_step!, hk_model_step!, terminate
end
function boundfilter(agent, model)
filter(
j -> abs(agent.old_opinion - j) < model.ϵ,
[a.old_opinion for a in allagents(model)],
)
end
function hk_agent_step!(agent, model)
agent.previous_opinon = agent.old_opinion
agent.new_opinion = mean(boundfilter(agent, model))
end
function hk_model_step!(model)
for a in allagents(model)
a.old_opinion = a.new_opinion
end
end
function terminate(model, s)
if any(
!isapprox(a.previous_opinon, a.new_opinion; rtol = 1e-12) for a in allagents(model)
)
return false
else
return true
end
end