Skip to content

Commit 5efcf99

Browse files
committed
Merge branch 'format' of https://github.com/jacobusmmsmit/Agents.jl into format
2 parents 4f9a81f + ae08e1a commit 5efcf99

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

66 files changed

+2109
-1845
lines changed

benchmark/agents.jl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,4 +83,3 @@ mutable struct ContinuousAgentFive <: AbstractAgent
8383
pos::NTuple{3,Float64}
8484
vel::NTuple{3,Float64}
8585
end
86-

benchmark/benchmarks.jl

Lines changed: 126 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -64,30 +64,37 @@ graph_model = ABM(GraphAgent, GraphSpace(complete_digraph(200)))
6464
graph_agent = GraphAgent(1, 82, 6.5, false)
6565
graph_union_model = ABM(
6666
Union{GraphAgent,GraphAgentTwo,GraphAgentThree,GraphAgentFour,GraphAgentFive},
67-
GraphSpace(complete_digraph(200)),
68-
warn = false,
67+
GraphSpace(complete_digraph(200));
68+
warn=false,
6969
)
7070

7171
# Limit samples here so space does not saturate with agents
72-
SUITE["graph"]["add"]["agent"] =
73-
@benchmarkable add_agent!($graph_agent, $graph_model) samples = 100
74-
SUITE["graph"]["add"]["agent_pos"] =
75-
@benchmarkable add_agent_pos!($graph_agent, $graph_model) samples = 100
76-
SUITE["graph"]["add"]["agent_single"] =
77-
@benchmarkable add_agent_single!($graph_agent, $graph_model) samples = 100
78-
SUITE["graph"]["add"]["create_pos"] =
79-
@benchmarkable add_agent!(26, $graph_model, 6.5, false) samples = 100
80-
SUITE["graph"]["add"]["create_single"] =
81-
@benchmarkable add_agent_single!($graph_model, 6.5, false) samples = 100
82-
SUITE["graph"]["add"]["create"] =
83-
@benchmarkable add_agent!($graph_model, 6.5, false) samples = 100
84-
85-
SUITE["graph"]["add_union"]["agent"] =
86-
@benchmarkable add_agent!($graph_agent, $graph_union_model) samples = 100
87-
SUITE["graph"]["add_union"]["agent_pos"] =
88-
@benchmarkable add_agent_pos!($graph_agent, $graph_union_model) samples = 100
89-
SUITE["graph"]["add_union"]["agent_single"] =
90-
@benchmarkable add_agent_single!($graph_agent, $graph_union_model) samples = 100
72+
SUITE["graph"]["add"]["agent"] = @benchmarkable add_agent!($graph_agent, $graph_model) samples =
73+
100
74+
SUITE["graph"]["add"]["agent_pos"] = @benchmarkable add_agent_pos!(
75+
$graph_agent, $graph_model
76+
) samples = 100
77+
SUITE["graph"]["add"]["agent_single"] = @benchmarkable add_agent_single!(
78+
$graph_agent, $graph_model
79+
) samples = 100
80+
SUITE["graph"]["add"]["create_pos"] = @benchmarkable add_agent!(
81+
26, $graph_model, 6.5, false
82+
) samples = 100
83+
SUITE["graph"]["add"]["create_single"] = @benchmarkable add_agent_single!(
84+
$graph_model, 6.5, false
85+
) samples = 100
86+
SUITE["graph"]["add"]["create"] = @benchmarkable add_agent!($graph_model, 6.5, false) samples =
87+
100
88+
89+
SUITE["graph"]["add_union"]["agent"] = @benchmarkable add_agent!(
90+
$graph_agent, $graph_union_model
91+
) samples = 100
92+
SUITE["graph"]["add_union"]["agent_pos"] = @benchmarkable add_agent_pos!(
93+
$graph_agent, $graph_union_model
94+
) samples = 100
95+
SUITE["graph"]["add_union"]["agent_single"] = @benchmarkable add_agent_single!(
96+
$graph_agent, $graph_union_model
97+
) samples = 100
9198

9299
graph_model = ABM(GraphAgent, GraphSpace(complete_digraph(100)))
93100
for position in 1:100
@@ -102,20 +109,24 @@ SUITE["graph"]["move"]["pos"] = @benchmarkable move_agent!($a, 68, $graph_model)
102109
SUITE["graph"]["move"]["single"] = @benchmarkable move_agent_single!($a, $graph_model)
103110

104111
# We use a digraph, so all agents are neighbors of each other
105-
SUITE["graph"]["neighbors"]["nearby_ids"] =
106-
@benchmarkable nearby_ids($pos, $graph_model) setup = (nearby_ids($pos, $graph_model))
107-
SUITE["graph"]["neighbors"]["nearby_agents"] =
108-
@benchmarkable nearby_ids($a, $graph_model) setup = (nearby_ids($a, $graph_model))
109-
SUITE["graph"]["neighbors"]["nearby_ids_iterate"] =
110-
@benchmarkable iterate_over_neighbors($pos, $graph_model, 1) setup =
111-
(nearby_ids($pos, $graph_model))
112-
SUITE["graph"]["neighbors"]["nearby_agents_iterate"] =
113-
@benchmarkable iterate_over_neighbors($a, $graph_model, 1) setup =
114-
(nearby_ids($a, $graph_model))
115-
SUITE["graph"]["neighbors"]["position_pos"] =
116-
@benchmarkable nearby_positions($pos, $graph_model)
117-
SUITE["graph"]["neighbors"]["position_agent"] =
118-
@benchmarkable nearby_positions($a, $graph_model)
112+
SUITE["graph"]["neighbors"]["nearby_ids"] = @benchmarkable nearby_ids($pos, $graph_model) setup = (nearby_ids(
113+
$pos, $graph_model
114+
))
115+
SUITE["graph"]["neighbors"]["nearby_agents"] = @benchmarkable nearby_ids($a, $graph_model) setup = (nearby_ids(
116+
$a, $graph_model
117+
))
118+
SUITE["graph"]["neighbors"]["nearby_ids_iterate"] = @benchmarkable iterate_over_neighbors(
119+
$pos, $graph_model, 1
120+
) setup = (nearby_ids($pos, $graph_model))
121+
SUITE["graph"]["neighbors"]["nearby_agents_iterate"] = @benchmarkable iterate_over_neighbors(
122+
$a, $graph_model, 1
123+
) setup = (nearby_ids($a, $graph_model))
124+
SUITE["graph"]["neighbors"]["position_pos"] = @benchmarkable nearby_positions(
125+
$pos, $graph_model
126+
)
127+
SUITE["graph"]["neighbors"]["position_agent"] = @benchmarkable nearby_positions(
128+
$a, $graph_model
129+
)
119130

120131
SUITE["graph"]["position"]["contents"] = @benchmarkable ids_in_position($pos, $graph_model)
121132
SUITE["graph"]["position"]["positions"] = @benchmarkable positions($graph_model)
@@ -127,18 +138,20 @@ grid_agent = GridAgent(1, (2, 3), 6.5, false)
127138
grid_union_model = ABM(
128139
Union{GridAgent,GridAgentTwo,GridAgentThree,GridAgentFour,GridAgentFive},
129140
GridSpace((15, 15));
130-
warn = false,
141+
warn=false,
131142
)
132143

133-
SUITE["grid"]["add"]["agent_pos"] =
134-
@benchmarkable add_agent_pos!($grid_agent, $grid_model) samples = 100
135-
SUITE["grid"]["add"]["create_fill"] =
136-
@benchmarkable fill_space!($grid_model, 6.5, false) samples = 100
144+
SUITE["grid"]["add"]["agent_pos"] = @benchmarkable add_agent_pos!($grid_agent, $grid_model) samples =
145+
100
146+
SUITE["grid"]["add"]["create_fill"] = @benchmarkable fill_space!($grid_model, 6.5, false) samples =
147+
100
137148

138-
SUITE["grid"]["add_union"]["agent_pos"] =
139-
@benchmarkable add_agent_pos!($grid_agent, $grid_union_model) samples = 100
140-
SUITE["grid"]["add_union"]["agent_fill"] =
141-
@benchmarkable fill_space!(GridAgent, $grid_union_model, 6.5, false) samples = 100
149+
SUITE["grid"]["add_union"]["agent_pos"] = @benchmarkable add_agent_pos!(
150+
$grid_agent, $grid_union_model
151+
) samples = 100
152+
SUITE["grid"]["add_union"]["agent_fill"] = @benchmarkable fill_space!(
153+
GridAgent, $grid_union_model, 6.5, false
154+
) samples = 100
142155

143156
grid_model = ABM(GridAgent, GridSpace((50, 50)))
144157
for x in 1:50
@@ -154,58 +167,62 @@ SUITE["grid"]["move"]["random"] = @benchmarkable move_agent!($a, $grid_model)
154167
SUITE["grid"]["move"]["pos"] = @benchmarkable move_agent!($a, (14, 35), $grid_model)
155168
SUITE["grid"]["move"]["single"] = @benchmarkable move_agent_single!($a, $grid_model)
156169

157-
SUITE["grid"]["neighbors"]["nearby_ids"] =
158-
@benchmarkable nearby_ids($pos, $grid_model, 5) setup =
159-
(nearby_ids($pos, $grid_model, 5))
160-
SUITE["grid"]["neighbors"]["nearby_agents"] =
161-
@benchmarkable nearby_ids($a, $grid_model, 5) setup = (nearby_ids($a, $grid_model, 5))
170+
SUITE["grid"]["neighbors"]["nearby_ids"] = @benchmarkable nearby_ids($pos, $grid_model, 5) setup = (nearby_ids(
171+
$pos, $grid_model, 5
172+
))
173+
SUITE["grid"]["neighbors"]["nearby_agents"] = @benchmarkable nearby_ids($a, $grid_model, 5) setup = (nearby_ids(
174+
$a, $grid_model, 5
175+
))
162176

163-
SUITE["grid"]["neighbors"]["nearby_ids_iterate"] =
164-
@benchmarkable iterate_over_neighbors($pos, $grid_model, 30) setup =
165-
(nearby_ids($pos, $grid_model, 30))
177+
SUITE["grid"]["neighbors"]["nearby_ids_iterate"] = @benchmarkable iterate_over_neighbors(
178+
$pos, $grid_model, 30
179+
) setup = (nearby_ids($pos, $grid_model, 30))
166180

167-
SUITE["grid"]["neighbors"]["nearby_agents_iterate"] =
168-
@benchmarkable iterate_over_neighbors($a, $grid_model, 30) setup =
169-
(nearby_ids($a, $grid_model, 30))
181+
SUITE["grid"]["neighbors"]["nearby_agents_iterate"] = @benchmarkable iterate_over_neighbors(
182+
$a, $grid_model, 30
183+
) setup = (nearby_ids($a, $grid_model, 30))
170184

171-
SUITE["grid"]["neighbors"]["position_pos"] =
172-
@benchmarkable nearby_positions($a, $grid_model)
173-
SUITE["grid"]["neighbors"]["position_agent"] =
174-
@benchmarkable nearby_positions($a, $grid_model)
185+
SUITE["grid"]["neighbors"]["position_pos"] = @benchmarkable nearby_positions(
186+
$a, $grid_model
187+
)
188+
SUITE["grid"]["neighbors"]["position_agent"] = @benchmarkable nearby_positions(
189+
$a, $grid_model
190+
)
175191

176192
SUITE["grid"]["position"]["contents"] = @benchmarkable ids_in_position($pos, $grid_model)
177193
SUITE["graph"]["position"]["positions"] = @benchmarkable positions($graph_model)
178194

179195
#### API -> CONTINUOUS ####
180196

181-
continuous_model = ABM(ContinuousAgent, ContinuousSpace((10.0, 10.0, 10.0); spacing = 0.5))
197+
continuous_model = ABM(ContinuousAgent, ContinuousSpace((10.0, 10.0, 10.0); spacing=0.5))
182198
continuous_agent = ContinuousAgent(1, (2.2, 1.9, 7.5), (0.5, 1.0, 0.01), 6.5, false)
183199

184200
# We must use setup create the model inside some benchmarks here, otherwise we hit the issue from #226.
185201
# For tuning, this is actually impossible. So until ContinuousSpace is implemented, we drop these tests.
186-
SUITE["continuous"]["add"]["agent_pos"] =
187-
@benchmarkable add_agent_pos!($continuous_agent, cmodel) setup =
188-
(cmodel = ABM(ContinuousAgent, ContinuousSpace((10.0, 10.0, 10.0); spacing = 0.5))) samples =
189-
100
190-
191-
SUITE["continuous"]["add_union"]["agent_pos"] =
192-
@benchmarkable add_agent_pos!($continuous_agent, cmodel) setup = (
193-
cmodel = ABM(
194-
Union{
195-
ContinuousAgent,
196-
ContinuousAgentTwo,
197-
ContinuousAgentThree,
198-
ContinuousAgentFour,
199-
ContinuousAgentFive,
200-
},
201-
ContinuousSpace((10.0, 10.0, 10.0), spacing = 0.5);
202-
warn = false,
203-
)
204-
) samples = 100
205-
206-
for x in range(0, stop = 9.99, length = 7)
207-
for y in range(0, stop = 9.99, length = 7)
208-
for z in range(0, stop = 9.99, length = 7)
202+
SUITE["continuous"]["add"]["agent_pos"] = @benchmarkable add_agent_pos!(
203+
$continuous_agent, cmodel
204+
) setup = (cmodel = ABM(ContinuousAgent, ContinuousSpace((10.0, 10.0, 10.0); spacing=0.5))) samples =
205+
100
206+
207+
SUITE["continuous"]["add_union"]["agent_pos"] = @benchmarkable add_agent_pos!(
208+
$continuous_agent, cmodel
209+
) setup = (
210+
cmodel = ABM(
211+
Union{
212+
ContinuousAgent,
213+
ContinuousAgentTwo,
214+
ContinuousAgentThree,
215+
ContinuousAgentFour,
216+
ContinuousAgentFive,
217+
},
218+
ContinuousSpace((10.0, 10.0, 10.0); spacing=0.5);
219+
warn=false,
220+
)
221+
) samples = 100
222+
223+
for x in range(0; stop=9.99, length=7)
224+
for y in range(0; stop=9.99, length=7)
225+
for z in range(0; stop=9.99, length=7)
209226
add_agent!((x, y, z), continuous_model, (0.8, 0.7, 1.3), 6.5, false)
210227
end
211228
end
@@ -214,22 +231,23 @@ a = continuous_model[139]
214231
pos = (7.07, 8.10, 6.58)
215232
SUITE["continuous"]["move"]["update"] = @benchmarkable move_agent!($a, $continuous_model)
216233

217-
SUITE["continuous"]["neighbors"]["nearby_ids"] =
218-
@benchmarkable nearby_ids($pos, $continuous_model, 5) setup =
219-
(nearby_ids($pos, $continuous_model, 5))
220-
221-
SUITE["continuous"]["neighbors"]["nearby_agents"] =
222-
@benchmarkable nearby_ids($a, $continuous_model, 5) setup =
223-
(nearby_ids($a, $continuous_model, 5))
224-
225-
SUITE["continuous"]["neighbors"]["nearby_ids_iterate"] =
226-
@benchmarkable iterate_over_neighbors($pos, $continuous_model, 10) setup =
227-
(nearby_ids($pos, $continuous_model, 10))
228-
SUITE["continuous"]["neighbors"]["nearby_agents_iterate"] =
229-
@benchmarkable iterate_over_neighbors($a, $continuous_model, 10) setup =
230-
(nearby_ids($a, $continuous_model, 10))
231-
SUITE["continuous"]["neighbors"]["nearest"] =
232-
@benchmarkable nearest_neighbor($a, $continuous_model, 5)
234+
SUITE["continuous"]["neighbors"]["nearby_ids"] = @benchmarkable nearby_ids(
235+
$pos, $continuous_model, 5
236+
) setup = (nearby_ids($pos, $continuous_model, 5))
237+
238+
SUITE["continuous"]["neighbors"]["nearby_agents"] = @benchmarkable nearby_ids(
239+
$a, $continuous_model, 5
240+
) setup = (nearby_ids($a, $continuous_model, 5))
241+
242+
SUITE["continuous"]["neighbors"]["nearby_ids_iterate"] = @benchmarkable iterate_over_neighbors(
243+
$pos, $continuous_model, 10
244+
) setup = (nearby_ids($pos, $continuous_model, 10))
245+
SUITE["continuous"]["neighbors"]["nearby_agents_iterate"] = @benchmarkable iterate_over_neighbors(
246+
$a, $continuous_model, 10
247+
) setup = (nearby_ids($a, $continuous_model, 10))
248+
SUITE["continuous"]["neighbors"]["nearest"] = @benchmarkable nearest_neighbor(
249+
$a, $continuous_model, 5
250+
)
233251

234252
# Benchmark takes too long to be reasonable, even with a small sample.
235253
# This needs to be looked at in the future, but it's being ignored for the moment
@@ -245,12 +263,15 @@ graph_df = init_agent_dataframe(graph_model, adata)
245263
grid_df = init_agent_dataframe(grid_model, adata)
246264
continuous_df = init_agent_dataframe(continuous_model, adata)
247265

248-
SUITE["graph"]["collect"]["store_agent"] =
249-
@benchmarkable collect_agent_data!($graph_df, $graph_model, $adata, 0)
250-
SUITE["grid"]["collect"]["store_agent"] =
251-
@benchmarkable collect_agent_data!($grid_df, $grid_model, $adata, 0)
252-
SUITE["continuous"]["collect"]["store_agent"] =
253-
@benchmarkable collect_agent_data!($continuous_df, $continuous_model, $adata, 0)
266+
SUITE["graph"]["collect"]["store_agent"] = @benchmarkable collect_agent_data!(
267+
$graph_df, $graph_model, $adata, 0
268+
)
269+
SUITE["grid"]["collect"]["store_agent"] = @benchmarkable collect_agent_data!(
270+
$grid_df, $grid_model, $adata, 0
271+
)
272+
SUITE["continuous"]["collect"]["store_agent"] = @benchmarkable collect_agent_data!(
273+
$continuous_df, $continuous_model, $adata, 0
274+
)
254275

255276
#### SCHEDULERS ###
256277
include("schedulers.jl")

benchmark/ensembles.jl

Lines changed: 34 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -15,31 +15,39 @@ function ensemble_benchmark(f, parallel, nreplicates)
1515
whensteps = 50
1616

1717
function genmodels(nreplicates)
18-
basemodels = [Models.schelling(; numagents)[1]
19-
for numagents in collect(numagents_low:numagents_high)]
18+
basemodels = [
19+
Models.schelling(; numagents)[1] for
20+
numagents in collect(numagents_low:numagents_high)
21+
]
2022

2123
return repeat(basemodels, nreplicates)
2224
end
2325

2426
if f == ensemblerun!
2527
models = genmodels(nreplicates)
26-
adf, mdf, _ = ensemblerun!(models, schelling_agent_step!, dummystep, nsteps;
27-
parallel, adata = [:pos, :mood, :group],
28-
showprogress = true,
29-
when = (model, step) ->
30-
( (step) % whensteps == 0 || step == 0 ),
31-
mdata = [:min_to_be_happy])
28+
adf, mdf, _ = ensemblerun!(
29+
models,
30+
schelling_agent_step!,
31+
dummystep,
32+
nsteps;
33+
parallel,
34+
adata=[:pos, :mood, :group],
35+
showprogress=true,
36+
when=(model, step) -> ((step) % whensteps == 0 || step == 0),
37+
mdata=[:min_to_be_happy],
38+
)
3239
else
3340
# TODO: Why do we need `replicate_idx` here?
3441
# Can't we just use the `Models.schelling`?
3542
function initialize(;
36-
replicate_idx = 1, numagents = 320, griddims = (20, 20), min_to_be_happy = 3
43+
replicate_idx=1, numagents=320, griddims=(20, 20), min_to_be_happy=3
3744
)
38-
space = GridSpace(griddims, periodic = false)
45+
space = GridSpace(griddims; periodic=false)
3946
properties = Dict(:min_to_be_happy => min_to_be_happy)
4047

41-
model = ABM(SchellingAgent, space;
42-
properties = properties, scheduler = Schedulers.randomly)
48+
model = ABM(
49+
SchellingAgent, space; properties=properties, scheduler=Schedulers.randomly
50+
)
4351

4452
for n in 1:numagents
4553
agent = SchellingAgent(n, (1, 1), false, n < numagents / 2 ? 1 : 2)
@@ -54,13 +62,17 @@ function ensemble_benchmark(f, parallel, nreplicates)
5462
:replicate_idx => collect(1:nreplicates),
5563
:griddims => (20, 20),
5664
)
57-
paramscan(parameters, initialize;
58-
parallel, adata = [:pos, :mood, :group],
59-
mdata = [:min_to_be_happy], showprogress = true,
60-
agent_step! = schelling_agent_step!,
61-
when = (model, step) ->
62-
( (step) % whensteps == 0 || step == 0 ),
63-
n = nsteps)
65+
paramscan(
66+
parameters,
67+
initialize;
68+
parallel,
69+
adata=[:pos, :mood, :group],
70+
mdata=[:min_to_be_happy],
71+
showprogress=true,
72+
(agent_step!)=schelling_agent_step!,
73+
when=(model, step) -> ((step) % whensteps == 0 || step == 0),
74+
n=nsteps,
75+
)
6476
end
6577
end
6678

@@ -72,7 +84,8 @@ for (f, parallel, nreplicates, name) in [
7284
(paramscan, false, 10, "paramscan serial 10 reps"),
7385
(paramscan, true, 10, "paramscan parallel 10 reps"),
7486
(paramscan, false, 20, "paramscan serial 20 reps"),
75-
(paramscan, true, 20, "paramscan parallel 20 reps")
87+
(paramscan, true, 20, "paramscan parallel 20 reps"),
7688
]
77-
ENSEMBLES_SUITE[name] = @benchmarkable ensemble_benchmark($f, $parallel, $nreplicates) samples = 1
89+
ENSEMBLES_SUITE[name] = @benchmarkable ensemble_benchmark($f, $parallel, $nreplicates) samples =
90+
1
7891
end

0 commit comments

Comments
 (0)