# Need some help/review on ants model

**URL:** <https://discourse.julialang.org/t/need-some-help-review-on-ants-model/83790>\
**Category:** Modelling & Simulations\
**Tags:** question, plotting\
**Created:** [July 5, 2022, 5:26pm UTC](https://discourse.julialang.org/t/need-some-help-review-on-ants-model/83790 "2022-07-05T17:26:23Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![reyman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/reyman/32/37693_2.png) [@reyman](https://discourse.julialang.org/u/reyman)\
**Post date:** [July 5, 2022, 5:26pm UTC](https://discourse.julialang.org/t/need-some-help-review-on-ants-model/83790/1 "2022-07-05T17:26:23Z")

</div>

Hi there,

Last year i start to replicate the famous “Ants” model using Agents.jl with version 4.x

The result was not bad, see the video and original repo :

> **[GitHub - reyman/julia-antworld-model](https://github.com/reyman/julia-antworld-model/)**
>
> Contribute to reyman/julia-antworld-model development by creating an account on GitHub.

[https://user-images.githubusercontent.com/466329/124834777-95627e00-df80-11eb-8b69-af9cb23716de.mp4](https://user-images.githubusercontent.com/466329/124834777-95627e00-df80-11eb-8b69-af9cb23716de.mp4)

This year, to finish integration of _Julia_ with _Agents.jl_ into OpenMole distributed HPC marketplace examples, i try to update to Agents.jl 5.x

I have some difficulties with evolution of `!step` method :

In my previous code i use step like this to update my graphics : `step!(abmstepper,model, ants_agent_step!, ants_model_step!, 1)`

with

` fig, abmstepper, abmobs = init_fig(model, observable)`

and `init_fig` running abmplot :

```julia
function init_fig(model, observable)

    function ants_marker(b::Ants)
       φ = atan(b.vel[2], b.vel[1]) #+ π/2 + π
       scale(rotate2D(ants_polygon, φ), 2)
    end

    function ants_color(b::Ants)
       return b.color
    end

    fig, abmstepper, abmobs = abmplot(model; ac=ants_color, am=ants_marker)

    ax, hm = heatmap(fig[1,2], observable[1]; colormap= :thermal, nan_color= :red)
    ax.aspect = AxisAspect(1)

    ax2, hm2 = heatmap(fig[2,1], observable[2]; colormap= :thermal, nan_color= :red)
    ax2.aspect = AxisAspect(1)

    ax3, hm3 = heatmap(fig[2,2], observable[3]; colormap= :thermal, nan_color= :red)
    ax3.aspect = AxisAspect(1)

    s = Observable(0) # counter of current step, also observable
    Colorbar(fig[1, 3], hm, width = 20,tellheight=false)
    rowsize!(fig.layout, 1 , ax.scene.px_area[].widths[2])

    return (fig, abmstepper, abmobs)

end

```

So the first update here was to understand if it’s possible to do the same thing with agents.jl v5, because `!step` signature change.

I also need some info of Agent.js dev to know if there is a more simple way to manage the double step for grid space : continuous (ants move) and discrete (food patches).

Actually i use a double step method to manage that :

```julia
     step!(abmstepper, model, ants_agent_step!, ants_model_step!, 1)
            step!(model.sugar_model, sugar_agent_step!, sugar_model_step!, 1)

```

I’m very interested to push a documented / updated version of this model, already implemented in python and netlogo) to use on HPC with OpenMOLE.

---

<div class="post-metadata">

**Author:** ![Datseris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/datseris/32/13406_2.png) [@Datseris](https://discourse.julialang.org/u/Datseris)\
**Post date:** [July 5, 2022, 6:49pm UTC](https://discourse.julialang.org/t/need-some-help-review-on-ants-model/83790/2 "2022-07-05T18:49:21Z")

</div>

Yes it is very much possible to do animated composite plots, see here [Plotting and Interactivity · Agents.jl](https://juliadynamics.github.io/Agents.jl/stable/agents_visualizations/#Creating-custom-ABM-plots-1)

Obviously in your case you won’t have the full interactive GUI as the left window, but rather an animatable plot.

---

<div class="post-metadata">

**Author:** ![reyman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/reyman/32/37693_2.png) [@reyman](https://discourse.julialang.org/u/reyman)\
**Post date:** [July 6, 2022, 9:54pm UTC](https://discourse.julialang.org/t/need-some-help-review-on-ants-model/83790/3 "2022-07-06T21:54:31Z")

</div>

Now that i’m ready to use latest version of Cairo + Agents with julia 1.7 🙂 i try to change the methods.

Into my `init_fig()` method i set abmplot like this, by passing the corresponding `ants_xx_step!` :

```julia

function init_fig(model, observable)

    ...
    fig, abmstepper, abmobs = abmplot(model; ants_agent_step!, ants_model_step!, am= ants_marker, ac=ants_color, figure=(;resolution=(800,800)))
    ...   
    return (fig, abmstepper, abmobs)

```

Called like that :

```julia
fig, abmstepper, abmobs = init_fig(model, observable)

```

And i change the step! into main loop function :

```julia
  while Agents.until(s, rununtil, model)
            print("sugar remaining on world = $(sum(model.sugar_model.sugar_landscape)) \n")
            recordframe!(io) # save current state
            if Agents.should_we_collect(s, model, true)
                Agents.collect_agent_data!(df_agent, model, adata, s)
            end
            if Agents.should_we_collect(s, model.sugar_model, true)
                Agents.collect_model_data!(df_model, model.sugar_model, mdata, s)
            end
        
            step!(abmobs, 1) # change here 
            step!(model.sugar_model, sugar_agent_step!, sugar_model_step!, 1)
            s += 1

        ...
        end

```

With

```julia
function ants_model_step!(model)
    model.tick += 1
end

```

But my ant model doesn’t step and run indifinetly, so something missing …

* * *

**Update 2**

I test only with this and it’s work, so it’s a problem with model calling with `Observables` stepping ?

```julia
step!(model,ants_agent_step!,ants_model_step!,1) #ants stepping
step!(model.sugar_model, sugar_agent_step!, sugar_model_step!, 1) #sugar stepping
            

```

* * *

**Update 3**

I continue to debug to understand @Datseris, doing this works :

```julia
abmobs = ABMObservable(model; agent_step! = ants_agent_step!, model_step! = ants_model_step!)

while Agents.until(s, rununtil, model)
    ....
    step!(abmobs,1)
    step!(model.sugar_model, sugar_agent_step!, sugar_model_step!, 1)
end

```

That’s work, so is there a problem in ambplot `ABMObservable` returned and `step!` ?

* * *

**Update 4**

I found the problem i think, the abmplot doesn’t trigger an error, even if the syntax is not correct …

**BAD**

```julia
 fig, abmstepper, abmobs = abmplot(model; ants_agent_step!, ants_model_step!, am= ants_marker, ac=ants_color, figure=(;resolution=(800,800)))

```

**GOOD**

```julia
 fig, abmstepper, abmobs = abmplot(model; agent_step! = ants_agent_step!, model_step! = ants_model_step!, am= ants_marker, ac=ants_color, figure=(;resolution=(800,800)))

```

---

<div class="post-metadata">

**Author:** ![reyman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/reyman/32/37693_2.png) [@reyman](https://discourse.julialang.org/u/reyman)\
**Post date:** [July 8, 2022, 9:37am UTC](https://discourse.julialang.org/t/need-some-help-review-on-ants-model/83790/4 "2022-07-08T09:37:05Z")

</div>

I push a first iteration of README doc and the updated model here : [GitHub - reyman/julia-antworld-model](https://github.com/reyman/julia-antworld-model)

---

<div class="post-metadata">

**Author:** ![Datseris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/datseris/32/13406_2.png) [@Datseris](https://discourse.julialang.org/u/Datseris)\
**Post date:** [July 9, 2022, 9:14am UTC](https://discourse.julialang.org/t/need-some-help-review-on-ants-model/83790/5 "2022-07-09T09:14:29Z")

</div>

you should open a PR at AgentsExampleZoo.jl which adds this example. There it is much simpelr to give reviews to code.
