JuliaDynamics / Agents.jl

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

Schelling interactive exploration example ERROR: LoadError: UndefVarError: `MakieLayout` not defined #948

Closed arisliang closed 6 months ago

arisliang commented 7 months ago

Describe the bug LoadError: UndefVarError: MakieLayout not defined for Schelling interactive exploration example

Minimal Working Example

"""
https://juliadynamics.github.io/Agents.jl/stable/examples/schelling/

Minimum example of Schelling's exploration example
"""

using Agents

mutable struct Schelling <: AbstractAgent
    id::Int
    pos::NTuple{2,Int}
    group::Int
    mood::Bool
end

function initialize(; N=320, M=20, min_to_be_happy=3)
    # 1. space
    space = GridSpaceSingle((M, M); periodic=false)
    @info "Created gridspace $space"

    # 3. model
    properties = Dict(:min_to_be_happy => min_to_be_happy)
    model = UnremovableABM(Schelling, space; properties)
    @info "Created model $model"

    # add agents
    for n in 1:N
        agent = Schelling(n, (1, 1), n < N / 2 ? 1 : 2, false)
        add_agent_single!(agent, model)
    end
    @info "Added $N agents to the model"
    return model
end

function agent_step!(agent::Schelling, model::AgentBasedModel)
    agent.mood && return

    nearby_same = 0

    for neighbor in nearby_agents(agent, model)
        if agent.group == neighbor.group
            nearby_same += 1
        end
    end

    if nearby_same ≥ model.min_to_be_happy
        agent.mood = true
    else
        move_agent_single!(agent, model)
    end
    return
end

using GLMakie # using a different plotting backend that enables interactive plots
using Statistics: mean

groupcolor(a) = a.group == 1 ? :blue : :orange
groupmarker(a) = a.group == 1 ? :circle : :rect
x(agent) = agent.pos[1]

function explore_model()
    adata = [(:mood, sum), (x, mean)]
    alabels = ["happy", "avg. x"]

    model = initialize(; N=300) # fresh model, noone happy
    parange = Dict(:min_to_be_happy => 0:8)

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

explore_model()

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

Agents.jl version v5.17.1

Tortar commented 7 months ago

Hi @arisliang

I think the simplest solution is to update to the v6 version of the library, it is still unreleased, so you need to use ]dev Agents to install it...actually you need to wait till #949 is resolved (just if you want to use deprecated methods, which is not good anyway :D) which should happen quickly, I verified it works after merging that pr

The breakage is probably due to the release of Makie 0.20

arisliang commented 6 months ago

@Tortar thank you very much for pointing to the right direction.

While waiting for the v6 to be out (current latest v5.17.1 is released half year ago), is there any previous version whose plotting doesn't have this issue?

Assume we don't need the latest features from either Agents nor any of its dependencies.

Tortar commented 6 months ago

To solve this issue on previous versions it woul be enough to specify in project.toml that Makie 0.19 is required.

However, I'm not sure at the moment how to backport only some commits without doing something drastic: e.g. move all the other commits to a new branch (v6 branch) and add this one and release a new version.

Maybe there is a way to specify to registrator to pull from a branch different from main where we add only this commit?

Maybe your expertise with release cycles @Datseris could help in this?

Tortar commented 6 months ago

Actually the ui for registering includes choosing a branch https://juliahub.com/ui/Packages, so we can probably add a new branch with commits until 5.17.1 + a new commit fixing the problem and release it, not entirely sure this is the right procedure though because I've never done it

Datseris commented 6 months ago

Actually the ui for registering includes choosing a branch https://juliahub.com/ui/Packages, so we can probably add a new branch with commits until 5.17.1 + a new commit fixing the problem and release it, not entirely sure this is the right procedure though because I've never done it

Yes, that is correct.

Tortar commented 6 months ago

Now it should be good, solved by adding dependencies at compatible versions in https://github.com/JuliaDynamics/Agents.jl/compare/main...fix-agents-again, your example now works with the released version

arisliang commented 6 months ago

Thanks for the fix.

The fix needs v5.17.2 right? A bit confused on how to update it, is it directly ]update? Unfortunately it's not updating to v5.17.2 in this way.

When use ]status --outdated -m, it shows:

⌃ [46ada45e] Agents v5.17.1 (<v5.17.2)

A bit confused by this status. On one hand, it shows Agents is upgradable to v5.17.2, on the other, it's not updating when use ]update.

After another addition ]add Makie@0.19, Agents is upgraded to v5.17.2.

Tortar commented 6 months ago

Yes, I think that it doesn't update automatically because it needs to downgrade Makie, but this is strange to me too, I thought it would have done it automatically actually. Maybe if you activate an environment it will work