# Scheduler errors when trying to graph agents

**URL:** <https://discourse.julialang.org/t/scheduler-errors-when-trying-to-graph-agents/121706>\
**Category:** Modelling & Simulations\
**Tags:** question, agents, graphs\
**Created:** [October 24, 2024, 2:43pm UTC](https://discourse.julialang.org/t/scheduler-errors-when-trying-to-graph-agents/121706 "2024-10-24T14:43:49Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![tfranks](https://avatars.discourse-cdn.com/v4/letter/t/898d66/32.png) [@tfranks](https://discourse.julialang.org/u/tfranks)\
**Post date:** [October 24, 2024, 2:43pm UTC](https://discourse.julialang.org/t/scheduler-errors-when-trying-to-graph-agents/121706/1 "2024-10-24T14:43:50Z")

</div>

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)

---

<div class="post-metadata">

**Author:** ![Datseris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/datseris/32/13406_2.png) [@Datseris](https://discourse.julialang.org/u/Datseris)\
**Post date:** [October 24, 2024, 4:11pm UTC](https://discourse.julialang.org/t/scheduler-errors-when-trying-to-graph-agents/121706/2 "2024-10-24T16:11:38Z")

</div>

Please see [Please read: make it easier to help you](https://discourse.julialang.org/t/please-read-make-it-easier-to-help-you/14757) .

in particular format your post so that code is shown as code, and report actual error stactraces, not approximate ones.

---

<div class="post-metadata">

**Author:** ![tfranks](https://avatars.discourse-cdn.com/v4/letter/t/898d66/32.png) [@tfranks](https://discourse.julialang.org/u/tfranks)\
**Post date:** [October 24, 2024, 6:24pm UTC](https://discourse.julialang.org/t/scheduler-errors-when-trying-to-graph-agents/121706/3 "2024-10-24T18:24:16Z")

</div>

Will do. I’ll close this post and report the error verbatim. Thank you.

---

<div class="post-metadata">

**Author:** ![sfuerst](https://avatars.discourse-cdn.com/v4/letter/s/3da27b/32.png) [@sfuerst](https://discourse.julialang.org/u/sfuerst)\
**Post date:** [October 28, 2024, 11:58am UTC](https://discourse.julialang.org/t/scheduler-errors-when-trying-to-graph-agents/121706/4 "2024-10-28T11:58:18Z")

</div>

In case your model primarily focuses on the bank/depositor network, it might be worth looking at Vahana.jl, where the model is expressed as a graph from the start, offering extensive functionality around the network structure.

For example, the code to create the network and its visualization would look like this:

```julia
using Vahana
import GraphMakie, GLMakie, NetworkLayout

struct Bank
    failed::Bool
    vulnerable::Bool
end

struct Depositor
end

struct Deposit
end

sim = ModelTypes() |>
    register_agenttype!(Bank) |>
    register_agenttype!(Depositor) |>
    register_edgetype!(Deposit) |>
    create_model("bank model") |>
    create_simulation()

banks = [Bank(rand(Bool), rand(Bool)) for _ in 1:500]
bankids = add_agents!(sim, banks)

healthy = filter(b -> b[2].failed == false && b[2].vulnerable == false,
                 zip(bankids, banks) |> collect)

for d in add_agents!(sim, [Depositor() for _ in 1:250])
    add_edge!(sim, d, rand(healthy)[1], Deposit())
end

finish_init!(sim)

modify_vis(state::Bank, _, _) =
    Dict(:node_color => state.vulnerable ? :orange : :blue,
         :node_marker => state.failed ? :rect : :circle)

modify_vis(state::Depositor, _, _) =
    Dict(:node_color => :black,
         :node_size => 8.0)

modify_vis(state::Deposit, _, _, _) = Dict(:edge_color => :lightgrey)

create_graphplot(sim, update_fn = modify_vis) |> display

```

---

<div class="post-metadata">

**Author:** ![tfranks](https://avatars.discourse-cdn.com/v4/letter/t/898d66/32.png) [@tfranks](https://discourse.julialang.org/u/tfranks)\
**Post date:** [October 29, 2024, 10:35pm UTC](https://discourse.julialang.org/t/scheduler-errors-when-trying-to-graph-agents/121706/5 "2024-10-29T22:35:05Z")

</div>

Thank you very much, sfuerst! I will try it.
