# Agents.jl and using a network model

**URL:** <https://discourse.julialang.org/t/agents-jl-and-using-a-network-model/126134>\
**Category:** General Usage\
**Created:** [February 21, 2025, 1:46am UTC](https://discourse.julialang.org/t/agents-jl-and-using-a-network-model/126134 "2025-02-21T01:46:58Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![zoek](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zoek/32/215261_2.png) [@zoek](https://discourse.julialang.org/u/zoek)\
**Post date:** [February 21, 2025, 1:46am UTC](https://discourse.julialang.org/t/agents-jl-and-using-a-network-model/126134/1 "2025-02-21T01:46:58Z")

</div>

Hi, I’m trying to use agents.jl for an SEIR network model, where each agent is a node and nodes are connected by edges. I want to run the model so that for each agent, ‘neighbours’ are identified through edges, and if a neighbour has status==I, then there is some probability that the agent will become infected. Currently I am stuck because when I run the model, neighbours aren’t being identified correctly - on my network model it looks like agents are randomly becoming infected and this is not occurring from being connected to another infected agent.

Here is the code:

```julia
using Agents 
using Random 
using Graphs
using GraphPlot
using Compose

# Create a simple graph with 5 nodes (agents)
network = SimpleGraph(5) # 5 nodes in a graph
add_edge!(network, 1, 2)
add_edge!(network, 1, 3)
add_edge!(network, 2, 4)
add_edge!(network, 3, 5)

# Number of nodes (agents)
num_nodes = nv(network)
space = GraphSpace(network)
p_initial_infected = 0.5 

# Set up agent
@agent struct Person(GraphAgent) # create agent that moves in a GraphSpace
    status::Symbol # S E I or R
end

# Set up model
function initialize(
    susceptibility::Float64 = 0.1
)

    # Define model props
    properties = Dict(
        :susceptibility => susceptibility
    )

    model = ABM(
        Person, 
        space; 
        properties, 
        agent_step!)

    # Add agents to model
    for i in 1:num_nodes
        status = rand() < p_initial_infected ? :I : :S # all are initially set as susceptible except a few as I
        add_agent!(Person, model, status) # place agents onto network and set Person variables

    end

    return model
end

# Model stepping function
function agent_step!(person::Person, model)
    neighbors = nearby_ids(person, model) # Get neighbors' IDs
    for neighbor_id in neighbors # Iterate over neighbor IDs
        neighbor = model[neighbor_id]  
        println("Agent ", person.id, " (", person.status, ") has neighbor ", neighbor_id)
        if neighbor.status == :I && person.status == :S
            # Calculate transmission probability (including edge weights if applicable)
            if rand(abmrng(model)) < model.susceptibility
                person.status = :I # Move to infected state
            end
        end  
    end
end

model = initialize()

# step model
for t in 1:20
    step!(model)
    println("Step $t: ", count(i -> i.status == :I, allagents(model)), "infected")
end 

```

Any help much appreciated! I could be totally on the wrong path - pretty new to Julia and ABMs. Thanks!
