Skip to content

Commit

Permalink
Merge branch 'format' of https://github.com/jacobusmmsmit/Agents.jl i…
Browse files Browse the repository at this point in the history
…nto format
  • Loading branch information
jacobusmmsmit committed Mar 9, 2023
2 parents 4f9a81f + ae08e1a commit 5efcf99
Show file tree
Hide file tree
Showing 66 changed files with 2,109 additions and 1,845 deletions.
1 change: 0 additions & 1 deletion benchmark/agents.jl
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,3 @@ mutable struct ContinuousAgentFive <: AbstractAgent
pos::NTuple{3,Float64}
vel::NTuple{3,Float64}
end

231 changes: 126 additions & 105 deletions benchmark/benchmarks.jl
Original file line number Diff line number Diff line change
Expand Up @@ -64,30 +64,37 @@ graph_model = ABM(GraphAgent, GraphSpace(complete_digraph(200)))
graph_agent = GraphAgent(1, 82, 6.5, false)
graph_union_model = ABM(
Union{GraphAgent,GraphAgentTwo,GraphAgentThree,GraphAgentFour,GraphAgentFive},
GraphSpace(complete_digraph(200)),
warn = false,
GraphSpace(complete_digraph(200));
warn=false,
)

# Limit samples here so space does not saturate with agents
SUITE["graph"]["add"]["agent"] =
@benchmarkable add_agent!($graph_agent, $graph_model) samples = 100
SUITE["graph"]["add"]["agent_pos"] =
@benchmarkable add_agent_pos!($graph_agent, $graph_model) samples = 100
SUITE["graph"]["add"]["agent_single"] =
@benchmarkable add_agent_single!($graph_agent, $graph_model) samples = 100
SUITE["graph"]["add"]["create_pos"] =
@benchmarkable add_agent!(26, $graph_model, 6.5, false) samples = 100
SUITE["graph"]["add"]["create_single"] =
@benchmarkable add_agent_single!($graph_model, 6.5, false) samples = 100
SUITE["graph"]["add"]["create"] =
@benchmarkable add_agent!($graph_model, 6.5, false) samples = 100

SUITE["graph"]["add_union"]["agent"] =
@benchmarkable add_agent!($graph_agent, $graph_union_model) samples = 100
SUITE["graph"]["add_union"]["agent_pos"] =
@benchmarkable add_agent_pos!($graph_agent, $graph_union_model) samples = 100
SUITE["graph"]["add_union"]["agent_single"] =
@benchmarkable add_agent_single!($graph_agent, $graph_union_model) samples = 100
SUITE["graph"]["add"]["agent"] = @benchmarkable add_agent!($graph_agent, $graph_model) samples =
100
SUITE["graph"]["add"]["agent_pos"] = @benchmarkable add_agent_pos!(
$graph_agent, $graph_model
) samples = 100
SUITE["graph"]["add"]["agent_single"] = @benchmarkable add_agent_single!(
$graph_agent, $graph_model
) samples = 100
SUITE["graph"]["add"]["create_pos"] = @benchmarkable add_agent!(
26, $graph_model, 6.5, false
) samples = 100
SUITE["graph"]["add"]["create_single"] = @benchmarkable add_agent_single!(
$graph_model, 6.5, false
) samples = 100
SUITE["graph"]["add"]["create"] = @benchmarkable add_agent!($graph_model, 6.5, false) samples =
100

SUITE["graph"]["add_union"]["agent"] = @benchmarkable add_agent!(
$graph_agent, $graph_union_model
) samples = 100
SUITE["graph"]["add_union"]["agent_pos"] = @benchmarkable add_agent_pos!(
$graph_agent, $graph_union_model
) samples = 100
SUITE["graph"]["add_union"]["agent_single"] = @benchmarkable add_agent_single!(
$graph_agent, $graph_union_model
) samples = 100

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

# We use a digraph, so all agents are neighbors of each other
SUITE["graph"]["neighbors"]["nearby_ids"] =
@benchmarkable nearby_ids($pos, $graph_model) setup = (nearby_ids($pos, $graph_model))
SUITE["graph"]["neighbors"]["nearby_agents"] =
@benchmarkable nearby_ids($a, $graph_model) setup = (nearby_ids($a, $graph_model))
SUITE["graph"]["neighbors"]["nearby_ids_iterate"] =
@benchmarkable iterate_over_neighbors($pos, $graph_model, 1) setup =
(nearby_ids($pos, $graph_model))
SUITE["graph"]["neighbors"]["nearby_agents_iterate"] =
@benchmarkable iterate_over_neighbors($a, $graph_model, 1) setup =
(nearby_ids($a, $graph_model))
SUITE["graph"]["neighbors"]["position_pos"] =
@benchmarkable nearby_positions($pos, $graph_model)
SUITE["graph"]["neighbors"]["position_agent"] =
@benchmarkable nearby_positions($a, $graph_model)
SUITE["graph"]["neighbors"]["nearby_ids"] = @benchmarkable nearby_ids($pos, $graph_model) setup = (nearby_ids(
$pos, $graph_model
))
SUITE["graph"]["neighbors"]["nearby_agents"] = @benchmarkable nearby_ids($a, $graph_model) setup = (nearby_ids(
$a, $graph_model
))
SUITE["graph"]["neighbors"]["nearby_ids_iterate"] = @benchmarkable iterate_over_neighbors(
$pos, $graph_model, 1
) setup = (nearby_ids($pos, $graph_model))
SUITE["graph"]["neighbors"]["nearby_agents_iterate"] = @benchmarkable iterate_over_neighbors(
$a, $graph_model, 1
) setup = (nearby_ids($a, $graph_model))
SUITE["graph"]["neighbors"]["position_pos"] = @benchmarkable nearby_positions(
$pos, $graph_model
)
SUITE["graph"]["neighbors"]["position_agent"] = @benchmarkable nearby_positions(
$a, $graph_model
)

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

SUITE["grid"]["add"]["agent_pos"] =
@benchmarkable add_agent_pos!($grid_agent, $grid_model) samples = 100
SUITE["grid"]["add"]["create_fill"] =
@benchmarkable fill_space!($grid_model, 6.5, false) samples = 100
SUITE["grid"]["add"]["agent_pos"] = @benchmarkable add_agent_pos!($grid_agent, $grid_model) samples =
100
SUITE["grid"]["add"]["create_fill"] = @benchmarkable fill_space!($grid_model, 6.5, false) samples =
100

SUITE["grid"]["add_union"]["agent_pos"] =
@benchmarkable add_agent_pos!($grid_agent, $grid_union_model) samples = 100
SUITE["grid"]["add_union"]["agent_fill"] =
@benchmarkable fill_space!(GridAgent, $grid_union_model, 6.5, false) samples = 100
SUITE["grid"]["add_union"]["agent_pos"] = @benchmarkable add_agent_pos!(
$grid_agent, $grid_union_model
) samples = 100
SUITE["grid"]["add_union"]["agent_fill"] = @benchmarkable fill_space!(
GridAgent, $grid_union_model, 6.5, false
) samples = 100

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

SUITE["grid"]["neighbors"]["nearby_ids"] =
@benchmarkable nearby_ids($pos, $grid_model, 5) setup =
(nearby_ids($pos, $grid_model, 5))
SUITE["grid"]["neighbors"]["nearby_agents"] =
@benchmarkable nearby_ids($a, $grid_model, 5) setup = (nearby_ids($a, $grid_model, 5))
SUITE["grid"]["neighbors"]["nearby_ids"] = @benchmarkable nearby_ids($pos, $grid_model, 5) setup = (nearby_ids(
$pos, $grid_model, 5
))
SUITE["grid"]["neighbors"]["nearby_agents"] = @benchmarkable nearby_ids($a, $grid_model, 5) setup = (nearby_ids(
$a, $grid_model, 5
))

SUITE["grid"]["neighbors"]["nearby_ids_iterate"] =
@benchmarkable iterate_over_neighbors($pos, $grid_model, 30) setup =
(nearby_ids($pos, $grid_model, 30))
SUITE["grid"]["neighbors"]["nearby_ids_iterate"] = @benchmarkable iterate_over_neighbors(
$pos, $grid_model, 30
) setup = (nearby_ids($pos, $grid_model, 30))

SUITE["grid"]["neighbors"]["nearby_agents_iterate"] =
@benchmarkable iterate_over_neighbors($a, $grid_model, 30) setup =
(nearby_ids($a, $grid_model, 30))
SUITE["grid"]["neighbors"]["nearby_agents_iterate"] = @benchmarkable iterate_over_neighbors(
$a, $grid_model, 30
) setup = (nearby_ids($a, $grid_model, 30))

SUITE["grid"]["neighbors"]["position_pos"] =
@benchmarkable nearby_positions($a, $grid_model)
SUITE["grid"]["neighbors"]["position_agent"] =
@benchmarkable nearby_positions($a, $grid_model)
SUITE["grid"]["neighbors"]["position_pos"] = @benchmarkable nearby_positions(
$a, $grid_model
)
SUITE["grid"]["neighbors"]["position_agent"] = @benchmarkable nearby_positions(
$a, $grid_model
)

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

#### API -> CONTINUOUS ####

continuous_model = ABM(ContinuousAgent, ContinuousSpace((10.0, 10.0, 10.0); spacing = 0.5))
continuous_model = ABM(ContinuousAgent, ContinuousSpace((10.0, 10.0, 10.0); spacing=0.5))
continuous_agent = ContinuousAgent(1, (2.2, 1.9, 7.5), (0.5, 1.0, 0.01), 6.5, false)

# We must use setup create the model inside some benchmarks here, otherwise we hit the issue from #226.
# For tuning, this is actually impossible. So until ContinuousSpace is implemented, we drop these tests.
SUITE["continuous"]["add"]["agent_pos"] =
@benchmarkable add_agent_pos!($continuous_agent, cmodel) setup =
(cmodel = ABM(ContinuousAgent, ContinuousSpace((10.0, 10.0, 10.0); spacing = 0.5))) samples =
100

SUITE["continuous"]["add_union"]["agent_pos"] =
@benchmarkable add_agent_pos!($continuous_agent, cmodel) setup = (
cmodel = ABM(
Union{
ContinuousAgent,
ContinuousAgentTwo,
ContinuousAgentThree,
ContinuousAgentFour,
ContinuousAgentFive,
},
ContinuousSpace((10.0, 10.0, 10.0), spacing = 0.5);
warn = false,
)
) samples = 100

for x in range(0, stop = 9.99, length = 7)
for y in range(0, stop = 9.99, length = 7)
for z in range(0, stop = 9.99, length = 7)
SUITE["continuous"]["add"]["agent_pos"] = @benchmarkable add_agent_pos!(
$continuous_agent, cmodel
) setup = (cmodel = ABM(ContinuousAgent, ContinuousSpace((10.0, 10.0, 10.0); spacing=0.5))) samples =
100

SUITE["continuous"]["add_union"]["agent_pos"] = @benchmarkable add_agent_pos!(
$continuous_agent, cmodel
) setup = (
cmodel = ABM(
Union{
ContinuousAgent,
ContinuousAgentTwo,
ContinuousAgentThree,
ContinuousAgentFour,
ContinuousAgentFive,
},
ContinuousSpace((10.0, 10.0, 10.0); spacing=0.5);
warn=false,
)
) samples = 100

for x in range(0; stop=9.99, length=7)
for y in range(0; stop=9.99, length=7)
for z in range(0; stop=9.99, length=7)
add_agent!((x, y, z), continuous_model, (0.8, 0.7, 1.3), 6.5, false)
end
end
Expand All @@ -214,22 +231,23 @@ a = continuous_model[139]
pos = (7.07, 8.10, 6.58)
SUITE["continuous"]["move"]["update"] = @benchmarkable move_agent!($a, $continuous_model)

SUITE["continuous"]["neighbors"]["nearby_ids"] =
@benchmarkable nearby_ids($pos, $continuous_model, 5) setup =
(nearby_ids($pos, $continuous_model, 5))

SUITE["continuous"]["neighbors"]["nearby_agents"] =
@benchmarkable nearby_ids($a, $continuous_model, 5) setup =
(nearby_ids($a, $continuous_model, 5))

SUITE["continuous"]["neighbors"]["nearby_ids_iterate"] =
@benchmarkable iterate_over_neighbors($pos, $continuous_model, 10) setup =
(nearby_ids($pos, $continuous_model, 10))
SUITE["continuous"]["neighbors"]["nearby_agents_iterate"] =
@benchmarkable iterate_over_neighbors($a, $continuous_model, 10) setup =
(nearby_ids($a, $continuous_model, 10))
SUITE["continuous"]["neighbors"]["nearest"] =
@benchmarkable nearest_neighbor($a, $continuous_model, 5)
SUITE["continuous"]["neighbors"]["nearby_ids"] = @benchmarkable nearby_ids(
$pos, $continuous_model, 5
) setup = (nearby_ids($pos, $continuous_model, 5))

SUITE["continuous"]["neighbors"]["nearby_agents"] = @benchmarkable nearby_ids(
$a, $continuous_model, 5
) setup = (nearby_ids($a, $continuous_model, 5))

SUITE["continuous"]["neighbors"]["nearby_ids_iterate"] = @benchmarkable iterate_over_neighbors(
$pos, $continuous_model, 10
) setup = (nearby_ids($pos, $continuous_model, 10))
SUITE["continuous"]["neighbors"]["nearby_agents_iterate"] = @benchmarkable iterate_over_neighbors(
$a, $continuous_model, 10
) setup = (nearby_ids($a, $continuous_model, 10))
SUITE["continuous"]["neighbors"]["nearest"] = @benchmarkable nearest_neighbor(
$a, $continuous_model, 5
)

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

SUITE["graph"]["collect"]["store_agent"] =
@benchmarkable collect_agent_data!($graph_df, $graph_model, $adata, 0)
SUITE["grid"]["collect"]["store_agent"] =
@benchmarkable collect_agent_data!($grid_df, $grid_model, $adata, 0)
SUITE["continuous"]["collect"]["store_agent"] =
@benchmarkable collect_agent_data!($continuous_df, $continuous_model, $adata, 0)
SUITE["graph"]["collect"]["store_agent"] = @benchmarkable collect_agent_data!(
$graph_df, $graph_model, $adata, 0
)
SUITE["grid"]["collect"]["store_agent"] = @benchmarkable collect_agent_data!(
$grid_df, $grid_model, $adata, 0
)
SUITE["continuous"]["collect"]["store_agent"] = @benchmarkable collect_agent_data!(
$continuous_df, $continuous_model, $adata, 0
)

#### SCHEDULERS ###
include("schedulers.jl")
Expand Down
55 changes: 34 additions & 21 deletions benchmark/ensembles.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,31 +15,39 @@ function ensemble_benchmark(f, parallel, nreplicates)
whensteps = 50

function genmodels(nreplicates)
basemodels = [Models.schelling(; numagents)[1]
for numagents in collect(numagents_low:numagents_high)]
basemodels = [
Models.schelling(; numagents)[1] for
numagents in collect(numagents_low:numagents_high)
]

return repeat(basemodels, nreplicates)
end

if f == ensemblerun!
models = genmodels(nreplicates)
adf, mdf, _ = ensemblerun!(models, schelling_agent_step!, dummystep, nsteps;
parallel, adata = [:pos, :mood, :group],
showprogress = true,
when = (model, step) ->
( (step) % whensteps == 0 || step == 0 ),
mdata = [:min_to_be_happy])
adf, mdf, _ = ensemblerun!(
models,
schelling_agent_step!,
dummystep,
nsteps;
parallel,
adata=[:pos, :mood, :group],
showprogress=true,
when=(model, step) -> ((step) % whensteps == 0 || step == 0),
mdata=[:min_to_be_happy],
)
else
# TODO: Why do we need `replicate_idx` here?
# Can't we just use the `Models.schelling`?
function initialize(;
replicate_idx = 1, numagents = 320, griddims = (20, 20), min_to_be_happy = 3
replicate_idx=1, numagents=320, griddims=(20, 20), min_to_be_happy=3
)
space = GridSpace(griddims, periodic = false)
space = GridSpace(griddims; periodic=false)
properties = Dict(:min_to_be_happy => min_to_be_happy)

model = ABM(SchellingAgent, space;
properties = properties, scheduler = Schedulers.randomly)
model = ABM(
SchellingAgent, space; properties=properties, scheduler=Schedulers.randomly
)

for n in 1:numagents
agent = SchellingAgent(n, (1, 1), false, n < numagents / 2 ? 1 : 2)
Expand All @@ -54,13 +62,17 @@ function ensemble_benchmark(f, parallel, nreplicates)
:replicate_idx => collect(1:nreplicates),
:griddims => (20, 20),
)
paramscan(parameters, initialize;
parallel, adata = [:pos, :mood, :group],
mdata = [:min_to_be_happy], showprogress = true,
agent_step! = schelling_agent_step!,
when = (model, step) ->
( (step) % whensteps == 0 || step == 0 ),
n = nsteps)
paramscan(
parameters,
initialize;
parallel,
adata=[:pos, :mood, :group],
mdata=[:min_to_be_happy],
showprogress=true,
(agent_step!)=schelling_agent_step!,
when=(model, step) -> ((step) % whensteps == 0 || step == 0),
n=nsteps,
)
end
end

Expand All @@ -72,7 +84,8 @@ for (f, parallel, nreplicates, name) in [
(paramscan, false, 10, "paramscan serial 10 reps"),
(paramscan, true, 10, "paramscan parallel 10 reps"),
(paramscan, false, 20, "paramscan serial 20 reps"),
(paramscan, true, 20, "paramscan parallel 20 reps")
(paramscan, true, 20, "paramscan parallel 20 reps"),
]
ENSEMBLES_SUITE[name] = @benchmarkable ensemble_benchmark($f, $parallel, $nreplicates) samples = 1
ENSEMBLES_SUITE[name] = @benchmarkable ensemble_benchmark($f, $parallel, $nreplicates) samples =
1
end
Loading

0 comments on commit 5efcf99

Please sign in to comment.