Hello,
I am using Agents.jl and I would like to plot a value for each agent dynamically with abmplot with enable_inspection = true. This example shows how to generate a static plot of this variety (here is the plot). But its not clear to me how this can be done.
Specifically, if I have in pseudo-code:
fig, ax, abmods = abmplot(..., enable_inspection = true)
widget_layout = fig[end + 1, :] = GridLayout()
plot_layout = widget_layout[1,1] = GridLayout()
ax_plot = Axis(plot_layout[1,1])
# throws error cannot convert Vector{Float64} to Float64
lines!(ax_plot, @lift(Point2f.($(abmods.mdf).time, $(abmods.mdf).beliefs)))
How do I modify the last line to support Vector{Float64}? I can add a runnable version of the code if that would be helpful.
Could you tell me what the type of abmods.mdf, abmods.mdf[].time, and abmods.mdf[].beliefs is? Or the full error message?
Without that it’s a bit hard to understand what is going on 
Thank you for your reply. I can provide a MWE tomorrow if that would be helpful. For now, I can tell you that abmobs.mdf.val.time is a Vector{Int} and abmods.mdf.val.beliefs is Vector{Vector{Float64}}, but according to the error message below, it is expecting Float32. I think the problem is that on each iteration, it is expecting a scalar, but it is receiving a vector containing the belief state of each agent. Again, if that doesn’t help, I can simplify the model tomorrow and post it tomorrow.
Summary
ERROR: MethodError: Cannot `convert` an object of type Vector{Float64} to an object of type Float32
The function `convert` exists, but no method is defined for this combination of argument types.
Closest candidates are:
convert(::Type{T}, ::Ratios.SimpleRatio{S}) where {T<:AbstractFloat, S}
@ Ratios C:\Users\christopher.fisher\.julia\packages\Ratios\FsiCW\src\Ratios.jl:51
convert(::Type{T}, ::IntervalArithmetic.ExactReal) where T<:Real
@ IntervalArithmetic C:\Users\christopher.fisher\.julia\packages\IntervalArithmetic\f3Gip\src\intervals\exact_literals.jl:130
convert(::Type{T}, ::Unit) where T<:Number
@ Makie C:\Users\christopher.fisher\.julia\packages\Makie\kJl0u\src\units.jl:29
...
Stacktrace:
[1] macro expansion
@ C:\Users\christopher.fisher\.julia\packages\StaticArraysCore\MoJEf\src\StaticArraysCore.jl:95 [inlined]
[2] convert_ntuple
@ C:\Users\christopher.fisher\.julia\packages\StaticArraysCore\MoJEf\src\StaticArraysCore.jl:91 [inlined]
[3] Point{2, Float32}(x::Tuple{Int64, Vector{Float64}})
@ GeometryBasics C:\Users\christopher.fisher\.julia\packages\GeometryBasics\yB1f1\src\fixed_arrays.jl:21
[4] StaticArray
@ C:\Users\christopher.fisher\.julia\packages\StaticArrays\0cEwi\src\convert.jl:173 [inlined]
[5] _broadcast_getindex_evalf
@ .\broadcast.jl:699 [inlined]
[6] _broadcast_getindex
@ .\broadcast.jl:672 [inlined]
[7] _getindex
@ .\broadcast.jl:620 [inlined]
[8] getindex
@ .\broadcast.jl:616 [inlined]
[9] copy
@ .\broadcast.jl:933 [inlined]
[10] materialize(bc::Base.Broadcast.Broadcasted{Base.Broadcast.DefaultArrayStyle{…}, Nothing, Type{…}, Tuple{…}})
@ Base.Broadcast .\broadcast.jl:894
[11] (::var"#62#63")(arg1#290::DataFrames.DataFrame)
@ Main .\none:-1
[12] #map#11
@ C:\Users\christopher.fisher\.julia\packages\Observables\YdEbO\src\Observables.jl:570 [inlined]
Are you looking for series/series! to plot a variable number of items?
This was a promising suggestion, but unfortunately it leads to a similar error. It looks like series! requires an 2 dimensional array in which rows correspond to separate lines. I am wondering whether the source of the problem is the way @lift accumulates observations from $(abmods.mdf).get_mean_beliefs).
As promised, I created a simplified MWE of my model to help arrive at a solution. I also included the version info for future reference.
MWE
using Agents
using GLMakie
using Statistics
@agent struct HKAgent(GridAgent{2})
beliefs::Float64
end
function hk_model(; numagents = 100, ϵ = 0.2)
space = GridSpaceSingle((20, 20))
model = StandardABM(HKAgent, space; agent_step!, model_step!, scheduler = Schedulers.fastest, properties = Dict(:ϵ => ϵ))
for i in 1:numagents
o = rand(abmrng(model))
add_agent_single!(model, o)
end
return model
end
function boundfilter(agent, model)
filter(
j -> abs(agent.beliefs - j) < model.ϵ,
[a.beliefs for a in allagents(model)],
)
end
function agent_step!(agent, model)
agent.beliefs = mean(boundfilter(agent, model))
end
model_step!(model) = nothing
get_beliefs(model) = [a.beliefs for a ∈ allagents(model)]
get_mean_beliefs(model) = mean(get_beliefs(model))
model = hk_model(; numagents = 100, ϵ = 0.2)
# _, df_model = run!(model, 20; mdata = [get_beliefs])
fig, ax, abmods = abmplot(model, enable_inspection = true, add_controls = true, mdata = [get_beliefs, get_mean_beliefs])
widget_layout = fig[end + 1, :] = GridLayout()
plot_mean_layout = widget_layout[1,1] = GridLayout()
ax_mean_beliefs = Axis(plot_mean_layout[1,1])
lines!(ax_mean_beliefs, @lift(Point2f.($(abmods.mdf).time, $(abmods.mdf).get_mean_beliefs)))
plot_individual_layout = widget_layout[2,1] = GridLayout()
ax_individual_beliefs = Axis(plot_individual_layout[1,1])
# for testing: runs with commented code
# lines!(ax_individual_beliefs, @lift(Point2f.($(abmods.mdf).time, $(abmods.mdf).get_mean_beliefs)))
# original failing code
# lines!(ax_individual_beliefs, @lift(Point2f.($(abmods.mdf).time, $(abmods.mdf).get_beliefs)))
series!(ax_individual_beliefs, @lift(Point2f.($(abmods.mdf).time, $(abmods.mdf).get_beliefs)))
on(abmods.model) do m
autolimits!(ax_mean_beliefs)
autolimits!(ax_individual_beliefs)
end
fig
Error
ERROR: MethodError: Cannot `convert` an object of type Vector{Float64} to an object of type Float32
The function `convert` exists, but no method is defined for this combination of argument types.
Closest candidates are:
convert(::Type{T}, ::IntervalArithmetic.ExactReal) where T<:Real
@ IntervalArithmetic ~/.julia/packages/IntervalArithmetic/f3Gip/src/intervals/exact_literals.jl:130
convert(::Type{T}, ::Unit) where T<:Number
@ Makie ~/.julia/packages/Makie/kJl0u/src/units.jl:29
convert(::Type{T}, ::T) where T<:Number
@ Base number.jl:6
...
Stacktrace:
[1] macro expansion
@ ~/.julia/packages/StaticArraysCore/MoJEf/src/StaticArraysCore.jl:95 [inlined]
[2] convert_ntuple
@ ~/.julia/packages/StaticArraysCore/MoJEf/src/StaticArraysCore.jl:91 [inlined]
[3] Point{2, Float32}(x::Tuple{Int64, Vector{Float64}})
@ GeometryBasics ~/.julia/packages/GeometryBasics/yB1f1/src/fixed_arrays.jl:21
[4] StaticArray
@ ~/.julia/packages/StaticArrays/0cEwi/src/convert.jl:173 [inlined]
[5] _broadcast_getindex_evalf
@ ./broadcast.jl:699 [inlined]
[6] _broadcast_getindex
@ ./broadcast.jl:672 [inlined]
[7] _getindex
@ ./broadcast.jl:620 [inlined]
[8] getindex
@ ./broadcast.jl:616 [inlined]
[9] copy
@ ./broadcast.jl:933 [inlined]
[10] materialize(bc::Base.Broadcast.Broadcasted{Base.Broadcast.DefaultArrayStyle{…}, Nothing, Type{…}, Tuple{…}})
@ Base.Broadcast ./broadcast.jl:894
[11] (::var"#40#41")(arg1#280::DataFrames.DataFrame)
@ Main ./none:-1
[12] #map#11
@ ~/.julia/packages/Observables/YdEbO/src/Observables.jl:570 [inlined]
[13] map(::var"#40#41", ::Observable{DataFrames.DataFrame})
@ Observables ~/.julia/packages/Observables/YdEbO/src/Observables.jl:568
[14] top-level scope
@ REPL[38]:5
Some type information was truncated. Use `show(err)` to see complete types.
Version Info
``julia
(abm_multi_line) pkg> st
Status ~/.julia/dev/sandbox/abm_multi_line/Project.toml
[46ada45e] Agents v7.0.1
[e9467ef8] GLMakie v0.13.9