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

Update to v0.2 of MixedStructTypes #972

Merged
merged 6 commits into from
Feb 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ JLD2 = "0.4"
LazyArtifacts = "1.3.0"
LightOSM = "0.2.0"
MacroTools = "0.5"
MixedStructTypes = "0.1"
MixedStructTypes = "0.2"
ProgressMeter = "1.5"
Rotations = "1.3"
Scratch = "1"
Expand Down
31 changes: 23 additions & 8 deletions src/core/agents.jl
Original file line number Diff line number Diff line change
Expand Up @@ -296,32 +296,47 @@ macro multiagent(version, struct_repr)
@capture(a_spec, @agent astruct_spec_)
int_type, new_fields = decompose_struct(astruct_spec)
push!(agent_specs_with_base,
:(mutable struct $int_type
:(@kwdef mutable struct $int_type
$(base_fields...)
$(new_fields...)
end))
end
t = :($new_type <: $abstract_type)
@capture(new_type, _{new_params__})
new_params === nothing && (new_params = [])
c = @capture(new_type, new_type_n_{new_params__})
if c == false
new_type_n = new_type
new_params = []
end
new_params_no_constr = [p isa Expr && p.head == :(<:) ? p.args[1] : p for p in new_params]
new_type_no_constr = :($new_type_n{$(new_params_no_constr...)})
a_specs = :(begin $(agent_specs_with_base...) end)
if version == QuoteNode(:opt_speed)
expr = quote
MixedStructTypes.@compact_struct_type @kwdef $t $a_specs
Agents.ismultiagentcompacttype(::Type{$(new_type)}) where {$(new_params...)} = true
MixedStructTypes.@compact_structs $t $a_specs
Agents.ismultiagentcompacttype(::Type{$(namify(new_type))}) = true
end
elseif version == QuoteNode(:opt_memory)
expr = quote
MixedStructTypes.@sum_struct_type @kwdef $t $a_specs
Agents.ismultiagentsumtype(::Type{$(new_type)}) where {$(new_params...)} = true
MixedStructTypes.@sum_structs $t $a_specs
Agents.ismultiagentsumtype(::Type{$(namify(new_type))}) = true
end
else
error("The version of @multiagent chosen was not recognized, use either :opt_speed or :opt_memory instead.")
end

expr_multiagent = :(Agents.ismultiagenttype(::Type{$(namify(new_type))}) = true)
if new_params != []
expr_multiagent_p = :(Agents.ismultiagenttype(::Type{$(new_type)}) where {$(new_params...)} = true)
if version == QuoteNode(:opt_speed)
expr_multiagent_p = quote
Agents.ismultiagenttype(::Type{$(new_type_no_constr)}) where {$(new_params...)} = true
Agents.ismultiagentcompacttype(::Type{$(new_type_no_constr)}) where {$(new_params...)} = true
end
else
expr_multiagent_p = quote
Agents.ismultiagenttype(::Type{$(new_type_no_constr)}) where {$(new_params...)} = true
Agents.ismultiagentsumtype(::Type{$(new_type_no_constr)}) where {$(new_params...)} = true
end
end
else
expr_multiagent_p = :()
end
Expand Down
31 changes: 31 additions & 0 deletions test/api_tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,28 @@ abstract type AbstractE <: AbstractAgent end
end
end

@multiagent :opt_speed struct MultiSchelling1{X}(GridAgent{2})
@agent struct Civilian1 # can't re-define existing `Schelling` name
mood::Bool = false
group::Int
end
@agent struct Governor1{X<:Real} # can't redefine existing `Politician` name
group::Int
influence::X
end
end

@multiagent :opt_memory struct MultiSchelling2{X}(GridAgent{2})
@agent struct Civilian2 # can't re-define existing `Schelling` name
mood::Bool = false
group::Int
end
@agent struct Governor2{X<:Real} # can't redefine existing `Politician` name
group::Int
influence::X
end
end

@testset "@multiagent macro" begin

hawk_1 = Hawk(1, (1, 1), 1.0, 2.0, 3)
Expand Down Expand Up @@ -457,4 +479,13 @@ end
@test kindof(g) == :G
@test E <: AbstractE && E <: AbstractE
@test f isa E && g isa E


civ = Civilian1(; id = 2, pos = (2, 2), group = 2)
gov = Governor1(; id = 3 , pos = (2, 2), group = 2, influence = 0.5)
civ = Civilian2(; id = 2, pos = (2, 2), group = 2)
gov = Governor2(; id = 3 , pos = (2, 2), group = 2, influence = 0.5)

@test_throws "" Governor1(; id = 3 , pos = (2, 2), group = 2, influence = im)
@test_throws "" Governor2(; id = 3 , pos = (2, 2), group = 2, influence = im)
end
Loading