Skip to content

Commit 6c080da

Browse files
committed
Apply style to src
1 parent 14d869a commit 6c080da

34 files changed

+533
-562
lines changed

src/Agents.jl

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -49,35 +49,35 @@ include("deprecations.jl")
4949
using Scratch
5050

5151
function __init__()
52-
display_update = true
53-
version_number = "5.7"
54-
update_name = "update_v$(version_number)"
55-
update_message = """
56-
Update message: Agents v$(version_number)
57-
Welcome to this new update of Agents.jl!
52+
display_update = true
53+
version_number = "5.7"
54+
update_name = "update_v$(version_number)"
55+
update_message = """
56+
Update message: Agents v$(version_number)
57+
Welcome to this new update of Agents.jl!
5858
59-
Noteworthy changes:
59+
Noteworthy changes:
6060
61-
- Internals of `AgentBasedModel` got reworked.
62-
It is now an abstract type, defining an abstract interface that concrete
63-
implementations may satisfy. This paves the way for flexibly defining new
64-
variants of `AgentBasedModel` that are more specialized in their applications.
65-
- The old `AgentBasedModel` is now `StandardABM`
66-
Nothing is breaking because the call to `AgentBasedModel` gives `StandardABM`.
67-
- Two new variants of agent based models: `UnkillableABM` and `FixedMassABM`.
68-
They yield huge performance benefits **(up to twice the speed!!!)**
69-
on iterating over agents if the agents can't get killed, or even added,
70-
during model evolution!
71-
"""
61+
- Internals of `AgentBasedModel` got reworked.
62+
It is now an abstract type, defining an abstract interface that concrete
63+
implementations may satisfy. This paves the way for flexibly defining new
64+
variants of `AgentBasedModel` that are more specialized in their applications.
65+
- The old `AgentBasedModel` is now `StandardABM`
66+
Nothing is breaking because the call to `AgentBasedModel` gives `StandardABM`.
67+
- Two new variants of agent based models: `UnkillableABM` and `FixedMassABM`.
68+
They yield huge performance benefits **(up to twice the speed!!!)**
69+
on iterating over agents if the agents can't get killed, or even added,
70+
during model evolution!
71+
"""
7272

73-
if display_update
74-
# Get scratch space for this package
75-
versions_dir = @get_scratch!("versions")
76-
if !isfile(joinpath(versions_dir, update_name))
77-
printstyled(stdout, "\n"*update_message; color=:light_magenta)
78-
touch(joinpath(versions_dir, update_name))
73+
if display_update
74+
# Get scratch space for this package
75+
versions_dir = @get_scratch!("versions")
76+
if !isfile(joinpath(versions_dir, update_name))
77+
printstyled(stdout, "\n" * update_message; color=:light_magenta)
78+
touch(joinpath(versions_dir, update_name))
79+
end
7980
end
80-
end
8181
end # _init__ function.
8282

8383
end # module

src/core/agents.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ macro agent(new_name, base_type, extra_fields)
213213
# `super_type = AbstractAgent`. This requires us to disable 'macro hygiene', see here
214214
# for a brief explanation of the potential issues with this:
215215
# https://discourse.julialang.org/t/calling-a-macro-from-within-a-macro-revisited/19680/16?u=fbanning
216-
esc(quote
216+
return esc(quote
217217
Agents.@agent($new_name, $base_type, Agents.AbstractAgent, $extra_fields)
218218
end)
219219
end

src/core/higher_order_iteration.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ e.g. `(agent1, agent7, agent8)`. `order` must be larger than `1` but has no uppe
1111
Index order is provided by the model scheduler by default,
1212
but can be altered with the `scheduler` keyword.
1313
"""
14-
iter_agent_groups(order::Int, model::ABM; scheduler = model.scheduler) =
14+
iter_agent_groups(order::Int, model::ABM; scheduler=model.scheduler) =
1515
Iterators.product((map(i -> model[i], scheduler(model)) for _ in 1:order)...)
1616

1717
"""
@@ -38,7 +38,7 @@ map_agent_groups(order::Int, f::Function, model::ABM, filter::Function; kwargs..
3838
index_mapped_groups(order::Int, model::ABM, filter::Function; scheduler = Schedulers.by_id)
3939
Return an iterable of agent ids in the model, meeting the `filter` criteria if used.
4040
"""
41-
index_mapped_groups(order::Int, model::ABM; scheduler = Schedulers.by_id) =
41+
index_mapped_groups(order::Int, model::ABM; scheduler=Schedulers.by_id) =
4242
Iterators.product((scheduler(model) for _ in 1:order)...)
43-
index_mapped_groups(order::Int, model::ABM, filter::Function; scheduler = Schedulers.by_id) =
43+
index_mapped_groups(order::Int, model::ABM, filter::Function; scheduler=Schedulers.by_id) =
4444
Iterators.filter(filter, Iterators.product((scheduler(model) for _ in 1:order)...))

src/core/model_abstract.jl

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,9 @@ ValidPos = Union{
2222
Int, # graph
2323
NTuple{N,Int}, # grid
2424
NTuple{M,<:AbstractFloat}, # continuous
25-
Tuple{Int,Int,Float64} # osm
25+
Tuple{Int,Int,Float64}, # osm
2626
} where {N,M}
2727

28-
2928
"""
3029
AgentBasedModel
3130
@@ -81,12 +80,12 @@ Here we the most important information on how to query an instance of `AgentBase
8180
8281
Many more functions exist in the API page, such as [`allagents`](@ref).
8382
"""
84-
abstract type AgentBasedModel{S<:SpaceType, A<:AbstractAgent} end
83+
abstract type AgentBasedModel{S<:SpaceType,A<:AbstractAgent} end
8584
const ABM = AgentBasedModel
8685

8786
function notimplemented(model)
88-
error("Function not implemented for model of type $(nameof(typeof(model))) "*
89-
"with space type $(nameof(typeof(abmspace(model))))")
87+
return error("Function not implemented for model of type $(nameof(typeof(model))) " *
88+
"with space type $(nameof(typeof(abmspace(model))))")
9089
end
9190

9291
###########################################################################################
@@ -180,7 +179,7 @@ function allocating_random_agent(model, condition)
180179
return nothing
181180
end
182181

183-
function optimistic_random_agent(model, condition; n_attempts = 3*nagents(model))
182+
function optimistic_random_agent(model, condition; n_attempts=3 * nagents(model))
184183
rng = abmrng(model)
185184
for _ in 1:n_attempts
186185
idx = rand(rng, allids(model))
@@ -240,7 +239,6 @@ function Base.setproperty!(m::ABM, s::Symbol, x)
240239
end
241240
end
242241

243-
244242
###########################################################################################
245243
# %% Non-public methods. Must be implemented but are not exported
246244
###########################################################################################
@@ -268,6 +266,6 @@ Return the space instance stored in the `model`.
268266
abmspace(model::ABM) = getfield(model, :space)
269267

270268
function Base.setindex!(m::ABM, args...; kwargs...)
271-
error("`setindex!` or `model[id] = agent` are invalid. Use `add_agent!(model, agent)` "*
272-
"or other variants of an `add_agent_...` function to add agents to an ABM.")
273-
end
269+
return error("`setindex!` or `model[id] = agent` are invalid. Use `add_agent!(model, agent)` " *
270+
"or other variants of an `add_agent_...` function to add agents to an ABM.")
271+
end

src/core/model_concrete.jl

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
export ABM, StandardABM, UnkillableABM, FixedMassABM
22
using StaticArrays: SizedVector
33

4-
ContainerType{A} = Union{AbstractDict{Int,A}, AbstractVector{A}}
4+
ContainerType{A} = Union{AbstractDict{Int,A},AbstractVector{A}}
55

66
# And the three implementations here are just variants with different `C` type.
77
struct SingleContainerABM{S<:SpaceType,A<:AbstractAgent,C<:ContainerType{A},F,P,R<:AbstractRNG} <: AgentBasedModel{S,A}
@@ -39,12 +39,12 @@ single container. Offers the variants:
3939
"""
4040
function SingleContainerABM(
4141
::Type{A},
42-
space::S = nothing;
43-
container::Type = Dict{Int},
44-
scheduler::F = Schedulers.fastest,
45-
properties::P = nothing,
46-
rng::R = Random.default_rng(),
47-
warn = true
42+
space::S=nothing;
43+
container::Type=Dict{Int},
44+
scheduler::F=Schedulers.fastest,
45+
properties::P=nothing,
46+
rng::R=Random.default_rng(),
47+
warn=true,
4848
) where {A<:AbstractAgent,S<:SpaceType,F,P,R<:AbstractRNG}
4949
agent_validator(A, space, warn)
5050
C = construct_agent_container(container, A)
@@ -59,7 +59,7 @@ end
5959
construct_agent_container(::Type{<:Dict}, A) = Dict{Int,A}
6060
construct_agent_container(::Type{<:Vector}, A) = Vector{A}
6161
construct_agent_container(container, A) = throw(
62-
"Unrecognised container $container, please specify either Dict or Vector."
62+
"Unrecognised container $container, please specify either Dict or Vector.",
6363
)
6464

6565
"""
@@ -101,13 +101,13 @@ in the given `agent_vector`.
101101
"""
102102
function FixedMassABM(
103103
agents::AbstractVector{A},
104-
space::S = nothing;
105-
scheduler::F = Schedulers.fastest,
106-
properties::P = nothing,
107-
rng::R = Random.default_rng(),
108-
warn = true
109-
) where {A<:AbstractAgent, S<:SpaceType,F,P,R<:AbstractRNG}
110-
C = SizedVector{length(agents), A}
104+
space::S=nothing;
105+
scheduler::F=Schedulers.fastest,
106+
properties::P=nothing,
107+
rng::R=Random.default_rng(),
108+
warn=true,
109+
) where {A<:AbstractAgent,S<:SpaceType,F,P,R<:AbstractRNG}
110+
C = SizedVector{length(agents),A}
111111
fixed_agents = C(agents)
112112
# Validate that agent ID is the same as its order in the vector.
113113
for (i, a) in enumerate(agents)
@@ -120,11 +120,11 @@ end
120120
#######################################################################################
121121
# %% Model accessing api
122122
#######################################################################################
123-
nextid(model::SingleContainerABM{<:SpaceType,A,Dict{Int, A}}) where {A} = getfield(model, :maxid)[] + 1
123+
nextid(model::SingleContainerABM{<:SpaceType,A,Dict{Int,A}}) where {A} = getfield(model, :maxid)[] + 1
124124
nextid(model::SingleContainerABM{<:SpaceType,A,Vector{A}}) where {A} = nagents(model) + 1
125125
nextid(::SingleContainerABM{<:SpaceType,A,<:SizedVector}) where {A} = error("There is no `nextid` in a `FixedMassABM`. Most likely an internal error.")
126126

127-
function add_agent_to_model!(agent, model::SingleContainerABM{<:SpaceType,A,Dict{Int, A}}) where {A<:AbstractAgent}
127+
function add_agent_to_model!(agent, model::SingleContainerABM{<:SpaceType,A,Dict{Int,A}}) where {A<:AbstractAgent}
128128
if haskey(agent_container(model), agent.id)
129129
error("Can't add agent to model. There is already an agent with id=$(agent.id)")
130130
else
@@ -133,7 +133,9 @@ function add_agent_to_model!(agent, model::SingleContainerABM{<:SpaceType,A,Dict
133133
# Only the `Dict` implementation actually uses the `maxid` field.
134134
# The `Vector` one uses the defaults, and the `Sized` one errors anyways.
135135
maxid = getfield(model, :maxid)
136-
if maxid[] < agent.id; maxid[] = agent.id; end
136+
if maxid[] < agent.id
137+
maxid[] = agent.id
138+
end
137139
return
138140
end
139141

@@ -144,12 +146,12 @@ function add_agent_to_model!(agent, model::SingleContainerABM{<:SpaceType,A,Vect
144146
end
145147

146148
function remove_agent_from_model!(agent::A, model::SingleContainerABM{<:SpaceType,A,<:AbstractDict{Int,A}}) where {A<:AbstractAgent}
147-
delete!(agent_container(model), agent.id)
149+
return delete!(agent_container(model), agent.id)
148150
end
149151
function remove_agent_from_model!(::A, model::SingleContainerABM{<:SpaceType,A,<:AbstractVector}) where {A<:AbstractAgent}
150-
error(
151-
"Cannot remove agents stored in $(containertype(model)). "*
152-
"Use the vanilla `SingleContainerABM` to be able to remove agents."
152+
return error(
153+
"Cannot remove agents stored in $(containertype(model)). " *
154+
"Use the vanilla `SingleContainerABM` to be able to remove agents.",
153155
)
154156
end
155157

@@ -188,18 +190,18 @@ end
188190
do_checks(agent, space)
189191
Helper function for `agent_validator`.
190192
"""
191-
function do_checks(::Type{A}, space::S, warn::Bool) where {A<:AbstractAgent, S<:SpaceType}
193+
function do_checks(::Type{A}, space::S, warn::Bool) where {A<:AbstractAgent,S<:SpaceType}
192194
if warn
193195
isbitstype(A) &&
194-
@warn "Agent type is not mutable, and most library functions assume that it is."
196+
@warn "Agent type is not mutable, and most library functions assume that it is."
195197
end
196198
(any(isequal(:id), fieldnames(A)) && fieldnames(A)[1] == :id) ||
197-
throw(ArgumentError("First field of agent type must be `id` (and should be of type `Int`)."))
199+
throw(ArgumentError("First field of agent type must be `id` (and should be of type `Int`)."))
198200
fieldtype(A, :id) <: Integer ||
199-
throw(ArgumentError("`id` field in agent type must be of type `Int`."))
201+
throw(ArgumentError("`id` field in agent type must be of type `Int`."))
200202
if space !== nothing
201203
(any(isequal(:pos), fieldnames(A)) && fieldnames(A)[2] == :pos) ||
202-
throw(ArgumentError("Second field of agent type must be `pos` when using a space."))
204+
throw(ArgumentError("Second field of agent type must be `pos` when using a space."))
203205
# Check `pos` field in A has the correct type
204206
pos_type = fieldtype(A, :pos)
205207
space_type = typeof(space)

0 commit comments

Comments
 (0)