Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve speed of random_agent #897

Merged
merged 3 commits into from
Oct 5, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/src/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ UnremovableABM
```@docs
getindex(::ABM, ::Integer)
getproperty(::ABM, ::Symbol)
random_id
random_agent
nagents
allagents
Expand Down
14 changes: 9 additions & 5 deletions src/core/model_abstract.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# during the definition of a new ABM type.
export AgentBasedModel, ABM
export abmrng, abmscheduler, abmspace, abmproperties
export random_agent, nagents, allagents, allids, nextid, seed!
export random_agent, random_id, nagents, allagents, allids, nextid, seed!

###########################################################################################
# %% Fundamental type definitions
Expand Down Expand Up @@ -148,11 +148,17 @@ Return a valid `id` for creating a new agent with it.
"""
nextid(model::ABM) = notimplemented(model)

"""
random_id(model) → id
Return a random id from the model.
"""
random_id(model) = rand(abmrng(model), allids(model))

"""
random_agent(model) → agent
Return a random agent from the model.
"""
random_agent(model) = model[rand(abmrng(model), allids(model))]
random_agent(model) = model[random_id(model)]

"""
random_agent(model, condition; optimistic=true, alloc = false) → agent
Expand All @@ -179,10 +185,8 @@ function random_agent(model, condition; optimistic = true, alloc = false)
end

function optimistic_random_agent(model, condition, alloc; n_attempts = nagents(model))
rng = abmrng(model)
ids = allids(model)
@inbounds while n_attempts != 0
idx = rand(rng, ids)
idx = random_id(model)
condition(model[idx]) && return model[idx]
n_attempts -= 1
end
Expand Down
2 changes: 2 additions & 0 deletions src/core/model_concrete.jl
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ function remove_agent_from_model!(agent::A, model::UnremovableABM) where {A<:Abs
error("Cannot remove agents in a `UnremovableABM`")
end

random_agent(model::StandardABM) = rand(abmrng(model), agent_container(model)).first
random_agent(model::StandardABM) = rand(abmrng(model), agent_container(model)).second

#######################################################################################
# %% Model construction validation
Expand Down
Loading