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

run! does not allow for heterogeneous primitive types which are subtypes or a homogeneous abstract type #1096

Open
fieldofnodes opened this issue Oct 16, 2024 · 0 comments
Labels
bug Something isn't working data related with datacollection

Comments

@fieldofnodes
Copy link

Describe the bug

Mini scenario

Let adata = [:abstract_type_var] where :abstract-type-var will have an abstract type SuperType and the outcomes of :abstract-type-var will have types such as A <: SuperType or B <: SuperType
One would expect after running the run! command to collect data that the column would look like:
adf.abstract_type_var = [A(),B(),C()] where each row has a different primitive type, but they are all the same abstract type.

Statement

Function: run! does not allow for heterogeneous primitive types which are subtypes or a homogeneous abstract type.

Minimal Working Example

abstract type SuperType end
struct A <: SuperType end
struct B <: SuperType end

@agent struct TestAgent(NoSpaceAgent)
    type_agent::SuperType
end


        
# Set up model
function initialize_model(;num_agents = 10, seed = 123)

    rng = MersenneTwister(seed)
    properties = ()
    
    model = StandardABM(TestAgent;
        agent_step! = agent_step!,
        properties = properties, 
        rng = rng, 
        scheduler = Schedulers.Randomly(), 
        warn = false
    )


    # Add agents
    for _ in Base.OneTo(num_agents)
        add_agent!(
            TestAgent, 
            model, 
            rand(abmrng(model),[A(),B()]))
    end

    return model

end

function agent_step!(agent,model)
    agent.type_agent = rand(abmrng(model),[A(),B()])
end


    
# Run a sample
stable_params = (;num_agents = 10,seed = 2314)
model = initialize_model(;stable_params...)
adata = [:type_agent]
steps = 60
adf, _ = run!(model, steps; adata)

This gives the following output

julia> adf, _ = run!(model, steps; adata)
┌ Error: Error adding value to column :type_agent. Maybe you forgot passing `promote=true`?
└ @ DataFrames C:\Users\jonathan.miller\.julia\packages\DataFrames\kcA9R\src\dataframe\insertion.jl:369
ERROR: MethodError: Cannot `convert` an object of type B to an object of type A
The function `convert` exists, but no method is defined for this combination of argument types.

Closest candidates are:
  convert(::Type{T}, ::T) where T
   @ Base Base.jl:126

Stacktrace:
  [1] push!(a::Vector{A}, item::B)
    @ Base .\array.jl:1244
  [2] _append!(a::Vector{A}, ::Base.HasShape{1}, iter::Vector{SuperType})
    @ Base .\array.jl:1322
  [3] append!(a::Vector{A}, iter::Vector{SuperType})
    @ Base .\array.jl:1313
  [4] _append_or_prepend!(df1::DataFrame, df2::DataFrame; cols::Symbol, promote::Bool, atend::Bool)
    @ DataFrames C:\Users\jonathan.miller\.julia\packages\DataFrames\kcA9R\src\dataframe\insertion.jl:308
  [5] _append_or_prepend!
    @ C:\Users\jonathan.miller\.julia\packages\DataFrames\kcA9R\src\dataframe\insertion.jl:239 [inlined]
  [6] append!(df1::DataFrame, df2::DataFrame)
    @ DataFrames C:\Users\jonathan.miller\.julia\packages\DataFrames\kcA9R\src\dataframe\insertion.jl:93
  [7] collect_agent_data!(df::DataFrame, model::StandardABM{…}, properties::Vector{…}, step::Int64; kwargs::@Kwargs{})
    @ Agents C:\Users\jonathan.miller\.julia\packages\Agents\eKASH\src\simulations\collect.jl:474
  [8] collect_agent_data!
    @ C:\Users\jonathan.miller\.julia\packages\Agents\eKASH\src\simulations\collect.jl:458 [inlined]
  [9] run!(model::StandardABM{…}, n::Int64; when::Int64, when_model::Int64, mdata::Nothing, adata::Vector{…}, obtainer::Function, showprogress::Bool, init::Bool, dt::Float64)
    @ Agents C:\Users\jonathan.miller\.julia\packages\Agents\eKASH\src\simulations\collect.jl:137
 [10] top-level scope
    @ c:\Users\jonathan.miller\Projects\genetic_modelling\src\julia-version\insect_sex_mating\src\testing_insect_sex_mating.jl:308
Some type information was truncated. Use `show(err)` to see complete types.

This is possible directly in DataFrames.jl

using DataFrames
vec_types = [A(),B()]
df = DataFrame(abstract_type_var = vec_types)
df

gives

2×1 DataFrame
 Row │ abstract_type_var 
     │ SuperType
─────┼───────────────────
   1A()
   2B()

Agents.jl version

Agents v6.1.9

Julia version

Version 1.11.0 (2024-10-07)

@Tortar Tortar added bug Something isn't working data related with datacollection labels Oct 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working data related with datacollection
Projects
None yet
Development

No branches or pull requests

2 participants