Hello,
I’ve created an agent-based model where depositor agents withdraw money from bank agents. I’m now trying to create a visual of the network by using Graphs.jl to show the banks as nodes and the depositors’ paths as the edges. The depositors can withdraw from several banks, which, I believe is why the “scheduler” error message keeps occurring. The messages include UnderVarError, scheduler not defined, and Argument Error pos when using space.
How would you suggest I fix? Or is it possible with Graphs.jl? Thank you.
Blockquote
#CREATE FUNCTION to INITIALIZE and SETUP MODEL#
Define the model initialization function
function initialize_model(; num_banks=500, num_depositors=250)
properties = Dict(
:num_banks => num_banks,
:num_depositors => num_depositors,
:num_failed_banks => 0,
:num_vulnerable_banks => 0,
:tick => 0,
)
space = GridSpace((10, 10), periodic=false)
scheduler = AgentSchedulers.fastest
model = ABM(Union{Bank, Depositor}, space; properties, scheduler=scheduler)
##Poisson distribution for x and y coordinates
xes = rand(Poisson(40), num_banks)
yes = rand(Poisson(75), num_banks)
# Add banks to the model
for (i, (x, y)) in enumerate(zip(xes, yes))
add_agent!(Bank(i, rand(Bool), rand(Bool), rand(), 2 * rand(), 100, rand(1:10), rand(1:3)), model; pos=(x, y))
end
# Add depositors to the model
for i in 1:num_depositors
x, y = rand(1:10), rand(1:10) # Random positions for depositors
add_agent!(Depositor(num_banks + i, (x, y)), model)
end
return model
end
Initialize the model
model = initialize_model()
CREATE GRAPH WITH BANKS AS NODES AND DEPOSITORS AS EDGES
Collect all banks and depositors
banks = [a for a in allagents(model) if isa(a, Bank)]
depositors = [a for a in allagents(model) if isa(a, Depositor)]
Initialize the graph with the total number of banks
n_banks = length(banks)
g = SimpleGraph(n_banks)
Create a mapping from bank IDs to graph node indices
bank_id_to_index = Dict(bank.id => i for (i, bank) in enumerate(banks))
Add edges based on depositors
for depositor in depositors
find_bank = random_agent(model, a → isa(a, Bank) && a.vul == true && a.health == true)
if find_bank != nothing
depositor_index = bank_id_to_index[find_bank.id] # This should be the bank’s index
bank_index = bank_id_to_index[find_bank.id]
Graphs.add_edge!(g, depositor_index, bank_index)
end
end
Define the colors based on vulnerability
bank_colors = [bank.vul ? :orange : :blue for bank in banks]
Define the layout using spring_layout from GraphPlot
layout = spring_layout(g; C=1.0, MAXITER=100)
Plot the graph
gplot(g, layout=layout, nodefillc=bank_colors)