Agent Based Modeling in Julia

I tried forest1.jl in JuliaBox. I could not find anywhere the package Random. Running the script without using Random I got the following error:

UndefVarError: findall not defined

Stacktrace:
 [1] go(::Array{Int64,2}) at ./In[1]:7
 [2] go_repeat(::Float64) at ./In[1]:24
 [3] collect(::Base.Generator{UnitRange{Int64},##5#6}) at ./array.jl:475

The scripts are for Julia 0.7/1.0. Unfortunately a lot of changes since Julia 0.6 are introduced (you are probably using it). Here is an example how you can rewrite the first code to run under Julia 0.6.4:

function setup(density)
    [x == 1 ? 2 : rand() < density ? 1 : 0 for x in 1:251, y in 1:251]
end

function go(grid)
    any(x -> x == 2, grid) || return true
    for pos in shuffle!(find(x -> x == 2, grid))
        x, y = ind2sub(grid, pos)
        for (dx, dy) in ((0, 1), (0, -1), (1, 0), (-1, 0))
            nx, ny = x + dx, y + dy
            if all((0,0) .< (nx, ny) .≤ size(grid)) && grid[nx, ny] == 1
                grid[nx, ny] = 2
            end
        end
        grid[pos] = 3
    end
    return false
end

function go_repeat(density)
    grid = setup(density)
    init_green = count(x -> x == 1, grid)
    while true
        go(grid) && return count(x -> x == 3, grid) / init_green * 100
    end
end

@time [go_repeat(0.55) for i in 1:100];
@time [go_repeat(0.75) for i in 1:100];

The biggest differences in the code are:

  • different approach to find functions
  • different approach to matrix indexing
1 Like

Thank you. Now it works fine in JuliaBox (Julia 0.6.2).
I will try the original version of forest1.jl in Julia 1.0.
What about the package Random? Is it not necessary?
I tried with 0.7. It works but gives the following warning:

WARNING: Base.shuffle! is deprecated: it has been moved to the standard library package `Random`.
Add `using Random` to your imports.

I tried also with 1.0 and it gives the following error:
UndefVarError: shuffle! not defined

But I know that I am not saying nothing new :slight_smile:

  • In Julia 0.6 shuffle! is imported from Random by default so you do not have to write anything;
  • In Julia 0.7 you should import it using using Random or a warning will be thrown;
  • In Julia 1.0 you must use using Random or an error will be thrown as shuffle! is not imported by default.

Julia 0.7 is a kind of “in transition” version of Julia - having the same functionality as 1.0 but trying to print warnings where possible to make life easier for people migrating the code from 0.6 to 1.0.

1 Like

Yes, in Julia 1.0 when I use using Random the script runs fine. I will try with the other forest scripts in Julia 1.0.

You can have a look at a follow up post (also for Julia 1.0) here: Julia snippets: ABM speed in Julia

1 Like

Yes I saw it today but I decided to read (give it another try :slight_smile: ) the first one first.

For googlers regarding performance:

By specifying the union type for mixed such as mixed = Union{B, C}[a;b], the performance becomes the same as for the same arrays :blush:

Yes - as long as the Union is not larger than 4 types.

1 Like

if it is, is there graceful degradation or a dropoff?

It is a cut off. For larger Unions Julia decides to do dynamic dispatch (treating them as if they were collections of Any)

1 Like

I have recently developed a package for ABM: Agents.

5 Likes

I will try to use Agents.jl with great interest.

Why do you think you are having difficulties to have more contributors to a most important theme like ABM?

Are you referring to the number of contributors to this package? I have only registered it a few days ago. Hopefully, people will find it useful and contribute to it.

1 Like

Yes. I hope so too.
I have more experience with NetLogo but I have a great interest in Julia.

Are you aware of Mesa packages like HARK? Maybe it is too soon to talk about ABM in Economics in Julia (sure we have QuantEcon and other packages).

Agents has already most of the functionalities of Mesa. But I am not familiar with other Mesa packages. Nevertheless, it should be easy to add more functionalities to Agents, since it is in Julia.

1 Like

OK. I am trying your two examples in Jupyter. I think it would be interesting to have a demo of Agents.jl in Jupyter format.

1 Like

HARK is not really an ABM package as the focus is exclusively on behavior coming from optimizing behavior, and more specifically models whose solutions can be characterized by Bellman equations. This means a lot wrt the tools it tries to expose and support.

signed,
contributor to HARK :slight_smile:

2 Likes

Maybe we should discuss some of the CA components you are using. I’m mid way through writing this:

Which is a base framework and bunch of visualisation outputs (like Interact/Blink, even REPL :wink: ) for writing more specific simulation packages like this:

Apologies the docs aren’t updating at the moment, so they’re a bit out of date.

1 Like

Sure, I will check your packages.