Code Monkey home page Code Monkey logo

Comments (8)

Datseris avatar Datseris commented on June 9, 2024

This is great. This is truly great. The only thing we need to make sure is that add_agent! plays well with it and works as expected, so that

add_agent!(Wolf, only_wolf_fields...)

does what it should and initializes an Animal with correct id, pos, and type.

SHould we add this option for v6, or you want to do it later? The benefit of doing it at v6 would be that this rather important perforamnce feature would be included in the v6 announcement post.

from agents.jl.

Datseris avatar Datseris commented on June 9, 2024

You are right, this doesn't totally resolve the multi-agent "problem". But it gives a very important option of having only one agent type. I think in the majority of use cases this option would have performance gains.

from agents.jl.

Tortar avatar Tortar commented on June 9, 2024

It would be cool to have this in v6 I agree, but it needs some more work, so I don't promise to finish it up in the very near future :D

from agents.jl.

mastrof avatar mastrof commented on June 9, 2024

This looks great. Haven't tried to clone the branch for testing, but if Wolf and Hawk are not defined as types, does this mean that instead of dispatching as f(::Wolf) and f(::Hawk) one would always need to check against Animal.type following a structure of the form f(a::Animal) = a.type == :hawk ? f_hawk(a) : f_wolf(a)?

I wonder, if it would make sense to dispatch on the value of type? E.g. f(::Val{:hawk}), f(::Val{:wolf})... I'm a bit clueless here, would this have negative performance impacts?

from agents.jl.

Tortar avatar Tortar commented on June 9, 2024

I know you like macros @mastrof :D

I think that it is an interesting idea, it will costs more because this is dynamic dispatch vs branching and branching wins, but in reality the real perf benefit here is to avoid abstract containers, so it will not change much adding that for only agent_step!, I will try it out on the benchmark I re-setuped for testing the macro on the PR.

from agents.jl.

Tortar avatar Tortar commented on June 9, 2024

tried:

Seems fine to me

branching:

561.657 ms (23076750 allocations: 1001.82 MiB)

with Val

589.242 ms (23076750 allocations: 1001.82 MiB)

(Referring to branching_faster_than_dispatch.jl file)

Mod Version:

function agent_step!(agent::GridAgentAll, model2)
    agent_step!(agent, model2, Val(agent.type))
end
agent_step!(agent, model2, ::Val{:gridagentone}) = randomwalk!(agent, model2)
function agent_step!(agent, model2, ::Val{:gridagenttwo})
    agent.one += rand(abmrng(model2))
    agent.two = rand(abmrng(model2), Bool)
end
function agent_step!(agent, model2, ::Val{:gridagentthree})
    if any(a-> a.type == :gridagenttwo, nearby_agents(agent, model2))
        agent.two = true
        randomwalk!(agent, model2)
    end
end
function agent_step!(agent, model2, ::Val{:gridagentfour})
    agent.one += sum(a.one for a in nearby_agents(agent, model2))
end
function agent_step!(agent, model2, ::Val{:gridagentfive})
    targets = filter!(a->a.one > 1.0, collect(nearby_agents(agent, model2, 3)))
    if !isempty(targets)
        idx = argmax(map(t->euclidean_distance(agent, t, model2), targets))
        farthest = targets[idx]
        walk!(agent, sign.(farthest.pos .- agent.pos), model2)
    end
end
function agent_step!(agent, model2, ::Val{:gridagentsix})
    agent.eight += sum(rand(abmrng(model2), (0, 1)) for a in nearby_agents(agent, model2))
end

from agents.jl.

Tortar avatar Tortar commented on June 9, 2024

But if you do something like this inside the step itself I expect it will have a noticeable impact on performance

from agents.jl.

Tortar avatar Tortar commented on June 9, 2024

Actually I created a package which does more than what it is described here and it could allow memory efficiency to approach the one of multiple types in many cases! It is at https://github.com/Tortar/MixedStructTypes.jl if you want to take a look

from agents.jl.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.