Agent Based Modeling in Julia


#21

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

#22

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

#23

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:


#24
  • 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.


#25

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.


#26

You can have a look at a follow up post (also for Julia 1.0) here: https://juliasnippets.blogspot.com/2018/08/abm-speed-in-julia.html


#27

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