JuliaDynamics / Agents.jl

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

Set resolution for abmplot to have pixel perfect plots #932

Open gasparalfaro opened 2 years ago

gasparalfaro commented 2 years ago

I want to plot an Agent Based Model with each agent being one square of color so that the plot doesn't have any white space. To do so I have set the shape of the agent as a :rect and chosen a size for the marker. I imagine that if I set the resolution of the plot to the number of agents in each side multiplied by the size of each agent, no white space should be visible and the markers won't overlap each other.

As model I have an AgentBasedModel with (50,50) grid dimension and each Gamer has a propertie named strategy that controls the color.

using Agents
using Random

L=50

mutable struct Gamer <: AbstractAgent
    id::Int
    pos::Tuple{Int, Int}
    strategy::Int
end

Base.@kwdef mutable struct Properties

end

function initialize(; seed = 1234, grid_dims = (L,L), kwargs...)
    rng = Random.MersenneTwister(seed)
    properties = Properties(;kwargs...)
    space = GridSpace(grid_dims; periodic = true, metric = :euclidean)
    # space = GridSpace(grid_dims; periodic = true, metric = :manhattan)
    scheduler = Schedulers.Randomly()
    model = AgentBasedModel(Gamer, space; properties, rng, scheduler)
    function how_to_fill(pos)
        x, y = pos # these are the x,y coordinates
        strategy = rand(model.rng, 1:3)
        return (strategy)
    end
    fill_space!(model, how_to_fill)
    return model
end

model = initialize()

I'm not able to change the resolution for abmplot. It throws ERROR: setting resolution for scene via plot attribute not supported anymore

using InteractiveDynamics, GLMakie
strategy_to_color = Dict(1=>:slateblue, 2=>:red2, 3=>:limegreen)

ac(a) = strategy_to_color[a.strategy]
as=5
am=:rect

plotkwargs = (;
    ac,as,am)

fig, = abmplot(model; (resolution=(250,250)),plotkwargs...)
display(fig)

How could I set the resolution instead? Or, is there any other way to get a plot with a square grid of each agent as a colored square without any white space?

I have tried setting a custom heatmap but the moment one of the strategies with largest or smaller number disappears, the colors changes.

Datseris commented 2 years ago

You're better off using a matrix to represent the properties instead of agents. Seems like you're making a cellular automaton. See e.g. https://juliadynamics.github.io/AgentsExampleZoo.jl/dev/examples/forest_fire/

In Agents.jl version 5.4 you're better of using GridSpaceSingle. I think I'll make this the way that GridSpaceSingle is plotted, which is a grid space that allows at most one agent per position.