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 performance of EventQueueABM by better union splitting #1065

Merged
merged 4 commits into from
Aug 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "Agents"
uuid = "46ada45e-f475-11e8-01d0-f70cc89e6671"
authors = ["George Datseris", "Tim DuBois", "Aayush Sabharwal", "Ali Vahdati", "Adriano Meligrana"]
version = "6.1.1"
version = "6.1.2"

[deps]
CSV = "336ed68f-0bac-5ca0-87d4-7b16caf5d00b"
Expand Down
2 changes: 1 addition & 1 deletion src/Agents.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ using DynamicSumTypes
export variant, variantof
import ProgressMeter
using Random
using StaticArrays: SVector
using StaticArrays: SVector, SizedVector
export SVector
import LinearAlgebra
import StreamSampling
Expand Down
15 changes: 8 additions & 7 deletions src/core/model_event_queue.jl
Original file line number Diff line number Diff line change
Expand Up @@ -141,15 +141,15 @@ can be evolved in time.
event for an agent after an event affected said agent has been triggered.
"""
function EventQueueABM(
::Type{A}, events::E,
::Type{A}, events,
space::S = nothing;
container::Type = Dict,
properties::P = nothing,
rng::R = Random.default_rng(),
warn = true,
autogenerate_on_add = true,
autogenerate_after_action = true,
) where {A<:AbstractAgent,S<:SpaceType,E,P,R<:AbstractRNG}
) where {A<:AbstractAgent,S<:SpaceType,P,R<:AbstractRNG}
if warn
@warn "This model type is still experimental which means that it is subject to breaking changes in the
future. Also, while all the core functionalities have been implemented, this model type
Expand All @@ -158,9 +158,10 @@ function EventQueueABM(
end
C = construct_agent_container(container, A)
agents = C()
I = events isa Tuple ? Int : keytype(events) # `Tuple` doesn't define `keytype`...
events = SizedVector{length(events), Union{typeof.(events)...}}(events...)

# the queue stores pairs of (agent ID, event index) mapping them to their trigger time
queue = BinaryHeap(Base.By(last), Pair{Tuple{I, Int}, Float64}[])
queue = BinaryHeap(Base.By(last), Pair{Tuple{Int, Int}, Float64}[])

agent_types = is_sumtype(A) ? values(allvariants(A)) : union_types(A)
type_func = is_sumtype(A) ? variantof : typeof
Expand Down Expand Up @@ -195,8 +196,8 @@ function EventQueueABM(
# because we use the index of `type_to_idx` to access them.

# construct the type
TF,ET,PT,FPT,TI,Q = typeof.((
type_func, idx_events_each_type, propensities_each_type,
E,TF,ET,PT,FPT,TI,Q = typeof.((
events, type_func, idx_events_each_type, propensities_each_type,
idx_func_propensities_each_type, type_to_idx, queue
))
return EventQueueABM{S,A,C,P,E,R,TF,ET,PT,FPT,TI,Q}(
Expand Down Expand Up @@ -249,7 +250,7 @@ function add_event!(agent, model) # TODO: Study type stability of this function
end
# Then, select an event based on propensities
event_idx = events_type[sample_propensity(abmrng(model), propensities_type)] # The time to the event is generated from the selected event
selected_event = abmevents(model)[event_idx]
selected_event = events[event_idx]
selected_prop = propensities_type[event_idx]
t = selected_event.timing(agent, model, selected_prop)
# we then propagate to the direct function
Expand Down
Loading