I want to add display of text values and statistics in an interactive Agents model, but I can’t get it to work. As an example, if we start with the Schelling model from the Tutorial:
using Agents
using GLMakie
space = GridSpace((20, 20))
@agent struct Schelling(GridAgent{2})
mood::Bool = false
group::Int
end
function schelling_step!(agent, model)
minhappy = model.min_to_be_happy
count_neighbors_same_group = 0
for neighbor in nearby_agents(agent, model)
if agent.group == neighbor.group
count_neighbors_same_group += 1
end
end
if count_neighbors_same_group ≥ minhappy
agent.mood = true
else
agent.mood = false
move_agent_single!(agent, model)
end
return
end
properties = Dict(:min_to_be_happy => 3)
model = StandardABM(
Schelling,
space;
agent_step! = schelling_step!, properties
)
for n in 1:300
add_agent_single!(model; group = n < 300 / 2 ? 1 : 2)
end
using Statistics: mean
xpos(agent) = agent.pos[1]
adata = [(:mood, sum), (xpos, mean)]
adf, mdf = run!(model, 5; adata)
groupcolor(a) = a.group == 1 ? :blue : :orange
groupmarker(a) = a.group == 1 ? :circle : :rect
fig, ax, abmobs = abmplot(model;
agent_color = groupcolor, agent_marker = groupmarker, agent_size = 10,
adata, add_controls = true)
I thought to add the following code to lift the current step number from abmobs.adf
:
time_val = @lift($(abmobs.adf).time[end])
value_layout = fig[:, 2] = GridLayout(valign = :top)
value_layout[1, 1] = Label(fig, "step: $(to_value(time_val))")
But this only prints out the step statically and doesn’t update with the rest of the abmplot. Do I have to add a notify
somewhere?