Skip to content

Commit 4e1bf10

Browse files
authored
New function: replicate! (#830)
1 parent 6902229 commit 4e1bf10

File tree

4 files changed

+52
-2
lines changed

4 files changed

+52
-2
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# main
22

3+
- New function `replicate!` allows to create a new instance of a given agent at the same position with the possibility to specify some
4+
fields with new values.
5+
- The `sample!` function is 3x faster than before.
6+
37
# v5.16
48

59
- New function `offline_run!` allows writing data to file at predefined intervals during `run!` instead of storing it in memory. Currently supports [CSV](https://csv.juliadata.org/stable/) and [Arrow](https://apache.github.io/arrow-julia/stable/) files.

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "Agents"
22
uuid = "46ada45e-f475-11e8-01d0-f70cc89e6671"
33
authors = ["George Datseris", "Tim DuBois", "Aayush Sabharwal", "Ali Vahdati", "Adriano Meligrana"]
4-
version = "5.16.0"
4+
version = "5.17.0"
55

66
[deps]
77
CSV = "336ed68f-0bac-5ca0-87d4-7b16caf5d00b"

src/simulations/sample.jl

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export sample!
1+
export sample!, replicate!
22
using StatsBase: sample, Weights
33

44
"""
@@ -55,3 +55,38 @@ function add_newids!(model, org_ids, newids)
5555
end
5656
end
5757
end
58+
59+
"""
60+
replicate!(agent, model; kwargs...)
61+
62+
Create a new agent at the same position of the given agent, copying the values
63+
of its fields. With the `kwargs` it is possible to override the values by specifying
64+
new ones for some fields.
65+
Return the new agent instance.
66+
67+
## Example
68+
```julia
69+
using Agents
70+
@agent A GridAgent{2} begin
71+
k::Float64
72+
w::Float64
73+
end
74+
75+
model = ABM(A, GridSpace((5, 5)))
76+
a = A(1, (2, 2), 0.5, 0.5)
77+
b = replicate!(a, model; w = 0.8)
78+
```
79+
"""
80+
function replicate!(agent, model; kwargs...)
81+
newagent = deepcopy(agent)
82+
for (key, value) in kwargs
83+
setfield!(newagent, key, value)
84+
end
85+
newagent.id = nextid(model)
86+
add_agent_pos!(newagent, model)
87+
return newagent
88+
end
89+
90+
function Base.deepcopy(agent::A) where {A<:AbstractAgent}
91+
return A((deepcopy(getfield(agent, name)) for name in fieldnames(A))...)
92+
end

test/api_tests.jl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,3 +281,14 @@ end
281281
@test idx_second_filtered[15] == (7, 2)
282282
@test idx_second_filtered[end] == (9, 10)
283283
end
284+
285+
@testset "replicate!" begin
286+
model = ABM(Agent8, ContinuousSpace((5, 5)))
287+
a = Agent8(1, (2.0, 2.0), true, 1)
288+
b = replicate!(a, model)
289+
@test b.pos == a.pos && b.f1 == a.f1 && b.f2 == a.f2
290+
c = replicate!(a, model; f2 = 2)
291+
@test c.pos == a.pos && c.f1 == a.f1 && c.f2 == 2
292+
d = replicate!(a, model; f1 = false, f2 = 2)
293+
@test d.pos == a.pos && d.f1 == false && d.f2 == 2
294+
end

0 commit comments

Comments
 (0)