# How to run a simple agent-based model via abmplot()

**URL:** https://discourse.julialang.org/t/how-to-run-a-simple-agent-based-model-via-abmplot/122476
**Category:** New to Julia
**Created:** [November 11, 2024, 12:24am UTC](https://discourse.julialang.org/t/how-to-run-a-simple-agent-based-model-via-abmplot/122476 "2024-11-11T00:24:25Z")
**Posts on this page:** 1
**Page:** 1

<div class="post-metadata">

### Author: ![pogikano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pogikano/32/212353_2.png) [@pogikano](https://discourse.julialang.org/u/pogikano)
#### Post date: [November 11, 2024, 12:24am UTC](https://discourse.julialang.org/t/how-to-run-a-simple-agent-based-model-via-abmplot/122476/1 "2024-11-11T00:24:25Z")

</div>

This simple model in continuous space starts with one dead (immobile) object and 500 live (moving ) ones. If a live object gets close to a dead one, it dies. The result should be a growing pattern. The control panel shows, but the model does not run. Any ideas?

```julia
using Agents
using Random

xMax = 100
yMax = 100
extent = (xMax, yMax)
touchDist = 10

println( "Starting here ======================" )

@agent struct Thing( ContinuousAgent{ 2, Float64 } )
    separation::Float64
    alive::Bool
end

rng = Random.MersenneTwister()

function initialize_model(;
    separation = touchDist,
    alive = true, )

    space2d = ContinuousSpace( extent; spacing = 1.0, periodic = false )

    model = StandardABM( Thing, space2d; 
                         rng, 
                         agent_step!, 
                         scheduler = Schedulers.Randomly() )
    vel = SVector( -1, -1 ) 
    
    for i = 1:501
        newAgent = add_agent!( model, vel, touchDist, alive )
        newAgent.pos = SVector( xMax/2, 1 )
    end
    
    model[1].pos = SVector( xMax/2, yMax/2 )
    model[1].alive = false
 
    return model
end

function agent_step!( thing, model )

    if ( !thing.alive ) return 
    end

    nums = rand( rng ) * 360  
    dx = cosd( nums )
    dy = sind( nums )
    newX::Float64 = thing.pos[1] + dx
    newY::Float64 = thing.pos[2] + dy

    if ( newX < 1 ) newX = xMax end
    if ( newX > xMax ) newX = 1 end
    if ( newY < 1 ) newY = yMax end
    if ( newY > yMax ) newY = 1 end 

    thing.pos = SVector( newX, newY )

    move_agent!( thing, thing.pos, model )

    nextThing = nearest_neighbor(thing, model::ABM{<:ContinuousSpace}, touchDist ) → nearest
    if( ( nextThing = !isnothing ) && ( !nextThing.alive ) )
        thing.alive = false
    end
end

using GLMakie

model = initialize_model()

plotkwargs = (;
    agent_marker = :circle, 
    agent_color = :red, 
    agent_size = 12
)

fig, ax, abmobs = abmplot( model; add_controls = true, plotkwargs...)
fig

```
