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

Fix reset button in abmexploration #999

Merged
merged 9 commits into from
Mar 7, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
3 changes: 2 additions & 1 deletion ext/AgentsVisualizations/src/convenience.jl
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,9 @@ function init_abm_data_plots!(fig, abmobs, adata, mdata, alabels, mlabels, plotk
end
end
on(resetclick) do clicks

for ax in axs
vlines!(ax, [abmtime(abmobs.model[])], color = "#c41818")
vlines!(ax, [abmobs._offset_time[]], color = "#c41818")
end
end
return nothing
Expand Down
13 changes: 7 additions & 6 deletions ext/AgentsVisualizations/src/interaction.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ function add_controls!(fig, abmobs, spu)
getfield.(Ref(abmobs), (:model, :agent_step!, :model_step!, :adata, :mdata, :adf, :mdf, :when))

init_dataframes!(model[], adata, mdata, adf, mdf)
collect_data!(model[], when, adata, mdata, adf, mdf)
collect_data!(abmobs, model[], when, adata, mdata, adf, mdf)

# Create new layout for control buttons
controllayout = fig[end+1,:][1,1] = GridLayout(tellheight = true)
Expand All @@ -42,7 +42,7 @@ function add_controls!(fig, abmobs, spu)
step = Button(fig, label = "step\nmodel")
on(step.clicks) do c
Agents.step!(abmobs, speed[])
collect_data!(model[], when[], adata, mdata, adf, mdf)
collect_data!(abmobs, model[], when[], adata, mdata, adf, mdf)
end
# Run button
run = Button(fig, label = "run\nmodel")
Expand All @@ -63,13 +63,14 @@ function add_controls!(fig, abmobs, spu)
reset = Button(fig, label = "reset\nmodel")
model0 = deepcopy(model[]) # backup initial model state
on(reset.clicks) do c
abmobs._offset_time[] += abmtime(model[])
model[] = deepcopy(model0)
end
# Clear button
clear = Button(fig, label = "clear\ndata")
on(clear.clicks) do c
init_dataframes!(model[], adata, mdata, adf, mdf)
collect_data!(model[], when, adata, mdata, adf, mdf)
collect_data!(abmobs, model[], when, adata, mdata, adf, mdf)
end
# Layout buttons
controllayout[2, :] = Makie.hbox!(step, run, reset, clear; tellwidth = false)
Expand All @@ -88,14 +89,14 @@ function init_dataframes!(model, adata, mdata, adf, mdf)
return nothing
end

function collect_data!(model, when, adata, mdata, adf, mdf)
function collect_data!(abmobs, model, when, adata, mdata, adf, mdf)
if Agents.should_we_collect(abmtime(model), model, when)
if !isnothing(adata)
Agents.collect_agent_data!(adf[], model, adata)
Agents.collect_agent_data!(adf[], model, adata; offset_time=abmobs._offset_time[])
adf[] = adf[] # trigger Observable
end
if !isnothing(mdata)
Agents.collect_model_data!(mdf[], model, mdata)
Agents.collect_model_data!(mdf[], model, mdata; offset_time=abmobs._offset_time[])
mdf[] = mdf[] # trigger Observable
end
end
Expand Down
10 changes: 5 additions & 5 deletions ext/AgentsVisualizations/src/model_observable.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ function Agents.ABMObservable(model::AgentBasedModel;
mdf = Observable(Agents.init_model_dataframe(model, mdata))
end
return ABMObservable(
Observable(model), agent_step!, model_step!, adata, mdata, adf, mdf, Observable(0), when
)
Observable(model), agent_step!, model_step!, adata, mdata, adf, mdf, Observable(0), when)
end

function Agents.step!(abmobs::ABMObservable, n; kwargs...)
model, adf, mdf = abmobs.model, abmobs.adf, abmobs.mdf
abmobs._offset_time[] += n
if Agents.agent_step_field(model[]) != Agents.dummystep || Agents.model_step_field(model[]) != Agents.dummystep
Agents.step!(model[], n; kwargs...)
else
Expand All @@ -31,11 +31,11 @@ function Agents.step!(abmobs::ABMObservable, n; kwargs...)
notify(model)
if Agents.should_we_collect(abmtime(model[]), model[], abmobs.when)
if !isnothing(abmobs.adata)
Agents.collect_agent_data!(adf[], model[], abmobs.adata)
Agents.collect_agent_data!(adf[], model[], abmobs.adata; offset_time=abmobs._offset_time[])
notify(adf)
end
if !isnothing(abmobs.mdata)
Agents.collect_model_data!(mdf[], model[], abmobs.mdata)
Agents.collect_model_data!(mdf[], model[], abmobs.mdata; offset_time=abmobs._offset_time[])
notify(mdf)
end
end
Expand All @@ -48,4 +48,4 @@ function Base.show(io::IO, abmobs::ABMObservable)
print(io, "\nand with data collection:\n")
print(io, " adata: $(abmobs.adata)\n")
print(io, " mdata: $(abmobs.mdata)")
end
end
10 changes: 6 additions & 4 deletions src/simulations/collect.jl
Original file line number Diff line number Diff line change
Expand Up @@ -547,14 +547,14 @@ Collect and add agent data into `df` (see [`run!`](@ref) for the dispatch rules
of `properties` and `obtainer`).
"""
collect_agent_data!(df, model, properties::Nothing, step::Int = 0; kwargs...) = df
function collect_agent_data!(df, model, properties::Vector, step::Int = 0; kwargs...)
function collect_agent_data!(df, model, properties::Vector, step::Int = 0; offset_time = 0, kwargs...)
if step != 0
@warn "Passing the `step` argument to `collect_agent_data!` is deprecated,
now `abmtime(model)` is used automatically" maxlog=1
end
alla = sort!(collect(allagents(model)), by = a -> a.id)
dd = DataFrame()
dd[!, :step] = fill(abmtime(model), length(alla))
dd[!, :step] = fill(abmtime(model)+offset_time, length(alla))
dd[!, :id] = map(a -> a.id, alla)
if :agent_type ∈ propertynames(df)
dd[!, :agent_type] = map(a -> Symbol(typeof(a)), alla)
Expand All @@ -571,14 +571,15 @@ function collect_agent_data!(
model::ABM,
properties::Vector{<:Tuple},
step::Int = 0;
offset_time = 0,
kwargs...,
)
if step != 0
@warn "Passing the `step` argument to `collect_agent_data!` is deprecated,
now `abmtime(model)` is used automatically" maxlog=1
end
alla = allagents(model)
push!(df[!, 1], abmtime(model))
push!(df[!, 1], abmtime(model)+offset_time)
for (i, prop) in enumerate(properties)
_add_col_data!(df[!, i+1], prop, alla; kwargs...)
end
Expand All @@ -595,13 +596,14 @@ function collect_model_data!(
model,
properties::Vector,
step::Real = 0;
offset_time = 0,
obtainer = identity,
)
if step != 0
@warn "Passing the `step` argument to `collect_model_data!` is deprecated,
now `abmtime(model)` is used automatically" maxlog=1
end
push!(df[!, :step], abmtime(model))
push!(df[!, :step], abmtime(model)+offset_time)
for fn in properties
push!(df[!, dataname(fn)], get_data(model, fn, obtainer))
end
Expand Down
2 changes: 1 addition & 1 deletion src/visualizations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ struct ABMObservable{M, AS, MS, AD, MD, ADF, MDF, W, S}
mdata::MD
adf::ADF # this is `nothing` or `Observable`
mdf::MDF # this is `nothing` or `Observable`
s::S # Observable{Int}
_offset_time::S # Observable{Int}
when::W
end
export ABMObservable
Expand Down
Loading