From 39a11bb210b9e14cf70d2c5bf1488803fb929463 Mon Sep 17 00:00:00 2001 From: Adriano Meligrana <68152031+Tortar@users.noreply.github.com> Date: Sat, 10 Aug 2024 14:53:14 +0200 Subject: [PATCH 1/4] Improve performance of EventQueueABM by better union splitting --- src/core/model_event_queue.jl | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/core/model_event_queue.jl b/src/core/model_event_queue.jl index a75d440e49..38f9a8edc0 100644 --- a/src/core/model_event_queue.jl +++ b/src/core/model_event_queue.jl @@ -141,7 +141,7 @@ 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, @@ -149,7 +149,7 @@ function EventQueueABM( 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 @@ -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 @@ -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}( From 8138fbc53579667ed8a936733d8ca59002ded73b Mon Sep 17 00:00:00 2001 From: Adriano Meligrana <68152031+Tortar@users.noreply.github.com> Date: Sat, 10 Aug 2024 14:58:33 +0200 Subject: [PATCH 2/4] Update Agents.jl --- src/Agents.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Agents.jl b/src/Agents.jl index c4494205fa..2ada403393 100644 --- a/src/Agents.jl +++ b/src/Agents.jl @@ -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 From fad629463ca09ba81ea22549417b706bc8c48440 Mon Sep 17 00:00:00 2001 From: Adriano Meligrana <68152031+Tortar@users.noreply.github.com> Date: Sat, 10 Aug 2024 15:00:58 +0200 Subject: [PATCH 3/4] Update Project.toml --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index af6c33781a..88c0e4e7cb 100644 --- a/Project.toml +++ b/Project.toml @@ -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" From 181c87b874ac81805331b99a3a8257cc8392403e Mon Sep 17 00:00:00 2001 From: Adriano Meligrana <68152031+Tortar@users.noreply.github.com> Date: Sat, 10 Aug 2024 22:41:43 +0200 Subject: [PATCH 4/4] Update model_event_queue.jl --- src/core/model_event_queue.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/core/model_event_queue.jl b/src/core/model_event_queue.jl index 38f9a8edc0..baab7a794c 100644 --- a/src/core/model_event_queue.jl +++ b/src/core/model_event_queue.jl @@ -250,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