Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow using default values in agents macro #833

Merged
merged 5 commits into from
Jul 21, 2023
Merged

Allow using default values in agents macro #833

merged 5 commits into from
Jul 21, 2023

Conversation

Tortar
Copy link
Member

@Tortar Tortar commented Jul 19, 2023

This tries to split in two parts #831 since the part with the consts variable is more hacky, but using default values is very easy and we can support it almost with no changes

@codecov-commenter
Copy link

codecov-commenter commented Jul 19, 2023

Codecov Report

Merging #833 (c559ed2) into main (489c24d) will not change coverage.
The diff coverage is n/a.

@@           Coverage Diff           @@
##             main     #833   +/-   ##
=======================================
  Coverage   70.13%   70.13%           
=======================================
  Files          42       42           
  Lines        2719     2719           
=======================================
  Hits         1907     1907           
  Misses        812      812           
Impacted Files Coverage Δ
src/core/agents.jl 100.00% <ø> (ø)

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

@Tortar
Copy link
Member Author

Tortar commented Jul 19, 2023

@fbanning I'd like to ask you what do you think about the pr since I saw you opened the linked issue :-)

@fbanning
Copy link
Member

To me this looks as clean and straightforward as possible.

@fbanning
Copy link
Member

Did you run any performance checks on the @agent macro with @kwdef added to it? I don't think it should result in any worse performance but would be good to check for that. I couldn't find any comments on the performance impact of using the @kwdef macro in a quick internet search.

@Tortar
Copy link
Member Author

Tortar commented Jul 21, 2023

yes, tried to see if there were any regression with micro-benchmarks on agent creation and there wasn't any. Thanks for the review anyway, I merge then!

@Tortar Tortar merged commit de57ea3 into main Jul 21, 2023
@Tortar Tortar deleted the default_values branch July 21, 2023 17:59
@Datseris
Copy link
Member

wow what a big improvement! 🎉

@Datseris
Copy link
Member

does this impact in any way add_agent!(pos, model, args...; kwargs....)? No, right?

Actually, I don't know. How does this work...?

@@ -35,6 +35,13 @@ using Test, Agents, Random
end
@test Fisher <: AbstractHuman
@test :fish_per_day ∈ fieldnames(Fisher)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here definitely more tests should have been added to see how this works with add_agent! that automatically creates agents.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, it is actually something I didn't consider when doing the pr :D, it seems that it shouldn't have any effect on the version with args, I'm also pretty sure that currently if we pass anything in kwargs it will throw error (even before this pr)

Copy link
Member Author

@Tortar Tortar Jul 22, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But we can support both ways with:

function add_agent!(
    pos::ValidPos,
    A::Type{<:AbstractAgent},
    model::ABM,
    properties::Vararg{Any, N};
    kwproperties...,
) where {N}
    id = nextid(model)
    if isempty(kwproperties)
        newagent = A(id, pos, properties...)
    else
        newagent = A(; id = id, pos = pos, kwproperties...)
    end
    add_agent_pos!(newagent, model)
end

It should be as fast as before since the branch prediction is pretty easy so the compiler should be able to infer what to do, but we should probably verify it

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

indeed I don't see any meaningful difference in our model comparisons

# before this modification
Agents.jl WolfSheep-small (ms): 2.8282849999999997
Agents.jl WolfSheep-large (ms): 99.588342
Agents.jl Flocking-small (ms): 9.377578999999999
Agents.jl Flocking-large (ms): 67.284021
Agents.jl Schelling-small (ms): 0.453293
Agents.jl Schelling-large (ms): 4.035073
Agents.jl ForestFire-small (ms): 0.767832
Agents.jl ForestFire-large (ms): 16.152233

# after this modification
Agents.jl WolfSheep-small (ms): 2.82549
Agents.jl WolfSheep-large (ms): 99.25996699999999
Agents.jl Flocking-small (ms): 8.799429
Agents.jl Flocking-large (ms): 67.878292
Agents.jl Schelling-small (ms): 0.455848
Agents.jl Schelling-large (ms): 4.059518
Agents.jl ForestFire-small (ms): 0.776428
Agents.jl ForestFire-large (ms): 16.248959

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants