Agents.jl: Interactive plotting - agent traits controllable on a slider

I want to generate an interactive plot for the wolf-sheep model. I want to make it so that agent traits can be controlled on an interactive slider, for example:
the initial number of sheep (n_sheep) vary between 1 and 20
the probability of sheep reproducing (sheep_reproduce) vary between 0.01 and 0.1 by 0.01 intervals

From what I can see in the “Interactive plotting” tutorial, only model properties can be made changeable on a slider.

I tried the following (from the interactive plotting tutorial):

params = Dict(
:n_sheep => 1:20,
)

fig, ax, abmobs = abmplot(sheepwolfgrass;
agent_step! = sheepwolf_step!,
model_step! = grass_step!,
params,
plotkwargs…)
fig

And n_sheep shows up as a slider but when you push the “update” button it gives the error: KeyError: key "n_sheep" not found.

I then added n_sheep = n_sheep to properties, but that didn’t work either.

My question: how to you add agent traits such as sheep_reproduce or n_sheep (not strictly an agent trait, but not a model property either) as sliders when generating interactive plots?

Any advice would be greatly appreciated!

The Dict key is a Symbol, with a preceding colon. Check that you’re using that for your lookup key?

Yep I am. When I include :n_sheep in properties, pressing the “update” button no longer produces the KeyError: key "n_sheep" not found error, but the initial number of sheep doesn’t change either (the button does nothing).

As you can see here the n_sheep property is only used during initialisation of the model. When you use the interactive application provided by InteractiveDynamics.jl, you already provide an existing model to the plotting function, e.g. fig, ax, abmobs = abmplot(sheepwolfgrass; agent_step! = sheepwolf_step!, model_step! = grass_step!, plotkwargs...). This means that whenever you click the reset model button, you just recreate the exact same model that you’ve had when you started the interactive app. :slight_smile: (That’s also the reason why the button is called “reset model” and not “new model” or something similar.)

What you want to do is to create a whole new model on the fly and replace it in the interactive plot. That’s not possible right now. Instead you can just as easily create a new model in the REPL and run the abmplot function again. It’s almost instantaneous so there’s no real downside to it except that you don’t do it via a graphical interface.

Having said that, your expectations regarding the functionality make sense to me. I’ll have a look if I can find the time to implement it.

3 Likes

Thank you for the clear explanation, that makes sense and I agree it is not too difficult to launch a new instance of the model.

If that functionality could be implemented that would be great! Being able to instantaneously tweak initial conditions and see how the model responds would be helpful for model debugging/testing.

Thank you for the fantastic tools!