From d7235837e1926cc6dd291959c6b3315abce26c04 Mon Sep 17 00:00:00 2001 From: Tortar <68152031+Tortar@users.noreply.github.com> Date: Wed, 19 Jul 2023 12:54:11 +0200 Subject: [PATCH 1/5] Allow using default values in agents macro --- src/core/agents.jl | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/core/agents.jl b/src/core/agents.jl index e8b48345c4..d47eb9cd33 100644 --- a/src/core/agents.jl +++ b/src/core/agents.jl @@ -91,6 +91,18 @@ mutable struct Baker{T} <: AbstractAgent breadz_per_day::T end ``` +Notice that you can also use default values for some fields, in this case you +will need to specify the field names with the non-default values +```julia +@agent Person{T} GridAgent{2} begin + age::Int = 30 + moneyz::T +end +# default age value +Person(id = 1, pos = (1, 1), moneyz = 2000) +# new age value +Person(1, (1, 1), 40, 2000) +``` ### Example with optional hierarchy An alternative way to make the above structs, that also establishes a user-specific subtyping hierarchy would be to do: @@ -192,7 +204,7 @@ macro agent(new_name, base_type, super_type, extra_fields) expr = quote # Also notice that we escape supertype and interpolate it twice # because this is expected to already be defined in the calling module - mutable struct $name <: $$(esc(super_type)) + @kwdef mutable struct $name <: $$(esc(super_type)) $(base_fields...) $(additional_fields...) end From 83a732604060653b79c78bc87be1d5782b46dc47 Mon Sep 17 00:00:00 2001 From: Tortar <68152031+Tortar@users.noreply.github.com> Date: Wed, 19 Jul 2023 12:56:30 +0200 Subject: [PATCH 2/5] Update model_creation_tests.jl --- test/model_creation_tests.jl | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/test/model_creation_tests.jl b/test/model_creation_tests.jl index ea12ab26dd..bb0ff1c289 100644 --- a/test/model_creation_tests.jl +++ b/test/model_creation_tests.jl @@ -35,6 +35,13 @@ using Test, Agents, Random end @test Fisher <: AbstractHuman @test :fish_per_day ∈ fieldnames(Fisher) + + agent_kwdef = Agent9(id = 1, f2 = 10) + values = (1, 40, 10, 3.0) + @test all(getfield(agent_kwdef, n) == v for (n, v) in zip(fieldnames(Agent9), values)) + agent_kwdef = Agent9(1, 20, 10, 4.0) + values = (1, 20, 10, 4.0) + @test all(getfield(agent_kwdef, n) == v for (n, v) in zip(fieldnames(Agent9), values)) end From b6a315a569ea48c6199c3a8de56db82c46c73787 Mon Sep 17 00:00:00 2001 From: Tortar <68152031+Tortar@users.noreply.github.com> Date: Wed, 19 Jul 2023 12:56:54 +0200 Subject: [PATCH 3/5] Update runtests.jl --- test/runtests.jl | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/test/runtests.jl b/test/runtests.jl index c14cbc631e..e75ab0e183 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -71,6 +71,12 @@ end Agent8(id, pos; f1, f2) = Agent8(id, pos, f1, f2) +@agent Agent9 NoSpaceAgent begin + f1::Int = 40 + f2::Int + f3::Float64 = 3.0 +end + @agent SchellingAgent GridAgent{2} begin mood::Bool group::Int From 9583dbcf7b769eaa738649d757c2e7c4e7416b37 Mon Sep 17 00:00:00 2001 From: Tortar <68152031+Tortar@users.noreply.github.com> Date: Wed, 19 Jul 2023 13:01:00 +0200 Subject: [PATCH 4/5] Update agents.jl --- src/core/agents.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/core/agents.jl b/src/core/agents.jl index d47eb9cd33..264893f014 100644 --- a/src/core/agents.jl +++ b/src/core/agents.jl @@ -29,8 +29,8 @@ as well as any additional ones the user may provide via the `begin` block. See below for examples. Using `@agent` is the recommended way to create agent types for Agents.jl, -however keep in mind that the macro (currently) doesn't work with `Base.@kwdef` -or `const` declarations in individual fields (for Julia v1.8+). +however keep in mind that the macro (currently) doesn't work with `const` +declarations in individual fields (for Julia v1.8+). Structs created with `@agent` by default subtype `AbstractAgent`. They cannot subtype each other, as all structs created from `@agent` are concrete types From c559ed2fa1df3681ea9dfab8c43d07da4b9978a7 Mon Sep 17 00:00:00 2001 From: Tortar <68152031+Tortar@users.noreply.github.com> Date: Wed, 19 Jul 2023 13:04:43 +0200 Subject: [PATCH 5/5] Update CHANGELOG.md --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index a7e361364c..98935a9291 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +# main + +- The `@agents` macro now supports fields with default values. + # v5.17 - New function `replicate!` allows to create a new instance of a given agent at the same position with the possibility to specify some