JuliaDynamics / Agents.jl

Agent-based modeling framework in Julia
https://juliadynamics.github.io/Agents.jl/stable/
MIT License
712 stars 114 forks source link

error trying to run abmexploration #960

Closed manentai closed 5 months ago

manentai commented 5 months ago

Describe the bug I am trying to run abmexploration on the schelling example and I get this:

┌ Warning: Found `resolution` in the theme when creating a `Scene`. The `resolution` keyword for `Scene`s and `Figure`s has been deprecated. Use `Figure(; size = ...` or `Scene(; size = ...)` instead, which better reflects that this is a unitless size and not a pixel resolution. The key could also come from `set_theme!` calls or related theming functions.
└ @ Makie ~/.julia/packages/Makie/fyNiH/src/scenes.jl:220
ERROR: UndefVarError: `MakieLayout` not defined
Stacktrace:
  [1] add_controls!(fig::Figure, abmobs::ABMObservable{…}, spu::Observable{…})
    @ AgentsVisualizations ~/.julia/packages/Agents/xtlGn/ext/AgentsVisualizations/src/interaction.jl:72
  [2] add_interaction!(fig::Figure, ax::Axis, abmplot::Plot{AgentsVisualizations._abmplot, Tuple{ABMObservable{…}}})
    @ AgentsVisualizations ~/.julia/packages/Agents/xtlGn/ext/AgentsVisualizations/src/interaction.jl:4
  [3] plot!(abmplot::Plot{AgentsVisualizations._abmplot, Tuple{ABMObservable{…}}})
    @ AgentsVisualizations ~/.julia/packages/Agents/xtlGn/ext/AgentsVisualizations/src/abmplot.jl:205
  [4] connect_plot!(parent::Scene, plot::Plot{AgentsVisualizations._abmplot, Tuple{ABMObservable{…}}})
    @ Makie ~/.julia/packages/Makie/fyNiH/src/interfaces.jl:255
  [5] plot!
    @ Makie ~/.julia/packages/Makie/fyNiH/src/interfaces.jl:260 [inlined]
  [6] plot!(ax::Axis, plot::Plot{AgentsVisualizations._abmplot, Tuple{ABMObservable{…}}})
    @ Makie ~/.julia/packages/Makie/fyNiH/src/figureplotting.jl:291
  [7] _create_plot!(::Function, ::Dict{…}, ::Axis, ::ABMObservable{…})
    @ Makie ~/.julia/packages/Makie/fyNiH/src/figureplotting.jl:259
  [8] _abmplot!
    @ ~/.julia/packages/MakieCore/5nuEY/src/recipes.jl:176 [inlined]
  [9] abmplot!(ax::Axis, abmobs::ABMObservable{…}; add_controls::Bool, enable_inspection::Bool, kwargs::@Kwargs{…})
    @ AgentsVisualizations ~/.julia/packages/Agents/xtlGn/ext/AgentsVisualizations/src/abmplot.jl:54
 [10] abmplot!
    @ ~/.julia/packages/Agents/xtlGn/ext/AgentsVisualizations/src/abmplot.jl:49 [inlined]
 [11] #abmplot!#12
    @ ~/.julia/packages/Agents/xtlGn/ext/AgentsVisualizations/src/abmplot.jl:24 [inlined]
 [12] abmplot!
    @ ~/.julia/packages/Agents/xtlGn/ext/AgentsVisualizations/src/abmplot.jl:15 [inlined]
 [13] abmplot(model::UnremovableABM{…}; figure::@NamedTuple{…}, axis::@NamedTuple{}, kwargs::@Kwargs{…})
    @ AgentsVisualizations ~/.julia/packages/Agents/xtlGn/ext/AgentsVisualizations/src/abmplot.jl:10
 [14] abmplot
    @ ~/.julia/packages/Agents/xtlGn/ext/AgentsVisualizations/src/abmplot.jl:3 [inlined]
 [15] abmexploration(model::UnremovableABM{…}; figure::@NamedTuple{…}, axis::@NamedTuple{}, alabels::Nothing, mlabels::Nothing, plotkwargs::@NamedTuple{}, kwargs::@Kwargs{…})
    @ AgentsVisualizations ~/.julia/packages/Agents/xtlGn/ext/AgentsVisualizations/src/convenience.jl:11
 [16] top-level scope
    @ ~/Library/Mobile Documents/com~apple~CloudDocs/Projects/prove julia/schelling.jl:64
Some type information was truncated. Use `show(err)` to see complete types.

Minimal Working Example This is the code I am using:

using Agents, GLMakie, CairoMakie, Random # for reproducibility
# Schelling's model implementation in Julia
@agent SchellingAgent GridAgent{2} begin
    mood::Bool # whether the agent is happy in its position. (true = happy)
    group::Int # The group of the agent, determines mood as it interacts with neighbors
end

function initialize(; total_agents = 320, griddims = (20, 20), min_to_be_happy = 3, seed = 125)
    space = GridSpaceSingle(griddims, periodic = false)
    properties = Dict(:min_to_be_happy => min_to_be_happy)
    rng = Random.Xoshiro(seed)
    model = UnremovableABM(
        SchellingAgent, space;
        properties, rng, scheduler = Schedulers.Randomly()
    )
    # populate the model with agents, adding equal amount of the two types of agents
    # at random positions in the model
    for n in 1:total_agents
        agent = SchellingAgent(n, (1, 1), false, n < total_agents / 2 ? 1 : 2)
        add_agent_single!(agent, model)
    end
    return model
end

model = initialize()

function agent_step!(agent, model)
    minhappy = model.min_to_be_happy
    count_neighbors_same_group = 0
    # For each neighbor, get group and compare to current agent's group
    # and increment `count_neighbors_same_group` as appropriately.
    # Here `nearby_agents` (with default arguments) will provide an iterator
    # over the nearby agents one grid point away, which are at most 8.
    for neighbor in nearby_agents(agent, model)
        if agent.group == neighbor.group
            count_neighbors_same_group += 1
        end
    end
    # After counting the neighbors, decide whether or not to move the agent.
    # If count_neighbors_same_group is at least the min_to_be_happy, set the
    # mood to true. Otherwise, move the agent to a random position, and set
    # mood to false.
    if count_neighbors_same_group ≥ minhappy
        agent.mood = true
    else
        agent.mood = false
        move_agent_single!(agent, model)
    end
    return
end

groupcolor(a) = a.group == 1 ? :blue : :orange
groupmarker(a) = a.group == 1 ? :circle : :rect
# figure, _ = abmplot(model; ac = groupcolor, am = groupmarker, as = 10)
# figure # returning the figure displays it

parange = Dict(:min_to_be_happy => 0:8)
x(agent) = agent.pos[1]
using Statistics: mean
adata = [(:mood, sum), (x, mean)]
alabels = ["happy", "avg. x"]
parange = Dict(:min_to_be_happy => 0:8)

figure, abmobs = abmexploration(
    model;
    agent_step!,dummystep, parange,
    ac = groupcolor, am = groupmarker, as = 10
)

If the code is runnable, it will help us identify the problem faster.

Status `~/.julia/environments/v1.10/Project.toml`
⌃ [46ada45e] Agents v5.17.1

I am on Julia 1.10, and I get the same error both on Mac OSX Sonoma, and on Ubuntu 20.04

Tortar commented 5 months ago

I think you need to update to v5.17.2 to solve the issue. There we have downgraded Makie so that it works fine as before.

manentai commented 5 months ago

thank you @Tortar , the error is resolved on my Mac, but nothing pops up, I just get the info below in console:

(Scene (1200px, 800px):
  1 Plot:
    └ Combined{Makie.tooltip, Tuple{Point{2, Float32}}}
  6 Child Scenes:
    ├ Scene (1200px, 800px)
    ├ Scene (1200px, 800px)
    ├ Scene (1200px, 800px)
    ├ Scene (1200px, 800px)
    ├ Scene (1200px, 800px)
    └ Scene (1200px, 800px), ABMObservable with model:
UnremovableABM with 320 agents of type SchellingAgent
 space: GridSpaceSingle with size (20, 20), metric=chebyshev, periodic=false
 scheduler: Agents.Schedulers.Randomly
 properties: min_to_be_happy
and with data collection:
 adata: nothing
 mdata: nothing)

julia> 

might it be because I am in VSCode?

Tortar commented 5 months ago

I think you need to call figure, otherwise it doesn't show it even if it computed it if I remember correctly

manentai commented 5 months ago

at least in VSCode on a Mac Sonoma, only thing that happens when I call figure is a static plot like in figure, but I cannot move anything nor start the simulation:

Screenshot 2024-01-20 at 18 16 24

am I missing something else maybe?

Tortar commented 5 months ago

I think this is because you are importing CairoMakie, try to change

using Agents, GLMakie, CairoMakie, Random # for reproducibility

in

using Agents, GLMakie, Random # for reproducibility

manentai commented 5 months ago

yes that was it, thanks!