-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathagents_jl_integration.jl
62 lines (60 loc) · 2.08 KB
/
agents_jl_integration.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
################################################
####### TEST INTEGRATION WITH AGENTS.JL ########
################################################
# Create a multilayer graph
graph = MultilayerGraph(all_layers_u, all_interlayers_u)
# Define the agent type
@agent EigenAgent GraphAgent begin
previous_value::Float64
old_value::Float64
new_value::Float64
end
# Instantiate the agent
function EigenAgent(id, pos; initial_opinion::Float64)
return EigenAgent(id, pos, initial_opinion, initial_opinion, -1.0)
end
# Define the agent-based model
EigenABM = ABM(EigenAgent, GraphSpace(graph))
# Add agents to the ABM so that agent i is located in vertex i
for (i, mv) in enumerate(mv_vertices(graph))
initial_opinion = 1.0
add_agent!(i, EigenABM; initial_opinion=initial_opinion)
end
# Define the individual-level dynamics (micro-dynamics)
function agent_step!(agent::EigenAgent, model)
agent.previous_value = agent.old_value
return agent.new_value = sum([
outneighbor_agent.old_value for
outneighbor_agent in nearby_agents(agent, model; neighbor_type=:all)
])
end
# Define the system-level dynamics (macro-dynamics)
function model_step!(model)
tot_edges_weight = sqrt(sum(abs2, [agent.new_value for agent in allagents(model)]))
for agent in allagents(model)
agent.new_value = agent.new_value / tot_edges_weight
agent.old_value = agent.new_value
end
end
# Set the rule to stop the model simulation
function terminate(model, s)
# println(s, typeof(s))
if any(
!isapprox(a.previous_value, a.new_value; rtol=1e-18) for a in allagents(model)
) && !(s > 10000)
return false
else
return true
end
end
# Simulate the model
agent_data, _ = run!(
EigenABM, agent_step!, model_step!, terminate; adata=[:new_value], when=terminate
)
# Compute the eigenvector centrality of the surrounding multilayer graph
eig_centr_swm, err_swm = eigenvector_centrality(
EigenABM.space.graph; weighted=false, tol=1e-18, norm="null"
)
for (val_1, val_2) in zip(eig_centr_swm, agent_data.new_value)
@test val_1 ≈ val_2
end